Creates a new database handle by opening
the given filename. SDBM actually uses
two physical files, with extensions ‘.dir’ and ‘.pag’. These
extensions will automatically be appended to the filename.
If the file does not exist, a new file
will be created using the given mode, unless mode is
explicitly set to nil. In the latter case, no database will be created.
If the file exists, it will be opened in read/write mode. If this fails, it
will be opened in read-only mode.
static VALUE
fsdbm_initialize(int argc, VALUE *argv, VALUE obj)
{
VALUE file, vmode;
DBM *dbm;
struct dbmdata *dbmp;
int mode;
if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
mode = 0666; /* default value */
}
else if (NIL_P(vmode)) {
mode = -1; /* return nil if DB not exist */
}
else {
mode = NUM2INT(vmode);
}
FilePathValue(file);
dbm = 0;
if (mode >= 0)
dbm = sdbm_open(RSTRING_PTR(file), O_RDWR|O_CREAT, mode);
if (!dbm)
dbm = sdbm_open(RSTRING_PTR(file), O_RDWR, 0);
if (!dbm)
dbm = sdbm_open(RSTRING_PTR(file), O_RDONLY, 0);
if (!dbm) {
if (mode == -1) return Qnil;
rb_sys_fail_str(file);
}
dbmp = ALLOC(struct dbmdata);
DATA_PTR(obj) = dbmp;
dbmp->di_dbm = dbm;
dbmp->di_size = -1;
return obj;
}