Module FileIO

In order to get a better way of accessing files I wrote the module FileIO. Here is the definition module:

DEFINITION MODULE FileIo;

FROM FileSystem     IMPORT File;

PROCEDURE ReadString   (device      : File;
                        VAR String  : ARRAY OF CHAR;
                        delimiter   : CHAR  );
(*  Read a String from file <device> until a <delimiter> char is found  *)

PROCEDURE WriteString  (device      : File;
                        String      : ARRAY OF CHAR;
                        delimiter   : CHAR  );
(*  Write a String to file <device> and add the specified <delimiter>.  *)

PROCEDURE ReadCard     (device      : File;
                        VAR card    : CARDINAL  ) : BOOLEAN;

(*  Read a CARDINAL as text from file <device> and store in <card>      *)

PROCEDURE WriteCard    (device      : File;
                        card        : CARDINAL;
                        precision   : CARDINAL;
                        delimiter   : CHAR      );
(*  Write a CARDINAL to file <device>       *)

END FileIo.
   
Just four procedures but they will read a device until one specific token is encountered.

And this is the implementation module:
IMPLEMENTATION MODULE FileIo;

FROM ASCII              IMPORT  EOL;
FROM FileSystem         IMPORT  File, ReadChar, WriteChar;
FROM NumberConversion   IMPORT  StringToCard, CardToString;

PROCEDURE ReadString (device        : File;
                      VAR String    : ARRAY OF CHAR;
                      delimiter     : CHAR  );
VAR char    : CHAR;
    index   : CARDINAL;

BEGIN
    index := 0;
    REPEAT
        ReadChar (device, char);
        String [index] := char;
        INC (index);
    UNTIL char = delimiter;
    DEC (index);
    String [index] := 0C;
END ReadString;

PROCEDURE WriteString  (device      : File;
                        String      : ARRAY OF CHAR;
                        delimiter   : CHAR  );
VAR index,
    Max     : CARDINAL;
    Char    : CHAR;

BEGIN
    index := 0;
    Max := HIGH (String);
    Char := String [index];
    WHILE (Char # 0C) AND (index <= Max) DO
        WriteChar (device, Char);
        INC (index);
        IF index <= Max THEN
            Char := String [index];
        END;
    END;
    WriteChar (device, delimiter);
END WriteString;

PROCEDURE ReadCard (device : File; VAR card : CARDINAL) : BOOLEAN;

VAR char    : CHAR;
    index   : CARDINAL;
    storage : ARRAY [0..7] OF CHAR;
    ok      : BOOLEAN;

BEGIN
    index := 0;
    LOOP
        ReadChar (device, char);
        CASE char OF
            "0".."9"      : storage [index] := char;
               |
            ",", EOL, " " : ok := TRUE; EXIT;
        ELSE
            ok := FALSE; EXIT;
        END;
        INC (index);
    END;
    storage [index] := 0C;
    IF ok THEN
        StringToCard (storage, card, ok);
    END;
    RETURN ok;
END ReadCard;

PROCEDURE WriteCard    (device      : File;
                        card        : CARDINAL;
                        precision   : CARDINAL;
                        delimiter   : CHAR      );

VAR storage     : ARRAY [0..9] OF CHAR;

BEGIN
    CardToString (card, storage, precision);
    WriteString (device, storage, delimiter);
END WriteCard;

END FileIo.
   

Page created September 9, 2012 and