summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/dbm/dbm.c76
-rw-r--r--ext/dbm/testdbm.rb28
-rw-r--r--ext/gdbm/gdbm.c196
-rw-r--r--ext/gdbm/testgdbm.rb26
-rw-r--r--ext/sdbm/init.c172
-rw-r--r--ext/sdbm/testsdbm.rb28
6 files changed, 354 insertions, 172 deletions
diff --git a/ext/dbm/dbm.c b/ext/dbm/dbm.c
index 55e207c49ab..f81f7efcb1a 100644
--- a/ext/dbm/dbm.c
+++ b/ext/dbm/dbm.c
@@ -32,7 +32,7 @@ struct dbmdata {
static void
closed_dbm()
{
- rb_raise(rb_eRuntimeError, "closed DBM file");
+ rb_raise(rb_eDBMError, "closed DBM file");
}
#define GetDBM(obj, dbmp) {\
@@ -51,7 +51,18 @@ free_dbm(dbmp)
}
}
-static VALUE fdbm_close _((VALUE));
+static VALUE
+fdbm_close(obj)
+ VALUE obj;
+{
+ struct dbmdata *dbmp;
+
+ GetDBM(obj, dbmp);
+ dbm_close(dbmp->di_dbm);
+ dbmp->di_dbm = 0;
+
+ return Qnil;
+}
static VALUE
fdbm_s_new(argc, argv, klass)
@@ -130,19 +141,6 @@ fdbm_s_open(argc, argv, klass)
}
static VALUE
-fdbm_close(obj)
- VALUE obj;
-{
- struct dbmdata *dbmp;
-
- GetDBM(obj, dbmp);
- dbm_close(dbmp->di_dbm);
- dbmp->di_dbm = 0;
-
- return Qnil;
-}
-
-static VALUE
fdbm_fetch(obj, keystr, ifnone)
VALUE obj, keystr, ifnone;
{
@@ -230,12 +228,52 @@ fdbm_indexes(argc, argv, obj)
}
static VALUE
+fdbm_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 = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
+ VALUE assoc;
+ 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);
+ }
+ }
+ else {
+ for (i=0; i<argc; i++) {
+ rb_ary_push(new, fdbm_fetch(obj, argv[i]));
+ }
+ }
+
+ return new;
+}
+
+static VALUE
fdbm_delete(obj, keystr)
VALUE obj, keystr;
{
datum key, value;
struct dbmdata *dbmp;
DBM *dbm;
+ VALUE valstr;
rb_secure(4);
StringValue(keystr);
@@ -247,10 +285,13 @@ fdbm_delete(obj, keystr)
value = dbm_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 dbm_delete() */
+ valstr = rb_tainted_str_new(value.dptr, value.dsize);
+
if (dbm_delete(dbm, key)) {
dbmp->di_size = -1;
rb_raise(rb_eDBMError, "dbm_delete failed");
@@ -258,7 +299,7 @@ fdbm_delete(obj, keystr)
else if (dbmp->di_size >= 0) {
dbmp->di_size--;
}
- return obj;
+ return valstr;
}
static VALUE
@@ -691,6 +732,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, -1);
rb_define_method(rb_cDBM, "length", fdbm_length, 0);
rb_define_method(rb_cDBM, "size", fdbm_length, 0);
rb_define_method(rb_cDBM, "empty?", fdbm_empty_p, 0);
diff --git a/ext/dbm/testdbm.rb b/ext/dbm/testdbm.rb
index c6b26e0cdf2..0be627d346e 100644
--- a/ext/dbm/testdbm.rb
+++ b/ext/dbm/testdbm.rb
@@ -165,7 +165,7 @@ class TestDBM < RUNIT::TestCase
assert_nil(dbm.close)
# closed DBM file
- assert_exception(RuntimeError) { dbm.close }
+ assert_exception(DBMError) { dbm.close }
end
def test_aref
@@ -245,6 +245,25 @@ class TestDBM < RUNIT::TestCase
assert_equals(values.reverse, @dbm.indexes(*keys.reverse))
end
+ def test_select
+ keys = %w(foo bar baz)
+ values = %w(FOO BAR BAZ)
+ @dbm[keys[0]], @dbm[keys[1]], @dbm[keys[2]] = values
+ assert_equals(values.reverse, @dbm.select(*keys.reverse))
+ end
+
+ def test_select_with_block
+ keys = %w(foo bar baz)
+ values = %w(FOO BAR BAZ)
+ @dbm[keys[0]], @dbm[keys[1]], @dbm[keys[2]] = values
+ ret = @dbm.select {|k,v|
+ assert_equals(k.upcase, v)
+ k != "bar"
+ }
+ assert_equals([['baz', 'BAZ'], ['foo', 'FOO']],
+ ret.sort)
+ end
+
def test_length
num = 10
assert_equals(0, @dbm.size)
@@ -377,7 +396,7 @@ class TestDBM < RUNIT::TestCase
@dbm[keys[0]], @dbm[keys[1]], @dbm[keys[2]] = values
- assert_equals(@dbm, @dbm.delete(key))
+ assert_equals('BAR', @dbm.delete(key))
assert_nil(@dbm[key])
assert_equals(2, @dbm.size)
@@ -394,12 +413,13 @@ class TestDBM < RUNIT::TestCase
def test_delete_with_block
key = 'no called block'
@dbm[key] = 'foo'
- assert_equals(@dbm, @dbm.delete(key) {|k| k.replace 'called block'})
+ assert_equals('foo', @dbm.delete(key) {|k| k.replace 'called block'})
assert_equals('no called block', key)
assert_equals(0, @dbm.size)
key = 'no called block'
- assert_nil(@dbm.delete(key) {|k| k.replace 'called block'})
+ assert_equals(:blockval,
+ @dbm.delete(key) {|k| k.replace 'called block'; :blockval})
assert_equals('called block', key)
assert_equals(0, @dbm.size)
end
diff --git a/ext/gdbm/gdbm.c b/ext/gdbm/gdbm.c
index eddeb11f8ee..d30f5dec7ac 100644
--- a/ext/gdbm/gdbm.c
+++ b/ext/gdbm/gdbm.c
@@ -14,11 +14,11 @@
#include <fcntl.h>
#include <errno.h>
-static VALUE cGDBM, rb_eGDBMError, rb_eGDBMFatalError;
+static VALUE rb_cGDBM, rb_eGDBMError, rb_eGDBMFatalError;
#define MY_BLOCK_SIZE (2048)
#define MY_FATAL_FUNC rb_gdbm_fatal
-void
+static void
rb_gdbm_fatal(msg)
char *msg;
{
@@ -52,7 +52,18 @@ free_dbm(dbmp)
}
}
-static VALUE fgdbm_close _((VALUE));
+static VALUE
+fgdbm_close(obj)
+ VALUE obj;
+{
+ struct dbmdata *dbmp;
+
+ GetDBM(obj, dbmp);
+ gdbm_close(dbmp->di_dbm);
+ dbmp->di_dbm = 0;
+
+ return Qnil;
+}
static VALUE
fgdbm_s_new(argc, argv, klass)
@@ -141,19 +152,6 @@ fgdbm_s_open(argc, argv, klass)
}
static VALUE
-fgdbm_close(obj)
- VALUE obj;
-{
- struct dbmdata *dbmp;
-
- GetDBM(obj, dbmp);
- gdbm_close(dbmp->di_dbm);
- dbmp->di_dbm = 0;
-
- return Qnil;
-}
-
-static VALUE
rb_gdbm_fetch(dbm, key)
GDBM_FILE dbm;
datum key;
@@ -219,7 +217,7 @@ rb_gdbm_firstkey(dbm)
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
OBJ_TAINT(str);
- return (VALUE)str;
+ return str;
}
static VALUE
@@ -243,7 +241,7 @@ rb_gdbm_nextkey(dbm, keystr)
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
OBJ_TAINT(str);
- return (VALUE)str;
+ return str;
}
static VALUE
@@ -327,6 +325,43 @@ fgdbm_indexes(argc, argv, obj)
}
static VALUE
+fgdbm_select(argc, argv, obj)
+ int argc;
+ VALUE *argv;
+ VALUE obj;
+{
+ VALUE new = rb_ary_new2(argc);
+ int i;
+
+ if (rb_block_given_p()) {
+ GDBM_FILE dbm;
+ struct dbmdata *dbmp;
+ VALUE keystr;
+
+ if (argc > 0) {
+ rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc);
+ }
+ GetDBM(obj, dbmp);
+ dbm = dbmp->di_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));
+
+ if (RTEST(rb_yield(assoc)))
+ rb_ary_push(new, assoc);
+ }
+ }
+ else {
+ for (i=0; i<argc; i++) {
+ rb_ary_push(new, rb_gdbm_fetch3(obj, argv[i]));
+ }
+ }
+
+ return new;
+}
+
+static VALUE
rb_gdbm_delete(obj, keystr)
VALUE obj, keystr;
{
@@ -407,7 +442,7 @@ fgdbm_delete_if(obj)
keystr = rb_gdbm_nextkey(dbm, keystr)) {
valstr = rb_gdbm_fetch2(dbm, keystr);
- ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status);
+ ret = rb_protect(rb_yield, rb_assoc_new(keystr, valstr), &status);
if (status != 0) break;
if (RTEST(ret)) rb_ary_push(ary, keystr);
}
@@ -625,7 +660,7 @@ fgdbm_each_key(obj)
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
- rb_yield(rb_str_dup(keystr));
+ rb_yield(keystr);
}
return obj;
}
@@ -644,8 +679,7 @@ fgdbm_each_pair(obj)
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
- rb_yield(rb_assoc_new(rb_str_dup(keystr),
- rb_gdbm_fetch2(dbm, keystr)));
+ rb_yield(rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
}
return obj;
@@ -755,8 +789,7 @@ fgdbm_to_a(obj)
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
keystr = rb_gdbm_nextkey(dbm, keystr)) {
- rb_ary_push(ary, rb_assoc_new(rb_str_dup(keystr),
- rb_gdbm_fetch2(dbm, keystr)));
+ rb_ary_push(ary, rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
}
return ary;
@@ -886,75 +919,76 @@ fgdbm_reject(obj)
void
Init_gdbm()
{
- cGDBM = rb_define_class("GDBM", rb_cObject);
+ rb_cGDBM = rb_define_class("GDBM", rb_cObject);
rb_eGDBMError = rb_define_class("GDBMError", rb_eStandardError);
rb_eGDBMFatalError = rb_define_class("GDBMFatalError", rb_eException);
- rb_include_module(cGDBM, rb_mEnumerable);
-
- rb_define_singleton_method(cGDBM, "new", fgdbm_s_new, -1);
- rb_define_singleton_method(cGDBM, "open", fgdbm_s_open, -1);
-
- rb_define_method(cGDBM, "initialize", fgdbm_initialize, -1);
- rb_define_method(cGDBM, "close", fgdbm_close, 0);
- rb_define_method(cGDBM, "[]", fgdbm_aref, 1);
- rb_define_method(cGDBM, "fetch", fgdbm_fetch_m, -1);
- rb_define_method(cGDBM, "[]=", fgdbm_store, 2);
- rb_define_method(cGDBM, "store", fgdbm_store, 2);
- rb_define_method(cGDBM, "index", fgdbm_index, 1);
- rb_define_method(cGDBM, "indexes", fgdbm_indexes, -1);
- rb_define_method(cGDBM, "indices", fgdbm_indexes, -1);
- rb_define_method(cGDBM, "length", fgdbm_length, 0);
- rb_define_alias(cGDBM, "size", "length");
- rb_define_method(cGDBM, "empty?", fgdbm_empty_p, 0);
- rb_define_method(cGDBM, "each", fgdbm_each_pair, 0);
- rb_define_method(cGDBM, "each_value", fgdbm_each_value, 0);
- rb_define_method(cGDBM, "each_key", fgdbm_each_key, 0);
- rb_define_method(cGDBM, "each_pair", fgdbm_each_pair, 0);
- rb_define_method(cGDBM, "keys", fgdbm_keys, 0);
- rb_define_method(cGDBM, "values", fgdbm_values, 0);
- rb_define_method(cGDBM, "shift", fgdbm_shift, 0);
- rb_define_method(cGDBM, "delete", fgdbm_delete, 1);
- rb_define_method(cGDBM, "delete_if", fgdbm_delete_if, 0);
- rb_define_method(cGDBM, "reject!", fgdbm_delete_if, 0);
- rb_define_method(cGDBM, "reject", fgdbm_reject, 0);
- rb_define_method(cGDBM, "clear", fgdbm_clear, 0);
- rb_define_method(cGDBM,"invert", fgdbm_invert, 0);
- rb_define_method(cGDBM,"update", fgdbm_update, 1);
- rb_define_method(cGDBM,"replace", fgdbm_replace, 1);
- rb_define_method(cGDBM,"reorganize", fgdbm_reorganize, 0);
- rb_define_method(cGDBM,"sync", fgdbm_sync, 0);
- /* rb_define_method(cGDBM,"setopt", fgdbm_setopt, 2); */
- rb_define_method(cGDBM,"cachesize=", fgdbm_set_cachesize, 1);
- rb_define_method(cGDBM,"fastmode=", fgdbm_set_fastmode, 1);
- rb_define_method(cGDBM,"syncmode=", fgdbm_set_syncmode, 1);
-
- rb_define_method(cGDBM, "include?", fgdbm_has_key, 1);
- rb_define_method(cGDBM, "has_key?", fgdbm_has_key, 1);
- rb_define_method(cGDBM, "member?", fgdbm_has_key, 1);
- rb_define_method(cGDBM, "has_value?", fgdbm_has_value, 1);
- rb_define_method(cGDBM, "key?", fgdbm_has_key, 1);
- rb_define_method(cGDBM, "value?", fgdbm_has_value, 1);
-
- rb_define_method(cGDBM, "to_a", fgdbm_to_a, 0);
- rb_define_method(cGDBM, "to_hash", fgdbm_to_hash, 0);
+ rb_include_module(rb_cGDBM, rb_mEnumerable);
+
+ rb_define_singleton_method(rb_cGDBM, "new", fgdbm_s_new, -1);
+ rb_define_singleton_method(rb_cGDBM, "open", fgdbm_s_open, -1);
+
+ rb_define_method(rb_cGDBM, "initialize", fgdbm_initialize, -1);
+ rb_define_method(rb_cGDBM, "close", fgdbm_close, 0);
+ rb_define_method(rb_cGDBM, "[]", fgdbm_aref, 1);
+ rb_define_method(rb_cGDBM, "fetch", fgdbm_fetch_m, -1);
+ rb_define_method(rb_cGDBM, "[]=", fgdbm_store, 2);
+ rb_define_method(rb_cGDBM, "store", fgdbm_store, 2);
+ rb_define_method(rb_cGDBM, "index", fgdbm_index, 1);
+ rb_define_method(rb_cGDBM, "indexes", fgdbm_indexes, -1);
+ rb_define_method(rb_cGDBM, "indices", fgdbm_indexes, -1);
+ rb_define_method(rb_cGDBM, "select", fgdbm_select, -1);
+ rb_define_method(rb_cGDBM, "length", fgdbm_length, 0);
+ rb_define_method(rb_cGDBM, "size", fgdbm_length, 0);
+ rb_define_method(rb_cGDBM, "empty?", fgdbm_empty_p, 0);
+ rb_define_method(rb_cGDBM, "each", fgdbm_each_pair, 0);
+ rb_define_method(rb_cGDBM, "each_value", fgdbm_each_value, 0);
+ rb_define_method(rb_cGDBM, "each_key", fgdbm_each_key, 0);
+ rb_define_method(rb_cGDBM, "each_pair", fgdbm_each_pair, 0);
+ rb_define_method(rb_cGDBM, "keys", fgdbm_keys, 0);
+ rb_define_method(rb_cGDBM, "values", fgdbm_values, 0);
+ rb_define_method(rb_cGDBM, "shift", fgdbm_shift, 0);
+ rb_define_method(rb_cGDBM, "delete", fgdbm_delete, 1);
+ rb_define_method(rb_cGDBM, "delete_if", fgdbm_delete_if, 0);
+ rb_define_method(rb_cGDBM, "reject!", fgdbm_delete_if, 0);
+ rb_define_method(rb_cGDBM, "reject", fgdbm_reject, 0);
+ rb_define_method(rb_cGDBM, "clear", fgdbm_clear, 0);
+ rb_define_method(rb_cGDBM,"invert", fgdbm_invert, 0);
+ rb_define_method(rb_cGDBM,"update", fgdbm_update, 1);
+ rb_define_method(rb_cGDBM,"replace", fgdbm_replace, 1);
+ rb_define_method(rb_cGDBM,"reorganize", fgdbm_reorganize, 0);
+ rb_define_method(rb_cGDBM,"sync", fgdbm_sync, 0);
+ /* rb_define_method(rb_cGDBM,"setopt", fgdbm_setopt, 2); */
+ rb_define_method(rb_cGDBM,"cachesize=", fgdbm_set_cachesize, 1);
+ rb_define_method(rb_cGDBM,"fastmode=", fgdbm_set_fastmode, 1);
+ rb_define_method(rb_cGDBM,"syncmode=", fgdbm_set_syncmode, 1);
+
+ rb_define_method(rb_cGDBM, "include?", fgdbm_has_key, 1);
+ rb_define_method(rb_cGDBM, "has_key?", fgdbm_has_key, 1);
+ rb_define_method(rb_cGDBM, "member?", fgdbm_has_key, 1);
+ rb_define_method(rb_cGDBM, "has_value?", fgdbm_has_value, 1);
+ rb_define_method(rb_cGDBM, "key?", fgdbm_has_key, 1);
+ rb_define_method(rb_cGDBM, "value?", fgdbm_has_value, 1);
+
+ rb_define_method(rb_cGDBM, "to_a", fgdbm_to_a, 0);
+ rb_define_method(rb_cGDBM, "to_hash", fgdbm_to_hash, 0);
/* flags for gdbm_opn() */
/*
- rb_define_const(cGDBM, "READER", INT2FIX(GDBM_READER));
- rb_define_const(cGDBM, "WRITER", INT2FIX(GDBM_WRITER));
- rb_define_const(cGDBM, "WRCREAT", INT2FIX(GDBM_WRCREAT));
- rb_define_const(cGDBM, "NEWDB", INT2FIX(GDBM_NEWDB));
+ rb_define_const(rb_cGDBM, "READER", INT2FIX(GDBM_READER));
+ rb_define_const(rb_cGDBM, "WRITER", INT2FIX(GDBM_WRITER));
+ rb_define_const(rb_cGDBM, "WRCREAT", INT2FIX(GDBM_WRCREAT));
+ rb_define_const(rb_cGDBM, "NEWDB", INT2FIX(GDBM_NEWDB));
*/
- rb_define_const(cGDBM, "FAST", INT2FIX(GDBM_FAST));
+ rb_define_const(rb_cGDBM, "FAST", INT2FIX(GDBM_FAST));
/* this flag is obsolete in gdbm 1.8.
On gdbm 1.8, fast mode is default behavior. */
/* gdbm version 1.8 specific */
#if defined(GDBM_SYNC)
- rb_define_const(cGDBM, "SYNC", INT2FIX(GDBM_SYNC));
+ rb_define_const(rb_cGDBM, "SYNC", INT2FIX(GDBM_SYNC));
#endif
#if defined(GDBM_NOLOCK)
- rb_define_const(cGDBM, "NOLOCK", INT2FIX(GDBM_NOLOCK));
+ rb_define_const(rb_cGDBM, "NOLOCK", INT2FIX(GDBM_NOLOCK));
#endif
- rb_define_const(cGDBM, "VERSION", rb_str_new2(gdbm_version));
+ rb_define_const(rb_cGDBM, "VERSION", rb_str_new2(gdbm_version));
}
diff --git a/ext/gdbm/testgdbm.rb b/ext/gdbm/testgdbm.rb
index 06f598cdafd..524d3f8ca3f 100644
--- a/ext/gdbm/testgdbm.rb
+++ b/ext/gdbm/testgdbm.rb
@@ -279,6 +279,25 @@ class TestGDBM < RUNIT::TestCase
assert_equals(values.reverse, @gdbm.indexes(*keys.reverse))
end
+ def test_select
+ keys = %w(foo bar baz)
+ values = %w(FOO BAR BAZ)
+ @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
+ assert_equals(values.reverse, @gdbm.select(*keys.reverse))
+ end
+
+ def test_select_with_block
+ keys = %w(foo bar baz)
+ values = %w(FOO BAR BAZ)
+ @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
+ ret = @gdbm.select {|k,v|
+ assert_equals(k.upcase, v)
+ k != "bar"
+ }
+ assert_equals([['baz', 'BAZ'], ['foo', 'FOO']],
+ ret.sort)
+ end
+
def test_length
num = 10
assert_equals(0, @gdbm.size)
@@ -411,7 +430,7 @@ class TestGDBM < RUNIT::TestCase
@gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
- assert_equals(@gdbm, @gdbm.delete(key))
+ assert_equals('BAR', @gdbm.delete(key))
assert_nil(@gdbm[key])
assert_equals(2, @gdbm.size)
@@ -428,12 +447,13 @@ class TestGDBM < RUNIT::TestCase
def test_delete_with_block
key = 'no called block'
@gdbm[key] = 'foo'
- assert_equals(@gdbm, @gdbm.delete(key) {|k| k.replace 'called block'})
+ assert_equals('foo', @gdbm.delete(key) {|k| k.replace 'called block'})
assert_equals('no called block', key)
assert_equals(0, @gdbm.size)
key = 'no called block'
- assert_nil(@gdbm.delete(key) {|k| k.replace 'called block'})
+ assert_equals(:blockval,
+ @gdbm.delete(key) {|k| k.replace 'called block'; :blockval})
assert_equals('called block', key)
assert_equals(0, @gdbm.size)
end
diff --git a/ext/sdbm/init.c b/ext/sdbm/init.c
index b3365ff429d..5412dd84528 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);
}
diff --git a/ext/sdbm/testsdbm.rb b/ext/sdbm/testsdbm.rb
index 2cea1e41445..550b47a0089 100644
--- a/ext/sdbm/testsdbm.rb
+++ b/ext/sdbm/testsdbm.rb
@@ -139,7 +139,7 @@ class TestSDBM < RUNIT::TestCase
assert_nil(sdbm.close)
# closed SDBM file
- assert_exception(RuntimeError) { sdbm.close }
+ assert_exception(SDBMError) { sdbm.close }
end
def test_aref
@@ -219,6 +219,25 @@ class TestSDBM < RUNIT::TestCase
assert_equals(values.reverse, @sdbm.indexes(*keys.reverse))
end
+ def test_select
+ keys = %w(foo bar baz)
+ values = %w(FOO BAR BAZ)
+ @sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
+ assert_equals(values.reverse, @sdbm.select(*keys.reverse))
+ end
+
+ def test_select_with_block
+ keys = %w(foo bar baz)
+ values = %w(FOO BAR BAZ)
+ @sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
+ ret = @sdbm.select {|k,v|
+ assert_equals(k.upcase, v)
+ k != "bar"
+ }
+ assert_equals([['baz', 'BAZ'], ['foo', 'FOO']],
+ ret.sort)
+ end
+
def test_length
num = 10
assert_equals(0, @sdbm.size)
@@ -351,7 +370,7 @@ class TestSDBM < RUNIT::TestCase
@sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
- assert_equals(@sdbm, @sdbm.delete(key))
+ assert_equals('BAR', @sdbm.delete(key))
assert_nil(@sdbm[key])
assert_equals(2, @sdbm.size)
@@ -360,12 +379,13 @@ class TestSDBM < RUNIT::TestCase
def test_delete_with_block
key = 'no called block'
@sdbm[key] = 'foo'
- assert_equals(@sdbm, @sdbm.delete(key) {|k| k.replace 'called block'})
+ assert_equals('foo', @sdbm.delete(key) {|k| k.replace 'called block'})
assert_equals('no called block', key)
assert_equals(0, @sdbm.size)
key = 'no called block'
- assert_nil(@sdbm.delete(key) {|k| k.replace 'called block'})
+ assert_equals(:blockval,
+ @sdbm.delete(key) {|k| k.replace 'called block'; :blockval})
assert_equals('called block', key)
assert_equals(0, @sdbm.size)
end