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


Ulm's Oberon Library:
Print


NAME

Print - formatted output to streams

SYNOPSIS

CONST tooManyArgs = 0; (* too many arguments given *)
CONST tooFewArgs = 1; (* too few arguments given *)
CONST badFormat = 2; (* syntax error in format string *)
CONST badArgumentSize = 3; (* bad size of argument *)
CONST errors = 4;
TYPE FormatString = ARRAY 128 OF CHAR;
TYPE ErrorCode = SHORTINT;
TYPE ErrorEvent = POINTER TO ErrorEventRec;
TYPE ErrorEventRec =
   RECORD
      (Events.EventRec)
      errorcode: ErrorCode;
      format: FormatString;
      errpos: LONGINT;
      nargs: INTEGER;
   END;
VAR error: Events.EventType;
VAR errormsg: ARRAY errors OF Events.Message;


PROCEDURE F(fmt: ARRAY OF CHAR); PROCEDURE F1(fmt: ARRAY OF CHAR; p1: ARRAY OF BYTE); PROCEDURE F2(fmt: ARRAY OF CHAR; p1, p2: ARRAY OF BYTE); PROCEDURE F3(fmt: ARRAY OF CHAR; p1, p2, p3: ARRAY OF BYTE); PROCEDURE F4(fmt: ARRAY OF CHAR; p1, p2, p3, p4: ARRAY OF BYTE); PROCEDURE F5(fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5: ARRAY OF BYTE); PROCEDURE F6(fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5, p6: ARRAY OF BYTE); PROCEDURE F7(fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5, p6, p7: ARRAY OF BYTE); PROCEDURE F8(fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5, p6, p7, p8: ARRAY OF BYTE); PROCEDURE F9(fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5, p6, p7, p8, p9: ARRAY OF BYTE);

PROCEDURE S(out: Streams.Stream; fmt: ARRAY OF CHAR); PROCEDURE S1(out: Streams.Stream; fmt: ARRAY OF CHAR; p1: ARRAY OF BYTE); PROCEDURE S2(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2: ARRAY OF BYTE); PROCEDURE S3(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3: ARRAY OF BYTE); PROCEDURE S4(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3, p4: ARRAY OF BYTE); PROCEDURE S5(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5: ARRAY OF BYTE); PROCEDURE S6(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5, p6: ARRAY OF BYTE); PROCEDURE S7(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5, p6, p7: ARRAY OF BYTE); PROCEDURE S8(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5, p6, p7, p8: ARRAY OF BYTE); PROCEDURE S9(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5, p6, p7, p8, p9: ARRAY OF BYTE);

PROCEDURE SE(out: Streams.Stream; fmt: ARRAY OF CHAR; errors: RelatedEvents.Object); PROCEDURE SE1(out: Streams.Stream; fmt: ARRAY OF CHAR; p1: ARRAY OF BYTE; errors: RelatedEvents.Object); PROCEDURE SE2(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2: ARRAY OF BYTE; errors: RelatedEvents.Object); PROCEDURE SE3(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3: ARRAY OF BYTE; errors: RelatedEvents.Object); PROCEDURE SE4(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3, p4: ARRAY OF BYTE; errors: RelatedEvents.Object); PROCEDURE SE5(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5: ARRAY OF BYTE; errors: RelatedEvents.Object); PROCEDURE SE6(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5, p6: ARRAY OF BYTE; errors: RelatedEvents.Object); PROCEDURE SE7(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5, p6, p7: ARRAY OF BYTE; errors: RelatedEvents.Object); PROCEDURE SE8(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5, p6, p7, p8: ARRAY OF BYTE; errors: RelatedEvents.Object); PROCEDURE SE9(out: Streams.Stream; fmt: ARRAY OF CHAR; p1, p2, p3, p4, p5, p6, p7, p8, p9: ARRAY OF BYTE; errors: RelatedEvents.Object);

DESCRIPTION

Print offers formatted printing in printf(3) style to Streams.stdout (F through F9) or to out (S through S9 and SE through SE9). The procedures convert their parameters (the number of parameters determines the procedure name) and instantiate them into the format string fmt.

The format string is interpreted as follows: Any character not belonging to an escape sequence introduced by \ or a format element introduced by % is simply appended to Streams.stdout resp. out. Escape sequences are substituted by a single character while format elements are instantiated by the next p? parameter.

Format elements must conform to the following syntax:

FormatElement = "%" {Flags} [Width] [Scale] Conversion .
Flags = "+" | "0" | "-" | "^" | "\" FillChar .
Width = Number | "*" .
Scale = "." Number .
Number = { DecDigits } .
DecDigits = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" .
Conversion = "b" | "c" | "d" | "e" | "f" | "g" | "j" | "o" | "s" | "x" | "y" .

Each format element defines a field in the output stream that is by default as wide as necessary to insert the result of a parameter conversion. The field width can be expanded by specifying Width. If not given as an explicit Number but as * Print uses the value of the next yet unused parameter (interpreted as an integer) as width indication. Values less or equal than the defaults have no effect. In all other cases the output field is filled up by leading blanks. Padding character and alignment strategy may be altered by use of Flags:

+
Any numeric output will be signed (by default positive values do not get a +sign).
--
The output will be left aligned within its field. This option has no effect if width is omitted.
0
The output of numeric values is padded with leading zeroes This option implies -- and ^.
^
The padding characters are inserted before the leading sign and the first digit of a number.
\FillChar
requires FillChar to become the padding character.

Other numeric output is not affected while strings are cut to the length given by Scale before they are aligned within their output fields. Print will use the next yet unused parameter as scale indication if * is specified.

Since Print has no idea about the actual types of the arguments corresponding to its formal parameters, convchar is used to determine the conversions to be executed for the next yet unused parameter. Print will not accept any other conversion character than those listed and described below. In detail the specifications of convchar have the following effect:

x
Hexadecimal output of an integer.
o
Octal output of an integer.
d
Decimal output of an integer.
f
Output of a real number in floating point notation. The scale, if given, fixes the number of digits following the decimal point. However, the first non-zero digit is printed regardless of the scale.
e
Output of a real number in its normalized exponential form. The scale, if given, fixes the number of digits following the decimal point.
g
Output of a real number in floating point or exponential notation. The selection depends on the exponent and the scale: If the exponent is greater or equal to -4 and less or equal to the scale, the floating point notation is chosen. The scale specifies also the number of significant digits to be shown. If no explicit scale is given, a default value of 6 is taken. Trailing zeroes are suppressed.
c
Output of a single CHAR.
s
Output of an ARRAY OF CHAR until the first null byte (0X) or the high bound of the array is reached.
b
Output of a BOOLEAN as text "TRUE" or "FALSE".
y
Output of a BOOLEAN as text "yes" or "no".
j
Output of a BOOLEAN as text "ja" or "nein".

Note that o, x, and d are legal conversion characters to output any type which has the same size (in bytes) as the expected one. This feature can be used to output an address (integer size presumed). Furthermore these conversion characters may be used to output the ascii-value of a CHAR. Vice versa c may be used to output a character that is specified by a SHORTINT-value.

%% is not interpreted as a format element. A single percent character is output instead.

Any appearance of the following escape sequences in format string fmt is substituted as listed:

\n
newline (line terminator as defined by StreamDisciplines)
\r
carriage return (0DX)
\t
horizontal tab (09X)
\e
escape (1BX)
\f
form feed (0CX)
\b
backspace (08X)
\&
bell (07X)
\Q
double quote (22X)
\q
quote("'")
\\
backslash ("\")
\[0-9A-F]+
character specified by [0-9A-F]+X.

DIAGNOSTICS

The count component of the output streams equals the number of bytes written.

Some calling errors lead to events which are passed to Events (if no errors parameter is given) or to RelatedEvents (by use of SE through SE9). Following error codes are implemented:

tooManyArgs
The format string references less arguments than given.
tooFewArgs
The format strings references more arguments than given.
badFormat
The format string does not follow the above specification.
badArgumentSize
One of the arguments has a size which does not comply to the argument specification in the format string (e.g. passing a REAL value for %d).

SEE ALSO

Events
error handling
RelatedEvents
error handling for SE through SE9
Scan
format driven input scanning
StreamDisciplines
definition of line terminator
Streams
stream operations
Write
portable module for formatted output to streams

BUGS

Print has been written prior to revised Oberon. Revised Oberon requires type identity for actual parameters to be passed to value parameters of type ARRAY OF BYTE. This restriction has not yet been implemented by Ulm's Oberon compiler but modules which use Print with parameters which cannot be passed to VAR-parameters are not portable to compilers which are conform to revised Oberon. This restricts the main purpose of this module to debugging output which vanishes in the production version.
Edited by: borchert, last change: 2004/01/14, revision: 1.7, converted to HTML: 2004/01/14

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