DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
G2++ Tutorial - G2++(C++)

Strings Containing Nonprintable Characters

At one time or another most every G2++ programmer has stumbled into the following situation: a field that seems natural to declare as a String needs to contain a newline, tab, or other nonprintable character. For compatibility with G2, such characters are not permitted; they cause String fields to be truncated on input or output. If this were not the case, tabs and newlines would corrupt the record structure, while permitting other nonprintable characters would violate the principal of visibility (see the section, ``Providing Inserters and Extractors for User-Defined Types'', in this topic,) and would destroy the interoperability of G2++ and G2 applications.

The restriction against including non-printable characters can be circumvented by defining a user-defined type that simply dumps out arbitrary characters and using it instead of String. For example, one user defined a USER type Blob whose inserter output a number N followed by N bytes, and whose extractor first extracted the number and then extracted as many bytes. This ``works,'' but read on.

This solution works when two G2++ applications communicate using the same record definition, but it destroys the interoperability of G2++ and G2 applications. The only safe solution is to convert the nonprintable characters to printable ones. Because this is such a common requirement, we have provided a user-defined type called Text.

Text is exactly like String(C++) in every respect but for its inserter and extractor, which convert nonprintable characters to and from printable ASCII escape sequences. The following example illustrates the definition and use of a Text field in a G2++ record:

   usr.g:
           Text    USER
           usr
                   name    *
                   age     SHORT
                   bio     Text    # 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...

To display a Text object in its original form, simply cast it to a String:

       cout << (String)u.bio;

which prints:

           Born on a mountaintop in Tennessee,
           Greenest state in the land of the Free,
           ...

Next topic: Pointers
Previous topic: Providing Inserters and Extractors for User-Defined Types

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005