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