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