9 static void enum_nt_dup(Tcl_Obj *src, Tcl_Obj *dup) {
10 dup->internalRep= src->internalRep;
11 dup->typePtr= src->typePtr;
14 static void enum_nt_ustr(Tcl_Obj *o) {
18 static int enum_nt_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
22 Tcl_ObjType enum_nearlytype = {
24 0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
27 Tcl_ObjType enum1_nearlytype = {
29 0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
32 static void report_bad(Tcl_Interp *ip, const char *what, const char *supplied,
33 const void *first, size_t each,
34 int (*isvalid)(const void *entry),
35 void (*appres)(Tcl_Interp *ip, const void *entry)) {
39 for (entry=first; isvalid(entry); entry+=each);
40 count= (entry - (const Byte*)first) / each;
43 Tcl_AppendResult(ip, "bad ",what," \"",supplied,"\": must be",(char*)0);
45 for (i=0, entry=first; i<count; i++, entry+=each) {
48 i+1==count ? ", or " :
55 static const char *enum_str(const void *p) { return *(const char*const*)p; }
56 static int isvalid_enum(const void *p) { return !!enum_str(p); }
57 static void appres_enum(Tcl_Interp *ip, const void *p) {
58 Tcl_AppendResult(ip, enum_str(p), (char*)0);
61 const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
62 const void *firstentry, size_t entrysize,
64 const char *supplied, *found;
67 if (o->typePtr == &enum_nearlytype &&
68 o->internalRep.twoPtrValue.ptr1 == firstentry)
69 return o->internalRep.twoPtrValue.ptr2;
71 supplied= Tcl_GetStringFromObj(o,0); assert(supplied);
73 (found= *(const char*const*)ep) && strcmp(supplied,found);
78 o->typePtr= &enum_nearlytype;
79 o->internalRep.twoPtrValue.ptr1= (void*)firstentry;
80 o->internalRep.twoPtrValue.ptr2= (void*)ep;
84 report_bad(ip,what,supplied, firstentry,entrysize, isvalid_enum,appres_enum);
88 static int isvalid_enum1(const void *p) { return !!*(const char*)p; }
89 static void appres_enum1(Tcl_Interp *ip, const void *p) {
91 buf[0]= *(const char*)p;
93 Tcl_AppendResult(ip, buf, (char*)0);
96 int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
97 const char *opts, const char *what) {
98 const char *supplied, *fp;
100 if (o->typePtr != &enum1_nearlytype ||
101 o->internalRep.twoPtrValue.ptr1 != opts) {
103 supplied= Tcl_GetStringFromObj(o,0); assert(supplied);
105 if (!(strlen(supplied) == 1 &&
106 (fp= strchr(opts, supplied[0])))) {
107 report_bad(ip,what,supplied, opts,1, isvalid_enum1,appres_enum1);
112 o->typePtr= &enum1_nearlytype;
113 o->internalRep.twoPtrValue.ptr1= (void*)opts;
114 o->internalRep.twoPtrValue.ptr2= (void*)fp;
116 return (const char*)o->internalRep.twoPtrValue.ptr2 - opts;