chiark / gitweb /
need cast for crappy tcl no const correct
[chiark-tcl.git] / base / enum.c
1 /*
2  *
3  */
4
5 #include <string.h>
6
7 #include "hbytes.h"
8
9 static void enum_nt_dup(Tcl_Obj *src, Tcl_Obj *dup) {
10   dup->internalRep= src->internalRep;
11   dup->typePtr= src->typePtr;
12 }
13
14 static void enum_nt_ustr(Tcl_Obj *o) {
15   abort();
16 }
17
18 static int enum_nt_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
19   abort();
20 }
21
22 Tcl_ObjType enum_nearlytype = {
23   "enum-nearly",
24   0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
25 };
26
27 Tcl_ObjType enum1_nearlytype = {
28   "enum1-nearly",
29   0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
30 };
31
32 static void report_bad(Tcl_Interp *ip, const char *what, const char *supplied,
33                        const void *first, size_t each,
34                        int (*isvalid)(const void *entry),
35                        void (*appres)(Tcl_Interp *ip, const void *entry)) {
36   int count, i;
37   const Byte *entry;
38
39   for (entry=first; isvalid(entry); entry+=each);
40   count= (entry - (const Byte*)first) / each;
41
42   Tcl_ResetResult(ip);
43   Tcl_AppendResult(ip, "bad ",what," \"",supplied,"\": must be",(char*)0);
44
45   for (i=0, entry=first; i<count; i++, entry+=each) {
46     Tcl_AppendResult(ip,
47                      (char*)(i==0 ? " " :
48                              i+1==count ? ", or " :
49                              ", "),
50                      (char*)0);
51     appres(ip,entry);
52   }
53 }
54
55 static const char *enum_str(const void *p) { return *(const char*const*)p; }
56 static int isvalid_enum(const void *p) { return !!enum_str(p); }
57 static void appres_enum(Tcl_Interp *ip, const void *p) {
58   Tcl_AppendResult(ip, enum_str(p), (char*)0);
59 }
60
61 const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
62                                     const void *firstentry, size_t entrysize,
63                                     const char *what) {
64   const char *supplied, *found;
65   const char *ep;
66   
67   if (o->typePtr == &enum_nearlytype &&
68       o->internalRep.twoPtrValue.ptr1 == firstentry)
69     return o->internalRep.twoPtrValue.ptr2;
70
71   supplied= Tcl_GetStringFromObj(o,0);  assert(supplied);
72   for (ep= firstentry;
73        (found= *(const char*const*)ep) && strcmp(supplied,found);
74        ep += entrysize);
75
76   if (found) {
77     objfreeir(o);
78     o->typePtr= &enum_nearlytype;
79     o->internalRep.twoPtrValue.ptr1= (void*)firstentry;
80     o->internalRep.twoPtrValue.ptr2= (void*)ep;
81     return ep;
82   }
83
84   report_bad(ip,what,supplied, firstentry,entrysize, isvalid_enum,appres_enum);
85   return 0;
86 }
87
88 static int isvalid_enum1(const void *p) { return !!*(const char*)p; }
89 static void appres_enum1(Tcl_Interp *ip, const void *p) {
90   char buf[2];
91   buf[0]= *(const char*)p;
92   buf[1]= 0;
93   Tcl_AppendResult(ip, buf, (char*)0);
94 }
95
96 int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
97                              const char *opts, const char *what) {
98   const char *supplied, *fp;
99   
100   if (o->typePtr != &enum1_nearlytype ||
101       o->internalRep.twoPtrValue.ptr1 != opts) {
102
103     supplied= Tcl_GetStringFromObj(o,0);  assert(supplied);
104     
105     if (!(strlen(supplied) == 1 &&
106           (fp= strchr(opts, supplied[0])))) {
107       report_bad(ip,what,supplied, opts,1, isvalid_enum1,appres_enum1);
108       return -1;
109     }
110     
111     objfreeir(o);
112     o->typePtr= &enum1_nearlytype;
113     o->internalRep.twoPtrValue.ptr1= (void*)opts;
114     o->internalRep.twoPtrValue.ptr2= (void*)fp;
115   }
116   return (const char*)o->internalRep.twoPtrValue.ptr2 - opts;
117 }