Ruby 1.9.2p136(2010-12-25revision30365)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# ext/dbm/dbm.c

Go to the documentation of this file.

    00001 /************************************************
    00002
    00003   dbm.c -
    00004
    00005   $Author: nobu $
    00006   created at: Mon Jan 24 15:59:52 JST 1994
    00007
    00008   Copyright (C) 1995-2001 Yukihiro Matsumoto
    00009
    00010 ************************************************/
    00011
    00012 #include "ruby.h"
    00013
    00014 #ifdef HAVE_CDEFS_H
    00015 # include <cdefs.h>
    00016 #endif
    00017 #ifdef HAVE_SYS_CDEFS_H
    00018 # include <sys/cdefs.h>
    00019 #endif
    00020 #include DBM_HDR
    00021 #include <fcntl.h>
    00022 #include <errno.h>
    00023
    00024 static VALUE rb_cDBM, rb_eDBMError;
    00025
    00026 #define RUBY_DBM_RW_BIT 0x20000000
    00027
    00028 struct dbmdata {
    00029     int  di_size;
    00030     DBM *di_dbm;
    00031 };
    00032
    00033 static void
    00034 closed_dbm(void)
    00035 {
    00036     rb_raise(rb_eDBMError, "closed DBM file");
    00037 }
    00038
    00039 #define GetDBM(obj, dbmp) {\
    00040     Data_Get_Struct(obj, struct dbmdata, dbmp);\
    00041     if (dbmp == 0) closed_dbm();\
    00042     if (dbmp->di_dbm == 0) closed_dbm();\
    00043 }
    00044
    00045 #define GetDBM2(obj, data, dbm) {\
    00046     GetDBM(obj, data);\
    00047     (dbm) = dbmp->di_dbm;\
    00048 }
    00049
    00050 static void
    00051 free_dbm(struct dbmdata *dbmp)
    00052 {
    00053     if (dbmp) {
    00054         if (dbmp->di_dbm) dbm_close(dbmp->di_dbm);
    00055         xfree(dbmp);
    00056     }
    00057 }
    00058
    00059 static VALUE
    00060 fdbm_close(VALUE obj)
    00061 {
    00062     struct dbmdata *dbmp;
    00063
    00064     GetDBM(obj, dbmp);
    00065     dbm_close(dbmp->di_dbm);
    00066     dbmp->di_dbm = 0;
    00067
    00068     return Qnil;
    00069 }
    00070
    00071 static VALUE
    00072 fdbm_closed(VALUE obj)
    00073 {
    00074     struct dbmdata *dbmp;
    00075
    00076     Data_Get_Struct(obj, struct dbmdata, dbmp);
    00077     if (dbmp == 0)
    00078         return Qtrue;
    00079     if (dbmp->di_dbm == 0)
    00080         return Qtrue;
    00081
    00082     return Qfalse;
    00083 }
    00084
    00085 static VALUE
    00086 fdbm_alloc(VALUE klass)
    00087 {
    00088     return Data_Wrap_Struct(klass, 0, free_dbm, 0);
    00089 }
    00090
    00091 static VALUE
    00092 fdbm_initialize(int argc, VALUE *argv, VALUE obj)
    00093 {
    00094     volatile VALUE file;
    00095     VALUE vmode, vflags;
    00096     DBM *dbm;
    00097     struct dbmdata *dbmp;
    00098     int mode, flags = 0;
    00099
    00100     if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
    00101         mode = 0666;            /* default value */
    00102     }
    00103     else if (NIL_P(vmode)) {
    00104         mode = -1;              /* return nil if DB not exist */
    00105     }
    00106     else {
    00107         mode = NUM2INT(vmode);
    00108     }
    00109
    00110     if (!NIL_P(vflags))
    00111         flags = NUM2INT(vflags);
    00112
    00113     FilePathValue(file);
    00114
    00115     if (flags & RUBY_DBM_RW_BIT) {
    00116         flags &= ~RUBY_DBM_RW_BIT;
    00117         dbm = dbm_open(RSTRING_PTR(file), flags, mode);
    00118     }
    00119     else {
    00120         dbm = 0;
    00121         if (mode >= 0) {
    00122             dbm = dbm_open(RSTRING_PTR(file), O_RDWR|O_CREAT, mode);
    00123         }
    00124         if (!dbm) {
    00125             dbm = dbm_open(RSTRING_PTR(file), O_RDWR, 0);
    00126         }
    00127         if (!dbm) {
    00128             dbm = dbm_open(RSTRING_PTR(file), O_RDONLY, 0);
    00129         }
    00130     }
    00131
    00132     if (!dbm) {
    00133         if (mode == -1) return Qnil;
    00134         rb_sys_fail(RSTRING_PTR(file));
    00135     }
    00136
    00137     dbmp = ALLOC(struct dbmdata);
    00138     DATA_PTR(obj) = dbmp;
    00139     dbmp->di_dbm = dbm;
    00140     dbmp->di_size = -1;
    00141
    00142     return obj;
    00143 }
    00144
    00145 static VALUE
    00146 fdbm_s_open(int argc, VALUE *argv, VALUE klass)
    00147 {
    00148     VALUE obj = Data_Wrap_Struct(klass, 0, free_dbm, 0);
    00149
    00150     if (NIL_P(fdbm_initialize(argc, argv, obj))) {
    00151         return Qnil;
    00152     }
    00153
    00154     if (rb_block_given_p()) {
    00155         return rb_ensure(rb_yield, obj, fdbm_close, obj);
    00156     }
    00157
    00158     return obj;
    00159 }
    00160
    00161 static VALUE
    00162 fdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone)
    00163 {
    00164     datum key, value;
    00165     struct dbmdata *dbmp;
    00166     DBM *dbm;
    00167
    00168     ExportStringValue(keystr);
    00169     key.dptr = RSTRING_PTR(keystr);
    00170     key.dsize = RSTRING_LEN(keystr);
    00171
    00172     GetDBM2(obj, dbmp, dbm);
    00173     value = dbm_fetch(dbm, key);
    00174     if (value.dptr == 0) {
    00175         if (ifnone == Qnil && rb_block_given_p())
    00176             return rb_yield(rb_tainted_str_new(key.dptr, key.dsize));
    00177         return ifnone;
    00178     }
    00179     return rb_tainted_str_new(value.dptr, value.dsize);
    00180 }
    00181
    00182 static VALUE
    00183 fdbm_aref(VALUE obj, VALUE keystr)
    00184 {
    00185     return fdbm_fetch(obj, keystr, Qnil);
    00186 }
    00187
    00188 static VALUE
    00189 fdbm_fetch_m(int argc, VALUE *argv, VALUE obj)
    00190 {
    00191     VALUE keystr, valstr, ifnone;
    00192
    00193     rb_scan_args(argc, argv, "11", &keystr, &ifnone);
    00194     valstr = fdbm_fetch(obj, keystr, ifnone);
    00195     if (argc == 1 && !rb_block_given_p() && NIL_P(valstr))
    00196         rb_raise(rb_eIndexError, "key not found");
    00197
    00198     return valstr;
    00199 }
    00200
    00201 static VALUE
    00202 fdbm_key(VALUE obj, VALUE valstr)
    00203 {
    00204     datum key, val;
    00205     struct dbmdata *dbmp;
    00206     DBM *dbm;
    00207
    00208     ExportStringValue(valstr);
    00209     val.dptr = RSTRING_PTR(valstr);
    00210     val.dsize = RSTRING_LEN(valstr);
    00211
    00212     GetDBM2(obj, dbmp, dbm);
    00213     for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00214         val = dbm_fetch(dbm, key);
    00215         if (val.dsize == RSTRING_LEN(valstr) &&
    00216             memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0) {
    00217             return rb_tainted_str_new(key.dptr, key.dsize);
    00218         }
    00219     }
    00220     return Qnil;
    00221 }
    00222
    00223 static VALUE
    00224 fdbm_index(VALUE hash, VALUE value)
    00225 {
    00226     rb_warn("DBM#index is deprecated; use DBM#key");
    00227     return fdbm_key(hash, value);
    00228 }
    00229
    00230 static VALUE
    00231 fdbm_select(VALUE obj)
    00232 {
    00233     VALUE new = rb_ary_new();
    00234     datum key, val;
    00235     DBM *dbm;
    00236     struct dbmdata *dbmp;
    00237
    00238     GetDBM2(obj, dbmp, dbm);
    00239     for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00240         VALUE assoc, v;
    00241         val = dbm_fetch(dbm, key);
    00242         assoc = rb_assoc_new(rb_tainted_str_new(key.dptr, key.dsize),
    00243                              rb_tainted_str_new(val.dptr, val.dsize));
    00244         v = rb_yield(assoc);
    00245         if (RTEST(v)) {
    00246             rb_ary_push(new, assoc);
    00247         }
    00248         GetDBM2(obj, dbmp, dbm);
    00249     }
    00250
    00251     return new;
    00252 }
    00253
    00254 static VALUE
    00255 fdbm_values_at(int argc, VALUE *argv, VALUE obj)
    00256 {
    00257     VALUE new = rb_ary_new2(argc);
    00258     int i;
    00259
    00260     for (i=0; i<argc; i++) {
    00261         rb_ary_push(new, fdbm_fetch(obj, argv[i], Qnil));
    00262     }
    00263
    00264     return new;
    00265 }
    00266
    00267 static void
    00268 fdbm_modify(VALUE obj)
    00269 {
    00270     rb_secure(4);
    00271     if (OBJ_FROZEN(obj)) rb_error_frozen("DBM");
    00272 }
    00273
    00274 static VALUE
    00275 fdbm_delete(VALUE obj, VALUE keystr)
    00276 {
    00277     datum key, value;
    00278     struct dbmdata *dbmp;
    00279     DBM *dbm;
    00280     VALUE valstr;
    00281
    00282     fdbm_modify(obj);
    00283     ExportStringValue(keystr);
    00284     key.dptr = RSTRING_PTR(keystr);
    00285     key.dsize = RSTRING_LEN(keystr);
    00286
    00287     GetDBM2(obj, dbmp, dbm);
    00288
    00289     value = dbm_fetch(dbm, key);
    00290     if (value.dptr == 0) {
    00291         if (rb_block_given_p()) return rb_yield(keystr);
    00292         return Qnil;
    00293     }
    00294
    00295     /* need to save value before dbm_delete() */
    00296     valstr = rb_tainted_str_new(value.dptr, value.dsize);
    00297
    00298     if (dbm_delete(dbm, key)) {
    00299         dbmp->di_size = -1;
    00300         rb_raise(rb_eDBMError, "dbm_delete failed");
    00301     }
    00302     else if (dbmp->di_size >= 0) {
    00303         dbmp->di_size--;
    00304     }
    00305     return valstr;
    00306 }
    00307
    00308 static VALUE
    00309 fdbm_shift(VALUE obj)
    00310 {
    00311     datum key, val;
    00312     struct dbmdata *dbmp;
    00313     DBM *dbm;
    00314     VALUE keystr, valstr;
    00315
    00316     fdbm_modify(obj);
    00317     GetDBM2(obj, dbmp, dbm);
    00318     dbmp->di_size = -1;
    00319
    00320     key = dbm_firstkey(dbm);
    00321     if (!key.dptr) return Qnil;
    00322     val = dbm_fetch(dbm, key);
    00323     keystr = rb_tainted_str_new(key.dptr, key.dsize);
    00324     valstr = rb_tainted_str_new(val.dptr, val.dsize);
    00325     dbm_delete(dbm, key);
    00326
    00327     return rb_assoc_new(keystr, valstr);
    00328 }
    00329
    00330 static VALUE
    00331 fdbm_delete_if(VALUE obj)
    00332 {
    00333     datum key, val;
    00334     struct dbmdata *dbmp;
    00335     DBM *dbm;
    00336     VALUE keystr, valstr;
    00337     VALUE ret, ary = rb_ary_new();
    00338     int i, status = 0, n;
    00339
    00340     fdbm_modify(obj);
    00341     GetDBM2(obj, dbmp, dbm);
    00342     n = dbmp->di_size;
    00343     dbmp->di_size = -1;
    00344
    00345     for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00346         val = dbm_fetch(dbm, key);
    00347         keystr = rb_tainted_str_new(key.dptr, key.dsize);
    00348         valstr = rb_tainted_str_new(val.dptr, val.dsize);
    00349         ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status);
    00350         if (status != 0) break;
    00351         if (RTEST(ret)) rb_ary_push(ary, keystr);
    00352         GetDBM2(obj, dbmp, dbm);
    00353     }
    00354
    00355     for (i = 0; i < RARRAY_LEN(ary); i++) {
    00356         keystr = RARRAY_PTR(ary)[i];
    00357         ExportStringValue(keystr);
    00358         key.dptr = RSTRING_PTR(keystr);
    00359         key.dsize = RSTRING_LEN(keystr);
    00360         if (dbm_delete(dbm, key)) {
    00361             rb_raise(rb_eDBMError, "dbm_delete failed");
    00362         }
    00363     }
    00364     if (status) rb_jump_tag(status);
    00365     if (n > 0) dbmp->di_size = n - RARRAY_LEN(ary);
    00366
    00367     return obj;
    00368 }
    00369
    00370 static VALUE
    00371 fdbm_clear(VALUE obj)
    00372 {
    00373     datum key;
    00374     struct dbmdata *dbmp;
    00375     DBM *dbm;
    00376
    00377     fdbm_modify(obj);
    00378     GetDBM2(obj, dbmp, dbm);
    00379     dbmp->di_size = -1;
    00380     while (key = dbm_firstkey(dbm), key.dptr) {
    00381         if (dbm_delete(dbm, key)) {
    00382             rb_raise(rb_eDBMError, "dbm_delete failed");
    00383         }
    00384     }
    00385     dbmp->di_size = 0;
    00386
    00387     return obj;
    00388 }
    00389
    00390 static VALUE
    00391 fdbm_invert(VALUE obj)
    00392 {
    00393     datum key, val;
    00394     struct dbmdata *dbmp;
    00395     DBM *dbm;
    00396     VALUE keystr, valstr;
    00397     VALUE hash = rb_hash_new();
    00398
    00399     GetDBM2(obj, dbmp, dbm);
    00400     for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00401         val = dbm_fetch(dbm, key);
    00402         keystr = rb_tainted_str_new(key.dptr, key.dsize);
    00403         valstr = rb_tainted_str_new(val.dptr, val.dsize);
    00404         rb_hash_aset(hash, valstr, keystr);
    00405     }
    00406     return hash;
    00407 }
    00408
    00409 static VALUE fdbm_store(VALUE,VALUE,VALUE);
    00410
    00411 static VALUE
    00412 update_i(VALUE pair, VALUE dbm)
    00413 {
    00414     Check_Type(pair, T_ARRAY);
    00415     if (RARRAY_LEN(pair) < 2) {
    00416         rb_raise(rb_eArgError, "pair must be [key, value]");
    00417     }
    00418     fdbm_store(dbm, RARRAY_PTR(pair)[0], RARRAY_PTR(pair)[1]);
    00419     return Qnil;
    00420 }
    00421
    00422 static VALUE
    00423 fdbm_update(VALUE obj, VALUE other)
    00424 {
    00425     rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
    00426     return obj;
    00427 }
    00428
    00429 static VALUE
    00430 fdbm_replace(VALUE obj, VALUE other)
    00431 {
    00432     fdbm_clear(obj);
    00433     rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
    00434     return obj;
    00435 }
    00436
    00437 static VALUE
    00438 fdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
    00439 {
    00440     datum key, val;
    00441     struct dbmdata *dbmp;
    00442     DBM *dbm;
    00443
    00444     fdbm_modify(obj);
    00445     keystr = rb_obj_as_string(keystr);
    00446     valstr = rb_obj_as_string(valstr);
    00447
    00448     key.dptr = RSTRING_PTR(keystr);
    00449     key.dsize = RSTRING_LEN(keystr);
    00450
    00451     val.dptr = RSTRING_PTR(valstr);
    00452     val.dsize = RSTRING_LEN(valstr);
    00453
    00454     GetDBM2(obj, dbmp, dbm);
    00455     dbmp->di_size = -1;
    00456     if (dbm_store(dbm, key, val, DBM_REPLACE)) {
    00457 #ifdef HAVE_DBM_CLEARERR
    00458         dbm_clearerr(dbm);
    00459 #endif
    00460         if (errno == EPERM) rb_sys_fail(0);
    00461         rb_raise(rb_eDBMError, "dbm_store failed");
    00462     }
    00463
    00464     return valstr;
    00465 }
    00466
    00467 static VALUE
    00468 fdbm_length(VALUE obj)
    00469 {
    00470     datum key;
    00471     struct dbmdata *dbmp;
    00472     DBM *dbm;
    00473     int i = 0;
    00474
    00475     GetDBM2(obj, dbmp, dbm);
    00476     if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
    00477
    00478     for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00479         i++;
    00480     }
    00481     dbmp->di_size = i;
    00482
    00483     return INT2FIX(i);
    00484 }
    00485
    00486 static VALUE
    00487 fdbm_empty_p(VALUE obj)
    00488 {
    00489     datum key;
    00490     struct dbmdata *dbmp;
    00491     DBM *dbm;
    00492     int i = 0;
    00493
    00494     GetDBM2(obj, dbmp, dbm);
    00495     if (dbmp->di_size < 0) {
    00496         dbm = dbmp->di_dbm;
    00497
    00498         for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00499             i++;
    00500         }
    00501     }
    00502     else {
    00503         i = dbmp->di_size;
    00504     }
    00505     if (i == 0) return Qtrue;
    00506     return Qfalse;
    00507 }
    00508
    00509 static VALUE
    00510 fdbm_each_value(VALUE obj)
    00511 {
    00512     datum key, val;
    00513     struct dbmdata *dbmp;
    00514     DBM *dbm;
    00515
    00516     RETURN_ENUMERATOR(obj, 0, 0);
    00517
    00518     GetDBM2(obj, dbmp, dbm);
    00519     for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00520         val = dbm_fetch(dbm, key);
    00521         rb_yield(rb_tainted_str_new(val.dptr, val.dsize));
    00522         GetDBM2(obj, dbmp, dbm);
    00523     }
    00524     return obj;
    00525 }
    00526
    00527 static VALUE
    00528 fdbm_each_key(VALUE obj)
    00529 {
    00530     datum key;
    00531     struct dbmdata *dbmp;
    00532     DBM *dbm;
    00533
    00534     RETURN_ENUMERATOR(obj, 0, 0);
    00535
    00536     GetDBM2(obj, dbmp, dbm);
    00537     for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00538         rb_yield(rb_tainted_str_new(key.dptr, key.dsize));
    00539         GetDBM2(obj, dbmp, dbm);
    00540     }
    00541     return obj;
    00542 }
    00543
    00544 static VALUE
    00545 fdbm_each_pair(VALUE obj)
    00546 {
    00547     datum key, val;
    00548     DBM *dbm;
    00549     struct dbmdata *dbmp;
    00550     VALUE keystr, valstr;
    00551
    00552     RETURN_ENUMERATOR(obj, 0, 0);
    00553
    00554     GetDBM2(obj, dbmp, dbm);
    00555
    00556     for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00557         val = dbm_fetch(dbm, key);
    00558         keystr = rb_tainted_str_new(key.dptr, key.dsize);
    00559         valstr = rb_tainted_str_new(val.dptr, val.dsize);
    00560         rb_yield(rb_assoc_new(keystr, valstr));
    00561         GetDBM2(obj, dbmp, dbm);
    00562     }
    00563
    00564     return obj;
    00565 }
    00566
    00567 static VALUE
    00568 fdbm_keys(VALUE obj)
    00569 {
    00570     datum key;
    00571     struct dbmdata *dbmp;
    00572     DBM *dbm;
    00573     VALUE ary;
    00574
    00575     GetDBM2(obj, dbmp, dbm);
    00576
    00577     ary = rb_ary_new();
    00578     for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00579         rb_ary_push(ary, rb_tainted_str_new(key.dptr, key.dsize));
    00580     }
    00581
    00582     return ary;
    00583 }
    00584
    00585 static VALUE
    00586 fdbm_values(VALUE obj)
    00587 {
    00588     datum key, val;
    00589     struct dbmdata *dbmp;
    00590     DBM *dbm;
    00591     VALUE ary;
    00592
    00593     GetDBM2(obj, dbmp, dbm);
    00594     ary = rb_ary_new();
    00595     for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00596         val = dbm_fetch(dbm, key);
    00597         rb_ary_push(ary, rb_tainted_str_new(val.dptr, val.dsize));
    00598     }
    00599
    00600     return ary;
    00601 }
    00602
    00603 static VALUE
    00604 fdbm_has_key(VALUE obj, VALUE keystr)
    00605 {
    00606     datum key, val;
    00607     struct dbmdata *dbmp;
    00608     DBM *dbm;
    00609
    00610     ExportStringValue(keystr);
    00611     key.dptr = RSTRING_PTR(keystr);
    00612     key.dsize = RSTRING_LEN(keystr);
    00613
    00614     GetDBM2(obj, dbmp, dbm);
    00615     val = dbm_fetch(dbm, key);
    00616     if (val.dptr) return Qtrue;
    00617     return Qfalse;
    00618 }
    00619
    00620 static VALUE
    00621 fdbm_has_value(VALUE obj, VALUE valstr)
    00622 {
    00623     datum key, val;
    00624     struct dbmdata *dbmp;
    00625     DBM *dbm;
    00626
    00627     ExportStringValue(valstr);
    00628     val.dptr = RSTRING_PTR(valstr);
    00629     val.dsize = RSTRING_LEN(valstr);
    00630
    00631     GetDBM2(obj, dbmp, dbm);
    00632     for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00633         val = dbm_fetch(dbm, key);
    00634         if (val.dsize == RSTRING_LEN(valstr) &&
    00635             memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0)
    00636             return Qtrue;
    00637     }
    00638     return Qfalse;
    00639 }
    00640
    00641 static VALUE
    00642 fdbm_to_a(VALUE obj)
    00643 {
    00644     datum key, val;
    00645     struct dbmdata *dbmp;
    00646     DBM *dbm;
    00647     VALUE ary;
    00648
    00649     GetDBM2(obj, dbmp, dbm);
    00650     ary = rb_ary_new();
    00651     for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00652         val = dbm_fetch(dbm, key);
    00653         rb_ary_push(ary, rb_assoc_new(rb_tainted_str_new(key.dptr, key.dsize),
    00654                                       rb_tainted_str_new(val.dptr, val.dsize)));
    00655     }
    00656
    00657     return ary;
    00658 }
    00659
    00660 static VALUE
    00661 fdbm_to_hash(VALUE obj)
    00662 {
    00663     datum key, val;
    00664     struct dbmdata *dbmp;
    00665     DBM *dbm;
    00666     VALUE hash;
    00667
    00668     GetDBM2(obj, dbmp, dbm);
    00669     hash = rb_hash_new();
    00670     for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
    00671         val = dbm_fetch(dbm, key);
    00672         rb_hash_aset(hash, rb_tainted_str_new(key.dptr, key.dsize),
    00673                            rb_tainted_str_new(val.dptr, val.dsize));
    00674     }
    00675
    00676     return hash;
    00677 }
    00678
    00679 static VALUE
    00680 fdbm_reject(VALUE obj)
    00681 {
    00682     return rb_hash_delete_if(fdbm_to_hash(obj));
    00683 }
    00684
    00685 void
    00686 Init_dbm(void)
    00687 {
    00688     rb_cDBM = rb_define_class("DBM", rb_cObject);
    00689     rb_eDBMError = rb_define_class("DBMError", rb_eStandardError);
    00690     rb_include_module(rb_cDBM, rb_mEnumerable);
    00691
    00692     rb_define_alloc_func(rb_cDBM, fdbm_alloc);
    00693     rb_define_singleton_method(rb_cDBM, "open", fdbm_s_open, -1);
    00694
    00695     rb_define_method(rb_cDBM, "initialize", fdbm_initialize, -1);
    00696     rb_define_method(rb_cDBM, "close", fdbm_close, 0);
    00697     rb_define_method(rb_cDBM, "closed?", fdbm_closed, 0);
    00698     rb_define_method(rb_cDBM, "[]", fdbm_aref, 1);
    00699     rb_define_method(rb_cDBM, "fetch", fdbm_fetch_m, -1);
    00700     rb_define_method(rb_cDBM, "[]=", fdbm_store, 2);
    00701     rb_define_method(rb_cDBM, "store", fdbm_store, 2);
    00702     rb_define_method(rb_cDBM, "index",  fdbm_index, 1);
    00703     rb_define_method(rb_cDBM, "key",  fdbm_key, 1);
    00704     rb_define_method(rb_cDBM, "select",  fdbm_select, 0);
    00705     rb_define_method(rb_cDBM, "values_at", fdbm_values_at, -1);
    00706     rb_define_method(rb_cDBM, "length", fdbm_length, 0);
    00707     rb_define_method(rb_cDBM, "size", fdbm_length, 0);
    00708     rb_define_method(rb_cDBM, "empty?", fdbm_empty_p, 0);
    00709     rb_define_method(rb_cDBM, "each", fdbm_each_pair, 0);
    00710     rb_define_method(rb_cDBM, "each_value", fdbm_each_value, 0);
    00711     rb_define_method(rb_cDBM, "each_key", fdbm_each_key, 0);
    00712     rb_define_method(rb_cDBM, "each_pair", fdbm_each_pair, 0);
    00713     rb_define_method(rb_cDBM, "keys", fdbm_keys, 0);
    00714     rb_define_method(rb_cDBM, "values", fdbm_values, 0);
    00715     rb_define_method(rb_cDBM, "shift", fdbm_shift, 0);
    00716     rb_define_method(rb_cDBM, "delete", fdbm_delete, 1);
    00717     rb_define_method(rb_cDBM, "delete_if", fdbm_delete_if, 0);
    00718     rb_define_method(rb_cDBM, "reject!", fdbm_delete_if, 0);
    00719     rb_define_method(rb_cDBM, "reject", fdbm_reject, 0);
    00720     rb_define_method(rb_cDBM, "clear", fdbm_clear, 0);
    00721     rb_define_method(rb_cDBM,"invert", fdbm_invert, 0);
    00722     rb_define_method(rb_cDBM,"update", fdbm_update, 1);
    00723     rb_define_method(rb_cDBM,"replace", fdbm_replace, 1);
    00724
    00725     rb_define_method(rb_cDBM, "include?", fdbm_has_key, 1);
    00726     rb_define_method(rb_cDBM, "has_key?", fdbm_has_key, 1);
    00727     rb_define_method(rb_cDBM, "member?", fdbm_has_key, 1);
    00728     rb_define_method(rb_cDBM, "has_value?", fdbm_has_value, 1);
    00729     rb_define_method(rb_cDBM, "key?", fdbm_has_key, 1);
    00730     rb_define_method(rb_cDBM, "value?", fdbm_has_value, 1);
    00731
    00732     rb_define_method(rb_cDBM, "to_a", fdbm_to_a, 0);
    00733     rb_define_method(rb_cDBM, "to_hash", fdbm_to_hash, 0);
    00734
    00735     /* flags for dbm_open() */
    00736     rb_define_const(rb_cDBM, "READER",  INT2FIX(O_RDONLY|RUBY_DBM_RW_BIT));
    00737     rb_define_const(rb_cDBM, "WRITER",  INT2FIX(O_RDWR|RUBY_DBM_RW_BIT));
    00738     rb_define_const(rb_cDBM, "WRCREAT", INT2FIX(O_RDWR|O_CREAT|RUBY_DBM_RW_BIT));
    00739     rb_define_const(rb_cDBM, "NEWDB",   INT2FIX(O_RDWR|O_CREAT|O_TRUNC|RUBY_DBM_RW_BIT));
    00740
    00741 #ifdef DB_VERSION_STRING
    00742     rb_define_const(rb_cDBM, "VERSION",  rb_str_new2(DB_VERSION_STRING));
    00743 #else
    00744     rb_define_const(rb_cDBM, "VERSION",  rb_str_new2("unknown"));
    00745 #endif
    00746 }
    00747