summaryrefslogtreecommitdiffstats
path: root/src/rccdb4.c
diff options
context:
space:
mode:
authorSuren A. Chilingaryan <csa@dside.dyndns.org>2005-07-08 14:45:55 +0000
committerSuren A. Chilingaryan <csa@dside.dyndns.org>2005-07-08 14:45:55 +0000
commitfdfe5b42f687fbf3d4537541cc1b0bd9b8c2c090 (patch)
tree81d199b6fbbb11d3f310668e5bdabf9129e5f41f /src/rccdb4.c
parent232dbc756390d30a86ae6de0cc84600eddb1c0b2 (diff)
downloadlibrcc-fdfe5b42f687fbf3d4537541cc1b0bd9b8c2c090.tar.gz
librcc-fdfe5b42f687fbf3d4537541cc1b0bd9b8c2c090.tar.bz2
librcc-fdfe5b42f687fbf3d4537541cc1b0bd9b8c2c090.tar.xz
librcc-fdfe5b42f687fbf3d4537541cc1b0bd9b8c2c090.zip
08.07.2005
Diffstat (limited to 'src/rccdb4.c')
-rw-r--r--src/rccdb4.c124
1 files changed, 124 insertions, 0 deletions
diff --git a/src/rccdb4.c b/src/rccdb4.c
new file mode 100644
index 0000000..795b721
--- /dev/null
+++ b/src/rccdb4.c
@@ -0,0 +1,124 @@
+#include <stdio.h>
+
+#include "internal.h"
+#include "rccdb4.h"
+
+#define DATABASE "autolearn.db"
+#define MIN_CHARS 3
+
+int rccInitDb4(rcc_context ctx, const char *name, rcc_db4_flags flags) {
+ size_t size;
+ char *dbname;
+
+ if (!ctx) {
+ if (rcc_default_ctx) ctx = rcc_default_ctx;
+ else return -1;
+ }
+
+ if (!name) name = "default";
+
+ size = strlen(rcc_home_dir) + strlen(name) + 32;
+ dbname = (char*)malloc(size*sizeof(char));
+ if (!dbname) return -1;
+
+ sprintf(dbname,"%s/.rcc/",rcc_home_dir);
+ mkdir(dbname, 00644);
+
+ sprintf(dbname,"%s/.rcc/%s.db/",rcc_home_dir,name);
+ mkdir(dbname, 00644);
+
+ ctx->db4ctx = rccDb4CreateContext(dbname, flags);
+ free(dbname);
+
+ if (!ctx->db4ctx) return -1;
+
+ return 0;
+}
+
+db4_context rccDb4CreateContext(const char *dbpath, rcc_db4_flags flags) {
+ int err;
+ db4_context ctx;
+ DB_ENV *dbe;
+ DB *db;
+
+ err = db_env_create(&dbe, 0);
+ if (err) return NULL;
+
+ err = dbe->open(dbe, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0);
+ if (err) {
+ dbe->close(dbe, 0);
+ return NULL;
+ }
+
+ err = db_create(&db, dbe, 0);
+ if (err) {
+ dbe->close(dbe, 0);
+ return NULL;
+ }
+
+ err = db->open(db, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0);
+ if (err) {
+ db->close(db, 0);
+ dbe->close(dbe, 0);
+ return NULL;
+ }
+
+ ctx = (db4_context)malloc(sizeof(db4_context_s));
+ if (!ctx) {
+ db->close(db, 0);
+ dbe->close(dbe, 0);
+ return NULL;
+ }
+
+ ctx->db = db;
+ ctx->dbe = dbe;
+ ctx->flags = flags;
+ return ctx;
+}
+
+void rccDb4FreeContext(db4_context ctx) {
+ if (ctx) {
+ ctx->db->close(ctx->db, 0);
+ ctx->dbe->close(ctx->dbe, 0);
+ free(ctx);
+ }
+}
+
+int rccDb4SetKey(db4_context ctx, const char *orig, size_t olen, const rcc_string string, size_t slen) {
+ int err;
+ DBT key, data;
+
+ if ((!ctx)||(!orig)||(!string)) return -1;
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ key.data = (char*)orig;
+ key.size = STRNLEN(orig, olen);
+ data.data = (char*)string;
+ data.size = STRNLEN(string, slen);
+
+ if (key.size < MIN_CHARS) return -1;
+
+ err = ctx->db->put(ctx->db, NULL, &key, &data, 0);
+ return err;
+}
+
+rcc_string rccDb4GetKey(db4_context ctx, const char *orig, size_t olen) {
+ int err;
+ DBT key, data;
+
+ if ((!ctx)||(!orig)) return NULL;
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ key.data = (char*)orig;
+ key.size = STRNLEN(orig, olen);
+
+ data.flags = DB_DBT_REALLOC;
+
+ err = ctx->db->get(ctx->db, NULL, &key, &data, 0);
+ if (err) return NULL;
+ return data.data;
+}