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