Ruby 2.1.5p273(2014-11-13revision48405)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

gdbm.c
Go to the documentation of this file.
1 /************************************************
2
3 gdbm.c -
4
5 $Author: nobu $
6 modified at: Mon Jan 24 15:59:52 JST 1994
7
8 Documentation by Peter Adolphs < futzilogik at users dot sourceforge dot net >
9
10 ************************************************/
11
12 #include "ruby.h"
13
14 #include <gdbm.h>
15 #include <fcntl.h>
16 #include <errno.h>
17
18 /*
19 * Document-class: GDBM
20 *
21 * == Summary
22 *
23 * Ruby extension for GNU dbm (gdbm) -- a simple database engine for storing
24 * key-value pairs on disk.
25 *
26 * == Description
27 *
28 * GNU dbm is a library for simple databases. A database is a file that stores
29 * key-value pairs. Gdbm allows the user to store, retrieve, and delete data by
30 * key. It furthermore allows a non-sorted traversal of all key-value pairs.
31 * A gdbm database thus provides the same functionality as a hash. As
32 * with objects of the Hash class, elements can be accessed with <tt>[]</tt>.
33 * Furthermore, GDBM mixes in the Enumerable module, thus providing convenient
34 * methods such as #find, #collect, #map, etc.
35 *
36 * A process is allowed to open several different databases at the same time.
37 * A process can open a database as a "reader" or a "writer". Whereas a reader
38 * has only read-access to the database, a writer has read- and write-access.
39 * A database can be accessed either by any number of readers or by exactly one
40 * writer at the same time.
41 *
42 * == Examples
43 *
44 * 1. Opening/creating a database, and filling it with some entries:
45 *
46 * require 'gdbm'
47 *
48 * gdbm = GDBM.new("fruitstore.db")
49 * gdbm["ananas"] = "3"
50 * gdbm["banana"] = "8"
51 * gdbm["cranberry"] = "4909"
52 * gdbm.close
53 *
54 * 2. Reading out a database:
55 *
56 * require 'gdbm'
57 *
58 * gdbm = GDBM.new("fruitstore.db")
59 * gdbm.each_pair do |key, value|
60 * print "#{key}: #{value}\n"
61 * end
62 * gdbm.close
63 *
64 * produces
65 *
66 * banana: 8
67 * ananas: 3
68 * cranberry: 4909
69 *
70 * == Links
71 *
72 * * http://www.gnu.org/software/gdbm/
73 */
74 static VALUE rb_cGDBM, rb_eGDBMError, rb_eGDBMFatalError;
75
76 #if SIZEOF_LONG > SIZEOF_INT
77 #define TOO_LONG(n) ((long)(+(int)(n)) != (long)(n))
78 #else
79 #define TOO_LONG(n) 0
80 #endif
81
82 #define RUBY_GDBM_RW_BIT 0x20000000
83
84 #define MY_BLOCK_SIZE (2048)
85 #define MY_FATAL_FUNC rb_gdbm_fatal
86 static void
87 rb_gdbm_fatal(const char *msg)
88 {
89 rb_raise(rb_eGDBMFatalError, "%s", msg);
90 }
91
92 struct dbmdata {
93 int di_size;
94 GDBM_FILE di_dbm;
95 };
96
97 static void
98 closed_dbm(void)
99 {
100 rb_raise(rb_eRuntimeError, "closed GDBM file");
101 }
102
103 #define GetDBM(obj, dbmp) do {\
104 Data_Get_Struct((obj), struct dbmdata, (dbmp));\
105 if ((dbmp) == 0) closed_dbm();\
106 if ((dbmp)->di_dbm == 0) closed_dbm();\
107 } while (0)
108
109 #define GetDBM2(obj, data, dbm) {\
110 GetDBM((obj), (data));\
111 (dbm) = dbmp->di_dbm;\
112 }
113
114 static void
115 free_dbm(struct dbmdata *dbmp)
116 {
117 if (dbmp) {
118 if (dbmp->di_dbm) gdbm_close(dbmp->di_dbm);
119 xfree(dbmp);
120 }
121 }
122
123 /*
124 * call-seq:
125 * gdbm.close -> nil
126 *
127 * Closes the associated database file.
128 */
129 static VALUE
130 fgdbm_close(VALUE obj)
131 {
132 struct dbmdata *dbmp;
133
134 GetDBM(obj, dbmp);
135 gdbm_close(dbmp->di_dbm);
136 dbmp->di_dbm = 0;
137
138 return Qnil;
139 }
140
141 /*
142 * call-seq:
143 * gdbm.closed? -> true or false
144 *
145 * Returns true if the associated database file has been closed.
146 */
147 static VALUE
148 fgdbm_closed(VALUE obj)
149 {
150 struct dbmdata *dbmp;
151
152 Data_Get_Struct(obj, struct dbmdata, dbmp);
153 if (dbmp == 0)
154 return Qtrue;
155 if (dbmp->di_dbm == 0)
156 return Qtrue;
157
158 return Qfalse;
159 }
160
161 static VALUE
162 fgdbm_s_alloc(VALUE klass)
163 {
164 return Data_Wrap_Struct(klass, 0, free_dbm, 0);
165 }
166
167 /*
168 * call-seq:
169 * GDBM.new(filename, mode = 0666, flags = nil)
170 *
171 * Creates a new GDBM instance by opening a gdbm file named _filename_.
172 * If the file does not exist, a new file with file mode _mode_ will be
173 * created. _flags_ may be one of the following:
174 * * *READER* - open as a reader
175 * * *WRITER* - open as a writer
176 * * *WRCREAT* - open as a writer; if the database does not exist, create a new one
177 * * *NEWDB* - open as a writer; overwrite any existing databases
178 *
179 * The values *WRITER*, *WRCREAT* and *NEWDB* may be combined with the following
180 * values by bitwise or:
181 * * *SYNC* - cause all database operations to be synchronized to the disk
182 * * *NOLOCK* - do not lock the database file
183 *
184 * If no _flags_ are specified, the GDBM object will try to open the database
185 * file as a writer and will create it if it does not already exist
186 * (cf. flag <tt>WRCREAT</tt>). If this fails (for instance, if another process
187 * has already opened the database as a reader), it will try to open the
188 * database file as a reader (cf. flag <tt>READER</tt>).
189 */
190 static VALUE
191 fgdbm_initialize(int argc, VALUE *argv, VALUE obj)
192 {
193 VALUE file, vmode, vflags;
194 GDBM_FILE dbm;
195 struct dbmdata *dbmp;
196 int mode, flags = 0;
197
198 if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
199 mode = 0666; /* default value */
200 }
201 else if (NIL_P(vmode)) {
202 mode = -1; /* return nil if DB does not exist */
203 }
204 else {
205 mode = NUM2INT(vmode);
206 }
207
208 if (!NIL_P(vflags))
209 flags = NUM2INT(vflags);
210
211 SafeStringValue(file);
212
213 #ifdef GDBM_CLOEXEC
214 /* GDBM_CLOEXEC is available since gdbm 1.10. */
215 flags |= GDBM_CLOEXEC;
216 #endif
217
218 if (flags & RUBY_GDBM_RW_BIT) {
219 flags &= ~RUBY_GDBM_RW_BIT;
220 dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
221 flags, mode, MY_FATAL_FUNC);
222 }
223 else {
224 dbm = 0;
225 if (mode >= 0)
226 dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
227 GDBM_WRCREAT|flags, mode, MY_FATAL_FUNC);
228 if (!dbm)
229 dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
230 GDBM_WRITER|flags, 0, MY_FATAL_FUNC);
231 if (!dbm)
232 dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
233 GDBM_READER|flags, 0, MY_FATAL_FUNC);
234 }
235
236 if (dbm) {
237 rb_fd_fix_cloexec(gdbm_fdesc(dbm));
238 }
239
240 if (!dbm) {
241 if (mode == -1) return Qnil;
242
243 if (gdbm_errno == GDBM_FILE_OPEN_ERROR ||
244 gdbm_errno == GDBM_CANT_BE_READER ||
245 gdbm_errno == GDBM_CANT_BE_WRITER)
246 rb_sys_fail_str(file);
247 else
248 rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
249 }
250
251 dbmp = ALLOC(struct dbmdata);
252 free_dbm(DATA_PTR(obj));
253 DATA_PTR(obj) = dbmp;
254 dbmp->di_dbm = dbm;
255 dbmp->di_size = -1;
256
257 return obj;
258 }
259
260 /*
261 * call-seq:
262 * GDBM.open(filename, mode = 0666, flags = nil)
263 * GDBM.open(filename, mode = 0666, flags = nil) { |gdbm| ... }
264 *
265 * If called without a block, this is synonymous to GDBM::new.
266 * If a block is given, the new GDBM instance will be passed to the block
267 * as a parameter, and the corresponding database file will be closed
268 * after the execution of the block code has been finished.
269 *
270 * Example for an open call with a block:
271 *
272 * require 'gdbm'
273 * GDBM.open("fruitstore.db") do |gdbm|
274 * gdbm.each_pair do |key, value|
275 * print "#{key}: #{value}\n"
276 * end
277 * end
278 */
279 static VALUE
280 fgdbm_s_open(int argc, VALUE *argv, VALUE klass)
281 {
282 VALUE obj = Data_Wrap_Struct(klass, 0, free_dbm, 0);
283
284 if (NIL_P(fgdbm_initialize(argc, argv, obj))) {
285 return Qnil;
286 }
287
288 if (rb_block_given_p()) {
289 return rb_ensure(rb_yield, obj, fgdbm_close, obj);
290 }
291
292 return obj;
293 }
294
295 static VALUE
296 rb_gdbm_fetch(GDBM_FILE dbm, datum key)
297 {
298 datum val;
299 VALUE str;
300
301 val = gdbm_fetch(dbm, key);
302 if (val.dptr == 0)
303 return Qnil;
304
305 str = rb_str_new(val.dptr, val.dsize);
306 free(val.dptr);
307 OBJ_TAINT(str);
308 return str;
309 }
310
311 static VALUE
312 rb_gdbm_fetch2(GDBM_FILE dbm, VALUE keystr)
313 {
314 datum key;
315 long len;
316
317 StringValue(keystr);
318 len = RSTRING_LEN(keystr);
319 if (TOO_LONG(len)) return Qnil;
320 key.dptr = RSTRING_PTR(keystr);
321 key.dsize = (int)len;
322
323 return rb_gdbm_fetch(dbm, key);
324 }
325
326 static VALUE
327 rb_gdbm_fetch3(VALUE obj, VALUE keystr)
328 {
329 struct dbmdata *dbmp;
330 GDBM_FILE dbm;
331
332 GetDBM2(obj, dbmp, dbm);
333 return rb_gdbm_fetch2(dbm, keystr);
334 }
335
336 static VALUE
337 rb_gdbm_firstkey(GDBM_FILE dbm)
338 {
339 datum key;
340 VALUE str;
341
342 key = gdbm_firstkey(dbm);
343 if (key.dptr == 0)
344 return Qnil;
345
346 str = rb_str_new(key.dptr, key.dsize);
347 free(key.dptr);
348 OBJ_TAINT(str);
349 return str;
350 }
351
352 static VALUE
353 rb_gdbm_nextkey(GDBM_FILE dbm, VALUE keystr)
354 {
355 datum key, key2;
356 VALUE str;
357 long len;
358
359 len = RSTRING_LEN(keystr);
360 if (TOO_LONG(len)) return Qnil;
361 key.dptr = RSTRING_PTR(keystr);
362 key.dsize = (int)len;
363 key2 = gdbm_nextkey(dbm, key);
364 if (key2.dptr == 0)
365 return Qnil;
366
367 str = rb_str_new(key2.dptr, key2.dsize);
368 free(key2.dptr);
369 OBJ_TAINT(str);
370 return str;
371 }
372
373 static VALUE
374 fgdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone)
375 {
376 VALUE valstr;
377
378 valstr = rb_gdbm_fetch3(obj, keystr);
379 if (NIL_P(valstr)) {
380 if (ifnone == Qnil && rb_block_given_p())
381 return rb_yield(keystr);
382 return ifnone;
383 }
384 return valstr;
385 }
386
387 /*
388 * call-seq:
389 * gdbm[key] -> value
390 *
391 * Retrieves the _value_ corresponding to _key_.
392 */
393 static VALUE
394 fgdbm_aref(VALUE obj, VALUE keystr)
395 {
396 return rb_gdbm_fetch3(obj, keystr);
397 }
398
399 /*
400 * call-seq:
401 * gdbm.fetch(key [, default]) -> value
402 *
403 * Retrieves the _value_ corresponding to _key_. If there is no value
404 * associated with _key_, _default_ will be returned instead.
405 */
406 static VALUE
407 fgdbm_fetch_m(int argc, VALUE *argv, VALUE obj)
408 {
409 VALUE keystr, valstr, ifnone;
410
411 rb_scan_args(argc, argv, "11", &keystr, &ifnone);
412 valstr = fgdbm_fetch(obj, keystr, ifnone);
413 if (argc == 1 && !rb_block_given_p() && NIL_P(valstr))
414 rb_raise(rb_eIndexError, "key not found");
415
416 return valstr;
417 }
418
419 /*
420 * call-seq:
421 * gdbm.key(value) -> key
422 *
423 * Returns the _key_ for a given _value_. If several keys may map to the
424 * same value, the key that is found first will be returned.
425 */
426 static VALUE
427 fgdbm_key(VALUE obj, VALUE valstr)
428 {
429 struct dbmdata *dbmp;
430 GDBM_FILE dbm;
431 VALUE keystr, valstr2;
432
433 StringValue(valstr);
434 GetDBM2(obj, dbmp, dbm);
435 for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
436 keystr = rb_gdbm_nextkey(dbm, keystr)) {
437
438 valstr2 = rb_gdbm_fetch2(dbm, keystr);
439 if (!NIL_P(valstr2) &&
440 (int)RSTRING_LEN(valstr) == (int)RSTRING_LEN(valstr2) &&
441 memcmp(RSTRING_PTR(valstr), RSTRING_PTR(valstr2),
442 (int)RSTRING_LEN(valstr)) == 0) {
443 return keystr;
444 }
445 }
446 return Qnil;
447 }
448
449 /* :nodoc: */
450 static VALUE
451 fgdbm_index(VALUE obj, VALUE value)
452 {
453 rb_warn("GDBM#index is deprecated; use GDBM#key");
454 return fgdbm_key(obj, value);
455 }
456
457 /*
458 * call-seq:
459 * gdbm.select { |key, value| block } -> array
460 *
461 * Returns a new array of all key-value pairs of the database for which _block_
462 * evaluates to true.
463 */
464 static VALUE
465 fgdbm_select(VALUE obj)
466 {
467 VALUE new = rb_ary_new();
468 GDBM_FILE dbm;
469 struct dbmdata *dbmp;
470 VALUE keystr;
471
472 GetDBM2(obj, dbmp, dbm);
473 for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
474 keystr = rb_gdbm_nextkey(dbm, keystr)) {
475 VALUE assoc = rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr));
476 VALUE v = rb_yield(assoc);
477
478 if (RTEST(v)) {
479 rb_ary_push(new, assoc);
480 }
481 GetDBM2(obj, dbmp, dbm);
482 }
483
484 return new;
485 }
486
487 /*
488 * call-seq:
489 * gdbm.values_at(key, ...) -> array
490 *
491 * Returns an array of the values associated with each specified _key_.
492 */
493 static VALUE
494 fgdbm_values_at(int argc, VALUE *argv, VALUE obj)
495 {
496 VALUE new = rb_ary_new2(argc);
497 int i;
498
499 for (i=0; i<argc; i++) {
500 rb_ary_push(new, rb_gdbm_fetch3(obj, argv[i]));
501 }
502
503 return new;
504 }
505
506 static void
507 rb_gdbm_modify(VALUE obj)
508 {
509 if (OBJ_FROZEN(obj)) rb_error_frozen("GDBM");
510 }
511
512 static VALUE
513 rb_gdbm_delete(VALUE obj, VALUE keystr)
514 {
515 datum key;
516 struct dbmdata *dbmp;
517 GDBM_FILE dbm;
518 long len;
519
520 rb_gdbm_modify(obj);
521 StringValue(keystr);
522 len = RSTRING_LEN(keystr);
523 if (TOO_LONG(len)) return Qnil;
524 key.dptr = RSTRING_PTR(keystr);
525 key.dsize = (int)len;
526
527 GetDBM2(obj, dbmp, dbm);
528 if (!gdbm_exists(dbm, key)) {
529 return Qnil;
530 }
531
532 if (gdbm_delete(dbm, key)) {
533 dbmp->di_size = -1;
534 rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
535 }
536 else if (dbmp->di_size >= 0) {
537 dbmp->di_size--;
538 }
539 return obj;
540 }
541
542 /*
543 * call-seq:
544 * gdbm.delete(key) -> value or nil
545 *
546 * Removes the key-value-pair with the specified _key_ from this database and
547 * returns the corresponding _value_. Returns nil if the database is empty.
548 */
549 static VALUE
550 fgdbm_delete(VALUE obj, VALUE keystr)
551 {
552 VALUE valstr;
553
554 valstr = fgdbm_fetch(obj, keystr, Qnil);
555 rb_gdbm_delete(obj, keystr);
556 return valstr;
557 }
558
559 /*
560 * call-seq:
561 * gdbm.shift -> (key, value) or nil
562 *
563 * Removes a key-value-pair from this database and returns it as a
564 * two-item array [ _key_, _value_ ]. Returns nil if the database is empty.
565 */
566 static VALUE
567 fgdbm_shift(VALUE obj)
568 {
569 struct dbmdata *dbmp;
570 GDBM_FILE dbm;
571 VALUE keystr, valstr;
572
573 rb_gdbm_modify(obj);
574 GetDBM2(obj, dbmp, dbm);
575 keystr = rb_gdbm_firstkey(dbm);
576 if (NIL_P(keystr)) return Qnil;
577 valstr = rb_gdbm_fetch2(dbm, keystr);
578 rb_gdbm_delete(obj, keystr);
579
580 return rb_assoc_new(keystr, valstr);
581 }
582
583 /*
584 * call-seq:
585 * gdbm.delete_if { |key, value| block } -> gdbm
586 * gdbm.reject! { |key, value| block } -> gdbm
587 *
588 * Deletes every key-value pair from _gdbm_ for which _block_ evaluates to true.
589 */
590 static VALUE
591 fgdbm_delete_if(VALUE obj)
592 {
593 struct dbmdata *dbmp;
594 GDBM_FILE dbm;
595 VALUE keystr, valstr;
596 VALUE ret, ary = rb_ary_tmp_new(0);
597 int i, status = 0, n;
598
599 rb_gdbm_modify(obj);
600 GetDBM2(obj, dbmp, dbm);
601 n = dbmp->di_size;
602 dbmp->di_size = -1;
603
604 for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
605 keystr = rb_gdbm_nextkey(dbm, keystr)) {
606
607 OBJ_FREEZE(keystr);
608 valstr = rb_gdbm_fetch2(dbm, keystr);
609 ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status);
610 if (status != 0) break;
611 if (RTEST(ret)) rb_ary_push(ary, keystr);
612 GetDBM2(obj, dbmp, dbm);
613 }
614
615 for (i = 0; i < RARRAY_LEN(ary); i++)
616 rb_gdbm_delete(obj, RARRAY_PTR(ary)[i]);
617 if (status) rb_jump_tag(status);
618 if (n > 0) dbmp->di_size = n - (int)RARRAY_LEN(ary);
619 rb_ary_clear(ary);
620
621 return obj;
622 }
623
624 /*
625 * call-seq:
626 * gdbm.clear -> gdbm
627 *
628 * Removes all the key-value pairs within _gdbm_.
629 */
630 static VALUE
631 fgdbm_clear(VALUE obj)
632 {
633 datum key, nextkey;
634 struct dbmdata *dbmp;
635 GDBM_FILE dbm;
636
637 rb_gdbm_modify(obj);
638 GetDBM2(obj, dbmp, dbm);
639 dbmp->di_size = -1;
640
641 #if 0
642 while (key = gdbm_firstkey(dbm), key.dptr) {
643 if (gdbm_delete(dbm, key)) {
644 free(key.dptr);
645 rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
646 }
647 free(key.dptr);
648 }
649 #else
650 while (key = gdbm_firstkey(dbm), key.dptr) {
651 for (; key.dptr; key = nextkey) {
652 nextkey = gdbm_nextkey(dbm, key);
653 if (gdbm_delete(dbm, key)) {
654 free(key.dptr);
655 if (nextkey.dptr) free(nextkey.dptr);
656 rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
657 }
658 free(key.dptr);
659 }
660 }
661 #endif
662 dbmp->di_size = 0;
663
664 return obj;
665 }
666
667 /*
668 * call-seq:
669 * gdbm.invert -> hash
670 *
671 * Returns a hash created by using _gdbm_'s values as keys, and the keys
672 * as values.
673 */
674 static VALUE
675 fgdbm_invert(VALUE obj)
676 {
677 struct dbmdata *dbmp;
678 GDBM_FILE dbm;
679 VALUE keystr, valstr;
680 VALUE hash = rb_hash_new();
681
682 GetDBM2(obj, dbmp, dbm);
683 for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
684 keystr = rb_gdbm_nextkey(dbm, keystr)) {
685 valstr = rb_gdbm_fetch2(dbm, keystr);
686
687 rb_hash_aset(hash, valstr, keystr);
688 }
689 return hash;
690 }
691
692 /*
693 * call-seq:
694 * gdbm[key]= value -> value
695 * gdbm.store(key, value) -> value
696 *
697 * Associates the value _value_ with the specified _key_.
698 */
699 static VALUE
700 fgdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
701 {
702 datum key, val;
703 struct dbmdata *dbmp;
704 GDBM_FILE dbm;
705
706 rb_gdbm_modify(obj);
707 StringValue(keystr);
708 StringValue(valstr);
709
710 key.dptr = RSTRING_PTR(keystr);
711 key.dsize = RSTRING_LENINT(keystr);
712
713 val.dptr = RSTRING_PTR(valstr);
714 val.dsize = RSTRING_LENINT(valstr);
715
716 GetDBM2(obj, dbmp, dbm);
717 dbmp->di_size = -1;
718 if (gdbm_store(dbm, key, val, GDBM_REPLACE)) {
719 if (errno == EPERM) rb_sys_fail(0);
720 rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
721 }
722
723 return valstr;
724 }
725
726 static VALUE
727 update_i(RB_BLOCK_CALL_FUNC_ARGLIST(pair, dbm))
728 {
729 Check_Type(pair, T_ARRAY);
730 if (RARRAY_LEN(pair) < 2) {
731 rb_raise(rb_eArgError, "pair must be [key, value]");
732 }
733 fgdbm_store(dbm, RARRAY_PTR(pair)[0], RARRAY_PTR(pair)[1]);
734 return Qnil;
735 }
736
737 /*
738 * call-seq:
739 * gdbm.update(other) -> gdbm
740 *
741 * Adds the key-value pairs of _other_ to _gdbm_, overwriting entries with
742 * duplicate keys with those from _other_. _other_ must have an each_pair
743 * method.
744 */
745 static VALUE
746 fgdbm_update(VALUE obj, VALUE other)
747 {
748 rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
749 return obj;
750 }
751
752 /*
753 * call-seq:
754 * gdbm.replace(other) -> gdbm
755 *
756 * Replaces the content of _gdbm_ with the key-value pairs of _other_.
757 * _other_ must have an each_pair method.
758 */
759 static VALUE
760 fgdbm_replace(VALUE obj, VALUE other)
761 {
762 fgdbm_clear(obj);
763 rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
764 return obj;
765 }
766
767 /*
768 * call-seq:
769 * gdbm.length -> fixnum
770 * gdbm.size -> fixnum
771 *
772 * Returns the number of key-value pairs in this database.
773 */
774 static VALUE
775 fgdbm_length(VALUE obj)
776 {
777 datum key, nextkey;
778 struct dbmdata *dbmp;
779 GDBM_FILE dbm;
780 int i = 0;
781
782 GetDBM2(obj, dbmp, dbm);
783 if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
784
785 for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
786 nextkey = gdbm_nextkey(dbm, key);
787 free(key.dptr);
788 i++;
789 }
790 dbmp->di_size = i;
791
792 return INT2FIX(i);
793 }
794
795 /*
796 * call-seq:
797 * gdbm.empty? -> true or false
798 *
799 * Returns true if the database is empty.
800 */
801 static VALUE
802 fgdbm_empty_p(VALUE obj)
803 {
804 datum key;
805 struct dbmdata *dbmp;
806 GDBM_FILE dbm;
807
808 GetDBM(obj, dbmp);
809 if (dbmp->di_size < 0) {
810 dbm = dbmp->di_dbm;
811
812 key = gdbm_firstkey(dbm);
813 if (key.dptr) {
814 free(key.dptr);
815 return Qfalse;
816 }
817 return Qtrue;
818 }
819
820 if (dbmp->di_size == 0) return Qtrue;
821 return Qfalse;
822 }
823
824 /*
825 * call-seq:
826 * gdbm.each_value { |value| block } -> gdbm
827 *
828 * Executes _block_ for each key in the database, passing the corresponding
829 * _value_ as a parameter.
830 */
831 static VALUE
832 fgdbm_each_value(VALUE obj)
833 {
834 struct dbmdata *dbmp;
835 GDBM_FILE dbm;
836 VALUE keystr;
837
838 RETURN_ENUMERATOR(obj, 0, 0);
839
840 GetDBM2(obj, dbmp, dbm);
841 for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
842 keystr = rb_gdbm_nextkey(dbm, keystr)) {
843
844 rb_yield(rb_gdbm_fetch2(dbm, keystr));
845 GetDBM2(obj, dbmp, dbm);
846 }
847 return obj;
848 }
849
850 /*
851 * call-seq:
852 * gdbm.each_key { |key| block } -> gdbm
853 *
854 * Executes _block_ for each key in the database, passing the
855 * _key_ as a parameter.
856 */
857 static VALUE
858 fgdbm_each_key(VALUE obj)
859 {
860 struct dbmdata *dbmp;
861 GDBM_FILE dbm;
862 VALUE keystr;
863
864 RETURN_ENUMERATOR(obj, 0, 0);
865
866 GetDBM2(obj, dbmp, dbm);
867 for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
868 keystr = rb_gdbm_nextkey(dbm, keystr)) {
869
870 rb_yield(keystr);
871 GetDBM2(obj, dbmp, dbm);
872 }
873 return obj;
874 }
875
876 /*
877 * call-seq:
878 * gdbm.each_pair { |key, value| block } -> gdbm
879 *
880 * Executes _block_ for each key in the database, passing the _key_ and the
881 * correspoding _value_ as a parameter.
882 */
883 static VALUE
884 fgdbm_each_pair(VALUE obj)
885 {
886 GDBM_FILE dbm;
887 struct dbmdata *dbmp;
888 VALUE keystr;
889
890 RETURN_ENUMERATOR(obj, 0, 0);
891
892 GetDBM2(obj, dbmp, dbm);
893 for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
894 keystr = rb_gdbm_nextkey(dbm, keystr)) {
895
896 rb_yield(rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
897 GetDBM2(obj, dbmp, dbm);
898 }
899
900 return obj;
901 }
902
903 /*
904 * call-seq:
905 * gdbm.keys -> array
906 *
907 * Returns an array of all keys of this database.
908 */
909 static VALUE
910 fgdbm_keys(VALUE obj)
911 {
912 struct dbmdata *dbmp;
913 GDBM_FILE dbm;
914 VALUE keystr, ary;
915
916 GetDBM2(obj, dbmp, dbm);
917 ary = rb_ary_new();
918 for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
919 keystr = rb_gdbm_nextkey(dbm, keystr)) {
920
921 rb_ary_push(ary, keystr);
922 }
923
924 return ary;
925 }
926
927 /*
928 * call-seq:
929 * gdbm.values -> array
930 *
931 * Returns an array of all values of this database.
932 */
933 static VALUE
934 fgdbm_values(VALUE obj)
935 {
936 datum key, nextkey;
937 struct dbmdata *dbmp;
938 GDBM_FILE dbm;
939 VALUE valstr, ary;
940
941 GetDBM2(obj, dbmp, dbm);
942 ary = rb_ary_new();
943 for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
944 nextkey = gdbm_nextkey(dbm, key);
945 valstr = rb_gdbm_fetch(dbm, key);
946 free(key.dptr);
947 rb_ary_push(ary, valstr);
948 }
949
950 return ary;
951 }
952
953 /*
954 * call-seq:
955 * gdbm.include?(k) -> true or false
956 * gdbm.has_key?(k) -> true or false
957 * gdbm.member?(k) -> true or false
958 * gdbm.key?(k) -> true or false
959 *
960 * Returns true if the given key _k_ exists within the database.
961 * Returns false otherwise.
962 */
963 static VALUE
964 fgdbm_has_key(VALUE obj, VALUE keystr)
965 {
966 datum key;
967 struct dbmdata *dbmp;
968 GDBM_FILE dbm;
969 long len;
970
971 StringValue(keystr);
972 len = RSTRING_LENINT(keystr);
973 if (TOO_LONG(len)) return Qfalse;
974 key.dptr = RSTRING_PTR(keystr);
975 key.dsize = (int)len;
976
977 GetDBM2(obj, dbmp, dbm);
978 if (gdbm_exists(dbm, key))
979 return Qtrue;
980 return Qfalse;
981 }
982
983 /*
984 * call-seq:
985 * gdbm.has_value?(v) -> true or false
986 * gdbm.value?(v) -> true or false
987 *
988 * Returns true if the given value _v_ exists within the database.
989 * Returns false otherwise.
990 */
991 static VALUE
992 fgdbm_has_value(VALUE obj, VALUE valstr)
993 {
994 struct dbmdata *dbmp;
995 GDBM_FILE dbm;
996 VALUE keystr, valstr2;
997
998 StringValue(valstr);
999 GetDBM2(obj, dbmp, dbm);
1000 for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
1001 keystr = rb_gdbm_nextkey(dbm, keystr)) {
1002
1003 valstr2 = rb_gdbm_fetch2(dbm, keystr);
1004
1005 if (!NIL_P(valstr2) &&
1006 (int)RSTRING_LEN(valstr) == (int)RSTRING_LEN(valstr2) &&
1007 memcmp(RSTRING_PTR(valstr), RSTRING_PTR(valstr2),
1008 (int)RSTRING_LEN(valstr)) == 0) {
1009 return Qtrue;
1010 }
1011 }
1012 return Qfalse;
1013 }
1014
1015 /*
1016 * call-seq:
1017 * gdbm.to_a -> array
1018 *
1019 * Returns an array of all key-value pairs contained in the database.
1020 */
1021 static VALUE
1022 fgdbm_to_a(VALUE obj)
1023 {
1024 struct dbmdata *dbmp;
1025 GDBM_FILE dbm;
1026 VALUE keystr, ary;
1027
1028 GetDBM2(obj, dbmp, dbm);
1029 ary = rb_ary_new();
1030 for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
1031 keystr = rb_gdbm_nextkey(dbm, keystr)) {
1032
1033 rb_ary_push(ary, rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
1034 }
1035
1036 return ary;
1037 }
1038
1039 /*
1040 * call-seq:
1041 * gdbm.reorganize -> gdbm
1042 *
1043 * Reorganizes the database file. This operation removes reserved space of
1044 * elements that have already been deleted. It is only useful after a lot of
1045 * deletions in the database.
1046 */
1047 static VALUE
1048 fgdbm_reorganize(VALUE obj)
1049 {
1050 struct dbmdata *dbmp;
1051 GDBM_FILE dbm;
1052
1053 rb_gdbm_modify(obj);
1054 GetDBM2(obj, dbmp, dbm);
1055 gdbm_reorganize(dbm);
1056 rb_fd_fix_cloexec(gdbm_fdesc(dbm));
1057 return obj;
1058 }
1059
1060 /*
1061 * call-seq:
1062 * gdbm.sync -> gdbm
1063 *
1064 * Unless the _gdbm_ object has been opened with the *SYNC* flag, it is not
1065 * guarenteed that database modification operations are immediately applied to
1066 * the database file. This method ensures that all recent modifications
1067 * to the database are written to the file. Blocks until all writing operations
1068 * to the disk have been finished.
1069 */
1070 static VALUE
1071 fgdbm_sync(VALUE obj)
1072 {
1073 struct dbmdata *dbmp;
1074 GDBM_FILE dbm;
1075
1076 rb_gdbm_modify(obj);
1077 GetDBM2(obj, dbmp, dbm);
1078 gdbm_sync(dbm);
1079 return obj;
1080 }
1081
1082 /*
1083 * call-seq:
1084 * gdbm.cachesize = size -> size
1085 *
1086 * Sets the size of the internal bucket cache to _size_.
1087 */
1088 static VALUE
1089 fgdbm_set_cachesize(VALUE obj, VALUE val)
1090 {
1091 struct dbmdata *dbmp;
1092 GDBM_FILE dbm;
1093 int optval;
1094
1095 GetDBM2(obj, dbmp, dbm);
1096 optval = FIX2INT(val);
1097 if (gdbm_setopt(dbm, GDBM_CACHESIZE, &optval, sizeof(optval)) == -1) {
1098 rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
1099 }
1100 return val;
1101 }
1102
1103 /*
1104 * call-seq:
1105 * gdbm.fastmode = boolean -> boolean
1106 *
1107 * Turns the database's fast mode on or off. If fast mode is turned on, gdbm
1108 * does not wait for writes to be flushed to the disk before continuing.
1109 *
1110 * This option is obsolete for gdbm >= 1.8 since fast mode is turned on by
1111 * default. See also: #syncmode=
1112 */
1113 static VALUE
1114 fgdbm_set_fastmode(VALUE obj, VALUE val)
1115 {
1116 struct dbmdata *dbmp;
1117 GDBM_FILE dbm;
1118 int optval;
1119
1120 GetDBM2(obj, dbmp, dbm);
1121 optval = 0;
1122 if (RTEST(val))
1123 optval = 1;
1124
1125 if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) {
1126 rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
1127 }
1128 return val;
1129 }
1130
1131 /*
1132 * call-seq:
1133 * gdbm.syncmode = boolean -> boolean
1134 *
1135 * Turns the database's synchronization mode on or off. If the synchronization
1136 * mode is turned on, the database's in-memory state will be synchronized to
1137 * disk after every database modification operation. If the synchronization
1138 * mode is turned off, GDBM does not wait for writes to be flushed to the disk
1139 * before continuing.
1140 *
1141 * This option is only available for gdbm >= 1.8 where syncmode is turned off
1142 * by default. See also: #fastmode=
1143 */
1144 static VALUE
1145 fgdbm_set_syncmode(VALUE obj, VALUE val)
1146 {
1147 #if !defined(GDBM_SYNCMODE)
1148 fgdbm_set_fastmode(obj, RTEST(val) ? Qfalse : Qtrue);
1149 return val;
1150 #else
1151 struct dbmdata *dbmp;
1152 GDBM_FILE dbm;
1153 int optval;
1154
1155 GetDBM2(obj, dbmp, dbm);
1156 optval = 0;
1157 if (RTEST(val))
1158 optval = 1;
1159
1160 if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) {
1161 rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
1162 }
1163 return val;
1164 #endif
1165 }
1166
1167 /*
1168 * call-seq:
1169 * gdbm.to_hash -> hash
1170 *
1171 * Returns a hash of all key-value pairs contained in the database.
1172 */
1173 static VALUE
1174 fgdbm_to_hash(VALUE obj)
1175 {
1176 struct dbmdata *dbmp;
1177 GDBM_FILE dbm;
1178 VALUE keystr, hash;
1179
1180 GetDBM2(obj, dbmp, dbm);
1181 hash = rb_hash_new();
1182 for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
1183 keystr = rb_gdbm_nextkey(dbm, keystr)) {
1184
1185 rb_hash_aset(hash, keystr, rb_gdbm_fetch2(dbm, keystr));
1186 }
1187
1188 return hash;
1189 }
1190
1191 /*
1192 * call-seq:
1193 * gdbm.reject { |key, value| block } -> hash
1194 *
1195 * Returns a hash copy of _gdbm_ where all key-value pairs from _gdbm_ for
1196 * which _block_ evaluates to true are removed. See also: #delete_if
1197 */
1198 static VALUE
1199 fgdbm_reject(VALUE obj)
1200 {
1201 return rb_hash_delete_if(fgdbm_to_hash(obj));
1202 }
1203
1204 void
1205 Init_gdbm(void)
1206 {
1207 rb_cGDBM = rb_define_class("GDBM", rb_cObject);
1208 rb_eGDBMError = rb_define_class("GDBMError", rb_eStandardError);
1209 rb_eGDBMFatalError = rb_define_class("GDBMFatalError", rb_eException);
1210 rb_include_module(rb_cGDBM, rb_mEnumerable);
1211
1212 rb_define_alloc_func(rb_cGDBM, fgdbm_s_alloc);
1213 rb_define_singleton_method(rb_cGDBM, "open", fgdbm_s_open, -1);
1214
1215 rb_define_method(rb_cGDBM, "initialize", fgdbm_initialize, -1);
1216 rb_define_method(rb_cGDBM, "close", fgdbm_close, 0);
1217 rb_define_method(rb_cGDBM, "closed?", fgdbm_closed, 0);
1218 rb_define_method(rb_cGDBM, "[]", fgdbm_aref, 1);
1219 rb_define_method(rb_cGDBM, "fetch", fgdbm_fetch_m, -1);
1220 rb_define_method(rb_cGDBM, "[]=", fgdbm_store, 2);
1221 rb_define_method(rb_cGDBM, "store", fgdbm_store, 2);
1222 rb_define_method(rb_cGDBM, "index", fgdbm_index, 1);
1223 rb_define_method(rb_cGDBM, "key", fgdbm_key, 1);
1224 rb_define_method(rb_cGDBM, "select", fgdbm_select, 0);
1225 rb_define_method(rb_cGDBM, "values_at", fgdbm_values_at, -1);
1226 rb_define_method(rb_cGDBM, "length", fgdbm_length, 0);
1227 rb_define_method(rb_cGDBM, "size", fgdbm_length, 0);
1228 rb_define_method(rb_cGDBM, "empty?", fgdbm_empty_p, 0);
1229 rb_define_method(rb_cGDBM, "each", fgdbm_each_pair, 0);
1230 rb_define_method(rb_cGDBM, "each_value", fgdbm_each_value, 0);
1231 rb_define_method(rb_cGDBM, "each_key", fgdbm_each_key, 0);
1232 rb_define_method(rb_cGDBM, "each_pair", fgdbm_each_pair, 0);
1233 rb_define_method(rb_cGDBM, "keys", fgdbm_keys, 0);
1234 rb_define_method(rb_cGDBM, "values", fgdbm_values, 0);
1235 rb_define_method(rb_cGDBM, "shift", fgdbm_shift, 0);
1236 rb_define_method(rb_cGDBM, "delete", fgdbm_delete, 1);
1237 rb_define_method(rb_cGDBM, "delete_if", fgdbm_delete_if, 0);
1238 rb_define_method(rb_cGDBM, "reject!", fgdbm_delete_if, 0);
1239 rb_define_method(rb_cGDBM, "reject", fgdbm_reject, 0);
1240 rb_define_method(rb_cGDBM, "clear", fgdbm_clear, 0);
1241 rb_define_method(rb_cGDBM, "invert", fgdbm_invert, 0);
1242 rb_define_method(rb_cGDBM, "update", fgdbm_update, 1);
1243 rb_define_method(rb_cGDBM, "replace", fgdbm_replace, 1);
1244 rb_define_method(rb_cGDBM, "reorganize", fgdbm_reorganize, 0);
1245 rb_define_method(rb_cGDBM, "sync", fgdbm_sync, 0);
1246 /* rb_define_method(rb_cGDBM, "setopt", fgdbm_setopt, 2); */
1247 rb_define_method(rb_cGDBM, "cachesize=", fgdbm_set_cachesize, 1);
1248 rb_define_method(rb_cGDBM, "fastmode=", fgdbm_set_fastmode, 1);
1249 rb_define_method(rb_cGDBM, "syncmode=", fgdbm_set_syncmode, 1);
1250
1251 rb_define_method(rb_cGDBM, "include?", fgdbm_has_key, 1);
1252 rb_define_method(rb_cGDBM, "has_key?", fgdbm_has_key, 1);
1253 rb_define_method(rb_cGDBM, "member?", fgdbm_has_key, 1);
1254 rb_define_method(rb_cGDBM, "has_value?", fgdbm_has_value, 1);
1255 rb_define_method(rb_cGDBM, "key?", fgdbm_has_key, 1);
1256 rb_define_method(rb_cGDBM, "value?", fgdbm_has_value, 1);
1257
1258 rb_define_method(rb_cGDBM, "to_a", fgdbm_to_a, 0);
1259 rb_define_method(rb_cGDBM, "to_hash", fgdbm_to_hash, 0);
1260
1261 /* flag for #new and #open: open database as a reader */
1262 rb_define_const(rb_cGDBM, "READER", INT2FIX(GDBM_READER|RUBY_GDBM_RW_BIT));
1263 /* flag for #new and #open: open database as a writer */
1264 rb_define_const(rb_cGDBM, "WRITER", INT2FIX(GDBM_WRITER|RUBY_GDBM_RW_BIT));
1265 /* flag for #new and #open: open database as a writer; if the database does not exist, create a new one */
1266 rb_define_const(rb_cGDBM, "WRCREAT", INT2FIX(GDBM_WRCREAT|RUBY_GDBM_RW_BIT));
1267 /* flag for #new and #open: open database as a writer; overwrite any existing databases */
1268 rb_define_const(rb_cGDBM, "NEWDB", INT2FIX(GDBM_NEWDB|RUBY_GDBM_RW_BIT));
1269
1270 /* flag for #new and #open. this flag is obsolete for gdbm >= 1.8 */
1271 rb_define_const(rb_cGDBM, "FAST", INT2FIX(GDBM_FAST));
1272 /* this flag is obsolete in gdbm 1.8.
1273 On gdbm 1.8, fast mode is default behavior. */
1274
1275 /* gdbm version 1.8 specific */
1276 #if defined(GDBM_SYNC)
1277 /* flag for #new and #open. only for gdbm >= 1.8 */
1278 rb_define_const(rb_cGDBM, "SYNC", INT2FIX(GDBM_SYNC));
1279 #endif
1280 #if defined(GDBM_NOLOCK)
1281 /* flag for #new and #open */
1282 rb_define_const(rb_cGDBM, "NOLOCK", INT2FIX(GDBM_NOLOCK));
1283 #endif
1284 /* version of the gdbm library*/
1285 rb_define_const(rb_cGDBM, "VERSION", rb_str_new2(gdbm_version));
1286 }
dbmdata::di_dbm
DBM * di_dbm
Definition: dbm.c:39
rb_eStandardError
VALUE rb_eStandardError
Definition: error.c:546
fgdbm_key
static VALUE fgdbm_key(VALUE obj, VALUE valstr)
Definition: gdbm.c:427
fgdbm_set_syncmode
static VALUE fgdbm_set_syncmode(VALUE obj, VALUE val)
Definition: gdbm.c:1145
RARRAY_LEN
#define RARRAY_LEN(a)
Definition: ruby.h:878
fgdbm_empty_p
static VALUE fgdbm_empty_p(VALUE obj)
Definition: gdbm.c:802
fgdbm_values_at
static VALUE fgdbm_values_at(int argc, VALUE *argv, VALUE obj)
Definition: gdbm.c:494
fgdbm_reject
static VALUE fgdbm_reject(VALUE obj)
Definition: gdbm.c:1199
NUM2INT
#define NUM2INT(x)
Definition: ruby.h:630
Data_Get_Struct
#define Data_Get_Struct(obj, type, sval)
Definition: ruby.h:1036
rb_define_singleton_method
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1655
fgdbm_clear
static VALUE fgdbm_clear(VALUE obj)
Definition: gdbm.c:631
Qtrue
#define Qtrue
Definition: ruby.h:426
rb_error_frozen
void rb_error_frozen(const char *what)
Definition: error.c:2077
ruby.h
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:896
dbmdata::di_size
int di_size
Definition: gdbm.c:93
rb_ary_tmp_new
VALUE rb_ary_tmp_new(long capa)
Definition: array.c:534
rb_protect
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:807
fgdbm_s_alloc
static VALUE fgdbm_s_alloc(VALUE klass)
Definition: gdbm.c:162
Check_Type
#define Check_Type(v, t)
Definition: ruby.h:532
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1857
GetDBM2
#define GetDBM2(obj, data, dbm)
Definition: gdbm.c:109
rb_ary_clear
VALUE rb_ary_clear(VALUE ary)
Definition: array.c:3381
rb_gdbm_fetch
static VALUE rb_gdbm_fetch(GDBM_FILE dbm, datum key)
Definition: gdbm.c:296
rb_define_alloc_func
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
dbmdata
Definition: dbm.c:37
DATA_PTR
#define DATA_PTR(dta)
Definition: ruby.h:992
fgdbm_keys
static VALUE fgdbm_keys(VALUE obj)
Definition: gdbm.c:910
rb_eGDBMError
static VALUE rb_eGDBMError
Definition: gdbm.c:74
rb_include_module
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:827
datum::dptr
char * dptr
Definition: sdbm.h:51
rb_block_call
VALUE rb_block_call(VALUE, ID, int, const VALUE *, rb_block_call_func_t, VALUE)
fgdbm_select
static VALUE fgdbm_select(VALUE obj)
Definition: gdbm.c:465
T_ARRAY
#define T_ARRAY
Definition: ruby.h:484
fgdbm_has_key
static VALUE fgdbm_has_key(VALUE obj, VALUE keystr)
Definition: gdbm.c:964
fgdbm_s_open
static VALUE fgdbm_s_open(int argc, VALUE *argv, VALUE klass)
Definition: gdbm.c:280
rb_cGDBM
static VALUE rb_cGDBM
Definition: gdbm.c:74
fgdbm_close
static VALUE fgdbm_close(VALUE obj)
Definition: gdbm.c:130
rb_ary_new2
#define rb_ary_new2
Definition: intern.h:90
Data_Wrap_Struct
#define Data_Wrap_Struct(klass, mark, free, sval)
Definition: ruby.h:1018
RB_BLOCK_CALL_FUNC_ARGLIST
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Definition: ruby.h:1511
fgdbm_aref
static VALUE fgdbm_aref(VALUE obj, VALUE keystr)
Definition: gdbm.c:394
fgdbm_each_value
static VALUE fgdbm_each_value(VALUE obj)
Definition: gdbm.c:832
rb_gdbm_nextkey
static VALUE rb_gdbm_nextkey(GDBM_FILE dbm, VALUE keystr)
Definition: gdbm.c:353
datum
Definition: sdbm.h:50
rb_block_given_p
int rb_block_given_p(void)
Definition: eval.c:712
rb_hash_aset
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1393
val
#define val
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1553
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:547
rb_gdbm_fetch3
static VALUE rb_gdbm_fetch3(VALUE obj, VALUE keystr)
Definition: gdbm.c:327
RUBY_GDBM_RW_BIT
#define RUBY_GDBM_RW_BIT
Definition: gdbm.c:82
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:495
fgdbm_reorganize
static VALUE fgdbm_reorganize(VALUE obj)
Definition: gdbm.c:1048
MY_BLOCK_SIZE
#define MY_BLOCK_SIZE
Definition: gdbm.c:84
closed_dbm
static void closed_dbm(void)
Definition: gdbm.c:98
NIL_P
#define NIL_P(v)
Definition: ruby.h:438
fgdbm_invert
static VALUE fgdbm_invert(VALUE obj)
Definition: gdbm.c:675
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:630
msg
static char msg[50]
Definition: strerror.c:8
fgdbm_to_hash
static VALUE fgdbm_to_hash(VALUE obj)
Definition: gdbm.c:1174
rb_define_const
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:2225
fgdbm_delete_if
static VALUE fgdbm_delete_if(VALUE obj)
Definition: gdbm.c:591
rb_sys_fail_str
void rb_sys_fail_str(VALUE mesg)
Definition: error.c:1982
OBJ_FROZEN
#define OBJ_FROZEN(x)
Definition: ruby.h:1185
argc
int argc
Definition: ruby.c:131
Qfalse
#define Qfalse
Definition: ruby.h:425
rb_str_new2
#define rb_str_new2
Definition: intern.h:840
OBJ_FREEZE
#define OBJ_FREEZE(x)
Definition: ruby.h:1186
fgdbm_has_value
static VALUE fgdbm_has_value(VALUE obj, VALUE valstr)
Definition: gdbm.c:992
rb_eIndexError
VALUE rb_eIndexError
Definition: error.c:550
dbmdata::di_dbm
GDBM_FILE di_dbm
Definition: gdbm.c:94
ALLOC
#define ALLOC(type)
Definition: ruby.h:1334
RSTRING_LEN
#define RSTRING_LEN(str)
Definition: ruby.h:841
fgdbm_to_a
static VALUE fgdbm_to_a(VALUE obj)
Definition: gdbm.c:1022
fgdbm_shift
static VALUE fgdbm_shift(VALUE obj)
Definition: gdbm.c:567
rb_yield
VALUE rb_yield(VALUE)
Definition: vm_eval.c:942
fgdbm_store
static VALUE fgdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
Definition: gdbm.c:700
errno
int errno
rb_gdbm_delete
static VALUE rb_gdbm_delete(VALUE obj, VALUE keystr)
Definition: gdbm.c:513
rb_mEnumerable
VALUE rb_mEnumerable
Definition: enum.c:20
fgdbm_each_pair
static VALUE fgdbm_each_pair(VALUE obj)
Definition: gdbm.c:884
rb_hash_new
VALUE rb_hash_new(void)
Definition: hash.c:298
fgdbm_initialize
static VALUE fgdbm_initialize(int argc, VALUE *argv, VALUE obj)
Definition: gdbm.c:191
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1728
rb_assoc_new
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Definition: array.c:616
Qnil
#define Qnil
Definition: ruby.h:427
datum::dsize
int dsize
Definition: sdbm.h:52
OBJ_TAINT
#define OBJ_TAINT(x)
Definition: ruby.h:1177
VALUE
unsigned long VALUE
Definition: ruby.h:88
FIX2INT
#define FIX2INT(x)
Definition: ruby.h:632
rb_gdbm_fetch2
static VALUE rb_gdbm_fetch2(GDBM_FILE dbm, VALUE keystr)
Definition: gdbm.c:312
EPERM
#define EPERM
Definition: _sdbm.c:94
rb_ensure
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:839
memcmp
int memcmp(const void *s1, const void *s2, size_t len)
Definition: memcmp.c:7
rb_sys_fail
void rb_sys_fail(const char *mesg)
Definition: error.c:1976
rb_jump_tag
void rb_jump_tag(int tag)
Definition: eval.c:706
rb_str_dup
VALUE rb_str_dup(VALUE)
Definition: string.c:1062
MY_FATAL_FUNC
#define MY_FATAL_FUNC
Definition: gdbm.c:85
GetDBM
#define GetDBM(obj, dbmp)
Definition: gdbm.c:103
TOO_LONG
#define TOO_LONG(n)
Definition: gdbm.c:79
RSTRING_PTR
#define RSTRING_PTR(str)
Definition: ruby.h:845
Init_gdbm
void Init_gdbm(void)
Definition: gdbm.c:1205
INT2FIX
#define INT2FIX(i)
Definition: ruby.h:231
fgdbm_fetch_m
static VALUE fgdbm_fetch_m(int argc, VALUE *argv, VALUE obj)
Definition: gdbm.c:407
rb_fd_fix_cloexec
void rb_fd_fix_cloexec(int fd)
Definition: io.c:221
RARRAY_PTR
#define RARRAY_PTR(a)
Definition: ruby.h:907
update_i
static VALUE update_i(RB_BLOCK_CALL_FUNC_ARGLIST(pair, dbm))
Definition: gdbm.c:727
key
uint8_t key[16]
Definition: random.c:1250
RTEST
#define RTEST(v)
Definition: ruby.h:437
rb_eGDBMFatalError
static VALUE rb_eGDBMFatalError
Definition: gdbm.c:74
fgdbm_set_cachesize
static VALUE fgdbm_set_cachesize(VALUE obj, VALUE val)
Definition: gdbm.c:1089
fgdbm_each_key
static VALUE fgdbm_each_key(VALUE obj)
Definition: gdbm.c:858
hash
static unsigned int hash(const char *str, unsigned int len)
Definition: lex.c:56
RETURN_ENUMERATOR
#define RETURN_ENUMERATOR(obj, argc, argv)
Definition: intern.h:242
SafeStringValue
#define SafeStringValue(v)
Definition: ruby.h:545
fgdbm_length
static VALUE fgdbm_length(VALUE obj)
Definition: gdbm.c:775
fgdbm_delete
static VALUE fgdbm_delete(VALUE obj, VALUE keystr)
Definition: gdbm.c:550
fgdbm_update
static VALUE fgdbm_update(VALUE obj, VALUE other)
Definition: gdbm.c:746
fgdbm_replace
static VALUE fgdbm_replace(VALUE obj, VALUE other)
Definition: gdbm.c:760
rb_gdbm_firstkey
static VALUE rb_gdbm_firstkey(GDBM_FILE dbm)
Definition: gdbm.c:337
RSTRING_LENINT
#define RSTRING_LENINT(str)
Definition: ruby.h:853
rb_hash_delete_if
VALUE rb_hash_delete_if(VALUE hash)
Definition: hash.c:1094
fgdbm_values
static VALUE fgdbm_values(VALUE obj)
Definition: gdbm.c:934
xfree
void void xfree(void *)
rb_intern
#define rb_intern(str)
fgdbm_closed
static VALUE fgdbm_closed(VALUE obj)
Definition: gdbm.c:148
fgdbm_sync
static VALUE fgdbm_sync(VALUE obj)
Definition: gdbm.c:1071
free_dbm
static void free_dbm(struct dbmdata *dbmp)
Definition: gdbm.c:115
rb_gdbm_modify
static void rb_gdbm_modify(VALUE obj)
Definition: gdbm.c:507
fgdbm_set_fastmode
static VALUE fgdbm_set_fastmode(VALUE obj, VALUE val)
Definition: gdbm.c:1114
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1488
rb_warn
void rb_warn(const char *fmt,...)
Definition: error.c:223
free
free(psz)
rb_gdbm_fatal
static void rb_gdbm_fatal(const char *msg)
Definition: gdbm.c:87
rb_eArgError
VALUE rb_eArgError
Definition: error.c:549
fgdbm_index
static VALUE fgdbm_index(VALUE obj, VALUE value)
Definition: gdbm.c:451
fgdbm_fetch
static VALUE fgdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone)
Definition: gdbm.c:374
argv
char ** argv
Definition: ruby.c:132
StringValue
#define StringValue(v)
Definition: ruby.h:539
rb_eException
VALUE rb_eException
Definition: error.c:541
rb_str_new
VALUE rb_str_new(const char *, long)
Definition: string.c:534