summaryrefslogtreecommitdiff
path: root/ext/dbm
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-12-09 02:19:50 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-12-09 02:19:50 +0000
commit3d66cc4c6b9585227b6489497d66dbc065c6f6a6 (patch)
treec92c837c765644f1ba35a315090fbfb6cc90369d /ext/dbm
parent32481c4126c09a659dd240a44fe27a2af513dd52 (diff)
* ext/dbm/dbm.c (fdbm_select): [ruby-dev:25132]
* ext/sdbm/init.c: ditto. * ext/gdbm/gdbm.c: ditto. * eval.c (proc_invoke): merge Guy Decoux's argument preserve patch in [ruby-core:03874]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7516 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/dbm')
-rw-r--r--ext/dbm/dbm.c81
1 files changed, 35 insertions, 46 deletions
diff --git a/ext/dbm/dbm.c b/ext/dbm/dbm.c
index 9dabca0..f7b4c8c 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 &&
@@ -229,16 +232,17 @@ fdbm_select(obj)
DBM *dbm;
struct dbmdata *dbmp;
- 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)))
+ v = rb_yield(assoc);
+ if (RTEST(v)) {
rb_ary_push(new, assoc);
+ }
+ GetDBM2(obj, dbmp, dbm);
}
return new;
@@ -282,8 +286,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) {
@@ -314,8 +317,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);
@@ -340,8 +342,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;
@@ -352,6 +353,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++) {
@@ -378,8 +380,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)) {
@@ -401,8 +402,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);
@@ -470,9 +470,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);
@@ -493,9 +492,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++;
@@ -514,7 +512,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;
@@ -537,11 +535,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;
}
@@ -554,10 +552,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;
}
@@ -571,14 +569,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;
@@ -593,8 +591,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)) {
@@ -613,9 +610,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);
@@ -637,8 +632,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;
@@ -656,8 +650,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 &&
@@ -676,14 +669,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;
@@ -698,9 +689,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);