chiark / gitweb /
hbytes overwrite; hbytes repeat
[chiark-tcl.git] / base / enum.c
index 1a66aa34629961b696d084d2bef9784de694be78..8041cd378a5b05f1c486f2f4049700299f91c631 100644 (file)
@@ -2,6 +2,8 @@
  *
  */
 
+#include <string.h>
+
 #include "hbytes.h"
 
 static void enum_nt_dup(Tcl_Obj *src, Tcl_Obj *dup) {
@@ -21,8 +23,42 @@ Tcl_ObjType enum_nearlytype = {
   0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
 };
 
+Tcl_ObjType enum1_nearlytype = {
+  "enum1-nearly",
+  0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
+};
+
+static void report_bad(Tcl_Interp *ip, const char *what, const char *supplied,
+                      const void *first, size_t each,
+                      int (*isvalid)(const void *entry),
+                      void (*appres)(Tcl_Interp *ip, const void *entry)) {
+  int count, i;
+  const Byte *entry;
+
+  for (entry=first; isvalid(entry); entry+=each);
+  count= (entry - (const Byte*)first) / each;
+
+  Tcl_ResetResult(ip);
+  Tcl_AppendResult(ip, "bad ",what," \"",supplied,"\": must be",(char*)0);
+
+  for (i=0, entry=first; i<count; i++, entry+=each) {
+    Tcl_AppendResult(ip,
+                    (char*)(i==0 ? " " :
+                            i+1==count ? ", or " :
+                            ", "),
+                    (char*)0);
+    appres(ip,entry);
+  }
+}
+
+static const char *enum_str(const void *p) { return *(const char*const*)p; }
+static int isvalid_enum(const void *p) { return !!enum_str(p); }
+static void appres_enum(Tcl_Interp *ip, const void *p) {
+  Tcl_AppendResult(ip, enum_str(p), (char*)0);
+}
+
 const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
-                                   size_t entrysize, const void *firstentry,
+                                   const void *firstentry, size_t entrysize,
                                    const char *what) {
   const char *supplied, *found;
   const char *ep;
@@ -44,11 +80,37 @@ const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
     return ep;
   }
 
-  Tcl_ResetResult(ip);
-  Tcl_AppendResult(ip, "invalid ",what," - must be one of:",(char*)0);
-  for (ep= firstentry;
-       (found= *(const char*const*)ep);
-       ep += entrysize)
-    Tcl_AppendResult(ip, " ",found,(char*)0);
+  report_bad(ip,what,supplied, firstentry,entrysize, isvalid_enum,appres_enum);
   return 0;
 }
+
+static int isvalid_enum1(const void *p) { return !!*(const char*)p; }
+static void appres_enum1(Tcl_Interp *ip, const void *p) {
+  char buf[2];
+  buf[0]= *(const char*)p;
+  buf[1]= 0;
+  Tcl_AppendResult(ip, buf, (char*)0);
+}
+
+int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
+                            const char *opts, const char *what) {
+  const char *supplied, *fp;
+  
+  if (o->typePtr != &enum1_nearlytype ||
+      o->internalRep.twoPtrValue.ptr1 != opts) {
+
+    supplied= Tcl_GetStringFromObj(o,0);  assert(supplied);
+    
+    if (!(strlen(supplied) == 1 &&
+         (fp= strchr(opts, supplied[0])))) {
+      report_bad(ip,what,supplied, opts,1, isvalid_enum1,appres_enum1);
+      return -1;
+    }
+    
+    objfreeir(o);
+    o->typePtr= &enum1_nearlytype;
+    o->internalRep.twoPtrValue.ptr1= (void*)opts;
+    o->internalRep.twoPtrValue.ptr2= (void*)fp;
+  }
+  return (const char*)o->internalRep.twoPtrValue.ptr2 - opts;
+}