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

example_stl_1.cpp

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

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        example_stl_1.cpp
00003  *
00004  * Purpose:     C++ example program for the Open-RJ/STL mapping. Demonstrates
00005  *              enumeration of all non-empty records and their fields, via.
00006  *              subscript operators.
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     "Name:      Barney\n"
00050     "Species:   Dog\n"
00051     "Breed:     Bijon \\\n"
00052     "           Frieze\n"
00053     "%%\n"
00054     "Name:      Elsa\n"
00055     "Species:   Dog\n"
00056     "Breed:     Mixed\n"
00057     "%%\n"
00058     "Name:      Fluffy Kitten\n"
00059     "Species:   Cat\n"
00060     "%%\n"
00061     "Name:      Moet\n"
00062     "Species:   Dog\n"
00063     "Breed:     Boxer\n"
00064     "%%\n"
00065     "Name:      Rebel\n"
00066     "Species:   Dog\n"
00067     "Breed:     German \\\n"
00068     "           Shepherd\n"
00069     "%%\n"
00070     "Name:      Sparky\n"
00071     "Species:   Cat\n"
00072     "%%\n";
00073 
00074 /* ////////////////////////////////////////////////////////////////////////// */
00075 
00076 int main(int /* argc */, char * /* argv */[])
00077 {
00078     try
00079     {
00080         unsigned                        flags   =   0;  // No special flags here.
00081         openrj::stl::memory_database    db(&contents[0], sizeof(contents), flags);
00082 
00083         cout    << "Database has " 
00084                 << db.num_lines() << " lines in " 
00085                 << db.num_fields() << " fields in "
00086                 << db.num_records() << " records"
00087                 << endl;
00088 
00089         cout << endl << "Enumerate records and their fields using subscript operators:" << endl;
00090 
00091         { for(size_t iRecord = 0; iRecord < db.size(); ++iRecord)
00092         {
00093             openrj::stl::record record(db[iRecord]);
00094 
00095             cout    << "  record-#" << iRecord
00096                     << " " << record.comment() << " "
00097                     << " (" << record.size() << " fields)"
00098                     << endl;
00099 
00100             for(size_t iField = 0; iField < record.size(); ++iField)
00101             {
00102                 openrj::stl::field  field(record[iField]);
00103                 std::string         name    =   field.name();
00104                 std::string         value   =   field.value();
00105 
00106                 cout << "    field-#" << iField << ": " << field << endl;
00107             }
00108         }}
00109     }
00110     catch(std::exception &x)
00111     {
00112         cerr << "Failed to open database: " << x.what() << endl;
00113     }
00114 
00115     return EXIT_SUCCESS;
00116 }
00117 
00118 /* /////////////////////////////////////////////////////////////////////////////
00119  * end of file
00120  */

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