1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/dbm/dbm.c (fdbm_initialize): accept optional 3rd argument to

specify an open flag.
  (Init_dbm): define open flags: DBM::READER, DBM::WRITER, DBM::WRCREAT
  and DBM::NEWDB.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6326 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2004-05-16 12:03:23 +00:00
parent a694dea2d8
commit eeb5c7e18e
4 changed files with 74 additions and 14 deletions

View file

@ -1,3 +1,10 @@
Sun May 16 20:55:49 2004 Tanaka Akira <akr@m17n.org>
* ext/dbm/dbm.c (fdbm_initialize): accept optional 3rd argument to
specify an open flag.
(Init_dbm): define open flags: DBM::READER, DBM::WRITER, DBM::WRCREAT
and DBM::NEWDB.
Sat May 15 17:52:24 Hirokazu Yamamoto <ocean@m2.ccsnet.ne.jp> Sat May 15 17:52:24 Hirokazu Yamamoto <ocean@m2.ccsnet.ne.jp>
* test/ruby/test_float.rb(test_strtod): Add test for signed 0.000...1 * test/ruby/test_float.rb(test_strtod): Add test for signed 0.000...1

View file

@ -24,6 +24,8 @@
static VALUE rb_cDBM, rb_eDBMError; static VALUE rb_cDBM, rb_eDBMError;
#define RUBY_DBM_RW_BIT 0x20000000
struct dbmdata { struct dbmdata {
int di_size; int di_size;
DBM *di_dbm; DBM *di_dbm;
@ -78,12 +80,12 @@ fdbm_initialize(argc, argv, obj)
VALUE *argv; VALUE *argv;
VALUE obj; VALUE obj;
{ {
VALUE file, vmode; VALUE file, vmode, vflags;
DBM *dbm; DBM *dbm;
struct dbmdata *dbmp; struct dbmdata *dbmp;
int mode; int mode, flags = 0;
if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) { if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
mode = 0666; /* default value */ mode = 0666; /* default value */
} }
else if (NIL_P(vmode)) { else if (NIL_P(vmode)) {
@ -92,8 +94,17 @@ fdbm_initialize(argc, argv, obj)
else { else {
mode = NUM2INT(vmode); mode = NUM2INT(vmode);
} }
if (!NIL_P(vflags))
flags = NUM2INT(vflags);
SafeStringValue(file); SafeStringValue(file);
if (flags & RUBY_DBM_RW_BIT) {
flags &= ~RUBY_DBM_RW_BIT;
dbm = dbm_open(RSTRING(file)->ptr, flags, mode);
}
else {
dbm = 0; dbm = 0;
if (mode >= 0) { if (mode >= 0) {
dbm = dbm_open(RSTRING(file)->ptr, O_RDWR|O_CREAT, mode); dbm = dbm_open(RSTRING(file)->ptr, O_RDWR|O_CREAT, mode);
@ -104,6 +115,7 @@ fdbm_initialize(argc, argv, obj)
if (!dbm) { if (!dbm) {
dbm = dbm_open(RSTRING(file)->ptr, O_RDONLY, 0); dbm = dbm_open(RSTRING(file)->ptr, O_RDONLY, 0);
} }
}
if (!dbm) { if (!dbm) {
if (mode == -1) return Qnil; if (mode == -1) return Qnil;
@ -787,6 +799,12 @@ Init_dbm()
rb_define_method(rb_cDBM, "to_a", fdbm_to_a, 0); rb_define_method(rb_cDBM, "to_a", fdbm_to_a, 0);
rb_define_method(rb_cDBM, "to_hash", fdbm_to_hash, 0); rb_define_method(rb_cDBM, "to_hash", fdbm_to_hash, 0);
/* flags for dbm_open() */
rb_define_const(rb_cDBM, "READER", INT2FIX(O_RDONLY|RUBY_DBM_RW_BIT));
rb_define_const(rb_cDBM, "WRITER", INT2FIX(O_RDWR|RUBY_DBM_RW_BIT));
rb_define_const(rb_cDBM, "WRCREAT", INT2FIX(O_RDWR|O_CREAT|RUBY_DBM_RW_BIT));
rb_define_const(rb_cDBM, "NEWDB", INT2FIX(O_RDWR|O_CREAT|O_TRUNC|RUBY_DBM_RW_BIT));
#ifdef DB_VERSION_STRING #ifdef DB_VERSION_STRING
rb_define_const(rb_cDBM, "VERSION", rb_str_new2(DB_VERSION_STRING)); rb_define_const(rb_cDBM, "VERSION", rb_str_new2(DB_VERSION_STRING));
#endif #endif

View file

@ -11,7 +11,7 @@ if defined? DBM
require 'fileutils' require 'fileutils'
class TestDBM < Test::Unit::TestCase class TestDBM < Test::Unit::TestCase
TMPROOT = "#{Dir.tmpdir}/ruby-gdbm.#{$$}" TMPROOT = "#{Dir.tmpdir}/ruby-dbm.#{$$}"
def setup def setup
Dir.mkdir TMPROOT Dir.mkdir TMPROOT
@ -21,8 +21,31 @@ if defined? DBM
FileUtils.rm_rf TMPROOT if File.directory?(TMPROOT) FileUtils.rm_rf TMPROOT if File.directory?(TMPROOT)
end end
def test_reader_open
DBM.open("#{TMPROOT}/a") {}
v = DBM.open("#{TMPROOT}/a", nil, DBM::READER) {|d|
# Errno::EPERM is raised on Solaris which use ndbm.
# DBMError is raised on Debian which use gdbm.
assert_raises(Errno::EPERM, DBMError) { d["k"] = "v" }
true
}
assert(v)
end
def test_newdb_open
DBM.open("#{TMPROOT}/a") {|dbm|
dbm["k"] = "v"
}
v = DBM.open("#{TMPROOT}/a", nil, DBM::NEWDB) {|d|
assert_equal(0, d.length)
assert_nil(d["k"])
true
}
assert(v)
end
def test_freeze def test_freeze
DBM.open("#{TMPROOT}/a.dbm") {|d| DBM.open("#{TMPROOT}/a") {|d|
d.freeze d.freeze
assert_raises(TypeError) { d["k"] = "v" } assert_raises(TypeError) { d["k"] = "v" }
} }

View file

@ -21,7 +21,7 @@ if defined? GDBM
FileUtils.rm_rf TMPROOT if File.directory?(TMPROOT) FileUtils.rm_rf TMPROOT if File.directory?(TMPROOT)
end end
def test_open def test_reader_open
GDBM.open("#{TMPROOT}/a.dbm") {} GDBM.open("#{TMPROOT}/a.dbm") {}
v = GDBM.open("#{TMPROOT}/a.dbm", nil, GDBM::READER) {|d| v = GDBM.open("#{TMPROOT}/a.dbm", nil, GDBM::READER) {|d|
assert_raises(GDBMError) { d["k"] = "v" } assert_raises(GDBMError) { d["k"] = "v" }
@ -30,6 +30,18 @@ if defined? GDBM
assert(v) assert(v)
end end
def test_newdb_open
GDBM.open("#{TMPROOT}/a.dbm") {|dbm|
dbm["k"] = "v"
}
v = GDBM.open("#{TMPROOT}/a.dbm", nil, GDBM::NEWDB) {|d|
assert_equal(0, d.length)
assert_nil(d["k"])
true
}
assert(v)
end
def test_freeze def test_freeze
GDBM.open("#{TMPROOT}/a.dbm") {|d| GDBM.open("#{TMPROOT}/a.dbm") {|d|
d.freeze d.freeze