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

example_stl_7.cpp

C++ example program for the Open-RJ/STL mapping. Demonstrates enumeration of all non-empty records and their fields, with use of processing instructions.

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

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