7 #include "chiark-tcl.h"
10 static void enum_nt_dup(Tcl_Obj *src, Tcl_Obj *dup) {
11 dup->internalRep= src->internalRep;
12 dup->typePtr= src->typePtr;
15 static void enum_nt_ustr(Tcl_Obj *o) {
19 static int enum_nt_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
23 Tcl_ObjType cht_enum_nearlytype = {
25 0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
28 Tcl_ObjType cht_enum1_nearlytype = {
30 0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
33 static void report_bad(Tcl_Interp *ip, const char *what, const char *supplied,
34 const void *first, size_t each,
35 int (*isvalid)(const void *entry),
36 void (*appres)(Tcl_Interp *ip, const void *entry)) {
40 for (entry=first; isvalid(entry); entry+=each);
41 count= (entry - (const Byte*)first) / each;
44 Tcl_AppendResult(ip, "bad ",what," \"",supplied,"\": must be",(char*)0);
46 for (i=0, entry=first; i<count; i++, entry+=each) {
49 i+1==count ? ", or " :
56 static const char *enum_str(const void *p) { return *(const char*const*)p; }
57 static int isvalid_enum(const void *p) { return !!enum_str(p); }
58 static void appres_enum(Tcl_Interp *ip, const void *p) {
59 Tcl_AppendResult(ip, enum_str(p), (char*)0);
62 const void *cht_enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
63 const void *firstentry, size_t entrysize,
65 const char *supplied, *found;
68 if (o->typePtr == &cht_enum_nearlytype &&
69 o->internalRep.twoPtrValue.ptr1 == firstentry)
70 return o->internalRep.twoPtrValue.ptr2;
72 supplied= Tcl_GetStringFromObj(o,0); assert(supplied);
74 (found= *(const char*const*)ep) && strcmp(supplied,found);
79 o->typePtr= &cht_enum_nearlytype;
80 o->internalRep.twoPtrValue.ptr1= (void*)firstentry;
81 o->internalRep.twoPtrValue.ptr2= (void*)ep;
85 report_bad(ip,what,supplied, firstentry,entrysize, isvalid_enum,appres_enum);
89 static int isvalid_enum1(const void *p) { return !!*(const char*)p; }
90 static void appres_enum1(Tcl_Interp *ip, const void *p) {
92 buf[0]= *(const char*)p;
94 Tcl_AppendResult(ip, buf, (char*)0);
97 int cht_enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
98 const char *opts, const char *what) {
99 const char *supplied, *fp;
101 if (o->typePtr != &cht_enum1_nearlytype ||
102 o->internalRep.twoPtrValue.ptr1 != opts) {
104 supplied= Tcl_GetStringFromObj(o,0); assert(supplied);
106 if (!(strlen(supplied) == 1 &&
107 (fp= strchr(opts, supplied[0])))) {
108 report_bad(ip,what,supplied, opts,1, isvalid_enum1,appres_enum1);
113 o->typePtr= &cht_enum1_nearlytype;
114 o->internalRep.twoPtrValue.ptr1= (void*)opts;
115 o->internalRep.twoPtrValue.ptr2= (void*)fp;
117 return (const char*)o->internalRep.twoPtrValue.ptr2 - opts;