DEFINITION MODULE Conversions;

        (********************************************************)
        (*                                                      *)
        (*              Miscellaneous type conversions          *)
        (*                                                      *)
        (*  Programmer:         P. Moylan                       *)
        (*  Last edited:        5 June 2024                     *)
        (*  Status:             Working                         *)
        (*                                                      *)
        (********************************************************)

FROM SYSTEM IMPORT
    (* type *)  CARD8, CARD32;

FROM Arith64 IMPORT
    (* type *)  CARD64LE;

FROM Arith64BE IMPORT
    (* type *)  CARD64BE;

TYPE HexDigit = [0..15];
     EightChar = ARRAY [0..7] OF CHAR;

PROCEDURE atoi (a: LONGREAL;  i: CARDINAL): LONGREAL;

    (* Calculates a**i.  This procedure does not really belong in this  *)
    (* module, but it's missing from MATHLIB and there's no more        *)
    (* logical place to put it.                                         *)

PROCEDURE StringToHex (string: ARRAY OF CHAR): CARDINAL;
PROCEDURE StringToCardinal (string: ARRAY OF CHAR): CARDINAL;
PROCEDURE StringToLongCard (string: ARRAY OF CHAR): CARD32;
PROCEDURE StringToCard64 (string: ARRAY OF CHAR): CARD64LE;
PROCEDURE StringToReal (string: ARRAY OF CHAR): REAL;
PROCEDURE StringToLongReal (string: ARRAY OF CHAR): LONGREAL;

    (* Converts a text string to numeric.  Leading blanks are ignored.  *)
    (* The conversion stops at the end of the array or at the first     *)
    (* character which cannot be part of the number, and in the         *)
    (* latter case all subsequent characters are ignored.               *)

PROCEDURE HexToChar (number: HexDigit): CHAR;

    (* Converts a one-digit hexadecimal number to its readable form.    *)

PROCEDURE HexByteToString (value: CARD8;
                        VAR (*OUT*) buffer: ARRAY OF CHAR;  pos: CARDINAL);

    (* Converts a byte value to 2-character hexadecimal, with the       *)
    (* result stored at buffer[pos] and buffer[pos+1].                  *)

PROCEDURE Card32ToHexString (value: CARD32;  VAR (*OUT*) buffer: ARRAY OF CHAR);
PROCEDURE HexToString (value: CARDINAL;  VAR (*OUT*) buffer: ARRAY OF CHAR);
PROCEDURE LongHexToString (value: CARD32;  VAR (*OUT*) buffer: EightChar);
PROCEDURE Card64ToHexString (number: CARD64LE;
                                    VAR (*OUT*) buffer: ARRAY OF CHAR);

    (* Converts 'value' to a string in hexadecimal notation.    *)

PROCEDURE HexToStringLJ (value: CARDINAL;  VAR (*OUT*) buffer: ARRAY OF CHAR;
                                            VAR (*INOUT*) pos: CARDINAL);

    (* Stores value in hexadecimal, left justified, starting at *)
    (* buffer[pos], and updates pos.                            *)

PROCEDURE ShortCardToString (number: CARD8;
                                        VAR (*OUT*) buffer: ARRAY OF CHAR;
                                        fieldsize: CARDINAL);
PROCEDURE CardinalToString (number: CARDINAL;
                                        VAR (*OUT*) buffer: ARRAY OF CHAR;
                                        fieldsize: CARDINAL);
PROCEDURE LongCardToString (number: CARD32;
                                        VAR (*OUT*) buffer: ARRAY OF CHAR;
                                        fieldsize: CARDINAL);
PROCEDURE Card64ToStringLE (number: CARD64LE;
                                        VAR (*OUT*) buffer: ARRAY OF CHAR;
                                        fieldsize: CARDINAL);
PROCEDURE Card64ToStringBE (number: CARD64BE;
                                        VAR (*OUT*) buffer: ARRAY OF CHAR;
                                        fieldsize: CARDINAL);
PROCEDURE Card64ToStringTS (number: CARD64LE;
                                     VAR (*OUT*) buffer: ARRAY OF CHAR;
                                     startpos, fieldsize: CARDINAL;
                                     thous_sep: CHAR);
PROCEDURE RealToString (number: REAL;  VAR (*OUT*) buffer: ARRAY OF CHAR;
                                        fieldsize: CARDINAL);
PROCEDURE LongRealToString (number: LONGREAL;
                                        VAR (*OUT*) buffer: ARRAY OF CHAR;
                                        fieldsize: CARDINAL);

    (* Converts the number to a decimal character string in array       *)
    (* "buffer", right-justified in a field of fieldsize characters.    *)
    (* In the case of reals the format depends on the size of the       *)
    (* number relative to the size of the buffer.                       *)

PROCEDURE CardinalToStringLJ (number: CARDINAL;
                                      VAR (*OUT*) buffer: ARRAY OF CHAR);
PROCEDURE Card64ToStringLJ (number: CARD64LE;
                                      VAR (*OUT*) buffer: ARRAY OF CHAR);

    (* Converts the number to a decimal character string in array       *)
    (* "buffer", left justified.                                        *)

PROCEDURE RealToF (number: REAL;  VAR (*INOUT*) fieldsize: CARDINAL;
                        decimalplaces: CARDINAL;  LeftJustified: BOOLEAN;
                        VAR (*OUT*) buffer: ARRAY OF CHAR);
PROCEDURE LongRealToF (number: LONGREAL;  VAR (*INOUT*) fieldsize: CARDINAL;
                        decimalplaces: CARDINAL;  LeftJustified: BOOLEAN;
                        VAR (*OUT*) buffer: ARRAY OF CHAR);

    (* Converts the number to an F-format string, of up to fieldsize    *)
    (* characters with decimalplaces digits after the decimal point.    *)
    (* The result is left justified if LeftJustified = TRUE is          *)
    (* specified by the caller, and right justified with space fill     *)
    (* otherwise.  On return fieldsize gives the number of character    *)
    (* positions actually used.  The result string is terminated with   *)
    (* at least one CHR(0) (which is not counted in fieldsize), except  *)
    (* where the result fills the entire buffer.                        *)

END Conversions.

