74 static VALUE rb_cGDBM, rb_eGDBMError, rb_eGDBMFatalError;
76 #if SIZEOF_LONG > SIZEOF_INT 77 #define TOO_LONG(n) ((long)(+(int)(n)) != (long)(n)) 82 #define RUBY_GDBM_RW_BIT 0x20000000 84 #define MY_BLOCK_SIZE (2048) 85 #define MY_FATAL_FUNC rb_gdbm_fatal 87 rb_gdbm_fatal(
const char *msg)
89 rb_raise(rb_eGDBMFatalError,
"%s", msg);
103 #define GetDBM(obj, dbmp) do {\ 104 TypedData_Get_Struct((obj), struct dbmdata, &dbm_type, (dbmp));\ 105 if ((dbmp) == 0) closed_dbm();\ 106 if ((dbmp)->di_dbm == 0) closed_dbm();\ 109 #define GetDBM2(obj, dbmp, dbm) do {\ 110 GetDBM((obj), (dbmp));\ 111 (dbm) = (dbmp)->di_dbm;\ 125 memsize_dbm(
const void *ptr)
128 const struct dbmdata *dbmp = ptr;
130 size +=
sizeof(*dbmp);
131 if (dbmp->
di_dbm) size += DBM_SIZEOF_DBM;
138 {0, free_dbm, memsize_dbm,},
150 fgdbm_close(
VALUE obj)
168 fgdbm_closed(
VALUE obj)
182 fgdbm_s_alloc(
VALUE klass)
213 VALUE file, vmode, vflags;
221 else if (
NIL_P(vmode)) {
235 flags |= GDBM_CLOEXEC;
239 flags &= ~RUBY_GDBM_RW_BIT;
261 if (mode == -1)
return Qnil;
263 if (gdbm_errno == GDBM_FILE_OPEN_ERROR ||
264 gdbm_errno == GDBM_CANT_BE_READER ||
265 gdbm_errno == GDBM_CANT_BE_WRITER)
268 rb_raise(rb_eGDBMError,
"%s", gdbm_strerror(gdbm_errno));
302 VALUE obj = fgdbm_s_alloc(klass);
316 rb_gdbm_fetch(GDBM_FILE dbm,
datum key)
321 val = gdbm_fetch(dbm, key);
332 rb_gdbm_fetch2(GDBM_FILE dbm,
VALUE keystr)
341 key.
dsize = (int)len;
343 return rb_gdbm_fetch(dbm, key);
353 return rb_gdbm_fetch2(dbm, keystr);
357 rb_gdbm_firstkey(GDBM_FILE dbm)
362 key = gdbm_firstkey(dbm);
373 rb_gdbm_nextkey(GDBM_FILE dbm,
VALUE keystr)
382 key.
dsize = (int)len;
383 key2 = gdbm_nextkey(dbm, key);
398 valstr = rb_gdbm_fetch3(obj, keystr);
416 return rb_gdbm_fetch3(obj, keystr);
429 VALUE keystr, valstr, ifnone;
432 valstr = fgdbm_fetch(obj, keystr, ifnone);
451 VALUE keystr, valstr2;
455 for (keystr = rb_gdbm_firstkey(dbm);
RTEST(keystr);
456 keystr = rb_gdbm_nextkey(dbm, keystr)) {
458 valstr2 = rb_gdbm_fetch2(dbm, keystr);
459 if (!
NIL_P(valstr2) &&
473 rb_warn(
"GDBM#index is deprecated; use GDBM#key");
474 return fgdbm_key(obj, value);
485 fgdbm_select(
VALUE obj)
493 for (keystr = rb_gdbm_firstkey(dbm);
RTEST(keystr);
494 keystr = rb_gdbm_nextkey(dbm, keystr)) {
519 for (i=0; i<
argc; i++) {
527 rb_gdbm_modify(
VALUE obj)
545 key.
dsize = (int)len;
548 if (!gdbm_exists(dbm, key)) {
552 if (gdbm_delete(dbm, key)) {
554 rb_raise(rb_eGDBMError,
"%s", gdbm_strerror(gdbm_errno));
574 valstr = fgdbm_fetch(obj, keystr,
Qnil);
575 rb_gdbm_delete(obj, keystr);
587 fgdbm_shift(
VALUE obj)
591 VALUE keystr, valstr;
595 keystr = rb_gdbm_firstkey(dbm);
597 valstr = rb_gdbm_fetch2(dbm, keystr);
598 rb_gdbm_delete(obj, keystr);
611 fgdbm_delete_if(
VALUE obj)
615 VALUE keystr, valstr;
625 for (keystr = rb_gdbm_firstkey(dbm);
RTEST(keystr);
626 keystr = rb_gdbm_nextkey(dbm, keystr)) {
629 valstr = rb_gdbm_fetch2(dbm, keystr);
631 if (status != 0)
break;
652 fgdbm_clear(
VALUE obj)
663 while (key = gdbm_firstkey(dbm), key.
dptr) {
664 if (gdbm_delete(dbm, key)) {
666 rb_raise(rb_eGDBMError,
"%s", gdbm_strerror(gdbm_errno));
671 while (key = gdbm_firstkey(dbm), key.
dptr) {
672 for (; key.
dptr; key = nextkey) {
673 nextkey = gdbm_nextkey(dbm, key);
674 if (gdbm_delete(dbm, key)) {
677 rb_raise(rb_eGDBMError,
"%s", gdbm_strerror(gdbm_errno));
696 fgdbm_invert(
VALUE obj)
700 VALUE keystr, valstr;
704 for (keystr = rb_gdbm_firstkey(dbm);
RTEST(keystr);
705 keystr = rb_gdbm_nextkey(dbm, keystr)) {
706 valstr = rb_gdbm_fetch2(dbm, keystr);
739 if (gdbm_store(dbm, key, val, GDBM_REPLACE)) {
741 rb_raise(rb_eGDBMError,
"%s", gdbm_strerror(gdbm_errno));
756 fgdbm_store(dbm, ptr[0], ptr[1]);
798 fgdbm_length(
VALUE obj)
808 for (key = gdbm_firstkey(dbm); key.
dptr; key = nextkey) {
809 nextkey = gdbm_nextkey(dbm, key);
825 fgdbm_empty_p(
VALUE obj)
835 key = gdbm_firstkey(dbm);
855 fgdbm_each_value(
VALUE obj)
864 for (keystr = rb_gdbm_firstkey(dbm);
RTEST(keystr);
865 keystr = rb_gdbm_nextkey(dbm, keystr)) {
867 rb_yield(rb_gdbm_fetch2(dbm, keystr));
881 fgdbm_each_key(
VALUE obj)
890 for (keystr = rb_gdbm_firstkey(dbm);
RTEST(keystr);
891 keystr = rb_gdbm_nextkey(dbm, keystr)) {
907 fgdbm_each_pair(
VALUE obj)
916 for (keystr = rb_gdbm_firstkey(dbm);
RTEST(keystr);
917 keystr = rb_gdbm_nextkey(dbm, keystr)) {
933 fgdbm_keys(
VALUE obj)
941 for (keystr = rb_gdbm_firstkey(dbm);
RTEST(keystr);
942 keystr = rb_gdbm_nextkey(dbm, keystr)) {
957 fgdbm_values(
VALUE obj)
966 for (key = gdbm_firstkey(dbm); key.
dptr; key = nextkey) {
967 nextkey = gdbm_nextkey(dbm, key);
968 valstr = rb_gdbm_fetch(dbm, key);
998 key.
dsize = (int)len;
1001 if (gdbm_exists(dbm, key))
1019 VALUE keystr, valstr2;
1023 for (keystr = rb_gdbm_firstkey(dbm);
RTEST(keystr);
1024 keystr = rb_gdbm_nextkey(dbm, keystr)) {
1026 valstr2 = rb_gdbm_fetch2(dbm, keystr);
1028 if (!
NIL_P(valstr2) &&
1045 fgdbm_to_a(
VALUE obj)
1053 for (keystr = rb_gdbm_firstkey(dbm);
RTEST(keystr);
1054 keystr = rb_gdbm_nextkey(dbm, keystr)) {
1071 fgdbm_reorganize(
VALUE obj)
1076 rb_gdbm_modify(obj);
1078 gdbm_reorganize(dbm);
1094 fgdbm_sync(
VALUE obj)
1099 rb_gdbm_modify(obj);
1120 if (gdbm_setopt(dbm, GDBM_CACHESIZE, &optval,
sizeof(optval)) == -1) {
1121 rb_raise(rb_eGDBMError,
"%s", gdbm_strerror(gdbm_errno));
1148 if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval,
sizeof(optval)) == -1) {
1149 rb_raise(rb_eGDBMError,
"%s", gdbm_strerror(gdbm_errno));
1170 #if !defined(GDBM_SYNCMODE) 1183 if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval,
sizeof(optval)) == -1) {
1184 rb_raise(rb_eGDBMError,
"%s", gdbm_strerror(gdbm_errno));
1197 fgdbm_to_hash(
VALUE obj)
1205 for (keystr = rb_gdbm_firstkey(dbm);
RTEST(keystr);
1206 keystr = rb_gdbm_nextkey(dbm, keystr)) {
1208 rb_hash_aset(hash, keystr, rb_gdbm_fetch2(dbm, keystr));
1222 fgdbm_reject(
VALUE obj)
1299 #if defined(GDBM_SYNC) 1303 #if defined(GDBM_NOLOCK)
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *pstate)
Protects a function call from potential global escapes from the function.
void rb_warn(const char *fmt,...)
#define RUBY_TYPED_FREE_IMMEDIATELY
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
int rb_block_given_p(void)
Determines if the current method is given a block.
void rb_raise(VALUE exc, const char *fmt,...)
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
#define TypedData_Wrap_Struct(klass, data_type, sval)
#define TypedData_Get_Struct(obj, type, data_type, sval)
VALUE rb_ary_push(VALUE ary, VALUE item)
VALUE rb_ary_tmp_new(long capa)
VALUE rb_ary_clear(VALUE ary)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
void rb_include_module(VALUE klass, VALUE module)
VALUE rb_block_call(VALUE, ID, int, const VALUE *, rb_block_call_func_t, VALUE)
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
An equivalent to ensure clause.
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
RUBY_EXTERN VALUE rb_cObject
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
void rb_define_const(VALUE, const char *, VALUE)
void rb_error_frozen(const char *what)
void rb_sys_fail(const char *mesg)
#define RARRAY_CONST_PTR(a)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
VALUE rb_assoc_new(VALUE car, VALUE cdr)
int memcmp(const void *s1, const void *s2, size_t len)
#define GetDBM2(obj, dbmp, dbm)
#define GetDBM(obj, dbmp)
register unsigned int len
#define ExportStringValue(v)
#define RARRAY_AREF(a, i)
void rb_fd_fix_cloexec(int fd)
#define RETURN_ENUMERATOR(obj, argc, argv)
#define RSTRING_LENINT(str)
VALUE rb_hash_delete_if(VALUE hash)
void rb_sys_fail_str(VALUE mesg)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_str_new(const char *, long)