summaryrefslogtreecommitdiff
path: root/ext/sdbm
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-07-04 07:04:42 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-07-04 07:04:42 +0000
commit79096829406349429b32054133ab178d55ed84f1 (patch)
tree1f49a98fb0a327f549b43744726c470e33b4ee90 /ext/sdbm
parent38402e0415d64e2516a72e4b9d764c675453da37 (diff)
matz
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@812 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/sdbm')
-rw-r--r--ext/sdbm/init.c88
1 files changed, 84 insertions, 4 deletions
diff --git a/ext/sdbm/init.c b/ext/sdbm/init.c
index ddbf384..f6dd73b 100644
--- a/ext/sdbm/init.c
+++ b/ext/sdbm/init.c
@@ -100,8 +100,8 @@ fsdbm_close(obj)
}
static VALUE
-fsdbm_fetch(obj, keystr)
- VALUE obj, keystr;
+fsdbm_fetch(obj, keystr, ifnone)
+ VALUE obj, keystr, ifnone;
{
datum key, value;
struct dbmdata *dbmp;
@@ -115,12 +115,56 @@ fsdbm_fetch(obj, keystr)
dbm = dbmp->di_dbm;
value = sdbm_fetch(dbm, key);
if (value.dptr == 0) {
- return Qnil;
+ if (ifnone == Qnil && rb_block_given_p())
+ return rb_yield(rb_tainted_str_new(key.dptr, key.dsize));
+ return ifnone;
}
return rb_tainted_str_new(value.dptr, value.dsize);
}
static VALUE
+fsdbm_aref(obj, keystr)
+ VALUE obj, keystr;
+{
+ return fsdbm_fetch(obj, keystr, Qnil);
+}
+
+static VALUE
+fsdbm_fetch_m(argc, argv, obj)
+ int argc;
+ VALUE *argv;
+ VALUE obj;
+{
+ VALUE keystr, ifnone;
+
+ rb_scan_args(argc, argv, "11", &keystr, &ifnone);
+ return fsdbm_fetch(obj, keystr, ifnone);
+}
+
+static VALUE
+fsdbm_index(obj, valstr)
+ VALUE obj, valstr;
+{
+ datum key, val;
+ struct dbmdata *dbmp;
+ DBM *dbm;
+
+ Check_Type(valstr, T_STRING);
+ val.dptr = RSTRING(valstr)->ptr;
+ val.dsize = RSTRING(valstr)->len;
+
+ GetDBM(obj, dbmp);
+ dbm = dbmp->di_dbm;
+ for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
+ val = sdbm_fetch(dbm, key);
+ if (val.dsize == RSTRING(valstr)->len &&
+ memcmp(val.dptr, RSTRING(valstr)->ptr, val.dsize) == 0)
+ return rb_tainted_str_new(key.dptr, key.dsize);
+ }
+ return Qnil;
+}
+
+static VALUE
fsdbm_indexes(argc, argv, obj)
int argc;
VALUE *argv;
@@ -540,6 +584,35 @@ fsdbm_to_a(obj)
return ary;
}
+static VALUE
+fsdbm_to_hash(obj)
+ VALUE obj;
+{
+ datum key, val;
+ struct dbmdata *dbmp;
+ DBM *dbm;
+ VALUE hash;
+
+ GetDBM(obj, dbmp);
+ dbm = dbmp->di_dbm;
+
+ hash = rb_hash_new();
+ for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
+ val = sdbm_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
+fsdbm_reject(obj)
+ VALUE obj;
+{
+ return rb_hash_delete_if(fsdbm_to_hash(obj));
+}
+
void
Init_sdbm()
{
@@ -549,8 +622,11 @@ Init_sdbm()
rb_define_singleton_method(cSDBM, "open", fsdbm_s_open, -1);
rb_define_singleton_method(cSDBM, "new", fsdbm_s_open, -1);
rb_define_method(cSDBM, "close", fsdbm_close, 0);
- rb_define_method(cSDBM, "[]", fsdbm_fetch, 1);
+ rb_define_method(cSDBM, "[]", fsdbm_aref, 1);
+ rb_define_method(cSDBM, "fetch", fsdbm_fetch_m, -1);
rb_define_method(cSDBM, "[]=", fsdbm_store, 2);
+ rb_define_method(cSDBM, "store", fsdbm_store, 2);
+ rb_define_method(cSDBM, "index", fsdbm_index, 1);
rb_define_method(cSDBM, "indexes", fsdbm_indexes, -1);
rb_define_method(cSDBM, "indices", fsdbm_indexes, -1);
rb_define_method(cSDBM, "length", fsdbm_length, 0);
@@ -565,6 +641,8 @@ Init_sdbm()
rb_define_method(cSDBM, "shift", fsdbm_shift, 1);
rb_define_method(cSDBM, "delete", fsdbm_delete, 1);
rb_define_method(cSDBM, "delete_if", fsdbm_delete_if, 0);
+ rb_define_method(cSDBM, "reject!", fsdbm_delete_if, 0);
+ rb_define_method(cSDBM, "reject", fsdbm_reject, 0);
rb_define_method(cSDBM, "clear", fsdbm_clear, 0);
rb_define_method(cSDBM,"invert", fsdbm_invert, 0);
rb_define_method(cSDBM,"update", fsdbm_update, 1);
@@ -572,9 +650,11 @@ Init_sdbm()
rb_define_method(cSDBM, "include?", fsdbm_has_key, 1);
rb_define_method(cSDBM, "has_key?", fsdbm_has_key, 1);
+ rb_define_method(cSDBM, "member?", fsdbm_has_key, 1);
rb_define_method(cSDBM, "has_value?", fsdbm_has_value, 1);
rb_define_method(cSDBM, "key?", fsdbm_has_key, 1);
rb_define_method(cSDBM, "value?", fsdbm_has_value, 1);
rb_define_method(cSDBM, "to_a", fsdbm_to_a, 0);
+ rb_define_method(cSDBM, "to_hash", fsdbm_to_hash, 0);
}