00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <openrj/stl/database.hpp>
00027
00028
00029 #include <iostream>
00030
00031
00032
00033
00034
00035 using std::cerr;
00036 using std::endl;
00037 using std::cout;
00038
00039
00040
00041
00042
00043
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 , char * [])
00077 {
00078 try
00079 {
00080 unsigned flags = 0;
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
00120