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

example: read XML section from E57 file More...

Include dependency graph for RawXML.cpp:

Functions

int main (int, char **)
 Example use of functions to extract XML section of E57 file.

Detailed Description

example: read XML section from E57 file

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

00001 /*** RawXML.cpp example: read XML section from E57 file */
00004 #include <iostream>
00005 #include "E57Foundation.h"
00006 using namespace e57;
00007 using namespace std;
00008 
00010 int main(int /*argc*/, char** /*argv*/) {
00011     ustring inputFileName = "temp._e57";
00012     try {
00013         ImageFile imf(inputFileName, "w");
00014         StructureNode root = imf.root();
00015         
00016         root.set("greeting", StringNode(imf, "Hello world."));
00017 
00018         imf.close(); // don't forget to explicitly close the ImageFile
00019     } catch(E57Exception& ex) {
00020         ex.report(__FILE__, __LINE__, __FUNCTION__);
00021         return(-1);
00022     }
00023 
00024     try {
00025         E57Utilities utils = E57Utilities();
00026 
00027         static uint8_t buf[8];
00028         uint64_t length = utils.rawXmlLength(inputFileName);
00029         
00030         size_t count = 0;
00031         for (uint64_t start = 0; start < length; start += count) {
00032             if (length-start > sizeof(buf))
00033                 count = sizeof(buf);
00034             else
00035                 count = static_cast<size_t>(length-start);
00036  
00037             utils.rawXmlRead(inputFileName, buf, start, count);
00038 
00039             cout.write(reinterpret_cast<char*>(buf), static_cast<std::streamsize>(count));
00040         }
00041     } catch(E57Exception& ex) {
00042         ex.report(__FILE__, __LINE__, __FUNCTION__);
00043         return(-1);
00044     }
00045     return(0);
00046 }

This example program writes a very small ImageFile, then uses the two XML utility functions to read the XML section of the file without opening it as an ImageFile in read-mode.

Source lines 13-18 write a simple ImageFile on the disk. In source line 25 a E57Utilities object is created once, to avoid overhead of constructing it multiple times (which might be expensive in some API implementations, see E57Utilities::E57Utilities for more discussion). Source line 28 gets the logical length of the XML section in the file on disk. The for loop on source lines 31-40 repeatedly fetch buffers of the XML section. In a production version, the buffer would be much larger than 8 characters. The buffers are sent to the cout ostream on source line 39, with appropriate casts to keep the compiler happy. The XML is not parsed, just read in blocks. If the file is corrupted and has checksum errors, the raw XML utility functions will fail. There are no E57 Fountation API functions to read a corrupt E57 file (a .e57 file with checksum errors).

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:

/*** RawXML.cpp example: read XML section from E57 file */
#include <iostream>
#include "E57Foundation.h"
using namespace e57;
using namespace std;

int main(int /*argc*/, char** /*argv*/) {
    ustring inputFileName = "temp._e57";
    try {
        ImageFile imf(inputFileName, "w");
        StructureNode root = imf.root();
        
        root.set("greeting", StringNode(imf, "Hello world."));

        imf.close(); // don't forget to explicitly close the ImageFile
    } catch(E57Exception& ex) {
        ex.report(__FILE__, __LINE__, __FUNCTION__);
        return(-1);
    }

    try {
        E57Utilities utils = E57Utilities();

        static uint8_t buf[8];
        uint64_t length = utils.rawXmlLength(inputFileName);
        
        size_t count = 0;
        for (uint64_t start = 0; start < length; start += count) {
            if (length-start > sizeof(buf))
                count = sizeof(buf);
            else
                count = static_cast<size_t>(length-start);
 
            utils.rawXmlRead(inputFileName, buf, start, count);

            cout.write(reinterpret_cast<char*>(buf), static_cast<std::streamsize>(count));
        }
    } catch(E57Exception& ex) {
        ex.report(__FILE__, __LINE__, __FUNCTION__);
        return(-1);
    }
    return(0);
}
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines