summaryrefslogtreecommitdiff
path: root/ext/sdbm/init.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-02-27 04:52:21 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-02-27 04:52:21 +0000
commit3fc04d9361549d5bb15f869c22d7ce9591a0ac8c (patch)
treeb160a4f2ed1605c1a4ef62d6c568ff807fda24c5 /ext/sdbm/init.c
parent2fcd221fecabe3e6acaa21c54ae6d1ccbe8c0204 (diff)
* ext/dbm/dbm.c (fdbm_select): 1.7 behavior.
* ext/gdbm/gdbm.c (fgdbm_select): ditto. * ext/sdbm/sdbm.c (fsdbm_select): ditto. * ext/dbm/dbm.c (fdbm_delete): adopt Hash#delete behavior. * ext/sdbm/sdbm.c (fsdbm_delete): ditto. * ext/gdbm/gdbm.c: need not to dup key to the block. * ext/sdbm/sdbm.c : replace RuntimeError with SDBMError. * eval.c (rb_f_missing): NoMethod error messages for true, false, nil must respond visibility like for other objects. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2145 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/sdbm/init.c')
-rw-r--r--ext/sdbm/init.c172
1 files changed, 109 insertions, 63 deletions
diff --git a/ext/sdbm/init.c b/ext/sdbm/init.c
index b3365ff..5412dd8 100644
--- a/ext/sdbm/init.c
+++ b/ext/sdbm/init.c
@@ -16,7 +16,7 @@
#include <fcntl.h>
#include <errno.h>
-static VALUE cSDBM;
+static VALUE rb_cDBM, rb_eDBMError;
struct dbmdata {
int di_size;
@@ -26,7 +26,7 @@ struct dbmdata {
static void
closed_sdbm()
{
- rb_raise(rb_eRuntimeError, "closed SDBM file");
+ rb_raise(rb_eDBMError, "closed SDBM file");
}
#define GetDBM(obj, dbmp) {\
@@ -57,6 +57,17 @@ fsdbm_close(obj)
}
static VALUE
+fsdbm_s_new(argc, argv, klass)
+ int argc;
+ VALUE *argv;
+ VALUE klass;
+{
+ VALUE obj = Data_Wrap_Struct(klass, 0, free_sdbm, 0);
+ rb_obj_call_init(obj, argc, argv);
+ return obj;
+}
+
+static VALUE
fsdbm_initialize(argc, argv, obj)
int argc;
VALUE *argv;
@@ -100,17 +111,6 @@ fsdbm_initialize(argc, argv, obj)
}
static VALUE
-fsdbm_s_new(argc, argv, klass)
- int argc;
- VALUE *argv;
- VALUE klass;
-{
- VALUE obj = Data_Wrap_Struct(klass, 0, free_sdbm, 0);
- rb_obj_call_init(obj, argc, argv);
- return obj;
-}
-
-static VALUE
fsdbm_s_open(argc, argv, klass)
int argc;
VALUE *argv;
@@ -216,12 +216,52 @@ fsdbm_indexes(argc, argv, obj)
}
static VALUE
+fsdbm_select(argc, argv, obj)
+ int argc;
+ VALUE *argv;
+ VALUE obj;
+{
+ VALUE new = rb_ary_new2(argc);
+ int i;
+
+ if (rb_block_given_p()) {
+ datum key, val;
+ DBM *dbm;
+ struct dbmdata *dbmp;
+ VALUE keystr, valstr;
+
+ if (argc > 0) {
+ rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc);
+ }
+ GetDBM(obj, dbmp);
+ dbm = dbmp->di_dbm;
+
+ for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
+ VALUE assoc;
+ 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)))
+ rb_ary_push(new, assoc);
+ }
+ }
+ else {
+ for (i=0; i<argc; i++) {
+ rb_ary_push(new, fsdbm_fetch(obj, argv[i]));
+ }
+ }
+
+ return new;
+}
+
+static VALUE
fsdbm_delete(obj, keystr)
VALUE obj, keystr;
{
datum key, value;
struct dbmdata *dbmp;
DBM *dbm;
+ VALUE valstr;
rb_secure(4);
StringValue(keystr);
@@ -234,18 +274,21 @@ fsdbm_delete(obj, keystr)
value = sdbm_fetch(dbm, key);
if (value.dptr == 0) {
- if (rb_block_given_p()) rb_yield(keystr);
+ if (rb_block_given_p()) return rb_yield(keystr);
return Qnil;
}
+ /* need to save value before sdbm_delete() */
+ valstr = rb_tainted_str_new(value.dptr, value.dsize);
+
if (sdbm_delete(dbm, key)) {
dbmp->di_size = -1;
- rb_raise(rb_eRuntimeError, "dbm_delete failed");
+ rb_raise(rb_eDBMError, "dbm_delete failed");
}
else if (dbmp->di_size >= 0) {
dbmp->di_size--;
}
- return obj;
+ return valstr;
}
static VALUE
@@ -304,7 +347,7 @@ fsdbm_delete_if(obj)
key.dptr = RSTRING(keystr)->ptr;
key.dsize = RSTRING(keystr)->len;
if (sdbm_delete(dbm, key)) {
- rb_raise(rb_eRuntimeError, "sdbm_delete failed");
+ rb_raise(rb_eDBMError, "sdbm_delete failed");
}
}
if (status) rb_jump_tag(status);
@@ -327,7 +370,7 @@ fsdbm_clear(obj)
dbmp->di_size = -1;
while (key = sdbm_firstkey(dbm), key.dptr) {
if (sdbm_delete(dbm, key)) {
- rb_raise(rb_eRuntimeError, "sdbm_delete failed");
+ rb_raise(rb_eDBMError, "sdbm_delete failed");
}
}
dbmp->di_size = 0;
@@ -381,7 +424,7 @@ static VALUE
fsdbm_update(obj, other)
VALUE obj, other;
{
- rb_iterate((VALUE(*)_((VALUE)))each_pair, other, update_i, obj);
+ rb_iterate(each_pair, other, update_i, obj);
return obj;
}
@@ -390,7 +433,7 @@ fsdbm_replace(obj, other)
VALUE obj, other;
{
fsdbm_clear(obj);
- rb_iterate((VALUE(*)_((VALUE)))each_pair, other, update_i, obj);
+ rb_iterate(each_pair, other, update_i, obj);
return obj;
}
@@ -427,7 +470,7 @@ fsdbm_store(obj, keystr, valstr)
sdbm_clearerr(dbm);
#endif
if (errno == EPERM) rb_sys_fail(0);
- rb_raise(rb_eRuntimeError, "sdbm_store failed");
+ rb_raise(rb_eDBMError, "sdbm_store failed");
}
return valstr;
@@ -670,46 +713,49 @@ fsdbm_reject(obj)
void
Init_sdbm()
{
- cSDBM = rb_define_class("SDBM", rb_cObject);
- rb_include_module(cSDBM, rb_mEnumerable);
-
- rb_define_singleton_method(cSDBM, "open", fsdbm_s_open, -1);
- rb_define_singleton_method(cSDBM, "new", fsdbm_s_new, -1);
- rb_define_method(cSDBM, "initialize", fsdbm_initialize, -1);
- rb_define_method(cSDBM, "close", fsdbm_close, 0);
- 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);
- rb_define_alias(cSDBM, "size", "length");
- rb_define_method(cSDBM, "empty?", fsdbm_empty_p, 0);
- rb_define_method(cSDBM, "each", fsdbm_each_pair, 0);
- rb_define_method(cSDBM, "each_value", fsdbm_each_value, 0);
- rb_define_method(cSDBM, "each_key", fsdbm_each_key, 0);
- rb_define_method(cSDBM, "each_pair", fsdbm_each_pair, 0);
- rb_define_method(cSDBM, "keys", fsdbm_keys, 0);
- rb_define_method(cSDBM, "values", fsdbm_values, 0);
- rb_define_method(cSDBM, "shift", fsdbm_shift, 0);
- 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);
- rb_define_method(cSDBM,"replace", fsdbm_replace, 1);
-
- 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);
+ rb_cDBM = rb_define_class("SDBM", rb_cObject);
+ rb_eDBMError = rb_define_class("SDBMError", rb_eStandardError);
+ rb_include_module(rb_cDBM, rb_mEnumerable);
+
+ rb_define_singleton_method(rb_cDBM, "new", fsdbm_s_new, -1);
+ rb_define_singleton_method(rb_cDBM, "open", fsdbm_s_open, -1);
+
+ rb_define_method(rb_cDBM, "initialize", fsdbm_initialize, -1);
+ rb_define_method(rb_cDBM, "close", fsdbm_close, 0);
+ rb_define_method(rb_cDBM, "[]", fsdbm_aref, 1);
+ rb_define_method(rb_cDBM, "fetch", fsdbm_fetch_m, -1);
+ rb_define_method(rb_cDBM, "[]=", fsdbm_store, 2);
+ rb_define_method(rb_cDBM, "store", fsdbm_store, 2);
+ rb_define_method(rb_cDBM, "index", fsdbm_index, 1);
+ rb_define_method(rb_cDBM, "indexes", fsdbm_indexes, -1);
+ rb_define_method(rb_cDBM, "indices", fsdbm_indexes, -1);
+ rb_define_method(rb_cDBM, "select", fsdbm_select, -1);
+ rb_define_method(rb_cDBM, "length", fsdbm_length, 0);
+ rb_define_method(rb_cDBM, "size", fsdbm_length, 0);
+ rb_define_method(rb_cDBM, "empty?", fsdbm_empty_p, 0);
+ rb_define_method(rb_cDBM, "each", fsdbm_each_pair, 0);
+ rb_define_method(rb_cDBM, "each_value", fsdbm_each_value, 0);
+ rb_define_method(rb_cDBM, "each_key", fsdbm_each_key, 0);
+ rb_define_method(rb_cDBM, "each_pair", fsdbm_each_pair, 0);
+ rb_define_method(rb_cDBM, "keys", fsdbm_keys, 0);
+ rb_define_method(rb_cDBM, "values", fsdbm_values, 0);
+ rb_define_method(rb_cDBM, "shift", fsdbm_shift, 0);
+ rb_define_method(rb_cDBM, "delete", fsdbm_delete, 1);
+ rb_define_method(rb_cDBM, "delete_if", fsdbm_delete_if, 0);
+ rb_define_method(rb_cDBM, "reject!", fsdbm_delete_if, 0);
+ rb_define_method(rb_cDBM, "reject", fsdbm_reject, 0);
+ rb_define_method(rb_cDBM, "clear", fsdbm_clear, 0);
+ rb_define_method(rb_cDBM,"invert", fsdbm_invert, 0);
+ rb_define_method(rb_cDBM,"update", fsdbm_update, 1);
+ rb_define_method(rb_cDBM,"replace", fsdbm_replace, 1);
+
+ rb_define_method(rb_cDBM, "include?", fsdbm_has_key, 1);
+ rb_define_method(rb_cDBM, "has_key?", fsdbm_has_key, 1);
+ rb_define_method(rb_cDBM, "member?", fsdbm_has_key, 1);
+ rb_define_method(rb_cDBM, "has_value?", fsdbm_has_value, 1);
+ rb_define_method(rb_cDBM, "key?", fsdbm_has_key, 1);
+ rb_define_method(rb_cDBM, "value?", fsdbm_has_value, 1);
+
+ rb_define_method(rb_cDBM, "to_a", fsdbm_to_a, 0);
+ rb_define_method(rb_cDBM, "to_hash", fsdbm_to_hash, 0);
}