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