X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=cdb%2Flookup.c;h=50d5598a3ff20fa719432419d7000121ed580b66;hb=ff05111a769ecdc4becb7960117be4c928d23791;hp=52598efdc1ccc68cb41801be921f2942563439fa;hpb=9eb8c5c8a5caa7264d7d3464cf650baffb42a05c;p=chiark-tcl.git diff --git a/cdb/lookup.c b/cdb/lookup.c index 52598ef..50d5598 100644 --- a/cdb/lookup.c +++ b/cdb/lookup.c @@ -1,21 +1,31 @@ -/**/ +/* + * cdb, cdb-wr - Tcl bindings for tinycdb and a journalling write extension + * Copyright 2006 Ian Jackson + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ #include "chiark_tcl_cdb.h" -int cht_cdb_dosomelookup(Tcl_Interp *ip, void *db_v, - const char *key, Tcl_Obj *def, - Tcl_Obj **result, - int (*somelookup)(Tcl_Interp *ip, void *db_v, - const char *key, - const Byte **data_r, int *len_r), - int (*storeanswer)(Tcl_Interp *ip, Tcl_Obj **result, - const Byte *data, int len)) { - int r, len; - const Byte *data; - - r= somelookup(ip, db_v, key, &data, &len); - if (r) return r; - if (len>0) return storeanswer(ip, result, data, len); +int cht_cdb_donesomelookup(Tcl_Interp *ip, void *db_v, + Tcl_Obj *def, Tcl_Obj **result, + const Byte *data, int dlen, + int (*storeanswer)(Tcl_Interp *ip, Tcl_Obj **result, + const Byte *data, int len)) { + if (dlen>0) return storeanswer(ip, result, data, dlen); if (def) { *result= def; return TCL_OK; } return cht_staticerr(ip, "cdbwr lookup key not found", "CDB NOTFOUND"); } @@ -35,3 +45,23 @@ int cht_cdb_storeanswer_hb(Tcl_Interp *ip, Tcl_Obj **result, *result= cht_ret_hb(ip, val); return TCL_OK; } + +int cht_cdb_lookup_cdb(Tcl_Interp *ip, struct cdb *cdb, + const Byte *key, int klen, + const Byte **data_r, int *len_r) { + int r; + + r= cdb_find(cdb, key, klen); + if (!r) { *data_r= 0; *len_r= -1; return TCL_OK; } + if (r<0) return cht_posixerr(ip, errno, "cdb_find failed"); + assert(r==1); + *len_r= cdb_datalen(cdb); + assert(*len_r > 0); + *data_r= cdb_getdata(cdb); + if (!*data_r) return cht_posixerr(ip, errno, "cdb_getdata failed"); + return TCL_OK; +} + +CHT_INIT(cdb, + CHTI_OTHER(hbytes), + CHTI_COMMANDS(cht_cdbtoplevel_entries))