chiark / gitweb /
writeable lookups now compile
[chiark-tcl.git] / cdb / writeable.c
index 3613a7b8084642d31c807fde215590f20caf00b1..1059a104e330d2c56e9d53569be47b25044190fc 100644 (file)
@@ -92,15 +92,13 @@ static void ht_maybeupdate(HashTable *ht, const char *key,
   Tcl_SetHashValue(he, val_eat);
 }
 
-static fixme ht_lookup(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);
-  if (!he) return notfound;
-  
-  htv= Tcl_GetHashValue(he);
+  he= Tcl_FindHashEntry(&ht->t, key);
+  if (!he) return 0;
   
+  return Tcl_GetHashValue(he);
 }
 
 static int ht_forall(HashTable *ht,
@@ -848,12 +846,39 @@ int cht_do_cdbwr_delete(ClientData cd, Tcl_Interp *ip, void *rw_v,
 
 /*---------- Lookups ----------*/
 
-static int lookup(Tcl_Interp *ip, Rw *rw, const char *key, ) {
-  ht_lookup(
+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;
+
+  val= ht_lookup(&rw->logincore, key);
+  if (val) {
+    if (val->len) { *data_r= val->data; *len_r= val->len; return TCL_OK; }
+    else { *data_r= 0; *len_r= -1; return TCL_OK; }
+  }
 
-int cht_do_cdbwr_lookup(ClientData cd, Tcl_Interp *ip, void *db,
-                       const char *key, Tcl_Obj *def, Tcl_Obj **result) {
+  r= cdb_find(&rw->cdb, key, strlen(key));
+  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(&rw->cdb);
+  assert(*len_r > 0);
+  *data_r= cdb_getdata(&rw->cdb);
+  if (!*data_r) return cht_posixerr(ip, errno, "cdb_getdata failed");
+  return TCL_OK;
+} 
+
+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);
+}
   
+int cht_do_cdbwr_lookup_hb(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_hb);
 }
-
-int cht_do_cdbwr_lookup_hb(ClientData cd, Tcl_Interp *ip, void *db, const char *key, HBytes_Value def, HBytes_Value *result);