1 /***********************************************************************/
5 /* Francois Rouaix, projet Cristal, INRIA Rocquencourt */
7 /* Copyright 1996 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the GNU Library General Public License, with */
10 /* the special exception on linking described in file ../../LICENSE. */
12 /***********************************************************************/
14 /* $Id: cldbm.c 5670 2003-07-08 13:50:31Z xleroy $ */
24 #ifdef DBM_USES_GDBM_NDBM
25 #include <gdbm-ndbm.h>
30 /* Quite close to sys_open_flags, but we need RDWR */
31 static int dbm_open_flags[] = {
32 O_RDONLY, O_WRONLY, O_RDWR, O_CREAT
35 static void raise_dbm (char *errmsg) Noreturn;
37 static void raise_dbm(char *errmsg)
39 static value * dbm_exn = NULL;
41 dbm_exn = caml_named_value("dbmerror");
42 raise_with_string(*dbm_exn, errmsg);
45 #define DBM_val(v) *((DBM **) &Field(v, 0))
47 static value alloc_dbm(DBM * db)
49 value res = alloc_small(1, Abstract_tag);
54 static DBM * extract_dbm(value vdb)
56 if (DBM_val(vdb) == NULL) raise_dbm("DBM has been closed");
60 /* Dbm.open : string -> Sys.open_flag list -> int -> t */
61 value caml_dbm_open(value vfile, value vflags, value vmode) /* ML */
63 char *file = String_val(vfile);
64 int flags = convert_flag_list(vflags, dbm_open_flags);
65 int mode = Int_val(vmode);
66 DBM *db = dbm_open(file,flags,mode);
69 raise_dbm("Can't open file");
71 return (alloc_dbm(db));
74 /* Dbm.close: t -> unit */
75 value caml_dbm_close(value vdb) /* ML */
77 dbm_close(extract_dbm(vdb));
82 /* Dbm.fetch: t -> string -> string */
83 value caml_dbm_fetch(value vdb, value vkey) /* ML */
86 key.dptr = String_val(vkey);
87 key.dsize = string_length(vkey);
88 answer = dbm_fetch(extract_dbm(vdb), key);
90 value res = alloc_string(answer.dsize);
91 memmove (String_val (res), answer.dptr, answer.dsize);
94 else raise_not_found();
97 value caml_dbm_insert(value vdb, value vkey, value vcontent) /* ML */
101 key.dptr = String_val(vkey);
102 key.dsize = string_length(vkey);
103 content.dptr = String_val(vcontent);
104 content.dsize = string_length(vcontent);
106 switch(dbm_store(extract_dbm(vdb), key, content, DBM_INSERT)) {
109 case 1: /* DBM_INSERT and already existing */
110 raise_dbm("Entry already exists");
112 raise_dbm("dbm_store failed");
116 value caml_dbm_replace(value vdb, value vkey, value vcontent) /* ML */
120 key.dptr = String_val(vkey);
121 key.dsize = string_length(vkey);
122 content.dptr = String_val(vcontent);
123 content.dsize = string_length(vcontent);
125 switch(dbm_store(extract_dbm(vdb), key, content, DBM_REPLACE)) {
129 raise_dbm("dbm_store failed");
133 value caml_dbm_delete(value vdb, value vkey) /* ML */
136 key.dptr = String_val(vkey);
137 key.dsize = string_length(vkey);
139 if (dbm_delete(extract_dbm(vdb), key) < 0)
140 raise_dbm("dbm_delete");
141 else return Val_unit;
144 value caml_dbm_firstkey(value vdb) /* ML */
146 datum key = dbm_firstkey(extract_dbm(vdb));
149 value res = alloc_string(key.dsize);
150 memmove (String_val (res), key.dptr, key.dsize);
153 else raise_not_found();
156 value caml_dbm_nextkey(value vdb) /* ML */
158 datum key = dbm_nextkey(extract_dbm(vdb));
161 value res = alloc_string(key.dsize);
162 memmove (String_val (res), key.dptr, key.dsize);
165 else raise_not_found();