Open-RJ C/C++ User's Journal Synesis Software STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ...

example_cpp_1.cpp

C++ example program for the Open-RJ/C++ mapping. Demonstrates enumeration of all non-empty records and their fields.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        example_cpp_1.cpp
00003  *
00004  * Purpose:     C++ example program for the Open-RJ/C++ mapping. Demonstrates
00005  *              enumeration of all non-empty records and their fields.
00006  *
00007  * Created:     12th May 2006
00008  * Updated:     28th May 2006
00009  *
00010  * www:         http://www.openrj.org/
00011  *
00012  * License:     Copyright (c) 2006, Synesis Software Pty Ltd.
00013  *              All rights reserved.
00014  *
00015  *              (Licensed under the Synesis Software Open License)
00016  *
00017  *              This source code is placed into the public domain 2006
00018  *              by Synesis Software Pty Ltd. There are no restrictions
00019  *              whatsoever to your use of the software.
00020  *
00021  * ////////////////////////////////////////////////////////////////////////// */
00022 
00023 
00024 /* Open-RJ Header Files */
00025 #include <openrj/cpp/database.hpp>
00026 
00027 /* Standard C++ Library Files */
00028 #include <iostream>
00029 
00030 /* /////////////////////////////////////////////////////////////////////////////
00031  * Namespace
00032  */
00033 
00034 using std::cerr;
00035 using std::endl;
00036 using std::cout;
00037 
00038 /* /////////////////////////////////////////////////////////////////////////////
00039  * Globals
00040  */
00041 
00042 // Sample database
00043 static const char   contents[] =
00044 
00045     "%% Sample Open-RJ database - Cats and Dogs\n"
00046     "%% Created:   28th September 2004\n"
00047     "%% Updated:   29th September 2004\n"
00048     "Name:      Barney\n"
00049     "Species:   Dog\n"
00050     "Breed:     Bijon \\\n"
00051     "           Frieze\n"
00052     "%%\n"
00053     "Name:      Elsa\n"
00054     "Species:   Dog\n"
00055     "Breed:     Mixed\n"
00056     "%%\n"
00057     "Name:      Fluffy Kitten\n"
00058     "Species:   Cat\n"
00059     "%%\n"
00060     "Name:      Moet\n"
00061     "Species:   Dog\n"
00062     "Breed:     Boxer\n"
00063     "%%\n"
00064     "Name:      Rebel\n"
00065     "Species:   Dog\n"
00066     "Breed:     German \\\n"
00067     "           Shepherd\n"
00068     "%%\n"
00069     "Name:      Sparky\n"
00070     "Species:   Cat\n"
00071     "%%\n";
00072 
00073 /* ////////////////////////////////////////////////////////////////////////// */
00074 
00075 int main(int /* argc */, char * /* argv */[])
00076 {
00077     try
00078     {
00079         // 1. Create a database instance on memory, so use memory_database
00080 
00081         unsigned                        flags   =   0;  // No special flags here.
00082         openrj::cpp::MemoryDatabase     db(&contents[0], sizeof(contents), flags);
00083 
00084         // 2. Display database characteristics
00085 
00086         cout << endl << "2. Display database characteristics:" << endl;
00087 
00088         cout    << "Database has " 
00089                 << db.GetNumLines() << " lines in " 
00090                 << db.GetNumFields() << " fields in "
00091                 << db.GetNumRecords() << " records"
00092                 << endl;
00093 
00094         // 3. Enumerate the contents using subscript operators
00095 
00096         cout << endl << "3. Enumerate records and their fields using subscript operators:" << endl;
00097 
00098         { for(size_t iRecord = 0; iRecord < db.GetNumRecords(); ++iRecord)
00099         {
00100             openrj::cpp::Record record(db[iRecord]);
00101 
00102             cout    << "record-#" << iRecord
00103                     << " " << record.GetComment() << " "
00104                     << " (" << record.GetNumFields() << " fields)"
00105                     << endl;
00106 
00107             for(size_t iField = 0; iField < record.GetNumFields(); ++iField)
00108             {
00109                 openrj::cpp::Field  field(record[iField]);
00110                 std::string         name    =   field.GetName();
00111                 std::string         value   =   field.GetValue();
00112 
00113                 cout << "  field-#" << iField << ": " << field << endl;
00114             }
00115         }}
00116     }
00117     catch(std::exception &x)
00118     {
00119         cerr << "Failed to open database: " << x.what() << endl;
00120     }
00121 
00122     return EXIT_SUCCESS;
00123 }
00124 
00125 /* /////////////////////////////////////////////////////////////////////////////
00126  * end of file
00127  */

Open-RJ Library documentation © Synesis Software Pty Ltd, 2004-2005