forked from ocaml/dbm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcldbm.c
214 lines (191 loc) · 5.66 KB
/
cldbm.c
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/***********************************************************************/
/* */
/* OCaml */
/* */
/* Francois Rouaix, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. All rights reserved. This file is distributed */
/* under the terms of the GNU Library General Public License, with */
/* the special exception on linking described in file ../../LICENSE. */
/* */
/***********************************************************************/
/* $Id: cldbm.c 11156 2011-07-27 14:17:02Z doligez $ */
#include <string.h>
#include <fcntl.h>
#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/fail.h>
#include <caml/callback.h>
#ifdef DBM_COMPAT
#include <ndbm.h>
#else
#include <gdbm.h>
#endif
#ifndef DBM_COMPAT
typedef struct gdbm_file_info DBM;
#endif
/* Quite close to sys_open_flags, but we need RDWR */
static int dbm_open_flags[] = {
O_RDONLY, O_WRONLY, O_RDWR, O_CREAT
};
static void raise_dbm (char *errmsg) Noreturn;
static void raise_dbm(char *errmsg)
{
static const value * dbm_exn = NULL;
if (dbm_exn == NULL)
dbm_exn = caml_named_value("dbmerror");
caml_raise_with_string(*dbm_exn, errmsg);
}
#define DBM_val(v) *((DBM **) &Field(v, 0))
#define DBM_db_memory_val(v) *((datum **) &Field(v, 1))
static value alloc_dbm(DBM * db)
{
value res = caml_alloc_small(2, Abstract_tag);
datum *db_mem = malloc(sizeof(datum));
if (db_mem == NULL)
caml_raise_out_of_memory();
DBM_val(res) = db;
DBM_db_memory_val(res) = db_mem;
db_mem->dptr = NULL;
db_mem->dsize = 0;
return res;
}
static DBM * extract_dbm(value vdb)
{
if (DBM_val(vdb) == NULL) raise_dbm("DBM has been closed");
return DBM_val(vdb);
}
static value alloc_datum(const datum * d)
{
value res = caml_alloc_string(d->dsize);
memcpy ((char *) String_val (res), d->dptr, d->dsize);
return res;
}
static void extract_datum(value v, datum * d)
{
d->dptr = (char *) String_val(v);
d->dsize = caml_string_length(v);
}
/* Dbm.open : string -> Sys.open_flag list -> int -> t */
value caml_dbm_open(value vfile, value vflags, value vmode) /* ML */
{
const char *file = String_val(vfile);
int flags = caml_convert_flag_list(vflags, dbm_open_flags);
int mode = Int_val(vmode);
#ifdef DBM_COMPAT
DBM *db = dbm_open(file,flags,mode);
#else
const char *ext = ".pag";
char *filename = malloc(sizeof(char) * (strlen(file) + strlen(ext) + 1));
if (filename == NULL)
caml_raise_out_of_memory();
strcpy(filename, file);
strcat(filename, ext);
DBM *db = gdbm_open(filename,0,flags,mode,NULL);
#endif
if (db == NULL)
raise_dbm("Can't open file");
else
return (alloc_dbm(db));
}
/* Dbm.close: t -> unit */
value caml_dbm_close(value vdb) /* ML */
{
#ifdef DBM_COMPAT
dbm_close(extract_dbm(vdb));
#else
gdbm_close(extract_dbm(vdb));
#endif
DBM_val(vdb) = NULL;
return Val_unit;
}
/* Dbm.fetch: t -> string -> string */
value caml_dbm_fetch(value vdb, value vkey) /* ML */
{
datum key, answer;
extract_datum(vkey, &key);
#ifdef DBM_COMPAT
answer = dbm_fetch(extract_dbm(vdb), key);
#else
answer = gdbm_fetch(extract_dbm(vdb), key);
#endif
if (answer.dptr) return alloc_datum(&answer); else caml_raise_not_found();
}
value caml_dbm_insert(value vdb, value vkey, value vcontent) /* ML */
{
datum key, content;
extract_datum(vkey, &key);
extract_datum(vcontent, &content);
#ifdef DBM_COMPAT
switch(dbm_store(extract_dbm(vdb), key, content, DBM_INSERT)) {
#else
switch(gdbm_store(extract_dbm(vdb), key, content, GDBM_INSERT)) {
#endif
case 0:
return Val_unit;
case 1: /* DBM_INSERT and already existing */
raise_dbm("Entry already exists");
default:
raise_dbm("dbm_store failed");
}
}
value caml_dbm_replace(value vdb, value vkey, value vcontent) /* ML */
{
datum key, content;
extract_datum(vkey, &key);
extract_datum(vcontent, &content);
#ifdef DBM_COMPAT
switch(dbm_store(extract_dbm(vdb), key, content, DBM_REPLACE)) {
#else
switch(gdbm_store(extract_dbm(vdb), key, content, GDBM_REPLACE)) {
#endif
case 0:
return Val_unit;
default:
raise_dbm("dbm_store failed");
}
}
value caml_dbm_delete(value vdb, value vkey) /* ML */
{
datum key;
extract_datum(vkey, &key);
#ifdef DBM_COMPAT
if (dbm_delete(extract_dbm(vdb), key) < 0)
#else
if (gdbm_delete(extract_dbm(vdb), key) < 0)
#endif
raise_dbm("dbm_delete");
else return Val_unit;
}
value caml_dbm_firstkey(value vdb) /* ML */
{
#ifdef DBM_COMPAT
datum key = dbm_firstkey(extract_dbm(vdb));
#else
datum key = gdbm_firstkey(extract_dbm(vdb));
datum *db_mem = DBM_db_memory_val(vdb);
if (db_mem->dptr != NULL)
free(db_mem->dptr);
(DBM_db_memory_val(vdb))->dptr = key.dptr;
(DBM_db_memory_val(vdb))->dsize = key.dsize;
#endif
if (key.dptr) return alloc_datum(&key); else caml_raise_not_found();
}
value caml_dbm_nextkey(value vdb) /* ML */
{
#ifdef DBM_COMPAT
datum key = dbm_nextkey(extract_dbm(vdb));
#else
datum key = {NULL, 0};
datum *db_mem = DBM_db_memory_val(vdb);
if (db_mem->dptr != NULL) {
key = gdbm_nextkey(extract_dbm(vdb), *db_mem);
free(db_mem->dptr);
(DBM_db_memory_val(vdb))->dptr = key.dptr;
(DBM_db_memory_val(vdb))->dsize = key.dsize;
}
#endif
if (key.dptr) return alloc_datum(&key); else caml_raise_not_found();
}