4 #include "chiark-tcl-base.h"
9 const IdDataSpec *idds;
16 IdDataAssocData *assoc;
20 static void assoc_del(ClientData assoc_cd, Tcl_Interp *ip) {
21 IdDataAssocData *assoc;
26 for (ix=0, p=assoc->a; ix<assoc->n; ix++, p++) {
29 assert(*(int*)v == ix);
31 assoc->idds->destroyitem(ip,v);
38 static void setobjdataid(Tcl_Interp *interp, Tcl_Obj *o,
39 int ix, const IdDataSpec *idds) {
41 IdDataAssocData *assoc;
43 assoc= Tcl_GetAssocData(interp, (char*)idds->assockey, 0);
45 assoc= TALLOC(sizeof(*assoc));
49 Tcl_SetAssocData(interp, (char*)idds->assockey, assoc_del, assoc);
52 dv= TALLOC(sizeof(*dv));
57 o->typePtr= &cht_tabledataid_nearlytype;
58 o->internalRep.otherValuePtr= dv;
61 int cht_tabledataid_parse(Tcl_Interp *ip, Tcl_Obj *o, const IdDataSpec *idds) {
65 IdDataAssocData *assoc;
68 if (o->typePtr != &cht_tabledataid_nearlytype) goto convert;
70 dv= o->internalRep.otherValuePtr;
71 if (dv->interp != ip) goto convert;
73 if (dv->assoc->idds != idds) goto convert;
78 l= strlen(idds->valprefix);
79 str= Tcl_GetStringFromObj(o,0);
80 if (memcmp(str,idds->valprefix,l))
81 return cht_staticerr(ip,"bad id (wrong prefix)",0);
83 errno=0; ul=strtoul(str+l,&ep,10);
84 if (errno || *ep) return cht_staticerr(ip,"bad id number",0);
85 if (ul > INT_MAX) return cht_staticerr(ip,"out of range id number",0);
88 setobjdataid(ip,o,ul,idds);
92 int cht_pat_iddata(Tcl_Interp *ip, Tcl_Obj *o, void **rv, const IdDataSpec *idds) {
95 IdDataAssocData *assoc;
98 rc= cht_tabledataid_parse(ip,o,idds);
101 dv= o->internalRep.otherValuePtr;
105 if (ix >= assoc->n || !(r= assoc->a[ix]))
106 return cht_staticerr(ip,"id not in use",0);
108 assert(*(int*)r == ix);
114 Tcl_Obj *cht_ret_iddata(Tcl_Interp *ip, void *val, const IdDataSpec *idds) {
115 /* Command procedure implementation may set val->ix,
116 * ie *(int*)val, to -1, to mean it's a new struct. Otherwise
117 * it had better be an old one !
121 IdDataAssocData *assoc;
125 setobjdataid(ip,o,0,idds);
126 dv= o->internalRep.otherValuePtr;
131 for (ix=0; ix<assoc->n && assoc->a[ix]; ix++);
135 assoc->a= TREALLOC(assoc->a, assoc->n*sizeof(*assoc->a));
136 while (ix<assoc->n) assoc->a[ix++]=0;
142 assert(val == assoc->a[ix]);
145 Tcl_InvalidateStringRep(o);
149 void cht_tabledataid_disposing(Tcl_Interp *ip, void *val, const IdDataSpec *idds) {
150 IdDataAssocData *assoc;
156 assoc= Tcl_GetAssocData(ip, (char*)idds->assockey, 0);
157 assert(assoc->a[ix] == val);
162 static int tabledataid_nt_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
166 static void tabledataid_nt_free(Tcl_Obj *o) {
167 TFREE(o->internalRep.otherValuePtr);
168 o->internalRep.otherValuePtr= 0;
171 static void tabledataid_nt_dup(Tcl_Obj *src, Tcl_Obj *dup) {
172 IdDataValue *sv, *dv;
174 sv= src->internalRep.otherValuePtr;
175 dv= TALLOC(sizeof(*dv));
177 dup->typePtr= &cht_tabledataid_nearlytype;
178 dup->internalRep.otherValuePtr= dv;
181 static void tabledataid_nt_ustr(Tcl_Obj *o) {
182 const IdDataValue *dv;
183 const IdDataAssocData *assoc;
184 const IdDataSpec *idds;
187 dv= o->internalRep.otherValuePtr;
191 snprintf(buf,sizeof(buf), "%d", dv->ix);
192 cht_obj_updatestr_vstringls(o,
193 idds->valprefix, strlen(idds->valprefix),
198 Tcl_ObjType cht_tabledataid_nearlytype = {
200 tabledataid_nt_free, tabledataid_nt_dup,
201 tabledataid_nt_ustr, tabledataid_nt_sfa