ext/bdb1/bdb1.c in bdb1-0.2.4 vs ext/bdb1/bdb1.c in bdb1-0.2.5

- old
+ new

@@ -11,11 +11,11 @@ static ID id_bt_compare, id_bt_prefix, id_h_hash, bdb1_id_call; static VALUE bdb1_errstr; static int bdb1_errcall = 0; -static char * +static const char * db_strerror(int err) { if (err == 0) return "" ; @@ -24,12 +24,11 @@ return "Unknown Error" ; } int -bdb1_test_error(comm) - int comm; +bdb1_test_error(int comm) { VALUE error; switch (comm) { case 0: @@ -37,27 +36,22 @@ break; default: error = bdb1_eFatal; if (bdb1_errcall) { bdb1_errcall = 0; - rb_raise(error, "%s -- %s", StringValuePtr(bdb1_errstr), db_strerror(comm)); + rb_raise(error, "%s -- %s", StringValueCStr(bdb1_errstr), db_strerror(comm)); } else rb_raise(error, "%s", db_strerror(errno)); } return comm; } static VALUE -test_dump(obj, key, a, type_kv) - VALUE obj; - DBT *key; - VALUE a; - int type_kv; +test_dump(VALUE obj, DBT *key, VALUE a, int type_kv) { bdb1_DB *dbst; - int is_nil = 0; VALUE tmp = a; Data_Get_Struct(obj, bdb1_DB, dbst); if (dbst->filter[type_kv]) { if (FIXNUM_P(dbst->filter[type_kv])) { @@ -65,51 +59,48 @@ } else { tmp = rb_funcall(dbst->filter[type_kv], bdb1_id_call, 1, a); } } - if (dbst->marshal) { + if (dbst->marshal != Qundef) { if (rb_obj_is_kind_of(a, bdb1_cDelegate)) { tmp = bdb1_deleg_to_orig(tmp); } tmp = rb_funcall(dbst->marshal, id_dump, 1, tmp); if (TYPE(tmp) != T_STRING) { rb_raise(rb_eTypeError, "dump() must return String"); } } else { tmp = rb_obj_as_string(tmp); - if (a == Qnil) - is_nil = 1; + if (NIL_P(a)) { + key->data = StringValueCStr(tmp); + key->size = RSTRING_LEN(tmp) + 1; + return tmp; + } } key->data = StringValuePtr(tmp); - key->size = RSTRING_LEN(tmp) + is_nil; + key->size = RSTRING_LEN(tmp); return tmp; } static VALUE -test_ret(obj, tmp, a, type_kv) - VALUE obj, tmp, a; - int type_kv; +test_ret(VALUE obj, VALUE tmp, VALUE a, int type_kv) { bdb1_DB *dbst; Data_Get_Struct(obj, bdb1_DB, dbst); - if (dbst->marshal || a == Qnil) { + if (dbst->marshal != Qundef || NIL_P(a)) { return a; } if (dbst->filter[type_kv]) { return rb_obj_as_string(a); } return tmp; } static VALUE -test_recno(obj, key, recno, a) - VALUE obj; - DBT *key; - db_recno_t *recno; - VALUE a; +test_recno(VALUE obj, DBT *key, db_recno_t *recno, VALUE a) { bdb1_DB *dbst; Data_Get_Struct(obj, bdb1_DB, dbst); if (dbst->type == DB_RECNO) { *recno = NUM2INT(a) + dbst->array_base; @@ -119,21 +110,18 @@ } return test_dump(obj, key, a, FILTER_KEY); } VALUE -bdb1_test_load(obj, a, type_kv) - VALUE obj; - DBT *a; - int type_kv; +bdb1_test_load(VALUE obj, const DBT *a, int type_kv) { VALUE res; int i; bdb1_DB *dbst; Data_Get_Struct(obj, bdb1_DB, dbst); - if (dbst->marshal) { + if (dbst->marshal != Qundef) { res = rb_str_new(a->data, a->size); if (dbst->filter[2 + type_kv]) { if (FIXNUM_P(dbst->filter[2 + type_kv])) { res = rb_funcall(obj, NUM2INT(dbst->filter[2 + type_kv]), 1, res); @@ -166,21 +154,19 @@ } return res; } static VALUE -test_load_dyna(obj, key, val) - VALUE obj; - DBT *key, *val; +test_load_dyna(VALUE obj, DBT *key, DBT *val) { bdb1_DB *dbst; VALUE del, res, tmp; struct deleg_class *delegst; Data_Get_Struct(obj, bdb1_DB, dbst); res = bdb1_test_load(obj, val, FILTER_VALUE); - if (dbst->marshal && !SPECIAL_CONST_P(res)) { + if (dbst->marshal != Qundef && !SPECIAL_CONST_P(res)) { del = Data_Make_Struct(bdb1_cDelegate, struct deleg_class, bdb1_deleg_mark, bdb1_deleg_free, delegst); delegst->db = obj; if (dbst->type == DB_RECNO) { tmp = INT2NUM((*(db_recno_t *)key->data) - dbst->array_base); @@ -195,17 +181,16 @@ return res; } static int -bdb1_bt_compare(a, b) - DBT *a, *b; +bdb1_bt_compare(const DBT *a, const DBT *b) { VALUE obj, av, bv, res; bdb1_DB *dbst; - if ((obj = rb_thread_local_aref(rb_thread_current(), bdb1_id_current_db)) == Qnil) { + if (NIL_P(obj = rb_thread_local_aref(rb_thread_current(), bdb1_id_current_db))) { rb_raise(bdb1_eFatal, "BUG : current_db not set"); } Data_Get_Struct(obj, bdb1_DB, dbst); av = bdb1_test_load(obj, a, FILTER_VALUE); bv = bdb1_test_load(obj, b, FILTER_VALUE); @@ -215,17 +200,16 @@ res = rb_funcall(dbst->bt_compare, bdb1_id_call, 2, av, bv); return NUM2INT(res); } static size_t -bdb1_bt_prefix(a, b) - DBT *a, *b; +bdb1_bt_prefix(const DBT *a, const DBT *b) { VALUE obj, av, bv, res; bdb1_DB *dbst; - if ((obj = rb_thread_local_aref(rb_thread_current(), bdb1_id_current_db)) == Qnil) { + if (NIL_P(obj = rb_thread_local_aref(rb_thread_current(), bdb1_id_current_db))) { rb_raise(bdb1_eFatal, "BUG : current_db not set"); } Data_Get_Struct(obj, bdb1_DB, dbst); av = bdb1_test_load(obj, a, FILTER_VALUE); bv = bdb1_test_load(obj, b, FILTER_VALUE); @@ -235,51 +219,46 @@ res = rb_funcall(dbst->bt_prefix, bdb1_id_call, 2, av, bv); return NUM2INT(res); } static u_int32_t -bdb1_h_hash(bytes, length) - void *bytes; - u_int32_t length; +bdb1_h_hash(const void *bytes, size_t length) { VALUE obj, st, res; bdb1_DB *dbst; - if ((obj = rb_thread_local_aref(rb_thread_current(), bdb1_id_current_db)) == Qnil) { + if (NIL_P(obj = rb_thread_local_aref(rb_thread_current(), bdb1_id_current_db))) { rb_raise(bdb1_eFatal, "BUG : current_db not set"); } Data_Get_Struct(obj, bdb1_DB, dbst); st = rb_tainted_str_new((char *)bytes, length); if (dbst->h_hash == 0) res = rb_funcall(obj, id_h_hash, 1, st); else res = rb_funcall(dbst->h_hash, bdb1_id_call, 1, st); - return NUM2UINT(res); + return (u_int32_t)NUM2LONG(res); } static void -bdb1_i_close(dbst) - bdb1_DB *dbst; +bdb1_i_close(bdb1_DB *dbst) { if (dbst->dbp != NULL && !(dbst->options & BDB1_NOT_OPEN)) { dbst->options |= BDB1_NOT_OPEN; bdb1_test_error(dbst->dbp->close(dbst->dbp)); } dbst->dbp = NULL; } static void -bdb1_free(dbst) - bdb1_DB *dbst; +bdb1_free(bdb1_DB *dbst) { bdb1_i_close(dbst); free(dbst); } static void -bdb1_mark(dbst) - bdb1_DB *dbst; +bdb1_mark(bdb1_DB *dbst) { int i; rb_gc_mark(dbst->marshal); rb_gc_mark(dbst->bt_compare); @@ -289,22 +268,21 @@ rb_gc_mark(dbst->filter[i]); } } static VALUE -bdb1_i185_btree(obj, dbstobj) - VALUE obj, dbstobj; +bdb1_i185_btree(VALUE obj, VALUE dbstobj) { VALUE key, value; bdb1_DB *dbst; char *options; Data_Get_Struct(dbstobj, bdb1_DB, dbst); key = rb_ary_entry(obj, 0); value = rb_ary_entry(obj, 1); key = rb_obj_as_string(key); - options = StringValuePtr(key); + options = StringValueCStr(key); if (strcmp(options, "set_flags") == 0) { dbst->has_info = Qtrue; dbst->info.bi.flags = NUM2INT(value); } else if (strcmp(options, "set_cachesize") == 0) { @@ -343,22 +321,21 @@ } return Qnil; } static VALUE -bdb1_i185_hash(obj, dbstobj) - VALUE obj, dbstobj; +bdb1_i185_hash(VALUE obj, VALUE dbstobj) { VALUE key, value; bdb1_DB *dbst; char *options; Data_Get_Struct(dbstobj, bdb1_DB, dbst); key = rb_ary_entry(obj, 0); value = rb_ary_entry(obj, 1); key = rb_obj_as_string(key); - options = StringValuePtr(key); + options = StringValueCStr(key); if (strcmp(options, "set_h_ffactor") == 0) { dbst->has_info = Qtrue; dbst->info.hi.ffactor = NUM2INT(value); } else if (strcmp(options, "set_h_nelem") == 0) { @@ -384,30 +361,29 @@ } return Qnil; } static VALUE -bdb1_i185_recno(obj, dbstobj) - VALUE obj, dbstobj; +bdb1_i185_recno(VALUE obj, VALUE dbstobj) { VALUE key, value; bdb1_DB *dbst; char *options, *str; Data_Get_Struct(dbstobj, bdb1_DB, dbst); key = rb_ary_entry(obj, 0); value = rb_ary_entry(obj, 1); key = rb_obj_as_string(key); - options = StringValuePtr(key); + options = StringValueCStr(key); if (strcmp(options, "set_flags") == 0) { dbst->has_info = Qtrue; dbst->info.ri.flags = NUM2INT(value); } else if (strcmp(options, "set_re_delim") == 0) { int ch; if (TYPE(value) == T_STRING) { - str = StringValuePtr(value); + str = StringValueCStr(value); dbst->info.ri.bval = str[0]; } else { dbst->info.ri.bval = NUM2INT(value); } @@ -420,11 +396,11 @@ dbst->info.ri.flags |= R_FIXEDLEN; } else if (strcmp(options, "set_re_pad") == 0) { int ch; if (TYPE(value) == T_STRING) { - str = StringValuePtr(value); + str = StringValueCStr(value); dbst->info.ri.bval = str[0]; } else { dbst->info.ri.bval = NUM2INT(value); } @@ -454,12 +430,11 @@ } return Qnil; } static VALUE -bdb1_load_dump(obj) - VALUE obj; +bdb1_load_dump(VALUE obj) { VALUE res; res = rb_funcall(obj, rb_intern("respond_to?"), 2, ID2SYM(id_load), Qtrue); if (RTEST(res)) { @@ -467,30 +442,29 @@ } return res; } static VALUE -bdb1_i185_common(obj, dbstobj) - VALUE obj, dbstobj; +bdb1_i185_common(VALUE obj, VALUE dbstobj) { VALUE key, value; bdb1_DB *dbst; char *options; Data_Get_Struct(dbstobj, bdb1_DB, dbst); key = rb_ary_entry(obj, 0); value = rb_ary_entry(obj, 1); key = rb_obj_as_string(key); - options = StringValuePtr(key); + options = StringValueCStr(key); if (strcmp(options, "marshal") == 0) { switch (value) { case Qtrue: dbst->marshal = bdb1_mMarshal; dbst->options |= BDB1_MARSHAL; break; case Qfalse: - dbst->marshal = Qfalse; + dbst->marshal = Qundef; dbst->options &= ~BDB1_MARSHAL; break; default: if (!RTEST(bdb1_load_dump(value))) { rb_raise(bdb1_eFatal, "marshal value must be true or false"); @@ -526,16 +500,15 @@ } return Qnil; } static int -bdb1_hard_count(dbp) - DB *dbp; +bdb1_hard_count(DB *dbp) { DBT key, data; db_recno_t recno; - long count = 0; + int count = 0; DATA_ZERO(key); key.data = &recno; key.size = sizeof(db_recno_t); DATA_ZERO(data); @@ -642,14 +615,11 @@ * end * end * end */ VALUE -bdb1_init(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +bdb1_init(int argc, VALUE *argv, VALUE obj) { VALUE b, c, d, f; int mode, oflags; char *name; bdb1_DB *dbst; @@ -666,11 +636,11 @@ case 3: mode = NUM2INT(d); /* ... */ case 2: if (TYPE(c) == T_STRING) { - char *m = StringValuePtr(c); + char *m = StringValueCStr(c); if (strcmp(m, "r") == 0) { oflags = DB_RDONLY; } else if (strcmp(m, "r+") == 0) { oflags = DB_WRITE; @@ -683,21 +653,21 @@ } else { rb_raise(bdb1_eFatal, "flags must be r, r+, w, w+, a or a+"); } } - else if (c == Qnil) { + else if (NIL_P(c)) { oflags = DB_RDONLY; } else { oflags = NUM2INT(c); } /* ... */ case 1: if (!NIL_P(b)) { SafeStringValue(b); - name = StringValuePtr(b); + name = StringValueCStr(b); } else { name = NULL; } /* ... */ @@ -756,12 +726,11 @@ * db.close(flags = 0) * * Closes the file. */ static VALUE -bdb1_close(obj) - VALUE obj; +bdb1_close(VALUE obj) { VALUE opt; bdb1_DB *dbst; if (!OBJ_TAINTED(obj) && rb_safe_level() >= 4) { @@ -771,17 +740,17 @@ bdb1_i_close(dbst); return Qnil; } VALUE -bdb1_s_alloc(obj) - VALUE obj; +bdb1_s_alloc(VALUE obj) { bdb1_DB *dbst; VALUE res, cl; res = Data_Make_Struct(obj, bdb1_DB, bdb1_mark, bdb1_free, dbst); + dbst->marshal = Qundef; dbst->options |= BDB1_NOT_OPEN; cl = obj; while (cl) { if (cl == bdb1_cBtree || RCLASS(cl)->m_tbl == RCLASS(bdb1_cBtree)->m_tbl) { dbst->type = DB_BTREE; @@ -793,53 +762,49 @@ } else if (cl == bdb1_cRecnum || RCLASS(cl)->m_tbl == RCLASS(bdb1_cRecnum)->m_tbl) { dbst->type = DB_RECNO; break; } - cl = RCLASS(cl)->super; + cl = RCLASS_SUPER(cl); } if (!cl) { rb_raise(bdb1_eFatal, "unknown database type"); } if (RTEST(bdb1_load_dump(obj))) { dbst->marshal = obj; dbst->options |= BDB1_MARSHAL; } - if (rb_method_boundp(obj, rb_intern("bdb1_store_key"), 0) == Qtrue) { + if (rb_method_boundp(obj, rb_intern("bdb1_store_key"), 0)) { dbst->filter[FILTER_KEY] = INT2FIX(rb_intern("bdb1_store_key")); } - if (rb_method_boundp(obj, rb_intern("bdb1_fetch_key"), 0) == Qtrue) { + if (rb_method_boundp(obj, rb_intern("bdb1_fetch_key"), 0)) { dbst->filter[2 + FILTER_KEY] = INT2FIX(rb_intern("bdb1_fetch_key")); } - if (rb_method_boundp(obj, rb_intern("bdb1_store_value"), 0) == Qtrue) { + if (rb_method_boundp(obj, rb_intern("bdb1_store_value"), 0)) { dbst->filter[FILTER_VALUE] = INT2FIX(rb_intern("bdb1_store_value")); } - if (rb_method_boundp(obj, rb_intern("bdb1_fetch_value"), 0) == Qtrue) { + if (rb_method_boundp(obj, rb_intern("bdb1_fetch_value"), 0)) { dbst->filter[2 + FILTER_VALUE] = INT2FIX(rb_intern("bdb1_fetch_value")); } return res; } /* * Same as +new+. */ static VALUE -bdb1_s_create(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +bdb1_s_create(int argc, VALUE *argv, VALUE obj) { VALUE st, res; res = rb_funcall2(obj, rb_intern("allocate"), 0, 0); rb_obj_call_init(res, argc, argv); return res; } static VALUE -bdb1_i_create(obj, db) - VALUE obj, db; +bdb1_i_create(VALUE obj, VALUE db) { VALUE tmp[2]; tmp[0] = rb_ary_entry(obj, 0); tmp[1] = rb_ary_entry(obj, 1); bdb1_put(2, tmp, db); @@ -857,14 +822,11 @@ * * Creates a new temporary on-memory database, populated with the * given hash or pairs of objects. */ static VALUE -bdb1_s_aref(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +bdb1_s_aref(int argc, VALUE *argv, VALUE obj) { VALUE res, tmp[2]; int i; res = rb_funcall2(obj, rb_intern("new"), 0, 0); @@ -884,14 +846,11 @@ /* * Same as +new+ except that if a block is given it is called with an * initialized object which is automatically closed when done. */ static VALUE -bdb1_s_open(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +bdb1_s_open(int argc, VALUE *argv, VALUE obj) { VALUE res = rb_funcall2(obj, rb_intern("new"), argc, argv); if (rb_block_given_p()) { return rb_ensure(rb_yield, res, bdb1_close, res); } @@ -907,14 +866,11 @@ * * +flags+ can have the value +DBD::NOOVERWRITE+, in this case it will * return +nil+ if the specified key exist, otherwise +true+. */ VALUE -bdb1_put(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +bdb1_put(int argc, VALUE *argv, VALUE obj) { volatile VALUE a0 = Qnil; volatile VALUE b0 = Qnil; VALUE a, b, c; bdb1_DB *dbst; @@ -922,75 +878,60 @@ int ret, flags; db_recno_t recno; rb_secure(4); GetDB(obj, dbst); - flags = 0; - a = b = c = Qnil; - if (rb_scan_args(argc, argv, "21", &a, &b, &c) == 3) { + if (rb_scan_args(argc, argv, "21", &a, &b, &c) == 3) flags = NUM2INT(c); - } + else + flags = 0; DATA_ZERO(key); DATA_ZERO(data); a0 = test_recno(obj, &key, &recno, a); b0 = test_dump(obj, &data, b, FILTER_VALUE); ret = bdb1_test_error(dbst->dbp->put(dbst->dbp, &key, &data, flags)); if (ret == DB_KEYEXIST) return Qfalse; - else { - return test_ret(obj, b0, b, FILTER_VALUE); - } + return test_ret(obj, b0, b, FILTER_VALUE); } static VALUE -bdb1_assign(obj, a, b) - VALUE obj, a, b; +bdb1_assign(VALUE obj, VALUE a, VALUE b) { VALUE tmp[2]; tmp[0] = a; tmp[1] = b; bdb1_put(2, tmp, obj); return b; } static VALUE -test_load_key(obj, key) - VALUE obj; - DBT *key; +test_load_key(VALUE obj, DBT *key) { bdb1_DB *dbst; Data_Get_Struct(obj, bdb1_DB, dbst); if (dbst->type == DB_RECNO) return INT2NUM((*(db_recno_t *)key->data) - dbst->array_base); return bdb1_test_load(obj, key, FILTER_KEY); } static VALUE -bdb1_assoc(obj, key, data) - VALUE obj; - DBT *key, *data; +bdb1_assoc(VALUE obj, DBT *key, DBT *data) { return rb_assoc_new(test_load_key(obj, key), bdb1_test_load(obj, data, FILTER_VALUE)); } static VALUE -bdb1_assoc_dyna(obj, key, data) - VALUE obj; - DBT *key, *data; +bdb1_assoc_dyna(VALUE obj, DBT *key, DBT *data) { return rb_assoc_new(test_load_key(obj, key), test_load_dyna(obj, key, data)); } static VALUE -bdb1_get_internal(argc, argv, obj, notfound, dyna) - int argc; - VALUE *argv; - VALUE obj; - VALUE notfound; - int dyna; +bdb1_get_internal(int argc, VALUE *argv, VALUE obj, VALUE notfound, int dyna) { volatile VALUE a = Qnil; VALUE b, c; bdb1_DB *dbst; DBT key, data; @@ -1022,14 +963,11 @@ return bdb1_test_load(obj, &data, FILTER_VALUE); } } VALUE -bdb1_get(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +bdb1_get(int argc, VALUE *argv, VALUE obj) { return bdb1_get_internal(argc, argv, obj, Qnil, 0); } /* @@ -1038,22 +976,17 @@ * db[key, flags = 0] * * Returns the value corresponding to +key+. */ static VALUE -bdb1_get_dyna(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +bdb1_get_dyna(int argc, VALUE *argv, VALUE obj) { return bdb1_get_internal(argc, argv, obj, Qnil, 1); } static VALUE -bdb1_fetch(argc, argv, obj) - int argc; - VALUE *argv, obj; +bdb1_fetch(int argc, VALUE *argv, VALUE obj) { VALUE key, if_none; VALUE val; rb_scan_args(argc, argv, "11", &key, &if_none); @@ -1072,25 +1005,23 @@ } return val; } static VALUE -bdb1_has_key(obj, key) - VALUE obj, key; +bdb1_has_key(VALUE obj, VALUE key) { - return bdb1_get_internal(1, &key, obj, Qfalse); + return bdb1_get_internal(1, &key, obj, Qfalse, 0); } /* * call-seq: * db.has_both?(key, value) * * Returns +true+ if the association from +key+ is +value+. */ static VALUE -bdb1_has_both(obj, a, b) - VALUE obj, a, b; +bdb1_has_both(VALUE obj, VALUE a, VALUE b) { bdb1_DB *dbst; DBT key, data; DBT keys, datas; int ret, flags; @@ -1130,12 +1061,11 @@ * * It returns the object deleted or +nil+ if the specified key doesn't * exist. */ VALUE -bdb1_del(obj, a) - VALUE a, obj; +bdb1_del(VALUE obj, VALUE a) { bdb1_DB *dbst; DBT key; int ret; db_recno_t recno; @@ -1154,12 +1084,11 @@ else return obj; } static VALUE -bdb1_empty(obj) - VALUE obj; +bdb1_empty(VALUE obj) { bdb1_DB *dbst; DBT key, data; int ret, flags; db_recno_t recno; @@ -1174,12 +1103,11 @@ FREE_KEY(dbst, key); return Qfalse; } static VALUE -bdb1_delete_if(obj) - VALUE obj; +bdb1_delete_if(VALUE obj) { bdb1_DB *dbst; DBT key, data, save; int ret, ret1, flags; db_recno_t recno; @@ -1201,12 +1129,11 @@ } while (1); return obj; } VALUE -bdb1_clear(obj) - VALUE obj; +bdb1_clear(VALUE obj) { bdb1_DB *dbst; DBT key, data, save; int ret, value, flags; db_recno_t recno; @@ -1227,12 +1154,11 @@ } while (1); return INT2NUM(-1); } static VALUE -bdb1_length(obj) - VALUE obj; +bdb1_length(VALUE obj) { bdb1_DB *dbst; DBT key, data; int ret, value, flags; db_recno_t recno; @@ -1256,13 +1182,11 @@ } while (1); return INT2NUM(value); } static VALUE -bdb1_each_valuec(obj, sens, result) - VALUE obj, result; - int sens; +bdb1_each_valuec(VALUE obj, VALUE sens, VALUE result) { bdb1_DB *dbst; DBT key, data; int ret, flags; db_recno_t recno; @@ -1279,23 +1203,30 @@ } flags = sens; FREE_KEY(dbst, key); interm = bdb1_test_load(obj, &data, FILTER_VALUE); rest = rb_yield(interm); - if (result != Qnil && RTEST(rest)) { + if (!NIL_P(result) && RTEST(rest)) { rb_ary_push(result, interm); } } while (1); } -VALUE bdb1_each_value(obj) VALUE obj;{ return bdb1_each_valuec(obj, DB_NEXT, Qnil); } -VALUE bdb1_each_eulav(obj) VALUE obj;{ return bdb1_each_valuec(obj, DB_PREV, Qnil); } +VALUE +bdb1_each_value(VALUE obj) +{ + return bdb1_each_valuec(obj, DB_NEXT, Qnil); +} +VALUE +bdb1_each_eulav(VALUE obj) +{ + return bdb1_each_valuec(obj, DB_PREV, Qnil); +} + static VALUE -bdb1_each_keyc(obj, sens) - VALUE obj; - int sens; +bdb1_each_keyc(VALUE obj, int sens) { bdb1_DB *dbst; DBT key, data; int ret, flags; db_recno_t recno; @@ -1313,18 +1244,25 @@ flags = sens; } while (1); return obj; } -VALUE bdb1_each_key(obj) VALUE obj;{ return bdb1_each_keyc(obj, DB_NEXT); } -static VALUE bdb1_each_yek(obj) VALUE obj;{ return bdb1_each_keyc(obj, DB_PREV); } +VALUE +bdb1_each_key(VALUE obj) +{ + return bdb1_each_keyc(obj, DB_NEXT); +} static VALUE -bdb1_each_common(obj, sens) - VALUE obj; - int sens; +bdb1_each_yek(VALUE obj) { + return bdb1_each_keyc(obj, DB_PREV); +} + +static VALUE +bdb1_each_common(VALUE obj, int sens) +{ bdb1_DB *dbst; DBT key, data; int ret, flags; db_recno_t recno; @@ -1341,79 +1279,83 @@ flags = sens; } while (1); return obj; } -static VALUE bdb1_each_pair(obj) VALUE obj;{ return bdb1_each_common(obj, DB_NEXT); } -static VALUE bdb1_each_riap(obj) VALUE obj;{ return bdb1_each_common(obj, DB_PREV); } +static VALUE +bdb1_each_pair(VALUE obj) +{ + return bdb1_each_common(obj, DB_NEXT); +} +static VALUE +bdb1_each_riap(VALUE obj) +{ + return bdb1_each_common(obj, DB_PREV); +} + VALUE -bdb1_to_type(obj, result, flag) - VALUE obj, result, flag; +bdb1_to_type(VALUE obj, VALUE result, VALUE flag) { bdb1_DB *dbst; DBT key, data; int ret, flags; db_recno_t recno; GetDB(obj, dbst); INIT_RECNO(dbst, key, recno); DATA_ZERO(data); - flags = (flag == Qnil)?DB_LAST:DB_FIRST; + flags = NIL_P(flag) ? DB_LAST : DB_FIRST; do { ret = bdb1_test_error(dbst->dbp->seq(dbst->dbp, &key, &data, flags)); if (ret == DB_NOTFOUND) { return result; } switch (TYPE(result)) { case T_ARRAY: - if (flag == Qtrue) { + if (RTEST(flag)) { rb_ary_push(result, bdb1_assoc(obj, &key, &data)); } else { rb_ary_push(result, bdb1_test_load(obj, &data, FILTER_VALUE)); } break; case T_HASH: - if (flag == Qtrue) { + if (RTEST(flag)) { rb_hash_aset(result, test_load_key(obj, &key), bdb1_test_load(obj, &data, FILTER_VALUE)); } else { rb_hash_aset(result, bdb1_test_load(obj, &data, FILTER_VALUE), test_load_key(obj, &key)); } } - flags = (flag == Qnil)?DB_PREV:DB_NEXT; + flags = NIL_P(flag) ? DB_PREV : DB_NEXT; } while (1); return result; } static VALUE -bdb1_to_a(obj) - VALUE obj; +bdb1_to_a(VALUE obj) { return bdb1_to_type(obj, rb_ary_new(), Qtrue); } static VALUE -bdb1_invert(obj) - VALUE obj; +bdb1_invert(VALUE obj) { return bdb1_to_type(obj, rb_hash_new(), Qfalse); } static VALUE -bdb1_to_hash(obj) - VALUE obj; +bdb1_to_hash(VALUE obj) { return bdb1_to_type(obj, rb_hash_new(), Qtrue); } static VALUE -bdb1_each_kv(obj, a, result, flag) - VALUE obj, a, result, flag; +bdb1_each_kv(VALUE obj, VALUE a, VALUE result, VALUE flag) { bdb1_DB *dbst; DBT keys, key, data; int ret, flags; db_recno_t recno; @@ -1427,11 +1369,11 @@ flags = R_CURSOR; while (1) { ret = bdb1_test_error(dbst->dbp->seq(dbst->dbp, &key, &data, flags)); if (ret == DB_NOTFOUND || keys.size != key.size || memcmp(keys.data, key.data, key.size) != 0) { - return (result == Qnil)?obj:result; + return NIL_P(result)?obj:result; } k = bdb1_test_load(obj, &data, FILTER_VALUE); if (RTEST(flag)) { k = rb_assoc_new(test_load_key(obj, &key), k); } @@ -1453,13 +1395,11 @@ * Returns an array of all duplicate associations for the +key+. * * If +assoc+ is +false+ return only the values. */ static VALUE -bdb1_bt_duplicates(argc, argv, obj) - int argc; - VALUE *argv, obj; +bdb1_bt_duplicates(int argc, VALUE *argv, VALUE obj) { VALUE a, b; if (rb_scan_args(argc, argv, "11", &a, &b) == 1) { b = Qtrue; @@ -1472,12 +1412,11 @@ * db.each_dup(key) * * Iterates over duplicate associations for the +key+. */ static VALUE -bdb1_bt_dup(obj, a) - VALUE a, obj; +bdb1_bt_dup(VALUE obj, VALUE a) { return bdb1_each_kv(obj, a, Qnil, Qtrue); } /* @@ -1485,26 +1424,23 @@ * db.each_dup_value(key) * * Iterates over duplicate values for the +key+. */ static VALUE -bdb1_bt_dupval(obj, a) - VALUE a, obj; +bdb1_bt_dupval(VALUE obj, VALUE a) { return bdb1_each_kv(obj, a, Qnil, Qfalse); } static VALUE -bdb1_reject(obj) - VALUE obj; +bdb1_reject(VALUE obj) { return rb_hash_delete_if(bdb1_to_hash(obj)); } static VALUE -bdb1_values(obj) - VALUE obj; +bdb1_values(VALUE obj) { bdb1_DB *dbst; DBT key, data; int ret, flags; db_recno_t recno; @@ -1526,81 +1462,53 @@ } while (1); return ary; } VALUE -bdb1_internal_value(obj, a, b, sens) - VALUE obj, a, b; - int sens; +bdb1_internal_value(VALUE obj, VALUE a, VALUE b, int sens) { bdb1_DB *dbst; DBT key, data; int ret, flags; db_recno_t recno; GetDB(obj, dbst); INIT_RECNO(dbst, key, recno); DATA_ZERO(data); flags = (sens == DB_NEXT)?DB_FIRST:DB_LAST; - do { + for (;;) { ret = bdb1_test_error(dbst->dbp->seq(dbst->dbp, &key, &data, flags)); if (ret == DB_NOTFOUND) { - return (b == Qfalse)?Qfalse:Qnil; + return RTEST(b) ? Qnil : Qfalse; } flags = sens; - if (rb_equal(a, bdb1_test_load(obj, &data, FILTER_VALUE)) == Qtrue) { + if (RTEST(rb_equal(a, bdb1_test_load(obj, &data, FILTER_VALUE)))) { VALUE d; - d = (b == Qfalse)?Qtrue:test_load_key(obj, &key); + d = RTEST(b) ? test_load_key(obj, &key) : Qtrue; FREE_KEY(dbst, key); return d; } FREE_KEY(dbst, key); - } while (1); - return (b == Qfalse)?Qfalse:Qnil; + } + return RTEST(b) ? Qnil : Qfalse; } VALUE -bdb1_index(obj, a) - VALUE obj, a; +bdb1_key(VALUE obj, VALUE a) { return bdb1_internal_value(obj, a, Qtrue, DB_NEXT); } -static VALUE -bdb1_indexes(argc, argv, obj) - int argc; - VALUE obj, *argv; -{ - VALUE indexes; - int i; - -#if HAVE_RB_ARY_VALUES_AT - rb_warn("BDB1#%s is deprecated; use BDB1#values_at", -#if HAVE_RB_FRAME_THIS_FUNC - rb_id2name(rb_frame_this_func())); -#else - rb_id2name(rb_frame_last_func())); -#endif -#endif - indexes = rb_ary_new2(argc); - for (i = 0; i < argc; i++) { - rb_ary_push(indexes, bdb1_get(1, argv + i, obj)); - } - return indexes; -} - VALUE -bdb1_has_value(obj, a) - VALUE obj, a; +bdb1_has_value(VALUE obj, VALUE a) { return bdb1_internal_value(obj, a, Qfalse, DB_NEXT); } static VALUE -bdb1_keys(obj) - VALUE obj; +bdb1_keys(VALUE obj) { bdb1_DB *dbst; DBT key, data; int ret, flags; db_recno_t recno; @@ -1622,66 +1530,46 @@ } while (1); return ary; } static VALUE -bdb1_sync(obj) - VALUE obj; +bdb1_sync(VALUE obj) { bdb1_DB *dbst; if (!OBJ_TAINTED(obj) && rb_safe_level() >= 4) rb_raise(rb_eSecurityError, "Insecure: can't sync the database"); GetDB(obj, dbst); bdb1_test_error(dbst->dbp->sync(dbst->dbp, 0)); return Qtrue; } -#if HAVE_RB_ARY_VALUES_AT - static VALUE -bdb1_values_at(argc, argv, obj) - int argc; - VALUE *argv, obj; +bdb1_values_at(int argc, VALUE *argv, VALUE obj) { VALUE result = rb_ary_new2(argc); long i; for (i = 0; i < argc; i++) { rb_ary_push(result, bdb1_get(1, argv + i, obj)); } return result; } -#endif - -#if HAVE_RB_ARY_SELECT - static VALUE -bdb1_select(argc, argv, obj) - int argc; - VALUE *argv, obj; +bdb1_select(VALUE obj) { VALUE result = rb_ary_new(); - long i; if (rb_block_given_p()) { - if (argc > 0) { - rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc); - } return bdb1_each_valuec(obj, DB_NEXT, result); } - rb_warn("Common#select(index..) is deprecated; use Common#values_at"); - return bdb1_values_at(argc, argv, obj); + rb_raise(rb_eArgError, "block is not given"); } -#endif - VALUE -bdb1_each_vc(obj, replace, rtest) - VALUE obj; - int replace, rtest; +bdb1_each_vc(VALUE obj, int replace, int rtest) { bdb1_DB *dbst; DBT key, data; int ret, flags; db_recno_t recno; @@ -1707,11 +1595,11 @@ } } else { rb_ary_push(result, res); } - if (replace == Qtrue) { + if (RTEST(replace)) { DATA_ZERO(data); res = test_dump(obj, &data, res, FILTER_VALUE); bdb1_test_error(dbst->dbp->put(dbst->dbp, &key, &data, 0)); } } while (1); @@ -1747,11 +1635,11 @@ * Fixed and Variable-Length Records. Stores fixed- or * variable-length records in sequential order. * */ void -Init_bdb1() +Init_bdb1(void) { bdb1_mMarshal = rb_const_get(rb_cObject, rb_intern("Marshal")); id_dump = rb_intern("dump"); id_load = rb_intern("load"); bdb1_id_current_db = rb_intern("bdb1_current_db"); @@ -1787,15 +1675,11 @@ rb_define_const(bdb1_mDb, "NOOVERWRITE", INT2FIX(DB_NOOVERWRITE)); /* DATABASE */ bdb1_cCommon = rb_define_class_under(bdb1_mDb, "Common", rb_cObject); rb_define_method(bdb1_cCommon, "initialize", bdb1_init, -1); rb_include_module(bdb1_cCommon, rb_mEnumerable); -#ifdef HAVE_RB_DEFINE_ALLOC_FUNC rb_define_alloc_func(bdb1_cCommon, bdb1_s_alloc); -#else - rb_define_singleton_method(bdb1_cCommon, "allocate", bdb1_s_alloc, 0); -#endif rb_define_singleton_method(bdb1_cCommon, "create", bdb1_s_create, -1); rb_define_singleton_method(bdb1_cCommon, "open", bdb1_s_open, -1); rb_define_singleton_method(bdb1_cCommon, "[]", bdb1_s_aref, -1); rb_define_method(bdb1_cCommon, "close", bdb1_close, 0); rb_define_method(bdb1_cCommon, "db_close", bdb1_close, 0); @@ -1839,18 +1723,13 @@ rb_define_method(bdb1_cCommon, "to_hash", bdb1_to_hash, 0); rb_define_method(bdb1_cCommon, "invert", bdb1_invert, 0); rb_define_method(bdb1_cCommon, "empty?", bdb1_empty, 0); rb_define_method(bdb1_cCommon, "length", bdb1_length, 0); rb_define_alias(bdb1_cCommon, "size", "length"); - rb_define_method(bdb1_cCommon, "index", bdb1_index, 1); - rb_define_method(bdb1_cCommon, "indexes", bdb1_indexes, -1); - rb_define_method(bdb1_cCommon, "indices", bdb1_indexes, -1); -#if HAVE_RB_ARY_SELECT - rb_define_method(bdb1_cCommon, "select", bdb1_select, -1); -#endif -#if HAVE_RB_ARY_VALUES_AT + rb_define_method(bdb1_cCommon, "key", bdb1_key, 1); + rb_define_method(bdb1_cCommon, "index", bdb1_key, 1); + rb_define_method(bdb1_cCommon, "select", bdb1_select, 0); rb_define_method(bdb1_cCommon, "values_at", bdb1_values_at, -1); -#endif bdb1_cBtree = rb_define_class_under(bdb1_mDb, "Btree", bdb1_cCommon); rb_define_method(bdb1_cBtree, "duplicates", bdb1_bt_duplicates, -1); rb_define_method(bdb1_cBtree, "each_dup", bdb1_bt_dup, 1); rb_define_method(bdb1_cBtree, "each_dup_value", bdb1_bt_dupval, 1); bdb1_cHash = rb_define_class_under(bdb1_mDb, "Hash", bdb1_cCommon);