summaryrefslogtreecommitdiffstats
path: root/ext
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-12-09 02:35:29 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-12-09 02:35:29 +0000
commitd6b9e4324c257a789e2e5126d182384232bf9fb6 (patch)
treefcf5a38913b0c32248834920ced013c1a8fe7735 /ext
parent8b6562f36a5a0109aeef570cbd397f45e97ca216 (diff)
downloadruby-d6b9e4324c257a789e2e5126d182384232bf9fb6.tar.gz
ruby-d6b9e4324c257a789e2e5126d182384232bf9fb6.tar.xz
ruby-d6b9e4324c257a789e2e5126d182384232bf9fb6.zip
* ext/dbm/dbm.c (fdbm_select): [ruby-dev:25132]
* ext/sdbm/init.c: ditto. * ext/gdbm/gdbm.c: ditto. git-svn-id: http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8@7517 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/dbm/dbm.c88
-rw-r--r--ext/gdbm/gdbm.c101
-rw-r--r--ext/sdbm/init.c81
3 files changed, 111 insertions, 159 deletions
diff --git a/ext/dbm/dbm.c b/ext/dbm/dbm.c
index b3bd3d2c0..9caf80896 100644
--- a/ext/dbm/dbm.c
+++ b/ext/dbm/dbm.c
@@ -43,6 +43,11 @@ closed_dbm()
if (dbmp->di_dbm == 0) closed_dbm();\
}
+#define GetDBM2(obj, data, dbm) {\
+ GetDBM(obj, data);\
+ (dbm) = dbmp->di_dbm;\
+}
+
static void
free_dbm(dbmp)
struct dbmdata *dbmp;
@@ -161,8 +166,7 @@ fdbm_fetch(obj, keystr, ifnone)
key.dptr = RSTRING(keystr)->ptr;
key.dsize = RSTRING(keystr)->len;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
value = dbm_fetch(dbm, key);
if (value.dptr == 0) {
if (ifnone == Qnil && rb_block_given_p())
@@ -207,8 +211,7 @@ fdbm_index(obj, valstr)
val.dptr = RSTRING(valstr)->ptr;
val.dsize = RSTRING(valstr)->len;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key);
if (val.dsize == RSTRING(valstr)->len &&
@@ -253,17 +256,18 @@ fdbm_select(argc, argv, obj)
if (argc > 0) {
rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc);
}
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
- VALUE assoc;
+ VALUE assoc, v;
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));
- if (RTEST(rb_yield(assoc)))
- rb_ary_push(new, assoc);
- }
+ v = rb_yield(assoc);
+ if (RTEST(v)) {
+ rb_ary_push(new, assoc);
+ }
+ GetDBM2(obj, dbmp, dbm);
+ }
}
else {
rb_warn("DBM#select(index..) is deprecated; use DBM#values_at");
@@ -314,9 +318,7 @@ fdbm_delete(obj, keystr)
key.dptr = RSTRING(keystr)->ptr;
key.dsize = RSTRING(keystr)->len;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
value = dbm_fetch(dbm, key);
if (value.dptr == 0) {
if (rb_block_given_p()) return rb_yield(keystr);
@@ -346,8 +348,7 @@ fdbm_shift(obj)
VALUE keystr, valstr;
fdbm_modify(obj);
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
key = dbm_firstkey(dbm);
@@ -372,8 +373,7 @@ fdbm_delete_if(obj)
int i, status = 0, n;
fdbm_modify(obj);
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
n = dbmp->di_size;
dbmp->di_size = -1;
@@ -384,6 +384,7 @@ fdbm_delete_if(obj)
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);
+ GetDBM2(obj, dbmp, dbm);
}
for (i = 0; i < RARRAY(ary)->len; i++) {
@@ -410,8 +411,7 @@ fdbm_clear(obj)
DBM *dbm;
fdbm_modify(obj);
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
while (key = dbm_firstkey(dbm), key.dptr) {
if (dbm_delete(dbm, key)) {
@@ -433,8 +433,7 @@ fdbm_invert(obj)
VALUE keystr, valstr;
VALUE hash = rb_hash_new();
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
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);
@@ -502,9 +501,8 @@ fdbm_store(obj, keystr, valstr)
val.dptr = RSTRING(valstr)->ptr;
val.dsize = RSTRING(valstr)->len;
- GetDBM(obj, dbmp);
+ GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
- dbm = dbmp->di_dbm;
if (dbm_store(dbm, key, val, DBM_REPLACE)) {
#ifdef HAVE_DBM_CLEARERR
dbm_clearerr(dbm);
@@ -525,9 +523,8 @@ fdbm_length(obj)
DBM *dbm;
int i = 0;
- GetDBM(obj, dbmp);
+ GetDBM2(obj, dbmp, dbm);
if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
- dbm = dbmp->di_dbm;
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
i++;
@@ -546,7 +543,7 @@ fdbm_empty_p(obj)
DBM *dbm;
int i = 0;
- GetDBM(obj, dbmp);
+ GetDBM2(obj, dbmp, dbm);
if (dbmp->di_size < 0) {
dbm = dbmp->di_dbm;
@@ -569,11 +566,11 @@ fdbm_each_value(obj)
struct dbmdata *dbmp;
DBM *dbm;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key);
rb_yield(rb_tainted_str_new(val.dptr, val.dsize));
+ GetDBM2(obj, dbmp, dbm);
}
return obj;
}
@@ -586,10 +583,10 @@ fdbm_each_key(obj)
struct dbmdata *dbmp;
DBM *dbm;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
rb_yield(rb_tainted_str_new(key.dptr, key.dsize));
+ GetDBM2(obj, dbmp, dbm);
}
return obj;
}
@@ -603,14 +600,14 @@ fdbm_each_pair(obj)
struct dbmdata *dbmp;
VALUE keystr, valstr;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
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_yield(rb_assoc_new(keystr, valstr));
+ GetDBM2(obj, dbmp, dbm);
}
return obj;
@@ -625,8 +622,7 @@ fdbm_keys(obj)
DBM *dbm;
VALUE ary;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
@@ -645,9 +641,7 @@ fdbm_values(obj)
DBM *dbm;
VALUE ary;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key);
@@ -669,8 +663,7 @@ fdbm_has_key(obj, keystr)
key.dptr = RSTRING(keystr)->ptr;
key.dsize = RSTRING(keystr)->len;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
val = dbm_fetch(dbm, key);
if (val.dptr) return Qtrue;
return Qfalse;
@@ -688,8 +681,7 @@ fdbm_has_value(obj, valstr)
val.dptr = RSTRING(valstr)->ptr;
val.dsize = RSTRING(valstr)->len;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key);
if (val.dsize == RSTRING(valstr)->len &&
@@ -708,14 +700,12 @@ fdbm_to_a(obj)
DBM *dbm;
VALUE ary;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key);
rb_ary_push(ary, rb_assoc_new(rb_tainted_str_new(key.dptr, key.dsize),
- rb_tainted_str_new(val.dptr, val.dsize)));
+ rb_tainted_str_new(val.dptr, val.dsize)));
}
return ary;
@@ -730,9 +720,7 @@ fdbm_to_hash(obj)
DBM *dbm;
VALUE hash;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
hash = rb_hash_new();
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key);
@@ -769,7 +757,7 @@ Init_dbm()
rb_define_method(rb_cDBM, "index", fdbm_index, 1);
rb_define_method(rb_cDBM, "indexes", fdbm_indexes, -1);
rb_define_method(rb_cDBM, "indices", fdbm_indexes, -1);
- rb_define_method(rb_cDBM, "select", fdbm_select, 0);
+ rb_define_method(rb_cDBM, "select", fdbm_select, -1);
rb_define_method(rb_cDBM, "values_at", fdbm_values_at, -1);
rb_define_method(rb_cDBM, "length", fdbm_length, 0);
rb_define_method(rb_cDBM, "size", fdbm_length, 0);
diff --git a/ext/gdbm/gdbm.c b/ext/gdbm/gdbm.c
index ac208efbe..e59b01add 100644
--- a/ext/gdbm/gdbm.c
+++ b/ext/gdbm/gdbm.c
@@ -44,6 +44,11 @@ closed_dbm()
if (dbmp->di_dbm == 0) closed_dbm();\
} while (0)
+#define GetDBM2(obj, data, dbm) {\
+ GetDBM(obj, data);\
+ (dbm) = dbmp->di_dbm;\
+}
+
static void
free_dbm(dbmp)
struct dbmdata *dbmp;
@@ -202,8 +207,7 @@ rb_gdbm_fetch3(obj, keystr)
struct dbmdata *dbmp;
GDBM_FILE dbm;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
return rb_gdbm_fetch2(dbm, keystr);
}
@@ -299,8 +303,7 @@ fgdbm_index(obj, valstr)
VALUE keystr, valstr2;
StringValue(valstr);
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
@@ -349,15 +352,16 @@ fgdbm_select(argc, argv, obj)
if (argc > 0) {
rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc);
}
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
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));
+ VALUE v = rb_yield(assoc);
- if (RTEST(rb_yield(assoc)))
- rb_ary_push(new, assoc);
+ if (RTEST(v)) {
+ rb_ary_push(new, assoc);
+ }
+ GetDBM2(obj, dbmp, dbm);
}
}
else {
@@ -408,9 +412,7 @@ rb_gdbm_delete(obj, keystr)
key.dptr = RSTRING(keystr)->ptr;
key.dsize = RSTRING(keystr)->len;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
if (!gdbm_exists(dbm, key)) {
return Qnil;
}
@@ -445,9 +447,7 @@ fgdbm_shift(obj)
VALUE keystr, valstr;
rb_gdbm_modify(obj);
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
keystr = rb_gdbm_firstkey(dbm);
if (NIL_P(keystr)) return Qnil;
valstr = rb_gdbm_fetch2(dbm, keystr);
@@ -467,8 +467,7 @@ fgdbm_delete_if(obj)
int i, status = 0, n;
rb_gdbm_modify(obj);
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
n = dbmp->di_size;
dbmp->di_size = -1;
@@ -479,6 +478,7 @@ fgdbm_delete_if(obj)
ret = rb_protect(rb_yield, rb_assoc_new(keystr, valstr), &status);
if (status != 0) break;
if (RTEST(ret)) rb_ary_push(ary, keystr);
+ GetDBM2(obj, dbmp, dbm);
}
for (i = 0; i < RARRAY(ary)->len; i++)
@@ -498,8 +498,7 @@ fgdbm_clear(obj)
GDBM_FILE dbm;
rb_gdbm_modify(obj);
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
#if 0
@@ -537,8 +536,7 @@ fgdbm_invert(obj)
VALUE keystr, valstr;
VALUE hash = rb_hash_new();
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
valstr = rb_gdbm_fetch2(dbm, keystr);
@@ -606,9 +604,8 @@ fgdbm_store(obj, keystr, valstr)
val.dptr = RSTRING(valstr)->ptr;
val.dsize = RSTRING(valstr)->len;
- GetDBM(obj, dbmp);
+ GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
- dbm = dbmp->di_dbm;
if (gdbm_store(dbm, key, val, GDBM_REPLACE)) {
if (errno == EPERM) rb_sys_fail(0);
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
@@ -626,9 +623,8 @@ fgdbm_length(obj)
GDBM_FILE dbm;
int i = 0;
- GetDBM(obj, dbmp);
+ GetDBM2(obj, dbmp, dbm);
if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
- dbm = dbmp->di_dbm;
for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
nextkey = gdbm_nextkey(dbm, key);
@@ -672,13 +668,12 @@ fgdbm_each_value(obj)
GDBM_FILE dbm;
VALUE keystr;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
rb_yield(rb_gdbm_fetch2(dbm, keystr));
+ GetDBM2(obj, dbmp, dbm);
}
return obj;
}
@@ -691,13 +686,12 @@ fgdbm_each_key(obj)
GDBM_FILE dbm;
VALUE keystr;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
rb_yield(keystr);
+ GetDBM2(obj, dbmp, dbm);
}
return obj;
}
@@ -710,13 +704,12 @@ fgdbm_each_pair(obj)
struct dbmdata *dbmp;
VALUE keystr;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
rb_yield(rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
+ GetDBM2(obj, dbmp, dbm);
}
return obj;
@@ -730,9 +723,7 @@ fgdbm_keys(obj)
GDBM_FILE dbm;
VALUE keystr, ary;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
@@ -752,9 +743,7 @@ fgdbm_values(obj)
GDBM_FILE dbm;
VALUE valstr, ary;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
nextkey = gdbm_nextkey(dbm, key);
@@ -778,8 +767,7 @@ fgdbm_has_key(obj, keystr)
key.dptr = RSTRING(keystr)->ptr;
key.dsize = RSTRING(keystr)->len;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
if (gdbm_exists(dbm, key))
return Qtrue;
return Qfalse;
@@ -794,8 +782,7 @@ fgdbm_has_value(obj, valstr)
VALUE keystr, valstr2;
StringValue(valstr);
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
@@ -819,9 +806,7 @@ fgdbm_to_a(obj)
GDBM_FILE dbm;
VALUE keystr, ary;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
@@ -840,8 +825,7 @@ fgdbm_reorganize(obj)
GDBM_FILE dbm;
rb_gdbm_modify(obj);
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
gdbm_reorganize(dbm);
return obj;
}
@@ -854,8 +838,7 @@ fgdbm_sync(obj)
GDBM_FILE dbm;
rb_gdbm_modify(obj);
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
gdbm_sync(dbm);
return obj;
}
@@ -868,9 +851,7 @@ fgdbm_set_cachesize(obj, val)
GDBM_FILE dbm;
int optval;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
optval = FIX2INT(val);
if (gdbm_setopt(dbm, GDBM_CACHESIZE, &optval, sizeof(optval)) == -1) {
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
@@ -886,9 +867,7 @@ fgdbm_set_fastmode(obj, val)
GDBM_FILE dbm;
int optval;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
optval = 0;
if (RTEST(val))
optval = 1;
@@ -911,9 +890,7 @@ fgdbm_set_syncmode(obj, val)
GDBM_FILE dbm;
int optval;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
optval = 0;
if (RTEST(val))
optval = 1;
@@ -933,9 +910,7 @@ fgdbm_to_hash(obj)
GDBM_FILE dbm;
VALUE keystr, hash;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
hash = rb_hash_new();
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
diff --git a/ext/sdbm/init.c b/ext/sdbm/init.c
index ae519c460..a237eb359 100644
--- a/ext/sdbm/init.c
+++ b/ext/sdbm/init.c
@@ -31,9 +31,15 @@ closed_sdbm()
#define GetDBM(obj, dbmp) {\
Data_Get_Struct(obj, struct dbmdata, dbmp);\
+ if (dbmp == 0) closed_dbm();\
if (dbmp->di_dbm == 0) closed_sdbm();\
}
+#define GetDBM2(obj, data, dbm) {\
+ GetDBM(obj, data);\
+ (dbm) = dbmp->di_dbm;\
+}
+
static void
free_sdbm(dbmp)
struct dbmdata *dbmp;
@@ -138,8 +144,7 @@ fsdbm_fetch(obj, keystr, ifnone)
key.dptr = RSTRING(keystr)->ptr;
key.dsize = RSTRING(keystr)->len;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
value = sdbm_fetch(dbm, key);
if (value.dptr == 0) {
if (ifnone == Qnil && rb_block_given_p())
@@ -184,8 +189,7 @@ fsdbm_index(obj, valstr)
val.dptr = RSTRING(valstr)->ptr;
val.dsize = RSTRING(valstr)->len;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
if (val.dsize == RSTRING(valstr)->len &&
@@ -229,14 +233,17 @@ fsdbm_select(argc, argv, obj)
if (argc > 0) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc);
}
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
- VALUE assoc;
+ VALUE assoc, v;
val = sdbm_fetch(dbm, key);
assoc = rb_assoc_new(rb_tainted_str_new(key.dptr, key.dsize),
rb_tainted_str_new(val.dptr, val.dsize));
+ v = rb_yield(assoc);
+ if (RTEST(v)) {
+ rb_ary_push(new, assoc);
+ }
+ GetDBM2(obj, dbmp, dbm);
if (RTEST(rb_yield(assoc)))
rb_ary_push(new, assoc);
}
@@ -290,8 +297,7 @@ fsdbm_delete(obj, keystr)
key.dptr = RSTRING(keystr)->ptr;
key.dsize = RSTRING(keystr)->len;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
value = sdbm_fetch(dbm, key);
@@ -323,9 +329,7 @@ fsdbm_shift(obj)
VALUE keystr, valstr;
fdbm_modify(obj);
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
key = sdbm_firstkey(dbm);
if (!key.dptr) return Qnil;
val = sdbm_fetch(dbm, key);
@@ -351,8 +355,7 @@ fsdbm_delete_if(obj)
int i, status = 0, n;
fdbm_modify(obj);
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
n = dbmp->di_size;
dbmp->di_size = -1;
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
@@ -362,6 +365,7 @@ fsdbm_delete_if(obj)
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);
+ GetDBM2(obj, dbmp, dbm);
}
for (i = 0; i < RARRAY(ary)->len; i++) {
@@ -388,8 +392,7 @@ fsdbm_clear(obj)
DBM *dbm;
fdbm_modify(obj);
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
while (key = sdbm_firstkey(dbm), key.dptr) {
if (sdbm_delete(dbm, key)) {
@@ -411,8 +414,7 @@ fsdbm_invert(obj)
VALUE keystr, valstr;
VALUE hash = rb_hash_new();
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
keystr = rb_tainted_str_new(key.dptr, key.dsize);
@@ -485,9 +487,8 @@ fsdbm_store(obj, keystr, valstr)
val.dptr = RSTRING(valstr)->ptr;
val.dsize = RSTRING(valstr)->len;
- GetDBM(obj, dbmp);
+ GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
- dbm = dbmp->di_dbm;
if (sdbm_store(dbm, key, val, DBM_REPLACE)) {
#ifdef HAVE_DBM_CLAERERR
sdbm_clearerr(dbm);
@@ -508,9 +509,8 @@ fsdbm_length(obj)
DBM *dbm;
int i = 0;
- GetDBM(obj, dbmp);
+ GetDBM2(obj, dbmp, dbm);
if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
- dbm = dbmp->di_dbm;
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
i++;
@@ -552,11 +552,11 @@ fsdbm_each_value(obj)
struct dbmdata *dbmp;
DBM *dbm;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
rb_yield(rb_tainted_str_new(val.dptr, val.dsize));
+ GetDBM2(obj, dbmp, dbm);
}
return obj;
}
@@ -569,10 +569,10 @@ fsdbm_each_key(obj)
struct dbmdata *dbmp;
DBM *dbm;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
rb_yield(rb_tainted_str_new(key.dptr, key.dsize));
+ GetDBM2(obj, dbmp, dbm);
}
return obj;
}
@@ -586,14 +586,13 @@ fsdbm_each_pair(obj)
struct dbmdata *dbmp;
VALUE keystr, valstr;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
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));
+ GetDBM2(obj, dbmp, dbm);
}
return obj;
@@ -608,9 +607,7 @@ fsdbm_keys(obj)
DBM *dbm;
VALUE ary;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
rb_ary_push(ary, rb_tainted_str_new(key.dptr, key.dsize));
@@ -628,9 +625,7 @@ fsdbm_values(obj)
DBM *dbm;
VALUE ary;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
@@ -652,8 +647,7 @@ fsdbm_has_key(obj, keystr)
key.dptr = RSTRING(keystr)->ptr;
key.dsize = RSTRING(keystr)->len;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
val = sdbm_fetch(dbm, key);
if (val.dptr) return Qtrue;
return Qfalse;
@@ -671,8 +665,7 @@ fsdbm_has_value(obj, valstr)
val.dptr = RSTRING(valstr)->ptr;
val.dsize = RSTRING(valstr)->len;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
+ GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
if (val.dsize == RSTRING(valstr)->len &&
@@ -691,9 +684,7 @@ fsdbm_to_a(obj)
DBM *dbm;
VALUE ary;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
@@ -713,9 +704,7 @@ fsdbm_to_hash(obj)
DBM *dbm;
VALUE hash;
- GetDBM(obj, dbmp);
- dbm = dbmp->di_dbm;
-
+ GetDBM2(obj, dbmp, dbm);
hash = rb_hash_new();
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);