1998-01-16 07:13:05 -05:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
dbm.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Mon Jan 24 15:59:52 JST 1994
|
|
|
|
|
2001-02-14 00:52:06 -05:00
|
|
|
Copyright (C) 1995-2001 Yukihiro Matsumoto
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
|
1999-08-13 01:37:52 -04:00
|
|
|
#ifdef HAVE_CDEFS_H
|
|
|
|
# include <cdefs.h>
|
|
|
|
#endif
|
2000-09-12 01:37:38 -04:00
|
|
|
#ifdef HAVE_SYS_CDEFS_H
|
|
|
|
# include <sys/cdefs.h>
|
|
|
|
#endif
|
2001-05-24 02:10:36 -04:00
|
|
|
#include DBM_HDR
|
1998-01-16 07:13:05 -05:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2001-05-24 02:10:36 -04:00
|
|
|
static VALUE rb_cDBM, rb_eDBMError;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-05-16 08:03:23 -04:00
|
|
|
#define RUBY_DBM_RW_BIT 0x20000000
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
struct dbmdata {
|
2010-07-16 03:40:37 -04:00
|
|
|
long di_size;
|
1998-01-16 07:13:05 -05:00
|
|
|
DBM *di_dbm;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2006-02-12 23:53:22 -05:00
|
|
|
closed_dbm(void)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-02-26 23:52:21 -05:00
|
|
|
rb_raise(rb_eDBMError, "closed DBM file");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#define GetDBM(obj, dbmp) {\
|
|
|
|
Data_Get_Struct(obj, struct dbmdata, dbmp);\
|
2001-02-13 00:09:11 -05:00
|
|
|
if (dbmp == 0) closed_dbm();\
|
1998-01-16 07:13:05 -05:00
|
|
|
if (dbmp->di_dbm == 0) closed_dbm();\
|
|
|
|
}
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
#define GetDBM2(obj, data, dbm) {\
|
|
|
|
GetDBM(obj, data);\
|
|
|
|
(dbm) = dbmp->di_dbm;\
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static void
|
2006-02-12 23:53:22 -05:00
|
|
|
free_dbm(struct dbmdata *dbmp)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2001-02-13 00:09:11 -05:00
|
|
|
if (dbmp) {
|
|
|
|
if (dbmp->di_dbm) dbm_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
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.close
|
|
|
|
*
|
|
|
|
* Closes the database.
|
|
|
|
*/
|
2002-02-26 23:52:21 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_close(VALUE obj)
|
2002-02-26 23:52:21 -05:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
|
|
|
|
GetDBM(obj, dbmp);
|
|
|
|
dbm_close(dbmp->di_dbm);
|
|
|
|
dbmp->di_dbm = 0;
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
2001-02-13 00:09:11 -05:00
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.closed? -> true or false
|
|
|
|
*
|
|
|
|
* Returns true if the database is closed, false otherwise.
|
|
|
|
*/
|
2005-06-20 04:17:53 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_closed(VALUE obj)
|
2005-06-20 04:17:53 -04:00
|
|
|
{
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
|
|
|
|
Data_Get_Struct(obj, struct dbmdata, dbmp);
|
|
|
|
if (dbmp == 0)
|
|
|
|
return Qtrue;
|
|
|
|
if (dbmp->di_dbm == 0)
|
|
|
|
return Qtrue;
|
|
|
|
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_alloc(VALUE klass)
|
2001-02-13 00:09:11 -05:00
|
|
|
{
|
2002-12-20 06:30:13 -05:00
|
|
|
return Data_Wrap_Struct(klass, 0, free_dbm, 0);
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* DBM.new(filename[, mode[, flags]]) -> dbm
|
|
|
|
*
|
|
|
|
* Open a dbm database with the specified name, which can include a directory
|
|
|
|
* path. Any file extensions needed will be supplied automatically by the dbm
|
|
|
|
* library. For example, Berkeley DB appends '.db', and GNU gdbm uses two
|
|
|
|
* physical files with extensions '.dir' and '.pag'.
|
|
|
|
*
|
|
|
|
* The mode should be an integer, as for Unix chmod.
|
|
|
|
*
|
|
|
|
* Flags should be one of READER, WRITER, WRCREAT or NEWDB.
|
|
|
|
*/
|
2001-02-13 00:09:11 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_initialize(int argc, VALUE *argv, VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2009-03-23 08:45:23 -04:00
|
|
|
volatile VALUE file;
|
|
|
|
VALUE vmode, vflags;
|
1998-01-16 07:13:05 -05:00
|
|
|
DBM *dbm;
|
|
|
|
struct dbmdata *dbmp;
|
2004-05-16 08:03:23 -04:00
|
|
|
int mode, flags = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-05-16 08:03:23 -04:00
|
|
|
if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
|
1998-01-16 07:13:05 -05:00
|
|
|
mode = 0666; /* default value */
|
|
|
|
}
|
|
|
|
else if (NIL_P(vmode)) {
|
|
|
|
mode = -1; /* return nil if DB not exist */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mode = NUM2INT(vmode);
|
|
|
|
}
|
2004-05-16 08:03:23 -04:00
|
|
|
|
|
|
|
if (!NIL_P(vflags))
|
|
|
|
flags = NUM2INT(vflags);
|
|
|
|
|
2008-10-20 04:05:44 -04:00
|
|
|
FilePathValue(file);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-05-16 08:03:23 -04:00
|
|
|
if (flags & RUBY_DBM_RW_BIT) {
|
|
|
|
flags &= ~RUBY_DBM_RW_BIT;
|
2006-08-31 06:30:33 -04:00
|
|
|
dbm = dbm_open(RSTRING_PTR(file), flags, mode);
|
1999-11-04 03:39:57 -05:00
|
|
|
}
|
2004-05-16 08:03:23 -04:00
|
|
|
else {
|
|
|
|
dbm = 0;
|
|
|
|
if (mode >= 0) {
|
2006-08-31 06:30:33 -04:00
|
|
|
dbm = dbm_open(RSTRING_PTR(file), O_RDWR|O_CREAT, mode);
|
2004-05-16 08:03:23 -04:00
|
|
|
}
|
|
|
|
if (!dbm) {
|
2006-08-31 06:30:33 -04:00
|
|
|
dbm = dbm_open(RSTRING_PTR(file), O_RDWR, 0);
|
2004-05-16 08:03:23 -04:00
|
|
|
}
|
|
|
|
if (!dbm) {
|
2006-08-31 06:30:33 -04:00
|
|
|
dbm = dbm_open(RSTRING_PTR(file), O_RDONLY, 0);
|
2004-05-16 08:03:23 -04:00
|
|
|
}
|
1999-11-04 03:39:57 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (!dbm) {
|
|
|
|
if (mode == -1) return Qnil;
|
2006-08-31 06:30:33 -04:00
|
|
|
rb_sys_fail(RSTRING_PTR(file));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-02-13 00:09:11 -05:00
|
|
|
dbmp = ALLOC(struct dbmdata);
|
|
|
|
DATA_PTR(obj) = dbmp;
|
1998-01-16 07:13:05 -05:00
|
|
|
dbmp->di_dbm = dbm;
|
|
|
|
dbmp->di_size = -1;
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* DBM.open(filename[, mode[, flags]]) -> dbm
|
|
|
|
* DBM.open(filename[, mode[, flags]]) {|dbm| block}
|
|
|
|
*
|
|
|
|
* Open a dbm database and yields it if a block is given. See also
|
|
|
|
* <code>DBM.new</code>.
|
|
|
|
*/
|
2001-02-13 00:09:11 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_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(fdbm_initialize(argc, argv, obj))) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rb_block_given_p()) {
|
|
|
|
return rb_ensure(rb_yield, obj, fdbm_close, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key, value;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2008-10-20 04:05:44 -04:00
|
|
|
ExportStringValue(keystr);
|
2006-08-31 06:30:33 -04:00
|
|
|
key.dptr = RSTRING_PTR(keystr);
|
|
|
|
key.dsize = RSTRING_LEN(keystr);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
value = dbm_fetch(dbm, key);
|
|
|
|
if (value.dptr == 0) {
|
2000-07-04 03:04:42 -04:00
|
|
|
if (ifnone == Qnil && rb_block_given_p())
|
|
|
|
return rb_yield(rb_tainted_str_new(key.dptr, key.dsize));
|
|
|
|
return ifnone;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_tainted_str_new(value.dptr, value.dsize);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm[key] -> string value or nil
|
|
|
|
*
|
|
|
|
* Return a value from the database by locating the key string
|
|
|
|
* provided. If the key is not found, returns nil.
|
|
|
|
*/
|
2000-07-04 03:04:42 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_aref(VALUE obj, VALUE keystr)
|
2000-07-04 03:04:42 -04:00
|
|
|
{
|
|
|
|
return fdbm_fetch(obj, keystr, Qnil);
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.fetch(key[, ifnone]) -> value
|
|
|
|
*
|
|
|
|
* Return a value from the database by locating the key string
|
|
|
|
* provided. If the key is not found, returns +ifnone+. If +ifnone+
|
|
|
|
* is not given, raises IndexError.
|
|
|
|
*/
|
2000-07-04 03:04:42 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_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 = fdbm_fetch(obj, keystr, ifnone);
|
|
|
|
if (argc == 1 && !rb_block_given_p() && NIL_P(valstr))
|
|
|
|
rb_raise(rb_eIndexError, "key not found");
|
|
|
|
|
|
|
|
return valstr;
|
2000-07-04 03:04:42 -04:00
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.key(value) -> string
|
|
|
|
*
|
|
|
|
* Returns the key for the specified value.
|
|
|
|
*/
|
2000-07-04 03:04:42 -04:00
|
|
|
static VALUE
|
2008-11-07 17:07:05 -05:00
|
|
|
fdbm_key(VALUE obj, VALUE valstr)
|
2000-07-04 03:04:42 -04:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2008-10-20 04:05:44 -04:00
|
|
|
ExportStringValue(valstr);
|
2006-08-31 06:30:33 -04:00
|
|
|
val.dptr = RSTRING_PTR(valstr);
|
|
|
|
val.dsize = RSTRING_LEN(valstr);
|
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
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
2010-07-16 03:40:37 -04:00
|
|
|
if ((long)val.dsize == RSTRING_LEN(valstr) &&
|
2006-08-31 06:30:33 -04:00
|
|
|
memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0) {
|
2000-07-04 03:04:42 -04:00
|
|
|
return rb_tainted_str_new(key.dptr, key.dsize);
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
2000-07-04 03:04:42 -04:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/* :nodoc: */
|
2008-11-07 17:07:05 -05:00
|
|
|
static VALUE
|
|
|
|
fdbm_index(VALUE hash, VALUE value)
|
|
|
|
{
|
|
|
|
rb_warn("DBM#index is deprecated; use DBM#key");
|
|
|
|
return fdbm_key(hash, value);
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.select {|key, value| block} -> array
|
|
|
|
*
|
|
|
|
* Returns a new array consisting of the [key, value] pairs for which the code
|
|
|
|
* block returns true.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_select(VALUE obj)
|
2002-02-26 23:52:21 -05:00
|
|
|
{
|
2004-06-11 09:33:47 -04:00
|
|
|
VALUE new = rb_ary_new();
|
|
|
|
datum key, val;
|
|
|
|
DBM *dbm;
|
|
|
|
struct dbmdata *dbmp;
|
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 (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
2004-12-08 21:19:50 -05:00
|
|
|
VALUE assoc, v;
|
2004-06-11 09:33:47 -04:00
|
|
|
val = dbm_fetch(dbm, key);
|
|
|
|
assoc = rb_assoc_new(rb_tainted_str_new(key.dptr, key.dsize),
|
|
|
|
rb_tainted_str_new(val.dptr, val.dsize));
|
2004-12-08 21:19:50 -05:00
|
|
|
v = rb_yield(assoc);
|
|
|
|
if (RTEST(v)) {
|
2004-06-11 09:33:47 -04:00
|
|
|
rb_ary_push(new, assoc);
|
2004-12-08 21:19:50 -05:00
|
|
|
}
|
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2002-02-26 23:52:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.values_at(key, ...) -> Array
|
|
|
|
*
|
|
|
|
* Returns an array containing the values associated with the given keys.
|
|
|
|
*/
|
2003-05-06 02:51:31 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_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, fdbm_fetch(obj, argv[i], Qnil));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
static void
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_modify(VALUE obj)
|
2004-05-07 04:44:24 -04:00
|
|
|
{
|
|
|
|
rb_secure(4);
|
2004-05-08 04:12:08 -04:00
|
|
|
if (OBJ_FROZEN(obj)) rb_error_frozen("DBM");
|
2004-05-07 04:44:24 -04:00
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.delete(key)
|
|
|
|
*
|
|
|
|
* Deletes an entry from the database.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_delete(VALUE obj, VALUE keystr)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key, value;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
2002-02-26 23:52:21 -05:00
|
|
|
VALUE valstr;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
fdbm_modify(obj);
|
2008-10-20 04:05:44 -04:00
|
|
|
ExportStringValue(keystr);
|
2006-08-31 06:30:33 -04:00
|
|
|
key.dptr = RSTRING_PTR(keystr);
|
|
|
|
key.dsize = RSTRING_LEN(keystr);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
value = dbm_fetch(dbm, key);
|
|
|
|
if (value.dptr == 0) {
|
2002-02-26 23:52:21 -05:00
|
|
|
if (rb_block_given_p()) return rb_yield(keystr);
|
1998-01-16 07:13:05 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2002-02-26 23:52:21 -05:00
|
|
|
/* need to save value before dbm_delete() */
|
|
|
|
valstr = rb_tainted_str_new(value.dptr, value.dsize);
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
if (dbm_delete(dbm, key)) {
|
|
|
|
dbmp->di_size = -1;
|
2001-02-13 00:09:11 -05:00
|
|
|
rb_raise(rb_eDBMError, "dbm_delete failed");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (dbmp->di_size >= 0) {
|
|
|
|
dbmp->di_size--;
|
|
|
|
}
|
2002-02-26 23:52:21 -05:00
|
|
|
return valstr;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.shift() -> [key, value]
|
|
|
|
*
|
|
|
|
* Removes a [key, value] pair from the database, and returns it.
|
|
|
|
* If the database is empty, returns nil.
|
|
|
|
* The order in which values are removed/returned is not guaranteed.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_shift(VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE keystr, valstr;
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
fdbm_modify(obj);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
dbmp->di_size = -1;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
key = dbm_firstkey(dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (!key.dptr) return Qnil;
|
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
keystr = rb_tainted_str_new(key.dptr, key.dsize);
|
|
|
|
valstr = rb_tainted_str_new(val.dptr, val.dsize);
|
2001-05-02 00:22:21 -04:00
|
|
|
dbm_delete(dbm, key);
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_assoc_new(keystr, valstr);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.reject! {|key, value| block} -> self
|
|
|
|
* dbm.delete_if {|key, value| block} -> self
|
|
|
|
*
|
|
|
|
* Deletes all entries for which the code block returns true.
|
|
|
|
* Returns self.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_delete_if(VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE keystr, valstr;
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE ret, ary = rb_ary_new();
|
2010-07-16 03:40:37 -04:00
|
|
|
int i, status = 0;
|
|
|
|
long n;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
fdbm_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;
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
keystr = rb_tainted_str_new(key.dptr, key.dsize);
|
|
|
|
valstr = rb_tainted_str_new(val.dptr, val.dsize);
|
2001-05-02 00:22:21 -04:00
|
|
|
ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status);
|
|
|
|
if (status != 0) break;
|
|
|
|
if (RTEST(ret)) rb_ary_push(ary, keystr);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
|
|
|
|
2006-09-02 10:42:08 -04:00
|
|
|
for (i = 0; i < RARRAY_LEN(ary); i++) {
|
|
|
|
keystr = RARRAY_PTR(ary)[i];
|
2008-10-20 04:05:44 -04:00
|
|
|
ExportStringValue(keystr);
|
2006-08-31 06:30:33 -04:00
|
|
|
key.dptr = RSTRING_PTR(keystr);
|
|
|
|
key.dsize = RSTRING_LEN(keystr);
|
2001-05-02 00:22:21 -04:00
|
|
|
if (dbm_delete(dbm, key)) {
|
|
|
|
rb_raise(rb_eDBMError, "dbm_delete failed");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2001-05-02 00:22:21 -04:00
|
|
|
if (status) rb_jump_tag(status);
|
2006-09-02 10:42:08 -04:00
|
|
|
if (n > 0) dbmp->di_size = n - RARRAY_LEN(ary);
|
2001-05-02 00:22:21 -04:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.clear
|
|
|
|
*
|
|
|
|
* Deletes all data from the database.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_clear(VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
fdbm_modify(obj);
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
dbmp->di_size = -1;
|
2001-05-02 00:22:21 -04:00
|
|
|
while (key = dbm_firstkey(dbm), key.dptr) {
|
1998-01-16 07:13:05 -05:00
|
|
|
if (dbm_delete(dbm, key)) {
|
2001-02-13 00:09:11 -05:00
|
|
|
rb_raise(rb_eDBMError, "dbm_delete failed");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2001-05-02 00:22:21 -04:00
|
|
|
dbmp->di_size = 0;
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.invert -> hash
|
|
|
|
*
|
|
|
|
* Returns a Hash (not a DBM database) created by using each value in the
|
|
|
|
* database as a key, with the corresponding key as its value.
|
|
|
|
*/
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_invert(VALUE obj)
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE keystr, valstr;
|
|
|
|
VALUE hash = rb_hash_new();
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-01-19 23:59:39 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
|
|
|
keystr = rb_tainted_str_new(key.dptr, key.dsize);
|
|
|
|
valstr = rb_tainted_str_new(val.dptr, val.dsize);
|
|
|
|
rb_hash_aset(hash, valstr, keystr);
|
|
|
|
}
|
2000-04-10 01:48:43 -04:00
|
|
|
return hash;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
2006-02-03 04:15:42 -05:00
|
|
|
static VALUE fdbm_store(VALUE,VALUE,VALUE);
|
2003-04-17 01:41:27 -04:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
2006-02-03 04:15:42 -05:00
|
|
|
update_i(VALUE pair, VALUE dbm)
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
|
|
|
Check_Type(pair, T_ARRAY);
|
2006-09-02 10:42:08 -04:00
|
|
|
if (RARRAY_LEN(pair) < 2) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "pair must be [key, value]");
|
|
|
|
}
|
2006-09-02 10:42:08 -04:00
|
|
|
fdbm_store(dbm, RARRAY_PTR(pair)[0], RARRAY_PTR(pair)[1]);
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.update(obj)
|
|
|
|
*
|
|
|
|
* Updates the database with multiple values from the specified object.
|
|
|
|
* Takes any object which implements the each_pair method, including
|
|
|
|
* Hash and DBM objects.
|
|
|
|
*/
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
2006-02-03 04:15:42 -05:00
|
|
|
fdbm_update(VALUE obj, VALUE other)
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
2006-02-03 04:15:42 -05:00
|
|
|
rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
|
1999-01-19 23:59:39 -05:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.replace(obj)
|
|
|
|
*
|
|
|
|
* Replaces the contents of the database with the contents of the specified
|
|
|
|
* object. Takes any object which implements the each_pair method, including
|
|
|
|
* Hash and DBM objects.
|
|
|
|
*/
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
2006-02-03 04:15:42 -05:00
|
|
|
fdbm_replace(VALUE obj, VALUE other)
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
|
|
|
fdbm_clear(obj);
|
2006-02-03 04:15:42 -05:00
|
|
|
rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
|
1999-01-19 23:59:39 -05:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.store(key, value) -> value
|
|
|
|
* dbm[key] = value
|
|
|
|
*
|
|
|
|
* Stores the specified string value in the database, indexed via the
|
|
|
|
* string key provided.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2004-05-07 04:44:24 -04:00
|
|
|
fdbm_modify(obj);
|
1999-01-19 23:59:39 -05:00
|
|
|
keystr = rb_obj_as_string(keystr);
|
2004-11-14 10:41:40 -05:00
|
|
|
valstr = rb_obj_as_string(valstr);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2006-08-31 06:30:33 -04:00
|
|
|
key.dptr = RSTRING_PTR(keystr);
|
|
|
|
key.dsize = RSTRING_LEN(keystr);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2006-08-31 06:30:33 -04:00
|
|
|
val.dptr = RSTRING_PTR(valstr);
|
|
|
|
val.dsize = RSTRING_LEN(valstr);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
dbmp->di_size = -1;
|
|
|
|
if (dbm_store(dbm, key, val, DBM_REPLACE)) {
|
1999-08-13 01:37:52 -04:00
|
|
|
#ifdef HAVE_DBM_CLEARERR
|
1998-01-16 07:13:05 -05:00
|
|
|
dbm_clearerr(dbm);
|
1999-01-19 23:59:39 -05:00
|
|
|
#endif
|
|
|
|
if (errno == EPERM) rb_sys_fail(0);
|
2001-02-13 00:09:11 -05:00
|
|
|
rb_raise(rb_eDBMError, "dbm_store failed");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return valstr;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.length -> integer
|
|
|
|
*
|
|
|
|
* Returns the number of entries in the database.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_length(VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
int i = 0;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
|
|
|
|
|
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
dbmp->di_size = i;
|
|
|
|
|
|
|
|
return INT2FIX(i);
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.empty?
|
|
|
|
*
|
|
|
|
* Returns true if the database is empty, false otherwise.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_empty_p(VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
int i = 0;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (dbmp->di_size < 0) {
|
|
|
|
dbm = dbmp->di_dbm;
|
|
|
|
|
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = dbmp->di_size;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (i == 0) return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.each_value {|value| block} -> self
|
|
|
|
*
|
|
|
|
* Calls the block once for each value string in the database. Returns self.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_each_value(VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
* 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);
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_yield(rb_tainted_str_new(val.dptr, val.dsize));
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.each_key {|key| block} -> self
|
|
|
|
*
|
|
|
|
* Calls the block once for each key string in the database. Returns self.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_each_key(VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
* 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);
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_yield(rb_tainted_str_new(key.dptr, key.dsize));
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.each_pair {|key,value| block} -> self
|
|
|
|
*
|
|
|
|
* Calls the block once for each [key, value] pair in the database.
|
|
|
|
* Returns self.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_each_pair(VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
DBM *dbm;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
VALUE keystr, valstr;
|
|
|
|
|
* 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);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
keystr = rb_tainted_str_new(key.dptr, key.dsize);
|
|
|
|
valstr = rb_tainted_str_new(val.dptr, val.dsize);
|
|
|
|
rb_yield(rb_assoc_new(keystr, valstr));
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.keys -> array
|
|
|
|
*
|
|
|
|
* Returns an array of all the string keys in the database.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_keys(VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE ary;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new();
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, rb_tainted_str_new(key.dptr, key.dsize));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.values -> array
|
|
|
|
*
|
|
|
|
* Returns an array of all the string values in the database.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_values(VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE ary;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new();
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, rb_tainted_str_new(val.dptr, val.dsize));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.has_key?(key) -> boolean
|
|
|
|
*
|
|
|
|
* Returns true if the database contains the specified key, false otherwise.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_has_key(VALUE obj, VALUE keystr)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2008-10-20 04:05:44 -04:00
|
|
|
ExportStringValue(keystr);
|
2006-08-31 06:30:33 -04:00
|
|
|
key.dptr = RSTRING_PTR(keystr);
|
|
|
|
key.dsize = RSTRING_LEN(keystr);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (val.dptr) return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.has_value?(value) -> boolean
|
|
|
|
*
|
|
|
|
* Returns true if the database contains the specified string value, false
|
|
|
|
* otherwise.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_has_value(VALUE obj, VALUE valstr)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
|
2008-10-20 04:05:44 -04:00
|
|
|
ExportStringValue(valstr);
|
2006-08-31 06:30:33 -04:00
|
|
|
val.dptr = RSTRING_PTR(valstr);
|
|
|
|
val.dsize = RSTRING_LEN(valstr);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
2006-08-31 06:30:33 -04:00
|
|
|
if (val.dsize == RSTRING_LEN(valstr) &&
|
|
|
|
memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0)
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.to_a -> array
|
|
|
|
*
|
|
|
|
* Converts the contents of the database to an array of [key, value] arrays,
|
|
|
|
* and returns it.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_to_a(VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE ary;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new();
|
1998-01-16 07:13:05 -05:00
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, rb_assoc_new(rb_tainted_str_new(key.dptr, key.dsize),
|
2004-12-08 21:19:50 -05:00
|
|
|
rb_tainted_str_new(val.dptr, val.dsize)));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.to_hash -> hash
|
|
|
|
*
|
|
|
|
* Converts the contents of the database to an in-memory Hash object, and
|
|
|
|
* returns it.
|
|
|
|
*/
|
2000-07-04 03:04:42 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_to_hash(VALUE obj)
|
2000-07-04 03:04:42 -04:00
|
|
|
{
|
|
|
|
datum key, val;
|
|
|
|
struct dbmdata *dbmp;
|
|
|
|
DBM *dbm;
|
|
|
|
VALUE hash;
|
|
|
|
|
2004-12-08 21:19:50 -05:00
|
|
|
GetDBM2(obj, dbmp, dbm);
|
2000-07-04 03:04:42 -04:00
|
|
|
hash = rb_hash_new();
|
|
|
|
for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
|
|
|
|
val = dbm_fetch(dbm, key);
|
|
|
|
rb_hash_aset(hash, rb_tainted_str_new(key.dptr, key.dsize),
|
|
|
|
rb_tainted_str_new(val.dptr, val.dsize));
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* dbm.reject {|key,value| block} -> Hash
|
|
|
|
*
|
|
|
|
* Converts the contents of the database to an in-memory Hash, then calls
|
|
|
|
* Hash#reject with the specified code block, returning a new Hash.
|
|
|
|
*/
|
2000-07-04 03:04:42 -04:00
|
|
|
static VALUE
|
2006-02-12 23:53:22 -05:00
|
|
|
fdbm_reject(VALUE obj)
|
2000-07-04 03:04:42 -04:00
|
|
|
{
|
|
|
|
return rb_hash_delete_if(fdbm_to_hash(obj));
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/*
|
|
|
|
* Documented by mathew meta@pobox.com.
|
|
|
|
* = Introduction
|
|
|
|
*
|
|
|
|
* The DBM class provides a wrapper to a Unix-style
|
|
|
|
* {dbm}[http://en.wikipedia.org/wiki/Dbm] or Database Manager library.
|
|
|
|
*
|
|
|
|
* Dbm databases do not have tables or columns; they are simple key-value
|
|
|
|
* data stores, like a Ruby Hash except not resident in RAM. Keys and values
|
|
|
|
* must be strings.
|
|
|
|
*
|
|
|
|
* The exact library used depends on how Ruby was compiled. It could be any
|
|
|
|
* of the following:
|
|
|
|
*
|
|
|
|
* - {Berkeley DB}[http://en.wikipedia.org/wiki/Berkeley_DB] versions
|
|
|
|
* 1 thru 5, also known as BDB and Sleepycat DB, now owned by Oracle
|
|
|
|
* Corporation.
|
|
|
|
* - ndbm, aka Berkeley DB 1.x, still found in FreeBSD and OpenBSD.
|
|
|
|
* - {gdbm}[http://www.gnu.org/software/gdbm/], the GNU implementation of dbm.
|
|
|
|
* - {qdbm}[http://fallabs.com/qdbm/index.html], another open source
|
|
|
|
* reimplementation of dbm.
|
|
|
|
*
|
|
|
|
* All of these dbm implementations have their own Ruby interfaces
|
|
|
|
* available, which provide richer (but varying) APIs.
|
|
|
|
*
|
|
|
|
* = Cautions
|
|
|
|
*
|
|
|
|
* Before you decide to use DBM, there are some issues you should consider:
|
|
|
|
*
|
|
|
|
* - Each implementation of dbm has its own file format. Generally, dbm
|
|
|
|
* libraries will not read each other's files. This makes dbm files
|
|
|
|
* a bad choice for data exchange.
|
|
|
|
*
|
|
|
|
* - Even running the same OS and the same dbm implementation, the database
|
|
|
|
* file format may depend on the CPU architecture. For example, files may
|
|
|
|
* not be portable between PowerPC and 386, or between 32 and 64 bit Linux.
|
|
|
|
*
|
|
|
|
* - Different versions of Berkeley DB use different file formats. A change to
|
|
|
|
* the OS may therefore break DBM access to existing files.
|
|
|
|
*
|
|
|
|
* - Data size limits vary between implementations. Original Berkeley DB was
|
|
|
|
* limited to 2GB of data. Dbm libraries also sometimes limit the total
|
|
|
|
* size of a key/value pair, and the total size of all the keys that hash
|
|
|
|
* to the same value. These limits can be as little as 512 bytes. That said,
|
|
|
|
* gdbm and recent versions of Berkeley DB do away with these limits.
|
|
|
|
*
|
|
|
|
* Given the above cautions, DBM is not a good choice for long term storage of
|
|
|
|
* important data. It is probably best used as a fast and easy alternative
|
|
|
|
* to a Hash for processing large amounts of data.
|
|
|
|
*
|
|
|
|
* = Example
|
|
|
|
*
|
|
|
|
* require 'dbm'
|
|
|
|
* db = DBM.open('rfcs', 666, DBM::CREATRW)
|
|
|
|
* db['822'] = 'Standard for the Format of ARPA Internet Text Messages'
|
|
|
|
* db['1123'] = 'Requirements for Internet Hosts - Application and Support'
|
|
|
|
* db['3068'] = 'An Anycast Prefix for 6to4 Relay Routers'
|
|
|
|
* puts db['822']
|
|
|
|
*/
|
1999-01-19 23:59:39 -05:00
|
|
|
void
|
2006-02-12 23:53:22 -05:00
|
|
|
Init_dbm(void)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2001-05-24 02:10:36 -04:00
|
|
|
rb_cDBM = rb_define_class("DBM", rb_cObject);
|
2010-10-20 10:54:40 -04:00
|
|
|
/* Document-class: DBMError
|
|
|
|
* Exception class used to return errors from the dbm library.
|
|
|
|
*/
|
2001-02-13 00:09:11 -05:00
|
|
|
rb_eDBMError = rb_define_class("DBMError", rb_eStandardError);
|
2001-05-24 02:10:36 -04:00
|
|
|
rb_include_module(rb_cDBM, rb_mEnumerable);
|
|
|
|
|
2002-12-20 06:30:13 -05:00
|
|
|
rb_define_alloc_func(rb_cDBM, fdbm_alloc);
|
2001-05-24 02:10:36 -04:00
|
|
|
rb_define_singleton_method(rb_cDBM, "open", fdbm_s_open, -1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cDBM, "initialize", fdbm_initialize, -1);
|
|
|
|
rb_define_method(rb_cDBM, "close", fdbm_close, 0);
|
2005-06-20 04:17:53 -04:00
|
|
|
rb_define_method(rb_cDBM, "closed?", fdbm_closed, 0);
|
2001-05-24 02:10:36 -04:00
|
|
|
rb_define_method(rb_cDBM, "[]", fdbm_aref, 1);
|
|
|
|
rb_define_method(rb_cDBM, "fetch", fdbm_fetch_m, -1);
|
|
|
|
rb_define_method(rb_cDBM, "[]=", fdbm_store, 2);
|
|
|
|
rb_define_method(rb_cDBM, "store", fdbm_store, 2);
|
|
|
|
rb_define_method(rb_cDBM, "index", fdbm_index, 1);
|
2008-11-07 17:07:05 -05:00
|
|
|
rb_define_method(rb_cDBM, "key", fdbm_key, 1);
|
2004-10-19 06:25:23 -04:00
|
|
|
rb_define_method(rb_cDBM, "select", fdbm_select, 0);
|
2003-05-06 02:51:31 -04:00
|
|
|
rb_define_method(rb_cDBM, "values_at", fdbm_values_at, -1);
|
2001-05-24 02:10:36 -04:00
|
|
|
rb_define_method(rb_cDBM, "length", fdbm_length, 0);
|
|
|
|
rb_define_method(rb_cDBM, "size", fdbm_length, 0);
|
|
|
|
rb_define_method(rb_cDBM, "empty?", fdbm_empty_p, 0);
|
|
|
|
rb_define_method(rb_cDBM, "each", fdbm_each_pair, 0);
|
|
|
|
rb_define_method(rb_cDBM, "each_value", fdbm_each_value, 0);
|
|
|
|
rb_define_method(rb_cDBM, "each_key", fdbm_each_key, 0);
|
|
|
|
rb_define_method(rb_cDBM, "each_pair", fdbm_each_pair, 0);
|
|
|
|
rb_define_method(rb_cDBM, "keys", fdbm_keys, 0);
|
|
|
|
rb_define_method(rb_cDBM, "values", fdbm_values, 0);
|
|
|
|
rb_define_method(rb_cDBM, "shift", fdbm_shift, 0);
|
|
|
|
rb_define_method(rb_cDBM, "delete", fdbm_delete, 1);
|
|
|
|
rb_define_method(rb_cDBM, "delete_if", fdbm_delete_if, 0);
|
|
|
|
rb_define_method(rb_cDBM, "reject!", fdbm_delete_if, 0);
|
|
|
|
rb_define_method(rb_cDBM, "reject", fdbm_reject, 0);
|
|
|
|
rb_define_method(rb_cDBM, "clear", fdbm_clear, 0);
|
|
|
|
rb_define_method(rb_cDBM,"invert", fdbm_invert, 0);
|
|
|
|
rb_define_method(rb_cDBM,"update", fdbm_update, 1);
|
|
|
|
rb_define_method(rb_cDBM,"replace", fdbm_replace, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cDBM, "include?", fdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cDBM, "has_key?", fdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cDBM, "member?", fdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cDBM, "has_value?", fdbm_has_value, 1);
|
|
|
|
rb_define_method(rb_cDBM, "key?", fdbm_has_key, 1);
|
|
|
|
rb_define_method(rb_cDBM, "value?", fdbm_has_value, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cDBM, "to_a", fdbm_to_a, 0);
|
|
|
|
rb_define_method(rb_cDBM, "to_hash", fdbm_to_hash, 0);
|
2003-05-06 02:51:31 -04:00
|
|
|
|
2010-10-20 10:54:40 -04:00
|
|
|
/* Indicates that dbm_open() should open the database in read-only mode */
|
2004-05-16 08:03:23 -04:00
|
|
|
rb_define_const(rb_cDBM, "READER", INT2FIX(O_RDONLY|RUBY_DBM_RW_BIT));
|
2010-10-20 10:54:40 -04:00
|
|
|
|
|
|
|
/* Indicates that dbm_open() should open the database in read/write mode */
|
2004-05-16 08:03:23 -04:00
|
|
|
rb_define_const(rb_cDBM, "WRITER", INT2FIX(O_RDWR|RUBY_DBM_RW_BIT));
|
2010-10-20 10:54:40 -04:00
|
|
|
|
|
|
|
/* Indicates that dbm_open() should open the database in read/write mode,
|
|
|
|
* and create it if it does not already exist
|
|
|
|
*/
|
2004-05-16 08:03:23 -04:00
|
|
|
rb_define_const(rb_cDBM, "WRCREAT", INT2FIX(O_RDWR|O_CREAT|RUBY_DBM_RW_BIT));
|
2010-10-20 10:54:40 -04:00
|
|
|
|
|
|
|
/* Indicates that dbm_open() should open the database in read/write mode,
|
|
|
|
* create it if it does not already exist, and delete all contents if it
|
|
|
|
* does already exist.
|
|
|
|
*/
|
2004-05-16 08:03:23 -04:00
|
|
|
rb_define_const(rb_cDBM, "NEWDB", INT2FIX(O_RDWR|O_CREAT|O_TRUNC|RUBY_DBM_RW_BIT));
|
|
|
|
|
2003-05-06 02:51:31 -04:00
|
|
|
#ifdef DB_VERSION_STRING
|
2010-10-20 10:54:40 -04:00
|
|
|
/* The version of the dbm library, if using Berkeley DB */
|
2003-05-06 02:51:31 -04:00
|
|
|
rb_define_const(rb_cDBM, "VERSION", rb_str_new2(DB_VERSION_STRING));
|
2004-06-11 09:33:47 -04:00
|
|
|
#else
|
|
|
|
rb_define_const(rb_cDBM, "VERSION", rb_str_new2("unknown"));
|
2003-05-06 02:51:31 -04:00
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|