ISO M2 I/O-Lib test file: TestExam.MOD 
Author Message
 ISO M2 I/O-Lib test file: TestExam.MOD

MODULE TestExample;
(*
  This module illustrates the use of the ISO Modula-2 Standard I/O-Library for
  creating, writing, reading, rewriting, and appending to/from a text file.
  Also, the just created text file is opened as binary random file
  and read forward and backward using the positioning procedures of RndFile.
  When reading backards there is no end-of-file detection (better called
  start-of-file). This is why the program terminates with an unhandled
  exception.
  The console output of this program is self-explaining and is listed
  in the associated program's description message (also availbale on
  CompuServe's CodePort forum, Lib 11 "ISO Modula-2").

  Written by Elmar Baumgart/11-Mar-1992

  (c) Copyright (1992) by G~unter Dotzel,
  ModulaWare GmbH, Wilhelmstr. 17A, D-W 8520 Erlangen/F.R.Germany
  Tel. +49 (9131) 208395, Fax +49 (9131) 28205.

*)

                      (* to make a channel, import *)
FROM SeqFile IMPORT
  OpenAppend, OpenWrite, OpenRead, Close, OpenResults, ChanId,
  Rewrite, Reread, text, binary, read, write;
IMPORT RndFile;
FROM RndFile IMPORT OpenOld, FilePos, EndPos, NewPos, SetPos, CurrentPos;

                      (* to get the terminal channels, import *)
FROM StdChans IMPORT InChan, OutChan;

                      (* to get status information on channels, import *)
FROM IOResult IMPORT ReadResult, ReadResults;

                      (* to send and receive data along channels, import *)
FROM TextIO IMPORT WriteString, ReadRestLine, WriteLn, SkipLine, WriteChar;
FROM RawIO IMPORT Read;

FROM CharClass IMPORT IsControl; (*information on characters*)
FROM SYSTEM IMPORT TSIZE; (*size of type in bytes, needed for NewPos*)

CONST
  filename = 'TESTFILE.LIS';(* feel free to use another name *)
  string10 = '  10 times foo';
  string5  = '  and now 5 times bar';
  string7  = '  makes 7 lines total';

PROCEDURE DumpFile(f: ChanId);
VAR line: ARRAY[0..255] OF CHAR;
BEGIN
  ReadRestLine(f, line); SkipLine(f);

  (* note: no EOL-character can be read. End-of-line is detected using
           ReadResult(f) = endOfLine. To get the next line you have to
           SkipLine(f). Never test for or use CR/LF or something else,
           because this wouldn't be portable.
  *)
  WHILE ReadResult(f) <> endOfInput DO
    WriteString(OutChan(), line); WriteLn(OutChan());
    ReadRestLine(f, line); SkipLine(f);
  END;
END DumpFile;

VAR
  f: ChanId;
  Oresult: OpenResults;
  Rresult: ReadResults;
  i: INTEGER;
  ch: CHAR;

BEGIN
  WriteString(OutChan(), 'small test program'); WriteLn(OutChan());

  (* note: you can access InChan() and OutChan() implicitly using the
           procedures defined in S*IO - modules. 'S' means 'simple'.
           These procedures have no channel - parameter.
           e.g.: STextIO.WriteString('terminal-output');
  *)

                        (*open a new file for reading and writing text*)
  OpenWrite(f, filename, read + write + text, Oresult);
  (*opens the file in writemode!*)
  IF Oresult = opened THEN

    (*writing to a file:*)
    FOR i:= 1 TO 10 DO WriteString(f, string10); WriteLn(f);
    END;

    (*reading from the file:*)
    WriteString(OutChan(), 'initial file status:'); WriteLn(OutChan());
    Reread(f); (*toggle to readmode and reset the fileposition*)
    DumpFile(f);

    (*note: a seqfile can be in either readmode or writemode. To toggle these
            modes use Reread and Rewrite. Writing to a file in readmode
            causes an exception and vice versa.
    *)

    Rewrite(f); (*toggle to writemode an clear the file*)
    FOR i:= 1 TO 5 DO WriteString(f, string5); WriteLn(f);
    END;
    WriteString(OutChan(), 'status after Rewrite:'); WriteLn(OutChan());
    Reread(f);
    DumpFile(f);
    Close(f);
                                   (*open old file in writemode at eof*)
    OpenAppend(f, filename, read + write + text, Oresult);
    FOR i:= 1 TO 2 DO WriteString(f, string7); WriteLn(f);
    END;
    WriteString(OutChan(), 'status after OpenAppend:'); WriteLn(OutChan());
    Reread(f);
    DumpFile(f);
    Close(f);
                                   (*open old file in readmode at start*)
    WriteString(OutChan(), 'now reading binary :-)'); WriteLn(OutChan());
    OpenRead(f, filename, binary, Oresult);

    Read(f, ch);
    WHILE ReadResult(f) <> endOfInput DO
      IF IsControl(ch) THEN
        WriteLn(OutChan());
        WHILE IsControl(ch) AND (ReadResult(f) <> endOfInput) DO Read(f, ch);
        END;
        IF ReadResult(f) <> endOfInput THEN WriteChar(OutChan(), ch);
        END;
      ELSE WriteChar(OutChan(), ch);
      END;
      Read(f, ch);
    END;
    Close(f);

                                        (* read in reverse order *)
                                        (* uses random access !!!*)
    WriteString(OutChan(), 'now reading from eof to start'); WriteLn(OutChan());
    RndFile.OpenOld(f, filename, read + binary, Oresult);
    SetPos(f, NewPos(f, -1, TSIZE(CHAR), EndPos(f)));

    Read(f, ch);
    SetPos(f, NewPos(f, -2, TSIZE(CHAR), CurrentPos(f)));
    WHILE TRUE DO
      IF IsControl(ch) THEN
        WriteLn(OutChan());
        WHILE IsControl(ch) DO
          Read(f, ch);
          SetPos(f, NewPos(f, -2, TSIZE(CHAR), CurrentPos(f)));
        END;
        WriteChar(OutChan(), ch);
      ELSE WriteChar(OutChan(), ch);
      END;
      Read(f, ch);
      SetPos(f, NewPos(f, -2, TSIZE(CHAR), CurrentPos(f)));
    END;
    (* terminates with an exception, tries to setpos in front of start*)
  ELSE
    WriteString(OutChan(), 'file not opened, sorry');
    WriteLn(OutChan());
  END;
END TestExample.



Fri, 09 Sep 1994 15:00:06 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. ISO M2 I/O-Lib test file: TestPos.MOD

2. ISO M2 Std Lib Defs .ARC

3. ISO M2 Std Lib Defs .ARC

4. ISO M2 COMPLEX-number constructor test

5. ISO Modula-2 Standard I/O-Lib test progr

6. Oberon-2 random file access w ISO M2 Std

7. check digit ISO 7064 MOD 11, 10

8. Editing LIB Files to Change Functions Within LIB Files

9. DIV, MOD, /, REM (M2,O2)

10. IOTRANSFER: why was it modified in ISO M2?

11. Status of M2 ISO Standard

12. ISO M2 Library for Oberon-2

 

 
Powered by phpBB® Forum Software