[net.lang.mod2] OSSI: SIConversions

biagioni@unc.UUCP (Edoardo Biagioni) (11/06/86)

(***************************************************************************)
(***                                                                     ***)
(***                                                                     ***)
(***                        O  S  S  I                                   ***)
(***                        ==========                                   ***)
(***                                                                     ***)
(**)               DEFINITION MODULE SIConversions;                      (**)
(***               ===============================                       ***)
(***                                                                     ***)
(***   This module defines conversions from strings to numbers and       ***)
(***   from numbers to strings                                           ***)
(***                                                                     ***)
(***---------------------------------------------------------------------***)
(***                                                                     ***)
(***   Hardware:             independent                                 ***)
(***   Operating System:     independent                                 ***)
(***   Compiler:             independent                                 ***)
(***                                                                     ***)
(***   Version:      3.0                                                 ***)
(***   Implemented:  see copyright                                       ***)
(***   Date:         1986-03-11                                          ***)
(***                                                                     ***)
(***---------------------------------------------------------------------***)
(***                                                                     ***)
(***   Copyright 1984, 1985, 1986 by                                     ***)
(***      E. S. Biagioni                                                 ***)
(***      G. Heiser                                                      ***)
(***      K. Hinrichs                                                    ***)
(***      C. Muller                                                      ***)
(***                                                                     ***)
(***   Institut fuer Informatik                                          ***)
(***   ETH Zuerich                                                       ***)
(***   CH 8092 Zuerich                                                   ***)
(***   Switzerland                                                       ***)
(***                                                                     ***)
(***   Department of Computer Science                                    ***)
(***   University of North Carolina                                      ***)
(***   Chapel Hill, North Carolina 27514                                 ***)
(***   U.S.A.                                                            ***)
(***                                                                     ***)
(*** Permission to copy without fee all of this material is granted      ***)
(*** provided that the copies are not made or distributed for direct     ***)
(*** commercial advantage, that this OSSI copyright notice is            ***)
(*** included in the copy, that the module is not modified in any way    ***)
(*** except where necessary for compilation on a particular system,      ***)
(*** and that the module is always distributed in its original form.     ***)
(*** Distribution of this module in a modified form without including    ***)
(*** the original version is a violation of this copyright notice.       ***)
(***                                                                     ***)
(***---------------------------------------------------------------------***)
(***                                                                     ***)
(***   Updates:                                                          ***)
(***                                                                     ***)
(***                                                                     ***)
(***************************************************************************)

FROM SISystem IMPORT
   SIResult;


EXPORT QUALIFIED
   NumberType,              (* TYPEs *)
   Number,
   IsStringOverflow,        (* PROCEDUREs and functions *)
   IsNumberOverflow,
   IsNumberNotFound,
   RealToNumber,
   CardToString,
   IntToString,
   RealToString,
   OctToString,
   HexToString,
   StringToCard,
   StringToInt,
   StringToReal,
   StringToOct,
   StringToHex;


TYPE NumberType = (IntType, CardType, RealType, IntOrCardType);

     Number = RECORD
                CASE Type: NumberType OF
                  IntType      : Int  : INTEGER  |  (* numbers < 0        *)
                  CardType     : Card : CARDINAL |  (* numbers > MaxInt   *)
                  RealType     : Real : REAL     |  (* for nonintegral or
                                                        large numbers     *)
                  IntOrCardType: select : RECORD
                                             (* for small positive integers:
                                                both have the same value  *)
                                             Int : INTEGER; 
                                             Card: CARDINAL
                                           END;  (* IntOrCard *)
                END;  (* CASE *)
              END;  (* Number *)
    (*  IntType       = MinInt ..    -1        integral numbers
        CardType      = MaxInt + 1 .. MaxCard  integral numbers
        IntOrCardType = 0 .. MaxInt            integral numbers
        RealType      = all other numbers.                 *)


PROCEDURE RealToNumber (r: REAL; VAR n: Number);
(* determines the range of the given real and, if it is
   within cardinal or integer ranges, converts it as
   described above. *)


PROCEDURE IsStringOverflow (result: SIResult): BOOLEAN;
(* Returns TRUE iff 'result' (returned by an earlier operation)
   indicates the result string is too short. *)

PROCEDURE IsNumberOverflow (result: SIResult): BOOLEAN;
(* Returns TRUE iff 'result' (returned by an earlier operation)
   indicates the number found overflows the machine representation.    *)

PROCEDURE IsNumberNotFound (result: SIResult): BOOLEAN;
(* Returns TRUE iff 'result' (returned by an earlier operation)
   indicates that no syntactically valid number was found.    *)


(* the following procedures all return the string corresponding to the
   given number.
   The "field" specification always gives the minimum field length to be
   used. The field parameter will be ignored if it is too small for a
   correct representation of the number. If the string s is to small,
   result = StringOverflow() is returned. If the field is larger than
   required to represent the number, the number is right-justified and
   the field is padded with spaces, except for the hex and oct procedures,
   where it is padded with 0's up to the number of digits required to
   represent the wordlength of the machine. Giving a field longer than the
   space allocated by s fills the entire string. *)

PROCEDURE CardToString (VAR s: ARRAY OF CHAR; c: CARDINAL; field: CARDINAL;
                        VAR result: SIResult);

PROCEDURE IntToString (VAR s: ARRAY OF CHAR; i: INTEGER; field: CARDINAL;
                       VAR result: SIResult);
(* If i is negative, '-' is inserted before the number, if i is positive,
   nothing is inserted. *)

PROCEDURE RealToString (VAR s: ARRAY OF CHAR; r: REAL;
                        field, digits: CARDINAL; scientific: BOOLEAN;
                        VAR result: SIResult);
(* The actual length f, of the field used to represent r is determined
   as follows:
   - the length of s gives the maximum field length
   - "field" specifies the minimum field length (up to the maximum)
   - "digits" specifies the maximum length of the fractional part. Less
     digits are used to represent the fractional part, if the maximum
     field length is insufficient
   - a decimal point is always given and is preceded by at least one
     digit representing the integral part.
     IF "scientific" = TRUE the integral part consists of exactly one digit
     and is zero if and only if "r" is zero.
   - for scientific format the exponent is represented as 'E' followed
     by a minus sign (if r < 0) followed by at least one exponent
     digit
   - if "scientific" = FALSE and the maximum field is insufficient to
     represent r, "scientific" is set to TRUE
   - if "scientific" = TRUE and Field is sufficiently large, a standard
     representation is choosen for the exponent. It consists of
      - 'E'
      - always a sign ('+' or '-')
      - a constant number of exponent digits. This number is machine
        dependent and is the minimum number of digits required to represent
        any possible exponent
   - if "field" is larger than required for the representation of "r", it is
     filled with leading spaces *)

PROCEDURE OctToString (VAR s: ARRAY OF CHAR; c: CARDINAL; field: CARDINAL;
                       VAR result: SIResult);

PROCEDURE HexToString (VAR s: ARRAY OF CHAR; c: CARDINAL; field: CARDINAL;
                       VAR result: SIResult);

(*----------------------------------------------------------------------- *)

(* The following procedures search the given string for the representation
   of a number (type and syntax depending on the procedure).
   The number is searched for starting at startindex and up to endindex.
   If a syntactically valid number is found, startindex is set to the first
   character which is part of the number's representation (including
   leading spaces), endindex is set to the last character (excluding
   trailing spaces).
   If the number found is within the machine defined range for the
   corresponding type, the result is SIDone and the value of the
   number is returned in the respective parameter, otherwise result
   is set to NumberOverflow().
   If no syntactically valid number is found, result is set to
   NumberNotFound().
   The first index in a string is always zero.

   Numbers are expected to conform to the following syntax:

   OctalDigit  = "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7".
   Digit       = OctalDigit|"8"|"9".
   HexDigit    = Digit|"A"|"B"|"C"|"D"|"E"|"F".
   OctalNumber = OctalDigit | OctalDigit OctalNumber.
   HexNumber   = HexDigit | HexDigit HexNumber.
   Cardinal    = Digit | Digit Cardinal.
   Integer     = ["+" | "-"] Cardinal.
   Mantissa    = Cardinal ["." [Cardinal]] | "." Cardinal
   Exponent    = ["E" | "e"] Integer
   Real        = ["+" | "-"] Mantissa [Exponent]

   Spaces are allowed following the sign and before and after the 'E' *)
(*------------------------------------------------------------------- *)

PROCEDURE StringToCard (s: ARRAY OF CHAR; VAR c: CARDINAL;
                        VAR startindex, endindex: CARDINAL;
                        VAR result: SIResult);

PROCEDURE StringToInt  (s: ARRAY OF CHAR; VAR i: INTEGER;
                        VAR startindex, endindex: CARDINAL;
                        VAR result: SIResult);

PROCEDURE StringToReal (s: ARRAY OF CHAR; VAR r: REAL;
                        VAR startindex, endindex: CARDINAL;
                        VAR result: SIResult);

PROCEDURE StringToOct  (s: ARRAY OF CHAR; VAR c: CARDINAL;
                        VAR startindex, endindex: CARDINAL;
                        VAR result: SIResult);

PROCEDURE StringToHex  (s: ARRAY OF CHAR; VAR c: CARDINAL;
                        VAR startindex, endindex: CARDINAL;
                        VAR result: SIResult);

END SIConversions.