Berkeley database handling examples: DB *open_db (const char *f, bool dup = false) { DB *dbp = 0; int r = db_create (&dbp, NULL, 0); if (r != 0) { printf ("db_create: %s\n", db_strerror (r)); exit (1); } if (dup) if (dbp->set_flags (dbp, DB_DUPSORT) != 0) { cout << "Couldn't set duplicate records for database" << endl; exit (1); } if ((r = dbp->open (dbp, NULL, f, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) { dbp->err (dbp, r, f); dbp->close (dbp, 0); exit (1); } return dbp; } void stor_string (DB *dbp, int32_t id, const dynstr *s) { DBT dkey, data; memset (&dkey, 0, sizeof (DBT)); memset (&data, 0, sizeof (DBT)); dkey.data = &id; dkey.size = sizeof (int32_t); data.data = s->str; data.size = s->len + 1; int r = dbp->put (dbp, NULL, &dkey, &data, 0); if (r != 0) { cerr << "Database put failed: " << id << " => " << s << endl; exit (1); } } void del_entry (DB *dbp, int32_t id) { DBT dkey; memset (&dkey, 0, sizeof (DBT)); dkey.data = &id; dkey.size = sizeof (int32_t); dbp->del (dbp, NULL, &dkey, 0); // FIXME: check returnvalue } bool retr_string (DB *dbp, int32_t id, dynstr *&s) { DBT dkey, data; memset (&dkey, 0, sizeof (DBT)); memset (&data, 0, sizeof (DBT)); dkey.data = &id; dkey.size = sizeof (int32_t); int r = dbp->get (dbp, NULL, &dkey, &data, 0); if (r != DB_NOTFOUND) { s = new dynstr ((char *)data.data, data.size - 1); return true; } return false; }