This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
5e02af7657199da676704ef802845362b54ddce4
[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 MODULE = NDBM_File      PACKAGE = NDBM_File     PREFIX = ndbm_
20
21 NDBM_File
22 ndbm_TIEHASH(dbtype, filename, flags, mode)
23         char *          dbtype
24         char *          filename
25         int             flags
26         int             mode
27         CODE:
28         {
29             DBM *       dbp ;
30
31             RETVAL = NULL ;
32             if ((dbp =  dbm_open(filename, flags, mode))) {
33                 RETVAL = (NDBM_File)safemalloc(sizeof(NDBM_File_type)) ;
34                 Zero(RETVAL, 1, NDBM_File_type) ;
35                 RETVAL->dbp = dbp ;
36             }
37             
38         }
39         OUTPUT:
40           RETVAL
41
42 void
43 ndbm_DESTROY(db)
44         NDBM_File       db
45         CODE:
46         dbm_close(db->dbp);
47         safefree(db);
48
49 #define ndbm_FETCH(db,key)                      dbm_fetch(db->dbp,key)
50 datum_value
51 ndbm_FETCH(db, key)
52         NDBM_File       db
53         datum_key       key
54
55 #define ndbm_STORE(db,key,value,flags)          dbm_store(db->dbp,key,value,flags)
56 int
57 ndbm_STORE(db, key, value, flags = DBM_REPLACE)
58         NDBM_File       db
59         datum_key       key
60         datum_value     value
61         int             flags
62     CLEANUP:
63         if (RETVAL) {
64             if (RETVAL < 0 && errno == EPERM)
65                 croak("No write permission to ndbm file");
66             croak("ndbm store returned %d, errno %d, key \"%s\"",
67                         RETVAL,errno,key.dptr);
68             dbm_clearerr(db->dbp);
69         }
70
71 #define ndbm_DELETE(db,key)                     dbm_delete(db->dbp,key)
72 int
73 ndbm_DELETE(db, key)
74         NDBM_File       db
75         datum_key       key
76
77 #define ndbm_FIRSTKEY(db)                       dbm_firstkey(db->dbp)
78 datum_key
79 ndbm_FIRSTKEY(db)
80         NDBM_File       db
81
82 #define ndbm_NEXTKEY(db,key)                    dbm_nextkey(db->dbp)
83 datum_key
84 ndbm_NEXTKEY(db, key)
85         NDBM_File       db
86         datum_key       key = NO_INIT
87
88 #define ndbm_error(db)                          dbm_error(db->dbp)
89 int
90 ndbm_error(db)
91         NDBM_File       db
92
93 #define ndbm_clearerr(db)                       dbm_clearerr(db->dbp)
94 void
95 ndbm_clearerr(db)
96         NDBM_File       db
97
98
99 SV *
100 filter_fetch_key(db, code)
101         NDBM_File       db
102         SV *            code
103         SV *            RETVAL = &PL_sv_undef ;
104         CODE:
105             DBM_setFilter(db->filter_fetch_key, code) ;
106
107 SV *
108 filter_store_key(db, code)
109         NDBM_File       db
110         SV *            code
111         SV *            RETVAL =  &PL_sv_undef ;
112         CODE:
113             DBM_setFilter(db->filter_store_key, code) ;
114
115 SV *
116 filter_fetch_value(db, code)
117         NDBM_File       db
118         SV *            code
119         SV *            RETVAL =  &PL_sv_undef ;
120         CODE:
121             DBM_setFilter(db->filter_fetch_value, code) ;
122
123 SV *
124 filter_store_value(db, code)
125         NDBM_File       db
126         SV *            code
127         SV *            RETVAL =  &PL_sv_undef ;
128         CODE:
129             DBM_setFilter(db->filter_store_value, code) ;
130