IMPLEMENTATION MODULE FileSystem ;


FROM SYSTEM IMPORT ADDRESS, ADR, TSIZE ;
FROM StrIO IMPORT WriteString, WriteLn ;

IMPORT FIO ;


PROCEDURE Lookup (VAR f: File ; filename: ARRAY OF CHAR ; Create: BOOLEAN) ;
BEGIN
   WITH f DO
      IF Create
      THEN
         io := write
      ELSE
         io := read
      END ;
      IF io=read
      THEN
         in := FIO.OpenToRead(filename) ;
         IF FIO.IsNoError(in)
         THEN
            Opened := TRUE ;
            res := done
         ELSE
            Opened := FALSE ;
            res := notdone
         END
      ELSE
         out := FIO.OpenToWrite(filename) ;
         IF FIO.IsNoError(out)
         THEN
            Opened := TRUE ;
            res := done
         ELSE
            Opened := FALSE ;
            res := notdone
         END
      END
   END
END Lookup ;


PROCEDURE Reset (VAR f: File) ;
BEGIN
END Reset ;


PROCEDURE ReadChar (VAR f: File ; VAR ch: CHAR) ;
BEGIN
   WITH f DO
      IF io=read
      THEN
         ch := FIO.ReadChar(in) ;
         IF FIO.IsNoError(in)
         THEN
            res := done
         ELSE
            res := notdone
         END
      ELSE
         WriteString('File Opened for Write') ; WriteLn ;
      END
   END
END ReadChar ;


PROCEDURE WriteChar (VAR f: File ; ch: CHAR) ;
BEGIN
   WITH f DO
      IF io=write
      THEN
         FIO.WriteChar(out, ch) ;
         IF FIO.IsNoError(out)
         THEN
            res := done
         ELSE
            res := notdone
         END
      ELSE
         WriteString('File Opened for Read') ; WriteLn ;
      END
   END
END WriteChar ;


PROCEDURE Close (VAR f: File) ;
BEGIN
   WITH f DO
      IF io=read
      THEN
         IF Opened
         THEN
            FIO.Close(in) ;
            res := done
         ELSE
            res := notdone
         END
      ELSE
         IF Opened
         THEN
            FIO.Close(out) ;
            res := done
         ELSE
            res := notdone
         END
      END
   END
END Close ;


PROCEDURE WriteStruct (a: ADDRESS ; size: CARDINAL) ;
VAR
   i : CARDINAL ;
BEGIN
   WriteString('Not Implemented Yet') ;
   HALT
(*
   FOR i := 1 TO (size+1)*TSIZE(WORD) DO
      BinWrch( CHAR( a^ ) ) ;
      INC( a )
   END ;
   FlushOut
*)
END WriteStruct ;


PROCEDURE ReadStruct (a: ADDRESS ; size: CARDINAL) ;
VAR
   i : CARDINAL ;
BEGIN
   WriteString('Not Implemented Yet') ;
   HALT
(*
   FOR i := 1 TO (size+1)*TSIZE(WORD) DO
      a^ := WORD( BinRdch() ) ;
      INC( a )
   END
*)
END ReadStruct ;


PROCEDURE WriteStructToFile (VAR f: File ; a: ARRAY OF WORD) ;
BEGIN
   WriteString('Not Implemented Yet') ;
   HALT

(*
   WITH f^ DO
      IF io=write
      THEN
         s := Output() ;
         SelectOutput( out ) ;
         WriteStruct( ADR( a ), HIGH( a ) ) ;
         IF ErrorCode=Success
         THEN
            res := done
         ELSE
            res := notdone
         END ;
         SelectOutput( s )
      ELSE
         WriteString('File Opened for Read') ; WriteLn
      END
   END
*)
END WriteStructToFile ;


PROCEDURE WriteStructToDefault (a: ARRAY OF WORD) ;
BEGIN
   WriteStruct( ADR( a ), HIGH( a ) ) ;
END WriteStructToDefault ;


PROCEDURE ReadStructFromFile (VAR f: File ; VAR a: ARRAY OF WORD) ;
BEGIN
   WriteString('Not Implemented Yet') ;
   HALT
(*
   WITH f^ DO
      IF io=read
      THEN
         s := Input() ;
         SelectInput( in ) ;
         ReadStruct( ADR( a ), HIGH( a ) ) ;
         IF ErrorCode=Success
         THEN
            res := done
         ELSE
            res := notdone
         END ;
         SelectInput( s )
      ELSE
         WriteString('File Opened for Write') ; WriteLn
      END
   END
*)
END ReadStructFromFile ;


PROCEDURE ReadStructFromDefault (VAR a: ARRAY OF WORD) ;
BEGIN
   ReadStruct( ADR( a ), HIGH( a ) )
END ReadStructFromDefault ;


END FileSystem.