2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 2008-2009 Oracle. All rights reserved.
17 #include "dbstl_map.h"
18 #include "dbstl_vector.h"
21 using namespace dbstl;
25 extern int getopt(int, char * const *, const char *);
45 // no need for copy and assignment
46 AccessExample(const AccessExample &);
47 void operator = (const AccessExample &);
53 (void)fprintf(stderr, "usage: AccessExample");
54 return (EXIT_FAILURE);
58 main(int argc, char *argv[])
60 // Use a try block just to report any errors.
61 // An alternate approach to using exceptions is to
62 // use error models (see DbEnv::set_error_model()) so
63 // that error codes are returned for all Berkeley DB methods.
68 return (EXIT_SUCCESS);
70 catch (DbException &dbe) {
71 cerr << "AccessExample: " << dbe.what() << "\n";
72 return (EXIT_FAILURE);
76 AccessExample::AccessExample()
80 void AccessExample::run()
82 typedef db_map<char *, char *, ElementHolder<char *> > strmap_t;
83 // Create a map container with inmemory anonymous database.
86 // Insert records into dbmap, where the key is the user
87 // input and the data is the user input in reverse order.
89 char buf[1024], rbuf[1024];
94 // Acquire user input string as key.
98 cin.getline(buf, sizeof(buf));
102 if ((len = (u_int32_t)strlen(buf)) <= 0)
104 if (strcmp(buf, "quit") == 0)
107 // Reverse input string as data.
108 for (t = rbuf, p = buf + (len - 1); p >= buf;)
113 // Insert key/data pair.
115 dbmap.insert(make_pair(buf, rbuf));
116 } catch (DbException ex) {
117 if (ex.get_errno() == DB_KEYEXIST) {
118 cout << "Key " << buf << " already exists.\n";
127 // We put a try block around this section of code
128 // to ensure that our database is properly closed
129 // in the event of an error.
132 strmap_t::iterator itr;
133 for (itr = dbmap.begin();
134 itr != dbmap.end(); ++itr)
135 cout<<itr->first<<" : "<<itr->second<<endl;
137 catch (DbException ex) {
138 cerr<<"AccessExample "<<ex.what()<<endl;