Tcl_ListObj (3)
manipulate Tcl objects as lists
SYNOPSIS
#include <tcl.h>
int
Tcl_ListObjAppendList(interp, listPtr, elemListPtr)
int
Tcl_ListObjAppendElement(interp, listPtr, objPtr)
Tcl_Obj *
Tcl_NewListObj(objc, objv)
Tcl_SetListObj(objPtr, objc, objv)
int
Tcl_ListObjGetElements(interp, listPtr, objcPtr, objvPtr)
int
Tcl_ListObjLength(interp, listPtr, intPtr)
int
Tcl_ListObjIndex(interp, listPtr, index, objPtrPtr)
int
Tcl_ListObjReplace(interp, listPtr, first, count, objc, objv)
ARGUMENTS
DESCRIPTION
Tcl list objects have an internal representation that supports
the efficient indexing and appending.
The procedures described in this man page are used to
create, modify, index, and append to Tcl list objects from C code.
Tcl_ListObjAppendList and Tcl_ListObjAppendElement
both add one or more objects
to the end of the list object referenced by listPtr.
Tcl_ListObjAppendList appends each element of the list object
referenced by elemListPtr while
Tcl_ListObjAppendElement appends the single object
referenced by objPtr.
Both procedures will convert the object referenced by listPtr
to a list object if necessary.
If an error occurs during conversion,
both procedures return TCL_ERROR and leave an error message
in the interpreter's result object if interp is not NULL.
Similarly, if elemListPtr does not already refer to a list object,
Tcl_ListObjAppendList will attempt to convert it to one
and if an error occurs during conversion,
will return TCL_ERROR
and leave an error message in the interpreter's result object
if interp is not NULL.
Both procedures invalidate any old string representation of listPtr
and, if it was converted to a list object,
free any old internal representation.
Similarly, Tcl_ListObjAppendList frees any old internal representation
of elemListPtr if it converts it to a list object.
After appending each element in elemListPtr,
Tcl_ListObjAppendList increments the element's reference count
since listPtr now also refers to it.
For the same reason, Tcl_ListObjAppendElement
increments objPtr's reference count.
If no error occurs,
the two procedures return TCL_OK after appending the objects.
Tcl_NewListObj and Tcl_SetListObj
create a new object or modify an existing object to hold
the objc elements of the array referenced by objv
where each element is a pointer to a Tcl object.
If objc is less than or equal to zero,
they return an empty object.
The new object's string representation is left invalid.
The two procedures increment the reference counts
of the elements in objc since the list object now refers to them.
The new list object returned by Tcl_NewListObj
has reference count zero.
Tcl_ListObjGetElements returns a count and
a pointer to an array of the elements in a list object.
It returns the count by storing it in the address objcPtr.
Similarly, it returns the array pointer by storing it
in the address objvPtr.
If listPtr is not already a list object,
Tcl_ListObjGetElements will attempt to convert it to one;
if the conversion fails, it returns TCL_ERROR
and leaves an error message in the interpreter's result object
if interp is not NULL.
Otherwise it returns TCL_OK after storing the count and array pointer.
Tcl_ListObjLength returns the number of elements in the list object
referenced by listPtr.
It returns this count by storing an integer in the address intPtr.
If the object is not already a list object,
Tcl_ListObjLength will attempt to convert it to one;
if the conversion fails, it returns TCL_ERROR
and leaves an error message in the interpreter's result object
if interp is not NULL.
Otherwise it returns TCL_OK after storing the list's length.
The procedure Tcl_ListObjIndex returns a pointer to the object
at element index in the list referenced by listPtr.
It returns this object by storing a pointer to it
in the address objPtrPtr.
If listPtr does not already refer to a list object,
Tcl_ListObjIndex will attempt to convert it to one;
if the conversion fails, it returns TCL_ERROR
and leaves an error message in the interpreter's result object
if interp is not NULL.
If the index is out of range,
that is, index is negative or
greater than or equal to the number of elements in the list,
Tcl_ListObjIndex stores a NULL in objPtrPtr
and returns TCL_OK.
Otherwise it returns TCL_OK after storing the element's
object pointer.
The reference count for the list element is not incremented;
the caller must do that if it needs to retain a pointer to the element.
Tcl_ListObjReplace replaces zero or more elements
of the list referenced by listPtr
with the objc objects in the array referenced by objv.
If listPtr does not point to a list object,
Tcl_ListObjReplace will attempt to convert it to one;
if the conversion fails, it returns TCL_ERROR
and leaves an error message in the interpreter's result object
if interp is not NULL.
Otherwise, it returns TCL_OK after replacing the objects.
If objv is NULL, no new elements are added.
If the argument first is zero or negative,
it refers to the first element.
If first is greater than or equal to the
number of elements in the list, then no elements are deleted;
the new elements are appended to the list.
count gives the number of elements to replace.
If count is zero or negative then no elements are deleted;
the new elements are simply inserted before the one
designated by first.
Tcl_ListObjReplace invalidates listPtr's
old string representation.
The reference counts of any elements inserted from objv
are incremented since the resulting list now refers to them.
Similarly, the reference counts for any replaced objects are decremented.
Because Tcl_ListObjReplace combines
both element insertion and deletion,
it can be used to implement a number of list operations.
For example, the following code inserts the objc objects
referenced by the array of object pointers objv
just before the element index of the list referenced by listPtr:
result = Tcl_ListObjReplace(interp, listPtr, index, 0, objc, objv);
Similarly, the following code appends the objc objects
referenced by the array objv
to the end of the list listPtr:
result = Tcl_ListObjLength(interp, listPtr, &length);
if (result == TCL_OK) {
result = Tcl_ListObjReplace(interp, listPtr, length, 0, objc, objv);
}
The count list elements starting at first can be deleted
by simply calling Tcl_ListObjReplace
with a NULL objvPtr:
result = Tcl_ListObjReplace(interp, listPtr, first, count, 0, NULL);
SEE ALSO
Tcl_NewObj Tcl_DecrRefCount Tcl_IncrRefCount Tcl_GetObjResult
KEYWORDS
append, index, insert, internal representation, length, list, list object, list type, object, object type, replace, string representation
'\"
'\" Copyright (c) 1996-1997 Sun Microsystems, Inc.
'\"
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
'\" RCS: @(#) $Id: IntObj.3,v 1.2 1998/09/14 18:39:49 stanton Exp $
'\"
'\" The definitions below are for supplemental macros used in Tcl/Tk
'\" manual entries.
'\"
'\" .AP type name in/out ?indent?
'\" Start paragraph describing an argument to a library procedure.
'\" type is type of argument (int, etc.), in/out is either "in", "out",
'\" or "in/out" to describe whether procedure reads or modifies arg,
'\" and indent is equivalent to second arg of .IP (shouldn't ever be
'\" needed; use .AS below instead)
'\"
'\" .AS ?type? ?name?
'\" Give maximum sizes of arguments for setting tab stops. Type and
'\" name are examples of largest possible arguments that will be passed
'\" to .AP later. If args are omitted, default tab stops are used.
'\"
'\" .BS
'\" Start box enclosure. From here until next .BE, everything will be
'\" enclosed in one large box.
'\"
'\" .BE
'\" End of box enclosure.
'\"
'\" .CS
'\" Begin code excerpt.
'\"
'\" .CE
'\" End code excerpt.
'\"
'\" .VS ?version? ?br?
'\" Begin vertical sidebar, for use in marking newly-changed parts
'\" of man pages. The first argument is ignored and used for recording
'\" the version when the .VS was added, so that the sidebars can be
'\" found and removed when they reach a certain age. If another argument
'\" is present, then a line break is forced before starting the sidebar.
'\"
'\" .VE
'\" End of vertical sidebar.
'\"
'\" .DS
'\" Begin an indented unfilled display.
'\"
'\" .DE
'\" End of indented unfilled display.
'\"
'\" .SO
'\" Start of list of standard options for a Tk widget. The
'\" options follow on successive lines, in four columns separated
'\" by tabs.
'\"
'\" .SE
'\" End of list of standard options for a Tk widget.
'\"
'\" .OP cmdName dbName dbClass
'\" Start of description of a specific option. cmdName gives the
'\" option's name as specified in the class command, dbName gives
'\" the option's name in the option database, and dbClass gives
'\" the option's class in the option database.
'\"
'\" .UL arg1 arg2
'\" Print arg1 underlined, then print arg2 normally.
'\"
'\" RCS: @(#) $Id: man.macros,v 1.2 1998/09/14 18:39:54 stanton Exp $
'\"
'\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages.
t .wh -1.3i ^B
^l \n(.l
b
'\" # Start an argument description
AP
!"\\$4"" .TP \\$4
\{\
!"\\$2"" .TP \\n()Cu
.TP 15
|