Debian Codegraph @

generic/bltTuple.c

raw symbols SCIP
1 #include <bltInt.h>
2 #include <bltTuple.h>
3
4 /*
5 * array or row pointers
6 * _
7 * |_---> [row index
8 * |_ [#columns array of Tcl_Objs
9 * |_ [tuple pointer ---> [ . . . . . ]
10 * |_
11 * |_
12 * |_
13 * |_
14 * |_
15 * |_
16 * |_
17 * |_
18 * |_
19 * |_
20 * |_
21 * |_
22 * |_
23 * |_
24 * |_
25 * |_
26 * |_
27 * |_
28 *
29 */
30
31 enum TagTypes { TAG_TYPE_NONE, TAG_TYPE_ALL, TAG_TYPE_TAG };
32
33 #define TUPLE_THREAD_KEY "BLT Tuple Data"
34 #define TUPLE_MAGIC ((unsigned int) 0x46170277)
35 #define TUPLE_DESTROYED (1<<0)
36
37 #define TRACE_IGNORE (0)
38 #define TRACE_OK (1)
39
40 #define VALUE(r,c) \
41 ((((c)->index < (r)->nColumns) && ((r)->data != NULL)) \
42 ? (r)->data[(c)->index] : NULL)
43
44 typedef struct Blt_TupleColumnStruct Column;
45 typedef struct Blt_TupleRowStruct Row;
46 typedef struct Blt_TupleTagTableStruct TagTable;
47 typedef struct Blt_TupleTraceStruct Trace;
48 typedef struct Blt_TupleClientStruct TupleClient;
49 typedef struct Blt_TupleNotifierStruct Notifier;
50
51 struct Blt_TupleRowStruct {
52 Tcl_Obj **data; /* Array of Tcl_Objs represents the tuple's
53 * data. */
54 unsigned short nColumns; /* # columns allocated for this tuple. */
55 unsigned short flags; /* Special flags for this tuple. */
56 unsigned int index; /* Index of the tuple in the array. */
57 };
58
59 struct Blt_TupleColumnStruct {
60 char *key; /* Name of column. */
61 int type; /* Type of column:
62 * TYPE_INTEGER
63 * TYPE_NUMBER
64 * TYPE_FLOAT
65 * TYPE_STRING
66 * TYPE_BOOLEAN
67 * TYPE_LIST
68 * TYPE_ARRAY
69 */
70 int nz; /* Number of (non-NULL) defined values
71 * in the column. */
72 unsigned int index; /* Location of column in tuple. */
73 unsigned int flags; /* Special flags for this column. */
74 Blt_HashEntry *hashPtr;
75
76 /* Vector linkage */
77 };
78
79 struct Blt_TupleTagTableStruct {
80 Blt_HashTable tagTable;
81 int refCount;
82 };
83
84 typedef struct {
85 Blt_HashTable instTable; /* Table of tuple objects. */
86 unsigned int nextId;
87 Tcl_Interp *interp;
88 } TupleInterpData;
89
90 typedef struct {
91 Row *rowPtr;
92 Column *columnPtr;
93 } BusyKey;
94
95 /*
96 Blt_TupleRemoveColumn(table, key);
97 Blt_TupleAddColumn(table, key);
98 Blt_TupleRenameColumn(table, key, newKey);
99 Blt_TupleSetColumnType(table, key, type);
100 Blt_TupleGetColumnType(table, key, typePtr);
101 unsigned int Blt_TupleColumnLength(table, key, flags);
102 Tcl_Obj **Blt_TupleGetColumnVector(tuple, key);
103 unsigned int Blt_TupleLength(table);
104 unsigned int Blt_TupleSize(table);
105
106 Blt_TupleTuple Blt_TableGetTuple(table, index);
107 Blt_TupleSetTuple(table, index, tuple);
108 Blt_TupleTuple Blt_TableAppendTuple(tuple);
109 Blt_TableTuple Blt_TupleInsertTuple(table, newPosition);
110 Blt_TupleDeleteTuple(table, tuple);
111 Blt_TupleMoveTuple(table, tuple, newPosition);
112 */
113
114 /*
115 * Blt_TupleObject --
116 *
117 * Structure representing a tuple object.
118 *
119 * Tuple objects are uniquely identified by a combination of
120 * their name and the originating namespace. Two objects in the
121 * same interpreter can have similar names but must reside in
122 * different namespaces.
123 *
124 * The tuple object is an array of tuples. A tuple itself is an
125 * array of Tcl_Objs representing data for each column (or key).
126 * Value are identified by their column name. A tuple does not
127 * need to contain values for all columns. Undefined values
128 * (Tcl_Objs) are NULL.
129 *
130 * A tuple object can be shared by several clients. When a
131 * client wants to use a tuple object, it is given a token that
132 * represents the table. The object tracks its clients by its
133 * token. When all clients have released their tokens, the tuple
134 * object is automatically destroyed.
135 * */
136
137 typedef struct {
138 Tcl_Interp *interp; /* Interpreter associated with this
139 * object. */
140
141 char *name; /* Name of tuple object. */
142
143 Tcl_Namespace *nsPtr;
144
145 Blt_HashEntry *hashPtr; /* Pointer to this object in object
146 * hash table. */
147 Blt_HashTable *tablePtr;
148
149 unsigned int columnsAllocated;
150 unsigned int rowsAllocated;
151 unsigned int nColumns; /* Number columns in each tuple. */
152 unsigned int nRows; /* Number of rows (tuples). */
153 Row **rows;
154 Column **columns; /* Array of column pointers. Supports
155 * mappings of numeric indices back to
156 * columns. */
157 Blt_HashTable columnTable; /* Hash table of columns. Supports
158 * mappings of string names back to
159 * columns. */
160
161 Blt_Pool rowPool; /* Pool that allocates row containers. */
162
163 char *sortCmd; /* Tcl command to invoke to sort
164 * entries. */
165
166 Blt_Chain *clients; /* List of clients using this table */
167
168 unsigned int flags; /* Internal flags. See definitions
169 * below. */
170 unsigned int notifyFlags; /* Notification flags. See definitions
171 * below. */
172 Blt_Chain *notifiers; /* Chain of node event handlers. */
173 Blt_Chain *traces; /* Chain of callbacks for value changes. */
174 Blt_HashTable busyTable; /* Hash table tracking the cells that
175 * have traces currently active. */
176 int notifyHold;
177 } TupleObject;
178
179 /*
180 * Blt_TupleClientStruct --
181 *
182 * A tuple object can be shared by several clients. Each client
183 * allocates this structure which acts as a ticket for using the
184 * table. Clients can designate notifier routines that are
185 * automatically invoked by the tuple object whenever the object
186 * is changed is specific ways by other clients.
187 */
188 struct Blt_TupleClientStruct {
189 unsigned int magic; /* Magic value indicating whether a
190 * generic pointer is really a tuple
191 * token or not. */
192 Blt_ChainLink *linkPtr; /* Pointer into the server's chain of
193 * clients. */
194 Tcl_Interp *interp;
195 TupleObject *tupleObjPtr; /* Pointer to the structure containing
196 * the master information about the
197 * table used by the client. If NULL,
198 * this indicates that the table has
199 * been destroyed (but as of yet, this
200 * client hasn't recognized it). */
201
202 TagTable *tagTablePtr;
203 };
204
205 #define Blt_TupleTableLength(table) ((table)->nRows)
206 #define Blt_TupleTableWidth(table) ((table)->nColumns)
207
208 static Tcl_InterpDeleteProc TupleInterpDeleteProc;
209
210 static Row *
211 NewRow(TupleObject *tupleObjPtr, unsigned int rowIndex)
212 {
213 Row *rowPtr;
214
215 rowPtr = Blt_PoolAllocItem(tupleObjPtr->rowPool, sizeof(Row));
216 rowPtr->data = NULL;
217 rowPtr->nColumns = 0;
218 rowPtr->index = rowIndex;
219 return rowPtr;
220 }
221
222 static void
223 DeleteRow(TupleObject *tupleObjPtr, Row *rowPtr)
224 {
225 if (rowPtr->data != NULL) {
226 int i;
227 Tcl_Obj *objPtr;
228
229 for(i = 0; i < rowPtr->nColumns; i++) {
230 objPtr = rowPtr->data[i];
231 if (objPtr != NULL) {
232 Tcl_DecrRefCount(objPtr);
233 }
234 }
235 Blt_Free(rowPtr->data);
236 }
237 Blt_PoolFreeItem(tupleObjPtr->rowPool, (char *)rowPtr);
238 }
239
240 /*
241 * --------------------------------------------------------------
242 *
243 * NewTupleObject --
244 *
245 * Creates and initializes a new tuple object.
246 *
247 * Results:
248 * Returns a pointer to the new object is successful, NULL
249 * otherwise. If a tuple object can't be generated,
250 * interp->result will contain an error message.
251 *
252 * --------------------------------------------------------------
253 */
254 static TupleObject *
255 NewTupleObject(
256 TupleInterpData *dataPtr,
257 Tcl_Interp *interp,
258 CONST char *name)
259 {
260 TupleObject *tupleObjPtr;
261 int isNew;
262 Blt_HashEntry *hPtr;
263
264 tupleObjPtr = Blt_Calloc(1, sizeof(TupleObject));
265 if (tupleObjPtr == NULL) {
266 Tcl_AppendResult(interp, "can't allocate tuple object", (char *)NULL);
267 return NULL;
268 }
269 tupleObjPtr->interp = interp;
270 tupleObjPtr->clients = Blt_ChainCreate();
271 tupleObjPtr->traces = Blt_ChainCreate();
272 tupleObjPtr->notifiers = Blt_ChainCreate();
273 tupleObjPtr->notifyFlags = 0;
274 Blt_InitHashTableWithPool(&tupleObjPtr->busyTable, BLT_ONE_WORD_KEYS);
275 Blt_InitHashTable(&tupleObjPtr->columnTable, BLT_STRING_KEYS);
276
277 tupleObjPtr->tablePtr = &dataPtr->instTable;
278 hPtr = Blt_CreateHashEntry(tupleObjPtr->tablePtr, name, &isNew);
279 tupleObjPtr->hashPtr = hPtr;
280 tupleObjPtr->name = Blt_GetHashKey(tupleObjPtr->tablePtr, hPtr);
281 Blt_SetHashValue(hPtr, tupleObjPtr);
282
283 return tupleObjPtr;
284 }
285
286 /*
287 * ----------------------------------------------------------------------
288 *
289 * DestroyTupleObject --
290 *
291
292 * Destroys the tuple object. This is the final clean up of the
293 * object. The object's entry is removed from the hash table of
294 * tuples.
295 *
296 * Results:
297 * None.
298 *
299 * ----------------------------------------------------------------------
300 */
301 static void
302 DestroyTupleObject(TupleObject *tupleObjPtr)
303 {
304 Blt_ChainLink *linkPtr;
305 Trace *tracePtr;
306 struct Blt_TupleNotifierStruct *notifyPtr;
307
308 tupleObjPtr->flags |= TUPLE_DESTROYED;
309
310 for (linkPtr = Blt_ChainFirstLink(tupleObjPtr->traces); linkPtr != NULL;
311 linkPtr = Blt_ChainNextLink(linkPtr)) {
312 tracePtr = Blt_ChainGetValue(linkPtr);
313 tracePtr->linkPtr = NULL;
314 Blt_TupleDeleteTrace(tracePtr);
315 }
316 for (linkPtr = Blt_ChainFirstLink(tupleObjPtr->notifiers); linkPtr != NULL;
317 linkPtr = Blt_ChainNextLink(linkPtr)) {
318 notifyPtr = Blt_ChainGetValue(linkPtr);
319 notifyPtr->linkPtr = NULL;
320 Blt_TupleDeleteNotifier(notifyPtr);
321 }
322 Blt_ChainDestroy(tupleObjPtr->traces);
323 Blt_ChainDestroy(tupleObjPtr->notifiers);
324 Blt_ChainDestroy(tupleObjPtr->clients);
325
326 if (tupleObjPtr->rows != NULL) {
327 int i;
328
329 for (i = 0; i < tupleObjPtr->nRows; i++) {
330 DeleteRow(tupleObjPtr, tupleObjPtr->rows[i]);
331 }
332 Blt_Free(tupleObjPtr->rows);
333 }
334 tupleObjPtr->nRows = 0;
335
336 Blt_PoolDestroy(tupleObjPtr->rowPool);
337 Blt_DeleteHashTable(&tupleObjPtr->columnTable);
338 Blt_DeleteHashTable(&tupleObjPtr->busyTable);
339
340 if (tupleObjPtr->hashPtr != NULL) {
341 Blt_DeleteHashEntry(tupleObjPtr->tablePtr, tupleObjPtr->hashPtr);
342 }
343 Blt_Free(tupleObjPtr);
344 }
345
346 /*
347 * -----------------------------------------------------------------------
348 *
349 * TupleInterpDeleteProc --
350 *
351 * This is called when the interpreter hosting the tree object
352 * is deleted from the interpreter.
353 *
354 * Results:
355 * None.
356 *
357 * Side effects:
358 * Destroys all remaining trees and removes the hash table
359 * used to register tree names.
360 *
361 * ------------------------------------------------------------------------
362 */
363 /* ARGSUSED */
364 static void
365 TupleInterpDeleteProc(
366 ClientData clientData, /* Interpreter-specific data. */
367 Tcl_Interp *interp)
368 {
369 TupleInterpData *dataPtr = clientData;
370 Blt_HashEntry *hPtr;
371 Blt_HashSearch cursor;
372 TupleObject *tupleObjPtr;
373
374 for (hPtr = Blt_FirstHashEntry(&dataPtr->instTable, &cursor);
375 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
376 tupleObjPtr = (TupleObject *)Blt_GetHashValue(hPtr);
377 tupleObjPtr->hashPtr = NULL;
378 DestroyTupleObject(tupleObjPtr);
379 }
380 Blt_DeleteHashTable(&dataPtr->instTable);
381 Tcl_DeleteAssocData(interp, TUPLE_THREAD_KEY);
382 Blt_Free(dataPtr);
383 }
384
385 /*
386 * --------------------------------------------------------------
387 *
388 * GetTupleInterpData --
389 *
390 * Creates or retrieves data associated with tuple data objects
391 * for a particular thread. We're using Tcl_GetAssocData rather
392 * than the Tcl thread routines so BLT can work with pre-8.0
393 * Tcl versions that don't have thread support.
394 *
395 * Results:
396 * Returns a pointer to the tuple interpreter data.
397 *
398 * --------------------------------------------------------------
399 */
400 static TupleInterpData *
401 GetTupleInterpData(Tcl_Interp *interp)
402 {
403 Tcl_InterpDeleteProc *proc;
404 TupleInterpData *dataPtr;
405
406 dataPtr = (TupleInterpData *)
407 Tcl_GetAssocData(interp, TUPLE_THREAD_KEY, &proc);
408 if (dataPtr == NULL) {
409 dataPtr = Blt_Malloc(sizeof(TupleInterpData));
410 assert(dataPtr);
411 dataPtr->interp = interp;
412 Tcl_SetAssocData(interp, TUPLE_THREAD_KEY, TupleInterpDeleteProc,
413 dataPtr);
414 Blt_InitHashTable(&dataPtr->instTable, BLT_STRING_KEYS);
415 }
416 return dataPtr;
417 }
418
419 /*
420 *----------------------------------------------------------------------
421 *
422 * NewTagTable --
423 *
424 *----------------------------------------------------------------------
425 */
426 static TagTable *
427 NewTagTable(void)
428 {
429 TagTable *tablePtr;
430
431 tablePtr = Blt_Malloc(sizeof(TagTable));
432 Blt_InitHashTable(&tablePtr->tagTable, BLT_STRING_KEYS);
433 tablePtr->refCount = 1;
434 return tablePtr;
435 }
436
437 static void
438 ReleaseTagTable(TagTable *tablePtr)
439 {
440 tablePtr->refCount--;
441 if (tablePtr->refCount <= 0) {
442 Blt_HashEntry *hPtr;
443 Blt_HashSearch cursor;
444 Blt_TupleTagEntry *tPtr;
445
446 for(hPtr = Blt_FirstHashEntry(&tablePtr->tagTable, &cursor);
447 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
448 tPtr = Blt_GetHashValue(hPtr);
449 Blt_DeleteHashTable(&tPtr->rowTable);
450 Blt_Free(tPtr);
451 }
452 Blt_DeleteHashTable(&tablePtr->tagTable);
453 Blt_Free(tablePtr);
454 }
455 }
456
457
458 static int
459 ParseParentheses(
460 Tcl_Interp *interp,
461 CONST char *string,
462 char **leftPtr,
463 char **rightPtr)
464 {
465 register char *p;
466 char *left, *right;
467
468 left = right = NULL;
469 for (p = (char *)string; *p != '\0'; p++) {
470 if (*p == '(') {
471 left = p;
472 } else if (*p == ')') {
473 right = p;
474 }
475 }
476 if (left != right) {
477 if (((left != NULL) && (right == NULL)) ||
478 ((left == NULL) && (right != NULL)) ||
479 (left > right) || (right != (p - 1))) {
480 if (interp != NULL) {
481 Tcl_AppendResult(interp, "bad array specification \"", string,
482 "\"", (char *)NULL);
483 }
484 return TCL_ERROR;
485 }
486 }
487 *leftPtr = left;
488 *rightPtr = right;
489 return TCL_OK;
490 }
491
492 /*
493 *----------------------------------------------------------------------
494 *
495 * Blt_TupleGetColumnIndex--
496 *
497 * Returns the index of the column by the name given. Translates
498 * between column names and indices. Column indices are a direct
499 * lookup while names must be hashed.
500 *
501 * Results:
502 * A standard Tcl result. Returns the index of via *indexPtr.
503 * If no column can be found, then TCL_ERROR is returned and
504 * an error message is left in the interpreter result.
505 *
506 *----------------------------------------------------------------------
507 */
508 int
509 Blt_TupleGetColumnIndex(
510 Tcl_Interp *interp,
511 TupleObject *tupleObjPtr,
512 CONST char *key,
513 unsigned int *indexPtr)
514 {
515 char *left, *right;
516 Column *columnPtr;
517 Blt_HashEntry *hPtr;
518
519 if (ParseParentheses(interp, key, &left, &right) != TCL_OK) {
520 return TCL_ERROR;
521 }
522 if (left != NULL) {
523 *left = '\0';
524 hPtr = Blt_FindHashEntry(&tupleObjPtr->columnTable, key);
525 *left = '(';
526 } else {
527 hPtr = Blt_FindHashEntry(&tupleObjPtr->columnTable, key);
528 }
529 if (hPtr == NULL) {
530 if (interp != NULL) {
531 Tcl_AppendResult(interp, "can't find column \"", key, "\" in \"",
532 tupleObjPtr->name, "\"", (char *)NULL);
533 }
534 return TCL_ERROR;
535 }
536 columnPtr = Blt_GetHashValue(hPtr);
537 *indexPtr = columnPtr->index;
538 return TCL_OK;
539 }
540
541 /*
542 *----------------------------------------------------------------------
543 *
544 * CallTraces --
545 *
546 * Examines the traces set for each client of the tuple object
547 * and fires any matching trace.
548 *
549 * Traces can match on
550 * row if the trace was specified by index.
551 * tag if the trace was specified by tag.
552 * key if the trace was specified with a list of
553 * one or more keys.
554 * flag type of trace (read, write, unset, create)
555 *
556 * If no matching criteria is specified (no tag, key, or tuple
557 * address) then only the bit flag has to match.
558 *
559 * If the TUPLE_TRACE_FOREIGN_ONLY is set in the handler, it
560 * means to ignore actions that are initiated by that client of
561 * the object. Only actions by other clients are handled.
562 *
563 * Results:
564 * Always returns TCL_OK.
565 *
566 * Side Effects:
567 * Traces on the tuple table location may be fired.
568 *
569 *----------------------------------------------------------------------
570 */
571 static void
572 CallTraces(
573 Tcl_Interp *interp,
574 TupleClient *clientPtr, /* Client holding a reference to the
575 * tuple object. */
576 Row *rowPtr, /* Tuple being modified. */
577 Column *columnPtr,
578 unsigned int flags)
579 {
580 Blt_ChainLink *linkPtr;
581 Trace *tracePtr;
582 TupleObject *tupleObjPtr; /* Tuple that was changed. */
583
584 tupleObjPtr = clientPtr->tupleObjPtr;
585 /* For each client, examine its trace handlers. */
586 for(linkPtr = Blt_ChainFirstLink(tupleObjPtr->traces);
587 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
588 tracePtr = Blt_ChainGetValue(linkPtr);
589 if ((tracePtr->tuple != NULL) && (tracePtr->tuple != rowPtr)) {
590 continue; /* Row doesn't match. */
591 }
592 if ((tracePtr->mask & flags) == 0) {
593 continue; /* Missing the required trace flags. */
594 }
595 if ((tracePtr->table == clientPtr) &&
596 (tracePtr->mask & TUPLE_TRACE_FOREIGN_ONLY)) {
597 continue; /* This client initiated the action. */
598 }
599 if ((tracePtr->withTag != NULL) &&
600 (!Blt_TupleHasTag(tracePtr->table, rowPtr, tracePtr->withTag))){
601 continue; /* Tag doesn't match. */
602 }
603 if (tracePtr->keys != NULL) {
604 register CONST char **p;
605
606 for (p = tracePtr->keys; *p != NULL; p++) {
607 if (strcmp(*p, columnPtr->key) == 0) {
608 break;
609 }
610 }
611 if (*p == NULL) {
612 continue; /* No keys match. */
613 }
614 }
615 if ((*tracePtr->proc) (tracePtr->clientData, tupleObjPtr->interp,
616 rowPtr->index, columnPtr->index, flags) != TCL_OK) {
617 if (interp != NULL) {
618 Tcl_BackgroundError(interp);
619 }
620 }
621 }
622 }
623
624 /*
625 * --------------------------------------------------------------
626 *
627 * GetValue --
628 *
629 * Gets a scalar Tcl_Obj value from the table at the designated
630 * row and column. Create and read traces may be fired.
631 *
632 * Results:
633 * Always returns TCL_OK.
634 *
635 * --------------------------------------------------------------
636 */
637 static int
638 GetValue(
639 Tcl_Interp *interp, /* Interpreter to reports result to. */
640 TupleClient *clientPtr, /* Table. */
641 Row *rowPtr, /* Row. */
642 Column *columnPtr, /* Column. */
643 Tcl_Obj **objPtrPtr) /* (out) Value from table. */
644 {
645 Blt_HashEntry *hPtr;
646 BusyKey busy;
647 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
648 int isNew;
649
650 busy.rowPtr = rowPtr;
651 busy.columnPtr = columnPtr;
652 hPtr = Blt_CreateHashEntry(&tupleObjPtr->busyTable, &busy, &isNew);
653 if (isNew) {
654 CallTraces(interp, clientPtr, rowPtr, columnPtr, TUPLE_TRACE_READ);
655 }
656 Blt_DeleteHashEntry(&tupleObjPtr->busyTable, hPtr);
657
658 /* Access the data value after traces have been called. */
659 *objPtrPtr = VALUE(rowPtr, columnPtr);
660 return TCL_OK;
661 }
662
663 /*
664 * --------------------------------------------------------------
665 *
666 * SetValue --
667 *
668 * Sets a scalar Tcl_Obj value in the table at the designated row
669 * and column. Write traces may be fired.
670 *
671 * Results:
672 * Always returns TCL_OK.
673 *
674 * --------------------------------------------------------------
675 */
676 static int
677 SetValue(
678 Tcl_Interp *interp,
679 TupleClient *clientPtr,
680 Row *rowPtr,
681 Column *columnPtr,
682 Tcl_Obj *objPtr)
683 {
684 Tcl_Obj *oldObjPtr;
685 unsigned int flags;
686 Blt_HashEntry *hPtr;
687 int isNew;
688 BusyKey busy;
689 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
690
691 if ((rowPtr->nColumns < columnPtr->index) && (rowPtr->data != NULL)) {
692 oldObjPtr = rowPtr->data[columnPtr->index];
693 } else {
694 AllocateRows(tupleObjPtr, rowPtr);
695 objObjPtr = NULL;
696 }
697 if (objPtr != NULL) {
698 Tcl_IncrRefCount(objPtr);
699 }
700 if (oldObjPtr != NULL) {
701 Tcl_DecrRefCount(oldObjPtr);
702 }
703 rowPtr->data[columnPtr->index] = objPtr;
704 flags = TUPLE_TRACE_WRITE;
705 if (oldObjPtr == NULL) {
706 flags |= TUPLE_TRACE_CREATE;
707 }
708 busy.rowPtr = rowPtr;
709 busy.columnPtr = columnPtr;
710 hPtr = Blt_CreateHashEntry(&tupleObjPtr->busyTable, &busy, &isNew);
711 if (isNew) {
712 CallTraces(interp, clientPtr, rowPtr, columnPtr, flags);
713 }
714 Blt_DeleteHashEntry(&tupleObjPtr->busyTable, hPtr);
715 return TCL_OK;
716 }
717
718 /*
719 * --------------------------------------------------------------
720 *
721 * UnsetValue --
722 *
723 * Unsets a scalar Tcl_Obj value in the table at the designated
724 * row and column. Unset traces may be fired.
725 *
726 * Results:
727 * Always returns TCL_OK.
728 *
729 * --------------------------------------------------------------
730 */
731 static int
732 UnsetValue(
733 Tcl_Interp *interp,
734 TupleClient *clientPtr,
735 Row *rowPtr,
736 Column *columnPtr)
737 {
738 Tcl_Obj *objPtr;
739
740 objPtr = VALUE(rowPtr, columnPtr);
741 if (objPtr != NULL) {
742 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
743 Blt_HashEntry *hPtr;
744 BusyKey busy;
745 int isNew;
746
747 Tcl_DecrRefCount(objPtr);
748 rowPtr->data[columnPtr->index] = NULL;
749 busy.rowPtr = rowPtr;
750 busy.columnPtr = columnPtr;
751 hPtr = Blt_CreateHashEntry(&tupleObjPtr->busyTable, &busy, &isNew);
752 if (isNew) {
753 CallTraces(interp, clientPtr, rowPtr, columnPtr, TUPLE_TRACE_UNSET);
754 }
755 Blt_DeleteHashEntry(&tupleObjPtr->busyTable, hPtr);
756 }
757 return TCL_OK;
758 }
759
760 static int
761 GetArrayValue(
762 Tcl_Interp *interp,
763 TupleClient *clientPtr,
764 Row *rowPtr,
765 Column *columnPtr,
766 CONST char *elemName,
767 Tcl_Obj **objPtrPtr,
768 int doTrace)
769 {
770 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
771 Tcl_Obj *objPtr;
772 Blt_HashTable *tablePtr;
773 Blt_HashEntry *hPtr;
774
775 if (doTrace) {
776 BusyKey busy;
777 int isNew;
778
779 busy.rowPtr = rowPtr;
780 busy.columnPtr = columnPtr;
781 hPtr = Blt_CreateHashEntry(&tupleObjPtr->busyTable, &busy, &isNew);
782 if (isNew) {
783 CallTraces(interp, clientPtr, rowPtr, columnPtr, TUPLE_TRACE_READ);
784 }
785 Blt_DeleteHashEntry(&tupleObjPtr->busyTable, hPtr);
786 }
787
788 /* Access the data value after traces have been called. */
789 objPtr = VALUE(rowPtr, columnPtr);
790 if (objPtr != NULL) {
791 if (Tcl_IsShared(objPtr)) {
792 Tcl_DecrRefCount(objPtr);
793 objPtr = Tcl_DuplicateObj(objPtr);
794 Tcl_IncrRefCount(objPtr);
795 rowPtr->data[columnPtr->index] = objPtr;
796 }
797 if (Blt_GetArrayFromObj(interp, objPtr, &tablePtr) != TCL_OK) {
798 return TCL_ERROR;
799 }
800 hPtr = Blt_FindHashEntry(tablePtr, elemName);
801 if (hPtr == NULL) {
802 if (interp != NULL) {
803 Tcl_AppendResult(interp, "can't find \"", columnPtr->key, "(",
804 elemName, ")\"", (char *)NULL);
805 }
806 return TCL_ERROR;
807 }
808 objPtr = Blt_GetHashValue(hPtr);
809 }
810 *objPtrPtr = objPtr;
811 return TCL_OK;
812 }
813
814 static int
815 SetArrayValue(
816 Tcl_Interp *interp,
817 TupleClient *clientPtr,
818 Row *rowPtr,
819 Column *columnPtr,
820 CONST char *elemName,
821 Tcl_Obj *valueObjPtr)
822 {
823 Blt_HashTable *tablePtr;
824 Tcl_Obj *arrayObjPtr;
825 unsigned int flags;
826 Blt_HashEntry *hPtr;
827 int isNew;
828 BusyKey busy;
829 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
830
831 flags = TUPLE_TRACE_WRITE;
832 arrayObjPtr = VALUE(rowPtr, columnPtr);
833 if (arrayObjPtr == NULL) {
834 arrayObjPtr = Blt_NewArrayObj(0, (Tcl_Obj **)NULL);
835 Tcl_IncrRefCount(arrayObjPtr);
836 flags |= TUPLE_TRACE_CREATE;
837 AllocateRows(tupleObjPtr, rowPtr);
838 } else if (Tcl_IsShared(arrayObjPtr)) {
839 Tcl_DecrRefCount(arrayObjPtr);
840 arrayObjPtr = Tcl_DuplicateObj(arrayObjPtr);
841 Tcl_IncrRefCount(arrayObjPtr);
842 }
843 rowPtr->data[columnPtr->index] = arrayObjPtr;
844 if (Blt_GetArrayFromObj(interp, arrayObjPtr, &tablePtr) != TCL_OK) {
845 return TCL_ERROR;
846 }
847 Tcl_InvalidateStringRep(arrayObjPtr);
848 hPtr = Blt_CreateHashEntry(tablePtr, elemName, &isNew);
849 assert(hPtr);
850
851 Tcl_IncrRefCount(valueObjPtr);
852 if (!isNew) {
853 Tcl_Obj *oldValueObjPtr;
854
855 /* An element by the same name already exists. Decrement the
856 * reference count of the old value. */
857
858 oldValueObjPtr = Blt_GetHashValue(hPtr);
859 if (oldValueObjPtr != NULL) {
860 Tcl_DecrRefCount(oldValueObjPtr);
861 }
862 }
863 Blt_SetHashValue(hPtr, valueObjPtr);
864
865 /*
866 * We don't handle traces on a per array element basis. Setting
867 * any element can fire traces for the value.
868 */
869 busy.rowPtr = rowPtr;
870 busy.columnPtr = columnPtr;
871 hPtr = Blt_CreateHashEntry(&tupleObjPtr->busyTable, &busy, &isNew);
872 if (isNew) {
873 CallTraces(interp, clientPtr, rowPtr, columnPtr, flags);
874 }
875 Blt_DeleteHashEntry(&tupleObjPtr->busyTable, hPtr);
876 return TCL_OK;
877 }
878
879 static int
880 UnsetArrayValue(
881 Tcl_Interp *interp,
882 TupleClient *clientPtr,
883 Row *rowPtr,
884 Column *columnPtr,
885 CONST char *elemName)
886 {
887 Blt_HashEntry *hPtr;
888 Blt_HashTable *tablePtr;
889 BusyKey busy;
890 Tcl_Obj *arrayObjPtr, *valueObjPtr;
891 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
892 int isNew;
893
894 arrayObjPtr = VALUE(rowPtr, columnPtr);
895 if (arrayObjPtr == NULL) {
896 return TCL_OK;
897 }
898 if (Tcl_IsShared(arrayObjPtr)) {
899 Tcl_DecrRefCount(arrayObjPtr);
900 arrayObjPtr = Tcl_DuplicateObj(arrayObjPtr);
901 Tcl_IncrRefCount(arrayObjPtr);
902 }
903 if (Blt_GetArrayFromObj(interp, arrayObjPtr, &tablePtr) != TCL_OK) {
904 return TCL_ERROR;
905 }
906 hPtr = Blt_FindHashEntry(tablePtr, elemName);
907 if (hPtr == NULL) {
908 return TCL_OK; /* Element doesn't exist, Ok. */
909 }
910 valueObjPtr = Blt_GetHashValue(hPtr);
911 Tcl_DecrRefCount(valueObjPtr);
912 Blt_DeleteHashEntry(tablePtr, hPtr);
913 rowPtr->data[columnPtr->index] = NULL;
914
915 /*
916 * Un-setting any element in the array can cause the trace on the value
917 * to fire.
918 */
919 busy.rowPtr = rowPtr;
920 busy.columnPtr = columnPtr;
921 hPtr = Blt_CreateHashEntry(&tupleObjPtr->busyTable, &busy, &isNew);
922 if (isNew) {
923 CallTraces(interp, clientPtr, rowPtr, columnPtr, TUPLE_TRACE_UNSET);
924 }
925 Blt_DeleteHashEntry(&tupleObjPtr->busyTable, hPtr);
926 return TCL_OK;
927 }
928
929
930 static int
931 ExtendRows(TupleObject *tupleObjPtr, int extra)
932 {
933 int nRows;
934 int i;
935
936 nRows = tupleObjPtr->nRows + extra;
937 if (tupleObjPtr->rowsAllocated < nRows) {
938 Row **rows;
939
940 if (tupleObjPtr->rowsAllocated == 0) {
941 tupleObjPtr->rowsAllocated = 32;
942 }
943 while (tupleObjPtr->rowsAllocated < nRows) {
944 tupleObjPtr->rowsAllocated += tupleObjPtr->rowsAllocated;
945 }
946 rows = Blt_Realloc(tupleObjPtr->rows,
947 tupleObjPtr->rowsAllocated * sizeof(Row *));
948 if (rows == NULL) {
949 return -1;
950 }
951 tupleObjPtr->rows = rows;
952 }
953 for (i = tupleObjPtr->nRows; i < nRows; i++) {
954 tupleObjPtr->rows[i] = NewRow(tupleObjPtr, i);
955 }
956 tupleObjPtr->nRows = nRows;
957 return 0;
958 }
959
960 static int
961 ExtendColumns(TupleObject *tupleObjPtr, Row *rowPtr)
962 {
963 if (tupleObjPtr->columnsAllocated < rowPtr->nColumns) {
964 Tcl_Obj **data;
965 int i;
966
967 data = Blt_Realloc(rowPtr->data,
968 tupleObjPtr->columnsAllocated * sizeof(Tcl_Obj *));
969 if (data == NULL) {
970 return TCL_ERROR;
971 }
972 for(i = rowPtr->nColumns; i < tupleObjPtr->columnsAllocated; i++) {
973 data[i] = NULL;
974 }
975 rowPtr->data = data;
976 rowPtr->nColumns = tupleObjPtr->columnsAllocated;
977 }
978 return TCL_OK;
979 }
980
981
982 /*
983 * ----------------------------------------------------------------------
984 *
985 * FindTupleObjectInNamespace --
986 *
987 * Searches for the tuple object in a given namespace.
988 *
989 * Results:
990 * Returns a pointer to the tuple object if found, otherwise NULL.
991 *
992 * ----------------------------------------------------------------------
993 */
994 static TupleObject *
995 FindTupleObjectInNamespace(
996 TupleInterpData *dataPtr, /* Interpreter-specific data. */
997 Tcl_Namespace *nsPtr,
998 CONST char *objName)
999 {
1000 Tcl_DString dString;
1001 char *name;
1002 Blt_HashEntry *hPtr;
1003
1004 name = Blt_GetQualifiedName(nsPtr, objName, &dString);
1005 hPtr = Blt_FindHashEntry(&dataPtr->instTable, name);
1006 Tcl_DStringFree(&dString);
1007 if (hPtr != NULL) {
1008 return Blt_GetHashValue(hPtr);
1009 }
1010 return NULL;
1011 }
1012
1013 /*
1014 * ----------------------------------------------------------------------
1015 *
1016 * GetTupleObject --
1017 *
1018 * Searches for the tuple object associated by the name given.
1019 *
1020 * Results:
1021 * Returns a pointer to the tuple object if found, otherwise NULL.
1022 *
1023 * ----------------------------------------------------------------------
1024 */
1025 static TupleObject *
1026 GetTupleObject(Tcl_Interp *interp, CONST char *name, unsigned int flags)
1027 {
1028 CONST char *tupleName;
1029 Tcl_Namespace *nsPtr; /* Namespace associated with the tuple object.
1030 * If NULL, indicates to look in first the
1031 * current namespace and then the global
1032 * for the object. */
1033 TupleInterpData *dataPtr; /* Interpreter-specific data. */
1034 TupleObject *tupleObjPtr;
1035
1036 tupleObjPtr = NULL;
1037 if (Blt_ParseQualifiedName(interp, name, &nsPtr, &tupleName) != TCL_OK) {
1038 Tcl_AppendResult(interp, "can't find namespace in \"", name, "\"",
1039 (char *)NULL);
1040 return NULL;
1041 }
1042 dataPtr = GetTupleInterpData(interp);
1043 if (nsPtr != NULL) {
1044 tupleObjPtr = FindTupleObjectInNamespace(dataPtr, nsPtr, tupleName);
1045 } else {
1046 if (flags & NS_SEARCH_CURRENT) {
1047 /* Look first in the current namespace. */
1048 nsPtr = Tcl_GetCurrentNamespace(interp);
1049 tupleObjPtr = FindTupleObjectInNamespace(dataPtr, nsPtr, tupleName);
1050 }
1051 if ((tupleObjPtr == NULL) && (flags & NS_SEARCH_GLOBAL)) {
1052 nsPtr = Tcl_GetGlobalNamespace(interp);
1053 tupleObjPtr = FindTupleObjectInNamespace(dataPtr, nsPtr, tupleName);
1054 }
1055 }
1056 return tupleObjPtr;
1057 }
1058
1059 /*
1060 *----------------------------------------------------------------------
1061 *
1062 * NewTupleClient --
1063 *
1064 * Creates a new tuple client. Clients shared a tuple data
1065 * object. They individually manage traces and events on tuple
1066 * objects. Returns a pointer to the malloc'ed structure. This
1067 * is passed to the client as a tuple token.
1068 *
1069 * Results:
1070 * A pointer to a TupleClient is returned. If one can't
1071 * be allocated, NULL is returned.
1072 *
1073 *----------------------------------------------------------------------
1074 */
1075 static TupleClient *
1076 NewTupleClient(TupleObject *tupleObjPtr)
1077 {
1078 TupleClient *clientPtr;
1079
1080 clientPtr = Blt_Calloc(1, sizeof(TupleClient));
1081 if (clientPtr != NULL) {
1082 clientPtr->magic = TUPLE_MAGIC;
1083 clientPtr->linkPtr = Blt_ChainAppend(tupleObjPtr->clients, clientPtr);
1084 clientPtr->tupleObjPtr = tupleObjPtr;
1085 clientPtr->tagTablePtr = NewTagTable();
1086 }
1087 return clientPtr;
1088 }
1089
1090
1091 /*
1092 *----------------------------------------------------------------------
1093 *
1094 * NotifyIdleProc --
1095 *
1096 * Used to invoke event handler routines at some idle point.
1097 * This routine is called from the Tcl event loop. Errors
1098 * generated by the event handler routines are backgrounded.
1099 *
1100 * Results:
1101 * None.
1102 *
1103 *----------------------------------------------------------------------
1104 */
1105 static void
1106 NotifyIdleProc(ClientData clientData)
1107 {
1108 Notifier *notifyPtr = clientData;
1109 int result;
1110
1111 notifyPtr->notifyPending = FALSE;
1112 notifyPtr->mask |= TUPLE_NOTIFY_ACTIVE;
1113 result = (*notifyPtr->proc)(notifyPtr->clientData, &notifyPtr->event);
1114 notifyPtr->mask &= ~TUPLE_NOTIFY_ACTIVE;
1115 if (result != TCL_OK) {
1116 Tcl_BackgroundError(notifyPtr->interp);
1117 }
1118 }
1119
1120 /*
1121 *----------------------------------------------------------------------
1122 *
1123 * NotifyClients --
1124 *
1125 * Traverses the list of event callbacks and checks if one
1126 * matches the given event. A client may trigger an action that
1127 * causes the tuple object to notify it. This can be prevented
1128 * by setting the TUPLE_NOTIFY_FOREIGN_ONLY bit in the event
1129 * handler.
1130 *
1131 * If a matching handler is found, a callback may be called either
1132 * immediately or at the next idle time depending upon the
1133 * TUPLE_NOTIFY_WHENIDLE bit.
1134 *
1135 * Since a handler routine may trigger yet another call to
1136 * itself, callbacks are ignored while the event handler is
1137 * executing.
1138 *
1139 *----------------------------------------------------------------------
1140 */
1141 static void
1142 NotifyClients(
1143 TupleClient *sourcePtr,
1144 TupleObject *tupleObjPtr,
1145 unsigned int eventFlag)
1146 {
1147 Blt_ChainLink *linkPtr;
1148 Blt_TupleNotifyEvent event;
1149 Notifier *notifyPtr;
1150
1151 event.type = eventFlag;
1152
1153 /* Issue callbacks indicating that the structure of the tuple
1154 * table has changed. */
1155 for (linkPtr = Blt_ChainFirstLink(tupleObjPtr->notifiers);
1156 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
1157 notifyPtr = Blt_ChainGetValue(linkPtr);
1158 if ((notifyPtr->mask & TUPLE_NOTIFY_ACTIVE) ||
1159 (notifyPtr->mask & eventFlag) == 0) {
1160 continue; /* Ignore callbacks that are generated
1161 * inside of a notify handler routine. */
1162 }
1163 if ((notifyPtr->table == sourcePtr) &&
1164 (notifyPtr->mask & TUPLE_NOTIFY_FOREIGN_ONLY)) {
1165 continue; /* Don't notify yourself. */
1166 }
1167 event.table = notifyPtr->table;
1168 if (notifyPtr->mask & TUPLE_NOTIFY_WHENIDLE) {
1169 if (!notifyPtr->notifyPending) {
1170 notifyPtr->notifyPending = TRUE;
1171 notifyPtr->event = event;
1172 Tcl_DoWhenIdle(NotifyIdleProc, notifyPtr);
1173 }
1174 } else {
1175 int result;
1176
1177 notifyPtr->mask |= TUPLE_NOTIFY_ACTIVE;
1178 result = (*notifyPtr->proc) (notifyPtr->clientData, &event);
1179 notifyPtr->mask &= ~TUPLE_NOTIFY_ACTIVE;
1180 if (result != TCL_OK) {
1181 Tcl_BackgroundError(notifyPtr->interp);
1182 }
1183 }
1184 }
1185 }
1186
1187 /*
1188 *----------------------------------------------------------------------
1189 *
1190 * Blt_TupleCreateObject --
1191 *
1192 * Creates a tuple object by the designated name. It's an error
1193 * if a tuple object already exists by that name. After
1194 * successfully creating the object, the caller must then call
1195 * Blt_TupleGetToken to allocate a token to share and manipulate
1196 * the object.
1197 *
1198 * Results:
1199 * A standard Tcl result. If successful, a new tuple object is
1200 * created and TCL_OK is returned. If an object already exists
1201 * or the tuple object can't be allocated, then TCL_ERROR is
1202 * returned and an error message is left in the interpreter.
1203 *
1204 * Side Effects:
1205 * A new tuple object is created.
1206 *
1207 *----------------------------------------------------------------------
1208 */
1209 int
1210 Blt_TupleCreateTable(
1211 Tcl_Interp *interp, /* Interpreter to report errors back to. */
1212 CONST char *name, /* Name of tuple in namespace. Object
1213 * must not already exist. */
1214 TupleClient **clientPtrPtr) /* (out) Client token of newly created
1215 * tuple object. Releasing the token
1216 * will free the tuple. If NULL, no
1217 * token is generated. */
1218 {
1219 Tcl_DString dString;
1220 Tcl_Namespace *nsPtr;
1221 TupleInterpData *dataPtr;
1222 TupleObject *tupleObjPtr;
1223 CONST char *tupleName;
1224 char string[200];
1225
1226 dataPtr = GetTupleInterpData(interp);
1227 if (name != NULL) {
1228 /* Does an object by this name already exist in the current
1229 * namespace? */
1230 tupleObjPtr = GetTupleObject(interp, name, NS_SEARCH_CURRENT);
1231 if (tupleObjPtr != NULL) {
1232 Tcl_AppendResult(interp, "a tuple object \"", name,
1233 "\" already exists", (char *)NULL);
1234 return TCL_ERROR;
1235 }
1236 } else {
1237 /* Generate a unique name in the current namespace. */
1238 do {
1239 sprintf(string, "tuple%d", dataPtr->nextId++);
1240 } while (GetTupleObject(interp, name, NS_SEARCH_CURRENT) != NULL);
1241 name = string;
1242 }
1243 /*
1244 * Tear apart and put back together the namespace-qualified name
1245 * of the object. This is to ensure that naming is consistent.
1246 */
1247 tupleName = name;
1248 if (Blt_ParseQualifiedName(interp, name, &nsPtr, &tupleName) != TCL_OK) {
1249 Tcl_AppendResult(interp, "can't find namespace in \"", name, "\"",
1250 (char *)NULL);
1251 return TCL_ERROR;
1252 }
1253 if (nsPtr == NULL) {
1254 /*
1255 * Note that unlike Tcl_CreateCommand, an unqualified name
1256 * doesn't imply the global namespace, but the current one.
1257 */
1258 nsPtr = Tcl_GetCurrentNamespace(interp);
1259 }
1260 name = Blt_GetQualifiedName(nsPtr, tupleName, &dString);
1261 tupleObjPtr = NewTupleObject(dataPtr, interp, name);
1262 if (tupleObjPtr == NULL) {
1263 Tcl_AppendResult(interp, "can't allocate tuple object \"", name, "\"",
1264 (char *)NULL);
1265 Tcl_DStringFree(&dString);
1266 return TCL_ERROR;
1267 }
1268 Tcl_DStringFree(&dString);
1269 if (clientPtrPtr != NULL) {
1270 TupleClient *clientPtr;
1271
1272 clientPtr = NewTupleClient(tupleObjPtr);
1273 if (clientPtr == NULL) {
1274 Tcl_AppendResult(interp, "can't allocate tuple token",
1275 (char *)NULL);
1276 return TCL_ERROR;
1277 }
1278 *clientPtrPtr = clientPtr;
1279 }
1280 return TCL_OK;
1281 }
1282
1283 /*
1284 *----------------------------------------------------------------------
1285 *
1286 * Blt_TupleGetToken --
1287 *
1288 * Allocates a token for the tuple object designated by name.
1289 * It's an error if no tuple object exists by that name. The
1290 * token returned is passed to various routines to manipulate the
1291 * object. Traces and event notifications are also made through
1292 * the token.
1293 *
1294 * Results:
1295 * A new token is returned representing the tuple object.
1296 *
1297 * Side Effects:
1298 * If this is the remaining client, then the tuple object itself
1299 * is freed.
1300 *
1301 *----------------------------------------------------------------------
1302 */
1303 int
1304 Blt_TupleGetTable(
1305 Tcl_Interp *interp, /* Interpreter to report errors back to. */
1306 CONST char *name, /* Name of tuple object in namespace. */
1307 TupleClient **clientPtrPtr)
1308 {
1309 TupleClient *clientPtr;
1310 TupleObject *tupleObjPtr;
1311
1312 tupleObjPtr = GetTupleObject(interp, name, NS_SEARCH_BOTH);
1313 if (tupleObjPtr == NULL) {
1314 Tcl_AppendResult(interp, "can't find a tuple object \"", name, "\"",
1315 (char *)NULL);
1316 return TCL_ERROR;
1317 }
1318 clientPtr = NewTupleClient(tupleObjPtr);
1319 if (clientPtr == NULL) {
1320 Tcl_AppendResult(interp, "can't allocate token for tuple \"", name,
1321 "\"", (char *)NULL);
1322 return TCL_ERROR;
1323 }
1324 *clientPtrPtr = clientPtr;
1325 return TCL_OK;
1326 }
1327
1328 /*
1329 *----------------------------------------------------------------------
1330 *
1331 * Blt_TupleReleaseToken --
1332 *
1333 * Releases the tuple token, indicating this the client is no
1334 * longer using the object. The client is removed from the tuple
1335 * object's client list. If this is the last client, then the
1336 * object itself is destroyed and memory is freed.
1337 *
1338 * Results:
1339 * None.
1340 *
1341 * Side Effects:
1342 * If this is the remaining client, then the tuple object itself
1343 * is freed.
1344 *
1345 *----------------------------------------------------------------------
1346 */
1347 void
1348 Blt_TupleReleaseTable(TupleClient *clientPtr)
1349 {
1350 TupleObject *tupleObjPtr;
1351 Blt_ChainLink *linkPtr;
1352 Notifier *notifyPtr;
1353 Trace *tracePtr;
1354
1355 if (clientPtr->magic != TUPLE_MAGIC) {
1356 fprintf(stderr, "invalid tuple object token 0x%lx\n",
1357 (unsigned long)clientPtr);
1358 return;
1359 }
1360 /* Remove any traces that were set by this client. */
1361 for (linkPtr = Blt_ChainFirstLink(tupleObjPtr->traces); linkPtr != NULL;
1362 linkPtr = Blt_ChainNextLink(linkPtr)) {
1363 tracePtr = Blt_ChainGetValue(linkPtr);
1364 if (tracePtr->table == clientPtr) {
1365 Blt_TupleDeleteTrace(tracePtr);
1366 }
1367 }
1368 /* Also remove all event handlers created by this client. */
1369 for(linkPtr = Blt_ChainFirstLink(tupleObjPtr->notifiers);
1370 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
1371 notifyPtr = Blt_ChainGetValue(linkPtr);
1372 if (notifyPtr->table == clientPtr) {
1373 Blt_TupleDeleteNotifier(notifyPtr);
1374 }
1375 }
1376
1377 if (clientPtr->tagTablePtr != NULL) {
1378 ReleaseTagTable(clientPtr->tagTablePtr);
1379 }
1380 tupleObjPtr = clientPtr->tupleObjPtr;
1381 if (tupleObjPtr != NULL) {
1382 /* Remove the client from the server's list */
1383 Blt_ChainDeleteLink(tupleObjPtr->clients, clientPtr->linkPtr);
1384 if (Blt_ChainGetLength(tupleObjPtr->clients) == 0) {
1385 DestroyTupleObject(tupleObjPtr);
1386 }
1387 }
1388 clientPtr->magic = 0;
1389 Blt_Free(clientPtr);
1390 }
1391
1392 /*
1393 *----------------------------------------------------------------------
1394 *
1395 * Blt_TupleTableExists --
1396 *
1397 * Indicates if a tuple object by the given name exists in either
1398 * the current or global namespace.
1399 *
1400 * Results:
1401 * Returns 1 if a tuple object exists and 0 otherwise.
1402 *
1403 *----------------------------------------------------------------------
1404 */
1405 int
1406 Blt_TupleTableExists(
1407 Tcl_Interp *interp, /* Interpreter to report errors back to. */
1408 CONST char *name) /* Name of tuple object in the
1409 * designated namespace. */
1410 {
1411 TupleObject *tupleObjPtr;
1412
1413 tupleObjPtr = GetTupleObject(interp, name, NS_SEARCH_BOTH);
1414 if (tupleObjPtr == NULL) {
1415 Tcl_ResetResult(interp);
1416 return 0;
1417 }
1418 return 1;
1419 }
1420
1421 /*
1422 *----------------------------------------------------------------------
1423 *
1424 * Blt_TupleCreateNotifier --
1425 *
1426 * Creates an notifier using the following three pieces of
1427 * information:
1428 * 1. C function pointer,
1429 * 2. one-word of data passed on each call, and
1430 * 3. event mask indicating which events are of interest.
1431 * If an event already exists matching all of the above criteria,
1432 * it is repositioned on the end of the event handler list. This
1433 * means that it will be the last to fire.
1434 *
1435 * Results:
1436 * Returns a pointer to the event handler.
1437 *
1438 * Side Effects:
1439 * Memory for the event handler is possibly allocated.
1440 *
1441 *----------------------------------------------------------------------
1442 */
1443 Blt_TupleNotifier
1444 Blt_TupleCreateNotifier(
1445 TupleClient *clientPtr,
1446 unsigned int mask,
1447 Blt_TupleNotifyEventProc *proc,
1448 ClientData clientData)
1449 {
1450 Notifier *notifyPtr;
1451
1452 notifyPtr = Blt_Malloc(sizeof (Notifier));
1453 assert(notifyPtr);
1454 notifyPtr->proc = proc;
1455 notifyPtr->clientData = clientData;
1456 notifyPtr->mask = mask;
1457 notifyPtr->notifyPending = FALSE;
1458 notifyPtr->interp = clientPtr->tupleObjPtr->interp;
1459 notifyPtr->linkPtr = Blt_ChainAppend(clientPtr->tupleObjPtr->notifiers,
1460 notifyPtr);
1461 return notifyPtr;
1462 }
1463
1464 /*
1465 *----------------------------------------------------------------------
1466 *
1467 * Blt_TupleDeleteNotifier --
1468 *
1469 * Removes the event handler designated by following three pieces
1470 * of information: 1. C function pointer, 2. one-word of data
1471 * passed on each call, and 3. event mask indicating which events
1472 * are of interest.
1473 *
1474 * Results:
1475 * Nothing.
1476 *
1477 * Side Effects:
1478 * Memory for the event handler is freed.
1479 *
1480 *----------------------------------------------------------------------
1481 */
1482 void
1483 Blt_TupleDeleteNotifier(Notifier *notifyPtr)
1484 {
1485 if (notifyPtr->notifyPending) {
1486 Tcl_CancelIdleCall(NotifyIdleProc, notifyPtr);
1487 }
1488 if (notifyPtr->linkPtr != NULL){
1489 Blt_ChainDeleteLink(notifyPtr->table->tupleObjPtr->notifiers,
1490 notifyPtr->linkPtr);
1491 Blt_Free(notifyPtr);
1492 }
1493 }
1494
1495 /*
1496 *----------------------------------------------------------------------
1497 *
1498 * Blt_TupleRowIndex --
1499 *
1500 * Returns the index of the tuple.
1501 *
1502 * Results:
1503 * Returns the row index of the tuple.
1504 *
1505 *----------------------------------------------------------------------
1506 */
1507 unsigned int
1508 Blt_TupleRowIndex(Row *rowPtr)
1509 {
1510 return rowPtr->index;
1511 }
1512
1513 /*
1514 *----------------------------------------------------------------------
1515 *
1516 * Blt_TupleGetTupleByIndex --
1517 *
1518 * Returns the tuple at the row designated int the tuple table.
1519 *
1520 * Results:
1521 * Returns the tuple at the row. If the index is out of range
1522 * then NULL is returned.
1523 *
1524 *----------------------------------------------------------------------
1525 */
1526 Blt_Tuple
1527 Blt_TupleGetTupleByIndex(TupleClient *clientPtr, unsigned int row)
1528 {
1529 if (row >= clientPtr->tupleObjPtr->nRows) {
1530 return NULL;
1531 }
1532 return clientPtr->tupleObjPtr->rows[row];
1533 }
1534
1535 Blt_Tuple
1536 Blt_TupleNextTuple(TupleClient *clientPtr, Row *rowPtr)
1537 {
1538 return Blt_TupleGetTupleByIndex(clientPtr, rowPtr->index + 1);
1539 }
1540
1541 /*
1542 *----------------------------------------------------------------------
1543 *
1544 * Blt_TupleGetColumnKey --
1545 *
1546 * Returns the key of the column with the given index. This is a
1547 * helper routine to translate between column indices and keys.
1548 *
1549 * Results:
1550 * Returns the key of the column with the given index. If the
1551 * index is invalid, NULL is returned.
1552 *
1553 *----------------------------------------------------------------------
1554 */
1555 CONST char *
1556 Blt_TupleGetColumnKey(TupleClient *clientPtr, int columnIndex)
1557 {
1558 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
1559 Column *columnPtr;
1560
1561 if ((columnIndex < 0) || (columnIndex >= tupleObjPtr->nColumns)) {
1562 return NULL;
1563 }
1564 columnPtr = tupleObjPtr->columns[columnIndex];
1565 return columnPtr->key;
1566 }
1567
1568
1569 /*
1570 *----------------------------------------------------------------------
1571 *
1572 * Blt_TupleAddColumn --
1573 *
1574 * Add a new column to the tuple object. Rows are slots in an
1575 * array of tuples. The array grows by doubling its size, so
1576 * there may be more slots than needed (# rows).
1577 *
1578 * Results:
1579 * Returns TCL_OK is the table is resized and TCL_ERROR if an
1580 * not enough memory was available.
1581 *
1582 * Side Effects:
1583 * If more rows are needed, the array which holds the tuples is
1584 * reallocated by doubling its size. Storage for the tuples
1585 * isn't allocated until the tuple is needed.
1586 *
1587 *----------------------------------------------------------------------
1588 */
1589 unsigned int
1590 Blt_TupleAddColumn(
1591 TupleClient *clientPtr,
1592 CONST char *key,
1593 int *isNewPtr)
1594 {
1595 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
1596 Blt_HashEntry *hPtr;
1597 Column *columnPtr;
1598
1599 hPtr = Blt_CreateHashEntry(&tupleObjPtr->columnTable, key, isNewPtr);
1600 if (!*isNewPtr) {
1601 columnPtr = Blt_GetHashValue(hPtr);
1602 } else {
1603 columnPtr = Blt_Calloc(1, sizeof(Column));
1604 columnPtr->key = Blt_GetHashKey(&tupleObjPtr->columnTable, hPtr);
1605 columnPtr->type = 0;
1606 columnPtr->hashPtr = hPtr;
1607 columnPtr->index = tupleObjPtr->nColumns;
1608 tupleObjPtr->columns[columnPtr->index] = columnPtr;
1609 tupleObjPtr->nColumns++;
1610 NotifyClients(clientPtr, tupleObjPtr, TUPLE_NOTIFY_CREATE_COLUMN);
1611 }
1612 return columnPtr->index;
1613 }
1614
1615 /*
1616 *----------------------------------------------------------------------
1617 *
1618 * Blt_TupleAddColumns --
1619 *
1620 * Adds one or more columns to the tuple table. This creates a
1621 * new column. The tuples are individually resized when new data
1622 * is added to them.
1623 *
1624 * Results:
1625 * Returns TCL_OK is the tuple is resized and TCL_ERROR if an
1626 * not enough memory was available.
1627 *
1628 * Side Effects:
1629 * If more rows are needed, the array which holds the tuples is
1630 * reallocated by doubling its size. Storage for the tuples
1631 * isn't allocated until the tuple is needed.
1632 *
1633 *----------------------------------------------------------------------
1634 */
1635 int
1636 Blt_TupleAddColumns(
1637 Tcl_Interp *interp,
1638 TupleClient *clientPtr,
1639 char **keys)
1640 {
1641 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
1642 int i;
1643 int nColumns, extra;
1644 char **p;
1645 Blt_HashEntry *hPtr;
1646 int isNew;
1647
1648 extra = 0;
1649 for (p = keys; *p != NULL; p++) {
1650 hPtr = Blt_FindHashEntry(&tupleObjPtr->columnTable, *p);
1651 if (hPtr != NULL) {
1652 Tcl_AppendResult(interp, "a column \"", *p,
1653 "\" already exists in \"", tupleObjPtr->name, "\"",
1654 (char *)NULL);
1655 return TCL_ERROR;
1656 }
1657 extra++; /* Count the number of new columns. */
1658 }
1659 nColumns = tupleObjPtr->nColumns + extra;
1660 if (nColumns >= tupleObjPtr->columnsAllocated) {
1661 Column **columns;
1662 int columnsAllocated;
1663
1664 columnsAllocated = tupleObjPtr->columnsAllocated;
1665 while (nColumns >= columnsAllocated) {
1666 columnsAllocated += columnsAllocated;
1667 }
1668 columns = Blt_Realloc(tupleObjPtr->columns,
1669 sizeof(Column *) * columnsAllocated);
1670 if (columns == NULL) {
1671 return TCL_ERROR;
1672 }
1673 tupleObjPtr->columns = columns;
1674 tupleObjPtr->columnsAllocated = columnsAllocated;
1675 }
1676 for (p = keys; *p != NULL; p++) {
1677 Column *columnPtr;
1678
1679 hPtr = Blt_CreateHashEntry(&tupleObjPtr->columnTable, *p, &isNew);
1680 columnPtr = Blt_Calloc(1, sizeof(Column));
1681 columnPtr->key = Blt_GetHashKey(&tupleObjPtr->columnTable, hPtr);
1682 columnPtr->index = 0;
1683 columnPtr->type = 0;
1684 columnPtr->hashPtr = hPtr;
1685 columnPtr->index = tupleObjPtr->nColumns;
1686 tupleObjPtr->columns[columnPtr->index] = columnPtr;
1687 tupleObjPtr->nColumns++;
1688 }
1689 for(i = tupleObjPtr->nColumns; i < tupleObjPtr->columnsAllocated; i++) {
1690 tupleObjPtr->columns[i] = NULL;
1691 }
1692 NotifyClients(clientPtr, tupleObjPtr, TUPLE_NOTIFY_CREATE_COLUMN);
1693 return TCL_OK;
1694 }
1695
1696
1697 int
1698 Blt_TupleDeleteColumnByIndex(
1699 Tcl_Interp *interp,
1700 TupleClient *clientPtr,
1701 unsigned int column)
1702 {
1703 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
1704 Column *columnPtr;
1705
1706 columnPtr = tupleObjPtr->columns[column];
1707 if (columnPtr != NULL) {
1708 Row *rowPtr;
1709 int i;
1710 int j, k;
1711
1712 if (columnPtr->hashPtr != NULL) {
1713 Blt_DeleteHashEntry(&tupleObjPtr->columnTable, columnPtr->hashPtr);
1714 }
1715 for (i = 0; i < tupleObjPtr->nRows; i++) {
1716 rowPtr = tupleObjPtr->rows[i];
1717 if ((column < rowPtr->nColumns) && (rowPtr->data != NULL)) {
1718 Tcl_Obj *objPtr;
1719
1720 /* Release the Tcl_Obj associated with this column and
1721 * compress the tuple. */
1722 objPtr = rowPtr->data[column];
1723 if (objPtr != NULL) {
1724 Tcl_DecrRefCount(objPtr);
1725 rowPtr->data[column] = NULL;
1726 }
1727 for (j = column, k = column + 1; j < rowPtr->nColumns;
1728 j++, k++) {
1729 rowPtr->data[k] = rowPtr->data[j];
1730 }
1731 rowPtr->nColumns--;
1732 }
1733 }
1734 for (j = column, k = column + 1; j < tupleObjPtr->nColumns; j++, k++) {
1735 tupleObjPtr->columns[k] = tupleObjPtr->columns[j];
1736 }
1737 tupleObjPtr->columns[k] = NULL;
1738 tupleObjPtr->nColumns--;
1739 Blt_Free(columnPtr);
1740 NotifyClients(clientPtr, tupleObjPtr, TUPLE_NOTIFY_DELETE_COLUMN);
1741 }
1742 return TCL_OK;
1743 }
1744
1745 int
1746 Blt_TupleDeleteColumn(
1747 Tcl_Interp *interp,
1748 TupleClient *clientPtr,
1749 CONST char *key)
1750 {
1751 unsigned int column;
1752
1753 if (Blt_TupleGetColumnIndex(interp, clientPtr->tupleObjPtr, key, &column)
1754 != TCL_OK) {
1755 return TCL_ERROR;
1756 }
1757 return Blt_TupleDeleteColumnByIndex(interp, clientPtr, column);
1758 }
1759
1760
1761 /*
1762 *----------------------------------------------------------------------
1763 *
1764 * Blt_TupleValueExists --
1765 *
1766 * Indicates if a value exists for a given row,column index in
1767 * the tuple. Note that this routine does not fire read traces.
1768 *
1769 * Results:
1770 * Returns 1 is a value exists, 0 otherwise.
1771 *
1772 *----------------------------------------------------------------------
1773 */
1774 int
1775 Blt_TupleValueExists(
1776 TupleClient *clientPtr,
1777 Row *rowPtr,
1778 CONST char *key)
1779 {
1780 Blt_HashEntry *hPtr;
1781 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
1782 char *left, *right;
1783 int result;
1784
1785 if (rowPtr->index < tupleObjPtr->nRows) {
1786 return FALSE;
1787 }
1788 if (ParseParentheses((Tcl_Interp *)NULL, key, &left, &right) != TCL_OK) {
1789 return TCL_ERROR;
1790 }
1791 result = FALSE;
1792 if (left != NULL) {
1793 *left = *right = '\0';
1794 hPtr = Blt_FindHashEntry(&tupleObjPtr->columnTable, key);
1795 if (hPtr != NULL) {
1796 Tcl_Obj *objPtr;
1797 Column *columnPtr;
1798
1799 columnPtr = Blt_GetHashValue(hPtr);
1800 if (GetArrayValue((Tcl_Interp *)NULL, clientPtr, rowPtr, columnPtr,
1801 left + 1, &objPtr, TRACE_IGNORE) == TCL_OK) {
1802 result = (objPtr != NULL);
1803 }
1804 }
1805 *left = '(', *right = ')';
1806 } else {
1807 hPtr = Blt_FindHashEntry(&tupleObjPtr->columnTable, key);
1808 if (hPtr != NULL) {
1809 Tcl_Obj *objPtr;
1810 Column *columnPtr;
1811
1812 columnPtr = Blt_GetHashValue(hPtr);
1813 objPtr = VALUE(rowPtr, columnPtr);
1814 result = (objPtr != NULL);
1815 }
1816 }
1817 return result;
1818 }
1819
1820 /*
1821 *----------------------------------------------------------------------
1822 *
1823 * Blt_TupleGetValue --
1824 *
1825 * Returns the value at the given row, column index, if one
1826 * exists. If no traces are currently active on the the slot,
1827 * then a call to check if any trace should fire is called.
1828 *
1829 * Results:
1830 * A standard Tcl result. If the column key isn't valid TCL_ERROR
1831 * is returned and an error message is left as the interpreter
1832 * result.
1833 *
1834 * Side Effects:
1835 * Traces on the table location may be fired.
1836 *
1837 *----------------------------------------------------------------------
1838 */
1839 int
1840 Blt_TupleGetValue(
1841 Tcl_Interp *interp,
1842 TupleClient *clientPtr,
1843 Row *rowPtr,
1844 CONST char *key,
1845 Tcl_Obj **objPtrPtr)
1846 {
1847 Blt_HashEntry *hPtr;
1848 Column *columnPtr;
1849 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
1850 char *left, *right;
1851 int result;
1852
1853 if (ParseParentheses(interp, key, &left, &right) != TCL_OK) {
1854 return TCL_ERROR;
1855 }
1856 if (left != NULL) {
1857 *left = *right = '\0';
1858 hPtr = Blt_FindHashEntry(&tupleObjPtr->columnTable, key);
1859 if (hPtr != NULL) {
1860 columnPtr = Blt_GetHashValue(hPtr);
1861 result = GetArrayValue(interp, clientPtr, rowPtr, columnPtr,
1862 left + 1, objPtrPtr, TRACE_OK);
1863 } else if (interp != NULL) {
1864 Tcl_AppendResult(interp, "can't find column \"", key,
1865 "\" in \"", tupleObjPtr->name, "\"", (char *)NULL);
1866 }
1867 *left = '(', *right = ')';
1868 if (hPtr == NULL) {
1869 return TCL_ERROR;
1870 }
1871 } else {
1872 hPtr = Blt_FindHashEntry(&tupleObjPtr->columnTable, key);
1873 if (hPtr == NULL) {
1874 if (interp != NULL) {
1875 Tcl_AppendResult(interp, "can't find column \"", key,
1876 "\" in \"", tupleObjPtr->name, "\"", (char *)NULL);
1877 }
1878 return TCL_ERROR;
1879 }
1880 columnPtr = Blt_GetHashValue(hPtr);
1881 result = GetValue(interp, clientPtr, rowPtr, columnPtr, objPtrPtr);
1882 }
1883 return result;
1884 }
1885
1886 /*
1887 *----------------------------------------------------------------------
1888 *
1889 * Blt_TupleSetValue --
1890 *
1891 * Sets the value at the given row, column index. If the slot
1892 * is currently filled, the old Tcl_Obj is decremented. The
1893 * new Tcl_Obj is incremented. If no traces are currently active
1894 * on the the slot, then a call to check if any trace should
1895 * fire is called. There are two type of traces that may fire:
1896 *
1897 * TUPLE_TRACE_WRITE indicates that the slot was
1898 * written to.
1899 * TUPLE_TRACE_CREATE indicates there was no former
1900 * value set.
1901 *
1902 * Results:
1903 * Always returns TCL_OK.
1904 *
1905 * Side Effects:
1906 * Traces on the table location may be fired.
1907 *
1908 *----------------------------------------------------------------------
1909 */
1910 int
1911 Blt_TupleSetValue(
1912 Tcl_Interp *interp,
1913 TupleClient *clientPtr,
1914 Row *rowPtr,
1915 CONST char *key,
1916 Tcl_Obj *objPtr)
1917 {
1918 Blt_HashEntry *hPtr;
1919 Column *columnPtr;
1920 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
1921 char *left, *right;
1922 int result;
1923
1924 if (ParseParentheses(interp, key, &left, &right) != TCL_OK) {
1925 return TCL_ERROR;
1926 }
1927 if (left != NULL) {
1928 *left = *right = '\0';
1929 hPtr = Blt_FindHashEntry(&tupleObjPtr->columnTable, key);
1930 if (hPtr != NULL) {
1931 columnPtr = Blt_GetHashValue(hPtr);
1932 result = SetArrayValue(interp, clientPtr, rowPtr, columnPtr,
1933 left + 1, objPtr);
1934 } else if (interp != NULL) {
1935 Tcl_AppendResult(interp, "can't find column \"", key,
1936 "\" in \"", tupleObjPtr->name, "\"", (char *)NULL);
1937 }
1938 *left = '(', *right = ')';
1939 if (hPtr == NULL) {
1940 return TCL_ERROR;
1941 }
1942 } else {
1943 hPtr = Blt_FindHashEntry(&tupleObjPtr->columnTable, key);
1944 if (hPtr == NULL) {
1945 if (interp != NULL) {
1946 Tcl_AppendResult(interp, "can't find column \"", key,
1947 "\" in \"", tupleObjPtr->name, "\"", (char *)NULL);
1948 }
1949 return TCL_ERROR;
1950 }
1951 columnPtr = Blt_GetHashValue(hPtr);
1952 result = SetValue(interp, clientPtr, rowPtr, columnPtr, objPtr);
1953 }
1954 return result;
1955 }
1956
1957 /*
1958 *----------------------------------------------------------------------
1959 *
1960 * Blt_TupleUnsetValue --
1961 *
1962 * Removes the value at the given tuple and column key, if one
1963 * exists. The Tcl_Obj is decremented and the slot is set to
1964 * NULL. If no traces are currently active on the the slot, then
1965 * a call to check if any trace should fire is called.
1966 *
1967 * The row, column indices are assumed to be valid.
1968 *
1969 * Results:
1970 * Always returns TCL_OK.
1971 *
1972 * Side Effects:
1973 * Traces on the table location may be fired.
1974 *
1975 *----------------------------------------------------------------------
1976 */
1977 int
1978 Blt_TupleUnsetValue(
1979 Tcl_Interp *interp,
1980 TupleClient *clientPtr,
1981 Row *rowPtr,
1982 CONST char *key)
1983 {
1984 Blt_HashEntry *hPtr;
1985 Column *columnPtr;
1986 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
1987 char *left, *right;
1988 int result;
1989
1990 if (ParseParentheses(interp, key, &left, &right) != TCL_OK) {
1991 return TCL_ERROR;
1992 }
1993 if (left != NULL) {
1994 *left = *right = '\0';
1995 hPtr = Blt_FindHashEntry(&tupleObjPtr->columnTable, key);
1996 if (hPtr != NULL) {
1997 columnPtr = Blt_GetHashValue(hPtr);
1998 result = UnsetArrayValue(interp, clientPtr, rowPtr, columnPtr,
1999 left + 1);
2000 } else if (interp != NULL) {
2001 Tcl_AppendResult(interp, "can't find column \"", key,
2002 "\" in \"", tupleObjPtr->name, "\"", (char *)NULL);
2003 }
2004 *left = '(', *right = ')';
2005 if (hPtr == NULL) {
2006 return TCL_ERROR;
2007 }
2008 } else {
2009 hPtr = Blt_FindHashEntry(&tupleObjPtr->columnTable, key);
2010 if (hPtr == NULL) {
2011 if (interp != NULL) {
2012 Tcl_AppendResult(interp, "can't find column \"", key,
2013 "\" in \"", tupleObjPtr->name, "\"", (char *)NULL);
2014 }
2015 return TCL_ERROR;
2016 }
2017 columnPtr = Blt_GetHashValue(hPtr);
2018 result = UnsetValue(interp, clientPtr, rowPtr, columnPtr);
2019 }
2020 return result;
2021 }
2022
2023
2024 /*
2025 *----------------------------------------------------------------------
2026 *
2027 * Blt_TupleGetValueByIndex --
2028 *
2029 * Returns the value at the given row, column index, if one
2030 * exists. If no traces are currently active on the the slot,
2031 * then a call to check if any trace should fire is called.
2032 *
2033 * The row, column indices are assumed to be valid.
2034 *
2035 * Results:
2036 * Always returns TCL_OK.
2037 *
2038 * Side Effects:
2039 * Traces on the table location may be fired.
2040 *
2041 *----------------------------------------------------------------------
2042 */
2043 int
2044 Blt_TupleGetValueByIndex(
2045 Tcl_Interp *interp,
2046 TupleClient *clientPtr,
2047 unsigned int row,
2048 unsigned int column,
2049 Tcl_Obj **objPtrPtr)
2050 {
2051 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
2052 Row *rowPtr;
2053 Column *columnPtr;
2054
2055 assert((row >= 0) && (row < tupleObjPtr->nRows));
2056 assert((column >= 0) && (column < tupleObjPtr->nColumns));
2057
2058 rowPtr = tupleObjPtr->rows[row];
2059 columnPtr = tupleObjPtr->columns[column];
2060 return GetValue(interp, clientPtr, rowPtr, columnPtr, objPtrPtr);
2061 }
2062
2063
2064 /*
2065 *----------------------------------------------------------------------
2066 *
2067 * Blt_TupleSetValueByIndex --
2068 *
2069 * Sets the value at the given row, column index. If the slot
2070 * is currently filled, the old Tcl_Obj is decremented. The
2071 * new Tcl_Obj is incremented. If no traces are currently active
2072 * on the the slot, then a call to check if any trace should
2073 * fire is called. There are two type of traces that may fire:
2074 *
2075 * TUPLE_TRACE_WRITE indicates that the slot was
2076 * written to.
2077 * TUPLE_TRACE_CREATE indicates there was no former
2078 * value set.
2079 *
2080 * The row, column indices are assumed to be valid.
2081 *
2082 * Results:
2083 * Always returns TCL_OK.
2084 *
2085 * Side Effects:
2086 * Traces on the table location may be fired.
2087 *
2088 *----------------------------------------------------------------------
2089 */
2090 int
2091 Blt_TupleSetValueByIndex(
2092 Tcl_Interp *interp,
2093 TupleClient *clientPtr,
2094 unsigned int row,
2095 unsigned int column,
2096 Tcl_Obj *objPtr)
2097 {
2098 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
2099 Row *rowPtr;
2100 Column *columnPtr;
2101
2102 assert(row < tupleObjPtr->nRows);
2103 assert(column < tupleObjPtr->nColumns);
2104
2105 rowPtr = tupleObjPtr->rows[row];
2106 columnPtr = tupleObjPtr->columns[column];
2107 return SetValue(interp, clientPtr, rowPtr, columnPtr, objPtr);
2108 }
2109
2110 /*
2111 *----------------------------------------------------------------------
2112 *
2113 * Blt_TupleUnsetValueByIndex --
2114 *
2115 * Removes the value at the given row, column index, if one
2116 * exists. The Tcl_Obj is decremented and the slot is set
2117 * to NULL. If no traces are currently active on the the slot,
2118 * then a call to check if any trace should fire is called.
2119 *
2120 * It's not an error to "unset" nonexistent table locations.
2121 *
2122 * Results:
2123 * Always returns TCL_OK.
2124 *
2125 * Side Effects:
2126 * Traces on the table location may be fired.
2127 *
2128 *----------------------------------------------------------------------
2129 */
2130 int
2131 Blt_TupleUnsetValueByIndex(
2132 Tcl_Interp *interp,
2133 TupleClient *clientPtr,
2134 unsigned int row,
2135 unsigned int column)
2136 {
2137 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
2138 Row *rowPtr;
2139 Column *columnPtr;
2140
2141 assert(row < tupleObjPtr->nRows);
2142 assert(column < tupleObjPtr->nColumns);
2143
2144 rowPtr = tupleObjPtr->rows[row];
2145 columnPtr = tupleObjPtr->columns[column];
2146 return UnsetValue(interp, clientPtr, rowPtr, columnPtr);
2147 }
2148
2149 /*
2150 *----------------------------------------------------------------------
2151 *
2152 * Blt_TupleInsertRows --
2153 *
2154 * Creates a new rows in the table. The name and position in
2155 * the parent are also provided.
2156 *
2157 *----------------------------------------------------------------------
2158 */
2159 int
2160 Blt_TupleInsertRows(
2161 TupleClient *clientPtr, /* The tuple client that is creating
2162 * this node. If NULL, indicates to
2163 * trigger notify events on behalf of
2164 * the initiating client also. */
2165 unsigned int insertRow, /* Position in the parent's list of children
2166 * where to insert the new node. */
2167 unsigned int nRows) /* Number of rows tuples to insert. */
2168 {
2169 TupleObject *tupleObjPtr;
2170 int oldRows;
2171
2172 tupleObjPtr = clientPtr->tupleObjPtr;
2173
2174 if (insertRow >= tupleObjPtr->nRows) {
2175 insertRow = tupleObjPtr->nRows;
2176 }
2177 oldRows = tupleObjPtr->nRows;
2178 if (!ExtendRows(tupleObjPtr, nRows)) {
2179 return TCL_ERROR;
2180 }
2181 if (insertRow < oldRows) {
2182 int i, j;
2183
2184 /* Slide the rows down, creating new tuples in its place. */
2185 for (i = insertRow, j = insertRow + nRows; i < oldRows; i++, j++) {
2186 tupleObjPtr->rows[j] = tupleObjPtr->rows[i];
2187 tupleObjPtr->rows[j]->index = j;
2188 tupleObjPtr->rows[i] = NewRow(tupleObjPtr, i);
2189 }
2190 } else {
2191 int i;
2192
2193 for (i = oldRows; i < tupleObjPtr->nRows; i++) {
2194 tupleObjPtr->rows[i] = NewRow(tupleObjPtr, i);
2195 }
2196 }
2197 /*
2198 * Issue callbacks to each client indicating that a new node has
2199 * been created.
2200 */
2201 NotifyClients(clientPtr, tupleObjPtr, TUPLE_NOTIFY_CREATE);
2202 return insertRow;
2203 }
2204
2205 /*
2206 *----------------------------------------------------------------------
2207 *
2208 * Blt_TupleMoveRows --
2209 *
2210 * Move one of more rows to a new location in the tuple.
2211 *
2212 *----------------------------------------------------------------------
2213 */
2214 /*ARGSUSED*/
2215 int
2216 Blt_TupleMoveRows(
2217 TupleClient *clientPtr,
2218 int fromRow,
2219 int toRow,
2220 int nRows)
2221 {
2222 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
2223 Row *fromPtr;
2224 int i;
2225
2226 /* [ | |a| | | | | | | | |b| | | ] */
2227
2228 fromPtr = tupleObjPtr->rows[fromRow];
2229 for (i = fromRow; i < tupleObjPtr->nRows; i++) {
2230 /* Compress the array. */
2231 }
2232 /*
2233 * Issue callbacks to each client indicating that a node has
2234 * been moved.
2235 */
2236 NotifyClients(clientPtr, tupleObjPtr, TUPLE_NOTIFY_MOVE);
2237 return TCL_OK;
2238 }
2239
2240 /*
2241 *----------------------------------------------------------------------
2242 *
2243 * Blt_TupleDeleteTuple --
2244 *
2245 * Deletes one or more rows starting at the given index.
2246 * This will cause a single delete event.
2247 *
2248 * Results:
2249 * Returns the row at the given index. If the index is out
2250 * of range, then NULL is returned.
2251 *
2252 *----------------------------------------------------------------------
2253 */
2254 void
2255 Blt_TupleDeleteTuple(
2256 TupleClient *clientPtr,
2257 Row *rowPtr)
2258 {
2259 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
2260 int i, j;
2261
2262 if (!tupleObjPtr->notifyHold) {
2263 /*
2264 * Issue callbacks to each client indicating that a tuple will be
2265 * removed.
2266 */
2267 NotifyClients(clientPtr, tupleObjPtr, TUPLE_NOTIFY_DELETE_ROW);
2268 for (j = tupleObjPtr->nRows - 1, i = j - 1; i >= rowPtr->index;
2269 i--, j--) {
2270 tupleObjPtr->rows[i] = tupleObjPtr->rows[j];
2271 tupleObjPtr->rows[i]->index = i;
2272 }
2273 tupleObjPtr->nRows--;
2274 }
2275 if (Blt_ChainGetLength(tupleObjPtr->clients) < 2) {
2276 Blt_TupleClearTags(clientPtr, rowPtr);
2277 }
2278 DeleteRow(tupleObjPtr, rowPtr);
2279 }
2280
2281 /*
2282 *----------------------------------------------------------------------
2283 *
2284 * Blt_TupleStartDeleteRows --
2285 *
2286 * Deletes one or more rows starting at the given index.
2287 * This will cause a single delete event.
2288 *
2289 * Results:
2290 * Returns the row at the given index. If the index is out
2291 * of range, then NULL is returned.
2292 *
2293 *----------------------------------------------------------------------
2294 */
2295 void
2296 Blt_TupleStartDeleteRows(TupleClient *clientPtr)
2297 {
2298 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
2299
2300 tupleObjPtr->notifyHold = TRUE;
2301 }
2302
2303 /*
2304 *----------------------------------------------------------------------
2305 *
2306 * Blt_TupleEndDeleteRows --
2307 *
2308 * Deletes one or more rows starting at the given index.
2309 * This will cause a single delete event.
2310 *
2311 * Results:
2312 * Returns the row at the given index. If the index is out
2313 * of range, then NULL is returned.
2314 *
2315 *----------------------------------------------------------------------
2316 */
2317 void
2318 Blt_TupleEndDeleteRows(TupleClient *clientPtr)
2319 {
2320 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
2321 int first;
2322 int count;
2323 int i;
2324
2325 /*
2326 * Issue callbacks to each client indicating that a tuple will be
2327 * removed.
2328 */
2329 tupleObjPtr->notifyHold = FALSE;
2330 count = 0;
2331 for (i = 0; i < tupleObjPtr->nRows; i++) {
2332 if (tupleObjPtr->rows[i] == NULL) {
2333 if (first == -1) {
2334 first = i;
2335 }
2336 continue;
2337 }
2338 if (count < i) {
2339 tupleObjPtr->rows[count] = tupleObjPtr->rows[i];
2340 tupleObjPtr->rows[count]->index = count;
2341 }
2342 count++;
2343 }
2344 tupleObjPtr->nRows = count++;
2345 NotifyClients(clientPtr, tupleObjPtr, TUPLE_NOTIFY_DELETE_ROW);
2346 }
2347
2348 /*
2349 *----------------------------------------------------------------------
2350 *
2351 * Blt_TupleCreateTrace --
2352 *
2353 * Creates a trace for one or more tuples with one or more column
2354 * keys. Whenever a matching action occurs in the tuple object,
2355 * the specified procedure is executed.
2356 *
2357 * Results:
2358 * Returns a token for the trace.
2359 *
2360 * Side Effects:
2361 * Memory is allocated for the trace.
2362 *
2363 *----------------------------------------------------------------------
2364 */
2365 Blt_TupleTrace
2366 Blt_TupleCreateTrace(
2367 TupleClient *clientPtr,
2368 Row *rowPtr, /* Tuple to be traced. May be NULL,
2369 * indicating no row. */
2370 CONST char *tagName, /* Tag of tuple(s) to be traced. May
2371 * be NULL. */
2372 CONST char *keyList, /* Tcl list of keys. Split and freed
2373 * by Blt_TupleDeleteTrace. May be
2374 * NULL indicating all keys match. */
2375 unsigned int mask, /* Bit mask indicating what actions to
2376 trace. */
2377 Blt_TupleTraceProc *proc, /* Callback procedure for the trace. */
2378 ClientData clientData) /* One-word of data passed along when
2379 * the callback is executed. */
2380 {
2381 Trace *tracePtr;
2382 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
2383
2384 tracePtr = Blt_Calloc(1, sizeof (Trace));
2385 assert(tracePtr);
2386 if (keyList != NULL) {
2387 CONST char **keys;
2388 int nKeys;
2389
2390 if (Tcl_SplitList(clientPtr->interp, keyList, &nKeys, (char ***)&keys)
2391 != TCL_OK) {
2392 Blt_Free(tracePtr);
2393 return NULL;
2394 }
2395 if (nKeys == 0) {
2396 Blt_Free(keys);
2397 } else {
2398 tracePtr->keys = keys;
2399 }
2400 }
2401 if (tagName != NULL) {
2402 tracePtr->withTag = Blt_Strdup(tagName);
2403 }
2404 tracePtr->table = clientPtr;
2405 tracePtr->proc = proc;
2406 tracePtr->clientData = clientData;
2407 tracePtr->mask = mask;
2408 tracePtr->tuple = rowPtr;
2409 tracePtr->withTag = NULL;
2410 tracePtr->linkPtr = Blt_ChainAppend(tupleObjPtr->traces, tracePtr);
2411 return tracePtr;
2412 }
2413
2414 /*
2415 *----------------------------------------------------------------------
2416 *
2417 * Blt_TupleDeleteTrace --
2418 *
2419 * Deletes a trace.
2420 *
2421 * Results:
2422 * None.
2423 *
2424 * Side Effects:
2425 * Memory is de-allocated for the trace.
2426 *
2427 *----------------------------------------------------------------------
2428 */
2429 void
2430 Blt_TupleDeleteTrace(Trace *tracePtr)
2431 {
2432 Blt_ChainDeleteLink(tracePtr->table->tupleObjPtr->traces,
2433 tracePtr->linkPtr);
2434 if (tracePtr->keys != NULL) {
2435 Blt_Free(tracePtr->keys);
2436 }
2437 if (tracePtr->withTag != NULL) {
2438 Blt_Free(tracePtr->withTag);
2439 }
2440 Blt_Free(tracePtr);
2441 }
2442
2443 /*
2444 *----------------------------------------------------------------------
2445 *
2446 * Blt_TupleExtendRows --
2447 *
2448 * Adds new rows to the table. Rows are slots in an array
2449 * of Rows. The array grows by doubling its size, so there
2450 * may be more slots than needed (# rows).
2451 *
2452 * Results:
2453 * Returns TCL_OK is the tuple is resized and TCL_ERROR if an
2454 * not enough memory was available.
2455 *
2456 * Side Effects:
2457 * If more rows are needed, the array which holds the tuples is
2458 * reallocated by doubling its size.
2459 *
2460 *----------------------------------------------------------------------
2461 */
2462 int
2463 Blt_TupleExtendRows(TupleClient *clientPtr, unsigned int nRows)
2464 {
2465 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
2466 int oldSize;
2467 int i;
2468
2469 oldSize = tupleObjPtr->nRows;
2470 if (!ExtendRows(tupleObjPtr, nRows)) {
2471 return TCL_ERROR;
2472 }
2473 for (i = oldSize; i < tupleObjPtr->nRows; i++) {
2474 tupleObjPtr->rows[i] = NewRow(tupleObjPtr, i);
2475 }
2476 tupleObjPtr->nRows = nRows;
2477 NotifyClients(clientPtr, tupleObjPtr, TUPLE_NOTIFY_CREATE);
2478 return TCL_OK;
2479 }
2480
2481 int
2482 Blt_TupleReduceRows(TupleClient *clientPtr, int nRows)
2483 {
2484 TupleObject *tupleObjPtr = clientPtr->tupleObjPtr;
2485
2486 if (nRows < tupleObjPtr->nRows) {
2487 if (tupleObjPtr->rowsAllocated < nRows) {
2488 Row **rows;
2489
2490 while (tupleObjPtr->rowsAllocated < nRows) {
2491 tupleObjPtr->rowsAllocated += tupleObjPtr->rowsAllocated;
2492 }
2493 rows = Blt_Realloc(tupleObjPtr->rows,
2494 tupleObjPtr->rowsAllocated * sizeof(Row *));
2495 if (rows == NULL) {
2496 return TCL_ERROR;
2497 }
2498 tupleObjPtr->rows = rows;
2499 }
2500 tupleObjPtr->nRows = nRows;
2501 }
2502 return TCL_OK;
2503 }
2504
2505 /*
2506 *----------------------------------------------------------------------
2507 *
2508 * Blt_TupleAddTag --
2509 *
2510 *----------------------------------------------------------------------
2511 */
2512 void
2513 Blt_TupleAddTag(
2514 TupleClient *clientPtr,
2515 Row *rowPtr,
2516 CONST char *tagName)
2517 {
2518 if (strcmp(tagName, "all") != 0) {
2519 Blt_HashEntry *hPtr;
2520 Blt_HashTable *tablePtr;
2521 Blt_TupleTagEntry *tPtr;
2522 int isNew;
2523
2524 tablePtr = &clientPtr->tagTablePtr->tagTable;
2525 hPtr = Blt_CreateHashEntry(tablePtr, tagName, &isNew);
2526 assert(hPtr);
2527 if (isNew) {
2528 tPtr = Blt_Malloc(sizeof(Blt_TupleTagEntry));
2529 Blt_InitHashTable(&tPtr->rowTable, BLT_ONE_WORD_KEYS);
2530 Blt_SetHashValue(hPtr, tPtr);
2531 tPtr->hashPtr = hPtr;
2532 tPtr->tagName = Blt_GetHashKey(tablePtr, hPtr);
2533 } else {
2534 tPtr = Blt_GetHashValue(hPtr);
2535 }
2536 hPtr = Blt_CreateHashEntry(&tPtr->rowTable, (char *)rowPtr, &isNew);
2537 assert(hPtr);
2538 if (isNew) {
2539 Blt_SetHashValue(hPtr, rowPtr);
2540 }
2541 }
2542 }
2543
2544 /*
2545 *----------------------------------------------------------------------
2546 *
2547 * Blt_TupleClearTags --
2548 *
2549 *----------------------------------------------------------------------
2550 */
2551 void
2552 Blt_TupleClearTags(TupleClient *clientPtr, Row *rowPtr)
2553 {
2554 Blt_HashEntry *hPtr, *h2Ptr;
2555 Blt_HashSearch cursor;
2556 Blt_TupleTagEntry *tPtr;
2557
2558 for (hPtr = Blt_FirstHashEntry(&clientPtr->tagTablePtr->tagTable, &cursor);
2559 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
2560 tPtr = Blt_GetHashValue(hPtr);
2561 h2Ptr = Blt_FindHashEntry(&tPtr->rowTable, (char *)rowPtr);
2562 if (h2Ptr != NULL) {
2563 Blt_DeleteHashEntry(&tPtr->rowTable, h2Ptr);
2564 }
2565 }
2566 }
2567
2568 /*
2569 *----------------------------------------------------------------------
2570 *
2571 * Blt_TupleFirstTag --
2572 *
2573 *----------------------------------------------------------------------
2574 */
2575 Blt_HashEntry *
2576 Blt_TupleFirstTag(
2577 TupleClient *clientPtr,
2578 Blt_HashSearch *cursorPtr)
2579 {
2580 return Blt_FirstHashEntry(&clientPtr->tagTablePtr->tagTable, cursorPtr);
2581 }
2582
2583 /*
2584 *----------------------------------------------------------------------
2585 *
2586 * Blt_TupleForgetTag --
2587 *
2588 *----------------------------------------------------------------------
2589 */
2590 void
2591 Blt_TupleForgetTag(
2592 TupleClient *clientPtr,
2593 CONST char *tagName)
2594 {
2595 Blt_HashEntry *hPtr;
2596
2597 hPtr = Blt_FindHashEntry(&clientPtr->tagTablePtr->tagTable, tagName);
2598 if (hPtr != NULL) {
2599 Blt_TupleTagEntry *tPtr;
2600
2601 tPtr = Blt_GetHashValue(hPtr);
2602 Blt_DeleteHashEntry(&clientPtr->tagTablePtr->tagTable, hPtr);
2603 Blt_DeleteHashTable(&tPtr->rowTable);
2604 Blt_Free(tPtr);
2605 }
2606 }
2607
2608 /*
2609 *----------------------------------------------------------------------
2610 *
2611 * Blt_TupleHasTag --
2612 *
2613 *----------------------------------------------------------------------
2614 */
2615 int
2616 Blt_TupleHasTag(
2617 TupleClient *clientPtr,
2618 Row *rowPtr,
2619 CONST char *tagName)
2620 {
2621 Blt_HashEntry *hPtr;
2622
2623 if (strcmp(tagName, "all") == 0) {
2624 return TRUE;
2625 }
2626 hPtr = Blt_FindHashEntry(&clientPtr->tagTablePtr->tagTable, tagName);
2627 if (hPtr != NULL) {
2628 Blt_TupleTagEntry *tPtr;
2629
2630 tPtr = Blt_GetHashValue(hPtr);
2631 hPtr = Blt_FindHashEntry(&tPtr->rowTable, (char *)rowPtr);
2632 if (hPtr != NULL) {
2633 return TRUE;
2634 }
2635 }
2636 return FALSE;
2637 }
2638
2639 /*
2640 *----------------------------------------------------------------------
2641 *
2642 * Blt_TupleRemoveTag --
2643 *
2644 *----------------------------------------------------------------------
2645 */
2646 void
2647 Blt_TupleRemoveTag(
2648 TupleClient *clientPtr,
2649 Row *rowPtr,
2650 CONST char *tagName)
2651 {
2652 if (strcmp(tagName, "all") != 0) {
2653 Blt_HashEntry *hPtr;
2654 Blt_HashTable *tablePtr;
2655
2656 tablePtr = &clientPtr->tagTablePtr->tagTable;
2657 hPtr = Blt_FindHashEntry(tablePtr, tagName);
2658 if (hPtr != NULL) {
2659 Blt_TupleTagEntry *tPtr;
2660
2661 tPtr = Blt_GetHashValue(hPtr);
2662
2663 hPtr = Blt_FindHashEntry(&tPtr->rowTable, (char *)rowPtr);
2664 if (hPtr != NULL) {
2665 Blt_DeleteHashEntry(&tPtr->rowTable, hPtr);
2666 }
2667 }
2668 }
2669 }
2670
2671
2672 /*
2673 *----------------------------------------------------------------------
2674 *
2675 * Blt_TupleTagSetHashTable --
2676 *
2677 *----------------------------------------------------------------------
2678 */
2679 Blt_HashTable *
2680 Blt_TupleTagTableHashTable(
2681 TupleClient *clientPtr,
2682 CONST char *tagName)
2683 {
2684 Blt_HashEntry *hPtr;
2685
2686 hPtr = Blt_FindHashEntry(&clientPtr->tagTablePtr->tagTable, tagName);
2687 if (hPtr != NULL) {
2688 Blt_TupleTagEntry *tPtr;
2689
2690 tPtr = Blt_GetHashValue(hPtr);
2691 return &tPtr->rowTable;
2692 }
2693 return NULL;
2694 }
2695
2696 int
2697 Blt_TupleTagTableIsShared(TupleClient *clientPtr)
2698 {
2699 return (clientPtr->tagTablePtr->refCount > 1);
2700 }
2701
2702 /*
2703 *----------------------------------------------------------------------
2704 *
2705 * Blt_TupleFirstTagged --
2706 *
2707 * Returns the id of the first tuple tagged by the given tag in
2708 * objPtr.
2709 *
2710 *----------------------------------------------------------------------
2711 */
2712 Blt_Tuple
2713 Blt_TupleFirstTagged(
2714 Tcl_Interp *interp,
2715 TupleClient *clientPtr,
2716 Tcl_Obj *objPtr,
2717 Blt_TupleTagSearch *cursorPtr)
2718 {
2719 char *tagName;
2720 cursorPtr->tagType = TAG_TYPE_NONE;
2721
2722 tagName = Tcl_GetString(objPtr);
2723 /* Process strings that start with digits as indices, not tags. */
2724 if (isdigit(UCHAR(*tagName))) {
2725 int row;
2726
2727 if (Tcl_GetIntFromObj(interp, objPtr, &row) != TCL_OK) {
2728 return NULL;
2729 }
2730 if ((row < 0) || (row >= clientPtr->tupleObjPtr->nRows)) {
2731 Tcl_AppendResult(interp, "row index \"", tagName,
2732 "\" is out of range", (char *)NULL);
2733 return NULL;
2734 }
2735 return clientPtr->tupleObjPtr->rows[row];
2736 }
2737 cursorPtr->nRows = clientPtr->tupleObjPtr->nRows;
2738 if (strcmp(tagName, "all") == 0) {
2739 cursorPtr->tagType = TAG_TYPE_ALL;
2740 cursorPtr->row = 0;
2741 return clientPtr->tupleObjPtr->rows[0];
2742 } else {
2743 Blt_HashEntry *hPtr;
2744
2745 hPtr = Blt_FindHashEntry(&clientPtr->tagTablePtr->tagTable, tagName);
2746 if (hPtr != NULL) {
2747 Blt_TupleTagEntry *tPtr;
2748 Row *rowPtr;
2749
2750 tPtr = Blt_GetHashValue(hPtr);
2751 cursorPtr->rowTablePtr = &tPtr->rowTable;
2752 cursorPtr->tagType = TAG_TYPE_TAG;
2753 hPtr = Blt_FirstHashEntry(cursorPtr->rowTablePtr,
2754 &cursorPtr->cursor);
2755 if (hPtr == NULL) {
2756 return NULL;
2757 }
2758 rowPtr = Blt_GetHashValue(hPtr);
2759 return rowPtr;
2760 }
2761 }
2762 Tcl_AppendResult(interp, "can't find tag or id \"", tagName, "\" in ",
2763 clientPtr->tupleObjPtr->name, (char *)NULL);
2764 return NULL;
2765 }
2766
2767 /*
2768 *----------------------------------------------------------------------
2769 *
2770 * Blt_TupleNextTagged --
2771 *
2772 *----------------------------------------------------------------------
2773 */
2774 Blt_Tuple
2775 Blt_TupleNextTagged(Blt_TupleTagSearch *cursorPtr)
2776 {
2777 if (cursorPtr->tagType == TAG_TYPE_ALL) {
2778 TupleClient *clientPtr = cursorPtr->table;
2779
2780 cursorPtr->row++;
2781 if (cursorPtr->row >= clientPtr->tupleObjPtr->nRows) {
2782 return NULL;
2783 }
2784 return clientPtr->tupleObjPtr->rows[cursorPtr->row];
2785 }
2786 if (cursorPtr->tagType == TAG_TYPE_TAG) {
2787 Blt_HashEntry *hPtr;
2788 Row *rowPtr;
2789
2790 hPtr = Blt_NextHashEntry(&cursorPtr->cursor);
2791 if (hPtr == NULL) {
2792 return NULL;
2793 }
2794 rowPtr = Blt_GetHashValue(hPtr);
2795 return rowPtr;
2796 }
2797 return NULL;
2798 }