DEFINITON FileBuffer; (* SJ *)

(* buffered file system *)

IMPORT S:=SYSTEM;

CONST (* modi for Seek *)
  absbeg* = 0; relative* = 1; absend* = 2;


TYPE
  file*    = POINTER TO filedesc;

(* this is a pointer to an info structure used as a handle *)

TYPE
  filedesc*= RECORD
               Pos-    : LONGINT; (* current file position *)
               Length* : LONGINT; (* current length while writing, whole
                                     length while reading *)
               Ok* : BOOLEAN; (* FALSE : eof while reading or write error *)
             END;


PROCEDURE Load*(Name : ARRAY OF CHAR; VAR f : file) : BOOLEAN;

(* loads file Name with File.Load and returns a handle. FALSE is returned
   if the file is not found.
*)

PROCEDURE ReadBytes*(f : file; NumBytes : LONGINT; VAR Bytes : ARRAY OF S.BYTE);

(* returns the desired amount of bytes *)


PROCEDURE ReadChar*(f : file; VAR Char : CHAR);

(* reads one character *)


PROCEDURE ReadByte*(f : file; VAR Byte : SHORTINT);

(* reads one byte *)


PROCEDURE ReadWord*(f : file; VAR Word : INTEGER);

(* reads one word (16 bit) *)


PROCEDURE ReadLong*(f : file; VAR Long : LONGINT);

(* reads one long word (32 bit) *)


PROCEDURE ReadString*(f : file; VAR String : ARRAY OF CHAR);

(* reads characters until 0X is read *)


PROCEDURE Open*(Name : ARRAY OF CHAR; BufSize : LONGINT; VAR f : file) : BOOLEAN;

(* returns a handle to be used for writing into a buffer with size BufSize *)


PROCEDURE Close*(f : file);

(* saves file with handle f *)


PROCEDURE WriteBytes*(f : file; NumBytes : LONGINT; VAR Bytes : ARRAY OF S.BYTE);

(* returns the desired amount of bytes *)


PROCEDURE WriteChar*(f : file; Char : CHAR);

(* writes one character *)


PROCEDURE WriteByte*(f : file; Byte : SHORTINT);
(* writes one byte *)


PROCEDURE WriteWord*(f : file; Word : INTEGER);

(* writes one word (16 bit) *)


PROCEDURE WriteLong*(f : file; Long : LONGINT);

(* writes one long word (32 bit) *)


PROCEDURE WriteString*(f : file; String : ARRAY OF CHAR);

(* writes string including 0X *)


PROCEDURE WriteChars*(f : file; String : ARRAY OF CHAR);

(* writes string not including 0X *)


PROCEDURE Seek*(f : file; Mode : INTEGER; Bytes : LONGINT);

(* sets the file pointer to the desired position. Can be used for read without
   restriction and for write, if the desired position is still in the buffer, so
   make it big enough.
*)

END FileBuffer.