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/stl/database.hpp>
00026 #include <openrj/stl/functional.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()
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 << "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
00125