This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add PERL_NO_GET_CONTEXT to GDBM_File
[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
a4051d29
NC
28#define fetch_key 0
29#define store_key 1
30#define fetch_value 2
31#define store_value 3
32
9fe6733a
PM
33typedef struct {
34 DBM * dbp ;
a4051d29 35 SV * filter[4];
9fe6733a
PM
36 int filtering ;
37 } NDBM_File_type;
38
39typedef NDBM_File_type * NDBM_File ;
40typedef datum datum_key ;
41typedef datum datum_value ;
42
e82f3e3c 43
e74475c7 44#if defined(__cplusplus) && !defined(NDBM_HEADER_USES_PROTOTYPES)
e82f3e3c
SP
45/* gdbm's header file used for compatibility with gdbm */
46/* isn't compatible to C++ syntax, so we need these */
47/* declarations to make everyone happy. */
48EXTERN_C DBM *dbm_open(const char *, int, mode_t);
49EXTERN_C void dbm_close(DBM *);
50EXTERN_C datum dbm_fetch(DBM *, datum);
51EXTERN_C int dbm_store(DBM *, datum, datum, int);
52EXTERN_C int dbm_delete(DBM *, datum);
53EXTERN_C datum dbm_firstkey(DBM *);
54EXTERN_C datum dbm_nextkey(DBM *);
55#endif
56
049cc612 57MODULE = NDBM_File PACKAGE = NDBM_File PREFIX = ndbm_
a0d0e21e
LW
58
59NDBM_File
049cc612 60ndbm_TIEHASH(dbtype, filename, flags, mode)
a0d0e21e
LW
61 char * dbtype
62 char * filename
63 int flags
64 int mode
9fe6733a
PM
65 CODE:
66 {
67 DBM * dbp ;
68
69 RETVAL = NULL ;
8fa7f367 70 if ((dbp = dbm_open(filename, flags, mode))) {
a5c6857c 71 RETVAL = (NDBM_File)safecalloc(1, sizeof(NDBM_File_type));
9fe6733a
PM
72 RETVAL->dbp = dbp ;
73 }
74
75 }
76 OUTPUT:
77 RETVAL
a0d0e21e
LW
78
79void
049cc612 80ndbm_DESTROY(db)
a0d0e21e 81 NDBM_File db
a4051d29
NC
82 PREINIT:
83 int i = store_value;
a0d0e21e 84 CODE:
9fe6733a 85 dbm_close(db->dbp);
a4051d29
NC
86 do {
87 if (db->filter[i])
88 SvREFCNT_dec(db->filter[i]);
89 } while (i-- > 0);
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 ;
a4051d29
NC
147 ALIAS:
148 NDBM_File::filter_fetch_key = fetch_key
149 NDBM_File::filter_store_key = store_key
150 NDBM_File::filter_fetch_value = fetch_value
151 NDBM_File::filter_store_value = store_value
9fe6733a 152 CODE:
a4051d29 153 DBM_setFilter(db->filter[ix], code);