2 * base code for various Tcl extensions
3 * Copyright 2006-2012 Ian Jackson
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "chiark-tcl-base.h"
21 static void enum_nt_dup(Tcl_Obj *src, Tcl_Obj *dup) {
22 dup->internalRep= src->internalRep;
23 dup->typePtr= src->typePtr;
26 static void enum_nt_ustr(Tcl_Obj *o) {
30 static int enum_nt_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
34 Tcl_ObjType cht_enum_nearlytype = {
36 0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
39 Tcl_ObjType cht_enum1_nearlytype = {
41 0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
44 static void report_bad(Tcl_Interp *ip, const char *what, const char *supplied,
45 const void *first, size_t each,
46 int (*isvalid)(const void *entry),
47 void (*appres)(Tcl_Interp *ip, const void *entry)) {
51 for (entry=first; isvalid(entry); entry+=each);
52 count= (entry - (const Byte*)first) / each;
55 Tcl_AppendResult(ip, "bad ",what," \"",supplied,"\": must be",(char*)0);
57 for (i=0, entry=first; i<count; i++, entry+=each) {
60 i+1==count ? ", or " :
67 static const char *enum_str(const void *p) { return *(const char*const*)p; }
68 static int isvalid_enum(const void *p) { return !!enum_str(p); }
69 static void appres_enum(Tcl_Interp *ip, const void *p) {
70 Tcl_AppendResult(ip, enum_str(p), (char*)0);
73 const void *cht_enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
74 const void *firstentry, size_t entrysize,
76 const char *supplied, *found;
79 if (o->typePtr == &cht_enum_nearlytype &&
80 o->internalRep.twoPtrValue.ptr1 == firstentry)
81 return o->internalRep.twoPtrValue.ptr2;
83 supplied= Tcl_GetStringFromObj(o,0); assert(supplied);
85 (found= *(const char*const*)ep) && strcmp(supplied,found);
90 o->typePtr= &cht_enum_nearlytype;
91 o->internalRep.twoPtrValue.ptr1= (void*)firstentry;
92 o->internalRep.twoPtrValue.ptr2= (void*)ep;
96 report_bad(ip,what,supplied, firstentry,entrysize, isvalid_enum,appres_enum);
100 static int isvalid_enum1(const void *p) { return !!*(const char*)p; }
101 static void appres_enum1(Tcl_Interp *ip, const void *p) {
103 buf[0]= *(const char*)p;
105 Tcl_AppendResult(ip, buf, (char*)0);
108 int cht_enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
109 const char *opts, const char *what) {
110 const char *supplied, *fp;
112 if (o->typePtr != &cht_enum1_nearlytype ||
113 o->internalRep.twoPtrValue.ptr1 != opts) {
115 supplied= Tcl_GetStringFromObj(o,0); assert(supplied);
117 if (!(strlen(supplied) == 1 &&
118 (fp= strchr(opts, supplied[0])))) {
119 report_bad(ip,what,supplied, opts,1, isvalid_enum1,appres_enum1);
124 o->typePtr= &cht_enum1_nearlytype;
125 o->internalRep.twoPtrValue.ptr1= (void*)opts;
126 o->internalRep.twoPtrValue.ptr2= (void*)fp;
128 return (const char*)o->internalRep.twoPtrValue.ptr2 - opts;