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

example_cpp_2.cpp

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

00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        example_cpp_2.cpp
00003  *
00004  * Purpose:     C++ example program for the Open-RJ/C++ 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/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()
00076 {
00077     try
00078     {
00079         unsigned                        flags   =   openrj::ELIDE_BLANK_RECORDS | openrj::IGNORE_CASE_ON_LOOKUP;
00080         openrj::cpp::MemoryDatabase     db(&contents[0], sizeof(contents), flags);
00081 
00082         cout    << "Database has " 
00083                 << db.GetNumLines() << " lines in " 
00084                 << db.GetNumFields() << " fields in "
00085                 << db.GetNumRecords() << " records"
00086                 << endl;
00087 
00088         cout << endl << "Searching for records in the database that have a \"Breed\" field:" << endl;
00089 
00090         { for(size_t iRecord = 0; iRecord < db.GetNumRecords(); ++iRecord)
00091         {
00092             openrj::cpp::Record record(db[iRecord]);
00093 
00094             if(!record.HasField("BREED"))
00095             {
00096                 cout << "record does not contain a field called \"Breed\"" << endl;
00097             }
00098             else
00099             {
00100                 cout    << "  record-#" << iRecord
00101                         << " " << record.GetComment() << " "
00102                         << " (" << record.GetNumFields() << " fields)"
00103                         << endl;
00104 
00105                 cout    << "    " << "Name=" << record["Name"] << endl;
00106                 cout    << "    " << "Species=" << record["Species"] << endl;
00107                 cout    << "    " << "Breed=" << record["Breed"] << endl;
00108             }
00109         }}
00110     }
00111     catch(std::exception &x)
00112     {
00113         cerr << "Failed to open database: " << x.what() << endl;
00114     }
00115 
00116     return EXIT_SUCCESS;
00117 }
00118 
00119 /* /////////////////////////////////////////////////////////////////////////////
00120  * end of file
00121  */

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