summaryrefslogtreecommitdiff
path: root/ext/sdbm/init.c
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/sdbm/init.c
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/sdbm/init.c')
-rw-r--r--ext/sdbm/init.c81
1 files changed, 34 insertions, 47 deletions
diff --git a/ext/sdbm/init.c b/ext/sdbm/init.c
index 104faa0..2c431e0 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 &&
@@ -205,16 +209,17 @@ fsdbm_select(obj)
DBM *dbm;
struct dbmdata *dbmp;
- 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));
- if (RTEST(rb_yield(assoc)))
+ v = rb_yield(assoc);
+ if (RTEST(v)) {
rb_ary_push(new, assoc);
+ }
+ GetDBM2(obj, dbmp, dbm);
}
return new;
@@ -258,8 +263,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);
@@ -291,9 +295,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);
@@ -319,8 +321,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)) {
@@ -330,6 +331,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++) {
@@ -356,8 +358,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)) {
@@ -379,8 +380,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);
@@ -453,9 +453,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);
@@ -476,9 +475,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++;
@@ -520,11 +518,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;
}
@@ -537,10 +535,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;
}
@@ -554,14 +552,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;
@@ -576,9 +573,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));
@@ -596,9 +591,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);
@@ -620,8 +613,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;
@@ -639,8 +631,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 &&
@@ -659,9 +650,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);
@@ -681,9 +670,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);