/* Calls the given function for each of the key/value pairs */ static void tc_db_foreach (void *db, void (*fp) (TCADB * m, void *k, int s, void *u), void *user_data) { TCADB *adb = db; int ksize = 0; void *key; tcadbiterinit (adb); while ((key = tcadbiternext (adb, &ksize)) != NULL) (*fp) (adb, key, ksize, user_data); }
static PyObject * list(PyObject *self,PyObject *args){ const char *dbname; if (!PyArg_ParseTuple(args, "s", &dbname)) return NULL; TCADB *adb = openadb(dbname); PyObject* pDict = PyDict_New(); if(!tcadbiterinit(adb)){ return NULL; } int ksiz; char *kbuf; while((kbuf = tcadbiternext(adb, &ksiz)) != NULL){ int vsiz; char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz); PyDict_SetItemString(pDict,kbuf,Py_BuildValue("s",vbuf)); tcfree(vbuf); tcfree(kbuf); } closeadb(adb); return Py_BuildValue("O",pDict); }
/* perform list command */ static int proclist(const char *name, int sep, int max, bool pv, bool px, const char *fmstr){ TCADB *adb = tcadbnew(); ADBSKEL skel; if(*name == '@'){ setskeltran(&skel); if(!tcadbsetskel(adb, &skel)){ printerr(adb); skel.del(skel.opq); tcadbdel(adb); return 1; } name++; } else if(*name == '%'){ if(!tcadbsetskelmulti(adb, 8)){ printerr(adb); tcadbdel(adb); return 1; } name++; } if(!tcadbopen(adb, name)){ printerr(adb); tcadbdel(adb); return 1; } bool err = false; if(fmstr){ TCLIST *keys = tcadbfwmkeys2(adb, fmstr, max); for(int i = 0; i < tclistnum(keys); i++){ int ksiz; const char *kbuf = tclistval(keys, i, &ksiz); printdata(kbuf, ksiz, px, sep); if(pv){ int vsiz; char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz); if(vbuf){ putchar('\t'); printdata(vbuf, vsiz, px, sep); tcfree(vbuf); } } putchar('\n'); } tclistdel(keys); } else { if(!tcadbiterinit(adb)){ printerr(adb); err = true; } int ksiz; char *kbuf; int cnt = 0; while((kbuf = tcadbiternext(adb, &ksiz)) != NULL){ printdata(kbuf, ksiz, px, sep); if(pv){ int vsiz; char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz); if(vbuf){ putchar('\t'); printdata(vbuf, vsiz, px, sep); tcfree(vbuf); } } putchar('\n'); tcfree(kbuf); if(max >= 0 && ++cnt >= max) break; } } if(!tcadbclose(adb)){ if(!err) printerr(adb); err = true; } tcadbdel(adb); return err ? 1 : 0; }