chiark / gitweb /
all compiles again. more pat to do.
[chiark-tcl.git] / base / enum.c
index 1a66aa34629961b696d084d2bef9784de694be78..a4e9de7ff9ad0f0d6d686f97f1c049023db2ccec 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,13 @@ 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
+};
+
 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;
@@ -52,3 +59,28 @@ const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
     Tcl_AppendResult(ip, " ",found,(char*)0);
   return 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])))) {
+      Tcl_ResetResult(ip);
+      Tcl_AppendResult(ip, "invalid ",what,
+                      " - must be one character from: ", opts);
+      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;
+}