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