2 * base code for various Tcl extensions
3 * Copyright 2006 Ian Jackson
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 #include "chiark-tcl-base.h"
26 const IdDataSpec *idds;
33 IdDataAssocData *assoc;
37 static void assoc_del(ClientData assoc_cd, Tcl_Interp *ip) {
38 IdDataAssocData *assoc;
43 for (ix=0, p=assoc->a; ix<assoc->n; ix++, p++) {
46 assert(*(int*)v == ix);
48 assoc->idds->destroyitem(ip,v);
55 static void setobjdataid(Tcl_Interp *interp, Tcl_Obj *o,
56 int ix, const IdDataSpec *idds) {
58 IdDataAssocData *assoc;
60 assoc= Tcl_GetAssocData(interp, (char*)idds->assockey, 0);
62 assoc= TALLOC(sizeof(*assoc));
66 Tcl_SetAssocData(interp, (char*)idds->assockey, assoc_del, assoc);
69 dv= TALLOC(sizeof(*dv));
74 o->typePtr= &cht_tabledataid_nearlytype;
75 o->internalRep.otherValuePtr= dv;
78 int cht_tabledataid_parse(Tcl_Interp *ip, Tcl_Obj *o, const IdDataSpec *idds) {
82 IdDataAssocData *assoc;
85 if (o->typePtr != &cht_tabledataid_nearlytype) goto convert;
87 dv= o->internalRep.otherValuePtr;
88 if (dv->interp != ip) goto convert;
90 if (dv->assoc->idds != idds) goto convert;
95 l= strlen(idds->valprefix);
96 str= Tcl_GetStringFromObj(o,0);
97 if (memcmp(str,idds->valprefix,l))
98 return cht_staticerr(ip,"bad id (wrong prefix)",0);
100 errno=0; ul=strtoul(str+l,&ep,10);
101 if (errno || *ep) return cht_staticerr(ip,"bad id number",0);
102 if (ul > INT_MAX) return cht_staticerr(ip,"out of range id number",0);
105 setobjdataid(ip,o,ul,idds);
109 int cht_pat_iddata(Tcl_Interp *ip, Tcl_Obj *o, void **rv, const IdDataSpec *idds) {
112 IdDataAssocData *assoc;
115 rc= cht_tabledataid_parse(ip,o,idds);
118 dv= o->internalRep.otherValuePtr;
122 if (ix >= assoc->n || !(r= assoc->a[ix]))
123 return cht_staticerr(ip,"id not in use",0);
125 assert(*(int*)r == ix);
131 Tcl_Obj *cht_ret_iddata(Tcl_Interp *ip, void *val, const IdDataSpec *idds) {
132 /* Command procedure implementation may set val->ix,
133 * ie *(int*)val, to -1, to mean it's a new struct. Otherwise
134 * it had better be an old one !
138 IdDataAssocData *assoc;
142 setobjdataid(ip,o,0,idds);
143 dv= o->internalRep.otherValuePtr;
148 for (ix=0; ix<assoc->n && assoc->a[ix]; ix++);
152 assoc->a= TREALLOC(assoc->a, assoc->n*sizeof(*assoc->a));
153 while (ix<assoc->n) assoc->a[ix++]=0;
159 assert(val == assoc->a[ix]);
162 Tcl_InvalidateStringRep(o);
166 void cht_tabledataid_disposing(Tcl_Interp *ip, void *val, const IdDataSpec *idds) {
167 IdDataAssocData *assoc;
173 assoc= Tcl_GetAssocData(ip, (char*)idds->assockey, 0);
174 assert(assoc->a[ix] == val);
179 static int tabledataid_nt_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
183 static void tabledataid_nt_free(Tcl_Obj *o) {
184 TFREE(o->internalRep.otherValuePtr);
185 o->internalRep.otherValuePtr= 0;
188 static void tabledataid_nt_dup(Tcl_Obj *src, Tcl_Obj *dup) {
189 IdDataValue *sv, *dv;
191 sv= src->internalRep.otherValuePtr;
192 dv= TALLOC(sizeof(*dv));
194 dup->typePtr= &cht_tabledataid_nearlytype;
195 dup->internalRep.otherValuePtr= dv;
198 static void tabledataid_nt_ustr(Tcl_Obj *o) {
199 const IdDataValue *dv;
200 const IdDataAssocData *assoc;
201 const IdDataSpec *idds;
204 dv= o->internalRep.otherValuePtr;
208 snprintf(buf,sizeof(buf), "%d", dv->ix);
209 cht_obj_updatestr_vstringls(o,
210 idds->valprefix, strlen(idds->valprefix),
215 Tcl_ObjType cht_tabledataid_nearlytype = {
217 tabledataid_nt_free, tabledataid_nt_dup,
218 tabledataid_nt_ustr, tabledataid_nt_sfa