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 objects 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 an object to be a list
41 object, an error message is left
42 in the interpreter's result
43 object unless interp is NULL.
44
45 Tcl_Obj *listPtr (in/out) Points to the list object to be
46 manipulated. If listPtr does
47 not already point to a list
48 object, an attempt will be made
49 to convert it to one.
50
51 Tcl_Obj *elemListPtr (in/out) For Tcl_ListObjAppendList, this
52 points to a list object contain‐
53 ing elements to be appended onto
54 listPtr. Each element of *elem‐
55 ListPtr will become a new ele‐
56 ment of listPtr. If *elem‐
57 ListPtr is not NULL and does not
58 already point to a list object,
59 an attempt will be made to con‐
60 vert it to one.
61
62 Tcl_Obj *objPtr (in) For Tcl_ListObjAppendElement,
63 points to the Tcl object that
64 will be appended to listPtr.
65 For Tcl_SetListObj, this points
66 to the Tcl object that will be
67 converted to a list object con‐
68 taining the objc elements of the
69 array referenced by objv.
70
71 int *objcPtr (in) Points to location where
72 Tcl_ListObjGetElements stores
73 the number of element objects in
74 listPtr.
75
76 Tcl_Obj ***objvPtr (out) A location where Tcl_ListOb‐
77 jGetElements stores a pointer to
78 an array of pointers to the ele‐
79 ment objects of listPtr.
80
81 int objc (in) The number of Tcl objects that
82 Tcl_NewListObj will insert into
83 a new list object, and Tcl_Lis‐
84 tObjReplace will insert into
85 listPtr. For Tcl_SetListObj,
86 the number of Tcl objects to
87 insert into objPtr. │
88
89 Tcl_Obj "*CONST objv[]" in │
90 An array of pointers to objects. │
91 Tcl_NewListObj will insert these │
92 objects into a new list object │
93 and Tcl_ListObjReplace will │
94 insert them into an existing │
95 listPtr. Each object will │
96 become a separate list element.
97
98 int *intPtr (out) Points to location where
99 Tcl_ListObjLength stores the
100 length of the list.
101
102 int index (in) Index of the list element that
103 Tcl_ListObjIndex is to return.
104 The first element has index 0.
105
106 Tcl_Obj **objPtrPtr (out) Points to place where Tcl_ListO‐
107 bjIndex is to store a pointer to
108 the resulting list element
109 object.
110
111 int first (in) Index of the starting list ele‐
112 ment that Tcl_ListObjReplace is
113 to replace. The list's first
114 element has index 0.
115
116 int count (in) The number of elements that
117 Tcl_ListObjReplace is to
118 replace.
119_________________________________________________________________
120
121
123 Tcl list objects have an internal representation that supports the
124 efficient indexing and appending. The procedures described in this man
125 page are used to create, modify, index, and append to Tcl list objects
126 from C code.
127
128 Tcl_ListObjAppendList and Tcl_ListObjAppendElement both add one or more
129 objects to the end of the list object referenced by listPtr. Tcl_Lis‐
130 tObjAppendList appends each element of the list object referenced by
131 elemListPtr while Tcl_ListObjAppendElement appends the single object
132 referenced by objPtr. Both procedures will convert the object refer‐
133 enced by listPtr to a list object if necessary. If an error occurs
134 during conversion, both procedures return TCL_ERROR and leave an error
135 message in the interpreter's result object if interp is not NULL. Sim‐
136 ilarly, if elemListPtr does not already refer to a list object,
137 Tcl_ListObjAppendList will attempt to convert it to one and if an error
138 occurs during conversion, will return TCL_ERROR and leave an error mes‐
139 sage in the interpreter's result object if interp is not NULL. Both
140 procedures invalidate any old string representation of listPtr and, if
141 it was converted to a list object, free any old internal representa‐
142 tion. Similarly, Tcl_ListObjAppendList frees any old internal repre‐
143 sentation of elemListPtr if it converts it to a list object. After
144 appending each element in elemListPtr, Tcl_ListObjAppendList increments
145 the element's reference count since listPtr now also refers to it. For
146 the same reason, Tcl_ListObjAppendElement increments objPtr's reference
147 count. If no error occurs, the two procedures return TCL_OK after
148 appending the objects.
149
150 Tcl_NewListObj and Tcl_SetListObj create a new object or modify an
151 existing object to hold the objc elements of the array referenced by
152 objv where each element is a pointer to a Tcl object. If objc is less
153 than or equal to zero, they return an empty object. The new object's
154 string representation is left invalid. The two procedures increment
155 the reference counts of the elements in objc since the list object now
156 refers to them. The new list object returned by Tcl_NewListObj has
157 reference count zero.
158
159 Tcl_ListObjGetElements returns a count and a pointer to an array of the
160 elements in a list object. It returns the count by storing it in the
161 address objcPtr. Similarly, it returns the array pointer by storing it
162 in the address objvPtr. The memory pointed to is managed by Tcl and
163 should not be freed by the caller. If listPtr is not already a list
164 object, Tcl_ListObjGetElements will attempt to convert it to one; if
165 the conversion fails, it returns TCL_ERROR and leaves an error message
166 in the interpreter's result object if interp is not NULL. Otherwise it
167 returns TCL_OK after storing the count and array pointer.
168
169 Tcl_ListObjLength returns the number of elements in the list object
170 referenced by listPtr. It returns this count by storing an integer in
171 the address intPtr. If the object is not already a list object,
172 Tcl_ListObjLength will attempt to convert it to one; if the conversion
173 fails, it returns TCL_ERROR and leaves an error message in the inter‐
174 preter's result object if interp is not NULL. Otherwise it returns
175 TCL_OK after storing the list's length.
176
177 The procedure Tcl_ListObjIndex returns a pointer to the object at ele‐
178 ment index in the list referenced by listPtr. It returns this object
179 by storing a pointer to it in the address objPtrPtr. If listPtr does
180 not already refer to a list object, Tcl_ListObjIndex will attempt to
181 convert it to one; if the conversion fails, it returns TCL_ERROR and
182 leaves an error message in the interpreter's result object if interp is
183 not NULL. If the index is out of range, that is, index is negative or
184 greater than or equal to the number of elements in the list, Tcl_ListO‐
185 bjIndex stores a NULL in objPtrPtr and returns TCL_OK. Otherwise it
186 returns TCL_OK after storing the element's object pointer. The refer‐
187 ence count for the list element is not incremented; the caller must do
188 that if it needs to retain a pointer to the element.
189
190 Tcl_ListObjReplace replaces zero or more elements of the list refer‐
191 enced by listPtr with the objc objects in the array referenced by objv.
192 If listPtr does not point to a list object, Tcl_ListObjReplace will
193 attempt to convert it to one; if the conversion fails, it returns
194 TCL_ERROR and leaves an error message in the interpreter's result
195 object if interp is not NULL. Otherwise, it returns TCL_OK after
196 replacing the objects. If objv is NULL, no new elements are added. If
197 the argument first is zero or negative, it refers to the first element.
198 If first is greater than or equal to the number of elements in the
199 list, then no elements are deleted; the new elements are appended to
200 the list. count gives the number of elements to replace. If count is
201 zero or negative then no elements are deleted; the new elements are
202 simply inserted before the one designated by first. Tcl_ListObjReplace
203 invalidates listPtr's old string representation. The reference counts
204 of any elements inserted from objv are incremented since the resulting
205 list now refers to them. Similarly, the reference counts for any
206 replaced objects are decremented.
207
208 Because Tcl_ListObjReplace combines both element insertion and dele‐
209 tion, it can be used to implement a number of list operations. For
210 example, the following code inserts the objc objects referenced by the
211 array of object pointers objv just before the element index of the list
212 referenced by listPtr:
213 result = Tcl_ListObjReplace(interp, listPtr, index, 0, objc, objv);
214 Similarly, the following code appends the objc objects referenced by
215 the array objv to the end of the list listPtr:
216 result = Tcl_ListObjLength(interp, listPtr, &length);
217 if (result == TCL_OK) {
218 result = Tcl_ListObjReplace(interp, listPtr, length, 0, objc, objv);
219 }
220 The count list elements starting at first can be deleted by simply
221 calling Tcl_ListObjReplace with a NULL objvPtr:
222 result = Tcl_ListObjReplace(interp, listPtr, first, count, 0, NULL);
223
224
226 Tcl_NewObj, Tcl_DecrRefCount, Tcl_IncrRefCount, Tcl_GetObjResult
227
228
230 append, index, insert, internal representation, length, list, list
231 object, list type, object, object type, replace, string representation
232
233
234
235Tcl 8.0 Tcl_ListObj(3)