5 #include "chiark-tcl-base.h"
7 static void enum_nt_dup(Tcl_Obj *src, Tcl_Obj *dup) {
8 dup->internalRep= src->internalRep;
9 dup->typePtr= src->typePtr;
12 static void enum_nt_ustr(Tcl_Obj *o) {
16 static int enum_nt_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
20 Tcl_ObjType cht_enum_nearlytype = {
22 0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
25 Tcl_ObjType cht_enum1_nearlytype = {
27 0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
30 static void report_bad(Tcl_Interp *ip, const char *what, const char *supplied,
31 const void *first, size_t each,
32 int (*isvalid)(const void *entry),
33 void (*appres)(Tcl_Interp *ip, const void *entry)) {
37 for (entry=first; isvalid(entry); entry+=each);
38 count= (entry - (const Byte*)first) / each;
41 Tcl_AppendResult(ip, "bad ",what," \"",supplied,"\": must be",(char*)0);
43 for (i=0, entry=first; i<count; i++, entry+=each) {
46 i+1==count ? ", or " :
53 static const char *enum_str(const void *p) { return *(const char*const*)p; }
54 static int isvalid_enum(const void *p) { return !!enum_str(p); }
55 static void appres_enum(Tcl_Interp *ip, const void *p) {
56 Tcl_AppendResult(ip, enum_str(p), (char*)0);
59 const void *cht_enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
60 const void *firstentry, size_t entrysize,
62 const char *supplied, *found;
65 if (o->typePtr == &cht_enum_nearlytype &&
66 o->internalRep.twoPtrValue.ptr1 == firstentry)
67 return o->internalRep.twoPtrValue.ptr2;
69 supplied= Tcl_GetStringFromObj(o,0); assert(supplied);
71 (found= *(const char*const*)ep) && strcmp(supplied,found);
76 o->typePtr= &cht_enum_nearlytype;
77 o->internalRep.twoPtrValue.ptr1= (void*)firstentry;
78 o->internalRep.twoPtrValue.ptr2= (void*)ep;
82 report_bad(ip,what,supplied, firstentry,entrysize, isvalid_enum,appres_enum);
86 static int isvalid_enum1(const void *p) { return !!*(const char*)p; }
87 static void appres_enum1(Tcl_Interp *ip, const void *p) {
89 buf[0]= *(const char*)p;
91 Tcl_AppendResult(ip, buf, (char*)0);
94 int cht_enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
95 const char *opts, const char *what) {
96 const char *supplied, *fp;
98 if (o->typePtr != &cht_enum1_nearlytype ||
99 o->internalRep.twoPtrValue.ptr1 != opts) {
101 supplied= Tcl_GetStringFromObj(o,0); assert(supplied);
103 if (!(strlen(supplied) == 1 &&
104 (fp= strchr(opts, supplied[0])))) {
105 report_bad(ip,what,supplied, opts,1, isvalid_enum1,appres_enum1);
110 o->typePtr= &cht_enum1_nearlytype;
111 o->internalRep.twoPtrValue.ptr1= (void*)opts;
112 o->internalRep.twoPtrValue.ptr2= (void*)fp;
114 return (const char*)o->internalRep.twoPtrValue.ptr2 - opts;