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

example: creating BlobNodes More...

Include dependency graph for BlobCreate.cpp:

Functions

int main (int, char **)
 Example use of BlobNode functions.

Detailed Description

example: creating BlobNodes

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

00001 /*** BlobCreate.cpp example: creating BlobNodes */
00004 #include <iostream>
00005 #include "E57Foundation.h"
00006 using namespace e57;
00007 using namespace std;
00008 
00010 int main(int /*argc*/, char** /*argv*/) {
00011     try {
00012         ImageFile imf("temp._e57", "w");
00013         StructureNode root = imf.root();
00014         
00015         const int blobLength = 10;
00016         static uint8_t buf[blobLength] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
00017 
00018         BlobNode b = BlobNode(imf, blobLength);
00019         root.set("blob1", b);
00020 
00021         const int halfLength = blobLength/2;
00022         b.write(buf, 0, halfLength);
00023         b.write(&buf[halfLength], halfLength, halfLength);
00024 
00025         imf.close(); // don't forget to explicitly close the ImageFile
00026     } catch(E57Exception& ex) {
00027         ex.report(__FILE__, __LINE__, __FUNCTION__);
00028         return(-1);
00029     }
00030 
00031     try {
00032         ImageFile imf("temp._e57", "r");
00033         StructureNode root = imf.root();
00034 
00035         BlobNode b = static_cast<BlobNode>(root.get("/blob1"));
00036 
00037         cout << "Blob length is " << b.byteCount() << " bytes" << endl;
00038         for (int64_t i = 0; i < b.byteCount(); i++) {
00039             static uint8_t buf[1];
00040             b.read(buf, i, 1);
00041             cout << "  byte " << i << " = " << static_cast<unsigned>(buf[0]) << endl;
00042         }
00043 
00044         imf.close(); // don't forget to explicitly close the ImageFile
00045     } catch(E57Exception& ex) {
00046         ex.report(__FILE__, __LINE__, __FUNCTION__);
00047         return(-1);
00048     }
00049     return(0);
00050 }

This example program writes an ImageFile containing a BlobNode, then reads it back and prints out the BlobNode contents. See the HelloWorld.cpp example for discussion of the use of include files, constructing an ImageFile, and the try/catch block to handle exceptions.

In source line 18, an empty 10 byte long BlobNode is created in the ImageFile. The 10 bytes are initially zero, but this example overwrites the zeroes with some given values from a buffer. The writing is performed in two pieces of 5 bytes each on source lines 22-23.

The ImageFile is closed, and then reopened for reading on source line 32. A handle to the BlobNode is fetched on source line 35 (with no checking since this program wrote the file). The byte length (10) of the BlobNode is printed out in source line 37. The for loop on source lines 38-42 reads the blob back one byte at a time and prints the values. Production code would, of course, read in much larger blocks, or perhaps a single, full length read into an allocated buffer. Since blobs are a byte sequence, they do not suffer from "endian-ness" (dependency on byte ordering of the host CPU). The BlobNode vales are stored in the binary section of the E57 file, and hence don't appear in the XML listing below.

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:

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

int main(int /*argc*/, char** /*argv*/) {
    try {
        ImageFile imf("temp._e57", "w");
        StructureNode root = imf.root();
        
        const int blobLength = 10;
        static uint8_t buf[blobLength] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

        BlobNode b = BlobNode(imf, blobLength);
        root.set("blob1", b);

        const int halfLength = blobLength/2;
        b.write(buf, 0, halfLength);
        b.write(&buf[halfLength], halfLength, halfLength);

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

    try {
        ImageFile imf("temp._e57", "r");
        StructureNode root = imf.root();

        BlobNode b = static_cast<BlobNode>(root.get("/blob1"));

        cout << "Blob length is " << b.byteCount() << " bytes" << endl;
        for (int64_t i = 0; i < b.byteCount(); i++) {
            static uint8_t buf[1];
            b.read(buf, i, 1);
            cout << "  byte " << i << " = " << static_cast<unsigned>(buf[0]) << endl;
        }

        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