2 * base code for various Tcl extensions
3 * Copyright 2006 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 #include "chiark-tcl-base.h"
23 static void enum_nt_dup(Tcl_Obj *src, Tcl_Obj *dup) {
24 dup->internalRep= src->internalRep;
25 dup->typePtr= src->typePtr;
28 static void enum_nt_ustr(Tcl_Obj *o) {
32 static int enum_nt_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
36 Tcl_ObjType cht_enum_nearlytype = {
38 0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
41 Tcl_ObjType cht_enum1_nearlytype = {
43 0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
46 static void report_bad(Tcl_Interp *ip, const char *what, const char *supplied,
47 const void *first, size_t each,
48 int (*isvalid)(const void *entry),
49 void (*appres)(Tcl_Interp *ip, const void *entry)) {
53 for (entry=first; isvalid(entry); entry+=each);
54 count= (entry - (const Byte*)first) / each;
57 Tcl_AppendResult(ip, "bad ",what," \"",supplied,"\": must be",(char*)0);
59 for (i=0, entry=first; i<count; i++, entry+=each) {
62 i+1==count ? ", or " :
69 static const char *enum_str(const void *p) { return *(const char*const*)p; }
70 static int isvalid_enum(const void *p) { return !!enum_str(p); }
71 static void appres_enum(Tcl_Interp *ip, const void *p) {
72 Tcl_AppendResult(ip, enum_str(p), (char*)0);
75 const void *cht_enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
76 const void *firstentry, size_t entrysize,
78 const char *supplied, *found;
81 if (o->typePtr == &cht_enum_nearlytype &&
82 o->internalRep.twoPtrValue.ptr1 == firstentry)
83 return o->internalRep.twoPtrValue.ptr2;
85 supplied= Tcl_GetStringFromObj(o,0); assert(supplied);
87 (found= *(const char*const*)ep) && strcmp(supplied,found);
92 o->typePtr= &cht_enum_nearlytype;
93 o->internalRep.twoPtrValue.ptr1= (void*)firstentry;
94 o->internalRep.twoPtrValue.ptr2= (void*)ep;
98 report_bad(ip,what,supplied, firstentry,entrysize, isvalid_enum,appres_enum);
102 static int isvalid_enum1(const void *p) { return !!*(const char*)p; }
103 static void appres_enum1(Tcl_Interp *ip, const void *p) {
105 buf[0]= *(const char*)p;
107 Tcl_AppendResult(ip, buf, (char*)0);
110 int cht_enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
111 const char *opts, const char *what) {
112 const char *supplied, *fp;
114 if (o->typePtr != &cht_enum1_nearlytype ||
115 o->internalRep.twoPtrValue.ptr1 != opts) {
117 supplied= Tcl_GetStringFromObj(o,0); assert(supplied);
119 if (!(strlen(supplied) == 1 &&
120 (fp= strchr(opts, supplied[0])))) {
121 report_bad(ip,what,supplied, opts,1, isvalid_enum1,appres_enum1);
126 o->typePtr= &cht_enum1_nearlytype;
127 o->internalRep.twoPtrValue.ptr1= (void*)opts;
128 o->internalRep.twoPtrValue.ptr2= (void*)fp;
130 return (const char*)o->internalRep.twoPtrValue.ptr2 - opts;