1999-08-13 01:37:52 -04:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
gdbm.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
modified at: Mon Jan 24 15:59:52 JST 1994
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
Documentation by Peter Adolphs < futzilogik at users dot sourceforge dot net >
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
|
|
|
|
#include <gdbm.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* Document-class: GDBM
|
|
|
|
*
|
|
|
|
* == Summary
|
|
|
|
*
|
|
|
|
* Ruby extension for GNU dbm (gdbm) -- a simple database engine for storing
|
|
|
|
* key-value pairs on disk.
|
|
|
|
*
|
|
|
|
* == Description
|
|
|
|
*
|
|
|
|
* GNU dbm is a library for simple databases. A database is a file that stores
|
|
|
|
* key-value pairs. Gdbm allows the user to store, retrieve, and delete data by
|
|
|
|
* key. It furthermore allows a non-sorted traversal of all key-value pairs.
|
|
|
|
* A gdbm database thus provides the same functionality as a hash. As
|
|
|
|
* with objects of the Hash class, elements can be accessed with <tt>[]</tt>.
|
|
|
|
* Furthermore, GDBM mixes in the Enumerable module, thus providing convenient
|
|
|
|
* methods such as #find, #collect, #map, etc.
|
|
|
|
*
|
|
|
|
* A process is allowed to open several different databases at the same time.
|
|
|
|
* A process can open a database as a "reader" or a "writer". Whereas a reader
|
|
|
|
* has only read-access to the database, a writer has read- and write-access.
|
|
|
|
* A database can be accessed either by any number of readers or by exactly one
|
|
|
|
* writer at the same time.
|
|
|
|
*
|
|
|
|
* == Examples
|
|
|
|
*
|
|
|
|
* 1. Opening/creating a database, and filling it with some entries:
|
|
|
|
*
|
|
|
|
* require 'gdbm'
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2006-09-19 20:30:38 -04:00
|
|
|
* gdbm = GDBM.new("fruitstore.db")
|
|
|
|
* gdbm["ananas"] = "3"
|
|
|
|
* gdbm["banana"] = "8"
|
|
|
|
* gdbm["cranberry"] = "4909"
|
|
|
|
* gdbm.close
|
|
|
|
*
|
|
|
|
* 2. Reading out a database:
|
|
|
|
*
|
|
|
|
* require 'gdbm'
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2006-09-19 20:30:38 -04:00
|
|
|
* gdbm = GDBM.new("fruitstore.db")
|
|
|
|
* gdbm.each_pair do |key, value|
|
|
|
|
* print "#{key}: #{value}\n"
|
|
|
|
* end
|
|
|
|
* gdbm.close
|
|
|
|
*
|
|
|
|
* produces
|
|
|
|
*
|
|
|
|
* banana: 8
|
|
|
|
* ananas: 3
|
|
|
|
* cranberry: 4909
|
|
|
|
*
|
|
|
|
* == Links
|
|
|
|
*
|
|
|
|
* * http://www.gnu.org/software/gdbm/
|
|
|
|
*/
|
2002-02-26 23:52:21 -05:00
|
|
|
static VALUE rb_cGDBM, rb_eGDBMError, rb_eGDBMFatalError;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2011-11-08 09:34:39 -05:00
|
|
|
#if SIZEOF_LONG > SIZEOF_INT
|
|
|
|
#define TOO_LONG(n) ((long)(+(int)(n)) != (long)(n))
|
|
|
|
#else
|
|
|
|
#define TOO_LONG(n) 0
|
|
|
|
#endif
|
|
|
|
|
2004-04-15 04:15:27 -04:00
|
|
|
#define RUBY_GDBM_RW_BIT 0x20000000
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
#define MY_BLOCK_SIZE (2048)
|
2001-05-06 11:06:00 -04:00
|
|
|
#define MY_FATAL_FUNC rb_gdbm_fatal
|
2002-02-26 23:52:21 -05:00
|
|
|
static void
|
2011-09-29 07:37:47 -04:00
|
|
|
rb_gdbm_fatal(const char *msg)
|
2001-05-06 11:06:00 -04:00
|
|
|
{
|
2006-10-14 10:33:10 -04:00
|
|
|
rb_raise(rb_eGDBMFatalError, "%s", msg);
|
2001-05-06 11:06:00 -04:00
|
|
|
}
|
1999-08-13 01:37:52 -04:00
|
|
|
|
|
|
|
struct dbmdata {
|
|
|
|
int di_size;
|
|
|
|
GDBM_FILE di_dbm;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2006-02-12 23:53:22 -05:00
|
|
|
closed_dbm(void)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
rb_raise(rb_eRuntimeError, "closed GDBM file");
|
|
|
|
}
|
|
|
|
|
2002-09-24 02:00:03 -04:00
|
|
|
#define GetDBM(obj, dbmp) do {\
|
2011-02-15 07:38:11 -05:00
|
|
|
Data_Get_Struct((obj), struct dbmdata, (dbmp));\
|
|
|
|
if ((dbmp) == 0) closed_dbm();\
|
|
|
|
if ((dbmp)->di_dbm == 0) closed_dbm();\
|
2002-09-24 02:00:03 -04:00
|
|
|
} while (0)
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
#define GetDBM2(obj, data, dbm) {\
|
2011-02-15 07:38:11 -05:00
|
|
|
GetDBM((obj), (data));\
|
2004-12-08 21:19:50 -05:00
|
|
|
(dbm) = dbmp->di_dbm;\
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
static void
|
2006-02-12 23:53:22 -05:00
|
|
|
free_dbm(struct dbmdata *dbmp)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
2001-02-13 00:09:11 -05:00
|
|
|
if (dbmp) {
|
2006-09-19 20:30:38 -04:00
|
|
|
if (dbmp->di_dbm) gdbm_close(dbmp->di_dbm);
|
* array.c, bignum.c, cont.c, dir.c, dln.c, encoding.c, enumerator.c,
enumerator.c (enumerator_allocate), eval_jump.c, file.c, hash.c,
io.c, load.c, pack.c, proc.c, random.c, re.c, ruby.c, st.c,
string.c, thread.c, thread_pthread.c, time.c, util.c, variable.c,
vm.c, gc.c:
allocated memory objects by xmalloc (ruby_xmalloc) should be
freed by xfree (ruby_xfree).
* ext/curses/curses.c, ext/dbm/dbm.c, ext/digest/digest.c,
ext/gdbm/gdbm.c, ext/json/ext/parser/parser.c,
ext/json/ext/parser/unicode.c, ext/openssl/ossl_cipher.c,
ext/openssl/ossl_hmac.c, ext/openssl/ossl_pkey_ec.c,
ext/sdbm/init.c, ext/strscan/strscan.c, ext/zlib/zlib.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17017 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-06-08 06:01:40 -04:00
|
|
|
xfree(dbmp);
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.close -> nil
|
|
|
|
*
|
|
|
|
* Closes the associated database file.
|
|
|
|
*/
|
2002-02-26 23:52:21 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_close(VALUE obj)
|
2002-02-26 23:52:21 -05:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
|
|
|
|
GetDBM(obj, dbmp);
|
|
|
|
gdbm_close(dbmp->di_dbm);
|
|
|
|
dbmp->di_dbm = 0;
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
2001-02-13 00:09:11 -05:00
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.closed? -> true or false
|
|
|
|
*
|
|
|
|
* Returns true if the associated database file has been closed.
|
|
|
|
*/
|
2005-06-20 04:17:53 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_closed(VALUE obj)
|
2005-06-20 04:17:53 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
|
|
|
|
Data_Get_Struct(obj, struct dbmdata, dbmp);
|
|
|
|
if (dbmp == 0)
|
2006-09-19 20:30:38 -04:00
|
|
|
return Qtrue;
|
2005-06-20 04:17:53 -04:00
|
|
|
if (dbmp->di_dbm == 0)
|
2006-09-19 20:30:38 -04:00
|
|
|
return Qtrue;
|
2005-06-20 04:17:53 -04:00
|
|
|
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_s_alloc(VALUE klass)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
2002-09-24 02:00:03 -04:00
|
|
|
return Data_Wrap_Struct(klass, 0, free_dbm, 0);
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* GDBM.new(filename, mode = 0666, flags = nil)
|
|
|
|
*
|
|
|
|
* Creates a new GDBM instance by opening a gdbm file named _filename_.
|
|
|
|
* If the file does not exist, a new file with file mode _mode_ will be
|
|
|
|
* created. _flags_ may be one of the following:
|
|
|
|
* * *READER* - open as a reader
|
|
|
|
* * *WRITER* - open as a writer
|
|
|
|
* * *WRCREAT* - open as a writer; if the database does not exist, create a new one
|
|
|
|
* * *NEWDB* - open as a writer; overwrite any existing databases
|
|
|
|
*
|
|
|
|
* The values *WRITER*, *WRCREAT* and *NEWDB* may be combined with the following
|
|
|
|
* values by bitwise or:
|
|
|
|
* * *SYNC* - cause all database operations to be synchronized to the disk
|
|
|
|
* * *NOLOCK* - do not lock the database file
|
|
|
|
*
|
|
|
|
* If no _flags_ are specified, the GDBM object will try to open the database
|
|
|
|
* file as a writer and will create it if it does not already exist
|
|
|
|
* (cf. flag <tt>WRCREAT</tt>). If this fails (for instance, if another process
|
|
|
|
* has already opened the database as a reader), it will try to open the
|
|
|
|
* database file as a reader (cf. flag <tt>READER</tt>).
|
|
|
|
*/
|
2001-02-13 00:09:11 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_initialize(int argc, VALUE *argv, VALUE obj)
|
2001-02-13 00:09:11 -05:00
|
|
|
{
|
|
|
|
VALUE file, vmode, vflags;
|
1999-08-13 01:37:52 -04:00
|
|
|
GDBM_FILE dbm;
|
|
|
|
struct dbmdata *dbmp;
|
2001-02-13 00:09:11 -05:00
|
|
|
int mode, flags = 0;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2001-02-13 00:09:11 -05:00
|
|
|
if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
|
2006-09-19 20:30:38 -04:00
|
|
|
mode = 0666; /* default value */
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
else if (NIL_P(vmode)) {
|
2006-09-19 20:30:38 -04:00
|
|
|
mode = -1; /* return nil if DB does not exist */
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
else {
|
2006-09-19 20:30:38 -04:00
|
|
|
mode = NUM2INT(vmode);
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
2001-02-13 00:09:11 -05:00
|
|
|
|
|
|
|
if (!NIL_P(vflags))
|
|
|
|
flags = NUM2INT(vflags);
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
SafeStringValue(file);
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2011-11-11 07:14:23 -05:00
|
|
|
#ifdef GDBM_CLOEXEC
|
2011-11-13 07:32:15 -05:00
|
|
|
/* GDBM_CLOEXEC is available since gdbm 1.10. */
|
2011-11-11 07:14:23 -05:00
|
|
|
flags |= GDBM_CLOEXEC;
|
|
|
|
#endif
|
|
|
|
|
2004-04-15 04:15:27 -04:00
|
|
|
if (flags & RUBY_GDBM_RW_BIT) {
|
|
|
|
flags &= ~RUBY_GDBM_RW_BIT;
|
2010-04-22 04:04:13 -04:00
|
|
|
dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
|
2006-09-19 20:30:38 -04:00
|
|
|
flags, mode, MY_FATAL_FUNC);
|
2004-04-15 04:15:27 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
dbm = 0;
|
|
|
|
if (mode >= 0)
|
2010-04-22 04:04:13 -04:00
|
|
|
dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
|
2004-04-15 04:15:27 -04:00
|
|
|
GDBM_WRCREAT|flags, mode, MY_FATAL_FUNC);
|
|
|
|
if (!dbm)
|
2010-04-22 04:04:13 -04:00
|
|
|
dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
|
2004-04-15 04:15:27 -04:00
|
|
|
GDBM_WRITER|flags, 0, MY_FATAL_FUNC);
|
|
|
|
if (!dbm)
|
2010-04-22 04:04:13 -04:00
|
|
|
dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
|
2004-04-15 04:15:27 -04:00
|
|
|
GDBM_READER|flags, 0, MY_FATAL_FUNC);
|
|
|
|
}
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2011-11-07 08:05:43 -05:00
|
|
|
if (dbm) {
|
|
|
|
rb_fd_fix_cloexec(gdbm_fdesc(dbm));
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
if (!dbm) {
|
2006-09-19 20:30:38 -04:00
|
|
|
if (mode == -1) return Qnil;
|
|
|
|
|
|
|
|
if (gdbm_errno == GDBM_FILE_OPEN_ERROR ||
|
|
|
|
gdbm_errno == GDBM_CANT_BE_READER ||
|
|
|
|
gdbm_errno == GDBM_CANT_BE_WRITER)
|
2012-02-26 20:50:27 -05:00
|
|
|
rb_sys_fail_str(file);
|
2006-09-19 20:30:38 -04:00
|
|
|
else
|
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
|
2001-02-13 00:09:11 -05:00
|
|
|
dbmp = ALLOC(struct dbmdata);
|
2002-09-24 02:00:03 -04:00
|
|
|
free_dbm(DATA_PTR(obj));
|
2001-02-13 00:09:11 -05:00
|
|
|
DATA_PTR(obj) = dbmp;
|
1999-08-13 01:37:52 -04:00
|
|
|
dbmp->di_dbm = dbm;
|
|
|
|
dbmp->di_size = -1;
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* GDBM.open(filename, mode = 0666, flags = nil)
|
|
|
|
* GDBM.open(filename, mode = 0666, flags = nil) { |gdbm| ... }
|
|
|
|
*
|
|
|
|
* If called without a block, this is synonymous to GDBM::new.
|
|
|
|
* If a block is given, the new GDBM instance will be passed to the block
|
|
|
|
* as a parameter, and the corresponding database file will be closed
|
|
|
|
* after the execution of the block code has been finished.
|
|
|
|
*
|
|
|
|
* Example for an open call with a block:
|
|
|
|
*
|
|
|
|
* require 'gdbm'
|
|
|
|
* GDBM.open("fruitstore.db") do |gdbm|
|
|
|
|
* gdbm.each_pair do |key, value|
|
|
|
|
* print "#{key}: #{value}\n"
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*/
|
2001-02-13 00:09:11 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_s_open(int argc, VALUE *argv, VALUE klass)
|
2001-02-13 00:09:11 -05:00
|
|
|
{
|
|
|
|
VALUE obj = Data_Wrap_Struct(klass, 0, free_dbm, 0);
|
|
|
|
|
|
|
|
if (NIL_P(fgdbm_initialize(argc, argv, obj))) {
|
2006-09-19 20:30:38 -04:00
|
|
|
return Qnil;
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rb_block_given_p()) {
|
|
|
|
return rb_ensure(rb_yield, obj, fgdbm_close, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
rb_gdbm_fetch(GDBM_FILE dbm, datum key)
|
2001-02-13 00:09:11 -05:00
|
|
|
{
|
|
|
|
datum val;
|
2001-12-13 03:19:09 -05:00
|
|
|
VALUE str;
|
2001-02-13 00:09:11 -05:00
|
|
|
|
|
|
|
val = gdbm_fetch(dbm, key);
|
2001-05-02 00:22:21 -04:00
|
|
|
if (val.dptr == 0)
|
|
|
|
return Qnil;
|
2001-02-13 00:09:11 -05:00
|
|
|
|
2006-08-31 06:30:33 -04:00
|
|
|
str = rb_str_new(val.dptr, val.dsize);
|
|
|
|
free(val.dptr);
|
2001-05-02 00:22:21 -04:00
|
|
|
OBJ_TAINT(str);
|
2009-01-05 01:53:42 -05:00
|
|
|
return str;
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
rb_gdbm_fetch2(GDBM_FILE dbm, VALUE keystr)
|
2001-05-02 00:22:21 -04:00
|
|
|
{
|
|
|
|
datum key;
|
2011-11-08 09:34:39 -05:00
|
|
|
long len;
|
2001-05-02 00:22:21 -04:00
|
|
|
|
|
|
|
StringValue(keystr);
|
2011-11-08 09:34:39 -05:00
|
|
|
len = RSTRING_LEN(keystr);
|
|
|
|
if (TOO_LONG(len)) return Qnil;
|
2006-08-31 06:30:33 -04:00
|
|
|
key.dptr = RSTRING_PTR(keystr);
|
2011-11-08 09:34:39 -05:00
|
|
|
key.dsize = (int)len;
|
2001-05-02 00:22:21 -04:00
|
|
|
|
|
|
|
return rb_gdbm_fetch(dbm, key);
|
|
|
|
}
|
|
|
|
|
2001-05-06 11:06:00 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
rb_gdbm_fetch3(VALUE obj, VALUE keystr)
|
2001-05-06 11:06:00 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-06 11:06:00 -04:00
|
|
|
return rb_gdbm_fetch2(dbm, keystr);
|
|
|
|
}
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
rb_gdbm_firstkey(GDBM_FILE dbm)
|
2001-05-02 00:22:21 -04:00
|
|
|
{
|
|
|
|
datum key;
|
2001-12-13 03:19:09 -05:00
|
|
|
VALUE str;
|
2001-05-02 00:22:21 -04:00
|
|
|
|
|
|
|
key = gdbm_firstkey(dbm);
|
|
|
|
if (key.dptr == 0)
|
|
|
|
return Qnil;
|
|
|
|
|
2006-08-31 06:30:33 -04:00
|
|
|
str = rb_str_new(key.dptr, key.dsize);
|
|
|
|
free(key.dptr);
|
2001-05-02 00:22:21 -04:00
|
|
|
OBJ_TAINT(str);
|
2002-02-26 23:52:21 -05:00
|
|
|
return str;
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
rb_gdbm_nextkey(GDBM_FILE dbm, VALUE keystr)
|
2001-05-02 00:22:21 -04:00
|
|
|
{
|
|
|
|
datum key, key2;
|
2001-12-13 03:19:09 -05:00
|
|
|
VALUE str;
|
2011-11-08 09:34:39 -05:00
|
|
|
long len;
|
2001-05-02 00:22:21 -04:00
|
|
|
|
2011-11-08 09:34:39 -05:00
|
|
|
len = RSTRING_LEN(keystr);
|
|
|
|
if (TOO_LONG(len)) return Qnil;
|
2006-08-31 06:30:33 -04:00
|
|
|
key.dptr = RSTRING_PTR(keystr);
|
2011-11-08 09:34:39 -05:00
|
|
|
key.dsize = (int)len;
|
2001-05-02 00:22:21 -04:00
|
|
|
key2 = gdbm_nextkey(dbm, key);
|
|
|
|
if (key2.dptr == 0)
|
|
|
|
return Qnil;
|
|
|
|
|
2006-08-31 06:30:33 -04:00
|
|
|
str = rb_str_new(key2.dptr, key2.dsize);
|
2008-11-26 01:39:14 -05:00
|
|
|
free(key2.dptr);
|
2001-05-02 00:22:21 -04:00
|
|
|
OBJ_TAINT(str);
|
2002-02-26 23:52:21 -05:00
|
|
|
return str;
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE valstr;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2001-05-06 11:06:00 -04:00
|
|
|
valstr = rb_gdbm_fetch3(obj, keystr);
|
2001-05-02 00:22:21 -04:00
|
|
|
if (NIL_P(valstr)) {
|
2006-09-19 20:30:38 -04:00
|
|
|
if (ifnone == Qnil && rb_block_given_p())
|
|
|
|
return rb_yield(keystr);
|
|
|
|
return ifnone;
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
2001-05-02 00:22:21 -04:00
|
|
|
return valstr;
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm[key] -> value
|
|
|
|
*
|
|
|
|
* Retrieves the _value_ corresponding to _key_.
|
|
|
|
*/
|
2000-07-04 03:04:42 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_aref(VALUE obj, VALUE keystr)
|
2000-07-04 03:04:42 -04:00
|
|
|
{
|
2001-05-06 11:06:00 -04:00
|
|
|
return rb_gdbm_fetch3(obj, keystr);
|
2000-07-04 03:04:42 -04:00
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.fetch(key [, default]) -> value
|
|
|
|
*
|
|
|
|
* Retrieves the _value_ corresponding to _key_. If there is no value
|
|
|
|
* associated with _key_, _default_ will be returned instead.
|
|
|
|
*/
|
2000-07-04 03:04:42 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_fetch_m(int argc, VALUE *argv, VALUE obj)
|
2000-07-04 03:04:42 -04:00
|
|
|
{
|
2001-02-13 00:09:11 -05:00
|
|
|
VALUE keystr, valstr, ifnone;
|
2000-07-04 03:04:42 -04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &keystr, &ifnone);
|
2001-02-13 00:09:11 -05:00
|
|
|
valstr = fgdbm_fetch(obj, keystr, ifnone);
|
|
|
|
if (argc == 1 && !rb_block_given_p() && NIL_P(valstr))
|
2006-09-19 20:30:38 -04:00
|
|
|
rb_raise(rb_eIndexError, "key not found");
|
2001-02-13 00:09:11 -05:00
|
|
|
|
|
|
|
return valstr;
|
2000-07-04 03:04:42 -04:00
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-11-07 22:15:28 -05:00
|
|
|
* gdbm.key(value) -> key
|
2006-09-19 20:30:38 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2000-07-04 03:04:42 -04:00
|
|
|
static VALUE
|
2008-11-07 22:15:28 -05:00
|
|
|
fgdbm_key(VALUE obj, VALUE valstr)
|
2000-07-04 03:04:42 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE keystr, valstr2;
|
2000-07-04 03:04:42 -04:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(valstr);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
valstr2 = rb_gdbm_fetch2(dbm, keystr);
|
2001-05-02 00:22:21 -04:00
|
|
|
if (!NIL_P(valstr2) &&
|
2011-03-21 17:23:42 -04:00
|
|
|
(int)RSTRING_LEN(valstr) == (int)RSTRING_LEN(valstr2) &&
|
2006-08-31 06:30:33 -04:00
|
|
|
memcmp(RSTRING_PTR(valstr), RSTRING_PTR(valstr2),
|
2011-03-21 17:23:42 -04:00
|
|
|
(int)RSTRING_LEN(valstr)) == 0) {
|
2006-09-19 20:30:38 -04:00
|
|
|
return keystr;
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
2000-07-04 03:04:42 -04:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2008-11-07 22:15:28 -05:00
|
|
|
/* :nodoc: */
|
|
|
|
static VALUE
|
|
|
|
fgdbm_index(VALUE obj, VALUE value)
|
|
|
|
{
|
|
|
|
rb_warn("GDBM#index is deprecated; use GDBM#key");
|
|
|
|
return fgdbm_key(obj, value);
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-10-11 02:54:54 -04:00
|
|
|
* gdbm.select { |key, value| block } -> array
|
2006-09-19 20:30:38 -04:00
|
|
|
*
|
2009-10-11 02:54:54 -04:00
|
|
|
* Returns a new array of all key-value pairs of the database for which _block_
|
2006-09-19 20:30:38 -04:00
|
|
|
* evaluates to true.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_select(VALUE obj)
|
2002-02-26 23:52:21 -05:00
|
|
|
{
|
2004-06-11 09:33:47 -04:00
|
|
|
VALUE new = rb_ary_new();
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
VALUE keystr;
|
2002-02-26 23:52:21 -05:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2004-06-11 09:33:47 -04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
2006-09-19 20:30:38 -04:00
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
VALUE assoc = rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr));
|
|
|
|
VALUE v = rb_yield(assoc);
|
2003-05-06 02:51:31 -04:00
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
if (RTEST(v)) {
|
|
|
|
rb_ary_push(new, assoc);
|
|
|
|
}
|
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2002-02-26 23:52:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.values_at(key, ...) -> array
|
|
|
|
*
|
|
|
|
* Returns an array of the values associated with each specified _key_.
|
|
|
|
*/
|
2003-05-06 02:51:31 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_values_at(int argc, VALUE *argv, VALUE obj)
|
2003-05-06 02:51:31 -04:00
|
|
|
{
|
|
|
|
VALUE new = rb_ary_new2(argc);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
rb_ary_push(new, rb_gdbm_fetch3(obj, argv[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
static void
|
2006-02-12 23:53:22 -05:00
|
|
|
rb_gdbm_modify(VALUE obj)
|
2004-05-07 04:44:24 -04:00
|
|
|
{
|
2004-05-08 04:12:08 -04:00
|
|
|
if (OBJ_FROZEN(obj)) rb_error_frozen("GDBM");
|
2004-05-07 04:44:24 -04:00
|
|
|
}
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
rb_gdbm_delete(VALUE obj, VALUE keystr)
|
2001-05-02 00:22:21 -04:00
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2011-11-08 09:34:39 -05:00
|
|
|
long len;
|
2001-05-02 00:22:21 -04:00
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
rb_gdbm_modify(obj);
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(keystr);
|
2011-11-08 09:34:39 -05:00
|
|
|
len = RSTRING_LEN(keystr);
|
|
|
|
if (TOO_LONG(len)) return Qnil;
|
2006-08-31 06:30:33 -04:00
|
|
|
key.dptr = RSTRING_PTR(keystr);
|
2011-11-08 09:34:39 -05:00
|
|
|
key.dsize = (int)len;
|
2001-05-02 00:22:21 -04:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
if (!gdbm_exists(dbm, key)) {
|
2006-09-19 20:30:38 -04:00
|
|
|
return Qnil;
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gdbm_delete(dbm, key)) {
|
2006-09-19 20:30:38 -04:00
|
|
|
dbmp->di_size = -1;
|
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
|
|
|
else if (dbmp->di_size >= 0) {
|
2006-09-19 20:30:38 -04:00
|
|
|
dbmp->di_size--;
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.delete(key) -> value or nil
|
|
|
|
*
|
|
|
|
* Removes the key-value-pair with the specified _key_ from this database and
|
|
|
|
* returns the corresponding _value_. Returns nil if the database is empty.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_delete(VALUE obj, VALUE keystr)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
2001-05-06 11:06:00 -04:00
|
|
|
VALUE valstr;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2001-05-06 11:06:00 -04:00
|
|
|
valstr = fgdbm_fetch(obj, keystr, Qnil);
|
|
|
|
rb_gdbm_delete(obj, keystr);
|
|
|
|
return valstr;
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.shift -> (key, value) or nil
|
|
|
|
*
|
2010-04-22 04:04:13 -04:00
|
|
|
* Removes a key-value-pair from this database and returns it as a
|
2006-09-19 20:30:38 -04:00
|
|
|
* two-item array [ _key_, _value_ ]. Returns nil if the database is empty.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_shift(VALUE obj)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
VALUE keystr, valstr;
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
rb_gdbm_modify(obj);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
keystr = rb_gdbm_firstkey(dbm);
|
|
|
|
if (NIL_P(keystr)) return Qnil;
|
|
|
|
valstr = rb_gdbm_fetch2(dbm, keystr);
|
|
|
|
rb_gdbm_delete(obj, keystr);
|
1999-08-13 01:37:52 -04:00
|
|
|
|
|
|
|
return rb_assoc_new(keystr, valstr);
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.delete_if { |key, value| block } -> gdbm
|
|
|
|
* gdbm.reject! { |key, value| block } -> gdbm
|
|
|
|
*
|
|
|
|
* Deletes every key-value pair from _gdbm_ for which _block_ evaluates to true.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_delete_if(VALUE obj)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
VALUE keystr, valstr;
|
2011-11-08 09:34:39 -05:00
|
|
|
VALUE ret, ary = rb_ary_tmp_new(0);
|
2001-05-02 00:22:21 -04:00
|
|
|
int i, status = 0, n;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
rb_gdbm_modify(obj);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
n = dbmp->di_size;
|
|
|
|
dbmp->di_size = -1;
|
|
|
|
|
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
2011-11-08 09:34:39 -05:00
|
|
|
OBJ_FREEZE(keystr);
|
2006-09-19 20:30:38 -04:00
|
|
|
valstr = rb_gdbm_fetch2(dbm, keystr);
|
2011-11-08 09:34:39 -05:00
|
|
|
ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status);
|
2001-05-02 00:22:21 -04:00
|
|
|
if (status != 0) break;
|
2006-09-19 20:30:38 -04:00
|
|
|
if (RTEST(ret)) rb_ary_push(ary, keystr);
|
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
2001-05-02 00:22:21 -04:00
|
|
|
|
2006-09-02 10:42:08 -04:00
|
|
|
for (i = 0; i < RARRAY_LEN(ary); i++)
|
|
|
|
rb_gdbm_delete(obj, RARRAY_PTR(ary)[i]);
|
2001-05-02 00:22:21 -04:00
|
|
|
if (status) rb_jump_tag(status);
|
2011-03-21 17:23:42 -04:00
|
|
|
if (n > 0) dbmp->di_size = n - (int)RARRAY_LEN(ary);
|
2011-11-08 09:34:39 -05:00
|
|
|
rb_ary_clear(ary);
|
2001-05-02 00:22:21 -04:00
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.clear -> gdbm
|
|
|
|
*
|
|
|
|
* Removes all the key-value pairs within _gdbm_.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_clear(VALUE obj)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
2001-05-06 11:06:00 -04:00
|
|
|
datum key, nextkey;
|
1999-08-13 01:37:52 -04:00
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
rb_gdbm_modify(obj);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 01:37:52 -04:00
|
|
|
dbmp->di_size = -1;
|
2001-05-02 00:22:21 -04:00
|
|
|
|
2001-05-06 11:06:00 -04:00
|
|
|
#if 0
|
2001-05-02 00:22:21 -04:00
|
|
|
while (key = gdbm_firstkey(dbm), key.dptr) {
|
2006-09-19 20:30:38 -04:00
|
|
|
if (gdbm_delete(dbm, key)) {
|
|
|
|
free(key.dptr);
|
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
|
|
|
}
|
2010-04-22 04:04:13 -04:00
|
|
|
free(key.dptr);
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
2001-05-06 11:06:00 -04:00
|
|
|
#else
|
|
|
|
while (key = gdbm_firstkey(dbm), key.dptr) {
|
|
|
|
for (; key.dptr; key = nextkey) {
|
|
|
|
nextkey = gdbm_nextkey(dbm, key);
|
|
|
|
if (gdbm_delete(dbm, key)) {
|
|
|
|
free(key.dptr);
|
|
|
|
if (nextkey.dptr) free(nextkey.dptr);
|
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
|
|
|
}
|
|
|
|
free(key.dptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2001-05-02 00:22:21 -04:00
|
|
|
dbmp->di_size = 0;
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.invert -> hash
|
|
|
|
*
|
|
|
|
* Returns a hash created by using _gdbm_'s values as keys, and the keys
|
|
|
|
* as values.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_invert(VALUE obj)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
VALUE keystr, valstr;
|
|
|
|
VALUE hash = rb_hash_new();
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
2006-09-19 20:30:38 -04:00
|
|
|
valstr = rb_gdbm_fetch2(dbm, keystr);
|
2001-05-02 00:22:21 -04:00
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
rb_hash_aset(hash, valstr, keystr);
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
2000-04-10 01:48:43 -04:00
|
|
|
return hash;
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm[key]= value -> value
|
|
|
|
* gdbm.store(key, value) -> value
|
|
|
|
*
|
|
|
|
* Associates the value _value_ with the specified _key_.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
rb_gdbm_modify(obj);
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(keystr);
|
2004-11-14 10:41:40 -05:00
|
|
|
StringValue(valstr);
|
|
|
|
|
2006-08-31 06:30:33 -04:00
|
|
|
key.dptr = RSTRING_PTR(keystr);
|
2011-11-08 09:34:39 -05:00
|
|
|
key.dsize = RSTRING_LENINT(keystr);
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2006-08-31 06:30:33 -04:00
|
|
|
val.dptr = RSTRING_PTR(valstr);
|
2011-11-08 09:34:39 -05:00
|
|
|
val.dsize = RSTRING_LENINT(valstr);
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 01:37:52 -04:00
|
|
|
dbmp->di_size = -1;
|
|
|
|
if (gdbm_store(dbm, key, val, GDBM_REPLACE)) {
|
2006-09-19 20:30:38 -04:00
|
|
|
if (errno == EPERM) rb_sys_fail(0);
|
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return valstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
update_i(VALUE pair, VALUE dbm)
|
|
|
|
{
|
|
|
|
Check_Type(pair, T_ARRAY);
|
2006-09-02 10:42:08 -04:00
|
|
|
if (RARRAY_LEN(pair) < 2) {
|
2006-09-19 20:30:38 -04:00
|
|
|
rb_raise(rb_eArgError, "pair must be [key, value]");
|
2006-02-12 23:53:22 -05:00
|
|
|
}
|
2006-09-02 10:42:08 -04:00
|
|
|
fgdbm_store(dbm, RARRAY_PTR(pair)[0], RARRAY_PTR(pair)[1]);
|
2006-02-12 23:53:22 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.update(other) -> gdbm
|
|
|
|
*
|
|
|
|
* Adds the key-value pairs of _other_ to _gdbm_, overwriting entries with
|
|
|
|
* duplicate keys with those from _other_. _other_ must have an each_pair
|
|
|
|
* method.
|
|
|
|
*/
|
2006-02-12 23:53:22 -05:00
|
|
|
static VALUE
|
|
|
|
fgdbm_update(VALUE obj, VALUE other)
|
|
|
|
{
|
|
|
|
rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.replace(other) -> gdbm
|
|
|
|
*
|
|
|
|
* Replaces the content of _gdbm_ with the key-value pairs of _other_.
|
|
|
|
* _other_ must have an each_pair method.
|
|
|
|
*/
|
2006-02-12 23:53:22 -05:00
|
|
|
static VALUE
|
|
|
|
fgdbm_replace(VALUE obj, VALUE other)
|
|
|
|
{
|
|
|
|
fgdbm_clear(obj);
|
|
|
|
rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.length -> fixnum
|
|
|
|
* gdbm.size -> fixnum
|
|
|
|
*
|
|
|
|
* Returns the number of key-value pairs in this database.
|
|
|
|
*/
|
2006-02-12 23:53:22 -05:00
|
|
|
static VALUE
|
|
|
|
fgdbm_length(VALUE obj)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
2001-05-02 00:22:21 -04:00
|
|
|
datum key, nextkey;
|
1999-08-13 01:37:52 -04:00
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
int i = 0;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 01:37:52 -04:00
|
|
|
if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
|
|
|
|
nextkey = gdbm_nextkey(dbm, key);
|
|
|
|
free(key.dptr);
|
2006-09-19 20:30:38 -04:00
|
|
|
i++;
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
dbmp->di_size = i;
|
|
|
|
|
|
|
|
return INT2FIX(i);
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.empty? -> true or false
|
|
|
|
*
|
|
|
|
* Returns true if the database is empty.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_empty_p(VALUE obj)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2000-06-16 03:44:16 -04:00
|
|
|
GetDBM(obj, dbmp);
|
1999-08-13 01:37:52 -04:00
|
|
|
if (dbmp->di_size < 0) {
|
2006-09-19 20:30:38 -04:00
|
|
|
dbm = dbmp->di_dbm;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
key = gdbm_firstkey(dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
if (key.dptr) {
|
|
|
|
free(key.dptr);
|
|
|
|
return Qfalse;
|
2006-09-19 20:30:38 -04:00
|
|
|
}
|
2001-05-02 00:22:21 -04:00
|
|
|
return Qtrue;
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
2001-05-02 00:22:21 -04:00
|
|
|
|
|
|
|
if (dbmp->di_size == 0) return Qtrue;
|
1999-08-13 01:37:52 -04:00
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.each_value { |value| block } -> gdbm
|
|
|
|
*
|
|
|
|
* Executes _block_ for each key in the database, passing the corresponding
|
|
|
|
* _value_ as a parameter.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_each_value(VALUE obj)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE keystr;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
* ext/dbm/dbm.c (fdbm_each_value, fdbm_each_key, fdbm_each_pair):
GDBM#{each,each_pair,each_key,each_value}: Return an enumerator
if no block is given.
* ext/gdbm/gdbm.c (fgdbm_each_value, fgdbm_each_key,
fgdbm_each_pair): GDBM#{each,each_pair,each_key,each_value}:
Return an enumerator if no block is given.
* ext/openssl/ossl_config.c (ossl_config_each):
OpenSSL::Config#each: Return an enumerator if no block is given.
* ext/readline/readline.c (hist_each): Readline::HISTORY#each:
Return an enumerator if no block is given.
* ext/sdbm/init.c (fsdbm_each_value, fsdbm_each_key,
fsdbm_each_pair): SDBM#{each,each_pair,each_key,each_value}:
Return an enumerator if no block is given.
* ext/stringio/stringio.c (strio_each_byte, strio_each):
StringIO#{each,each_line,each_byte}: Return an enumerator if no
block is given.
* ext/stringio/stringio.c (Init_stringio): Add #lines and #bytes,
which are aliases to #each_line and #each_byte, respectively.
* ext/win32ole/win32ole.c (fole_each): WIN32OLE#each: Return an
enumerator if no block is given.
* ext/zlib/zlib.c (rb_gzreader_each_byte, rb_gzreader_each):
Zlib::GzipReader#{each,each_line,each_byte}: Return an
enumerator if no block is given.
* ext/zlib/zlib.c (Init_zlib): Add Zlib::GzipReader#lines and
#bytes, which are aliases to #each_line and #each_byte,
respectively.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16125 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-04-21 04:48:33 -04:00
|
|
|
RETURN_ENUMERATOR(obj, 0, 0);
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
|
|
|
rb_yield(rb_gdbm_fetch2(dbm, keystr));
|
2006-09-19 20:30:38 -04:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.each_key { |key| block } -> gdbm
|
|
|
|
*
|
|
|
|
* Executes _block_ for each key in the database, passing the
|
|
|
|
* _key_ as a parameter.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_each_key(VALUE obj)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE keystr;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
* ext/dbm/dbm.c (fdbm_each_value, fdbm_each_key, fdbm_each_pair):
GDBM#{each,each_pair,each_key,each_value}: Return an enumerator
if no block is given.
* ext/gdbm/gdbm.c (fgdbm_each_value, fgdbm_each_key,
fgdbm_each_pair): GDBM#{each,each_pair,each_key,each_value}:
Return an enumerator if no block is given.
* ext/openssl/ossl_config.c (ossl_config_each):
OpenSSL::Config#each: Return an enumerator if no block is given.
* ext/readline/readline.c (hist_each): Readline::HISTORY#each:
Return an enumerator if no block is given.
* ext/sdbm/init.c (fsdbm_each_value, fsdbm_each_key,
fsdbm_each_pair): SDBM#{each,each_pair,each_key,each_value}:
Return an enumerator if no block is given.
* ext/stringio/stringio.c (strio_each_byte, strio_each):
StringIO#{each,each_line,each_byte}: Return an enumerator if no
block is given.
* ext/stringio/stringio.c (Init_stringio): Add #lines and #bytes,
which are aliases to #each_line and #each_byte, respectively.
* ext/win32ole/win32ole.c (fole_each): WIN32OLE#each: Return an
enumerator if no block is given.
* ext/zlib/zlib.c (rb_gzreader_each_byte, rb_gzreader_each):
Zlib::GzipReader#{each,each_line,each_byte}: Return an
enumerator if no block is given.
* ext/zlib/zlib.c (Init_zlib): Add Zlib::GzipReader#lines and
#bytes, which are aliases to #each_line and #each_byte,
respectively.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16125 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-04-21 04:48:33 -04:00
|
|
|
RETURN_ENUMERATOR(obj, 0, 0);
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_yield(keystr);
|
2006-09-19 20:30:38 -04:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.each_pair { |key, value| block } -> gdbm
|
|
|
|
*
|
|
|
|
* Executes _block_ for each key in the database, passing the _key_ and the
|
|
|
|
* correspoding _value_ as a parameter.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_each_pair(VALUE obj)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
struct dbmdata *dbmp;
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE keystr;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
* ext/dbm/dbm.c (fdbm_each_value, fdbm_each_key, fdbm_each_pair):
GDBM#{each,each_pair,each_key,each_value}: Return an enumerator
if no block is given.
* ext/gdbm/gdbm.c (fgdbm_each_value, fgdbm_each_key,
fgdbm_each_pair): GDBM#{each,each_pair,each_key,each_value}:
Return an enumerator if no block is given.
* ext/openssl/ossl_config.c (ossl_config_each):
OpenSSL::Config#each: Return an enumerator if no block is given.
* ext/readline/readline.c (hist_each): Readline::HISTORY#each:
Return an enumerator if no block is given.
* ext/sdbm/init.c (fsdbm_each_value, fsdbm_each_key,
fsdbm_each_pair): SDBM#{each,each_pair,each_key,each_value}:
Return an enumerator if no block is given.
* ext/stringio/stringio.c (strio_each_byte, strio_each):
StringIO#{each,each_line,each_byte}: Return an enumerator if no
block is given.
* ext/stringio/stringio.c (Init_stringio): Add #lines and #bytes,
which are aliases to #each_line and #each_byte, respectively.
* ext/win32ole/win32ole.c (fole_each): WIN32OLE#each: Return an
enumerator if no block is given.
* ext/zlib/zlib.c (rb_gzreader_each_byte, rb_gzreader_each):
Zlib::GzipReader#{each,each_line,each_byte}: Return an
enumerator if no block is given.
* ext/zlib/zlib.c (Init_zlib): Add Zlib::GzipReader#lines and
#bytes, which are aliases to #each_line and #each_byte,
respectively.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16125 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-04-21 04:48:33 -04:00
|
|
|
RETURN_ENUMERATOR(obj, 0, 0);
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_yield(rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
|
2006-09-19 20:30:38 -04:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.keys -> array
|
|
|
|
*
|
|
|
|
* Returns an array of all keys of this database.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_keys(VALUE obj)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE keystr, ary;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 01:37:52 -04:00
|
|
|
ary = rb_ary_new();
|
2001-05-02 00:22:21 -04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
|
|
|
rb_ary_push(ary, keystr);
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.values -> array
|
|
|
|
*
|
|
|
|
* Returns an array of all values of this database.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_values(VALUE obj)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
2001-05-02 00:22:21 -04:00
|
|
|
datum key, nextkey;
|
1999-08-13 01:37:52 -04:00
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE valstr, ary;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 01:37:52 -04:00
|
|
|
ary = rb_ary_new();
|
2001-05-02 00:22:21 -04:00
|
|
|
for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
|
|
|
|
nextkey = gdbm_nextkey(dbm, key);
|
2006-09-19 20:30:38 -04:00
|
|
|
valstr = rb_gdbm_fetch(dbm, key);
|
2001-05-02 00:22:21 -04:00
|
|
|
free(key.dptr);
|
2006-09-19 20:30:38 -04:00
|
|
|
rb_ary_push(ary, valstr);
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-04-15 23:25:50 -04:00
|
|
|
* gdbm.include?(k) -> true or false
|
2006-09-19 20:30:38 -04:00
|
|
|
* gdbm.has_key?(k) -> true or false
|
2013-04-15 23:25:50 -04:00
|
|
|
* gdbm.member?(k) -> true or false
|
2006-09-19 20:30:38 -04:00
|
|
|
* gdbm.key?(k) -> true or false
|
|
|
|
*
|
|
|
|
* Returns true if the given key _k_ exists within the database.
|
|
|
|
* Returns false otherwise.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_has_key(VALUE obj, VALUE keystr)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
2001-02-08 04:19:27 -05:00
|
|
|
datum key;
|
1999-08-13 01:37:52 -04:00
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2011-11-08 09:34:39 -05:00
|
|
|
long len;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(keystr);
|
2011-11-08 09:34:39 -05:00
|
|
|
len = RSTRING_LENINT(keystr);
|
|
|
|
if (TOO_LONG(len)) return Qfalse;
|
2006-08-31 06:30:33 -04:00
|
|
|
key.dptr = RSTRING_PTR(keystr);
|
2011-11-08 09:34:39 -05:00
|
|
|
key.dsize = (int)len;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-02-08 04:19:27 -05:00
|
|
|
if (gdbm_exists(dbm, key))
|
|
|
|
return Qtrue;
|
1999-08-13 01:37:52 -04:00
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.has_value?(v) -> true or false
|
|
|
|
* gdbm.value?(v) -> true or false
|
|
|
|
*
|
|
|
|
* Returns true if the given value _v_ exists within the database.
|
|
|
|
* Returns false otherwise.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_has_value(VALUE obj, VALUE valstr)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE keystr, valstr2;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(valstr);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
valstr2 = rb_gdbm_fetch2(dbm, keystr);
|
2001-05-02 00:22:21 -04:00
|
|
|
|
|
|
|
if (!NIL_P(valstr2) &&
|
2011-03-21 17:23:42 -04:00
|
|
|
(int)RSTRING_LEN(valstr) == (int)RSTRING_LEN(valstr2) &&
|
2006-08-31 06:30:33 -04:00
|
|
|
memcmp(RSTRING_PTR(valstr), RSTRING_PTR(valstr2),
|
2011-03-21 17:23:42 -04:00
|
|
|
(int)RSTRING_LEN(valstr)) == 0) {
|
2006-09-19 20:30:38 -04:00
|
|
|
return Qtrue;
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.to_a -> array
|
|
|
|
*
|
|
|
|
* Returns an array of all key-value pairs contained in the database.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_to_a(VALUE obj)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE keystr, ary;
|
1999-08-13 01:37:52 -04:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 01:37:52 -04:00
|
|
|
ary = rb_ary_new();
|
2001-05-02 00:22:21 -04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_ary_push(ary, rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.reorganize -> gdbm
|
|
|
|
*
|
|
|
|
* Reorganizes the database file. This operation removes reserved space of
|
|
|
|
* elements that have already been deleted. It is only useful after a lot of
|
|
|
|
* deletions in the database.
|
|
|
|
*/
|
1999-08-13 01:37:52 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_reorganize(VALUE obj)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
rb_gdbm_modify(obj);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-08-13 01:37:52 -04:00
|
|
|
gdbm_reorganize(dbm);
|
2011-11-13 09:22:17 -05:00
|
|
|
rb_fd_fix_cloexec(gdbm_fdesc(dbm));
|
1999-08-13 01:37:52 -04:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.sync -> gdbm
|
|
|
|
*
|
|
|
|
* Unless the _gdbm_ object has been opened with the *SYNC* flag, it is not
|
|
|
|
* guarenteed that database modification operations are immediately applied to
|
|
|
|
* the database file. This method ensures that all recent modifications
|
|
|
|
* to the database are written to the file. Blocks until all writing operations
|
|
|
|
* to the disk have been finished.
|
|
|
|
*/
|
2001-02-13 00:09:11 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_sync(VALUE obj)
|
2001-02-13 00:09:11 -05:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
rb_gdbm_modify(obj);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-02-13 00:09:11 -05:00
|
|
|
gdbm_sync(dbm);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.cachesize = size -> size
|
|
|
|
*
|
|
|
|
* Sets the size of the internal bucket cache to _size_.
|
|
|
|
*/
|
2001-02-13 00:09:11 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_set_cachesize(VALUE obj, VALUE val)
|
2001-02-13 00:09:11 -05:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
int optval;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-02-13 00:09:11 -05:00
|
|
|
optval = FIX2INT(val);
|
|
|
|
if (gdbm_setopt(dbm, GDBM_CACHESIZE, &optval, sizeof(optval)) == -1) {
|
2006-09-19 20:30:38 -04:00
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.fastmode = boolean -> boolean
|
|
|
|
*
|
|
|
|
* Turns the database's fast mode on or off. If fast mode is turned on, gdbm
|
|
|
|
* does not wait for writes to be flushed to the disk before continuing.
|
|
|
|
*
|
|
|
|
* This option is obsolete for gdbm >= 1.8 since fast mode is turned on by
|
|
|
|
* default. See also: #syncmode=
|
|
|
|
*/
|
2001-02-13 00:09:11 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_set_fastmode(VALUE obj, VALUE val)
|
2001-02-13 00:09:11 -05:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
int optval;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-02-13 00:09:11 -05:00
|
|
|
optval = 0;
|
|
|
|
if (RTEST(val))
|
|
|
|
optval = 1;
|
|
|
|
|
|
|
|
if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) {
|
2006-09-19 20:30:38 -04:00
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.syncmode = boolean -> boolean
|
|
|
|
*
|
|
|
|
* Turns the database's synchronization mode on or off. If the synchronization
|
|
|
|
* mode is turned on, the database's in-memory state will be synchronized to
|
|
|
|
* disk after every database modification operation. If the synchronization
|
|
|
|
* mode is turned off, GDBM does not wait for writes to be flushed to the disk
|
|
|
|
* before continuing.
|
|
|
|
*
|
|
|
|
* This option is only available for gdbm >= 1.8 where syncmode is turned off
|
|
|
|
* by default. See also: #fastmode=
|
|
|
|
*/
|
2001-02-13 00:09:11 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_set_syncmode(VALUE obj, VALUE val)
|
2001-02-13 00:09:11 -05:00
|
|
|
{
|
|
|
|
#if !defined(GDBM_SYNCMODE)
|
|
|
|
fgdbm_set_fastmode(obj, RTEST(val) ? Qfalse : Qtrue);
|
|
|
|
return val;
|
|
|
|
#else
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
|
|
|
int optval;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-02-13 00:09:11 -05:00
|
|
|
optval = 0;
|
|
|
|
if (RTEST(val))
|
|
|
|
optval = 1;
|
|
|
|
|
|
|
|
if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) {
|
2006-09-19 20:30:38 -04:00
|
|
|
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.to_hash -> hash
|
|
|
|
*
|
|
|
|
* Returns a hash of all key-value pairs contained in the database.
|
|
|
|
*/
|
2000-07-04 03:04:42 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_to_hash(VALUE obj)
|
2000-07-04 03:04:42 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
GDBM_FILE dbm;
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE keystr, hash;
|
2000-07-04 03:04:42 -04:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2000-07-04 03:04:42 -04:00
|
|
|
hash = rb_hash_new();
|
2001-05-02 00:22:21 -04:00
|
|
|
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
|
|
|
|
keystr = rb_gdbm_nextkey(dbm, keystr)) {
|
|
|
|
|
|
|
|
rb_hash_aset(hash, keystr, rb_gdbm_fetch2(dbm, keystr));
|
2000-07-04 03:04:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* gdbm.reject { |key, value| block } -> hash
|
|
|
|
*
|
|
|
|
* Returns a hash copy of _gdbm_ where all key-value pairs from _gdbm_ for
|
|
|
|
* which _block_ evaluates to true are removed. See also: #delete_if
|
|
|
|
*/
|
2000-07-04 03:04:42 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fgdbm_reject(VALUE obj)
|
2000-07-04 03:04:42 -04:00
|
|
|
{
|
|
|
|
return rb_hash_delete_if(fgdbm_to_hash(obj));
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
void
|
2006-02-12 23:53:22 -05:00
|
|
|
Init_gdbm(void)
|
1999-08-13 01:37:52 -04:00
|
|
|
{
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_cGDBM = rb_define_class("GDBM", rb_cObject);
|
2001-02-13 00:09:11 -05:00
|
|
|
rb_eGDBMError = rb_define_class("GDBMError", rb_eStandardError);
|
2001-05-06 11:06:00 -04:00
|
|
|
rb_eGDBMFatalError = rb_define_class("GDBMFatalError", rb_eException);
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_include_module(rb_cGDBM, rb_mEnumerable);
|
|
|
|
|
* ext/curses/curses.c, ext/digest/digest.c, ext/dl/handle.c,
ext/dl/ptr.c, ext/dl/sym.c, ext/gdbm/gdbm.c, ext/iconv/iconv.c,
ext/stringio/stringio.c, ext/strscan/strscan.c,
ext/tcltklib/tcltklib.c, ext/win32ole/win32ole.c:
use rb_define_alloc_func().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3193 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-20 06:23:40 -05:00
|
|
|
rb_define_alloc_func(rb_cGDBM, fgdbm_s_alloc);
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_define_singleton_method(rb_cGDBM, "open", fgdbm_s_open, -1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cGDBM, "initialize", fgdbm_initialize, -1);
|
|
|
|
rb_define_method(rb_cGDBM, "close", fgdbm_close, 0);
|
2005-06-20 04:17:53 -04:00
|
|
|
rb_define_method(rb_cGDBM, "closed?", fgdbm_closed, 0);
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_define_method(rb_cGDBM, "[]", fgdbm_aref, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "fetch", fgdbm_fetch_m, -1);
|
|
|
|
rb_define_method(rb_cGDBM, "[]=", fgdbm_store, 2);
|
|
|
|
rb_define_method(rb_cGDBM, "store", fgdbm_store, 2);
|
|
|
|
rb_define_method(rb_cGDBM, "index", fgdbm_index, 1);
|
2008-11-07 22:15:28 -05:00
|
|
|
rb_define_method(rb_cGDBM, "key", fgdbm_key, 1);
|
2004-06-11 09:33:47 -04:00
|
|
|
rb_define_method(rb_cGDBM, "select", fgdbm_select, 0);
|
2003-05-06 02:51:31 -04:00
|
|
|
rb_define_method(rb_cGDBM, "values_at", fgdbm_values_at, -1);
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_define_method(rb_cGDBM, "length", fgdbm_length, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "size", fgdbm_length, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "empty?", fgdbm_empty_p, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "each", fgdbm_each_pair, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "each_value", fgdbm_each_value, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "each_key", fgdbm_each_key, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "each_pair", fgdbm_each_pair, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "keys", fgdbm_keys, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "values", fgdbm_values, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "shift", fgdbm_shift, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "delete", fgdbm_delete, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "delete_if", fgdbm_delete_if, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "reject!", fgdbm_delete_if, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "reject", fgdbm_reject, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "clear", fgdbm_clear, 0);
|
2006-09-19 20:30:38 -04:00
|
|
|
rb_define_method(rb_cGDBM, "invert", fgdbm_invert, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "update", fgdbm_update, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "replace", fgdbm_replace, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "reorganize", fgdbm_reorganize, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "sync", fgdbm_sync, 0);
|
|
|
|
/* rb_define_method(rb_cGDBM, "setopt", fgdbm_setopt, 2); */
|
|
|
|
rb_define_method(rb_cGDBM, "cachesize=", fgdbm_set_cachesize, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "fastmode=", fgdbm_set_fastmode, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "syncmode=", fgdbm_set_syncmode, 1);
|
2002-02-26 23:52:21 -05:00
|
|
|
|
|
|
|
rb_define_method(rb_cGDBM, "include?", fgdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "has_key?", fgdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "member?", fgdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "has_value?", fgdbm_has_value, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "key?", fgdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cGDBM, "value?", fgdbm_has_value, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cGDBM, "to_a", fgdbm_to_a, 0);
|
|
|
|
rb_define_method(rb_cGDBM, "to_hash", fgdbm_to_hash, 0);
|
2001-02-13 00:09:11 -05:00
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/* flag for #new and #open: open database as a reader */
|
2004-04-15 04:15:27 -04:00
|
|
|
rb_define_const(rb_cGDBM, "READER", INT2FIX(GDBM_READER|RUBY_GDBM_RW_BIT));
|
2006-09-19 20:30:38 -04:00
|
|
|
/* flag for #new and #open: open database as a writer */
|
2004-04-15 04:15:27 -04:00
|
|
|
rb_define_const(rb_cGDBM, "WRITER", INT2FIX(GDBM_WRITER|RUBY_GDBM_RW_BIT));
|
2006-09-19 20:30:38 -04:00
|
|
|
/* flag for #new and #open: open database as a writer; if the database does not exist, create a new one */
|
2004-04-15 04:15:27 -04:00
|
|
|
rb_define_const(rb_cGDBM, "WRCREAT", INT2FIX(GDBM_WRCREAT|RUBY_GDBM_RW_BIT));
|
2006-09-19 20:30:38 -04:00
|
|
|
/* flag for #new and #open: open database as a writer; overwrite any existing databases */
|
2004-04-15 04:15:27 -04:00
|
|
|
rb_define_const(rb_cGDBM, "NEWDB", INT2FIX(GDBM_NEWDB|RUBY_GDBM_RW_BIT));
|
|
|
|
|
2006-09-19 20:30:38 -04:00
|
|
|
/* flag for #new and #open. this flag is obsolete for gdbm >= 1.8 */
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_define_const(rb_cGDBM, "FAST", INT2FIX(GDBM_FAST));
|
2001-02-13 00:09:11 -05:00
|
|
|
/* this flag is obsolete in gdbm 1.8.
|
|
|
|
On gdbm 1.8, fast mode is default behavior. */
|
|
|
|
|
|
|
|
/* gdbm version 1.8 specific */
|
|
|
|
#if defined(GDBM_SYNC)
|
2006-09-19 20:30:38 -04:00
|
|
|
/* flag for #new and #open. only for gdbm >= 1.8 */
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_define_const(rb_cGDBM, "SYNC", INT2FIX(GDBM_SYNC));
|
2001-02-13 00:09:11 -05:00
|
|
|
#endif
|
|
|
|
#if defined(GDBM_NOLOCK)
|
2006-09-19 20:30:38 -04:00
|
|
|
/* flag for #new and #open */
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_define_const(rb_cGDBM, "NOLOCK", INT2FIX(GDBM_NOLOCK));
|
2001-02-13 00:09:11 -05:00
|
|
|
#endif
|
2006-09-19 20:30:38 -04:00
|
|
|
/* version of the gdbm library*/
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_define_const(rb_cGDBM, "VERSION", rb_str_new2(gdbm_version));
|
1999-08-13 01:37:52 -04:00
|
|
|
}
|