Oberon || Library || Module Index || Search Engine || Definition || Module


Ulm's Oberon Library:
Read


NAME

Read - formatted input

SYNOPSIS

PROCEDURE ShortInt(VAR shortint: SHORTINT);
PROCEDURE Int(VAR int: INTEGER);
PROCEDURE LongInt(VAR longint: LONGINT);
PROCEDURE Real(VAR real: REAL);
PROCEDURE LongReal(VAR longreal: LONGREAL);
PROCEDURE Char(VAR ch: CHAR);
PROCEDURE Byte(VAR byte: BYTE);
PROCEDURE Line(VAR line: ARRAY OF CHAR);
PROCEDURE Ln;
PROCEDURE String(VAR string: ARRAY OF CHAR);
PROCEDURE Field(VAR string: ARRAY OF CHAR) : BOOLEAN;
PROCEDURE ShortIntS(s: Streams.Stream; VAR shortint: SHORTINT);
PROCEDURE IntS(s: Streams.Stream; VAR int: INTEGER);
PROCEDURE LongIntS(s: Streams.Stream; VAR longint: LONGINT);
PROCEDURE RealS(s: Streams.Stream; VAR real: REAL);
PROCEDURE LongRealS(s: Streams.Stream; VAR longreal: LONGREAL);
PROCEDURE CharS(s: Streams.Stream; VAR ch: CHAR);
PROCEDURE ByteS(s: Streams.Stream; VAR byte: BYTE);
PROCEDURE LineS(s: Streams.Stream; VAR line: ARRAY OF CHAR);
PROCEDURE LnS(s: Streams.Stream);
PROCEDURE StringS(s: Streams.Stream; VAR string: ARRAY OF CHAR);
PROCEDURE FieldS(s: Streams.Stream; VAR string: ARRAY OF CHAR) : BOOLEAN;

DESCRIPTION

Read implements formatted input for streams. All procedures read either from the given stream s (procedures ending in "S") or from Streams.stdin (all other procedures). All termination characters (e.g. first non-digit after Read.Int) are pushed back to the stream by Streams.Back. The definition what constitutes white space, a line terminator, or a field separator is provided by StreamDisciplines.

ShortInt, Int, and LongInt read integer values in decimal representation. Integers must conform to following regular expression: "[+-][0-9]+". Preceding white space is skipped.

Real and LongReal read real values according to following syntax:

"[+-]?[0-9]+(\.[0-9]*)?([eEdD][+-]?[0-9]+)?"
Leading white space is skipped. Char and Byte read the next character or byte, respectively.

Line reads a complete line (termination either by the line terminator or by the end of stream). The input line without terminating characters is copied to line. Line skips the rest of the line if it does not fit into line and guarantees line to be 0X-terminated. Streams.stdin.count (resp. s.count for LineS) equals the number of characters read including the line terminator. Note that the termination character is not pushed back to the input stream. Ln skips input until the next line termination or end of stream. Like Line, Ln does not push back the line termination character. Streams.stdin.count (resp. s.count for LnS) equals the number of characters read including the line terminator.

String skips input to the first non-white space character. Further input is copied into string until string is filled or white space is read. string is guaranteed to be 0X-terminated and does not contain white space characters. Streams.stdin.count (resp. s.count) equals the number of characters copied into string.

Field returns the next input field into string. Following the UNIX convention of small databases, records are separated by line terminators and fields are delimited either by field separators or line terminators. In normal case, each field separator separates two adjacent fields which allows possibly empty fields. However, field separators that are also whitespace are skipped at the beginning of a field. The resulting string is trimmed in respect to surrounding white space. The terminating field separator is consumed. Field returns FALSE if there are no more fields on the line, i.e. line terminators must be skipped explicitely. The resulting count (Streams.stdin.count resp. s.count) equals the number of characters read. Field skips the rest of the field if it does not fit into string.

DIAGNOSTICS

If not defined otherwise Streams.stdin.count (resp. s.count) equals 1 on success and 0 on failure.

EXAMPLE

Following code snippet shows how to parse small databases:
LOOP
   WHILE Read.FieldS(in, field) DO
      (* process field *)
   END;
   IF in.eof OR in.error THEN EXIT END;
   Read.LnS(in);
   IF in.count = 0 THEN EXIT END;
END;

SEE ALSO

Reals
real conversions
StreamDisciplines
line terminator, white space and field separators
Streams
stream operations
Write
formatted output

BUGS

Streams does not support Streams.Back for unbuffered streams. Thus termination characters are lost by reading from streams with buffering mode Streams.nobuf. This, for example, causes Field and FieldS to consume the entire line terminator that terminates the last field on a line.
Edited by: borchert, last change: 2004/09/24, revision: 1.7, converted to HTML: 2004/09/24

Oberon || Library || Module Index || Search Engine || Definition || Module