Modula-2 Reloaded

A Modern Typesafe & Literate Programming Notation

Site Menu

Project

Specification

Implementation

Recommendations

Reference

Needs Updating

Work in Progress

Wastebasket

Wiki Manual

edit SideBar

Pervasive IO

DEFINITION MODULE PervasiveIO;
 (* Support for the I/O of built-in types using specified files. *)
 (* All file status results are of the type File.FileStatus.)

IMPORT File;
  (* Input and output of individual characters on specified files *)

PROCEDURE CHAR.Read (fid: File; VAR ch: CHAR);
  (* If possible, removes a character from the input file and assigns the corresponding value to ch.  The file status is set to the value allRight, endOfLine, or endOfInput.  *)

PROCEDURE CHAR.Write (fid: File; ch: CHAR);
  (* Writes the value of ch to the output stream fid. *)

 (* Input and output of Boolean values on specified files *)

PROCEDURE BOOLEAN.Read (fid: File; VAR b: BOOLEAN);
  (* If possible, does a ReadRestLine on the input file and assigns the value determined from the first letter of the string obtained to b. If the first letter is "Y", "y", "T", or "t", the value assigned is TRUE. Otherwise it is FALSE. The file status is set to the value allRight, endOfLine, or endOfInput.  *)

PROCEDURE BOOLEAN.Write (fid: File; b: BOOLEAN);
  (* Writes the string TRUE or FALSE to the output stream fid according to the the value of b. *)

  (* Input and output of whole numbers in decimal text form on specified files *)


  (* The text form of a signed whole number is ["+" | "-"], decimal digit, {decimal digit}

     The text form of an unsigned whole number is decimal digit, {decimal digit}  *)

PROCEDURE SetFieldLen (width: CARDINAL);
 (* Set the field length for all bare numeric Write procedures. This value defaults to zero. *)

PROCEDURE FieldLen () : CARDINAL;
 (* Get the default field length for the bare numeric Write procedures *)

(* I/O for built-in numeric types *)

(* Note: The semantics of all numeric read procedures are:
           - leading whitespace is skipped
           - any remaining characters from fid that form part of the numeral being read are removed
           - the numeric value of the string read is assigned to the supplied variable
           - the file status is set to the value allRight, outOfRange, wrongFormat, endOfLine, or endOfInput.  *)

(* Note: The semantics of the field width, whether supplied explicitly or taken from FieldLen () are:
           - If the width is sufficiently large, the numeral is written right justified in the field
           - Otherwise, the numeral is written out taking whatever space it requires, with no padding
           - In the case that the supplied width is zero, one padding space is written first.     *)

PROCEDURE CARDINAL.Read (fid: File; VAR card: CARDINAL);

PROCEDURE CARDINAL.Write (fid: File; card: CARDINAL);
  (* Writes the value of card to fid in text form, in a field of width FieldLen (). *)

PROCEDURE CARDINAL.WriteF (fid: File; card: CARDINAL; width: CARDINAL);
  (* Writes the value of card to fid in text form, in a field of the given minimum width. *)

PROCEDURE LONGCARD.Read (fid: File; VAR lCard: LONGCARD);

PROCEDURE LONGCARD.Write (fid: File; lCard: LONGCARD);
	(* Writes the value of card to fid in text form, in a field in a field of width FieldLen (). *)

PROCEDURE LONGCARD.WriteF (fid: File; lCard: LONGCARD; width: CARDINAL);
    (* Writes the value of card to fid in text form, in a field of the given minimum width. *)

PROCEDURE LONGINT.Read (fid: File; VAR int: LONGINT);

PROCEDURE LONGINT.Write (fid: File; int: LONGINT; width: CARDINAL);
  (* Writes the value of int to fid in text form, in a field of the given minimum width. *)

  (* Input and output of real numbers in decimal text form over specified
channels. The file status is of the type IOConsts.ReadResults. *)


  (* The text form of a signed fixed-point real number is
       ["+" | "-"], decimal digit, {decimal digit},
       [".", {decimal digit}]

     The text form of a signed floating-point real number is
       signed fixed-point real number,
       "E", ["+" | "-"], decimal digit, {decimal digit}
  *)

PROCEDURE REAL.Read (fid: File; VAR real: REAL);

PROCEDURE REAL.Write (fid: File; real: REAL);
  (* Writes the value of real to fid, as WriteFixed if the sign and magnitude can be shown in the width FieldLen (), or otherwise as WriteFloat.  The number of places or significant digits depends on FieldLen ().  *)

PROCEDURE REAL.WriteF (fid: File; real: REAL; width: CARDINAL);
  (* Writes the value of real to fid, as WriteFixed if the sign and magnitude can be shown in the given width, or otherwise as WriteFloat.  The number of places or significant digits depends on the given width.  *)

PROCEDURE REAL.WriteFloat (fid: File; real: REAL; sigFigs: CARDINAL; width: CARDINAL);
  (* Writes the value of real to fid in floating-point text form, with sigFigs significant figures, in a field of the given minimum width. *)

PROCEDURE REAL.WriteEng (fid: File; real: REAL; sigFigs: CARDINAL; width: CARDINAL);
  (* As for WriteFloat, except that the number is scaled with one to three
digits in the whole number part, and with an exponent that is a multiple of three. *)

PROCEDURE REAL.WriteFixed (fid: File; real: REAL; place: INTEGER; width: CARDINAL);
  (* Writes the value of real to fid in fixed-point text form, rounded to the
given place relative to the decimal point, in a field of the given minimum width. *)

PROCEDURE LONGREAL.Read (fid: File; VAR lReal: LONGREAL);

PROCEDURE LONGREAL.Write (fid: File; lReal: LONGREAL; width: CARDINAL);
  (* Writes the value of lReal to fid, as WriteFixed if the sign and magnitude can be shown in the width FieldLen (), or otherwise as WriteFloat.  The number of places or significant digits depends on the width FieldLen ().  *)

PROCEDURE LONGREAL.WriteF (fid: File; lReal: LONGREAL; width: CARDINAL);
  (* Writes the value of lReal to fid, as WriteFixed if the sign and magnitude can be shown in the given width, or otherwise as WriteFloat.  The number of places or significant digits depends on the given width.  *)

PROCEDURE LONGREAL.WriteFloat (fid: File; lReal: LONGREAL; sigFigs: CARDINAL; width: CARDINAL);
  (* Writes the value of lReal to fid in floating-point text form, with sigFigs significant figures, in a field of the given minimum width. *)

PROCEDURE LONGREAL.WriteEng (fid: File; lReal: LONGREAL; sigFigs: CARDINAL; width: CARDINAL);
  (* As for WriteFloat, except that the number is scaled with one to three
digits in the whole number part, and with an exponent that is a multiple of three. *)

PROCEDURE LONGREAL.WriteFixed (fid: File; lReal: LONGREAL; place: INTEGER; width: CARDINAL);
  (* Writes the value of real to fid in fixed-point text form, rounded to the
given place relative to the decimal point, in a field of the given minimum width. *)

END PervasiveIO.