~kaction/haskell-gdbm

ref: 8156cb20b5bd459e1b4d0a8dfc1999346fe22d8b haskell-gdbm/cbits/link.c -rw-r--r-- 1.4 KiB
8156cb20 — Dmitry Bogatov Write low-level bindings to GDBM 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <gdbm.h>
#include <stdlib.h>

GDBM_FILE
cbit_open_reader(/*const*/ char *name)
{
	return gdbm_open(name, 0, GDBM_READER, 0, NULL);
}

GDBM_FILE
cbit_open_writer(/*const*/ char *name)
{
	return gdbm_open(name, 0, GDBM_WRITER, 0, NULL);
}

GDBM_FILE
cbit_open_recreate(/*const*/ char *name)
{
	return gdbm_open(name, 0, GDBM_WRCREAT, 0644, NULL);
}

void
cbit_close(GDBM_FILE db)
{
	gdbm_close(db);
}

int
cbit_insert(GDBM_FILE db, void *keyp, int keysize, void *valuep, int valuesize)
{
	datum key = { keyp, keysize };
	datum value = { valuep, valuesize };
	return gdbm_store(db, key, value, GDBM_REPLACE);
}

void* cbit_fetch(GDBM_FILE db, void *keyp, int keysize, int *valuesize)
{
	datum key = { keyp, keysize };
	datum value = gdbm_fetch(db, key);
	if (value.dptr) {
		*valuesize = value.dsize;
	}
	return value.dptr;
}

int
cbit_delete(GDBM_FILE db, void *keyp, int keysize)
{
	datum key = { keyp, keysize };
	return gdbm_delete(db, key);
}

void*
cbit_firstkey(GDBM_FILE db, int *size)
{
	datum first = gdbm_firstkey(db);
	if (first.dptr) {
		*size = first.dsize;
	}
	return first.dptr;
}

void*
cbit_nextkey(GDBM_FILE db, void *keyp, int keysize, int *nextsize)
{
	datum key = { keyp, keysize };
	datum next = gdbm_nextkey(db, key);
	if (next.dptr) {
		*nextsize = next.dsize;
	}
	return next.dptr;
}

int
cbit_exists(GDBM_FILE db, void *keyp, int keysize)
{
	datum key = { keyp, keysize };
	return gdbm_exists(db, key);
}