Debian Codegraph @

pTk/mTk/tclGeneric/tclListObj.c

raw symbols SCIP
1 /*
2 * tclListObj.c --
3 *
4 * This file contains procedures that implement the Tcl list object
5 * type.
6 *
7 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
8 * Copyright (c) 1998 by Scriptics Corporation.
9 * Copyright (c) 2001 by Kevin B. Kenny. All rights reserved.
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: tclListObj.c,v 1.13 2002/01/07 23:09:13 dgp Exp $
15 */
16
17 #include "tclInt.h"
18
19 /*
20 * Prototypes for procedures defined later in this file:
21 */
22
23 static void DupListInternalRep _ANSI_ARGS_((Tcl_Obj *srcPtr,
24 Tcl_Obj *copyPtr));
25 static void FreeListInternalRep _ANSI_ARGS_((Tcl_Obj *listPtr));
26 static int SetListFromAny _ANSI_ARGS_((Tcl_Interp *interp,
27 Tcl_Obj *objPtr));
28 static void UpdateStringOfList _ANSI_ARGS_((Tcl_Obj *listPtr));
29
30 /*
31 * The structure below defines the list Tcl object type by means of
32 * procedures that can be invoked by generic object code.
33 *
34 * The internal representation of a list object is a two-pointer
35 * representation. The first pointer designates a List structure that
36 * contains an array of pointers to the element objects, together with
37 * integers that represent the current element count and the allocated
38 * size of the array. The second pointer is normally NULL; during
39 * execution of functions in this file that operate on nested sublists,
40 * it is occasionally used as working storage to avoid an auxiliary
41 * stack.
42 */
43
44 Tcl_ObjType tclListType = {
45 "list", /* name */
46 FreeListInternalRep, /* freeIntRepProc */
47 DupListInternalRep, /* dupIntRepProc */
48 UpdateStringOfList, /* updateStringProc */
49 SetListFromAny /* setFromAnyProc */
50 };
51
52 /*
53 *----------------------------------------------------------------------
54 *
55 * Tcl_NewListObj --
56 *
57 * This procedure is normally called when not debugging: i.e., when
58 * TCL_MEM_DEBUG is not defined. It creates a new list object from an
59 * (objc,objv) array: that is, each of the objc elements of the array
60 * referenced by objv is inserted as an element into a new Tcl object.
61 *
62 * When TCL_MEM_DEBUG is defined, this procedure just returns the
63 * result of calling the debugging version Tcl_DbNewListObj.
64 *
65 * Results:
66 * A new list object is returned that is initialized from the object
67 * pointers in objv. If objc is less than or equal to zero, an empty
68 * object is returned. The new object's string representation
69 * is left NULL. The resulting new list object has ref count 0.
70 *
71 * Side effects:
72 * The ref counts of the elements in objv are incremented since the
73 * resulting list now refers to them.
74 *
75 *----------------------------------------------------------------------
76 */
77
78 #ifdef TCL_MEM_DEBUG
79 #undef Tcl_NewListObj
80
81 Tcl_Obj *
82 Tcl_NewListObj(objc, objv)
83 int objc; /* Count of objects referenced by objv. */
84 Tcl_Obj *CONST objv[]; /* An array of pointers to Tcl objects. */
85 {
86 return Tcl_DbNewListObj(objc, objv, "unknown", 0);
87 }
88
89 #else /* if not TCL_MEM_DEBUG */
90
91 Tcl_Obj *
92 Tcl_NewListObj(objc, objv)
93 int objc; /* Count of objects referenced by objv. */
94 Tcl_Obj *CONST objv[]; /* An array of pointers to Tcl objects. */
95 {
96 register Tcl_Obj *listPtr;
97 register Tcl_Obj **elemPtrs;
98 register List *listRepPtr;
99 int i;
100
101 TclNewObj(listPtr);
102
103 if (objc > 0) {
104 Tcl_InvalidateStringRep(listPtr);
105
106 elemPtrs = (Tcl_Obj **)
107 ckalloc((unsigned) (objc * sizeof(Tcl_Obj *)));
108 for (i = 0; i < objc; i++) {
109 elemPtrs[i] = objv[i];
110 Tcl_IncrRefCount(elemPtrs[i]);
111 }
112
113 listRepPtr = (List *) ckalloc(sizeof(List));
114 listRepPtr->maxElemCount = objc;
115 listRepPtr->elemCount = objc;
116 listRepPtr->elements = elemPtrs;
117
118 listPtr->internalRep.twoPtrValue.ptr1 = (VOID *) listRepPtr;
119 listPtr->internalRep.twoPtrValue.ptr2 = NULL;
120 listPtr->typePtr = &tclListType;
121 }
122 return listPtr;
123 }
124 #endif /* if TCL_MEM_DEBUG */
125
126 /*
127 *----------------------------------------------------------------------
128 *
129 * Tcl_DbNewListObj --
130 *
131 * This procedure is normally called when debugging: i.e., when
132 * TCL_MEM_DEBUG is defined. It creates new list objects. It is the
133 * same as the Tcl_NewListObj procedure above except that it calls
134 * Tcl_DbCkalloc directly with the file name and line number from its
135 * caller. This simplifies debugging since then the [memory active]
136 * command will report the correct file name and line number when
137 * reporting objects that haven't been freed.
138 *
139 * When TCL_MEM_DEBUG is not defined, this procedure just returns the
140 * result of calling Tcl_NewListObj.
141 *
142 * Results:
143 * A new list object is returned that is initialized from the object
144 * pointers in objv. If objc is less than or equal to zero, an empty
145 * object is returned. The new object's string representation
146 * is left NULL. The new list object has ref count 0.
147 *
148 * Side effects:
149 * The ref counts of the elements in objv are incremented since the
150 * resulting list now refers to them.
151 *
152 *----------------------------------------------------------------------
153 */
154
155 #ifdef TCL_MEM_DEBUG
156
157 Tcl_Obj *
158 Tcl_DbNewListObj(objc, objv, file, line)
159 int objc; /* Count of objects referenced by objv. */
160 Tcl_Obj *CONST objv[]; /* An array of pointers to Tcl objects. */
161 CONST char *file; /* The name of the source file calling this
162 * procedure; used for debugging. */
163 int line; /* Line number in the source file; used
164 * for debugging. */
165 {
166 register Tcl_Obj *listPtr;
167 register Tcl_Obj **elemPtrs;
168 register List *listRepPtr;
169 int i;
170
171 TclDbNewObj(listPtr, file, line);
172
173 if (objc > 0) {
174 Tcl_InvalidateStringRep(listPtr);
175
176 elemPtrs = (Tcl_Obj **)
177 ckalloc((unsigned) (objc * sizeof(Tcl_Obj *)));
178 for (i = 0; i < objc; i++) {
179 elemPtrs[i] = objv[i];
180 Tcl_IncrRefCount(elemPtrs[i]);
181 }
182
183 listRepPtr = (List *) ckalloc(sizeof(List));
184 listRepPtr->maxElemCount = objc;
185 listRepPtr->elemCount = objc;
186 listRepPtr->elements = elemPtrs;
187
188 listPtr->internalRep.twoPtrValue.ptr1 = (VOID *) listRepPtr;
189 listPtr->internalRep.twoPtrValue.ptr2 = NULL;
190 listPtr->typePtr = &tclListType;
191 }
192 return listPtr;
193 }
194
195 #else /* if not TCL_MEM_DEBUG */
196
197 Tcl_Obj *
198 Tcl_DbNewListObj(objc, objv, file, line)
199 int objc; /* Count of objects referenced by objv. */
200 Tcl_Obj *CONST objv[]; /* An array of pointers to Tcl objects. */
201 CONST char *file; /* The name of the source file calling this
202 * procedure; used for debugging. */
203 int line; /* Line number in the source file; used
204 * for debugging. */
205 {
206 return Tcl_NewListObj(objc, objv);
207 }
208 #endif /* TCL_MEM_DEBUG */
209
210 /*
211 *----------------------------------------------------------------------
212 *
213 * Tcl_SetListObj --
214 *
215 * Modify an object to be a list containing each of the objc elements
216 * of the object array referenced by objv.
217 *
218 * Results:
219 * None.
220 *
221 * Side effects:
222 * The object is made a list object and is initialized from the object
223 * pointers in objv. If objc is less than or equal to zero, an empty
224 * object is returned. The new object's string representation
225 * is left NULL. The ref counts of the elements in objv are incremented
226 * since the list now refers to them. The object's old string and
227 * internal representations are freed and its type is set NULL.
228 *
229 *----------------------------------------------------------------------
230 */
231
232 void
233 Tcl_SetListObj(objPtr, objc, objv)
234 Tcl_Obj *objPtr; /* Object whose internal rep to init. */
235 int objc; /* Count of objects referenced by objv. */
236 Tcl_Obj *CONST objv[]; /* An array of pointers to Tcl objects. */
237 {
238 register Tcl_Obj **elemPtrs;
239 register List *listRepPtr;
240 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
241 int i;
242
243 if (Tcl_IsShared(objPtr)) {
244 panic("Tcl_SetListObj called with shared object");
245 }
246
247 /*
248 * Free any old string rep and any internal rep for the old type.
249 */
250
251 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
252 oldTypePtr->freeIntRepProc(objPtr);
253 }
254 objPtr->typePtr = NULL;
255 Tcl_InvalidateStringRep(objPtr);
256
257 /*
258 * Set the object's type to "list" and initialize the internal rep.
259 * However, if there are no elements to put in the list, just give
260 * the object an empty string rep and a NULL type.
261 */
262
263 if (objc > 0) {
264 elemPtrs = (Tcl_Obj **)
265 ckalloc((unsigned) (objc * sizeof(Tcl_Obj *)));
266 for (i = 0; i < objc; i++) {
267 elemPtrs[i] = objv[i];
268 Tcl_IncrRefCount(elemPtrs[i]);
269 }
270
271 listRepPtr = (List *) ckalloc(sizeof(List));
272 listRepPtr->maxElemCount = objc;
273 listRepPtr->elemCount = objc;
274 listRepPtr->elements = elemPtrs;
275
276 objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) listRepPtr;
277 objPtr->internalRep.twoPtrValue.ptr2 = NULL;
278 objPtr->typePtr = &tclListType;
279 } else {
280 objPtr->bytes = tclEmptyStringRep;
281 objPtr->length = 0;
282 }
283 }
284
285 /*
286 *----------------------------------------------------------------------
287 *
288 * Tcl_ListObjGetElements --
289 *
290 * This procedure returns an (objc,objv) array of the elements in a
291 * list object.
292 *
293 * Results:
294 * The return value is normally TCL_OK; in this case *objcPtr is set to
295 * the count of list elements and *objvPtr is set to a pointer to an
296 * array of (*objcPtr) pointers to each list element. If listPtr does
297 * not refer to a list object and the object can not be converted to
298 * one, TCL_ERROR is returned and an error message will be left in
299 * the interpreter's result if interp is not NULL.
300 *
301 * The objects referenced by the returned array should be treated as
302 * readonly and their ref counts are _not_ incremented; the caller must
303 * do that if it holds on to a reference. Furthermore, the pointer
304 * and length returned by this procedure may change as soon as any
305 * procedure is called on the list object; be careful about retaining
306 * the pointer in a local data structure.
307 *
308 * Side effects:
309 * The possible conversion of the object referenced by listPtr
310 * to a list object.
311 *
312 *----------------------------------------------------------------------
313 */
314
315 int
316 Tcl_ListObjGetElements(interp, listPtr, objcPtr, objvPtr)
317 Tcl_Interp *interp; /* Used to report errors if not NULL. */
318 register Tcl_Obj *listPtr; /* List object for which an element array
319 * is to be returned. */
320 int *objcPtr; /* Where to store the count of objects
321 * referenced by objv. */
322 Tcl_Obj ***objvPtr; /* Where to store the pointer to an array
323 * of pointers to the list's objects. */
324 {
325 register List *listRepPtr;
326
327 if (listPtr->typePtr != &tclListType) {
328 int result = SetListFromAny(interp, listPtr);
329 if (result != TCL_OK) {
330 return result;
331 }
332 }
333 listRepPtr = (List *) listPtr->internalRep.twoPtrValue.ptr1;
334 *objcPtr = listRepPtr->elemCount;
335 *objvPtr = listRepPtr->elements;
336 return TCL_OK;
337 }
338
339 /*
340 *----------------------------------------------------------------------
341 *
342 * Tcl_ListObjAppendList --
343 *
344 * This procedure appends the objects in the list referenced by
345 * elemListPtr to the list object referenced by listPtr. If listPtr is
346 * not already a list object, an attempt will be made to convert it to
347 * one.
348 *
349 * Results:
350 * The return value is normally TCL_OK. If listPtr or elemListPtr do
351 * not refer to list objects and they can not be converted to one,
352 * TCL_ERROR is returned and an error message is left in
353 * the interpreter's result if interp is not NULL.
354 *
355 * Side effects:
356 * The reference counts of the elements in elemListPtr are incremented
357 * since the list now refers to them. listPtr and elemListPtr are
358 * converted, if necessary, to list objects. Also, appending the
359 * new elements may cause listObj's array of element pointers to grow.
360 * listPtr's old string representation, if any, is invalidated.
361 *
362 *----------------------------------------------------------------------
363 */
364
365 int
366 Tcl_ListObjAppendList(interp, listPtr, elemListPtr)
367 Tcl_Interp *interp; /* Used to report errors if not NULL. */
368 register Tcl_Obj *listPtr; /* List object to append elements to. */
369 Tcl_Obj *elemListPtr; /* List obj with elements to append. */
370 {
371 register List *listRepPtr;
372 int listLen, objc, result;
373 Tcl_Obj **objv;
374
375 if (Tcl_IsShared(listPtr)) {
376 panic("Tcl_ListObjAppendList called with shared object");
377 }
378 if (listPtr->typePtr != &tclListType) {
379 result = SetListFromAny(interp, listPtr);
380 if (result != TCL_OK) {
381 return result;
382 }
383 }
384 listRepPtr = (List *) listPtr->internalRep.twoPtrValue.ptr1;
385 listLen = listRepPtr->elemCount;
386
387 result = Tcl_ListObjGetElements(interp, elemListPtr, &objc, &objv);
388 if (result != TCL_OK) {
389 return result;
390 }
391
392 /*
393 * Insert objc new elements starting after the lists's last element.
394 * Delete zero existing elements.
395 */
396
397 return Tcl_ListObjReplace(interp, listPtr, listLen, 0, objc, objv);
398 }
399
400 /*
401 *----------------------------------------------------------------------
402 *
403 * Tcl_ListObjAppendElement --
404 *
405 * This procedure is a special purpose version of
406 * Tcl_ListObjAppendList: it appends a single object referenced by
407 * objPtr to the list object referenced by listPtr. If listPtr is not
408 * already a list object, an attempt will be made to convert it to one.
409 *
410 * Results:
411 * The return value is normally TCL_OK; in this case objPtr is added
412 * to the end of listPtr's list. If listPtr does not refer to a list
413 * object and the object can not be converted to one, TCL_ERROR is
414 * returned and an error message will be left in the interpreter's
415 * result if interp is not NULL.
416 *
417 * Side effects:
418 * The ref count of objPtr is incremented since the list now refers
419 * to it. listPtr will be converted, if necessary, to a list object.
420 * Also, appending the new element may cause listObj's array of element
421 * pointers to grow. listPtr's old string representation, if any,
422 * is invalidated.
423 *
424 *----------------------------------------------------------------------
425 */
426
427 int
428 Tcl_ListObjAppendElement(interp, listPtr, objPtr)
429 Tcl_Interp *interp; /* Used to report errors if not NULL. */
430 Tcl_Obj *listPtr; /* List object to append objPtr to. */
431 Tcl_Obj *objPtr; /* Object to append to listPtr's list. */
432 {
433 register List *listRepPtr;
434 register Tcl_Obj **elemPtrs;
435 int numElems, numRequired;
436
437 if (Tcl_IsShared(listPtr)) {
438 panic("Tcl_ListObjAppendElement called with shared object");
439 }
440 if (listPtr->typePtr != &tclListType) {
441 int result = SetListFromAny(interp, listPtr);
442 if (result != TCL_OK) {
443 return result;
444 }
445 }
446
447 listRepPtr = (List *) listPtr->internalRep.twoPtrValue.ptr1;
448 elemPtrs = listRepPtr->elements;
449 numElems = listRepPtr->elemCount;
450 numRequired = numElems + 1 ;
451
452 /*
453 * If there is no room in the current array of element pointers,
454 * allocate a new, larger array and copy the pointers to it.
455 */
456
457 if (numRequired > listRepPtr->maxElemCount) {
458 int newMax = (2 * numRequired);
459 Tcl_Obj **newElemPtrs = (Tcl_Obj **)
460 ckalloc((unsigned) (newMax * sizeof(Tcl_Obj *)));
461
462 memcpy((VOID *) newElemPtrs, (VOID *) elemPtrs,
463 (size_t) (numElems * sizeof(Tcl_Obj *)));
464
465 listRepPtr->maxElemCount = newMax;
466 listRepPtr->elements = newElemPtrs;
467 ckfree((char *) elemPtrs);
468 elemPtrs = newElemPtrs;
469 }
470
471 /*
472 * Add objPtr to the end of listPtr's array of element
473 * pointers. Increment the ref count for the (now shared) objPtr.
474 */
475
476 elemPtrs[numElems] = objPtr;
477 Tcl_IncrRefCount(objPtr);
478 listRepPtr->elemCount++;
479
480 /*
481 * Invalidate any old string representation since the list's internal
482 * representation has changed.
483 */
484
485 Tcl_InvalidateStringRep(listPtr);
486 return TCL_OK;
487 }
488
489 /*
490 *----------------------------------------------------------------------
491 *
492 * Tcl_ListObjIndex --
493 *
494 * This procedure returns a pointer to the index'th object from the
495 * list referenced by listPtr. The first element has index 0. If index
496 * is negative or greater than or equal to the number of elements in
497 * the list, a NULL is returned. If listPtr is not a list object, an
498 * attempt will be made to convert it to a list.
499 *
500 * Results:
501 * The return value is normally TCL_OK; in this case objPtrPtr is set
502 * to the Tcl_Obj pointer for the index'th list element or NULL if
503 * index is out of range. This object should be treated as readonly and
504 * its ref count is _not_ incremented; the caller must do that if it
505 * holds on to the reference. If listPtr does not refer to a list and
506 * can't be converted to one, TCL_ERROR is returned and an error
507 * message is left in the interpreter's result if interp is not NULL.
508 *
509 * Side effects:
510 * listPtr will be converted, if necessary, to a list object.
511 *
512 *----------------------------------------------------------------------
513 */
514
515 int
516 Tcl_ListObjIndex(interp, listPtr, index, objPtrPtr)
517 Tcl_Interp *interp; /* Used to report errors if not NULL. */
518 register Tcl_Obj *listPtr; /* List object to index into. */
519 register int index; /* Index of element to return. */
520 Tcl_Obj **objPtrPtr; /* The resulting Tcl_Obj* is stored here. */
521 {
522 register List *listRepPtr;
523
524 if (listPtr->typePtr != &tclListType) {
525 int result = SetListFromAny(interp, listPtr);
526 if (result != TCL_OK) {
527 return result;
528 }
529 }
530
531 listRepPtr = (List *) listPtr->internalRep.twoPtrValue.ptr1;
532 if ((index < 0) || (index >= listRepPtr->elemCount)) {
533 *objPtrPtr = NULL;
534 } else {
535 *objPtrPtr = listRepPtr->elements[index];
536 }
537
538 return TCL_OK;
539 }
540
541 /*
542 *----------------------------------------------------------------------
543 *
544 * Tcl_ListObjLength --
545 *
546 * This procedure returns the number of elements in a list object. If
547 * the object is not already a list object, an attempt will be made to
548 * convert it to one.
549 *
550 * Results:
551 * The return value is normally TCL_OK; in this case *intPtr will be
552 * set to the integer count of list elements. If listPtr does not refer
553 * to a list object and the object can not be converted to one,
554 * TCL_ERROR is returned and an error message will be left in
555 * the interpreter's result if interp is not NULL.
556 *
557 * Side effects:
558 * The possible conversion of the argument object to a list object.
559 *
560 *----------------------------------------------------------------------
561 */
562
563 int
564 Tcl_ListObjLength(interp, listPtr, intPtr)
565 Tcl_Interp *interp; /* Used to report errors if not NULL. */
566 register Tcl_Obj *listPtr; /* List object whose #elements to return. */
567 register int *intPtr; /* The resulting int is stored here. */
568 {
569 register List *listRepPtr;
570
571 if (listPtr->typePtr != &tclListType) {
572 int result = SetListFromAny(interp, listPtr);
573 if (result != TCL_OK) {
574 return result;
575 }
576 }
577
578 listRepPtr = (List *) listPtr->internalRep.twoPtrValue.ptr1;
579 *intPtr = listRepPtr->elemCount;
580 return TCL_OK;
581 }
582
583 /*
584 *----------------------------------------------------------------------
585 *
586 * Tcl_ListObjReplace --
587 *
588 * This procedure replaces zero or more elements of the list referenced
589 * by listPtr with the objects from an (objc,objv) array.
590 * The objc elements of the array referenced by objv replace the
591 * count elements in listPtr starting at first.
592 *
593 * If the argument first is zero or negative, it refers to the first
594 * element. If first is greater than or equal to the number of elements
595 * in the list, then no elements are deleted; the new elements are
596 * appended to the list. Count gives the number of elements to
597 * replace. If count is zero or negative then no elements are deleted;
598 * the new elements are simply inserted before first.
599 *
600 * The argument objv refers to an array of objc pointers to the new
601 * elements to be added to listPtr in place of those that were
602 * deleted. If objv is NULL, no new elements are added. If listPtr is
603 * not a list object, an attempt will be made to convert it to one.
604 *
605 * Results:
606 * The return value is normally TCL_OK. If listPtr does
607 * not refer to a list object and can not be converted to one,
608 * TCL_ERROR is returned and an error message will be left in
609 * the interpreter's result if interp is not NULL.
610 *
611 * Side effects:
612 * The ref counts of the objc elements in objv are incremented since
613 * the resulting list now refers to them. Similarly, the ref counts for
614 * replaced objects are decremented. listPtr is converted, if
615 * necessary, to a list object. listPtr's old string representation, if
616 * any, is freed.
617 *
618 *----------------------------------------------------------------------
619 */
620
621 int
622 Tcl_ListObjReplace(interp, listPtr, first, count, objc, objv)
623 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
624 Tcl_Obj *listPtr; /* List object whose elements to replace. */
625 int first; /* Index of first element to replace. */
626 int count; /* Number of elements to replace. */
627 int objc; /* Number of objects to insert. */
628 Tcl_Obj *CONST objv[]; /* An array of objc pointers to Tcl objects
629 * to insert. */
630 {
631 List *listRepPtr;
632 register Tcl_Obj **elemPtrs, **newPtrs;
633 Tcl_Obj *victimPtr;
634 int numElems, numRequired, numAfterLast;
635 int start, shift, newMax, i, j, result;
636
637 if (Tcl_IsShared(listPtr)) {
638 panic("Tcl_ListObjReplace called with shared object");
639 }
640 if (listPtr->typePtr != &tclListType) {
641 result = SetListFromAny(interp, listPtr);
642 if (result != TCL_OK) {
643 return result;
644 }
645 }
646 listRepPtr = (List *) listPtr->internalRep.twoPtrValue.ptr1;
647 elemPtrs = listRepPtr->elements;
648 numElems = listRepPtr->elemCount;
649
650 if (first < 0) {
651 first = 0;
652 }
653 if (first >= numElems) {
654 first = numElems; /* so we'll insert after last element */
655 }
656 if (count < 0) {
657 count = 0;
658 }
659
660 numRequired = (numElems - count + objc);
661 if (numRequired <= listRepPtr->maxElemCount) {
662 /*
663 * Enough room in the current array. First "delete" count
664 * elements starting at first.
665 */
666
667 for (i = 0, j = first; i < count; i++, j++) {
668 victimPtr = elemPtrs[j];
669 TclDecrRefCount(victimPtr);
670 }
671
672 /*
673 * Shift the elements after the last one removed to their
674 * new locations.
675 */
676
677 start = (first + count);
678 numAfterLast = (numElems - start);
679 shift = (objc - count); /* numNewElems - numDeleted */
680 if ((numAfterLast > 0) && (shift != 0)) {
681 Tcl_Obj **src, **dst;
682
683 if (shift < 0) {
684 for (src = elemPtrs + start, dst = src + shift;
685 numAfterLast > 0; numAfterLast--, src++, dst++) {
686 *dst = *src;
687 }
688 } else {
689 for (src = elemPtrs + numElems - 1, dst = src + shift;
690 numAfterLast > 0; numAfterLast--, src--, dst--) {
691 *dst = *src;
692 }
693 }
694 }
695
696 /*
697 * Insert the new elements into elemPtrs before "first".
698 */
699
700 for (i = 0, j = first; i < objc; i++, j++) {
701 elemPtrs[j] = objv[i];
702 Tcl_IncrRefCount(objv[i]);
703 }
704
705 /*
706 * Update the count of elements.
707 */
708
709 listRepPtr->elemCount = numRequired;
710 } else {
711 /*
712 * Not enough room in the current array. Allocate a larger array and
713 * insert elements into it.
714 */
715
716 newMax = (2 * numRequired);
717 newPtrs = (Tcl_Obj **)
718 ckalloc((unsigned) (newMax * sizeof(Tcl_Obj *)));
719
720 /*
721 * Copy over the elements before "first".
722 */
723
724 if (first > 0) {
725 memcpy((VOID *) newPtrs, (VOID *) elemPtrs,
726 (size_t) (first * sizeof(Tcl_Obj *)));
727 }
728
729 /*
730 * "Delete" count elements starting at first.
731 */
732
733 for (i = 0, j = first; i < count; i++, j++) {
734 victimPtr = elemPtrs[j];
735 TclDecrRefCount(victimPtr);
736 }
737
738 /*
739 * Copy the elements after the last one removed, shifted to
740 * their new locations.
741 */
742
743 start = (first + count);
744 numAfterLast = (numElems - start);
745 if (numAfterLast > 0) {
746 memcpy((VOID *) &(newPtrs[first + objc]),
747 (VOID *) &(elemPtrs[start]),
748 (size_t) (numAfterLast * sizeof(Tcl_Obj *)));
749 }
750
751 /*
752 * Insert the new elements before "first" and update the
753 * count of elements.
754 */
755
756 for (i = 0, j = first; i < objc; i++, j++) {
757 newPtrs[j] = objv[i];
758 Tcl_IncrRefCount(objv[i]);
759 }
760
761 listRepPtr->elemCount = numRequired;
762 listRepPtr->maxElemCount = newMax;
763 listRepPtr->elements = newPtrs;
764 ckfree((char *) elemPtrs);
765 }
766
767 /*
768 * Invalidate and free any old string representation since it no longer
769 * reflects the list's internal representation.
770 */
771
772 Tcl_InvalidateStringRep(listPtr);
773 return TCL_OK;
774 }
775
776 /*
777 *----------------------------------------------------------------------
778 *
779 * TclLsetList --
780 *
781 * Core of the 'lset' command when objc == 4. Objv[2] may be
782 * either a scalar index or a list of indices.
783 *
784 * Results:
785 * Returns the new value of the list variable, or NULL if an
786 * error occurs.
787 *
788 * Side effects:
789 * Surgery is performed on the list value to produce the
790 * result.
791 *
792 * On entry, the reference count of the variable value does not reflect
793 * any references held on the stack. The first action of this function
794 * is to determine whether the object is shared, and to duplicate it if
795 * it is. The reference count of the duplicate is incremented.
796 * At this point, the reference count will be 1 for either case, so that
797 * the object will appear to be unshared.
798 *
799 * If an error occurs, and the object has been duplicated, the reference
800 * count on the duplicate is decremented so that it is now 0: this dismisses
801 * any memory that was allocated by this procedure.
802 *
803 * If no error occurs, the reference count of the original object is
804 * incremented if the object has not been duplicated, and nothing is
805 * done to a reference count of the duplicate. Now the reference count
806 * of an unduplicated object is 2 (the returned pointer, plus the one
807 * stored in the variable). The reference count of a duplicate object
808 * is 1, reflecting that the returned pointer is the only active
809 * reference. The caller is expected to store the returned value back
810 * in the variable and decrement its reference count. (INST_STORE_*
811 * does exactly this.)
812 *
813 * Tcl_LsetFlat and related functions maintain a linked list of
814 * Tcl_Obj's whose string representations must be spoilt by threading
815 * via 'ptr2' of the two-pointer internal representation. On entry
816 * to Tcl_LsetList, the values of 'ptr2' are immaterial; on exit,
817 * the 'ptr2' field of any Tcl_Obj that has been modified is set to
818 * NULL.
819 *
820 *----------------------------------------------------------------------
821 */
822
823 Tcl_Obj*
824 TclLsetList( interp, listPtr, indexArgPtr, valuePtr )
825 Tcl_Interp* interp; /* Tcl interpreter */
826 Tcl_Obj* listPtr; /* Pointer to the list being modified */
827 Tcl_Obj* indexArgPtr; /* Index or index-list arg to 'lset' */
828 Tcl_Obj* valuePtr; /* Value arg to 'lset' */
829 {
830 int indexCount; /* Number of indices in the index list */
831 Tcl_Obj** indices; /* Vector of indices in the index list*/
832
833 int duplicated; /* Flag == 1 if the obj has been
834 * duplicated, 0 otherwise */
835 Tcl_Obj* retValuePtr; /* Pointer to the list to be returned */
836 int index; /* Current index in the list - discarded */
837 int result; /* Status return from library calls */
838 Tcl_Obj* subListPtr; /* Pointer to the current sublist */
839 int elemCount; /* Count of elements in the current sublist */
840 Tcl_Obj** elemPtrs; /* Pointers to elements of current sublist */
841 Tcl_Obj* chainPtr; /* Pointer to the enclosing sublist
842 * of the current sublist */
843 int i;
844
845
846 /*
847 * Determine whether the index arg designates a list or a single
848 * index. We have to be careful about the order of the checks to
849 * avoid repeated shimmering; see TIP #22 and #23 for details.
850 */
851
852 if ( indexArgPtr->typePtr != &tclListType
853 && TclGetIntForIndex( NULL, indexArgPtr, 0, &index ) == TCL_OK ) {
854
855 /*
856 * indexArgPtr designates a single index.
857 */
858
859 return TclLsetFlat( interp, listPtr, 1, &indexArgPtr, valuePtr );
860
861 } else if ( Tcl_ListObjGetElements( NULL, indexArgPtr,
862 &indexCount, &indices ) != TCL_OK ) {
863
864 /*
865 * indexArgPtr designates something that is neither an index nor a
866 * well formed list. Report the error via TclLsetFlat.
867 */
868
869 return TclLsetFlat( interp, listPtr, 1, &indexArgPtr, valuePtr );
870
871 }
872
873 /*
874 * At this point, we know that argPtr designates a well formed list,
875 * and the 'else if' above has parsed it into indexCount and indices.
876 * If there are no indices, simply return 'valuePtr', counting the
877 * returned pointer as a reference.
878 */
879
880 if ( indexCount == 0 ) {
881 Tcl_IncrRefCount( valuePtr );
882 return valuePtr;
883 }
884
885 /*
886 * Duplicate the list arg if necessary.
887 */
888
889 if ( Tcl_IsShared( listPtr ) ) {
890 duplicated = 1;
891 listPtr = Tcl_DuplicateObj( listPtr );
892 Tcl_IncrRefCount( listPtr );
893 } else {
894 duplicated = 0;
895 }
896
897 /*
898 * It would be tempting simply to go off to TclLsetFlat to finish the
899 * processing. Alas, it is also incorrect! The problem is that
900 * 'indexArgPtr' may designate a sublist of 'listPtr' whose value
901 * is to be manipulated. The fact that 'listPtr' is itself unshared
902 * does not guarantee that no sublist is. Therefore, it's necessary
903 * to replicate all the work here, expanding the index list on each
904 * trip through the loop.
905 */
906
907 /*
908 * Anchor the linked list of Tcl_Obj's whose string reps must be
909 * invalidated if the operation succeeds.
910 */
911
912 retValuePtr = listPtr;
913 chainPtr = NULL;
914
915 /*
916 * Handle each index arg by diving into the appropriate sublist
917 */
918
919 for ( i = 0; ; ++i ) {
920
921 /*
922 * Take the sublist apart.
923 */
924
925 result = Tcl_ListObjGetElements( interp, listPtr,
926 &elemCount, &elemPtrs );
927 if ( result != TCL_OK ) {
928 break;
929 }
930 listPtr->internalRep.twoPtrValue.ptr2 = (VOID *) chainPtr;
931
932 /*
933 * Reconstitute the index array
934 */
935
936 result = Tcl_ListObjGetElements( interp, indexArgPtr,
937 &indexCount, &indices );
938 if ( result != TCL_OK ) {
939 /*
940 * Shouldn't be able to get here, because we already
941 * parsed the thing successfully once.
942 */
943 break;
944 }
945
946 /*
947 * Determine the index of the requested element.
948 */
949
950 result = TclGetIntForIndex( interp, indices[ i ],
951 (elemCount - 1), &index );
952 if ( result != TCL_OK ) {
953 break;
954 }
955
956 /*
957 * Check that the index is in range.
958 */
959
960 if ( ( index < 0 ) || ( index >= elemCount ) ) {
961 Tcl_SetObjResult( interp,
962 Tcl_NewStringObj( "list index out of range",
963 -1 ) );
964 result = TCL_ERROR;
965 break;
966 }
967
968 /*
969 * Break the loop after extracting the innermost sublist
970 */
971
972 if ( i >= indexCount-1 ) {
973 result = TCL_OK;
974 break;
975 }
976
977 /*
978 * Extract the appropriate sublist, and make sure that it is unshared.
979 */
980
981 subListPtr = elemPtrs[ index ];
982 if ( Tcl_IsShared( subListPtr ) ) {
983 subListPtr = Tcl_DuplicateObj( subListPtr );
984 result = TclListObjSetElement( interp, listPtr, index,
985 subListPtr );
986 if ( result != TCL_OK ) {
987 /*
988 * We actually shouldn't be able to get here, because
989 * we've already checked everything that TclListObjSetElement
990 * checks. If we were to get here, it would result in leaking
991 * subListPtr.
992 */
993 break;
994 }
995 }
996
997 /*
998 * Chain the current sublist onto the linked list of Tcl_Obj's
999 * whose string reps must be spoilt.
1000 */
1001
1002 chainPtr = listPtr;
1003 listPtr = subListPtr;
1004
1005 }
1006
1007 /*
1008 * Store the new element into the correct slot in the innermost sublist.
1009 */
1010
1011 if ( result == TCL_OK ) {
1012 result = TclListObjSetElement( interp, listPtr, index, valuePtr );
1013 }
1014
1015 if ( result == TCL_OK ) {
1016
1017 listPtr->internalRep.twoPtrValue.ptr2 = (VOID *) chainPtr;
1018
1019 /* Spoil all the string reps */
1020
1021 while ( listPtr != NULL ) {
1022 subListPtr = (Tcl_Obj *) listPtr->internalRep.twoPtrValue.ptr2;
1023 Tcl_InvalidateStringRep( listPtr );
1024 listPtr->internalRep.twoPtrValue.ptr2 = NULL;
1025 listPtr = subListPtr;
1026 }
1027
1028 /* Return the new list if everything worked. */
1029
1030 if ( !duplicated ) {
1031 Tcl_IncrRefCount( retValuePtr );
1032 }
1033 return retValuePtr;
1034 }
1035
1036 /* Clean up the one dangling reference otherwise */
1037
1038 if ( duplicated ) {
1039 Tcl_DecrRefCount( retValuePtr );
1040 }
1041 return NULL;
1042
1043 }
1044
1045 /*
1046 *----------------------------------------------------------------------
1047 *
1048 * TclLsetFlat --
1049 *
1050 * Core of the 'lset' command when objc>=5. Objv[2], ... ,
1051 * objv[objc-2] contain scalar indices.
1052 *
1053 * Results:
1054 * Returns the new value of the list variable, or NULL if an
1055 * error occurs.
1056 *
1057 * Side effects:
1058 * Surgery is performed on the list value to produce the
1059 * result.
1060 *
1061 * On entry, the reference count of the variable value does not reflect
1062 * any references held on the stack. The first action of this function
1063 * is to determine whether the object is shared, and to duplicate it if
1064 * it is. The reference count of the duplicate is incremented.
1065 * At this point, the reference count will be 1 for either case, so that
1066 * the object will appear to be unshared.
1067 *
1068 * If an error occurs, and the object has been duplicated, the reference
1069 * count on the duplicate is decremented so that it is now 0: this dismisses
1070 * any memory that was allocated by this procedure.
1071 *
1072 * If no error occurs, the reference count of the original object is
1073 * incremented if the object has not been duplicated, and nothing is
1074 * done to a reference count of the duplicate. Now the reference count
1075 * of an unduplicated object is 2 (the returned pointer, plus the one
1076 * stored in the variable). The reference count of a duplicate object
1077 * is 1, reflecting that the returned pointer is the only active
1078 * reference. The caller is expected to store the returned value back
1079 * in the variable and decrement its reference count. (INST_STORE_*
1080 * does exactly this.)
1081 *
1082 * Tcl_LsetList and related functions maintain a linked list of
1083 * Tcl_Obj's whose string representations must be spoilt by threading
1084 * via 'ptr2' of the two-pointer internal representation. On entry
1085 * to Tcl_LsetList, the values of 'ptr2' are immaterial; on exit,
1086 * the 'ptr2' field of any Tcl_Obj that has been modified is set to
1087 * NULL.
1088 *
1089 *----------------------------------------------------------------------
1090 */
1091
1092 Tcl_Obj*
1093 TclLsetFlat( interp, listPtr, indexCount, indexArray, valuePtr )
1094 Tcl_Interp* interp; /* Tcl interpreter */
1095 Tcl_Obj* listPtr; /* Pointer to the list being modified */
1096 int indexCount; /* Number of index args */
1097 Tcl_Obj *CONST indexArray[];
1098 /* Index args */
1099 Tcl_Obj* valuePtr; /* Value arg to 'lset' */
1100 {
1101
1102 int duplicated; /* Flag == 1 if the obj has been
1103 * duplicated, 0 otherwise */
1104 Tcl_Obj* retValuePtr; /* Pointer to the list to be returned */
1105
1106 int elemCount; /* Length of one sublist being changed */
1107 Tcl_Obj** elemPtrs; /* Pointers to the elements of a sublist */
1108
1109 Tcl_Obj* subListPtr; /* Pointer to the current sublist */
1110
1111 int index; /* Index of the element to replace in the
1112 * current sublist */
1113 Tcl_Obj* chainPtr; /* Pointer to the enclosing list of
1114 * the current sublist. */
1115
1116 int result; /* Status return from library calls */
1117
1118
1119
1120 int i;
1121
1122 /*
1123 * If there are no indices, then simply return the new value,
1124 * counting the returned pointer as a reference
1125 */
1126
1127 if ( indexCount == 0 ) {
1128 Tcl_IncrRefCount( valuePtr );
1129 return valuePtr;
1130 }
1131
1132 /*
1133 * If the list is shared, make a private copy.
1134 */
1135
1136 if ( Tcl_IsShared( listPtr ) ) {
1137 duplicated = 1;
1138 listPtr = Tcl_DuplicateObj( listPtr );
1139 Tcl_IncrRefCount( listPtr );
1140 } else {
1141 duplicated = 0;
1142 }
1143
1144 /*
1145 * Anchor the linked list of Tcl_Obj's whose string reps must be
1146 * invalidated if the operation succeeds.
1147 */
1148
1149 retValuePtr = listPtr;
1150 chainPtr = NULL;
1151
1152 /*
1153 * Handle each index arg by diving into the appropriate sublist
1154 */
1155
1156 for ( i = 0; ; ++i ) {
1157
1158 /*
1159 * Take the sublist apart.
1160 */
1161
1162 result = Tcl_ListObjGetElements( interp, listPtr,
1163 &elemCount, &elemPtrs );
1164 if ( result != TCL_OK ) {
1165 break;
1166 }
1167 listPtr->internalRep.twoPtrValue.ptr2 = (VOID *) chainPtr;
1168
1169 /*
1170 * Determine the index of the requested element.
1171 */
1172
1173 result = TclGetIntForIndex( interp, indexArray[ i ],
1174 (elemCount - 1), &index );
1175 if ( result != TCL_OK ) {
1176 break;
1177 }
1178
1179 /*
1180 * Check that the index is in range.
1181 */
1182
1183 if ( ( index < 0 ) || ( index >= elemCount ) ) {
1184 Tcl_SetObjResult( interp,
1185 Tcl_NewStringObj( "list index out of range",
1186 -1 ) );
1187 result = TCL_ERROR;
1188 break;
1189 }
1190
1191 /*
1192 * Break the loop after extracting the innermost sublist
1193 */
1194
1195 if ( i >= indexCount-1 ) {
1196 result = TCL_OK;
1197 break;
1198 }
1199
1200 /*
1201 * Extract the appropriate sublist, and make sure that it is unshared.
1202 */
1203
1204 subListPtr = elemPtrs[ index ];
1205 if ( Tcl_IsShared( subListPtr ) ) {
1206 subListPtr = Tcl_DuplicateObj( subListPtr );
1207 result = TclListObjSetElement( interp, listPtr, index,
1208 subListPtr );
1209 if ( result != TCL_OK ) {
1210 /*
1211 * We actually shouldn't be able to get here.
1212 * If we do, it would result in leaking subListPtr,
1213 * but everything's been validated already; the error
1214 * exit from TclListObjSetElement should never happen.
1215 */
1216 break;
1217 }
1218 }
1219
1220 /*
1221 * Chain the current sublist onto the linked list of Tcl_Obj's
1222 * whose string reps must be spoilt.
1223 */
1224
1225 chainPtr = listPtr;
1226 listPtr = subListPtr;
1227
1228 }
1229
1230 /* Store the result in the list element */
1231
1232 if ( result == TCL_OK ) {
1233 result = TclListObjSetElement( interp, listPtr, index, valuePtr );
1234 }
1235
1236 if ( result == TCL_OK ) {
1237
1238 listPtr->internalRep.twoPtrValue.ptr2 = (VOID *) chainPtr;
1239
1240 /* Spoil all the string reps */
1241
1242 while ( listPtr != NULL ) {
1243 subListPtr = (Tcl_Obj *) listPtr->internalRep.twoPtrValue.ptr2;
1244 Tcl_InvalidateStringRep( listPtr );
1245 listPtr->internalRep.twoPtrValue.ptr2 = NULL;
1246 listPtr = subListPtr;
1247 }
1248
1249 /* Return the new list if everything worked. */
1250
1251 if ( !duplicated ) {
1252 Tcl_IncrRefCount( retValuePtr );
1253 }
1254 return retValuePtr;
1255 }
1256
1257 /* Clean up the one dangling reference otherwise */
1258
1259 if ( duplicated ) {
1260 Tcl_DecrRefCount( retValuePtr );
1261 }
1262 return NULL;
1263
1264 }
1265
1266 /*
1267 *----------------------------------------------------------------------
1268 *
1269 * TclListObjSetElement --
1270 *
1271 * Set a single element of a list to a specified value
1272 *
1273 * Results:
1274 *
1275 * The return value is normally TCL_OK. If listPtr does not
1276 * refer to a list object and cannot be converted to one, TCL_ERROR
1277 * is returned and an error message will be left in the interpreter
1278 * result if interp is not NULL. Similarly, if index designates
1279 * an element outside the range [0..listLength-1], where
1280 * listLength is the count of elements in the list object designated
1281 * by listPtr, TCL_ERROR is returned and an error message is left
1282 * in the interpreter result.
1283 *
1284 * Side effects:
1285 *
1286 * Panics if listPtr designates a shared object. Otherwise, attempts
1287 * to convert it to a list. Decrements the ref count of the object
1288 * at the specified index within the list, replaces with the
1289 * object designated by valuePtr, and increments the ref count
1290 * of the replacement object.
1291 *
1292 * It is the caller's responsibility to invalidate the string
1293 * representation of the object.
1294 *
1295 *----------------------------------------------------------------------
1296 */
1297
1298 int
1299 TclListObjSetElement( interp, listPtr, index, valuePtr )
1300 Tcl_Interp* interp; /* Tcl interpreter; used for error reporting
1301 * if not NULL */
1302 Tcl_Obj* listPtr; /* List object in which element should be
1303 * stored */
1304 int index; /* Index of element to store */
1305 Tcl_Obj* valuePtr; /* Tcl object to store in the designated
1306 * list element */
1307 {
1308 int result; /* Return value from this function */
1309 List* listRepPtr; /* Internal representation of the list
1310 * being modified */
1311 Tcl_Obj** elemPtrs; /* Pointers to elements of the list */
1312 int elemCount; /* Number of elements in the list */
1313
1314 /* Ensure that the listPtr parameter designates an unshared list */
1315
1316 if ( Tcl_IsShared( listPtr ) ) {
1317 panic( "Tcl_ListObjSetElement called with shared object" );
1318 }
1319 if ( listPtr->typePtr != &tclListType ) {
1320 result = SetListFromAny( interp, listPtr );
1321 if ( result != TCL_OK ) {
1322 return result;
1323 }
1324 }
1325 listRepPtr = (List*) listPtr->internalRep.twoPtrValue.ptr1;
1326 elemPtrs = listRepPtr->elements;
1327 elemCount = listRepPtr->elemCount;
1328
1329 /* Ensure that the index is in bounds */
1330
1331 if ( index < 0 || index >= elemCount ) {
1332 if ( interp != NULL ) {
1333 Tcl_SetObjResult( interp,
1334 Tcl_NewStringObj( "list index out of range",
1335 -1 ) );
1336 return TCL_ERROR;
1337 }
1338 }
1339
1340 /* Add a reference to the new list element */
1341
1342 Tcl_IncrRefCount( valuePtr );
1343
1344 /* Remove a reference from the old list element */
1345
1346 Tcl_DecrRefCount( elemPtrs[ index ] );
1347
1348 /* Stash the new object in the list */
1349
1350 elemPtrs[ index ] = valuePtr;
1351
1352 return TCL_OK;
1353
1354 }
1355
1356 /*
1357 *----------------------------------------------------------------------
1358 *
1359 * FreeListInternalRep --
1360 *
1361 * Deallocate the storage associated with a list object's internal
1362 * representation.
1363 *
1364 * Results:
1365 * None.
1366 *
1367 * Side effects:
1368 * Frees listPtr's List* internal representation and sets listPtr's
1369 * internalRep.twoPtrValue.ptr1 to NULL. Decrements the ref counts
1370 * of all element objects, which may free them.
1371 *
1372 *----------------------------------------------------------------------
1373 */
1374
1375 static void
1376 FreeListInternalRep(listPtr)
1377 Tcl_Obj *listPtr; /* List object with internal rep to free. */
1378 {
1379 register List *listRepPtr = (List *) listPtr->internalRep.twoPtrValue.ptr1;
1380 register Tcl_Obj **elemPtrs = listRepPtr->elements;
1381 register Tcl_Obj *objPtr;
1382 int numElems = listRepPtr->elemCount;
1383 int i;
1384
1385 for (i = 0; i < numElems; i++) {
1386 objPtr = elemPtrs[i];
1387 Tcl_DecrRefCount(objPtr);
1388 }
1389 ckfree((char *) elemPtrs);
1390 ckfree((char *) listRepPtr);
1391
1392 listPtr->internalRep.twoPtrValue.ptr1 = NULL;
1393 listPtr->internalRep.twoPtrValue.ptr2 = NULL;
1394 }
1395
1396 /*
1397 *----------------------------------------------------------------------
1398 *
1399 * DupListInternalRep --
1400 *
1401 * Initialize the internal representation of a list Tcl_Obj to a
1402 * copy of the internal representation of an existing list object.
1403 *
1404 * Results:
1405 * None.
1406 *
1407 * Side effects:
1408 * "srcPtr"s list internal rep pointer should not be NULL and we assume
1409 * it is not NULL. We set "copyPtr"s internal rep to a pointer to a
1410 * newly allocated List structure that, in turn, points to "srcPtr"s
1411 * element objects. Those element objects are not actually copied but
1412 * are shared between "srcPtr" and "copyPtr". The ref count of each
1413 * element object is incremented.
1414 *
1415 *----------------------------------------------------------------------
1416 */
1417
1418 static void
1419 DupListInternalRep(srcPtr, copyPtr)
1420 Tcl_Obj *srcPtr; /* Object with internal rep to copy. */
1421 Tcl_Obj *copyPtr; /* Object with internal rep to set. */
1422 {
1423 List *srcListRepPtr = (List *) srcPtr->internalRep.twoPtrValue.ptr1;
1424 int numElems = srcListRepPtr->elemCount;
1425 int maxElems = srcListRepPtr->maxElemCount;
1426 register Tcl_Obj **srcElemPtrs = srcListRepPtr->elements;
1427 register Tcl_Obj **copyElemPtrs;
1428 register List *copyListRepPtr;
1429 int i;
1430
1431 /*
1432 * Allocate a new List structure that points to "srcPtr"s element
1433 * objects. Increment the ref counts for those (now shared) element
1434 * objects.
1435 */
1436
1437 copyElemPtrs = (Tcl_Obj **)
1438 ckalloc((unsigned) maxElems * sizeof(Tcl_Obj *));
1439 for (i = 0; i < numElems; i++) {
1440 copyElemPtrs[i] = srcElemPtrs[i];
1441 Tcl_IncrRefCount(copyElemPtrs[i]);
1442 }
1443
1444 copyListRepPtr = (List *) ckalloc(sizeof(List));
1445 copyListRepPtr->maxElemCount = maxElems;
1446 copyListRepPtr->elemCount = numElems;
1447 copyListRepPtr->elements = copyElemPtrs;
1448
1449 copyPtr->internalRep.twoPtrValue.ptr1 = (VOID *) copyListRepPtr;
1450 copyPtr->internalRep.twoPtrValue.ptr2 = NULL;
1451 copyPtr->typePtr = &tclListType;
1452 }
1453
1454 /*
1455 *----------------------------------------------------------------------
1456 *
1457 * SetListFromAny --
1458 *
1459 * Attempt to generate a list internal form for the Tcl object
1460 * "objPtr".
1461 *
1462 * Results:
1463 * The return value is TCL_OK or TCL_ERROR. If an error occurs during
1464 * conversion, an error message is left in the interpreter's result
1465 * unless "interp" is NULL.
1466 *
1467 * Side effects:
1468 * If no error occurs, a list is stored as "objPtr"s internal
1469 * representation.
1470 *
1471 *----------------------------------------------------------------------
1472 */
1473
1474 static int
1475 SetListFromAny(interp, objPtr)
1476 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1477 Tcl_Obj *objPtr; /* The object to convert. */
1478 {
1479 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
1480 char *string, *s;
1481 CONST char *elemStart, *nextElem;
1482 int lenRemain, length, estCount, elemSize, hasBrace, i, j, result;
1483 char *limit; /* Points just after string's last byte. */
1484 register CONST char *p;
1485 register Tcl_Obj **elemPtrs;
1486 register Tcl_Obj *elemPtr;
1487 List *listRepPtr;
1488
1489 /*
1490 * Get the string representation. Make it up-to-date if necessary.
1491 */
1492
1493 string = Tcl_GetStringFromObj(objPtr, &length);
1494
1495 /*
1496 * Parse the string into separate string objects, and create a List
1497 * structure that points to the element string objects. We use a
1498 * modified version of Tcl_SplitList's implementation to avoid one
1499 * malloc and a string copy for each list element. First, estimate the
1500 * number of elements by counting the number of space characters in the
1501 * list.
1502 */
1503
1504 limit = (string + length);
1505 estCount = 1;
1506 for (p = string; p < limit; p++) {
1507 if (isspace(UCHAR(*p))) { /* INTL: ISO space. */
1508 estCount++;
1509 }
1510 }
1511
1512 /*
1513 * Allocate a new List structure with enough room for "estCount"
1514 * elements. Each element is a pointer to a Tcl_Obj with the appropriate
1515 * string rep. The initial "estCount" elements are set using the
1516 * corresponding "argv" strings.
1517 */
1518
1519 elemPtrs = (Tcl_Obj **)
1520 ckalloc((unsigned) (estCount * sizeof(Tcl_Obj *)));
1521 for (p = string, lenRemain = length, i = 0;
1522 lenRemain > 0;
1523 p = nextElem, lenRemain = (limit - nextElem), i++) {
1524 result = TclFindElement(interp, p, lenRemain, &elemStart, &nextElem,
1525 &elemSize, &hasBrace);
1526 if (result != TCL_OK) {
1527 for (j = 0; j < i; j++) {
1528 elemPtr = elemPtrs[j];
1529 Tcl_DecrRefCount(elemPtr);
1530 }
1531 ckfree((char *) elemPtrs);
1532 return result;
1533 }
1534 if (elemStart >= limit) {
1535 break;
1536 }
1537 if (i > estCount) {
1538 panic("SetListFromAny: bad size estimate for list");
1539 }
1540
1541 /*
1542 * Allocate a Tcl object for the element and initialize it from the
1543 * "elemSize" bytes starting at "elemStart".
1544 */
1545
1546 s = ckalloc((unsigned) elemSize + 1);
1547 if (hasBrace) {
1548 memcpy((VOID *) s, (VOID *) elemStart, (size_t) elemSize);
1549 s[elemSize] = 0;
1550 } else {
1551 elemSize = TclCopyAndCollapse(elemSize, elemStart, s);
1552 }
1553
1554 TclNewObj(elemPtr);
1555 elemPtr->bytes = s;
1556 elemPtr->length = elemSize;
1557 elemPtrs[i] = elemPtr;
1558 Tcl_IncrRefCount(elemPtr); /* since list now holds ref to it */
1559 }
1560
1561 listRepPtr = (List *) ckalloc(sizeof(List));
1562 listRepPtr->maxElemCount = estCount;
1563 listRepPtr->elemCount = i;
1564 listRepPtr->elements = elemPtrs;
1565
1566 /*
1567 * Free the old internalRep before setting the new one. We do this as
1568 * late as possible to allow the conversion code, in particular
1569 * Tcl_GetStringFromObj, to use that old internalRep.
1570 */
1571
1572 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
1573 oldTypePtr->freeIntRepProc(objPtr);
1574 }
1575
1576 objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) listRepPtr;
1577 objPtr->internalRep.twoPtrValue.ptr2 = NULL;
1578 objPtr->typePtr = &tclListType;
1579 return TCL_OK;
1580 }
1581
1582 /*
1583 *----------------------------------------------------------------------
1584 *
1585 * UpdateStringOfList --
1586 *
1587 * Update the string representation for a list object.
1588 * Note: This procedure does not invalidate an existing old string rep
1589 * so storage will be lost if this has not already been done.
1590 *
1591 * Results:
1592 * None.
1593 *
1594 * Side effects:
1595 * The object's string is set to a valid string that results from
1596 * the list-to-string conversion. This string will be empty if the
1597 * list has no elements. The list internal representation
1598 * should not be NULL and we assume it is not NULL.
1599 *
1600 *----------------------------------------------------------------------
1601 */
1602
1603 static void
1604 UpdateStringOfList(listPtr)
1605 Tcl_Obj *listPtr; /* List object with string rep to update. */
1606 {
1607 # define LOCAL_SIZE 20
1608 int localFlags[LOCAL_SIZE], *flagPtr;
1609 List *listRepPtr = (List *) listPtr->internalRep.twoPtrValue.ptr1;
1610 int numElems = listRepPtr->elemCount;
1611 register int i;
1612 char *elem, *dst;
1613 int length;
1614
1615 /*
1616 * Convert each element of the list to string form and then convert it
1617 * to proper list element form, adding it to the result buffer.
1618 */
1619
1620 /*
1621 * Pass 1: estimate space, gather flags.
1622 */
1623
1624 if (numElems <= LOCAL_SIZE) {
1625 flagPtr = localFlags;
1626 } else {
1627 flagPtr = (int *) ckalloc((unsigned) numElems*sizeof(int));
1628 }
1629 listPtr->length = 1;
1630 for (i = 0; i < numElems; i++) {
1631 elem = Tcl_GetStringFromObj(listRepPtr->elements[i], &length);
1632 listPtr->length += Tcl_ScanCountedElement(elem, length,
1633 &flagPtr[i]) + 1;
1634 }
1635
1636 /*
1637 * Pass 2: copy into string rep buffer.
1638 */
1639
1640 listPtr->bytes = ckalloc((unsigned) listPtr->length);
1641 dst = listPtr->bytes;
1642 for (i = 0; i < numElems; i++) {
1643 elem = Tcl_GetStringFromObj(listRepPtr->elements[i], &length);
1644 dst += Tcl_ConvertCountedElement(elem, length, dst, flagPtr[i]);
1645 *dst = ' ';
1646 dst++;
1647 }
1648 if (flagPtr != localFlags) {
1649 ckfree((char *) flagPtr);
1650 }
1651 if (dst == listPtr->bytes) {
1652 *dst = 0;
1653 } else {
1654 dst--;
1655 *dst = 0;
1656 }
1657 listPtr->length = dst - listPtr->bytes;
1658 }