1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2008-2009 Oracle. All rights reserved.
*
* $Id$
*/
#include <sys/types.h>
#include <iostream>
#include <iomanip>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include "dbstl_map.h"
#include "dbstl_vector.h"
using namespace std;
using namespace dbstl;
#ifdef _WIN32
extern "C" {
extern int getopt(int, char * const *, const char *);
extern int optind;
}
#else
#include <unistd.h>
#endif
#include <db_cxx.h>
using std::cin;
using std::cout;
using std::cerr;
class AccessExample
{
public:
AccessExample();
void run();
private:
// no need for copy and assignment
AccessExample(const AccessExample &);
void operator = (const AccessExample &);
};
int
usage()
{
(void)fprintf(stderr, "usage: AccessExample");
return (EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
// Use a try block just to report any errors.
// An alternate approach to using exceptions is to
// use error models (see DbEnv::set_error_model()) so
// that error codes are returned for all Berkeley DB methods.
//
try {
AccessExample app;
app.run();
return (EXIT_SUCCESS);
}
catch (DbException &dbe) {
cerr << "AccessExample: " << dbe.what() << "\n";
return (EXIT_FAILURE);
}
}
AccessExample::AccessExample()
{
}
void AccessExample::run()
{
typedef db_map<char *, char *, ElementHolder<char *> > strmap_t;
// Create a map container with inmemory anonymous database.
strmap_t dbmap;
// Insert records into dbmap, where the key is the user
// input and the data is the user input in reverse order.
//
char buf[1024], rbuf[1024];
char *p, *t;
u_int32_t len;
for (;;) {
// Acquire user input string as key.
cout << "input> ";
cout.flush();
cin.getline(buf, sizeof(buf));
if (cin.eof())
break;
if ((len = (u_int32_t)strlen(buf)) <= 0)
continue;
if (strcmp(buf, "quit") == 0)
break;
// Reverse input string as data.
for (t = rbuf, p = buf + (len - 1); p >= buf;)
*t++ = *p--;
*t++ = '\0';
// Insert key/data pair.
try {
dbmap.insert(make_pair(buf, rbuf));
} catch (DbException ex) {
if (ex.get_errno() == DB_KEYEXIST) {
cout << "Key " << buf << " already exists.\n";
} else
throw;
} catch (...) {
throw;
}
}
cout << "\n";
// We put a try block around this section of code
// to ensure that our database is properly closed
// in the event of an error.
//
try {
strmap_t::iterator itr;
for (itr = dbmap.begin();
itr != dbmap.end(); ++itr)
cout<<itr->first<<" : "<<itr->second<<endl;
}
catch (DbException ex) {
cerr<<"AccessExample "<<ex.what()<<endl;
}
dbstl_exit();
}
|