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