|
|
#include <G2text.h>class G2Text : public String{ // See String(3C++) public:
// Constructors G2Text( ); G2Text(const char* p); G2Text(const char* p,unsigned n); G2Text(char c); G2Text(const Substring& s); G2Text(Tmpstring& s); G2Text(Stringsize n); G2Text(const G2Text& t);
// Insertion and extraction friend ostream& operator<<(ostream& os,const G2Text& t); friend istream& operator>>(istream& is,G2Text& t); };
When a String(3C++) is inserted into an output stream, its characters, including nonprintable characters, are written without translation. When a G2++ record contains a String value (declared as n, *, or *(n)), any nonprintable character will cause the field to be truncated on either input or output. To avoid truncation, you may declare the field as type G2Text whenever you know this possibility exists (see the second Example).
G2Texts are exactly like Strings, except that their insertion operator translates non-printable characters into octal escape sequences, and their extraction operator performs the opposite translation.
All constructors have exactly the same meaning as for Strings (they are provided because constructors are not inherited).
friend ostream& operator<<(ostream& os,const G2Text& t);" G2Text inserter. Inserts the characters of t into stream os, translating each nonprintable ASCII character, as defined by the isprint() function (see ctype(3C)), to an octal escape sequence. The sequence consists of a backslash followed by three octal digits; the digits represent the ASCII value of the character. For example, the newline character is translated into the sequence \012. To avoid confusing the extractor (see below), a single backslash is converted to a double backslash.
friend istream& operator>>(istream& is,G2Text& t);" G2Text extractor. Performs the inverse of the translation performed by the G2Text inserter. That is, it translates octal escape sequences to the ASCII characters they represent (for example, it translates\012 to a newline character) and translates double backslash to single backslash.
Example 1:
G2Text t = "hello\nworld"; cout << "t = " << t << endl; cout << "(String)t = " << (String)t << endl;
The above code segment prints three lines:
t = hello 12world (String)t = hello world
Example 2 (defining a G2++ record containing G2Text):
usr.g: G2Text USER usr name * age SHORT bio G2Text # can contain # nonprintables client.c: #include "usr.h" main(){ USR u; u.name = "Crockett"; u.age = 50; while(cin){ u.bio += sgets(cin) + '\n'; } cout << u; } standard input: Born on a mountaintop in Tennessee, Greenest state in the land of the Free. standard output: usr name Crockett age 50 bio Born on a mountaintop in Tennessee,\012Greenest state...