chiark / gitweb /
dispatch entrypoints
[chiark-tcl.git] / cdb / writeable.c
index fb7a67f3ffe64781a4b4fe4ea7506331c4abfe39..2b7adcc486fa1ada9e41336e2d4aa9cc12c1cd0b 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 ----------*/
@@ -94,9 +90,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);
@@ -417,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");
@@ -847,11 +842,10 @@ 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;
 
   val= ht_lookup(&rw->logincore, key);
   if (val) {
@@ -859,61 +853,33 @@ static int wrlookup(Tcl_Interp *ip, void *rw_v, const char *key,
     else { *data_r= 0; *len_r= -1; return TCL_OK; }
   }
 
-  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;
+  return cht_cdb_lookup_cdb(ip, &rw->cdb, key, strlen(key), data_r, len_r);
 } 
 
-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;
+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= 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(
+  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);
 }
-
-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;
+                          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);
+}
 
-  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
+int cht_do_cdbtoplevel_cdb_wr(ClientData cd, Tcl_Interp *ip,
+                             const Cdbwr_SubCommand* subcmd,
+                             int objc, Tcl_Obj *const *objv) {
+  return subcmd->func((void*)subcmd,ip,objc,objv);
+}