1999-08-13 09:37:52 +04:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
gdbm.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
modified at: Mon Jan 24 15:59:52 JST 1994
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
|
|
|
|
#include <gdbm.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2002-02-27 07:52:21 +03:00
|
|
|
static VALUE rb_cGDBM, rb_eGDBMError, rb_eGDBMFatalError;
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2004-04-15 12:15:27 +04:00
|
|
|
#define RUBY_GDBM_RW_BIT 0x20000000
|
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
#define MY_BLOCK_SIZE (2048)
|
2001-05-06 19:06:00 +04:00
|
|
|
#define MY_FATAL_FUNC rb_gdbm_fatal
|
2002-02-27 07:52:21 +03:00
|
|
|
static void
|
2006-02-13 07:53:22 +03:00
|
|
|
rb_gdbm_fatal(char *msg)
|
2001-05-06 19:06:00 +04:00
|
|
|
{
|
2001-05-06 20:37:38 +04:00
|
|
|
rb_raise(rb_eGDBMFatalError, msg);
|
2001-05-06 19:06:00 +04:00
|
|
|
}
|
1999-08-13 09:37:52 +04:00
|
|
|
|
|
|
|
struct dbmdata {
|
|
|
|
int di_size;
|
|
|
|
GDBM_FILE di_dbm;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2006-02-13 07:53:22 +03:00
|
|
|
closed_dbm(void)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
rb_raise(rb_eRuntimeError, "closed GDBM file");
|
|
|
|
}
|
|
|
|
|
2002-09-24 10:00:03 +04:00
|
|
|
#define GetDBM(obj, dbmp) do {\
|
1999-08-13 09:37:52 +04:00
|
|
|
Data_Get_Struct(obj, struct dbmdata, dbmp);\
|
2001-02-13 08:09:11 +03:00
|
|
|
if (dbmp == 0) closed_dbm();\
|
1999-08-13 09:37:52 +04:00
|
|
|
if (dbmp->di_dbm == 0) closed_dbm();\
|
2002-09-24 10:00:03 +04:00
|
|
|
} while (0)
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
#define GetDBM2(obj, data, dbm) {\
|
|
|
|
GetDBM(obj, data);\
|
|
|
|
(dbm) = dbmp->di_dbm;\
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
static void
|
2006-02-13 07:53:22 +03:00
|
|
|
free_dbm(struct dbmdata *dbmp)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
2001-02-13 08:09:11 +03:00
|
|
|
if (dbmp) {
|
|
|
|
if (dbmp->di_dbm) gdbm_close(dbmp->di_dbm);
|
|
|
|
free(dbmp);
|
|
|
|
}
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
|
|
|
|
2002-02-27 07:52:21 +03:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_close(VALUE obj)
|
2002-02-27 07:52:21 +03:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
|
|
|
|
GetDBM(obj, dbmp);
|
|
|
|
gdbm_close(dbmp->di_dbm);
|
|
|
|
dbmp->di_dbm = 0;
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
2001-02-13 08:09:11 +03:00
|
|
|
|
2005-06-20 12:17:53 +04:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_closed(VALUE obj)
|
2005-06-20 12:17:53 +04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
|
|
|
|
Data_Get_Struct(obj, struct dbmdata, dbmp);
|
|
|
|
if (dbmp == 0)
|
|
|
|
return Qtrue;
|
|
|
|
if (dbmp->di_dbm == 0)
|
|
|
|
return Qtrue;
|
|
|
|
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_s_alloc(VALUE klass)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
2002-09-24 10:00:03 +04:00
|
|
|
return Data_Wrap_Struct(klass, 0, free_dbm, 0);
|
2001-02-13 08:09:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_initialize(int argc, VALUE *argv, VALUE obj)
|
2001-02-13 08:09:11 +03:00
|
|
|
{
|
|
|
|
VALUE file, vmode, vflags;
|
1999-08-13 09:37:52 +04:00
|
|
|
GDBM_FILE dbm;
|
|
|
|
struct dbmdata *dbmp;
|
2001-02-13 08:09:11 +03:00
|
|
|
int mode, flags = 0;
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2001-02-13 08:09:11 +03:00
|
|
|
if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
|
1999-08-13 09:37:52 +04:00
|
|
|
mode = 0666; /* default value */
|
|
|
|
}
|
|
|
|
else if (NIL_P(vmode)) {
|
|
|
|
mode = -1; /* return nil if DB not exist */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mode = NUM2INT(vmode);
|
|
|
|
}
|
2001-02-13 08:09:11 +03:00
|
|
|
|
|
|
|
if (!NIL_P(vflags))
|
|
|
|
flags = NUM2INT(vflags);
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(file);
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2004-04-15 12:15:27 +04:00
|
|
|
if (flags & RUBY_GDBM_RW_BIT) {
|
|
|
|
flags &= ~RUBY_GDBM_RW_BIT;
|
1999-08-13 09:37:52 +04:00
|
|
|
dbm = gdbm_open(RSTRING(file)->ptr, MY_BLOCK_SIZE,
|
2004-04-15 12:15:27 +04:00
|
|
|
flags, mode, MY_FATAL_FUNC);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dbm = 0;
|
|
|
|
if (mode >= 0)
|
|
|
|
dbm = gdbm_open(RSTRING(file)->ptr, MY_BLOCK_SIZE,
|
|
|
|
GDBM_WRCREAT|flags, mode, MY_FATAL_FUNC);
|
|
|
|
if (!dbm)
|
|
|
|
dbm = gdbm_open(RSTRING(file)->ptr, MY_BLOCK_SIZE,
|
|
|
|
GDBM_WRITER|flags, 0, MY_FATAL_FUNC);
|
|
|
|
if (!dbm)
|
|
|
|
dbm = gdbm_open(RSTRING(file)->ptr, MY_BLOCK_SIZE,
|
|
|
|
GDBM_READER|flags, 0, MY_FATAL_FUNC);
|
|
|
|
}
|
1999-08-13 09:37:52 +04:00
|
|
|
|
|
|
|
if (!dbm) {
|
|
|
|
if (mode == -1) return Qnil;
|
2001-02-13 08:09:11 +03:00
|
|
|
|
|
|
|
if (gdbm_errno == GDBM_FILE_OPEN_ERROR ||
|
|
|
|
gdbm_errno == GDBM_CANT_BE_READER ||
|
|
|
|
gdbm_errno == GDBM_CANT_BE_WRITER)
|
|
|
|
rb_sys_fail(RSTRING(file)->ptr);
|
|
|
|
else
|
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
|
|
|
|
2001-02-13 08:09:11 +03:00
|
|
|
dbmp = ALLOC(struct dbmdata);
|
2002-09-24 10:00:03 +04:00
|
|
|
free_dbm(DATA_PTR(obj));
|
2001-02-13 08:09:11 +03:00
|
|
|
DATA_PTR(obj) = dbmp;
|
1999-08-13 09:37:52 +04:00
|
|
|
dbmp->di_dbm = dbm;
|
|
|
|
dbmp->di_size = -1;
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2001-02-13 08:09:11 +03:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_s_open(int argc, VALUE *argv, VALUE klass)
|
2001-02-13 08:09:11 +03:00
|
|
|
{
|
|
|
|
VALUE obj = Data_Wrap_Struct(klass, 0, free_dbm, 0);
|
|
|
|
|
|
|
|
if (NIL_P(fgdbm_initialize(argc, argv, obj))) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rb_block_given_p()) {
|
|
|
|
return rb_ensure(rb_yield, obj, fgdbm_close, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
rb_gdbm_fetch(GDBM_FILE dbm, datum key)
|
2001-02-13 08:09:11 +03:00
|
|
|
{
|
|
|
|
datum val;
|
2001-12-13 11:19:09 +03:00
|
|
|
VALUE str;
|
2001-02-13 08:09:11 +03:00
|
|
|
|
|
|
|
val = gdbm_fetch(dbm, key);
|
2001-05-02 08:22:21 +04:00
|
|
|
if (val.dptr == 0)
|
|
|
|
return Qnil;
|
2001-02-13 08:09:11 +03:00
|
|
|
|
2001-12-13 11:19:09 +03:00
|
|
|
str = rb_obj_alloc(rb_cString);
|
2001-10-03 11:19:19 +04:00
|
|
|
RSTRING(str)->len = val.dsize;
|
2001-12-10 10:24:00 +03:00
|
|
|
RSTRING(str)->aux.capa = val.dsize;
|
2001-10-03 11:19:19 +04:00
|
|
|
RSTRING(str)->ptr = REALLOC_N(val.dptr,char,val.dsize+1);
|
2001-10-03 11:52:09 +04:00
|
|
|
RSTRING(str)->ptr[val.dsize] = '\0';
|
2001-05-02 08:22:21 +04:00
|
|
|
|
|
|
|
OBJ_TAINT(str);
|
|
|
|
return (VALUE)str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
rb_gdbm_fetch2(GDBM_FILE dbm, VALUE keystr)
|
2001-05-02 08:22:21 +04:00
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
|
|
|
|
StringValue(keystr);
|
|
|
|
key.dptr = RSTRING(keystr)->ptr;
|
|
|
|
key.dsize = RSTRING(keystr)->len;
|
|
|
|
|
|
|
|
return rb_gdbm_fetch(dbm, key);
|
|
|
|
}
|
|
|
|
|
2001-05-06 19:06:00 +04:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
rb_gdbm_fetch3(VALUE obj, VALUE keystr)
|
2001-05-06 19:06:00 +04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-06 19:06:00 +04:00
|
|
|
return rb_gdbm_fetch2(dbm, keystr);
|
|
|
|
}
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
rb_gdbm_firstkey(GDBM_FILE dbm)
|
2001-05-02 08:22:21 +04:00
|
|
|
{
|
|
|
|
datum key;
|
2001-12-13 11:19:09 +03:00
|
|
|
VALUE str;
|
2001-05-02 08:22:21 +04:00
|
|
|
|
|
|
|
key = gdbm_firstkey(dbm);
|
|
|
|
if (key.dptr == 0)
|
|
|
|
return Qnil;
|
|
|
|
|
2001-12-13 11:19:09 +03:00
|
|
|
str = rb_obj_alloc(rb_cString);
|
2001-10-03 11:19:19 +04:00
|
|
|
RSTRING(str)->len = key.dsize;
|
2001-12-10 10:24:00 +03:00
|
|
|
RSTRING(str)->aux.capa = key.dsize;
|
2001-10-03 11:19:19 +04:00
|
|
|
RSTRING(str)->ptr = REALLOC_N(key.dptr,char,key.dsize+1);
|
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
|
2001-05-02 08:22:21 +04:00
|
|
|
|
|
|
|
OBJ_TAINT(str);
|
2002-02-27 07:52:21 +03:00
|
|
|
return str;
|
2001-05-02 08:22:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
rb_gdbm_nextkey(GDBM_FILE dbm, VALUE keystr)
|
2001-05-02 08:22:21 +04:00
|
|
|
{
|
|
|
|
datum key, key2;
|
2001-12-13 11:19:09 +03:00
|
|
|
VALUE str;
|
2001-05-02 08:22:21 +04:00
|
|
|
|
|
|
|
key.dptr = RSTRING(keystr)->ptr;
|
|
|
|
key.dsize = RSTRING(keystr)->len;
|
|
|
|
key2 = gdbm_nextkey(dbm, key);
|
|
|
|
if (key2.dptr == 0)
|
|
|
|
return Qnil;
|
|
|
|
|
2001-12-13 11:19:09 +03:00
|
|
|
str = rb_obj_alloc(rb_cString);
|
2001-10-03 11:19:19 +04:00
|
|
|
RSTRING(str)->len = key2.dsize;
|
2001-12-10 10:24:00 +03:00
|
|
|
RSTRING(str)->aux.capa = key2.dsize;
|
2001-10-03 11:19:19 +04:00
|
|
|
RSTRING(str)->ptr = REALLOC_N(key2.dptr,char,key2.dsize+1);
|
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
|
2001-05-02 08:22:21 +04:00
|
|
|
|
|
|
|
OBJ_TAINT(str);
|
2002-02-27 07:52:21 +03:00
|
|
|
return str;
|
2001-02-13 08:09:11 +03:00
|
|
|
}
|
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE valstr;
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2001-05-06 19:06:00 +04:00
|
|
|
valstr = rb_gdbm_fetch3(obj, keystr);
|
2001-05-02 08:22:21 +04:00
|
|
|
if (NIL_P(valstr)) {
|
2000-07-04 11:04:42 +04:00
|
|
|
if (ifnone == Qnil && rb_block_given_p())
|
2001-05-06 19:06:00 +04:00
|
|
|
return rb_yield(keystr);
|
2000-07-04 11:04:42 +04:00
|
|
|
return ifnone;
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
return valstr;
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
|
|
|
|
2000-07-04 11:04:42 +04:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_aref(VALUE obj, VALUE keystr)
|
2000-07-04 11:04:42 +04:00
|
|
|
{
|
2001-05-06 19:06:00 +04:00
|
|
|
return rb_gdbm_fetch3(obj, keystr);
|
2000-07-04 11:04:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_fetch_m(int argc, VALUE *argv, VALUE obj)
|
2000-07-04 11:04:42 +04:00
|
|
|
{
|
2001-02-13 08:09:11 +03:00
|
|
|
VALUE keystr, valstr, ifnone;
|
2000-07-04 11:04:42 +04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &keystr, &ifnone);
|
2001-02-13 08:09:11 +03:00
|
|
|
valstr = fgdbm_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 11:04:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_index(VALUE obj, VALUE valstr)
|
2000-07-04 11:04:42 +04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE keystr, valstr2;
|
2000-07-04 11:04:42 +04:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(valstr);
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 08:22:21 +04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
|
|
|
valstr2 = rb_gdbm_fetch2(dbm, keystr);
|
|
|
|
if (!NIL_P(valstr2) &&
|
|
|
|
RSTRING(valstr)->len == RSTRING(valstr2)->len &&
|
|
|
|
memcmp(RSTRING(valstr)->ptr, RSTRING(valstr2)->ptr,
|
|
|
|
RSTRING(valstr)->len) == 0) {
|
|
|
|
return keystr;
|
|
|
|
}
|
2000-07-04 11:04:42 +04:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_select(VALUE obj)
|
2002-02-27 07:52:21 +03:00
|
|
|
{
|
2004-06-11 17:33:47 +04:00
|
|
|
VALUE new = rb_ary_new();
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
VALUE keystr;
|
2002-02-27 07:52:21 +03:00
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2004-06-11 17:33:47 +04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
VALUE assoc = rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr));
|
2004-12-09 05:19:50 +03:00
|
|
|
VALUE v = rb_yield(assoc);
|
2003-05-06 10:51:31 +04:00
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
if (RTEST(v)) {
|
2004-06-11 17:33:47 +04:00
|
|
|
rb_ary_push(new, assoc);
|
2004-12-09 05:19:50 +03:00
|
|
|
}
|
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2002-02-27 07:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2003-05-06 10:51:31 +04:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_values_at(int argc, VALUE *argv, VALUE obj)
|
2003-05-06 10:51:31 +04:00
|
|
|
{
|
|
|
|
VALUE new = rb_ary_new2(argc);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
rb_ary_push(new, rb_gdbm_fetch3(obj, argv[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2004-05-07 12:44:24 +04:00
|
|
|
static void
|
2006-02-13 07:53:22 +03:00
|
|
|
rb_gdbm_modify(VALUE obj)
|
2004-05-07 12:44:24 +04:00
|
|
|
{
|
|
|
|
rb_secure(4);
|
2004-05-08 12:12:08 +04:00
|
|
|
if (OBJ_FROZEN(obj)) rb_error_frozen("GDBM");
|
2004-05-07 12:44:24 +04:00
|
|
|
}
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
rb_gdbm_delete(VALUE obj, VALUE keystr)
|
2001-05-02 08:22:21 +04:00
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2004-05-07 12:44:24 +04:00
|
|
|
rb_gdbm_modify(obj);
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(keystr);
|
|
|
|
key.dptr = RSTRING(keystr)->ptr;
|
|
|
|
key.dsize = RSTRING(keystr)->len;
|
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 08:22:21 +04:00
|
|
|
if (!gdbm_exists(dbm, key)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gdbm_delete(dbm, key)) {
|
|
|
|
dbmp->di_size = -1;
|
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
|
|
|
}
|
|
|
|
else if (dbmp->di_size >= 0) {
|
|
|
|
dbmp->di_size--;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_delete(VALUE obj, VALUE keystr)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
2001-05-06 19:06:00 +04:00
|
|
|
VALUE valstr;
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2001-05-06 19:06:00 +04:00
|
|
|
valstr = fgdbm_fetch(obj, keystr, Qnil);
|
|
|
|
rb_gdbm_delete(obj, keystr);
|
|
|
|
return valstr;
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_shift(VALUE obj)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
VALUE keystr, valstr;
|
|
|
|
|
2004-05-07 12:44:24 +04:00
|
|
|
rb_gdbm_modify(obj);
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 08:22:21 +04:00
|
|
|
keystr = rb_gdbm_firstkey(dbm);
|
|
|
|
if (NIL_P(keystr)) return Qnil;
|
|
|
|
valstr = rb_gdbm_fetch2(dbm, keystr);
|
|
|
|
rb_gdbm_delete(obj, keystr);
|
1999-08-13 09:37:52 +04:00
|
|
|
|
|
|
|
return rb_assoc_new(keystr, valstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_delete_if(VALUE obj)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
VALUE keystr, valstr;
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE ret, ary = rb_ary_new();
|
|
|
|
int i, status = 0, n;
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2004-05-07 12:44:24 +04:00
|
|
|
rb_gdbm_modify(obj);
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 08:22:21 +04:00
|
|
|
n = dbmp->di_size;
|
|
|
|
dbmp->di_size = -1;
|
|
|
|
|
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
|
|
|
valstr = rb_gdbm_fetch2(dbm, keystr);
|
2002-02-27 07:52:21 +03:00
|
|
|
ret = rb_protect(rb_yield, rb_assoc_new(keystr, valstr), &status);
|
2001-05-02 08:22:21 +04:00
|
|
|
if (status != 0) break;
|
|
|
|
if (RTEST(ret)) rb_ary_push(ary, keystr);
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
|
|
|
|
for (i = 0; i < RARRAY(ary)->len; i++)
|
|
|
|
rb_gdbm_delete(obj, RARRAY(ary)->ptr[i]);
|
|
|
|
if (status) rb_jump_tag(status);
|
|
|
|
if (n > 0) dbmp->di_size = n - RARRAY(ary)->len;
|
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_clear(VALUE obj)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
2001-05-06 19:06:00 +04:00
|
|
|
datum key, nextkey;
|
1999-08-13 09:37:52 +04:00
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2004-05-07 12:44:24 +04:00
|
|
|
rb_gdbm_modify(obj);
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 09:37:52 +04:00
|
|
|
dbmp->di_size = -1;
|
2001-05-02 08:22:21 +04:00
|
|
|
|
2001-05-06 19:06:00 +04:00
|
|
|
#if 0
|
2001-05-02 08:22:21 +04:00
|
|
|
while (key = gdbm_firstkey(dbm), key.dptr) {
|
1999-08-13 09:37:52 +04:00
|
|
|
if (gdbm_delete(dbm, key)) {
|
2001-05-02 08:22:21 +04:00
|
|
|
free(key.dptr);
|
2001-02-13 08:09:11 +03:00
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
free(key.dptr);
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
2001-05-06 19:06:00 +04:00
|
|
|
#else
|
|
|
|
while (key = gdbm_firstkey(dbm), key.dptr) {
|
|
|
|
for (; key.dptr; key = nextkey) {
|
|
|
|
nextkey = gdbm_nextkey(dbm, key);
|
|
|
|
if (gdbm_delete(dbm, key)) {
|
|
|
|
free(key.dptr);
|
|
|
|
if (nextkey.dptr) free(nextkey.dptr);
|
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
|
|
|
}
|
|
|
|
free(key.dptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2001-05-02 08:22:21 +04:00
|
|
|
dbmp->di_size = 0;
|
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_invert(VALUE obj)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
VALUE keystr, valstr;
|
|
|
|
VALUE hash = rb_hash_new();
|
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 08:22:21 +04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
valstr = rb_gdbm_fetch2(dbm, keystr);
|
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
rb_hash_aset(hash, valstr, keystr);
|
|
|
|
}
|
2000-04-10 09:48:43 +04:00
|
|
|
return hash;
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2004-05-07 12:44:24 +04:00
|
|
|
rb_gdbm_modify(obj);
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(keystr);
|
2004-11-14 18:41:40 +03:00
|
|
|
StringValue(valstr);
|
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
key.dptr = RSTRING(keystr)->ptr;
|
|
|
|
key.dsize = RSTRING(keystr)->len;
|
|
|
|
|
|
|
|
val.dptr = RSTRING(valstr)->ptr;
|
|
|
|
val.dsize = RSTRING(valstr)->len;
|
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 09:37:52 +04:00
|
|
|
dbmp->di_size = -1;
|
|
|
|
if (gdbm_store(dbm, key, val, GDBM_REPLACE)) {
|
|
|
|
if (errno == EPERM) rb_sys_fail(0);
|
2001-02-13 08:09:11 +03:00
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return valstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
update_i(VALUE pair, VALUE dbm)
|
|
|
|
{
|
|
|
|
Check_Type(pair, T_ARRAY);
|
|
|
|
if (RARRAY(pair)->len < 2) {
|
|
|
|
rb_raise(rb_eArgError, "pair must be [key, value]");
|
|
|
|
}
|
|
|
|
fgdbm_store(dbm, RARRAY(pair)->ptr[0], RARRAY(pair)->ptr[1]);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fgdbm_update(VALUE obj, VALUE other)
|
|
|
|
{
|
|
|
|
rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fgdbm_replace(VALUE obj, VALUE other)
|
|
|
|
{
|
|
|
|
fgdbm_clear(obj);
|
|
|
|
rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fgdbm_length(VALUE obj)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
2001-05-02 08:22:21 +04:00
|
|
|
datum key, nextkey;
|
1999-08-13 09:37:52 +04:00
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
int i = 0;
|
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 09:37:52 +04:00
|
|
|
if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
|
|
|
|
nextkey = gdbm_nextkey(dbm, key);
|
|
|
|
free(key.dptr);
|
1999-08-13 09:37:52 +04:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
dbmp->di_size = i;
|
|
|
|
|
|
|
|
return INT2FIX(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_empty_p(VALUE obj)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2000-06-16 11:44:16 +04:00
|
|
|
GetDBM(obj, dbmp);
|
1999-08-13 09:37:52 +04:00
|
|
|
if (dbmp->di_size < 0) {
|
|
|
|
dbm = dbmp->di_dbm;
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
key = gdbm_firstkey(dbm);
|
|
|
|
if (key.dptr) {
|
|
|
|
free(key.dptr);
|
|
|
|
return Qfalse;
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
return Qtrue;
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
|
|
|
|
if (dbmp->di_size == 0) return Qtrue;
|
1999-08-13 09:37:52 +04:00
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_each_value(VALUE obj)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE keystr;
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 08:22:21 +04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
|
|
|
rb_yield(rb_gdbm_fetch2(dbm, keystr));
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_each_key(VALUE obj)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE keystr;
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 08:22:21 +04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
2002-02-27 07:52:21 +03:00
|
|
|
rb_yield(keystr);
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_each_pair(VALUE obj)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
struct dbmdata *dbmp;
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE keystr;
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 08:22:21 +04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
2002-02-27 07:52:21 +03:00
|
|
|
rb_yield(rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_keys(VALUE obj)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE keystr, ary;
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 09:37:52 +04:00
|
|
|
ary = rb_ary_new();
|
2001-05-02 08:22:21 +04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
|
|
|
rb_ary_push(ary, keystr);
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_values(VALUE obj)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
2001-05-02 08:22:21 +04:00
|
|
|
datum key, nextkey;
|
1999-08-13 09:37:52 +04:00
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE valstr, ary;
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 09:37:52 +04:00
|
|
|
ary = rb_ary_new();
|
2001-05-02 08:22:21 +04:00
|
|
|
for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
|
|
|
|
nextkey = gdbm_nextkey(dbm, key);
|
|
|
|
valstr = rb_gdbm_fetch(dbm, key);
|
|
|
|
free(key.dptr);
|
|
|
|
rb_ary_push(ary, valstr);
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_has_key(VALUE obj, VALUE keystr)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
2001-02-08 12:19:27 +03:00
|
|
|
datum key;
|
1999-08-13 09:37:52 +04:00
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(keystr);
|
1999-08-13 09:37:52 +04:00
|
|
|
key.dptr = RSTRING(keystr)->ptr;
|
|
|
|
key.dsize = RSTRING(keystr)->len;
|
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-02-08 12:19:27 +03:00
|
|
|
if (gdbm_exists(dbm, key))
|
|
|
|
return Qtrue;
|
1999-08-13 09:37:52 +04:00
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_has_value(VALUE obj, VALUE valstr)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE keystr, valstr2;
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(valstr);
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 08:22:21 +04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
|
|
|
valstr2 = rb_gdbm_fetch2(dbm, keystr);
|
|
|
|
|
|
|
|
if (!NIL_P(valstr2) &&
|
|
|
|
RSTRING(valstr)->len == RSTRING(valstr2)->len &&
|
|
|
|
memcmp(RSTRING(valstr)->ptr, RSTRING(valstr2)->ptr,
|
|
|
|
RSTRING(valstr)->len) == 0) {
|
1999-08-13 09:37:52 +04:00
|
|
|
return Qtrue;
|
2001-05-02 08:22:21 +04:00
|
|
|
}
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_to_a(VALUE obj)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE keystr, ary;
|
1999-08-13 09:37:52 +04:00
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 09:37:52 +04:00
|
|
|
ary = rb_ary_new();
|
2001-05-02 08:22:21 +04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
2002-02-27 07:52:21 +03:00
|
|
|
rb_ary_push(ary, rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_reorganize(VALUE obj)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2004-05-07 12:44:24 +04:00
|
|
|
rb_gdbm_modify(obj);
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 09:37:52 +04:00
|
|
|
gdbm_reorganize(dbm);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2001-02-13 08:09:11 +03:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_sync(VALUE obj)
|
2001-02-13 08:09:11 +03:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2004-05-07 12:44:24 +04:00
|
|
|
rb_gdbm_modify(obj);
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-02-13 08:09:11 +03:00
|
|
|
gdbm_sync(dbm);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_set_cachesize(VALUE obj, VALUE val)
|
2001-02-13 08:09:11 +03:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
int optval;
|
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-02-13 08:09:11 +03:00
|
|
|
optval = FIX2INT(val);
|
|
|
|
if (gdbm_setopt(dbm, GDBM_CACHESIZE, &optval, sizeof(optval)) == -1) {
|
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_set_fastmode(VALUE obj, VALUE val)
|
2001-02-13 08:09:11 +03:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
int optval;
|
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-02-13 08:09:11 +03:00
|
|
|
optval = 0;
|
|
|
|
if (RTEST(val))
|
|
|
|
optval = 1;
|
|
|
|
|
|
|
|
if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) {
|
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_set_syncmode(VALUE obj, VALUE val)
|
2001-02-13 08:09:11 +03:00
|
|
|
{
|
|
|
|
#if !defined(GDBM_SYNCMODE)
|
|
|
|
fgdbm_set_fastmode(obj, RTEST(val) ? Qfalse : Qtrue);
|
|
|
|
return val;
|
|
|
|
#else
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
int optval;
|
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-02-13 08:09:11 +03:00
|
|
|
optval = 0;
|
|
|
|
if (RTEST(val))
|
|
|
|
optval = 1;
|
|
|
|
|
|
|
|
if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) {
|
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-07-04 11:04:42 +04:00
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_to_hash(VALUE obj)
|
2000-07-04 11:04:42 +04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE keystr, hash;
|
2000-07-04 11:04:42 +04:00
|
|
|
|
2004-12-09 05:19:50 +03:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2000-07-04 11:04:42 +04:00
|
|
|
hash = rb_hash_new();
|
2001-05-02 08:22:21 +04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
|
|
|
rb_hash_aset(hash, keystr, rb_gdbm_fetch2(dbm, keystr));
|
2000-07-04 11:04:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-13 07:53:22 +03:00
|
|
|
fgdbm_reject(VALUE obj)
|
2000-07-04 11:04:42 +04:00
|
|
|
{
|
|
|
|
return rb_hash_delete_if(fgdbm_to_hash(obj));
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:37:52 +04:00
|
|
|
void
|
2006-02-13 07:53:22 +03:00
|
|
|
Init_gdbm(void)
|
1999-08-13 09:37:52 +04:00
|
|
|
{
|
2002-02-27 07:52:21 +03:00
|
|
|
rb_cGDBM = rb_define_class("GDBM", rb_cObject);
|
2001-02-13 08:09:11 +03:00
|
|
|
rb_eGDBMError = rb_define_class("GDBMError", rb_eStandardError);
|
2001-05-06 19:06:00 +04:00
|
|
|
rb_eGDBMFatalError = rb_define_class("GDBMFatalError", rb_eException);
|
2002-02-27 07:52:21 +03:00
|
|
|
rb_include_module(rb_cGDBM, rb_mEnumerable);
|
|
|
|
|
* ext/curses/curses.c, ext/digest/digest.c, ext/dl/handle.c,
ext/dl/ptr.c, ext/dl/sym.c, ext/gdbm/gdbm.c, ext/iconv/iconv.c,
ext/stringio/stringio.c, ext/strscan/strscan.c,
ext/tcltklib/tcltklib.c, ext/win32ole/win32ole.c:
use rb_define_alloc_func().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3193 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-20 14:23:40 +03:00
|
|
|
rb_define_alloc_func(rb_cGDBM, fgdbm_s_alloc);
|
2002-02-27 07:52:21 +03:00
|
|
|
rb_define_singleton_method(rb_cGDBM, "open", fgdbm_s_open, -1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cGDBM, "initialize", fgdbm_initialize, -1);
|
|
|
|
rb_define_method(rb_cGDBM, "close", fgdbm_close, 0);
|
2005-06-20 12:17:53 +04:00
|
|
|
rb_define_method(rb_cGDBM, "closed?", fgdbm_closed, 0);
|
2002-02-27 07:52:21 +03:00
|
|
|
rb_define_method(rb_cGDBM, "[]", fgdbm_aref, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "fetch", fgdbm_fetch_m, -1);
|
|
|
|
rb_define_method(rb_cGDBM, "[]=", fgdbm_store, 2);
|
|
|
|
rb_define_method(rb_cGDBM, "store", fgdbm_store, 2);
|
|
|
|
rb_define_method(rb_cGDBM, "index", fgdbm_index, 1);
|
2004-06-11 17:33:47 +04:00
|
|
|
rb_define_method(rb_cGDBM, "select", fgdbm_select, 0);
|
2003-05-06 10:51:31 +04:00
|
|
|
rb_define_method(rb_cGDBM, "values_at", fgdbm_values_at, -1);
|
2002-02-27 07:52:21 +03:00
|
|
|
rb_define_method(rb_cGDBM, "length", fgdbm_length, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "size", fgdbm_length, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "empty?", fgdbm_empty_p, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "each", fgdbm_each_pair, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "each_value", fgdbm_each_value, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "each_key", fgdbm_each_key, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "each_pair", fgdbm_each_pair, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "keys", fgdbm_keys, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "values", fgdbm_values, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "shift", fgdbm_shift, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "delete", fgdbm_delete, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "delete_if", fgdbm_delete_if, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "reject!", fgdbm_delete_if, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "reject", fgdbm_reject, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "clear", fgdbm_clear, 0);
|
|
|
|
rb_define_method(rb_cGDBM,"invert", fgdbm_invert, 0);
|
|
|
|
rb_define_method(rb_cGDBM,"update", fgdbm_update, 1);
|
|
|
|
rb_define_method(rb_cGDBM,"replace", fgdbm_replace, 1);
|
|
|
|
rb_define_method(rb_cGDBM,"reorganize", fgdbm_reorganize, 0);
|
|
|
|
rb_define_method(rb_cGDBM,"sync", fgdbm_sync, 0);
|
|
|
|
/* rb_define_method(rb_cGDBM,"setopt", fgdbm_setopt, 2); */
|
|
|
|
rb_define_method(rb_cGDBM,"cachesize=", fgdbm_set_cachesize, 1);
|
|
|
|
rb_define_method(rb_cGDBM,"fastmode=", fgdbm_set_fastmode, 1);
|
|
|
|
rb_define_method(rb_cGDBM,"syncmode=", fgdbm_set_syncmode, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cGDBM, "include?", fgdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "has_key?", fgdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "member?", fgdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "has_value?", fgdbm_has_value, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "key?", fgdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "value?", fgdbm_has_value, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cGDBM, "to_a", fgdbm_to_a, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "to_hash", fgdbm_to_hash, 0);
|
2001-02-13 08:09:11 +03:00
|
|
|
|
2004-04-15 12:15:27 +04:00
|
|
|
/* flags for gdbm_open() */
|
|
|
|
rb_define_const(rb_cGDBM, "READER", INT2FIX(GDBM_READER|RUBY_GDBM_RW_BIT));
|
|
|
|
rb_define_const(rb_cGDBM, "WRITER", INT2FIX(GDBM_WRITER|RUBY_GDBM_RW_BIT));
|
|
|
|
rb_define_const(rb_cGDBM, "WRCREAT", INT2FIX(GDBM_WRCREAT|RUBY_GDBM_RW_BIT));
|
|
|
|
rb_define_const(rb_cGDBM, "NEWDB", INT2FIX(GDBM_NEWDB|RUBY_GDBM_RW_BIT));
|
|
|
|
|
2002-02-27 07:52:21 +03:00
|
|
|
rb_define_const(rb_cGDBM, "FAST", INT2FIX(GDBM_FAST));
|
2001-02-13 08:09:11 +03:00
|
|
|
/* this flag is obsolete in gdbm 1.8.
|
|
|
|
On gdbm 1.8, fast mode is default behavior. */
|
|
|
|
|
|
|
|
/* gdbm version 1.8 specific */
|
|
|
|
#if defined(GDBM_SYNC)
|
2002-02-27 07:52:21 +03:00
|
|
|
rb_define_const(rb_cGDBM, "SYNC", INT2FIX(GDBM_SYNC));
|
2001-02-13 08:09:11 +03:00
|
|
|
#endif
|
|
|
|
#if defined(GDBM_NOLOCK)
|
2002-02-27 07:52:21 +03:00
|
|
|
rb_define_const(rb_cGDBM, "NOLOCK", INT2FIX(GDBM_NOLOCK));
|
2001-02-13 08:09:11 +03:00
|
|
|
#endif
|
2002-02-27 07:52:21 +03:00
|
|
|
rb_define_const(rb_cGDBM, "VERSION", rb_str_new2(gdbm_version));
|
1999-08-13 09:37:52 +04:00
|
|
|
}
|