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