1996-06-12 06:54:53 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Objective Caml */
|
|
|
|
/* */
|
|
|
|
/* Francois Rouaix, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../../LICENSE. */
|
1996-06-12 06:54:53 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1997-06-13 08:54:18 -07:00
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
1996-05-15 07:18:46 -07:00
|
|
|
#include <ndbm.h>
|
|
|
|
#include <mlvalues.h>
|
|
|
|
#include <alloc.h>
|
|
|
|
#include <memory.h>
|
|
|
|
#include <fail.h>
|
1997-07-15 07:31:29 -07:00
|
|
|
#include <callback.h>
|
1996-05-15 07:18:46 -07:00
|
|
|
|
1996-06-12 06:54:53 -07:00
|
|
|
/* Quite close to sys_open_flags, but we need RDWR */
|
1996-05-30 04:11:07 -07:00
|
|
|
static int dbm_open_flags[] = {
|
|
|
|
O_RDONLY, O_WRONLY, O_RDWR, O_CREAT
|
1996-05-15 07:18:46 -07:00
|
|
|
};
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void raise_dbm (char *errmsg) Noreturn;
|
1997-06-13 08:54:18 -07:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void raise_dbm(char *errmsg)
|
1996-05-15 07:18:46 -07:00
|
|
|
{
|
1997-07-15 07:31:29 -07:00
|
|
|
static value * dbm_exn = NULL;
|
|
|
|
if (dbm_exn == NULL)
|
|
|
|
dbm_exn = caml_named_value("dbmerror");
|
|
|
|
raise_with_string(*dbm_exn, errmsg);
|
1996-05-15 07:18:46 -07:00
|
|
|
}
|
|
|
|
|
1999-02-25 06:35:00 -08:00
|
|
|
#define DBM_val(v) *((DBM **) &Field(v, 0))
|
|
|
|
|
|
|
|
static value alloc_dbm(DBM * db)
|
|
|
|
{
|
|
|
|
value res = alloc_small(1, Abstract_tag);
|
|
|
|
DBM_val(res) = db;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBM * extract_dbm(value vdb)
|
|
|
|
{
|
|
|
|
if (DBM_val(vdb) == NULL) raise_dbm("DBM has been closed");
|
|
|
|
return DBM_val(vdb);
|
|
|
|
}
|
|
|
|
|
1996-05-15 07:18:46 -07:00
|
|
|
/* Dbm.open : string -> Sys.open_flag list -> int -> t */
|
1997-09-02 05:55:01 -07:00
|
|
|
value caml_dbm_open(value vfile, value vflags, value vmode) /* ML */
|
1996-05-15 07:18:46 -07:00
|
|
|
{
|
|
|
|
char *file = String_val(vfile);
|
1996-05-30 04:11:07 -07:00
|
|
|
int flags = convert_flag_list(vflags, dbm_open_flags);
|
1996-05-15 07:18:46 -07:00
|
|
|
int mode = Int_val(vmode);
|
|
|
|
DBM *db = dbm_open(file,flags,mode);
|
|
|
|
|
|
|
|
if (db == NULL)
|
|
|
|
raise_dbm("Can't open file");
|
|
|
|
else
|
1999-02-25 06:35:00 -08:00
|
|
|
return (alloc_dbm(db));
|
1996-05-15 07:18:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Dbm.close: t -> unit */
|
1997-09-02 05:55:01 -07:00
|
|
|
value caml_dbm_close(value vdb) /* ML */
|
1996-05-15 07:18:46 -07:00
|
|
|
{
|
1999-02-25 06:35:00 -08:00
|
|
|
dbm_close(extract_dbm(vdb));
|
|
|
|
DBM_val(vdb) = NULL;
|
1996-05-15 07:18:46 -07:00
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dbm.fetch: t -> string -> string */
|
1997-09-02 05:55:01 -07:00
|
|
|
value caml_dbm_fetch(value vdb, value vkey) /* ML */
|
1996-05-15 07:18:46 -07:00
|
|
|
{
|
|
|
|
datum key,answer;
|
|
|
|
key.dptr = String_val(vkey);
|
|
|
|
key.dsize = string_length(vkey);
|
1999-02-25 06:35:00 -08:00
|
|
|
answer = dbm_fetch(extract_dbm(vdb), key);
|
1996-05-15 07:18:46 -07:00
|
|
|
if (answer.dptr) {
|
|
|
|
value res = alloc_string(answer.dsize);
|
2000-11-23 05:45:03 -08:00
|
|
|
memmove (String_val (res), answer.dptr, answer.dsize);
|
1996-05-15 07:18:46 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else raise_not_found();
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value caml_dbm_insert(value vdb, value vkey, value vcontent) /* ML */
|
1996-05-15 07:18:46 -07:00
|
|
|
{
|
|
|
|
datum key, content;
|
|
|
|
|
|
|
|
key.dptr = String_val(vkey);
|
|
|
|
key.dsize = string_length(vkey);
|
|
|
|
content.dptr = String_val(vcontent);
|
|
|
|
content.dsize = string_length(vcontent);
|
|
|
|
|
1999-02-25 06:35:00 -08:00
|
|
|
switch(dbm_store(extract_dbm(vdb), key, content, DBM_INSERT)) {
|
1996-05-15 07:18:46 -07:00
|
|
|
case 0:
|
|
|
|
return Val_unit;
|
1997-05-19 08:42:21 -07:00
|
|
|
case 1: /* DBM_INSERT and already existing */
|
1996-06-10 05:09:41 -07:00
|
|
|
raise_dbm("Entry already exists");
|
|
|
|
default:
|
|
|
|
raise_dbm("dbm_store failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value caml_dbm_replace(value vdb, value vkey, value vcontent) /* ML */
|
1996-06-10 05:09:41 -07:00
|
|
|
{
|
|
|
|
datum key, content;
|
|
|
|
|
|
|
|
key.dptr = String_val(vkey);
|
|
|
|
key.dsize = string_length(vkey);
|
|
|
|
content.dptr = String_val(vcontent);
|
|
|
|
content.dsize = string_length(vcontent);
|
|
|
|
|
1999-02-25 06:35:00 -08:00
|
|
|
switch(dbm_store(extract_dbm(vdb), key, content, DBM_REPLACE)) {
|
1996-06-10 05:09:41 -07:00
|
|
|
case 0:
|
|
|
|
return Val_unit;
|
1996-05-15 07:18:46 -07:00
|
|
|
default:
|
1996-06-10 05:09:41 -07:00
|
|
|
raise_dbm("dbm_store failed");
|
1996-05-15 07:18:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value caml_dbm_delete(value vdb, value vkey) /* ML */
|
1996-05-15 07:18:46 -07:00
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
key.dptr = String_val(vkey);
|
|
|
|
key.dsize = string_length(vkey);
|
|
|
|
|
1999-02-25 06:35:00 -08:00
|
|
|
if (dbm_delete(extract_dbm(vdb), key) < 0)
|
1996-05-15 07:18:46 -07:00
|
|
|
raise_dbm("dbm_delete");
|
|
|
|
else return Val_unit;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value caml_dbm_firstkey(value vdb) /* ML */
|
1996-05-15 07:18:46 -07:00
|
|
|
{
|
1999-02-25 06:35:00 -08:00
|
|
|
datum key = dbm_firstkey(extract_dbm(vdb));
|
1996-05-15 07:18:46 -07:00
|
|
|
|
|
|
|
if (key.dptr) {
|
|
|
|
value res = alloc_string(key.dsize);
|
2000-11-23 05:45:03 -08:00
|
|
|
memmove (String_val (res), key.dptr, key.dsize);
|
1996-05-15 07:18:46 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else raise_not_found();
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value caml_dbm_nextkey(value vdb) /* ML */
|
1996-05-15 07:18:46 -07:00
|
|
|
{
|
1999-02-25 06:35:00 -08:00
|
|
|
datum key = dbm_nextkey(extract_dbm(vdb));
|
1996-05-15 07:18:46 -07:00
|
|
|
|
|
|
|
if (key.dptr) {
|
|
|
|
value res = alloc_string(key.dsize);
|
2000-11-23 05:45:03 -08:00
|
|
|
memmove (String_val (res), key.dptr, key.dsize);
|
1996-05-15 07:18:46 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else raise_not_found();
|
|
|
|
}
|