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