E57 Foundation API v1.1.312  Aug. 10, 2011
Functions
examples/StringCreate.cpp File Reference

example: creating StringNodes More...

Include dependency graph for StringCreate.cpp:

Functions

void printStringInfo (ImageFile imf, ustring pathName)
 Get and print some info about a StringNode.
int main (int, char **)
 Example use of StringNode functions.

Detailed Description

example: creating StringNodes

Also see listing at end of this page for source without line numbers (to cut&paste from).

00001 /*** StringCreate.cpp example: creating StringNodes */
00004 #include <iostream>
00005 #include "E57Foundation.h"
00006 using namespace e57;
00007 using namespace std;
00008 
00010 void printStringInfo(ImageFile imf, ustring pathName)
00011 {
00012     cout << pathName << ":" << endl;
00013 
00014     StructureNode root = imf.root();
00015 
00016     if (root.isDefined(pathName)) {
00017         Node n = root.get(pathName);
00018         if (n.type() == E57_STRING) {
00019             StringNode s = static_cast<StringNode>(n);
00020             cout << "  value = " << s.value() << endl;
00021         } else
00022             cout << "oops " << n.pathName() << " isn't an String" << endl;
00023     }
00024 }
00025 
00027 int main(int /*argc*/, char** /*argv*/) {
00028     try {
00029         ImageFile imf("temp._e57", "w");
00030         StructureNode root = imf.root();
00031 
00032         // Create 3 example Strings
00033         root.set("s1", StringNode(imf));
00034         root.set("s2", StringNode(imf, "hey there"));
00035         root.set("s3", StringNode(imf, "has a ]]> in it"));
00036 
00037         printStringInfo(imf, "/s1");
00038         printStringInfo(imf, "/s2");
00039         printStringInfo(imf, "/s3");
00040 
00041         imf.close(); // don't forget to explicitly close the ImageFile
00042     } catch(E57Exception& ex) {
00043         ex.report(__FILE__, __LINE__, __FUNCTION__);
00044         return(-1);
00045     }
00046     return(0);
00047 }

This example program writes an ImageFile containing 3 StringNodes. It then prints out the value of each StringNode. See the HelloWorld.cpp example for discussion of the use of include files, constructing an ImageFile, and the try/catch block to handle exceptions. Also see discussion in the IntegerCreate.cpp example concerning downcasting.

Source lines 33-35 illustrate the use of the default arguments in the StringNode constructor. In source line 33, an empty string is created. In source line 34, a 9 character string is created. In source line 35, a 15 character string is created that has a problematic "]]>" substring embedded in it. The ASTM E57 standard specifies special handling for strings that contain a "]]>" substring. XML line 6 shows how the string is encoded in XML. This is handled automatically by the API implementation, without intervention by the API user.

The following console output is produced:

The XML section of the temp._e57 E57 file produced by this example program is as follows:

Here is the source code without line numbers to cut&paste from:

/*** StringCreate.cpp example: creating StringNodes */
#include <iostream>
#include "E57Foundation.h"
using namespace e57;
using namespace std;

void printStringInfo(ImageFile imf, ustring pathName)
{
    cout << pathName << ":" << endl;

    StructureNode root = imf.root();

    if (root.isDefined(pathName)) {
        Node n = root.get(pathName);
        if (n.type() == E57_STRING) {
            StringNode s = static_cast<StringNode>(n);
            cout << "  value = " << s.value() << endl;
        } else
            cout << "oops " << n.pathName() << " isn't an String" << endl;
    }
}

int main(int /*argc*/, char** /*argv*/) {
    try {
        ImageFile imf("temp._e57", "w");
        StructureNode root = imf.root();

        // Create 3 example Strings
        root.set("s1", StringNode(imf));
        root.set("s2", StringNode(imf, "hey there"));
        root.set("s3", StringNode(imf, "has a ]]> in it"));

        printStringInfo(imf, "/s1");
        printStringInfo(imf, "/s2");
        printStringInfo(imf, "/s3");

        imf.close(); // don't forget to explicitly close the ImageFile
    } catch(E57Exception& ex) {
        ex.report(__FILE__, __LINE__, __FUNCTION__);
        return(-1);
    }
    return(0);
}
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines