DEFINITION Streams; (* portable *)

 IMPORT SYSTEM, Files;

 CONST
  closed = 0; error = 1; in = 2; out = 3; inout = 4;
  binary = 0; oberon = 1; iso8859 = 2;

 TYPE
  Stream = POINTER TO StreamDesc;
  StreamDesc = RECORD
   ReadBytes, WriteBytes: PROCEDURE (S: Stream; VAR x: ARRAY OF SYSTEM.BYTE; n: LONGINT);
   Available: PROCEDURE (S: Stream): LONGINT; (* returns the number of bytes
which may read without blocking *)
   State: PROCEDURE (S: Stream): INTEGER; (* closed, error, in, out, inout
*)
   Flush: PROCEDURE (S: Stream); (* flush all buffers of the stream *)
   Close: PROCEDURE (S: Stream); (* close the stream *)
   res: LONGINT; (* bytes requested but not read/written *)
   mode: INTEGER; (* binary, oberon, iso8859 *)
   eos, buffer: BOOLEAN (* set, when either the end of the stream is reached
or the stream is closed. *)
  END;

  FileStream = POINTER TO FileStreamDesc;

 VAR 
  NULL: Stream; (* a stream which does nothing *)

(* on any streams *)
 PROCEDURE Read (S: Stream; VAR x: CHAR);
 PROCEDURE Write (S: Stream; x: CHAR);

(* on binary streams *)
(* Portable routines to write and read the standard Oberon types. Each procedure
is provided in a read and write pair. *)
 PROCEDURE ReadBool (S: Stream; VAR x: BOOLEAN);
 PROCEDURE ReadInt (S: Stream; VAR x: INTEGER);
 PROCEDURE ReadLInt (S: Stream; VAR x: LONGINT);
 PROCEDURE ReadLReal (S: Stream; VAR x: LONGREAL);
 PROCEDURE ReadNum (S: Stream; VAR x: LONGINT);
 PROCEDURE ReadReal (S: Stream; VAR x: REAL);
 PROCEDURE ReadSet (S: Stream; VAR x: SET);
 PROCEDURE ReadString (S: Stream; VAR x: ARRAY OF CHAR);
 PROCEDURE WriteBool (S: Stream; x: BOOLEAN);
 PROCEDURE WriteInt (S: Stream; x: INTEGER);
 PROCEDURE WriteLInt (S: Stream; x: LONGINT);
 PROCEDURE WriteLReal (S: Stream; x: LONGREAL);
 PROCEDURE WriteNum (S: Stream; x: LONGINT);
 PROCEDURE WriteReal (S: Stream; x: REAL);
 PROCEDURE WriteSet (S: Stream; x: SET);
 PROCEDURE WriteString (S: Stream; x: ARRAY OF CHAR);

(* Opens a stream reading on file F starting at postion pos. *)
 PROCEDURE OpenFileReader (F: Files.File; pos: LONGINT): Stream;

(* Opens a stream writeing on file F starting at postion pos. *)
 PROCEDURE OpenFileWriter (F: Files.File; pos: LONGINT): Stream;
 PROCEDURE FilePos (S: FileStream): LONGINT;
 PROCEDURE Copy (src, dest: Stream);

END Streams.