ISO Modula-2 standard I/O discussion
The structure of this collection of modules is not readily apparent thus we willdiscuss the organization of the library here.
The input/output library allows for the reading and writing of data streams overone or more "channels". Channels are connected to sources of input data,or to destinations of output data, known a "devices" or "device instances".There is a separation between modules that are concerned with device-independentoperations, such as reading and writing, and modules concerned with device-dependentoperations, such as making connections to named files. This separation allows thelibrary to be extended to work with new devices. The module structure is as follows.
Input/Output on a given channel.
|IOResult||results of a Read operation|
|TextIO||Characters and strings|
|WholeIO||Whole CARDINAL and INTEGER numbers as text.|
|RealIO||REAL numbers as text.|
|LongIO||LONGREAL numbers as text.|
|RawIO||Any value in binary form, as ARRAY OF SYSTEM.LOC.|
|IOChan||Device-independent interface to channels.|
|IOConsts||Constants for IO modules.|
|LWholeIO||Whole LONGINT numbers as text. Stony Brook extension.|
|ComplexIO||COMPLEX numbers as text. Stony Brook extension.|
|LongComplexIO||LONGCOMPLEX numbers as text. Stony Brook extension.|
Device dependent operations.
|StdChans||Standard and default I/O channels.|
|SeqFile||Rewindable, restart from beginning, sequential streams.|
|RndFile||Random access streams.|
|TermFile||Channels to the terminal/console.|
|ProgramArgs||Channel to read the program command arguments.|
|IOLink||Link between channels and new devices.|
|ChanConsts||Constants for device modules.|
"Simple" IO modules. No channel parameter given. Usescurrent default from StdChans.
|STextIO||TextIO using the current default channels.|
|SWholeIO||WholeIO using the current default channels.|
|SRealIO||RealIO using the current default channels.|
|SLongIO||LongIO using the current default channels.|
|SRawIO||RawIO using the current default channels.|
|SIOResult||Read results for current default input channel.|
|SLWholeIO||LWholeIO using the current default channels. Stony Brook extension.|
|SComplexIO||ComplexIO using the current default channels. Stony Brook extension.|
|SLongComplexIO||LongComplexIO using the current default channels. Stony Brook extension.|
Channels already open to standard sources and destinations can be identified usingprocedures provided by the module StdChans. This module also provides for the selectionof channels used by default for input and output operations.
Text operations produce or consume data streams as sequences of characters andline markers. Raw operations produce or consume data streams as sequences of storagelocations (i.e. as array whose component type is SYSTEM.LOC).
The library allows devices to support both text and raw operations on a singlechannel, although this behavior is not required.
The module IOResult provides the facility for a program to determine whether thelast operation to read data from a specified input channel found data in the requiredformat.
The module IOConsts defines types and constants used by IOResult and SIOResult.
The device modules StreamFile, SeqFile, RndFile and TermFile provide facilitiesthat allow a channel to be opened to a named stream, to a rewindable sequential file,to a random access file, or to a terminal device respectively. The device moduleProgramArgs provides an open channel from which program arguments may be read. Devicespecific operations, such as positioning within a random access file, are also definedby the appropriate device module.
The module IOChan defines general inout/output library exception values that maybe raised when using any device through a channel. Device errors, such as a hardwareread/write error, are reported by raising one of the general exception values, andproviding an implementation defined error number. Exception values associated withdevice specific operations are defined by the appropriate device module.
The module IOLink provides facilities that allow a user to provide further specializeddevice modules for use with channels, following the pattern of the rest of the library.
Standard channels do not have to be opened by a client program since they arealready open and ready for use.
No method is provided for closing a standard channel, and the values used to identifystandard channels are constant throughout the execution of the program.
Note: Most operating systems provide ways to redirect the standard handlesthemselves, possibly changing the standard handle value, so the above statement canbe broken.
Default channels are channels whose identities have been stored as those to beused by default for input and output operations. Initially these correspond to thestandard channels, but their values may be varied to obtain the effect of redirection.
The module StdChans defines functions that identify channels already open to implementation-defined sources and destinations of standard input, standard output and standard error output. Access to a "null device" is provided to allow unwanted output to be suppressed. The null device throws away all data written to it, and gives an immediate end of input indication on reading.
Using the I/O modules
The I/O modules TextIO, WholeIO, LWholeIO, RealIO, LongIO, ComplexIO, LongComplexIO are all text format (human readable). They perform I/O on a line by line basis. Multiple unique entry items can exist on a single line of input. Items are typically separated by spaces. The module ReadResult returns the status of the previous read API call. Input is processed a line at a time and the end of line will not be consumed until the SkipLine API is used. If the current input is at an end of line position and a read API (like ReadInt) is called the call will immediately return and set the read result value to endOfLine.
When receiving input from the console (keyboard), input is not accepted until the Enter key is pressed thus completing a single line of input. When receiving input from a file, input is read on a line by line basis. In this way reading input from the console or a file operates the same.
When receiving input from a file it is possible for read API calls to set the read result to endOfInput signifying the end of the file has been reached.
The "S" or simple I/O modules operate on the standard I/O channels. At program startup these are initialized to the standard I/O channels of the host operating system. Some operating systems do not have standard I/O channels. In these cases the standard I/O will be emulated. While an operating system can redirect standard I/O to sources other than the console, the ISO Modula-2 I/O system also gives you program control over redirection of the standard I/O channels. This control comes from the StdChans module.
ISO command line arguments
The module ProgramArgs allows you to read the implementation dependent programarguments. i.e. The command line. The simplest way to do this is
WHILE ProgramArgs.ArgIsPresent() DO
Arguments are separated by one or more spaces, as this is the way TextIO.ReadTokenoperates. If you want to re-read the arguments you can do the following IOChan.Reset(ProgramArgs.ArgChan()).