chiark / gitweb /
ro compiles
[chiark-tcl.git] / cdb / writeable.c
index ccdd6aa3de4e15b4a974544060d3ab2706ae59bf..e584b79c29dc75cb2b129efb61a1d49f83060fc4 100644 (file)
@@ -15,10 +15,6 @@ static void maybe_close(int fd) {
   if (fd>=0) close(fd);
 }
 
-#define PE(m) do{                                              \
-    rc= cht_posixerr(ip, errno, "failed to " m); goto x_rc;    \
-  }while(0)
-
 /*==================== Subsystems and subtypes ====================*/
 
 /*---------- Pathbuf ----------*/
@@ -92,6 +88,15 @@ static void ht_maybeupdate(HashTable *ht, const char *key,
   Tcl_SetHashValue(he, val_eat);
 }
 
+static const HashValue *ht_lookup(HashTable *ht, const char *key) {
+  Tcl_HashEntry *he;
+  
+  he= Tcl_FindHashEntry(&ht->t, key);
+  if (!he) return 0;
+  
+  return Tcl_GetHashValue(he);
+}
+
 static int ht_forall(HashTable *ht,
                     int (*fn)(const char *key, HashValue *val,
                               struct ht_forall_ctx *ctx),
@@ -407,7 +412,7 @@ int cht_do_cdbwr_open(ClientData cd, Tcl_Interp *ip, const char *pathb,
   }
 
   mainfd= open(pathbuf_sfx(&rw->pbsome,".main"), O_RDONLY);
-  if (mainfd<0) PE("open exist3ing database file .main");
+  if (mainfd<0) PE("open existing database file .main");
   rc= acquire_lock(ip, &rw->pbsome, &rw->lock_fd);  if (rc) goto x_rc;
 
   r= fstat(mainfd, &stab);  if (r) PE("fstat .main");
@@ -834,3 +839,41 @@ int cht_do_cdbwr_delete(ClientData cd, Tcl_Interp *ip, void *rw_v,
                        const char *key) {
   return update(ip, rw_v, key, 0, 0);
 }
+
+/*---------- Lookups ----------*/
+
+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;
+
+  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; }
+  }
+
+  return cht_cdb_lookup_cdb(ip, &rw->cdb, key, strlen(key), data_r, len_r);
+} 
+
+int cht_do_cdbwr_lookup(ClientData cd, Tcl_Interp *ip, void *rw_v,
+                       const char *key, Tcl_Obj *def,
+                       Tcl_Obj **result) {
+  const Byte *data;
+  int dlen, r;
+  
+  r= lookup_rw(ip, rw_v, key, &data, &dlen);  if (r) return r;
+  return cht_cdb_donesomelookup(ip, rw_v, def, result, data, dlen,
+                               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) {
+  const Byte *data;
+  int dlen, r;
+  
+  r= lookup_rw(ip, rw_v, key, &data, &dlen);  if (r) return r;
+  return cht_cdb_donesomelookup(ip, rw_v, def, result, data, dlen,
+                               cht_cdb_storeanswer_hb);
+}