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

example_stl_3.cpp

C++ example program for the Open-RJ/STL mapping. Demonstrates searching for fields in records.

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        example_stl_3.cpp
00003  *
00004  * Purpose:     C++ example program for the Open-RJ/STL mapping. Demonstrates
00005  *              searching for fields in records.
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/stl/database.hpp>
00026 #include <openrj/stl/functional.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()
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 << "Searching for records in the database that have a \"Breed\" field:" << endl;
00090 
00091         openrj::stl::database_base::const_iterator   begin   =   db.begin();
00092         openrj::stl::database_base::const_iterator   end     =   db.end();
00093 
00094         { for(; begin != end; ++begin)
00095         {
00096             openrj::stl::record record(*begin);
00097 
00098             if(!record.has_field("BREED"))
00099             {
00100                 cout << "record does not contain a field called \"Breed\"" << endl;
00101             }
00102             else
00103             {
00104                 cout    << "  record"
00105                         << " " << record.comment() << " "
00106                         << " (" << record.size() << " fields)"
00107                         << endl;
00108 
00109                 cout    << "    " << "Name=" << record["Name"] << endl;
00110                 cout    << "    " << "Species=" << record["Species"] << endl;
00111                 cout    << "    " << "Breed=" << record["Breed"] << endl;
00112             }
00113         }}
00114     }
00115     catch(std::exception &x)
00116     {
00117         cerr << "Failed to open database: " << x.what() << endl;
00118     }
00119 
00120     return EXIT_SUCCESS;
00121 }
00122 
00123 /* /////////////////////////////////////////////////////////////////////////////
00124  * end of file
00125  */

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