Node: Printing, Previous: Reading, Up: Input Output



Printing

The Lpp printing functions emulate Common Lisp printing functions in that there are two ways to print Lpp objects to C++ streams, with or without escape characters. All Lpp objects automatically get two type dispatching printing functions for doing this. The user can redefine these dispatching functions if need be, See Accessing Type Meta-Objects.

prin1 object Function

prin1 object stream Function

princ object Function

princ object stream Function

print object Function

print object stream Function

All of these functions print the Lpp object object to the C++ output stream stream. If stream is omitted cout is assumed. All of these functions return object as its value.

With prin1 escape characters are used as appropriate while princ prints object with no escape characters. Roughly speaking the output of prin1 is suitable for read and the output of princ is intended to look good to people. print is just like prin1 except that the printed representation of object is preceded with a newline and followed by a space. Some examples will make this clear, the following code

     let obj = L("one two three");
     prin1(obj);
     print(object);
     princ(obj);
     

would produce on cout

     "one two three"
     "one two three" one two three
     

Note that the last output produced by princ when read back using read would produce the Symbol one instead of the String "one two three".

C++ output stream operators for Lpp objects default to using princ so that the following code

     let obj = L("one two three");
     cout << "Object = " << obj << endl;
     

would produce on cout

     Object = one two three
     

O object Macro

The O macro allows the Lpp object argument object when using C++ output stream operators to print using prin1. Contrast the following code and results using the O macro with the previous example

     let obj = L("one two three");
     cout << "Object = " << O(obj) << endl;
     

would produce on cout

     Object = "one two three"
     

Note that it only makes sense to use the O macro with C++ stream operators. The results of using the O macro elsewhere is undefined.

terpri Function

terpri stream Function

terpri outputs a newline to the C++ output stream stream. If the stream argument is omitted cout is assumed. terpri returns nil.

finishOutput Function

finishOutput stream Function

finishOutput attempts to ensure that all output sent to the C++ output stream stream has reached its destination and only then finally returns nil. If the stream argument is omitted cout is assumed.

pprint object Function

pprint object stream Function

pprint object stream start end Function

pprint is just like print except that the space is omitted after object is printed to the C++ output stream stream and the output is pretty. If the stream argument is omitted cout is assumed. If the start and end arguments are included the output will be pretty printed between columns start and end. start and end must be ints. All of these functions return object as its value.

Pretty means that the object is printed using extra white space to make it easily human readable. For example nested lists are indented to make the tree structure apparent.

prin1ToString object Function

princToString object Function

Both of these functions return an Lpp String with the printed representation of object. prin1ToString returns the prin1 printed representation. princToString returns the princ printed representation. For example:

     let obj = prin1ToString(list(L(1), L(2), L(3)));
     prin1(obj) => "(1 2 3)"
     

Typically prin1ToString and princToString are used to print relatively short expressions or values to strings. The maximum length of such a string allowed by default is 4096 characters which is determined by the define variable LPP_PRINTOSTRING_MAX. If more characters are desired for some reason redefine LPP_PRINTOSTRING_MAX to a value bigger than 4096 before the include file Lpp.hh in the Lpp library compile stream.

prin1Length object Function

prin1LengthL object Function

princLength object Function

princLengthL object Function

prin1Length returns as an int the prin1 printed representation length of the Lpp object object. prin1LengthL does the same but returns the length as an Lpp Integer. princLength and princLengthL are the counterparts for princ. Here are some examples:

     prin1Length(S(hello)) => 5
     prin1Length(L("hello")) => 7
     princLength(L("hello")) => 5
     prin1Length(L(-1234)) => 5
     prin1Length(list(L(1), L(2))) => 5