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