2014-06-04 17:15:52 +00:00
|
|
|
|
2014-05-30 03:15:40 +00:00
|
|
|
with H2.Pool;
|
2014-06-04 17:15:52 +00:00
|
|
|
with H2.Sysdef;
|
2014-05-30 03:15:40 +00:00
|
|
|
|
2014-06-05 15:26:37 +00:00
|
|
|
separate (H2.OS)
|
2014-05-30 03:15:40 +00:00
|
|
|
|
|
|
|
package body File is
|
|
|
|
|
2014-06-04 17:15:52 +00:00
|
|
|
-- External functions and procedures
|
|
|
|
function Sys_Open (path: Slim_String; flags: Sysdef.int_t; mode: Sysdef.int_t) return Sysdef.int_t;
|
|
|
|
pragma Import (C, Sys_Open, "open");
|
2014-05-30 03:15:40 +00:00
|
|
|
|
2014-06-04 17:15:52 +00:00
|
|
|
procedure Sys_Close (fd: Sysdef.int_t);
|
2014-05-30 03:15:40 +00:00
|
|
|
pragma Import (C, sys_close, "close");
|
|
|
|
|
2014-06-04 17:15:52 +00:00
|
|
|
function Sys_Read (fd: Sysdef.int_t; buf: in System.Address; count: in Sysdef.size_t) return Sysdef.ssize_t;
|
|
|
|
pragma Import (C, Sys_Read, "read");
|
|
|
|
|
|
|
|
function Sys_Write (fd: Sysdef.int_t; buf: in System.Address; count: in Sysdef.size_t) return Sysdef.ssize_t;
|
|
|
|
pragma Import (C, Sys_Write, "write");
|
|
|
|
|
|
|
|
-- Common constants
|
|
|
|
INVALID_HANDLE: constant := -1;
|
|
|
|
ERROR_RETURN: constant := -1;
|
2014-06-02 15:25:42 +00:00
|
|
|
|
2014-06-04 17:15:52 +00:00
|
|
|
-- File record
|
2014-05-30 03:15:40 +00:00
|
|
|
type Posix_File_Record is new File_Record with record
|
|
|
|
Pool: Storage_Pool_Pointer := null;
|
2014-06-04 17:15:52 +00:00
|
|
|
Handle: Sysdef.int_t := INVALID_HANDLE;
|
2014-05-30 03:15:40 +00:00
|
|
|
end record;
|
|
|
|
type Posix_File_Pointer is access all Posix_File_Record;
|
|
|
|
|
2014-06-04 17:15:52 +00:00
|
|
|
-- Standard Files
|
|
|
|
Stdin: aliased Posix_File_Record := (null, 0);
|
|
|
|
Stdout: aliased Posix_File_Record := (null, 1);
|
|
|
|
Stderr: aliased Posix_File_Record := (null, 2);
|
|
|
|
|
|
|
|
function Flag_To_System (Bits: in Flag_Bits) return System_Word is
|
|
|
|
V: System_Word := 0;
|
2014-05-30 03:15:40 +00:00
|
|
|
begin
|
2014-06-04 17:15:52 +00:00
|
|
|
if ((Bits and FLAG_READ) /= 0) and then
|
|
|
|
((Bits and FLAG_WRITE) /= 0) then
|
|
|
|
V := V or Sysdef.O_RDWR;
|
|
|
|
elsif ((Bits and FLAG_WRITE) /= 0) then
|
|
|
|
V := V or Sysdef.O_WRONLY;
|
|
|
|
else
|
|
|
|
V := V or Sysdef.O_RDONLY;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
if ((Bits and FLAG_CREATE) /= 0) then
|
|
|
|
V := V or Sysdef.O_CREAT;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
if ((Bits and FLAG_TRUNCATE) /= 0) then
|
|
|
|
V := V or Sysdef.O_TRUNC;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
if ((Bits and FLAG_SYNC) /= 0) then
|
|
|
|
V := V or Sysdef.O_SYNC;
|
|
|
|
end if;
|
2014-06-02 15:25:42 +00:00
|
|
|
|
|
|
|
return V;
|
2014-05-30 03:15:40 +00:00
|
|
|
end Flag_To_System;
|
|
|
|
|
2014-06-04 17:15:52 +00:00
|
|
|
function Get_Stdin return File_Pointer is
|
|
|
|
begin
|
|
|
|
--return File_Pointer'(Stdin'Access);
|
|
|
|
return File_Record(Stdin)'Access;
|
|
|
|
end Get_Stdin;
|
|
|
|
|
|
|
|
function Get_Stdout return File_Pointer is
|
|
|
|
begin
|
|
|
|
--return File_Pointer'(Stdout'Access);
|
|
|
|
return File_Record(Stdout)'Access;
|
|
|
|
end Get_Stdout;
|
|
|
|
|
|
|
|
function Get_Stderr return File_Pointer is
|
|
|
|
begin
|
|
|
|
--return File_Pointer'(Stderr'Access);
|
|
|
|
return File_Record(Stdout)'Access;
|
|
|
|
end Get_Stderr;
|
|
|
|
|
2014-05-30 03:15:40 +00:00
|
|
|
procedure Open (File: out File_Pointer;
|
|
|
|
Name: in Slim_String;
|
2014-06-04 17:15:52 +00:00
|
|
|
Flag: in Flag_Record;
|
|
|
|
Mode: in Mode_Record := DEFAULT_MODE;
|
2014-05-30 03:15:40 +00:00
|
|
|
Pool: in Storage_Pool_Pointer := null) is
|
|
|
|
|
|
|
|
package P is new H2.Pool (Posix_File_Record, Posix_File_Pointer, Pool);
|
|
|
|
F: Posix_File_Pointer;
|
2014-06-02 15:25:42 +00:00
|
|
|
|
2014-05-30 03:15:40 +00:00
|
|
|
begin
|
|
|
|
F := P.Allocate;
|
|
|
|
F.Pool := Pool;
|
|
|
|
|
2014-06-04 17:15:52 +00:00
|
|
|
F.Handle := Sys_Open (Name & Slim_Character'Val(0),
|
|
|
|
Sysdef.int_t(Flag_To_System(Flag.Bits)),
|
|
|
|
Sysdef.int_t(Mode.Bits));
|
|
|
|
if Sysdef."<=" (F.Handle, INVALID_HANDLE) then
|
2014-05-30 03:15:40 +00:00
|
|
|
raise Constraint_Error; -- TODO: raise a proper exception.
|
|
|
|
end if;
|
|
|
|
|
|
|
|
File := File_Pointer(F);
|
|
|
|
end Open;
|
|
|
|
|
|
|
|
procedure Open (File: out File_Pointer;
|
|
|
|
Name: in Wide_String;
|
2014-06-04 17:15:52 +00:00
|
|
|
Flag: in Flag_Record;
|
|
|
|
Mode: in Mode_Record := DEFAULT_MODE;
|
2014-06-02 15:25:42 +00:00
|
|
|
Pool: in Storage_Pool_Pointer := null) is
|
2014-05-30 03:15:40 +00:00
|
|
|
begin
|
|
|
|
Open (File, Wide_To_Slim(Name), Flag, Mode, Pool);
|
|
|
|
end Open;
|
|
|
|
|
|
|
|
procedure Close (File: in out File_Pointer) is
|
2014-06-04 17:15:52 +00:00
|
|
|
F: Posix_File_Pointer := Posix_File_Pointer(File);
|
2014-05-30 03:15:40 +00:00
|
|
|
begin
|
2014-06-04 17:15:52 +00:00
|
|
|
if F /= Stdin'Access and then F /= Stdout'Access and then F /= Stderr'Access then
|
|
|
|
-- Don't close standard files.
|
|
|
|
|
|
|
|
Sys_Close (F.Handle);
|
|
|
|
F.Handle := INVALID_HANDLE;
|
2014-05-30 03:15:40 +00:00
|
|
|
|
2014-06-04 17:15:52 +00:00
|
|
|
declare
|
|
|
|
package P is new H2.Pool (Posix_File_Record, Posix_File_Pointer, F.Pool);
|
|
|
|
begin
|
|
|
|
P.Deallocate (F);
|
|
|
|
end;
|
2014-05-30 03:15:40 +00:00
|
|
|
|
2014-06-04 17:15:52 +00:00
|
|
|
File := null;
|
|
|
|
end if;
|
2014-05-30 03:15:40 +00:00
|
|
|
end Close;
|
|
|
|
|
2014-06-05 15:26:37 +00:00
|
|
|
procedure Read (File: in File_Pointer;
|
|
|
|
Buffer: in out System_Byte_Array;
|
|
|
|
Length: out System_Length) is
|
|
|
|
pragma Assert (Buffer'Length > 0);
|
2014-06-04 17:15:52 +00:00
|
|
|
F: Posix_File_Pointer := Posix_File_Pointer(File);
|
|
|
|
N: Sysdef.ssize_t;
|
|
|
|
begin
|
|
|
|
N := Sys_Read (F.Handle, Buffer'Address, Buffer'Length);
|
|
|
|
if Sysdef."<=" (N, ERROR_RETURN) then
|
|
|
|
raise Constraint_Error; -- TODO rename exception
|
|
|
|
else
|
2014-06-05 15:26:37 +00:00
|
|
|
Length := System_Length(N);
|
2014-06-04 17:15:52 +00:00
|
|
|
end if;
|
|
|
|
end Read;
|
|
|
|
|
2014-06-05 15:26:37 +00:00
|
|
|
procedure Write (File: in File_Pointer;
|
|
|
|
Buffer: in System_Byte_Array;
|
|
|
|
Length: out System_Length) is
|
|
|
|
pragma Assert (Buffer'Length > 0);
|
2014-06-04 17:15:52 +00:00
|
|
|
F: Posix_File_Pointer := Posix_File_Pointer(File);
|
|
|
|
N: Sysdef.ssize_t;
|
|
|
|
begin
|
|
|
|
N := Sys_Write (F.Handle, Buffer'Address, Buffer'Length);
|
|
|
|
if Sysdef."<=" (N, ERROR_RETURN) then
|
|
|
|
raise Constraint_Error; -- TODO rename exception
|
|
|
|
else
|
2014-06-05 15:26:37 +00:00
|
|
|
Length := System_Length(N);
|
2014-06-04 17:15:52 +00:00
|
|
|
end if;
|
2014-06-05 15:26:37 +00:00
|
|
|
|
2014-06-04 17:15:52 +00:00
|
|
|
end Write;
|
|
|
|
|
2014-05-30 03:15:40 +00:00
|
|
|
end File;
|