X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=base%2Fenum.c;h=e40b51343b3564cf7d3708d5f0f93f0c927e0f1f;hb=c48252ff1a8cfbd77e1be9717dbcb957bbcf57a8;hp=a4e9de7ff9ad0f0d6d686f97f1c049023db2ccec;hpb=b740135c3efcb63e0c7741384e5fa8ff229d2919;p=chiark-tcl.git diff --git a/base/enum.c b/base/enum.c index a4e9de7..e40b513 100644 --- a/base/enum.c +++ b/base/enum.c @@ -2,12 +2,11 @@ * */ -#include - -#include "hbytes.h" +#include "chiark-tcl-base.h" static void enum_nt_dup(Tcl_Obj *src, Tcl_Obj *dup) { dup->internalRep= src->internalRep; + dup->typePtr= src->typePtr; } static void enum_nt_ustr(Tcl_Obj *o) { @@ -18,23 +17,52 @@ static int enum_nt_sfa(Tcl_Interp *ip, Tcl_Obj *o) { abort(); } -Tcl_ObjType enum_nearlytype = { +Tcl_ObjType cht_enum_nearlytype = { "enum-nearly", 0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa }; -Tcl_ObjType enum1_nearlytype = { +Tcl_ObjType cht_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, +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; itypePtr == &enum_nearlytype && + if (o->typePtr == &cht_enum_nearlytype && o->internalRep.twoPtrValue.ptr1 == firstentry) return o->internalRep.twoPtrValue.ptr2; @@ -44,41 +72,42 @@ const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o, ep += entrysize); if (found) { - objfreeir(o); - o->typePtr= &enum_nearlytype; + cht_objfreeir(o); + o->typePtr= &cht_enum_nearlytype; o->internalRep.twoPtrValue.ptr1= (void*)firstentry; o->internalRep.twoPtrValue.ptr2= (void*)ep; 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; } -int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o, +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 cht_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 || + if (o->typePtr != &cht_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); + report_bad(ip,what,supplied, opts,1, isvalid_enum1,appres_enum1); return -1; } - objfreeir(o); - o->typePtr= &enum1_nearlytype; + cht_objfreeir(o); + o->typePtr= &cht_enum1_nearlytype; o->internalRep.twoPtrValue.ptr1= (void*)opts; o->internalRep.twoPtrValue.ptr2= (void*)fp; }