~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to libdb/examples_cxx/AccessExample.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * See the file LICENSE for redistribution information.
3
 
 *
4
 
 * Copyright (c) 1997-2002
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 *
7
 
 * $Id$
8
 
 */
9
 
 
10
 
#include <sys/types.h>
11
 
 
12
 
#include <iostream>
13
 
#include <iomanip>
14
 
#include <errno.h>
15
 
#include <stdlib.h>
16
 
#include <string.h>
17
 
 
18
 
#include <db_cxx.h>
19
 
 
20
 
using std::cin;
21
 
using std::cout;
22
 
using std::cerr;
23
 
 
24
 
class AccessExample
25
 
{
26
 
public:
27
 
        AccessExample();
28
 
        void run();
29
 
 
30
 
private:
31
 
        static const char FileName[];
32
 
 
33
 
        // no need for copy and assignment
34
 
        AccessExample(const AccessExample &);
35
 
        void operator = (const AccessExample &);
36
 
};
37
 
 
38
 
int main()
39
 
{
40
 
        // Use a try block just to report any errors.
41
 
        // An alternate approach to using exceptions is to
42
 
        // use error models (see DbEnv::set_error_model()) so
43
 
        // that error codes are returned for all Berkeley DB methods.
44
 
        //
45
 
        try {
46
 
                AccessExample app;
47
 
                app.run();
48
 
                return (EXIT_SUCCESS);
49
 
        }
50
 
        catch (DbException &dbe) {
51
 
                cerr << "AccessExample: " << dbe.what() << "\n";
52
 
                return (EXIT_FAILURE);
53
 
        }
54
 
}
55
 
 
56
 
const char AccessExample::FileName[] = "access.db";
57
 
 
58
 
AccessExample::AccessExample()
59
 
{
60
 
}
61
 
 
62
 
void AccessExample::run()
63
 
{
64
 
        // Remove the previous database.
65
 
        (void)remove(FileName);
66
 
 
67
 
        // Create the database object.
68
 
        // There is no environment for this simple example.
69
 
        Db db(0, 0);
70
 
 
71
 
        db.set_error_stream(&cerr);
72
 
        db.set_errpfx("AccessExample");
73
 
        db.set_pagesize(1024);          /* Page size: 1K. */
74
 
        db.set_cachesize(0, 32 * 1024, 0);
75
 
        db.open(NULL, FileName, NULL, DB_BTREE, DB_CREATE, 0664);
76
 
 
77
 
        //
78
 
        // Insert records into the database, where the key is the user
79
 
        // input and the data is the user input in reverse order.
80
 
        //
81
 
        char buf[1024];
82
 
        char rbuf[1024];
83
 
        char *t;
84
 
        char *p;
85
 
        int ret;
86
 
        int len;
87
 
 
88
 
        for (;;) {
89
 
                cout << "input> ";
90
 
                cout.flush();
91
 
 
92
 
                cin.getline(buf, sizeof(buf));
93
 
                if (cin.eof())
94
 
                        break;
95
 
 
96
 
                if ((len = strlen(buf)) <= 0)
97
 
                        continue;
98
 
                for (t = rbuf, p = buf + (len - 1); p >= buf;)
99
 
                        *t++ = *p--;
100
 
                *t++ = '\0';
101
 
 
102
 
                Dbt key(buf, len + 1);
103
 
                Dbt data(rbuf, len + 1);
104
 
 
105
 
                ret = db.put(0, &key, &data, DB_NOOVERWRITE);
106
 
                if (ret == DB_KEYEXIST) {
107
 
                        cout << "Key " << buf << " already exists.\n";
108
 
                }
109
 
        }
110
 
        cout << "\n";
111
 
 
112
 
        // We put a try block around this section of code
113
 
        // to ensure that our database is properly closed
114
 
        // in the event of an error.
115
 
        //
116
 
        try {
117
 
                // Acquire a cursor for the table.
118
 
                Dbc *dbcp;
119
 
                db.cursor(NULL, &dbcp, 0);
120
 
 
121
 
                // Walk through the table, printing the key/data pairs.
122
 
                Dbt key;
123
 
                Dbt data;
124
 
                while (dbcp->get(&key, &data, DB_NEXT) == 0) {
125
 
                        char *key_string = (char *)key.get_data();
126
 
                        char *data_string = (char *)data.get_data();
127
 
                        cout << key_string << " : " << data_string << "\n";
128
 
                }
129
 
                dbcp->close();
130
 
        }
131
 
        catch (DbException &dbe) {
132
 
                cerr << "AccessExample: " << dbe.what() << "\n";
133
 
        }
134
 
 
135
 
        db.close(0);
136
 
}