[Top] [Prev] [Next]

print, fprint, sprint - print formatted output

include "sys.m"; sys:= load Sys Sys->PATH; fprint: fn(fd: ref FD, format: string, *): int; print: fn(format: string, *): int; sprint: fn(format: string, *): string;

Description

These functions format and print their arguments as UTF text.
print (format, *)

Write text to the standard output. Return the number of bytes transmitted or a negative value if an error was encountered when writing the output.

fprint (fd, *)

Write to the fd file descriptor. Return the number of bytes transmitted or a negative value, if an error was encountered when writing the output.

sprint (format, *)

Format text into a string and return the string. Returns the formatted string.

Each of these functions converts, formats, and prints its trailing arguments under control of a format string. The format contains two types of objects:
plain characters

Plain characters are simply copied to the output stream.

conversion specifications

Each conversion results in fetching of zero or more arguments.

The Limbo compiler recognizes calls to these functions and checks that the arguments match the format specifications in number and type.

Each conversion specification has the following format:

%[flags]verb

The verb is a single character and each flag is a single character or a (decimal) numeric string. Up to two numeric strings may be used. The first string is called f1, the second f2. A period can be used to separate them, and if the period is present, then f1 and f2 are taken to be zero if missing, otherwise they are omitted. Either or both of the numbers may be replaced with the character *, meaning that the actual number will be obtained from the argument list as an integer.

The flags and numbers are arguments to the verb defined as follows:
d, x

The numeric verbs d and x format their int arguments in decimal and hexadecimal, respectively. Address values of lists, arrays, etc. can also be printed

Each verb interprets the flags # and - to mean alternate format and left justified and the flag b is used to allow big's to be printed.

If f2 is not omitted, the number is padded on the left with zeros until at least f2 digits appear. If alternate format is specified for x conversion, the number is preceded by 16r. Finally, if f1 is not omitted, the number is padded on the left (or right, if left justification is specified) with enough blanks to make the field at least f1 characters long.

e, f, g

The floating point verbs e, f, and g take a real argument.

Each interprets the flags +, -, and # to mean always print a sign, left justified, and alternate format.

The e conversion appends an exponent e[-]digits.

The f verb produces output of the form [-]digits [.digits].

The g verb will output the argument in either e or f with the goal of producing the smallest output. Also, trailing zeros are omitted from the fraction part of the output, and a trailing decimal point appears only if it is followed by a digit. When alternate format is specified, the result will always contain a decimal point, and for g conversions, trailing zeros are not removed.

The f1 flag is the minimum field width and, if the converted value takes up less than f1 characters, it is padded on the left (or right, if 'left justified') with spaces.

The f2 flag is the number of digits that are converted after the decimal place for e and f conversions, and f2 is the maximum number of significant digits for g conversions.

c

The c verb copies a single character (integer) argument justified within a field of f1 characters as described above.

r

The r verb takes no arguments. It prints the error string associated with the most recent system error.

s

The s verb copies a string to the output. The number of characters copied (n) is the minimum of the size of the string and f2. These n characters are justified within a field of f1 characters as described above.

u

The u verb treats the argument as unsigned.

%

The % verb is used for printing a literal.

E

The E verb is the same as e, but specify exponent using E rather than e.

G

The G verb is the same as g , but specify exponent using E rather than e.

X

The X verb is the same as x, but print hex digits in upper-case (and print 0X rather than 0x if # flag present).

Example

Program

implement print;
include "sys.m";
sys: Sys;
include "draw.m";
print: module
{
        init: fn(ctxt: ref Draw->Context, argv: list of string);
};
init(ctxt: ref Draw->Context, argv: list of string)
{
        sys = load Sys sys->PATH;
        sys->print("[%%%%]\t%%\n");
        i : int = 16rab9cd;
        sys->print("[%%#x]\t%#x\n", i);
        sys->print("[%%#X]\t%#X\n", i);
        r : real = 0.00000345;
        sys->print("[%%e]\t%e\n", r);
        sys->print("[%%E]\t%E\n", r);
        sys->print("[%%g]\t%g\n", r);
        sys->print("[%%G]\t%G\n", r);
        i = -1;
        sys->print("[%%d]\t%d\n", i);
        sys->print("[%%ud]\t%ud\n", i);
}

Output Data

[%%]    %
[%#x]   0xab9cd
[%#X]   0XAB9CD
[%e]    3.450000e-06
[%E]    3.450000E-06
[%g]    3.45e-06
[%G]    3.45E-06
[%d]    -1
[%ud]   4294967295
 

Errors

Output longer than 256 bytes is truncated.

The x verb does not apply the 0x prefix when f2 is present.

See Also

Limbo System Module and open, create - open/create a file for reading or writing



[Top] [Prev] [Next]

infernosupport@lucent.com
Copyright © 1996,Lucent Technologies, Inc. All rights reserved.