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

« back to all changes in this revision

Viewing changes to libdb/examples_cxx/BtRecExample.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 <errno.h>
13
 
#include <iostream>
14
 
#include <iomanip>
15
 
#include <stddef.h>
16
 
#include <stdio.h>
17
 
#include <stdlib.h>
18
 
#include <string.h>
19
 
 
20
 
#include <db_cxx.h>
21
 
 
22
 
using std::cout;
23
 
using std::cerr;
24
 
 
25
 
#define DATABASE        "access.db"
26
 
#define WORDLIST        "../test/wordlist"
27
 
 
28
 
const char *progname = "BtRecExample";          // Program name.
29
 
 
30
 
class BtRecExample
31
 
{
32
 
public:
33
 
        BtRecExample(FILE *fp);
34
 
        ~BtRecExample();
35
 
        void run();
36
 
        void stats();
37
 
        void show(const char *msg, Dbt *key, Dbt *data);
38
 
 
39
 
private:
40
 
        Db *dbp;
41
 
        Dbc *dbcp;
42
 
};
43
 
 
44
 
BtRecExample::BtRecExample(FILE *fp)
45
 
{
46
 
        char *p, *t, buf[1024], rbuf[1024];
47
 
        int ret;
48
 
 
49
 
        // Remove the previous database.
50
 
        (void)remove(DATABASE);
51
 
 
52
 
        dbp = new Db(NULL, 0);
53
 
 
54
 
        dbp->set_error_stream(&cerr);
55
 
        dbp->set_errpfx(progname);
56
 
        dbp->set_pagesize(1024);                        // 1K page sizes.
57
 
 
58
 
        dbp->set_flags(DB_RECNUM);                      // Record numbers.
59
 
        dbp->open(NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664);
60
 
 
61
 
        //
62
 
        // Insert records into the database, where the key is the word
63
 
        // preceded by its record number, and the data is the same, but
64
 
        // in reverse order.
65
 
        //
66
 
 
67
 
        for (int cnt = 1; cnt <= 1000; ++cnt) {
68
 
                (void)sprintf(buf, "%04d_", cnt);
69
 
                if (fgets(buf + 4, sizeof(buf) - 4, fp) == NULL)
70
 
                        break;
71
 
                u_int32_t len = strlen(buf);
72
 
                buf[len - 1] = '\0';
73
 
                for (t = rbuf, p = buf + (len - 2); p >= buf;)
74
 
                        *t++ = *p--;
75
 
                *t++ = '\0';
76
 
 
77
 
                // As a convenience for printing, we include the null terminator
78
 
                // in the stored data.
79
 
                //
80
 
                Dbt key(buf, len);
81
 
                Dbt data(rbuf, len);
82
 
 
83
 
                if ((ret = dbp->put(NULL, &key, &data, DB_NOOVERWRITE)) != 0) {
84
 
                        dbp->err(ret, "Db::put");
85
 
                        if (ret != DB_KEYEXIST)
86
 
                                throw DbException(ret);
87
 
                }
88
 
        }
89
 
}
90
 
 
91
 
BtRecExample::~BtRecExample()
92
 
{
93
 
        if (dbcp != 0)
94
 
                dbcp->close();
95
 
        dbp->close(0);
96
 
        delete dbp;
97
 
}
98
 
 
99
 
//
100
 
// Print out the number of records in the database.
101
 
//
102
 
void BtRecExample::stats()
103
 
{
104
 
        DB_BTREE_STAT *statp;
105
 
 
106
 
        dbp->stat(&statp, 0);
107
 
        cout << progname << ": database contains "
108
 
             << (u_long)statp->bt_ndata << " records\n";
109
 
 
110
 
        // Note: must use free, not delete.
111
 
        // This struct is allocated by C.
112
 
        //
113
 
        free(statp);
114
 
}
115
 
 
116
 
void BtRecExample::run()
117
 
{
118
 
        db_recno_t recno;
119
 
        int ret;
120
 
        char buf[1024];
121
 
 
122
 
        // Acquire a cursor for the database.
123
 
        dbp->cursor(NULL, &dbcp, 0);
124
 
 
125
 
        //
126
 
        // Prompt the user for a record number, then retrieve and display
127
 
        // that record.
128
 
        //
129
 
        for (;;) {
130
 
                // Get a record number.
131
 
                cout << "recno #> ";
132
 
                cout.flush();
133
 
                if (fgets(buf, sizeof(buf), stdin) == NULL)
134
 
                        break;
135
 
                recno = atoi(buf);
136
 
 
137
 
                //
138
 
                // Start with a fresh key each time,
139
 
                // the dbp->get() routine returns
140
 
                // the key and data pair, not just the key!
141
 
                //
142
 
                Dbt key(&recno, sizeof(recno));
143
 
                Dbt data;
144
 
 
145
 
                if ((ret = dbcp->get(&key, &data, DB_SET_RECNO)) != 0) {
146
 
                        dbp->err(ret, "DBcursor->get");
147
 
                        throw DbException(ret);
148
 
                }
149
 
 
150
 
                // Display the key and data.
151
 
                show("k/d\t", &key, &data);
152
 
 
153
 
                // Move the cursor a record forward.
154
 
                if ((ret = dbcp->get(&key, &data, DB_NEXT)) != 0) {
155
 
                        dbp->err(ret, "DBcursor->get");
156
 
                        throw DbException(ret);
157
 
                }
158
 
 
159
 
                // Display the key and data.
160
 
                show("next\t", &key, &data);
161
 
 
162
 
                //
163
 
                // Retrieve the record number for the following record into
164
 
                // local memory.
165
 
                //
166
 
                data.set_data(&recno);
167
 
                data.set_size(sizeof(recno));
168
 
                data.set_ulen(sizeof(recno));
169
 
                data.set_flags(data.get_flags() | DB_DBT_USERMEM);
170
 
 
171
 
                if ((ret = dbcp->get(&key, &data, DB_GET_RECNO)) != 0) {
172
 
                        if (ret != DB_NOTFOUND && ret != DB_KEYEMPTY) {
173
 
                                dbp->err(ret, "DBcursor->get");
174
 
                                throw DbException(ret);
175
 
                        }
176
 
                }
177
 
                else {
178
 
                        cout << "retrieved recno: " << (u_long)recno << "\n";
179
 
                }
180
 
        }
181
 
 
182
 
        dbcp->close();
183
 
        dbcp = NULL;
184
 
}
185
 
 
186
 
//
187
 
// show --
188
 
//      Display a key/data pair.
189
 
//
190
 
void BtRecExample::show(const char *msg, Dbt *key, Dbt *data)
191
 
{
192
 
        cout << msg << (char *)key->get_data()
193
 
             << " : " << (char *)data->get_data() << "\n";
194
 
}
195
 
 
196
 
int
197
 
main()
198
 
{
199
 
        FILE *fp;
200
 
 
201
 
        // Open the word database.
202
 
        if ((fp = fopen(WORDLIST, "r")) == NULL) {
203
 
                fprintf(stderr, "%s: open %s: %s\n",
204
 
                        progname, WORDLIST, db_strerror(errno));
205
 
                return (EXIT_FAILURE);
206
 
        }
207
 
 
208
 
        try {
209
 
                BtRecExample app(fp);
210
 
 
211
 
                // Close the word database.
212
 
                (void)fclose(fp);
213
 
                fp = NULL;
214
 
 
215
 
                app.stats();
216
 
                app.run();
217
 
        }
218
 
        catch (DbException &dbe) {
219
 
                cerr << "Exception: " << dbe.what() << "\n";
220
 
                return (EXIT_FAILURE);
221
 
        }
222
 
 
223
 
        return (EXIT_SUCCESS);
224
 
}