00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <sys/types.h>
00011
00012 #include <iostream>
00013 #include <iomanip>
00014 #include <errno.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017
00018 #ifdef _WIN32
00019 extern "C" {
00020 extern int getopt(int, char * const *, const char *);
00021 extern int optind;
00022 }
00023 #else
00024 #include <unistd.h>
00025 #endif
00026
00027 #include <db_cxx.h>
00028
00029 #define DATABASE "access.db"
00030
00031 using std::cin;
00032 using std::cout;
00033 using std::cerr;
00034
00035 class AccessExample
00036 {
00037 public:
00038 AccessExample();
00039 void run(bool removeExistingDatabase, const char *fileName);
00040
00041 private:
00042
00043 AccessExample(const AccessExample &);
00044 void operator = (const AccessExample &);
00045 };
00046
00047 int
00048 usage()
00049 {
00050 (void)fprintf(stderr, "usage: AccessExample [-r] [database]\n");
00051 return (EXIT_FAILURE);
00052 }
00053
00054 int
00055 main(int argc, char *argv[])
00056 {
00057 int ch, rflag;
00058 const char *database;
00059
00060 rflag = 0;
00061 while ((ch = getopt(argc, argv, "r")) != EOF)
00062 switch (ch) {
00063 case 'r':
00064 rflag = 1;
00065 break;
00066 case '?':
00067 default:
00068 return (usage());
00069 }
00070 argc -= optind;
00071 argv += optind;
00072
00073
00074 database = *argv == NULL ? DATABASE : argv[0];
00075
00076
00077
00078
00079
00080
00081 try {
00082 AccessExample app;
00083 app.run((bool)(rflag == 1 ? true : false), database);
00084 return (EXIT_SUCCESS);
00085 }
00086 catch (DbException &dbe) {
00087 cerr << "AccessExample: " << dbe.what() << "\n";
00088 return (EXIT_FAILURE);
00089 }
00090 }
00091
00092 AccessExample::AccessExample()
00093 {
00094 }
00095
00096 void AccessExample::run(bool removeExistingDatabase, const char *fileName)
00097 {
00098
00099 if (removeExistingDatabase)
00100 (void)remove(fileName);
00101
00102
00103
00104 Db db(0, 0);
00105
00106 db.set_error_stream(&cerr);
00107 db.set_errpfx("AccessExample");
00108 db.set_pagesize(1024);
00109 db.set_cachesize(0, 32 * 1024, 0);
00110 db.open(NULL, fileName, NULL, DB_BTREE, DB_CREATE, 0664);
00111
00112
00113
00114
00115
00116 char buf[1024], rbuf[1024];
00117 char *p, *t;
00118 int ret;
00119 u_int32_t len;
00120
00121 for (;;) {
00122 cout << "input> ";
00123 cout.flush();
00124
00125 cin.getline(buf, sizeof(buf));
00126 if (cin.eof())
00127 break;
00128
00129 if ((len = (u_int32_t)strlen(buf)) <= 0)
00130 continue;
00131 for (t = rbuf, p = buf + (len - 1); p >= buf;)
00132 *t++ = *p--;
00133 *t++ = '\0';
00134
00135 Dbt key(buf, len + 1);
00136 Dbt data(rbuf, len + 1);
00137
00138 ret = db.put(0, &key, &data, DB_NOOVERWRITE);
00139 if (ret == DB_KEYEXIST) {
00140 cout << "Key " << buf << " already exists.\n";
00141 }
00142 }
00143 cout << "\n";
00144
00145
00146
00147
00148
00149 try {
00150
00151 Dbc *dbcp;
00152 db.cursor(NULL, &dbcp, 0);
00153
00154
00155 Dbt key;
00156 Dbt data;
00157 while (dbcp->get(&key, &data, DB_NEXT) == 0) {
00158 char *key_string = (char *)key.get_data();
00159 char *data_string = (char *)data.get_data();
00160 cout << key_string << " : " << data_string << "\n";
00161 }
00162 dbcp->close();
00163 }
00164 catch (DbException &dbe) {
00165 cerr << "AccessExample: " << dbe.what() << "\n";
00166 }
00167
00168 db.close(0);
00169 }