This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Various changes to regex diagnostics and testing
[perl5.git] / ext / NDBM_File / NDBM_File.xs
CommitLineData
a0d0e21e
LW
1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
a33f2d9f
AD
4#if defined(I_GDBM_NDBM)
5# include <gdbm-ndbm.h> /* Debian compatibility version */
6#elif defined(I_GDBMNDBM)
7# include <gdbm/ndbm.h> /* RedHat compatibility version */
8#elif defined(I_NDBM)
a0d0e21e 9#include <ndbm.h>
a33f2d9f 10#endif
a0d0e21e 11
9fe6733a
PM
12typedef struct {
13 DBM * dbp ;
14 SV * filter_fetch_key ;
15 SV * filter_store_key ;
16 SV * filter_fetch_value ;
17 SV * filter_store_value ;
18 int filtering ;
19 } NDBM_File_type;
20
21typedef NDBM_File_type * NDBM_File ;
22typedef datum datum_key ;
23typedef datum datum_value ;
24
e82f3e3c 25
5046273b 26#if defined(__cplusplus) && defined(HAS_GDBM)
e82f3e3c
SP
27/* gdbm's header file used for compatibility with gdbm */
28/* isn't compatible to C++ syntax, so we need these */
29/* declarations to make everyone happy. */
30EXTERN_C DBM *dbm_open(const char *, int, mode_t);
31EXTERN_C void dbm_close(DBM *);
32EXTERN_C datum dbm_fetch(DBM *, datum);
33EXTERN_C int dbm_store(DBM *, datum, datum, int);
34EXTERN_C int dbm_delete(DBM *, datum);
35EXTERN_C datum dbm_firstkey(DBM *);
36EXTERN_C datum dbm_nextkey(DBM *);
37#endif
38
049cc612 39MODULE = NDBM_File PACKAGE = NDBM_File PREFIX = ndbm_
a0d0e21e
LW
40
41NDBM_File
049cc612 42ndbm_TIEHASH(dbtype, filename, flags, mode)
a0d0e21e
LW
43 char * dbtype
44 char * filename
45 int flags
46 int mode
9fe6733a
PM
47 CODE:
48 {
49 DBM * dbp ;
50
51 RETVAL = NULL ;
8fa7f367 52 if ((dbp = dbm_open(filename, flags, mode))) {
9fe6733a
PM
53 RETVAL = (NDBM_File)safemalloc(sizeof(NDBM_File_type)) ;
54 Zero(RETVAL, 1, NDBM_File_type) ;
55 RETVAL->dbp = dbp ;
56 }
57
58 }
59 OUTPUT:
60 RETVAL
a0d0e21e
LW
61
62void
049cc612 63ndbm_DESTROY(db)
a0d0e21e
LW
64 NDBM_File db
65 CODE:
9fe6733a 66 dbm_close(db->dbp);
eb99164f 67 safefree(db);
a0d0e21e 68
049cc612 69#define ndbm_FETCH(db,key) dbm_fetch(db->dbp,key)
9fe6733a 70datum_value
049cc612 71ndbm_FETCH(db, key)
a0d0e21e 72 NDBM_File db
9fe6733a 73 datum_key key
a0d0e21e 74
049cc612 75#define ndbm_STORE(db,key,value,flags) dbm_store(db->dbp,key,value,flags)
a0d0e21e 76int
049cc612 77ndbm_STORE(db, key, value, flags = DBM_REPLACE)
a0d0e21e 78 NDBM_File db
9fe6733a
PM
79 datum_key key
80 datum_value value
a0d0e21e
LW
81 int flags
82 CLEANUP:
83 if (RETVAL) {
84 if (RETVAL < 0 && errno == EPERM)
85 croak("No write permission to ndbm file");
748a9306 86 croak("ndbm store returned %d, errno %d, key \"%s\"",
a0d0e21e 87 RETVAL,errno,key.dptr);
9fe6733a 88 dbm_clearerr(db->dbp);
a0d0e21e
LW
89 }
90
049cc612 91#define ndbm_DELETE(db,key) dbm_delete(db->dbp,key)
a0d0e21e 92int
049cc612 93ndbm_DELETE(db, key)
a0d0e21e 94 NDBM_File db
9fe6733a 95 datum_key key
a0d0e21e 96
049cc612 97#define ndbm_FIRSTKEY(db) dbm_firstkey(db->dbp)
9fe6733a 98datum_key
049cc612 99ndbm_FIRSTKEY(db)
a0d0e21e
LW
100 NDBM_File db
101
049cc612 102#define ndbm_NEXTKEY(db,key) dbm_nextkey(db->dbp)
9fe6733a 103datum_key
049cc612 104ndbm_NEXTKEY(db, key)
a0d0e21e 105 NDBM_File db
0bf2e707 106 datum_key key = NO_INIT
a0d0e21e 107
049cc612 108#define ndbm_error(db) dbm_error(db->dbp)
a0d0e21e 109int
049cc612 110ndbm_error(db)
a0d0e21e
LW
111 NDBM_File db
112
049cc612 113#define ndbm_clearerr(db) dbm_clearerr(db->dbp)
a0d0e21e 114void
049cc612 115ndbm_clearerr(db)
a0d0e21e
LW
116 NDBM_File db
117
9fe6733a 118
9fe6733a
PM
119SV *
120filter_fetch_key(db, code)
121 NDBM_File db
122 SV * code
123 SV * RETVAL = &PL_sv_undef ;
124 CODE:
6a31061a 125 DBM_setFilter(db->filter_fetch_key, code) ;
9fe6733a
PM
126
127SV *
128filter_store_key(db, code)
129 NDBM_File db
130 SV * code
131 SV * RETVAL = &PL_sv_undef ;
132 CODE:
6a31061a 133 DBM_setFilter(db->filter_store_key, code) ;
9fe6733a
PM
134
135SV *
136filter_fetch_value(db, code)
137 NDBM_File db
138 SV * code
139 SV * RETVAL = &PL_sv_undef ;
140 CODE:
6a31061a 141 DBM_setFilter(db->filter_fetch_value, code) ;
9fe6733a
PM
142
143SV *
144filter_store_value(db, code)
145 NDBM_File db
146 SV * code
147 SV * RETVAL = &PL_sv_undef ;
148 CODE:
6a31061a 149 DBM_setFilter(db->filter_store_value, code) ;
9fe6733a 150