DB_File.xs -- Perl 5 interface to Berkeley DB
written by Paul Marquess (pmarquess@bfsec.bt.co.uk)
- last modified 12th Mar 1997
- version 1.12
+ last modified 8th Oct 1997
+ version 1.16
All comments/suggestions/problems are welcome
in-memory files when db 1.86 is used.
1.11 - No change to DB_File.xs
1.12 - No change to DB_File.xs
+ 1.13 - Tidied up a few casts.
+ 1.14 - Made it illegal to tie an associative array to a RECNO
+ database and an ordinary array to a HASH or BTREE database.
+ 1.15 - Patch from Gisle Aas <gisle@aas.no> to suppress "use of
+ undefined value" warning with db_get and db_seq.
+ 1.16 - Minor additions to DB_File.xs to support multithreaded perl.
*/
#include "perl.h"
#include "XSUB.h"
+/* Being the Berkeley DB we prefer the <sys/cdefs.h> (which will be
+ * shortly #included by the <db.h>) __attribute__ to the possibly
+ * already defined __attribute__, for example by GNUC or by Perl. */
+#undef __attribute__
+
#include <db.h>
+/* #ifdef DB_VERSION_MAJOR */
+/* #include <db_185.h> */
+/* #endif */
#include <fcntl.h>
typedef DBT DBTKEY ;
-/* #define TRACE */
+/* #define TRACE */
#define db_DESTROY(db) ((db->dbp)->close)(db->dbp)
#define db_DELETE(db, key, flags) ((db->dbp)->del)(db->dbp, &key, flags)
printf (" cachesize = %d\n", recno->cachesize) ;
printf (" psize = %d\n", recno->psize) ;
printf (" lorder = %d\n", recno->lorder) ;
- printf (" reclen = %d\n", recno->reclen) ;
+ printf (" reclen = %lu\n", (unsigned long)recno->reclen) ;
printf (" bval = %d 0x%x\n", recno->bval, recno->bval) ;
printf (" bfname = %d [%s]\n", recno->bfname, recno->bfname) ;
}
}
static DB_File
-ParseOpenInfo(name, flags, mode, sv)
+ParseOpenInfo(isHASH, name, flags, mode, sv)
+int isHASH ;
char * name ;
int flags ;
int mode ;
if (sv_isa(sv, "DB_File::HASHINFO"))
{
+
+ if (!isHASH)
+ croak("DB_File can only tie an associative array to a DB_HASH database") ;
+
RETVAL->type = DB_HASH ;
openinfo = (void*)info ;
}
else if (sv_isa(sv, "DB_File::BTREEINFO"))
{
+ if (!isHASH)
+ croak("DB_File can only tie an associative array to a DB_BTREE database");
+
RETVAL->type = DB_BTREE ;
openinfo = (void*)info ;
}
else if (sv_isa(sv, "DB_File::RECNOINFO"))
{
+ if (isHASH)
+ croak("DB_File can only tie an array to a DB_RECNO database");
+
RETVAL->type = DB_RECNO ;
openinfo = (void *)info ;
svp = hv_fetch(action, "flags", 5, FALSE);
- info->recno.flags = (u_long) svp ? SvIV(*svp) : 0;
+ info->recno.flags = (u_long) (svp ? SvIV(*svp) : 0);
svp = hv_fetch(action, "cachesize", 9, FALSE);
- info->recno.cachesize = (u_int) svp ? SvIV(*svp) : 0;
+ info->recno.cachesize = (u_int) (svp ? SvIV(*svp) : 0);
svp = hv_fetch(action, "psize", 5, FALSE);
- info->recno.psize = (int) svp ? SvIV(*svp) : 0;
+ info->recno.psize = (u_int) (svp ? SvIV(*svp) : 0);
svp = hv_fetch(action, "lorder", 6, FALSE);
- info->recno.lorder = (int) svp ? SvIV(*svp) : 0;
+ info->recno.lorder = (int) (svp ? SvIV(*svp) : 0);
svp = hv_fetch(action, "reclen", 6, FALSE);
- info->recno.reclen = (size_t) svp ? SvIV(*svp) : 0;
+ info->recno.reclen = (size_t) (svp ? SvIV(*svp) : 0);
svp = hv_fetch(action, "bval", 4, FALSE);
if (svp && SvOK(*svp))
svp = hv_fetch(action, "bfname", 6, FALSE);
if (svp && SvOK(*svp)) {
char * ptr = SvPV(*svp,na) ;
- info->recno.bfname = (char*) na ? ptr : NULL ;
+ info->recno.bfname = (char*) (na ? ptr : NULL) ;
}
else
info->recno.bfname = NULL ;
DB_File
-db_DoTie_(dbtype, name=undef, flags=O_CREAT|O_RDWR, mode=0666, type=DB_HASH)
+db_DoTie_(isHASH, dbtype, name=undef, flags=O_CREAT|O_RDWR, mode=0666, type=DB_HASH)
+ int isHASH
char * dbtype
int flags
int mode
char * name = (char *) NULL ;
SV * sv = (SV *) NULL ;
- if (items >= 2 && SvOK(ST(1)))
- name = (char*) SvPV(ST(1), na) ;
+ if (items >= 3 && SvOK(ST(2)))
+ name = (char*) SvPV(ST(2), na) ;
- if (items == 5)
- sv = ST(4) ;
+ if (items == 6)
+ sv = ST(5) ;
- RETVAL = ParseOpenInfo(name, flags, mode, sv) ;
+ RETVAL = ParseOpenInfo(isHASH, name, flags, mode, sv) ;
if (RETVAL->dbp == NULL)
RETVAL = NULL ;
}
ST(0) = sv_newmortal();
if (RETVAL == 0)
{
- if (Db->type != DB_RECNO)
+ if (db->type != DB_RECNO)
sv_setpvn(ST(0), key.data, key.size);
else
sv_setiv(ST(0), (I32)*(I32*)key.data - 1);
ST(0) = sv_newmortal();
if (RETVAL == 0)
{
- if (Db->type != DB_RECNO)
+ if (db->type != DB_RECNO)
sv_setpvn(ST(0), key.data, key.size);
else
sv_setiv(ST(0), (I32)*(I32*)key.data - 1);
db_get(db, key, value, flags=0)
DB_File db
DBTKEY key
- DBT value
+ DBT value = NO_INIT
u_int flags
INIT:
CurrentDB = db ;
db_seq(db, key, value, flags)
DB_File db
DBTKEY key
- DBT value
+ DBT value = NO_INIT
u_int flags
INIT:
CurrentDB = db ;