1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* DBM module using dictionary interface */
28 #include "allobjects.h"
29 #include "modsupport.h"
31 #include <sys/types.h>
38 int di_size
; /* -1 means recompute */
42 staticforward typeobject Dbmtype
;
44 #define is_dbmobject(v) ((v)->ob_type == &Dbmtype)
46 static object
*DbmError
;
49 newdbmobject(file
, flags
, mode
)
56 dp
= NEWOBJ(dbmobject
, &Dbmtype
);
60 if ( (dp
->di_dbm
= dbm_open(file
, flags
, mode
)) == 0 ) {
72 register dbmobject
*dp
;
75 dbm_close(dp
->di_dbm
);
83 if ( dp
->di_size
< 0 ) {
88 for ( key
=dbm_firstkey(dp
->di_dbm
); key
.dptr
;
89 key
= dbm_nextkey(dp
->di_dbm
))
97 dbm_subscript(dp
, key
)
104 if (!getargs(key
, "s#", &krec
.dptr
, &krec
.dsize
) )
107 drec
= dbm_fetch(dp
->di_dbm
, krec
);
108 if ( drec
.dptr
== 0 ) {
109 err_setstr(KeyError
, GETSTRINGVALUE((stringobject
*)key
));
112 if ( dbm_error(dp
->di_dbm
) ) {
113 dbm_clearerr(dp
->di_dbm
);
114 err_setstr(DbmError
, "");
117 return newsizedstringobject(drec
.dptr
, drec
.dsize
);
121 dbm_ass_sub(dp
, v
, w
)
127 if ( !getargs(v
, "s#", &krec
.dptr
, &krec
.dsize
) ) {
128 err_setstr(TypeError
, "dbm mappings have string indices only");
133 if ( dbm_delete(dp
->di_dbm
, krec
) < 0 ) {
134 dbm_clearerr(dp
->di_dbm
);
135 err_setstr(KeyError
, GETSTRINGVALUE((stringobject
*)v
));
139 if ( !getargs(w
, "s#", &drec
.dptr
, &drec
.dsize
) ) {
140 err_setstr(TypeError
,
141 "dbm mappings have string elements only");
144 if ( dbm_store(dp
->di_dbm
, krec
, drec
, DBM_REPLACE
) < 0 ) {
145 dbm_clearerr(dp
->di_dbm
);
146 err_setstr(DbmError
, "Cannot add item to database");
150 if ( dbm_error(dp
->di_dbm
) ) {
151 dbm_clearerr(dp
->di_dbm
);
152 err_setstr(DbmError
, "");
158 static mapping_methods dbm_as_mapping
= {
159 (inquiry
)dbm_length
, /*mp_length*/
160 (binaryfunc
)dbm_subscript
, /*mp_subscript*/
161 (objobjargproc
)dbm_ass_sub
, /*mp_ass_subscript*/
166 register dbmobject
*dp
;
169 if ( !getnoarg(args
) )
172 dbm_close(dp
->di_dbm
);
180 register dbmobject
*dp
;
183 register object
*v
, *item
;
189 v
= newlistobject(0);
192 for (key
= dbm_firstkey(dp
->di_dbm
); key
.dptr
;
193 key
= dbm_nextkey(dp
->di_dbm
)) {
194 item
= newsizedstringobject(key
.dptr
, key
.dsize
);
199 err
= addlistitem(v
, item
);
210 dbm_has_key(dp
, args
)
211 register dbmobject
*dp
;
216 if (!getargs(args
, "s#", &key
.dptr
, &key
.dsize
))
218 val
= dbm_fetch(dp
->di_dbm
, key
);
219 return newintobject(val
.dptr
!= NULL
);
222 static struct methodlist dbm_methods
[] = {
223 {"close", (method
)dbm__close
},
224 {"keys", (method
)dbm_keys
},
225 {"has_key", (method
)dbm_has_key
},
226 {NULL
, NULL
} /* sentinel */
230 dbm_getattr(dp
, name
)
234 return findmethod(dbm_methods
, (object
*)dp
, name
);
237 static typeobject Dbmtype
= {
238 OB_HEAD_INIT(&Typetype
)
243 (destructor
)dbm_dealloc
, /*tp_dealloc*/
245 (getattrfunc
)dbm_getattr
, /*tp_getattr*/
250 0, /*tp_as_sequence*/
251 &dbm_as_mapping
, /*tp_as_mapping*/
254 /* ----------------------------------------------------------------- */
266 if ( !newgetargs(args
, "s|si", &name
, &flags
, &mode
) )
268 if ( strcmp(flags
, "r") == 0 )
270 else if ( strcmp(flags
, "w") == 0 )
272 else if ( strcmp(flags
, "rw") == 0 ) /* B/W compat */
273 iflags
= O_RDWR
|O_CREAT
;
274 else if ( strcmp(flags
, "c") == 0 )
275 iflags
= O_RDWR
|O_CREAT
;
276 else if ( strcmp(flags
, "n") == 0 )
277 iflags
= O_RDWR
|O_CREAT
|O_TRUNC
;
280 "Flags should be one of 'r', 'w', 'c' or 'n'");
283 return newdbmobject(name
, iflags
, mode
);
286 static struct methodlist dbmmodule_methods
[] = {
287 { "open", (method
)dbmopen
, 1 },
295 m
= initmodule("dbm", dbmmodule_methods
);
296 d
= getmoduledict(m
);
297 DbmError
= newstringobject("dbm.error");
298 if ( DbmError
== NULL
|| dictinsert(d
, "error", DbmError
) )
299 fatal("can't define dbm.error");