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 #include <openrj/cpp/database.hpp>
00026
00027
00028 #include <iostream>
00029
00030
00031
00032
00033
00034 using std::cerr;
00035 using std::endl;
00036 using std::cout;
00037
00038
00039
00040
00041
00042
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
00121