1998-01-16 07:13:05 -05:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
dbm.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Mon Jan 24 15:59:52 JST 1994
|
|
|
|
|
2001-02-14 00:52:06 -05:00
|
|
|
Copyright (C) 1995-2001 Yukihiro Matsumoto
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
#ifdef HAVE_CDEFS_H
|
|
|
|
# include <cdefs.h>
|
|
|
|
#endif
|
2000-09-12 01:37:38 -04:00
|
|
|
#ifdef HAVE_SYS_CDEFS_H
|
|
|
|
# include <sys/cdefs.h>
|
|
|
|
#endif
|
2001-05-24 02:10:36 -04:00
|
|
|
#include DBM_HDR
|
1998-01-16 07:13:05 -05:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2001-05-24 02:10:36 -04:00
|
|
|
static VALUE rb_cDBM, rb_eDBMError;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-05-16 08:03:23 -04:00
|
|
|
#define RUBY_DBM_RW_BIT 0x20000000
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
struct dbmdata {
|
|
|
|
int di_size;
|
|
|
|
DBM *di_dbm;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
closed_dbm()
|
|
|
|
{
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_raise(rb_eDBMError, "closed DBM file");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#define GetDBM(obj, dbmp) {\
|
|
|
|
Data_Get_Struct(obj, struct dbmdata, dbmp);\
|
2001-02-13 00:09:11 -05:00
|
|
|
if (dbmp == 0) closed_dbm();\
|
1998-01-16 07:13:05 -05:00
|
|
|
if (dbmp->di_dbm == 0) closed_dbm();\
|
|
|
|
}
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
#define GetDBM2(obj, data, dbm) {\
|
|
|
|
GetDBM(obj, data);\
|
|
|
|
(dbm) = dbmp->di_dbm;\
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static void
|
|
|
|
free_dbm(dbmp)
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
{
|
2001-02-13 00:09:11 -05:00
|
|
|
if (dbmp) {
|
|
|
|
if (dbmp->di_dbm) dbm_close(dbmp->di_dbm);
|
|
|
|
free(dbmp);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-02-26 23:52:21 -05:00
|
|
|
static VALUE
|
|
|
|
fdbm_close(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
|
|
|
|
GetDBM(obj, dbmp);
|
|
|
|
dbm_close(dbmp->di_dbm);
|
|
|
|
dbmp->di_dbm = 0;
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
2001-02-13 00:09:11 -05:00
|
|
|
|
2005-06-20 04:17:53 -04:00
|
|
|
static VALUE
|
|
|
|
fdbm_closed(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
|
|
|
|
Data_Get_Struct(obj, struct dbmdata, dbmp);
|
|
|
|
if (dbmp == 0)
|
|
|
|
return Qtrue;
|
|
|
|
if (dbmp->di_dbm == 0)
|
|
|
|
return Qtrue;
|
|
|
|
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2002-12-21 14:08:50 -05:00
|
|
|
static VALUE fdbm_alloc _((VALUE));
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2002-12-21 14:08:50 -05:00
|
|
|
fdbm_alloc(klass)
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE klass;
|
2001-02-13 00:09:11 -05:00
|
|
|
{
|
2002-12-20 06:30:13 -05:00
|
|
|
return Data_Wrap_Struct(klass, 0, free_dbm, 0);
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_initialize(argc, argv, obj)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE obj;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2004-05-16 08:03:23 -04:00
|
|
|
VALUE file, vmode, vflags;
|
1998-01-16 07:13:05 -05:00
|
|
|
DBM *dbm;
|
|
|
|
struct dbmdata *dbmp;
|
2004-05-16 08:03:23 -04:00
|
|
|
int mode, flags = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-05-16 08:03:23 -04:00
|
|
|
if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
|
1998-01-16 07:13:05 -05:00
|
|
|
mode = 0666; /* default value */
|
|
|
|
}
|
|
|
|
else if (NIL_P(vmode)) {
|
|
|
|
mode = -1; /* return nil if DB not exist */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mode = NUM2INT(vmode);
|
|
|
|
}
|
2004-05-16 08:03:23 -04:00
|
|
|
|
|
|
|
if (!NIL_P(vflags))
|
|
|
|
flags = NUM2INT(vflags);
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
SafeStringValue(file);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-05-16 08:03:23 -04:00
|
|
|
if (flags & RUBY_DBM_RW_BIT) {
|
|
|
|
flags &= ~RUBY_DBM_RW_BIT;
|
|
|
|
dbm = dbm_open(RSTRING(file)->ptr, flags, mode);
|
1999-11-04 03:39:57 -05:00
|
|
|
}
|
2004-05-16 08:03:23 -04:00
|
|
|
else {
|
|
|
|
dbm = 0;
|
|
|
|
if (mode >= 0) {
|
|
|
|
dbm = dbm_open(RSTRING(file)->ptr, O_RDWR|O_CREAT, mode);
|
|
|
|
}
|
|
|
|
if (!dbm) {
|
|
|
|
dbm = dbm_open(RSTRING(file)->ptr, O_RDWR, 0);
|
|
|
|
}
|
|
|
|
if (!dbm) {
|
|
|
|
dbm = dbm_open(RSTRING(file)->ptr, O_RDONLY, 0);
|
|
|
|
}
|
1999-11-04 03:39:57 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (!dbm) {
|
|
|
|
if (mode == -1) return Qnil;
|
|
|
|
rb_sys_fail(RSTRING(file)->ptr);
|
|
|
|
}
|
|
|
|
|
2001-02-13 00:09:11 -05:00
|
|
|
dbmp = ALLOC(struct dbmdata);
|
|
|
|
DATA_PTR(obj) = dbmp;
|
1998-01-16 07:13:05 -05:00
|
|
|
dbmp->di_dbm = dbm;
|
|
|
|
dbmp->di_size = -1;
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2001-02-13 00:09:11 -05:00
|
|
|
static VALUE
|
|
|
|
fdbm_s_open(argc, argv, klass)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE klass;
|
|
|
|
{
|
|
|
|
VALUE obj = Data_Wrap_Struct(klass, 0, free_dbm, 0);
|
|
|
|
|
|
|
|
if (NIL_P(fdbm_initialize(argc, argv, obj))) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rb_block_given_p()) {
|
|
|
|
return rb_ensure(rb_yield, obj, fdbm_close, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2000-07-04 03:04:42 -04:00
|
|
|
fdbm_fetch(obj, keystr, ifnone)
|
|
|
|
VALUE obj, keystr, ifnone;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key, value;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(keystr);
|
1998-01-16 07:13:05 -05:00
|
|
|
key.dptr = RSTRING(keystr)->ptr;
|
|
|
|
key.dsize = RSTRING(keystr)->len;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
value = dbm_fetch(dbm, key);
|
|
|
|
if (value.dptr == 0) {
|
2000-07-04 03:04:42 -04:00
|
|
|
if (ifnone == Qnil && rb_block_given_p())
|
|
|
|
return rb_yield(rb_tainted_str_new(key.dptr, key.dsize));
|
|
|
|
return ifnone;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_tainted_str_new(value.dptr, value.dsize);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-07-04 03:04:42 -04:00
|
|
|
static VALUE
|
|
|
|
fdbm_aref(obj, keystr)
|
|
|
|
VALUE obj, keystr;
|
|
|
|
{
|
|
|
|
return fdbm_fetch(obj, keystr, Qnil);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_fetch_m(argc, argv, obj)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2001-02-13 00:09:11 -05:00
|
|
|
VALUE keystr, valstr, ifnone;
|
2000-07-04 03:04:42 -04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &keystr, &ifnone);
|
2001-02-13 00:09:11 -05:00
|
|
|
valstr = fdbm_fetch(obj, keystr, ifnone);
|
|
|
|
if (argc == 1 && !rb_block_given_p() && NIL_P(valstr))
|
|
|
|
rb_raise(rb_eIndexError, "key not found");
|
|
|
|
|
|
|
|
return valstr;
|
2000-07-04 03:04:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_index(obj, valstr)
|
|
|
|
VALUE obj, valstr;
|
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(valstr);
|
2000-07-04 03:04:42 -04:00
|
|
|
val.dptr = RSTRING(valstr)->ptr;
|
|
|
|
val.dsize = RSTRING(valstr)->len;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2000-07-04 03:04:42 -04:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
|
|
|
if (val.dsize == RSTRING(valstr)->len &&
|
2001-02-13 00:09:11 -05:00
|
|
|
memcmp(val.dptr, RSTRING(valstr)->ptr, val.dsize) == 0) {
|
2000-07-04 03:04:42 -04:00
|
|
|
return rb_tainted_str_new(key.dptr, key.dsize);
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
2000-07-04 03:04:42 -04:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2004-06-11 09:33:47 -04:00
|
|
|
fdbm_select(obj)
|
2002-02-26 23:52:21 -05:00
|
|
|
VALUE obj;
|
|
|
|
{
|
2004-06-11 09:33:47 -04:00
|
|
|
VALUE new = rb_ary_new();
|
2002-02-26 23:52:21 -05:00
|
|
|
int i;
|
2004-06-11 09:33:47 -04:00
|
|
|
datum key, val;
|
|
|
|
DBM *dbm;
|
|
|
|
struct dbmdata *dbmp;
|
2002-02-26 23:52:21 -05:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2004-06-11 09:33:47 -04:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
2004-12-08 21:19:50 -05:00
|
|
|
VALUE assoc, v;
|
2004-06-11 09:33:47 -04:00
|
|
|
val = dbm_fetch(dbm, key);
|
|
|
|
assoc = rb_assoc_new(rb_tainted_str_new(key.dptr, key.dsize),
|
|
|
|
rb_tainted_str_new(val.dptr, val.dsize));
|
2004-12-08 21:19:50 -05:00
|
|
|
v = rb_yield(assoc);
|
|
|
|
if (RTEST(v)) {
|
2004-06-11 09:33:47 -04:00
|
|
|
rb_ary_push(new, assoc);
|
2004-12-08 21:19:50 -05:00
|
|
|
}
|
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2002-02-26 23:52:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2003-05-06 02:51:31 -04:00
|
|
|
static VALUE
|
|
|
|
fdbm_values_at(argc, argv, obj)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE new = rb_ary_new2(argc);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
rb_ary_push(new, fdbm_fetch(obj, argv[i], Qnil));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
static void
|
|
|
|
fdbm_modify(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
rb_secure(4);
|
2004-05-08 04:12:08 -04:00
|
|
|
if (OBJ_FROZEN(obj)) rb_error_frozen("DBM");
|
2004-05-07 04:44:24 -04:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
fdbm_delete(obj, keystr)
|
|
|
|
VALUE obj, keystr;
|
|
|
|
{
|
|
|
|
datum key, value;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
2002-02-26 23:52:21 -05:00
|
|
|
VALUE valstr;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
fdbm_modify(obj);
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(keystr);
|
1998-01-16 07:13:05 -05:00
|
|
|
key.dptr = RSTRING(keystr)->ptr;
|
|
|
|
key.dsize = RSTRING(keystr)->len;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
value = dbm_fetch(dbm, key);
|
|
|
|
if (value.dptr == 0) {
|
2002-02-26 23:52:21 -05:00
|
|
|
if (rb_block_given_p()) return rb_yield(keystr);
|
1998-01-16 07:13:05 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2002-02-26 23:52:21 -05:00
|
|
|
/* need to save value before dbm_delete() */
|
|
|
|
valstr = rb_tainted_str_new(value.dptr, value.dsize);
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
if (dbm_delete(dbm, key)) {
|
|
|
|
dbmp->di_size = -1;
|
2001-02-13 00:09:11 -05:00
|
|
|
rb_raise(rb_eDBMError, "dbm_delete failed");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (dbmp->di_size >= 0) {
|
|
|
|
dbmp->di_size--;
|
|
|
|
}
|
2002-02-26 23:52:21 -05:00
|
|
|
return valstr;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_shift(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE keystr, valstr;
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
fdbm_modify(obj);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
dbmp->di_size = -1;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
key = dbm_firstkey(dbm);
|
|
|
|
if (!key.dptr) return Qnil;
|
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
keystr = rb_tainted_str_new(key.dptr, key.dsize);
|
|
|
|
valstr = rb_tainted_str_new(val.dptr, val.dsize);
|
2001-05-02 00:22:21 -04:00
|
|
|
dbm_delete(dbm, key);
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_assoc_new(keystr, valstr);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_delete_if(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE keystr, valstr;
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE ret, ary = rb_ary_new();
|
|
|
|
int i, status = 0, n;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
fdbm_modify(obj);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
n = dbmp->di_size;
|
|
|
|
dbmp->di_size = -1;
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
keystr = rb_tainted_str_new(key.dptr, key.dsize);
|
|
|
|
valstr = rb_tainted_str_new(val.dptr, val.dsize);
|
2001-05-02 00:22:21 -04:00
|
|
|
ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status);
|
|
|
|
if (status != 0) break;
|
|
|
|
if (RTEST(ret)) rb_ary_push(ary, keystr);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < RARRAY(ary)->len; i++) {
|
|
|
|
keystr = RARRAY(ary)->ptr[i];
|
2004-10-19 15:49:49 -04:00
|
|
|
StringValue(keystr);
|
2001-05-02 00:22:21 -04:00
|
|
|
key.dptr = RSTRING(keystr)->ptr;
|
|
|
|
key.dsize = RSTRING(keystr)->len;
|
|
|
|
if (dbm_delete(dbm, key)) {
|
|
|
|
rb_raise(rb_eDBMError, "dbm_delete failed");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2001-05-02 00:22:21 -04:00
|
|
|
if (status) rb_jump_tag(status);
|
|
|
|
if (n > 0) dbmp->di_size = n - RARRAY(ary)->len;
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_clear(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
fdbm_modify(obj);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
dbmp->di_size = -1;
|
2001-05-02 00:22:21 -04:00
|
|
|
while (key = dbm_firstkey(dbm), key.dptr) {
|
1998-01-16 07:13:05 -05:00
|
|
|
if (dbm_delete(dbm, key)) {
|
2001-02-13 00:09:11 -05:00
|
|
|
rb_raise(rb_eDBMError, "dbm_delete failed");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2001-05-02 00:22:21 -04:00
|
|
|
dbmp->di_size = 0;
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
fdbm_invert(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE keystr, valstr;
|
|
|
|
VALUE hash = rb_hash_new();
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-01-19 23:59:39 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
|
|
|
keystr = rb_tainted_str_new(key.dptr, key.dsize);
|
|
|
|
valstr = rb_tainted_str_new(val.dptr, val.dsize);
|
|
|
|
rb_hash_aset(hash, valstr, keystr);
|
|
|
|
}
|
2000-04-10 01:48:43 -04:00
|
|
|
return hash;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
2003-04-17 01:41:27 -04:00
|
|
|
static VALUE each_pair _((VALUE));
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
each_pair(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
return rb_funcall(obj, rb_intern("each_pair"), 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE fdbm_store _((VALUE,VALUE,VALUE));
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
update_i(pair, dbm)
|
|
|
|
VALUE pair, dbm;
|
|
|
|
{
|
|
|
|
Check_Type(pair, T_ARRAY);
|
|
|
|
if (RARRAY(pair)->len < 2) {
|
|
|
|
rb_raise(rb_eArgError, "pair must be [key, value]");
|
|
|
|
}
|
|
|
|
fdbm_store(dbm, RARRAY(pair)->ptr[0], RARRAY(pair)->ptr[1]);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_update(obj, other)
|
|
|
|
VALUE obj, other;
|
|
|
|
{
|
|
|
|
rb_iterate(each_pair, other, update_i, obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_replace(obj, other)
|
|
|
|
VALUE obj, other;
|
|
|
|
{
|
|
|
|
fdbm_clear(obj);
|
|
|
|
rb_iterate(each_pair, other, update_i, obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
fdbm_store(obj, keystr, valstr)
|
|
|
|
VALUE obj, keystr, valstr;
|
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
fdbm_modify(obj);
|
1999-01-19 23:59:39 -05:00
|
|
|
keystr = rb_obj_as_string(keystr);
|
2004-11-14 10:41:40 -05:00
|
|
|
valstr = rb_obj_as_string(valstr);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
key.dptr = RSTRING(keystr)->ptr;
|
|
|
|
key.dsize = RSTRING(keystr)->len;
|
|
|
|
|
|
|
|
val.dptr = RSTRING(valstr)->ptr;
|
|
|
|
val.dsize = RSTRING(valstr)->len;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
dbmp->di_size = -1;
|
|
|
|
if (dbm_store(dbm, key, val, DBM_REPLACE)) {
|
1999-08-13 01:37:52 -04:00
|
|
|
#ifdef HAVE_DBM_CLEARERR
|
1998-01-16 07:13:05 -05:00
|
|
|
dbm_clearerr(dbm);
|
1999-01-19 23:59:39 -05:00
|
|
|
#endif
|
|
|
|
if (errno == EPERM) rb_sys_fail(0);
|
2001-02-13 00:09:11 -05:00
|
|
|
rb_raise(rb_eDBMError, "dbm_store failed");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return valstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_length(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
int i = 0;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
|
|
|
|
|
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
dbmp->di_size = i;
|
|
|
|
|
|
|
|
return INT2FIX(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_empty_p(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
int i = 0;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (dbmp->di_size < 0) {
|
|
|
|
dbm = dbmp->di_dbm;
|
|
|
|
|
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = dbmp->di_size;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (i == 0) return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_each_value(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_yield(rb_tainted_str_new(val.dptr, val.dsize));
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_each_key(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_yield(rb_tainted_str_new(key.dptr, key.dsize));
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_each_pair(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
DBM *dbm;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
VALUE keystr, valstr;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
keystr = rb_tainted_str_new(key.dptr, key.dsize);
|
|
|
|
valstr = rb_tainted_str_new(val.dptr, val.dsize);
|
|
|
|
rb_yield(rb_assoc_new(keystr, valstr));
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_keys(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE ary;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new();
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, rb_tainted_str_new(key.dptr, key.dsize));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_values(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE ary;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new();
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, rb_tainted_str_new(val.dptr, val.dsize));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_has_key(obj, keystr)
|
|
|
|
VALUE obj, keystr;
|
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(keystr);
|
1998-01-16 07:13:05 -05:00
|
|
|
key.dptr = RSTRING(keystr)->ptr;
|
|
|
|
key.dsize = RSTRING(keystr)->len;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (val.dptr) return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_has_value(obj, valstr)
|
|
|
|
VALUE obj, valstr;
|
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(valstr);
|
1998-01-16 07:13:05 -05:00
|
|
|
val.dptr = RSTRING(valstr)->ptr;
|
|
|
|
val.dsize = RSTRING(valstr)->len;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
|
|
|
if (val.dsize == RSTRING(valstr)->len &&
|
|
|
|
memcmp(val.dptr, RSTRING(valstr)->ptr, val.dsize) == 0)
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_to_a(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE ary;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new();
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, rb_assoc_new(rb_tainted_str_new(key.dptr, key.dsize),
|
2004-12-08 21:19:50 -05:00
|
|
|
rb_tainted_str_new(val.dptr, val.dsize)));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2000-07-04 03:04:42 -04:00
|
|
|
static VALUE
|
|
|
|
fdbm_to_hash(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE hash;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2000-07-04 03:04:42 -04:00
|
|
|
hash = rb_hash_new();
|
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
|
|
|
rb_hash_aset(hash, rb_tainted_str_new(key.dptr, key.dsize),
|
|
|
|
rb_tainted_str_new(val.dptr, val.dsize));
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fdbm_reject(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
return rb_hash_delete_if(fdbm_to_hash(obj));
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
void
|
1998-01-16 07:13:05 -05:00
|
|
|
Init_dbm()
|
|
|
|
{
|
2001-05-24 02:10:36 -04:00
|
|
|
rb_cDBM = rb_define_class("DBM", rb_cObject);
|
2001-02-13 00:09:11 -05:00
|
|
|
rb_eDBMError = rb_define_class("DBMError", rb_eStandardError);
|
2001-05-24 02:10:36 -04:00
|
|
|
rb_include_module(rb_cDBM, rb_mEnumerable);
|
|
|
|
|
2002-12-20 06:30:13 -05:00
|
|
|
rb_define_alloc_func(rb_cDBM, fdbm_alloc);
|
2001-05-24 02:10:36 -04:00
|
|
|
rb_define_singleton_method(rb_cDBM, "open", fdbm_s_open, -1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cDBM, "initialize", fdbm_initialize, -1);
|
|
|
|
rb_define_method(rb_cDBM, "close", fdbm_close, 0);
|
2005-06-20 04:17:53 -04:00
|
|
|
rb_define_method(rb_cDBM, "closed?", fdbm_closed, 0);
|
2001-05-24 02:10:36 -04:00
|
|
|
rb_define_method(rb_cDBM, "[]", fdbm_aref, 1);
|
|
|
|
rb_define_method(rb_cDBM, "fetch", fdbm_fetch_m, -1);
|
|
|
|
rb_define_method(rb_cDBM, "[]=", fdbm_store, 2);
|
|
|
|
rb_define_method(rb_cDBM, "store", fdbm_store, 2);
|
|
|
|
rb_define_method(rb_cDBM, "index", fdbm_index, 1);
|
2004-10-19 06:25:23 -04:00
|
|
|
rb_define_method(rb_cDBM, "select", fdbm_select, 0);
|
2003-05-06 02:51:31 -04:00
|
|
|
rb_define_method(rb_cDBM, "values_at", fdbm_values_at, -1);
|
2001-05-24 02:10:36 -04:00
|
|
|
rb_define_method(rb_cDBM, "length", fdbm_length, 0);
|
|
|
|
rb_define_method(rb_cDBM, "size", fdbm_length, 0);
|
|
|
|
rb_define_method(rb_cDBM, "empty?", fdbm_empty_p, 0);
|
|
|
|
rb_define_method(rb_cDBM, "each", fdbm_each_pair, 0);
|
|
|
|
rb_define_method(rb_cDBM, "each_value", fdbm_each_value, 0);
|
|
|
|
rb_define_method(rb_cDBM, "each_key", fdbm_each_key, 0);
|
|
|
|
rb_define_method(rb_cDBM, "each_pair", fdbm_each_pair, 0);
|
|
|
|
rb_define_method(rb_cDBM, "keys", fdbm_keys, 0);
|
|
|
|
rb_define_method(rb_cDBM, "values", fdbm_values, 0);
|
|
|
|
rb_define_method(rb_cDBM, "shift", fdbm_shift, 0);
|
|
|
|
rb_define_method(rb_cDBM, "delete", fdbm_delete, 1);
|
|
|
|
rb_define_method(rb_cDBM, "delete_if", fdbm_delete_if, 0);
|
|
|
|
rb_define_method(rb_cDBM, "reject!", fdbm_delete_if, 0);
|
|
|
|
rb_define_method(rb_cDBM, "reject", fdbm_reject, 0);
|
|
|
|
rb_define_method(rb_cDBM, "clear", fdbm_clear, 0);
|
|
|
|
rb_define_method(rb_cDBM,"invert", fdbm_invert, 0);
|
|
|
|
rb_define_method(rb_cDBM,"update", fdbm_update, 1);
|
|
|
|
rb_define_method(rb_cDBM,"replace", fdbm_replace, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cDBM, "include?", fdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cDBM, "has_key?", fdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cDBM, "member?", fdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cDBM, "has_value?", fdbm_has_value, 1);
|
|
|
|
rb_define_method(rb_cDBM, "key?", fdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cDBM, "value?", fdbm_has_value, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cDBM, "to_a", fdbm_to_a, 0);
|
|
|
|
rb_define_method(rb_cDBM, "to_hash", fdbm_to_hash, 0);
|
2003-05-06 02:51:31 -04:00
|
|
|
|
2004-05-16 08:03:23 -04:00
|
|
|
/* flags for dbm_open() */
|
|
|
|
rb_define_const(rb_cDBM, "READER", INT2FIX(O_RDONLY|RUBY_DBM_RW_BIT));
|
|
|
|
rb_define_const(rb_cDBM, "WRITER", INT2FIX(O_RDWR|RUBY_DBM_RW_BIT));
|
|
|
|
rb_define_const(rb_cDBM, "WRCREAT", INT2FIX(O_RDWR|O_CREAT|RUBY_DBM_RW_BIT));
|
|
|
|
rb_define_const(rb_cDBM, "NEWDB", INT2FIX(O_RDWR|O_CREAT|O_TRUNC|RUBY_DBM_RW_BIT));
|
|
|
|
|
2003-05-06 02:51:31 -04:00
|
|
|
#ifdef DB_VERSION_STRING
|
|
|
|
rb_define_const(rb_cDBM, "VERSION", rb_str_new2(DB_VERSION_STRING));
|
2004-06-11 09:33:47 -04:00
|
|
|
#else
|
|
|
|
rb_define_const(rb_cDBM, "VERSION", rb_str_new2("unknown"));
|
2003-05-06 02:51:31 -04:00
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|