AccessExample.cpp 2.73 KB
Newer Older
1 2 3
/*-
 * See the file LICENSE for redistribution information.
 *
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
4
 * Copyright (c) 1997-2002
5 6
 *	Sleepycat Software.  All rights reserved.
 *
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
7
 * $Id: AccessExample.cpp,v 11.18 2002/01/23 15:33:20 bostic Exp $
8 9 10 11
 */

#include <sys/types.h>

ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
12 13
#include <iostream>
#include <iomanip>
14 15 16 17 18 19
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include <db_cxx.h>

ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
20 21 22 23
using std::cin;
using std::cout;
using std::cerr;

24 25 26 27 28 29 30 31 32 33 34 35 36 37
class AccessExample
{
public:
	AccessExample();
	void run();

private:
	static const char FileName[];

	// no need for copy and assignment
	AccessExample(const AccessExample &);
	void operator = (const AccessExample &);
};

ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
38
int main()
39 40 41 42 43 44 45 46 47
{
	// 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();
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
48
		return (EXIT_SUCCESS);
49 50 51
	}
	catch (DbException &dbe) {
		cerr << "AccessExample: " << dbe.what() << "\n";
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
52
		return (EXIT_FAILURE);
53 54 55 56 57 58 59 60 61 62 63 64
	}
}

const char AccessExample::FileName[] = "access.db";

AccessExample::AccessExample()
{
}

void AccessExample::run()
{
	// Remove the previous database.
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
65
	(void)remove(FileName);
66 67 68 69 70 71 72 73 74

	// Create the database object.
	// There is no environment for this simple example.
	Db db(0, 0);

	db.set_error_stream(&cerr);
	db.set_errpfx("AccessExample");
	db.set_pagesize(1024);		/* Page size: 1K. */
	db.set_cachesize(0, 32 * 1024, 0);
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
75
	db.open(NULL, FileName, NULL, DB_BTREE, DB_CREATE, 0664);
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

	//
	// Insert records into the database, where the key is the user
	// input and the data is the user input in reverse order.
	//
	char buf[1024];
	char rbuf[1024];
	char *t;
	char *p;
	int ret;
	int len;

	for (;;) {
		cout << "input> ";
		cout.flush();

		cin.getline(buf, sizeof(buf));
		if (cin.eof())
			break;

		if ((len = strlen(buf)) <= 0)
			continue;
		for (t = rbuf, p = buf + (len - 1); p >= buf;)
			*t++ = *p--;
		*t++ = '\0';

		Dbt key(buf, len + 1);
		Dbt data(rbuf, len + 1);

		ret = db.put(0, &key, &data, DB_NOOVERWRITE);
		if (ret == DB_KEYEXIST) {
			cout << "Key " << buf << " already exists.\n";
		}
	}
	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 {
		// Acquire a cursor for the table.
		Dbc *dbcp;
		db.cursor(NULL, &dbcp, 0);

		// Walk through the table, printing the key/data pairs.
		Dbt key;
		Dbt data;
		while (dbcp->get(&key, &data, DB_NEXT) == 0) {
			char *key_string = (char *)key.get_data();
			char *data_string = (char *)data.get_data();
			cout << key_string << " : " << data_string << "\n";
		}
		dbcp->close();
	}
	catch (DbException &dbe) {
		cerr << "AccessExample: " << dbe.what() << "\n";
	}

	db.close(0);
}