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