chiark / gitweb /
use dispatch()
[chiark-tcl.git] / base / enum.c
1 /*
2  * base code for various Tcl extensions
3  * Copyright 2006 Ian Jackson
4  *
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.
9  *
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.
14  *
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
18  * 02110-1301, USA.
19  */
20
21 #include "chiark-tcl-base.h"
22
23 static void enum_nt_dup(Tcl_Obj *src, Tcl_Obj *dup) {
24   dup->internalRep= src->internalRep;
25   dup->typePtr= src->typePtr;
26 }
27
28 static void enum_nt_ustr(Tcl_Obj *o) {
29   abort();
30 }
31
32 static int enum_nt_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
33   abort();
34 }
35
36 Tcl_ObjType cht_enum_nearlytype = {
37   "enum-nearly",
38   0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
39 };
40
41 Tcl_ObjType cht_enum1_nearlytype = {
42   "enum1-nearly",
43   0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
44 };
45
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)) {
50   int count, i;
51   const Byte *entry;
52
53   for (entry=first; isvalid(entry); entry+=each);
54   count= (entry - (const Byte*)first) / each;
55
56   Tcl_ResetResult(ip);
57   Tcl_AppendResult(ip, "bad ",what," \"",supplied,"\": must be",(char*)0);
58
59   for (i=0, entry=first; i<count; i++, entry+=each) {
60     Tcl_AppendResult(ip,
61                      (char*)(i==0 ? " " :
62                              i+1==count ? ", or " :
63                              ", "),
64                      (char*)0);
65     appres(ip,entry);
66   }
67 }
68
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);
73 }
74
75 const void *cht_enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
76                                     const void *firstentry, size_t entrysize,
77                                     const char *what) {
78   const char *supplied, *found;
79   const char *ep;
80   
81   if (o->typePtr == &cht_enum_nearlytype &&
82       o->internalRep.twoPtrValue.ptr1 == firstentry)
83     return o->internalRep.twoPtrValue.ptr2;
84
85   supplied= Tcl_GetStringFromObj(o,0);  assert(supplied);
86   for (ep= firstentry;
87        (found= *(const char*const*)ep) && strcmp(supplied,found);
88        ep += entrysize);
89
90   if (found) {
91     cht_objfreeir(o);
92     o->typePtr= &cht_enum_nearlytype;
93     o->internalRep.twoPtrValue.ptr1= (void*)firstentry;
94     o->internalRep.twoPtrValue.ptr2= (void*)ep;
95     return ep;
96   }
97
98   report_bad(ip,what,supplied, firstentry,entrysize, isvalid_enum,appres_enum);
99   return 0;
100 }
101
102 static int isvalid_enum1(const void *p) { return !!*(const char*)p; }
103 static void appres_enum1(Tcl_Interp *ip, const void *p) {
104   char buf[2];
105   buf[0]= *(const char*)p;
106   buf[1]= 0;
107   Tcl_AppendResult(ip, buf, (char*)0);
108 }
109
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;
113   
114   if (o->typePtr != &cht_enum1_nearlytype ||
115       o->internalRep.twoPtrValue.ptr1 != opts) {
116
117     supplied= Tcl_GetStringFromObj(o,0);  assert(supplied);
118     
119     if (!(strlen(supplied) == 1 &&
120           (fp= strchr(opts, supplied[0])))) {
121       report_bad(ip,what,supplied, opts,1, isvalid_enum1,appres_enum1);
122       return -1;
123     }
124     
125     cht_objfreeir(o);
126     o->typePtr= &cht_enum1_nearlytype;
127     o->internalRep.twoPtrValue.ptr1= (void*)opts;
128     o->internalRep.twoPtrValue.ptr2= (void*)fp;
129   }
130   return (const char*)o->internalRep.twoPtrValue.ptr2 - opts;
131 }