static VALUE fdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone) { datum key, value; struct dbmdata *dbmp; DBM *dbm; long len; ExportStringValue(keystr); len = RSTRING_LEN(keystr); if (TOO_LONG(len)) goto not_found; key.dptr = RSTRING_PTR(keystr); key.dsize = (DSIZE_TYPE)len; GetDBM2(obj, dbmp, dbm); value = dbm_fetch(dbm, key); if (value.dptr == 0) { not_found: if (NIL_P(ifnone) && rb_block_given_p()) { keystr = rb_str_dup(keystr); OBJ_TAINT(keystr); return rb_yield(keystr); } return ifnone; } return rb_tainted_str_new(value.dptr, value.dsize); }
static VALUE rb_gdbm_fetch3(VALUE obj, VALUE keystr) { struct dbmdata *dbmp; GDBM_FILE dbm; GetDBM2(obj, dbmp, dbm); return rb_gdbm_fetch2(dbm, keystr); }
/* * call-seq: * sdbm.key(value) -> key * * Returns the +key+ associated with the given +value+. If more than one * +key+ corresponds to the given +value+, then the first key to be found * will be returned. If no keys are found, +nil+ will be returned. */ static VALUE fsdbm_key(VALUE obj, VALUE valstr) { datum key, val; struct dbmdata *dbmp; DBM *dbm; ExportStringValue(valstr); val.dptr = RSTRING_PTR(valstr); val.dsize = RSTRING_LENINT(valstr); GetDBM2(obj, dbmp, dbm); for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { val = sdbm_fetch(dbm, key); if (val.dsize == RSTRING_LEN(valstr) && memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0) return rb_external_str_new(key.dptr, key.dsize); } return Qnil; }
static VALUE fsdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone) { datum key, value; struct dbmdata *dbmp; DBM *dbm; ExportStringValue(keystr); key.dptr = RSTRING_PTR(keystr); key.dsize = RSTRING_LENINT(keystr); GetDBM2(obj, dbmp, dbm); value = sdbm_fetch(dbm, key); if (value.dptr == 0) { if (ifnone == Qnil && rb_block_given_p()) return rb_yield(rb_external_str_new(key.dptr, key.dsize)); return ifnone; } return rb_external_str_new(value.dptr, value.dsize); }
/* * call-seq: * dbm.key(value) -> string * * Returns the key for the specified value. */ static VALUE fdbm_key(VALUE obj, VALUE valstr) { datum key, val; struct dbmdata *dbmp; DBM *dbm; ExportStringValue(valstr); val.dptr = RSTRING_PTR(valstr); val.dsize = (int)RSTRING_LEN(valstr); GetDBM2(obj, dbmp, dbm); for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) { val = dbm_fetch(dbm, key); if ((long)val.dsize == (int)RSTRING_LEN(valstr) && memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0) { return rb_tainted_str_new(key.dptr, key.dsize); } } return Qnil; }
/* * call-seq: * gdbm.key(value) -> key * * Returns the _key_ for a given _value_. If several keys may map to the * same value, the key that is found first will be returned. */ static VALUE fgdbm_key(VALUE obj, VALUE valstr) { struct dbmdata *dbmp; GDBM_FILE dbm; VALUE keystr, valstr2; StringValue(valstr); GetDBM2(obj, dbmp, dbm); for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr); keystr = rb_gdbm_nextkey(dbm, keystr)) { valstr2 = rb_gdbm_fetch2(dbm, keystr); if (!NIL_P(valstr2) && (int)RSTRING_LEN(valstr) == (int)RSTRING_LEN(valstr2) && memcmp(RSTRING_PTR(valstr), RSTRING_PTR(valstr2), (int)RSTRING_LEN(valstr)) == 0) { return keystr; } } return Qnil; }