Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/ListObj.3 @ 43

Last change on this file since 43 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 10.2 KB
Line 
1'\"
2'\" Copyright (c) 1996-1997 Sun Microsystems, Inc.
3'\"
4'\" See the file "license.terms" for information on usage and redistribution
5'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6'\"
7'\" RCS: @(#) $Id: ListObj.3,v 1.12 2007/12/13 15:22:31 dgp Exp $
8'\"
9.so man.macros
10.TH Tcl_ListObj 3 8.0 Tcl "Tcl Library Procedures"
11.BS
12.SH NAME
13Tcl_ListObjAppendList, Tcl_ListObjAppendElement, Tcl_NewListObj, Tcl_SetListObj, Tcl_ListObjGetElements, Tcl_ListObjLength, Tcl_ListObjIndex, Tcl_ListObjReplace \- manipulate Tcl objects as lists
14.SH SYNOPSIS
15.nf
16\fB#include <tcl.h>\fR
17.sp
18int
19\fBTcl_ListObjAppendList\fR(\fIinterp, listPtr, elemListPtr\fR)
20.sp
21int
22\fBTcl_ListObjAppendElement\fR(\fIinterp, listPtr, objPtr\fR)
23.sp
24Tcl_Obj *
25\fBTcl_NewListObj\fR(\fIobjc, objv\fR)
26.sp
27\fBTcl_SetListObj\fR(\fIobjPtr, objc, objv\fR)
28.sp
29int
30\fBTcl_ListObjGetElements\fR(\fIinterp, listPtr, objcPtr, objvPtr\fR)
31.sp
32int
33\fBTcl_ListObjLength\fR(\fIinterp, listPtr, intPtr\fR)
34.sp
35int
36\fBTcl_ListObjIndex\fR(\fIinterp, listPtr, index, objPtrPtr\fR)
37.sp
38int
39\fBTcl_ListObjReplace\fR(\fIinterp, listPtr, first, count, objc, objv\fR)
40.SH ARGUMENTS
41.AS "Tcl_Obj *const" *elemListPtr in/out
42.AP Tcl_Interp *interp in
43If an error occurs while converting an object to be a list object,
44an error message is left in the interpreter's result object
45unless \fIinterp\fR is NULL.
46.AP Tcl_Obj *listPtr in/out
47Points to the list object to be manipulated.
48If \fIlistPtr\fR does not already point to a list object,
49an attempt will be made to convert it to one.
50.AP Tcl_Obj *elemListPtr in/out
51For \fBTcl_ListObjAppendList\fR, this points to a list object
52containing elements to be appended onto \fIlistPtr\fR.
53Each element of *\fIelemListPtr\fR will
54become a new element of \fIlistPtr\fR.
55If *\fIelemListPtr\fR is not NULL and
56does not already point to a list object,
57an attempt will be made to convert it to one.
58.AP Tcl_Obj *objPtr in
59For \fBTcl_ListObjAppendElement\fR,
60points to the Tcl object that will be appended to \fIlistPtr\fR.
61For \fBTcl_SetListObj\fR,
62this points to the Tcl object that will be converted to a list object
63containing the \fIobjc\fR elements of the array referenced by \fIobjv\fR.
64.AP int *objcPtr in
65Points to location where \fBTcl_ListObjGetElements\fR
66stores the number of element objects in \fIlistPtr\fR.
67.AP Tcl_Obj ***objvPtr out
68A location where \fBTcl_ListObjGetElements\fR stores a pointer to an array
69of pointers to the element objects of \fIlistPtr\fR. 
70.AP int objc in
71The number of Tcl objects that \fBTcl_NewListObj\fR
72will insert into a new list object,
73and \fBTcl_ListObjReplace\fR will insert into \fIlistPtr\fR.
74For \fBTcl_SetListObj\fR,
75the number of Tcl objects to insert into \fIobjPtr\fR.
76.AP "Tcl_Obj *const" objv[] in
77An array of pointers to objects.
78\fBTcl_NewListObj\fR will insert these objects into a new list object
79and \fBTcl_ListObjReplace\fR will insert them into an existing \fIlistPtr\fR.
80Each object will become a separate list element. 
81.AP int *intPtr out
82Points to location where \fBTcl_ListObjLength\fR
83stores the length of the list.
84.AP int index in
85Index of the list element that \fBTcl_ListObjIndex\fR
86is to return.
87The first element has index 0.
88.AP Tcl_Obj **objPtrPtr out
89Points to place where \fBTcl_ListObjIndex\fR is to store
90a pointer to the resulting list element object.
91.AP int first in
92Index of the starting list element that \fBTcl_ListObjReplace\fR
93is to replace.
94The list's first element has index 0.
95.AP int count in
96The number of elements that \fBTcl_ListObjReplace\fR
97is to replace.
98.BE
99
100.SH DESCRIPTION
101.PP
102Tcl list objects have an internal representation that supports
103the efficient indexing and appending.
104The procedures described in this man page are used to
105create, modify, index, and append to Tcl list objects from C code.
106.PP
107\fBTcl_ListObjAppendList\fR and \fBTcl_ListObjAppendElement\fR
108both add one or more objects
109to the end of the list object referenced by \fIlistPtr\fR.
110\fBTcl_ListObjAppendList\fR appends each element of the list object
111referenced by \fIelemListPtr\fR while
112\fBTcl_ListObjAppendElement\fR appends the single object
113referenced by \fIobjPtr\fR.
114Both procedures will convert the object referenced by \fIlistPtr\fR
115to a list object if necessary.
116If an error occurs during conversion,
117both procedures return \fBTCL_ERROR\fR and leave an error message
118in the interpreter's result object if \fIinterp\fR is not NULL.
119Similarly, if \fIelemListPtr\fR does not already refer to a list object,
120\fBTcl_ListObjAppendList\fR will attempt to convert it to one
121and if an error occurs during conversion,
122will return \fBTCL_ERROR\fR
123and leave an error message in the interpreter's result object
124if interp is not NULL.
125Both procedures invalidate any old string representation of \fIlistPtr\fR
126and, if it was converted to a list object,
127free any old internal representation.
128Similarly, \fBTcl_ListObjAppendList\fR frees any old internal representation
129of \fIelemListPtr\fR if it converts it to a list object.
130After appending each element in \fIelemListPtr\fR,
131\fBTcl_ListObjAppendList\fR increments the element's reference count
132since \fIlistPtr\fR now also refers to it.
133For the same reason, \fBTcl_ListObjAppendElement\fR
134increments \fIobjPtr\fR's reference count.
135If no error occurs,
136the two procedures return \fBTCL_OK\fR after appending the objects.
137.PP
138\fBTcl_NewListObj\fR and \fBTcl_SetListObj\fR
139create a new object or modify an existing object to hold
140the \fIobjc\fR elements of the array referenced by \fIobjv\fR
141where each element is a pointer to a Tcl object.
142If \fIobjc\fR is less than or equal to zero,
143they return an empty object.
144The new object's string representation is left invalid.
145The two procedures increment the reference counts
146of the elements in \fIobjc\fR since the list object now refers to them.
147The new list object returned by \fBTcl_NewListObj\fR
148has reference count zero.
149.PP
150\fBTcl_ListObjGetElements\fR returns a count and a pointer to an array of
151the elements in a list object.  It returns the count by storing it in the
152address \fIobjcPtr\fR.  Similarly, it returns the array pointer by storing
153it in the address \fIobjvPtr\fR.
154The memory pointed to is managed by Tcl and should not be freed or written
155to by the caller. If the list is empty, 0 is stored at \fIobjcPtr\fR
156and NULL at \fIobjvPtr\fR.
157If \fIlistPtr\fR is not already a list object, \fBTcl_ListObjGetElements\fR
158will attempt to convert it to one; if the conversion fails, it returns
159\fBTCL_ERROR\fR and leaves an error message in the interpreter's result
160object if \fIinterp\fR is not NULL.
161Otherwise it returns \fBTCL_OK\fR after storing the count and array pointer.
162.PP
163\fBTcl_ListObjLength\fR returns the number of elements in the list object
164referenced by \fIlistPtr\fR.
165It returns this count by storing an integer in the address \fIintPtr\fR.
166If the object is not already a list object,
167\fBTcl_ListObjLength\fR will attempt to convert it to one;
168if the conversion fails, it returns \fBTCL_ERROR\fR
169and leaves an error message in the interpreter's result object
170if \fIinterp\fR is not NULL.
171Otherwise it returns \fBTCL_OK\fR after storing the list's length.
172.PP
173The procedure \fBTcl_ListObjIndex\fR returns a pointer to the object
174at element \fIindex\fR in the list referenced by \fIlistPtr\fR.
175It returns this object by storing a pointer to it
176in the address \fIobjPtrPtr\fR.
177If \fIlistPtr\fR does not already refer to a list object,
178\fBTcl_ListObjIndex\fR will attempt to convert it to one;
179if the conversion fails, it returns \fBTCL_ERROR\fR
180and leaves an error message in the interpreter's result object
181if \fIinterp\fR is not NULL.
182If the index is out of range,
183that is, \fIindex\fR is negative or
184greater than or equal to the number of elements in the list,
185\fBTcl_ListObjIndex\fR stores a NULL in \fIobjPtrPtr\fR
186and returns \fBTCL_OK\fR.
187Otherwise it returns \fBTCL_OK\fR after storing the element's
188object pointer.
189The reference count for the list element is not incremented;
190the caller must do that if it needs to retain a pointer to the element.
191.PP
192\fBTcl_ListObjReplace\fR replaces zero or more elements
193of the list referenced by \fIlistPtr\fR
194with the \fIobjc\fR objects in the array referenced by \fIobjv\fR.
195If \fIlistPtr\fR does not point to a list object,
196\fBTcl_ListObjReplace\fR will attempt to convert it to one;
197if the conversion fails, it returns \fBTCL_ERROR\fR
198and leaves an error message in the interpreter's result object
199if \fIinterp\fR is not NULL.
200Otherwise, it returns \fBTCL_OK\fR after replacing the objects.
201If \fIobjv\fR is NULL, no new elements are added.
202If the argument \fIfirst\fR is zero or negative,
203it refers to the first element.
204If \fIfirst\fR is greater than or equal to the
205number of elements in the list, then no elements are deleted;
206the new elements are appended to the list.
207\fIcount\fR gives the number of elements to replace.
208If \fIcount\fR is zero or negative then no elements are deleted;
209the new elements are simply inserted before the one
210designated by \fIfirst\fR.
211\fBTcl_ListObjReplace\fR invalidates \fIlistPtr\fR's
212old string representation.
213The reference counts of any elements inserted from \fIobjv\fR
214are incremented since the resulting list now refers to them.
215Similarly, the reference counts for any replaced objects are decremented.
216.PP
217Because \fBTcl_ListObjReplace\fR combines
218both element insertion and deletion,
219it can be used to implement a number of list operations.
220For example, the following code inserts the \fIobjc\fR objects
221referenced by the array of object pointers \fIobjv\fR
222just before the element \fIindex\fR of the list referenced by \fIlistPtr\fR:
223.PP
224.CS
225result = Tcl_ListObjReplace(interp, listPtr, index, 0,
226        objc, objv);
227.CE
228.PP
229Similarly, the following code appends the \fIobjc\fR objects
230referenced by the array \fIobjv\fR
231to the end of the list \fIlistPtr\fR:
232.PP
233.CS
234result = Tcl_ListObjLength(interp, listPtr, &length);
235if (result == TCL_OK) {
236    result = Tcl_ListObjReplace(interp, listPtr, length, 0,
237            objc, objv);
238}
239.CE
240.PP
241The \fIcount\fR list elements starting at \fIfirst\fR can be deleted
242by simply calling \fBTcl_ListObjReplace\fR
243with a NULL \fIobjvPtr\fR:
244.PP
245.CS
246result = Tcl_ListObjReplace(interp, listPtr, first, count,
247        0, NULL);
248.CE
249.SH "SEE ALSO"
250Tcl_NewObj, Tcl_DecrRefCount, Tcl_IncrRefCount, Tcl_GetObjResult
251.SH KEYWORDS
252append, index, insert, internal representation, length, list, list object, list type, object, object type, replace, string representation
Note: See TracBrowser for help on using the repository browser.