1Tcl_ListObj(3) Tcl Library Procedures Tcl_ListObj(3)
2
3
4
5______________________________________________________________________________
6
8 Tcl_ListObjAppendList, Tcl_ListObjAppendElement, Tcl_NewListObj,
9 Tcl_SetListObj, Tcl_ListObjGetElements, Tcl_ListObjLength, Tcl_ListOb‐
10 jIndex, Tcl_ListObjReplace - manipulate Tcl values as lists
11
13 #include <tcl.h>
14
15 int
16 Tcl_ListObjAppendList(interp, listPtr, elemListPtr)
17
18 int
19 Tcl_ListObjAppendElement(interp, listPtr, objPtr)
20
21 Tcl_Obj *
22 Tcl_NewListObj(objc, objv)
23
24 Tcl_SetListObj(objPtr, objc, objv)
25
26 int
27 Tcl_ListObjGetElements(interp, listPtr, objcPtr, objvPtr)
28
29 int
30 Tcl_ListObjLength(interp, listPtr, intPtr)
31
32 int
33 Tcl_ListObjIndex(interp, listPtr, index, objPtrPtr)
34
35 int
36 Tcl_ListObjReplace(interp, listPtr, first, count, objc, objv)
37
39 Tcl_Interp *interp (in) If an error occurs while con‐
40 verting a value to be a list
41 value, an error message is
42 left in the interpreter's re‐
43 sult value unless interp is
44 NULL.
45
46 Tcl_Obj *listPtr (in/out) Points to the list value to
47 be manipulated. If listPtr
48 does not already point to a
49 list value, an attempt will
50 be made to convert it to one.
51
52 Tcl_Obj *elemListPtr (in/out) For Tcl_ListObjAppendList,
53 this points to a list value
54 containing elements to be ap‐
55 pended onto listPtr. Each
56 element of *elemListPtr will
57 become a new element of
58 listPtr. If *elemListPtr is
59 not NULL and does not already
60 point to a list value, an at‐
61 tempt will be made to convert
62 it to one.
63
64 Tcl_Obj *objPtr (in) For Tcl_ListObjAppendElement,
65 points to the Tcl value that
66 will be appended to listPtr.
67 For Tcl_SetListObj, this
68 points to the Tcl value that
69 will be converted to a list
70 value containing the objc el‐
71 ements of the array refer‐
72 enced by objv.
73
74 int *objcPtr (in) Points to location where
75 Tcl_ListObjGetElements stores
76 the number of element values
77 in listPtr.
78
79 Tcl_Obj ***objvPtr (out) A location where Tcl_ListOb‐
80 jGetElements stores a pointer
81 to an array of pointers to
82 the element values of
83 listPtr.
84
85 int objc (in) The number of Tcl values that
86 Tcl_NewListObj will insert
87 into a new list value, and
88 Tcl_ListObjReplace will in‐
89 sert into listPtr. For
90 Tcl_SetListObj, the number of
91 Tcl values to insert into ob‐
92 jPtr.
93
94 Tcl_Obj *const objv[] (in) An array of pointers to val‐
95 ues. Tcl_NewListObj will in‐
96 sert these values into a new
97 list value and Tcl_ListObjRe‐
98 place will insert them into
99 an existing listPtr. Each
100 value will become a separate
101 list element.
102
103 int *intPtr (out) Points to location where
104 Tcl_ListObjLength stores the
105 length of the list.
106
107 int index (in) Index of the list element
108 that Tcl_ListObjIndex is to
109 return. The first element
110 has index 0.
111
112 Tcl_Obj **objPtrPtr (out) Points to place where
113 Tcl_ListObjIndex is to store
114 a pointer to the resulting
115 list element value.
116
117 int first (in) Index of the starting list
118 element that Tcl_ListObjRe‐
119 place is to replace. The
120 list's first element has in‐
121 dex 0.
122
123 int count (in) The number of elements that
124 Tcl_ListObjReplace is to re‐
125 place.
126______________________________________________________________________________
127
128
130 Tcl list values have an internal representation that supports the effi‐
131 cient indexing and appending. The procedures described in this man
132 page are used to create, modify, index, and append to Tcl list values
133 from C code.
134
135 Tcl_ListObjAppendList and Tcl_ListObjAppendElement both add one or more
136 values to the end of the list value referenced by listPtr. Tcl_ListOb‐
137 jAppendList appends each element of the list value referenced by elem‐
138 ListPtr while Tcl_ListObjAppendElement appends the single value refer‐
139 enced by objPtr. Both procedures will convert the value referenced by
140 listPtr to a list value if necessary. If an error occurs during con‐
141 version, both procedures return TCL_ERROR and leave an error message in
142 the interpreter's result value if interp is not NULL. Similarly, if
143 elemListPtr does not already refer to a list value, Tcl_ListObjAp‐
144 pendList will attempt to convert it to one and if an error occurs dur‐
145 ing conversion, will return TCL_ERROR and leave an error message in the
146 interpreter's result value if interp is not NULL. Both procedures in‐
147 validate any old string representation of listPtr and, if it was con‐
148 verted to a list value, free any old internal representation. Simi‐
149 larly, Tcl_ListObjAppendList frees any old internal representation of
150 elemListPtr if it converts it to a list value. After appending each
151 element in elemListPtr, Tcl_ListObjAppendList increments the element's
152 reference count since listPtr now also refers to it. For the same rea‐
153 son, Tcl_ListObjAppendElement increments objPtr's reference count. If
154 no error occurs, the two procedures return TCL_OK after appending the
155 values.
156
157 Tcl_NewListObj and Tcl_SetListObj create a new value or modify an ex‐
158 isting value to hold the objc elements of the array referenced by objv
159 where each element is a pointer to a Tcl value. If objc is less than
160 or equal to zero, they return an empty value. If objv is NULL, the re‐
161 sulting list contains 0 elements, with reserved space in an internal
162 representation for objc more elements (to avoid its reallocation
163 later). The new value's string representation is left invalid. The
164 two procedures increment the reference counts of the elements in objc
165 since the list value now refers to them. The new list value returned
166 by Tcl_NewListObj has reference count zero.
167
168 Tcl_ListObjGetElements returns a count and a pointer to an array of the
169 elements in a list value. It returns the count by storing it in the
170 address objcPtr. Similarly, it returns the array pointer by storing it
171 in the address objvPtr. The memory pointed to is managed by Tcl and
172 should not be freed or written to by the caller. If the list is empty,
173 0 is stored at objcPtr and NULL at objvPtr. If listPtr is not already
174 a list value, Tcl_ListObjGetElements will attempt to convert it to one;
175 if the conversion fails, it returns TCL_ERROR and leaves an error mes‐
176 sage in the interpreter's result value if interp is not NULL. Other‐
177 wise it returns TCL_OK after storing the count and array pointer.
178
179 Tcl_ListObjLength returns the number of elements in the list value ref‐
180 erenced by listPtr. It returns this count by storing an integer in the
181 address intPtr. If the value is not already a list value, Tcl_ListOb‐
182 jLength will attempt to convert it to one; if the conversion fails, it
183 returns TCL_ERROR and leaves an error message in the interpreter's re‐
184 sult value if interp is not NULL. Otherwise it returns TCL_OK after
185 storing the list's length.
186
187 The procedure Tcl_ListObjIndex returns a pointer to the value at ele‐
188 ment index in the list referenced by listPtr. It returns this value by
189 storing a pointer to it in the address objPtrPtr. If listPtr does not
190 already refer to a list value, Tcl_ListObjIndex will attempt to convert
191 it to one; if the conversion fails, it returns TCL_ERROR and leaves an
192 error message in the interpreter's result value if interp is not NULL.
193 If the index is out of range, that is, index is negative or greater
194 than or equal to the number of elements in the list, Tcl_ListObjIndex
195 stores a NULL in objPtrPtr and returns TCL_OK. Otherwise it returns
196 TCL_OK after storing the element's value pointer. The reference count
197 for the list element is not incremented; the caller must do that if it
198 needs to retain a pointer to the element.
199
200 Tcl_ListObjReplace replaces zero or more elements of the list refer‐
201 enced by listPtr with the objc values in the array referenced by objv.
202 If listPtr does not point to a list value, Tcl_ListObjReplace will at‐
203 tempt to convert it to one; if the conversion fails, it returns TCL_ER‐
204 ROR and leaves an error message in the interpreter's result value if
205 interp is not NULL. Otherwise, it returns TCL_OK after replacing the
206 values. If objv is NULL, no new elements are added. If the argument
207 first is zero or negative, it refers to the first element. If first is
208 greater than or equal to the number of elements in the list, then no
209 elements are deleted; the new elements are appended to the list. count
210 gives the number of elements to replace. If count is zero or negative
211 then no elements are deleted; the new elements are simply inserted be‐
212 fore the one designated by first. Tcl_ListObjReplace invalidates
213 listPtr's old string representation. The reference counts of any ele‐
214 ments inserted from objv are incremented since the resulting list now
215 refers to them. Similarly, the reference counts for any replaced val‐
216 ues are decremented.
217
218 Because Tcl_ListObjReplace combines both element insertion and dele‐
219 tion, it can be used to implement a number of list operations. For ex‐
220 ample, the following code inserts the objc values referenced by the ar‐
221 ray of value pointers objv just before the element index of the list
222 referenced by listPtr:
223
224 result = Tcl_ListObjReplace(interp, listPtr, index, 0,
225 objc, objv);
226
227 Similarly, the following code appends the objc values referenced by the
228 array objv to the end of the list listPtr:
229
230 result = Tcl_ListObjLength(interp, listPtr, &length);
231 if (result == TCL_OK) {
232 result = Tcl_ListObjReplace(interp, listPtr, length, 0,
233 objc, objv);
234 }
235
236 The count list elements starting at first can be deleted by simply
237 calling Tcl_ListObjReplace with a NULL objvPtr:
238
239 result = Tcl_ListObjReplace(interp, listPtr, first, count,
240 0, NULL);
241
243 Tcl_NewObj(3), Tcl_DecrRefCount(3), Tcl_IncrRefCount(3), Tcl_GetObjRe‐
244 sult(3)
245
247 append, index, insert, internal representation, length, list, list
248 value, list type, value, value type, replace, string representation
249
250
251
252Tcl 8.0 Tcl_ListObj(3)