Fixed bug in time-to-midnight calculation.
[python.git] / Modules / dbmmodule.c
blob40d06fc26803463d9a1d1aa67b6867715adec060
2 /* DBM module using dictionary interface */
5 #include "Python.h"
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
11 /* Some Linux systems install gdbm/ndbm.h, but not ndbm.h. This supports
12 * whichever configure was able to locate.
14 #if defined(HAVE_NDBM_H)
15 #include <ndbm.h>
16 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
17 static char *which_dbm = "ndbm";
18 #else
19 static char *which_dbm = "GNU gdbm"; /* EMX port of GDBM */
20 #endif
21 #elif defined(HAVE_GDBM_NDBM_H)
22 #include <gdbm/ndbm.h>
23 static char *which_dbm = "GNU gdbm";
24 #elif defined(HAVE_BERKDB_H)
25 #include <db.h>
26 static char *which_dbm = "Berkeley DB";
27 #else
28 #error "No ndbm.h available!"
29 #endif
31 typedef struct {
32 PyObject_HEAD
33 int di_size; /* -1 means recompute */
34 DBM *di_dbm;
35 } dbmobject;
37 static PyTypeObject Dbmtype;
39 #define is_dbmobject(v) ((v)->ob_type == &Dbmtype)
40 #define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \
41 { PyErr_SetString(DbmError, "DBM object has already been closed"); \
42 return NULL; }
44 static PyObject *DbmError;
46 static PyObject *
47 newdbmobject(char *file, int flags, int mode)
49 dbmobject *dp;
51 dp = PyObject_New(dbmobject, &Dbmtype);
52 if (dp == NULL)
53 return NULL;
54 dp->di_size = -1;
55 if ( (dp->di_dbm = dbm_open(file, flags, mode)) == 0 ) {
56 PyErr_SetFromErrno(DbmError);
57 Py_DECREF(dp);
58 return NULL;
60 return (PyObject *)dp;
63 /* Methods */
65 static void
66 dbm_dealloc(register dbmobject *dp)
68 if ( dp->di_dbm )
69 dbm_close(dp->di_dbm);
70 PyObject_Del(dp);
73 static int
74 dbm_length(dbmobject *dp)
76 if (dp->di_dbm == NULL) {
77 PyErr_SetString(DbmError, "DBM object has already been closed");
78 return -1;
80 if ( dp->di_size < 0 ) {
81 datum key;
82 int size;
84 size = 0;
85 for ( key=dbm_firstkey(dp->di_dbm); key.dptr;
86 key = dbm_nextkey(dp->di_dbm))
87 size++;
88 dp->di_size = size;
90 return dp->di_size;
93 static PyObject *
94 dbm_subscript(dbmobject *dp, register PyObject *key)
96 datum drec, krec;
97 int tmp_size;
99 if (!PyArg_Parse(key, "s#", &krec.dptr, &tmp_size) )
100 return NULL;
102 krec.dsize = tmp_size;
103 check_dbmobject_open(dp);
104 drec = dbm_fetch(dp->di_dbm, krec);
105 if ( drec.dptr == 0 ) {
106 PyErr_SetString(PyExc_KeyError,
107 PyString_AS_STRING((PyStringObject *)key));
108 return NULL;
110 if ( dbm_error(dp->di_dbm) ) {
111 dbm_clearerr(dp->di_dbm);
112 PyErr_SetString(DbmError, "");
113 return NULL;
115 return PyString_FromStringAndSize(drec.dptr, drec.dsize);
118 static int
119 dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
121 datum krec, drec;
122 int tmp_size;
124 if ( !PyArg_Parse(v, "s#", &krec.dptr, &tmp_size) ) {
125 PyErr_SetString(PyExc_TypeError,
126 "dbm mappings have string indices only");
127 return -1;
129 krec.dsize = tmp_size;
130 if (dp->di_dbm == NULL) {
131 PyErr_SetString(DbmError, "DBM object has already been closed");
132 return -1;
134 dp->di_size = -1;
135 if (w == NULL) {
136 if ( dbm_delete(dp->di_dbm, krec) < 0 ) {
137 dbm_clearerr(dp->di_dbm);
138 PyErr_SetString(PyExc_KeyError,
139 PyString_AS_STRING((PyStringObject *)v));
140 return -1;
142 } else {
143 if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) {
144 PyErr_SetString(PyExc_TypeError,
145 "dbm mappings have string elements only");
146 return -1;
148 drec.dsize = tmp_size;
149 if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) {
150 dbm_clearerr(dp->di_dbm);
151 PyErr_SetString(DbmError,
152 "cannot add item to database");
153 return -1;
156 if ( dbm_error(dp->di_dbm) ) {
157 dbm_clearerr(dp->di_dbm);
158 PyErr_SetString(DbmError, "");
159 return -1;
161 return 0;
164 static PyMappingMethods dbm_as_mapping = {
165 (inquiry)dbm_length, /*mp_length*/
166 (binaryfunc)dbm_subscript, /*mp_subscript*/
167 (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/
170 static PyObject *
171 dbm__close(register dbmobject *dp, PyObject *args)
173 if (!PyArg_ParseTuple(args, ":close"))
174 return NULL;
175 if (dp->di_dbm)
176 dbm_close(dp->di_dbm);
177 dp->di_dbm = NULL;
178 Py_INCREF(Py_None);
179 return Py_None;
182 static PyObject *
183 dbm_keys(register dbmobject *dp, PyObject *args)
185 register PyObject *v, *item;
186 datum key;
187 int err;
189 if (!PyArg_ParseTuple(args, ":keys"))
190 return NULL;
191 check_dbmobject_open(dp);
192 v = PyList_New(0);
193 if (v == NULL)
194 return NULL;
195 for (key = dbm_firstkey(dp->di_dbm); key.dptr;
196 key = dbm_nextkey(dp->di_dbm)) {
197 item = PyString_FromStringAndSize(key.dptr, key.dsize);
198 if (item == NULL) {
199 Py_DECREF(v);
200 return NULL;
202 err = PyList_Append(v, item);
203 Py_DECREF(item);
204 if (err != 0) {
205 Py_DECREF(v);
206 return NULL;
209 return v;
212 static PyObject *
213 dbm_has_key(register dbmobject *dp, PyObject *args)
215 datum key, val;
216 int tmp_size;
218 if (!PyArg_ParseTuple(args, "s#:has_key", &key.dptr, &tmp_size))
219 return NULL;
220 key.dsize = tmp_size;
221 check_dbmobject_open(dp);
222 val = dbm_fetch(dp->di_dbm, key);
223 return PyInt_FromLong(val.dptr != NULL);
226 static PyObject *
227 dbm_get(register dbmobject *dp, PyObject *args)
229 datum key, val;
230 PyObject *defvalue = Py_None;
231 int tmp_size;
233 if (!PyArg_ParseTuple(args, "s#|O:get",
234 &key.dptr, &tmp_size, &defvalue))
235 return NULL;
236 key.dsize = tmp_size;
237 check_dbmobject_open(dp);
238 val = dbm_fetch(dp->di_dbm, key);
239 if (val.dptr != NULL)
240 return PyString_FromStringAndSize(val.dptr, val.dsize);
241 else {
242 Py_INCREF(defvalue);
243 return defvalue;
247 static PyObject *
248 dbm_setdefault(register dbmobject *dp, PyObject *args)
250 datum key, val;
251 PyObject *defvalue = NULL;
252 int tmp_size;
254 if (!PyArg_ParseTuple(args, "s#|S:setdefault",
255 &key.dptr, &tmp_size, &defvalue))
256 return NULL;
257 key.dsize = tmp_size;
258 check_dbmobject_open(dp);
259 val = dbm_fetch(dp->di_dbm, key);
260 if (val.dptr != NULL)
261 return PyString_FromStringAndSize(val.dptr, val.dsize);
262 if (defvalue == NULL) {
263 defvalue = PyString_FromStringAndSize(NULL, 0);
264 if (defvalue == NULL)
265 return NULL;
267 else
268 Py_INCREF(defvalue);
269 val.dptr = PyString_AS_STRING(defvalue);
270 val.dsize = PyString_GET_SIZE(defvalue);
271 if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) {
272 dbm_clearerr(dp->di_dbm);
273 PyErr_SetString(DbmError, "cannot add item to database");
274 return NULL;
276 return defvalue;
279 static PyMethodDef dbm_methods[] = {
280 {"close", (PyCFunction)dbm__close, METH_VARARGS,
281 "close()\nClose the database."},
282 {"keys", (PyCFunction)dbm_keys, METH_VARARGS,
283 "keys() -> list\nReturn a list of all keys in the database."},
284 {"has_key", (PyCFunction)dbm_has_key, METH_VARARGS,
285 "has_key(key} -> boolean\nReturn true iff key is in the database."},
286 {"get", (PyCFunction)dbm_get, METH_VARARGS,
287 "get(key[, default]) -> value\n"
288 "Return the value for key if present, otherwise default."},
289 {"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS,
290 "setdefault(key[, default]) -> value\n"
291 "Return the value for key if present, otherwise default. If key\n"
292 "is not in the database, it is inserted with default as the value."},
293 {NULL, NULL} /* sentinel */
296 static PyObject *
297 dbm_getattr(dbmobject *dp, char *name)
299 return Py_FindMethod(dbm_methods, (PyObject *)dp, name);
302 static PyTypeObject Dbmtype = {
303 PyObject_HEAD_INIT(NULL)
305 "dbm.dbm",
306 sizeof(dbmobject),
308 (destructor)dbm_dealloc, /*tp_dealloc*/
309 0, /*tp_print*/
310 (getattrfunc)dbm_getattr, /*tp_getattr*/
311 0, /*tp_setattr*/
312 0, /*tp_compare*/
313 0, /*tp_repr*/
314 0, /*tp_as_number*/
315 0, /*tp_as_sequence*/
316 &dbm_as_mapping, /*tp_as_mapping*/
319 /* ----------------------------------------------------------------- */
321 static PyObject *
322 dbmopen(PyObject *self, PyObject *args)
324 char *name;
325 char *flags = "r";
326 int iflags;
327 int mode = 0666;
329 if ( !PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode) )
330 return NULL;
331 if ( strcmp(flags, "r") == 0 )
332 iflags = O_RDONLY;
333 else if ( strcmp(flags, "w") == 0 )
334 iflags = O_RDWR;
335 else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */
336 iflags = O_RDWR|O_CREAT;
337 else if ( strcmp(flags, "c") == 0 )
338 iflags = O_RDWR|O_CREAT;
339 else if ( strcmp(flags, "n") == 0 )
340 iflags = O_RDWR|O_CREAT|O_TRUNC;
341 else {
342 PyErr_SetString(DbmError,
343 "arg 2 to open should be 'r', 'w', 'c', or 'n'");
344 return NULL;
346 return newdbmobject(name, iflags, mode);
349 static PyMethodDef dbmmodule_methods[] = {
350 { "open", (PyCFunction)dbmopen, METH_VARARGS,
351 "open(path[, flag[, mode]]) -> mapping\n"
352 "Return a database object."},
353 { 0, 0 },
356 PyMODINIT_FUNC
357 initdbm(void) {
358 PyObject *m, *d, *s;
360 Dbmtype.ob_type = &PyType_Type;
361 m = Py_InitModule("dbm", dbmmodule_methods);
362 d = PyModule_GetDict(m);
363 if (DbmError == NULL)
364 DbmError = PyErr_NewException("dbm.error", NULL, NULL);
365 s = PyString_FromString(which_dbm);
366 if (s != NULL) {
367 PyDict_SetItemString(d, "library", s);
368 Py_DECREF(s);
370 if (DbmError != NULL)
371 PyDict_SetItemString(d, "error", DbmError);