2 * cdb, cdb-wr - Tcl bindings for tinycdb and a journalling write extension
3 * Copyright 2006 Ian Jackson
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 #include "chiark_tcl_cdb.h"
28 static void ro_close(Ro *ro) {
33 static void destroy_cdb_idtabcb(Tcl_Interp *ip, void *ro_v) {
38 const IdDataSpec cdbtcl_databases= {
39 "cdb-db", "cdb-opendatabases-table", destroy_cdb_idtabcb
42 int cht_do_cdb_open(ClientData cd, Tcl_Interp *ip,
43 const char *path, void **result) {
47 ro= TALLOC(sizeof(*ro));
49 ro->fd= open(path, O_RDONLY);
50 if (ro->fd<0) PE("open database file");
51 r= cdb_init(&ro->cdb, ro->fd);
52 if (r) PE("initialise cdb");
57 if (ro->fd >= 0) close(ro->fd);
61 int cht_do_cdb_close(ClientData cd, Tcl_Interp *ip, void *ro_v) {
63 cht_tabledataid_disposing(ip, ro_v, &cdbtcl_databases);
68 int cht_do_cdb_lookup(ClientData cd, Tcl_Interp *ip, void *ro_v,
69 Tcl_Obj *keyo, Tcl_Obj *def, Tcl_Obj **result) {
75 key= Tcl_GetStringFromObj(keyo, &klen); assert(key);
77 r= cht_cdb_lookup_cdb(ip, &ro->cdb, key, klen, &data, &dlen);
80 return cht_cdb_donesomelookup(ip, ro_v, def, result, data, dlen,
81 cht_cdb_storeanswer_string);
84 int cht_do_cdb_lookup_hb(ClientData cd, Tcl_Interp *ip, void *ro_v,
85 HBytes_Value key, Tcl_Obj *def, Tcl_Obj **result) {
90 r= cht_cdb_lookup_cdb(ip, &ro->cdb,
91 cht_hb_data(&key), cht_hb_len(&key),
95 return cht_cdb_donesomelookup(ip, ro_v, def, result, data, dlen,
96 cht_cdb_storeanswer_hb);