DEFINITION MODULE FileSystem;

(* (C) Copyright 1987 Fitted Software Tools. All rights reserved. *)

(*
    This module provides routines for file handling similar to the ones
    described in the book "Programming in Modula-2" by Niklaus Wirth.
*)

FROM SYSTEM IMPORT WORD, ADDRESS;

TYPE
    FDPtr;  (* for internal use only *)

    Response = ( done, notdone );
    IOMode   = ( read, write, io );
    File = RECORD
        id      :INTEGER;
        res     :Response;
        eof     :BOOLEAN;
        mode    :IOMode;
        fdptr   :FDPtr;     (* for internal use only *)
    END;

(*
    All the procedures will set File.res to 'done' if the call succeeds
    and to 'notdone' otherwise.

    File.eof is TRUE after a new file is created or when one of the Read
    operations is attempted and there is no more data to read.
*)


PROCEDURE Lookup( VAR f :File; filename :ARRAY OF CHAR; new :BOOLEAN );
(*
    Open the file named in filename.
    IF the file does not exist THEN
        IF new THEN create a new file
        ELSE fail

    Lookup always tries to open the file for io first; if that fails,
    it tries to open the file for reading.
    f.mode will be set according to how the file was opened.
*)

PROCEDURE Create( VAR f :File; mediumname :ARRAY OF CHAR );
(*
    Create a new temporary file.
    If mediumname matches a variable in the environment, the
    value of that variable is assumed to be the path to where
    the file is to be created.
*)

PROCEDURE Close( VAR f :File );
(*
    close the file.
    if the file was Rename'd, the directory entry is modified
    at this time; if necessary (the file was renamed to a different
    drive), the file is copied!
*)

PROCEDURE Reset( VAR f :File );
(*
    Set the file pointer to the beginning of the file.
*)

PROCEDURE Rewrite( VAR f :File );
(*
    Set the file pointer to the beginning of the file and truncate
    the file (make it empty).
*)

PROCEDURE Rename( VAR f :File; filename :ARRAY OF CHAR );
(*
    change the name of the file f to filename.
    a file can be Rename'd (moved) to a different drive and/or pathname.
    NOTE: the directory entry is not modified until the file is closed.
*)

PROCEDURE ReadWord( VAR f :File; VAR w :WORD );
(*
    read a WORD from the file
*)

PROCEDURE WriteWord( VAR f :File; w :WORD );
(*
    write a WORD to the file
*)

PROCEDURE ReadChar( VAR f :File; VAR ch :CHAR );
(*
    read a character from the file.
    the character ASCII.CR is converted to ASCII.EOL and ASCII.LF is ignored.
*)

PROCEDURE WriteChar( VAR f :File; ch :CHAR );
(*
    write the character ch to the file.
    ASCII.EOL is converted to the sequence ASCII.CR ASCII.LF.
*)

PROCEDURE GetPos( VAR f :File; VAR highpos, lowpos :CARDINAL );
(*
    return the current position of the file pointer
*)

PROCEDURE SetPos( VAR f :File; highpos, lowpos :CARDINAL );
(*
    move the file pointer to position highpos*65536+lowpos
*)

PROCEDURE GetLPos( VAR f :File; VAR pos :LONGCARD );
(*
    return the current position of the file pointer
*)

PROCEDURE SetLPos( VAR f :File; pos :LONGCARD );
(*
    move the file pointer to pos.
*)

PROCEDURE Length( VAR f :File; VAR highlen, lowlen :CARDINAL );
(*
    return the size of the file
*)

PROCEDURE LLength( VAR f :File; VAR length :LONGCARD );
(*
    return the size of the file
*)

PROCEDURE ReadNBytes( VAR f :File; buffPtr :ADDRESS; n :CARDINAL;
                      VAR nRead :CARDINAL );
(*
    try to read n number of bytes.
    On return, nRead is the actual number of bytes read.
*)

PROCEDURE WriteNBytes( VAR f :File; buffPtr :ADDRESS; n :CARDINAL;
                       VAR nWritten :CARDINAL );
(*
    write n bytes to the file.
    On return nWritten is the actual number of bytes that were written.
*)

END FileSystem.
