chiark / gitweb /
writeable lookups now compile
authorian <ian>
Sat, 11 Feb 2006 19:25:36 +0000 (19:25 +0000)
committerian <ian>
Sat, 11 Feb 2006 19:25:36 +0000 (19:25 +0000)
cdb/Makefile
cdb/cdb.tct
cdb/chiark_tcl_cdb.h
cdb/lookup.c [new file with mode: 0644]
cdb/writeable.c

index 58fa5aa99cf9d094bd75dd5e135acde459d17a85..07082e959c886963db08155d4bd91222cd4bb78c 100644 (file)
@@ -1,6 +1,6 @@
 BASE_DIR =     ../base
 EXTBASE =      cdb
-CFILES =       readonly writeable
+CFILES =       readonly writeable lookup
 OTHER_TCTS =   ../hbytes/hbytes-base.tct
 CPPFLAGS +=    -I../hbytes
 LDLIBS +=      ../hbytes/chiark_tcl_hbytes.so -lcdb
index 29325f19cb339e0ebdd0a7e707f70b0caaea3fe1..4df47f3bd54d3e97200b2f675d89e0c038b6c076 100644 (file)
@@ -89,8 +89,8 @@ Table cdbwr Cdbwr_SubCommand
        lookup-hb 0
                db      iddata(&cdbtcl_rwdatabases)
                key     string
-               ?def    hb
-               =>      hb
+               ?def    obj
+               =>      obj
        delete 0
                db      iddata(&cdbtcl_rwdatabases)
                key     string
index 99bcc036c3e68a8fbc2d5ec836fa5dd38da65224..63259a86d1896d5690a7c76ebbe3cbc9ebc82d3d 100644 (file)
 
 extern const IdDataSpec cdbtcl_databases, cdbtcl_rwdatabases;
 
+/*---------- from lookup.c ----------*/
+
+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 cht_cdb_storeanswer_string(Tcl_Interp *ip, Tcl_Obj **result,
+                              const Byte *data, int len);
+int cht_cdb_storeanswer_hb(Tcl_Interp *ip, Tcl_Obj **result,
+                          const Byte *data, int len);
+
 #endif /*CHIARK_TCL_CDB_H*/
diff --git a/cdb/lookup.c b/cdb/lookup.c
new file mode 100644 (file)
index 0000000..52598ef
--- /dev/null
@@ -0,0 +1,37 @@
+/**/
+
+#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);
+  if (def) { *result= def; return TCL_OK; }
+  return cht_staticerr(ip, "cdbwr lookup key not found", "CDB NOTFOUND");
+}
+
+int cht_cdb_storeanswer_string(Tcl_Interp *ip, Tcl_Obj **result,
+                              const Byte *data, int len) {
+  *result= Tcl_NewStringObj(data, len);
+  if (!*result) return cht_staticerr(ip, "Tcl_NewStringObj failed for"
+       " lookup (utf-8 encoding problem?)", "CDB BADSTRING");
+  return TCL_OK;
+}
+  
+int cht_cdb_storeanswer_hb(Tcl_Interp *ip, Tcl_Obj **result,
+                          const Byte *data, int len) {
+  HBytes_Value val;
+  cht_hb_array(&val, data, len);
+  *result= cht_ret_hb(ip, val);
+  return TCL_OK;
+}
index fb7a67f3ffe64781a4b4fe4ea7506331c4abfe39..1059a104e330d2c56e9d53569be47b25044190fc 100644 (file)
@@ -94,9 +94,8 @@ static void ht_maybeupdate(HashTable *ht, const char *key,
 
 static const HashValue *ht_lookup(HashTable *ht, const char *key) {
   Tcl_HashEntry *he;
-  const HashValue *htv;
   
-  he= Tcl_FindHashEntry(ht, key);
+  he= Tcl_FindHashEntry(&ht->t, key);
   if (!he) return 0;
   
   return Tcl_GetHashValue(he);
@@ -847,11 +846,11 @@ int cht_do_cdbwr_delete(ClientData cd, Tcl_Interp *ip, void *rw_v,
 
 /*---------- Lookups ----------*/
 
-static int wrlookup(Tcl_Interp *ip, void *rw_v, const char *key,
+static int lookup_rw(Tcl_Interp *ip, void *rw_v, const char *key,
                    const Byte **data_r, int *len_r /* -1 => notfound */) {
   Rw *rw= rw_v;
   const HashValue *val;
-  int r, l;
+  int r;
 
   val= ht_lookup(&rw->logincore, key);
   if (val) {
@@ -870,50 +869,16 @@ static int wrlookup(Tcl_Interp *ip, void *rw_v, const char *key,
   return TCL_OK;
 } 
 
-int cht_cdb_dosomelookup(ClientData cd, Tcl_Interp *ip, void *db_v,
-                        int (*somelookup)(Tcl_Interp *ip, void *db_v,
-                                          const char *key,
-                                          const Byte **data_r, int *len_r),
-                        const char *key, Tcl_Obj *def,
-                        Tcl_Obj **result) {
-  int r, len;
-  const Byte *data;
-  
-  r= somelookup(ip, db_v, key, &data, &len);
-  if (r) return r;
-  if (len>0) {
-    r= storeanswer();
-  }
-  if (def) {
-    *result= def;
-    return TCL_OK;
-  }
-  return cht_staticerr(ip, "cdbwr lookup key not found", "CDB NOTFOUND");
-  
-  if (len<0) *result= def;
-  else *result= Tcl_NewStringObj(
+int cht_do_cdbwr_lookup(ClientData cd, Tcl_Interp *ip, void *rw_v,
+                       const char *key, Tcl_Obj *def,
+                       Tcl_Obj **result) {
+  return cht_cdb_dosomelookup(ip, rw_v, key, def, result,
+                             lookup_rw, cht_cdb_storeanswer_string);
 }
-
-static int storeanswer
   
-int cht_do_cdbwr_lookup(ClientData cd, Tcl_Interp *ip, void *rw_v,
-                       const char *key, HBytes_Value def,
-                       HBytes_Value *result) {
-    *result= Tcl_NewStringObj(data, len);
-    if (!*result) return cht_staticerr(ip, "Tcl_NewStringObj failed for"
-          " lookup (utf-8 encoding problem?)", "CDB BADSTRING");
-    return TCL_OK;
 int cht_do_cdbwr_lookup_hb(ClientData cd, Tcl_Interp *ip, void *rw_v,
-                          const char *key, HBytes_Value def,
-                          HBytes_Value *result) {
-  Rw *rw= rw_v;
-  int r;
-
-  r= lookup(ip, rw, key, &data, &len);
-  if (r) return r;
-  if (len>0) {
-    cht_hb_array(result, data, len);
-    return TCL_OK;
-  }
-  if (!HBYTES_ISSENTINEL(def)) {
-    cht_hb_array(result, cht_hb_data(&def), cht_hb
+                          const char *key, Tcl_Obj *def,
+                          Tcl_Obj **result) {
+  return cht_cdb_dosomelookup(ip, rw_v, key, def, result,
+                             lookup_rw, cht_cdb_storeanswer_hb);
+}