Tcl_ListObj(3) Tcl Library Procedures Tcl_ListObj(3) ______________________________________________________________________________ NAME Tcl_ListObjAppendList, Tcl_ListObjAppendElement, Tcl_NewListObj, Tcl_SetListObj, Tcl_ListObjGetElements, Tcl_ListObjLength, Tcl_ListOb- jIndex, Tcl_ListObjReplace - 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 Tcl_Interp *interp (in(1,8)) If an error(8,n) occurs while con- verting an object to be a list object, an error(8,n) message is left in(1,8) the interpreter's result object unless interp is NULL. Tcl_Obj *listPtr (in(1,8)/out) Points to the list object to be manipulated. If listPtr does not already point to a list object, an attempt will be made to convert it to one. Tcl_Obj *elemListPtr (in(1,8)/out) For Tcl_ListObjAppendList, this points to a list object contain- ing elements to be appended onto listPtr. Each element of *elem- ListPtr will become a new ele- ment of listPtr. If *elem- ListPtr is not NULL and does not already point to a list object, an attempt will be made to con- vert it to one. Tcl_Obj *objPtr (in(1,8)) For Tcl_ListObjAppendElement, points to the Tcl object that will be appended to listPtr. For Tcl_SetListObj, this points to the Tcl object that will be converted to a list object con- taining the objc elements of the array referenced by objv. int *objcPtr (in(1,8)) Points to location where Tcl_ListObjGetElements stores the number of element objects in(1,8) listPtr. Tcl_Obj ***objvPtr (out) A location where Tcl_ListOb- jGetElements stores a pointer to an array of pointers to the ele- ment objects of listPtr. int objc (in(1,8)) The number of Tcl objects that Tcl_NewListObj will insert into a new list object, and Tcl_Lis- tObjReplace will insert into listPtr. For Tcl_SetListObj, the number of Tcl objects to insert into objPtr. | Tcl_Obj "*CONST objv[]" in(1,8) || An array of pointers to objects. | Tcl_NewListObj will insert these | objects into a new list object | and Tcl_ListObjReplace will | insert them into an existing | listPtr. Each object will | become a separate list element. int *intPtr (out) Points to location where Tcl_ListObjLength stores the length of the list. int index (in(1,8)) Index of the list element that Tcl_ListObjIndex is to return. The first element has index 0. Tcl_Obj **objPtrPtr (out) Points to place where Tcl_ListO- bjIndex is to store a pointer to the resulting list element object. int first (in(1,8)) Index of the starting list ele- ment that Tcl_ListObjReplace is to replace. The list's first element has index 0. int count (in(1,8)) The number of elements that Tcl_ListObjReplace is to replace. _________________________________________________________________ DESCRIPTION Tcl list objects have an internal representation that supports the efficient indexing and appending. The procedures described in(1,8) this man(1,5,7) 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_Lis- tObjAppendList 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 refer- enced by listPtr to a list object if(3,n) necessary. If an error(8,n) occurs during conversion, both procedures return TCL_ERROR and leave an error(8,n) message in(1,8) the interpreter's result object if(3,n) interp is not NULL. Sim- ilarly, if(3,n) elemListPtr does not already refer to a list object, Tcl_ListObjAppendList will attempt to convert it to one and if(3,n) an error(8,n) occurs during conversion, will return TCL_ERROR and leave an error(8,n) mes- sage in(1,8) the interpreter's result object if(3,n) interp is not NULL. Both procedures invalidate any old string(3,n) representation of listPtr and, if(3,n) it was converted to a list object, free any old internal representa- tion. Similarly, Tcl_ListObjAppendList frees any old internal repre- sentation of elemListPtr if(3,n) it converts it to a list object. After appending each element in(1,8) 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(8,n) 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(1,3) than or equal to zero, they return an empty object. The new object's string(3,n) representation is left invalid. The two procedures increment the reference counts of the elements in(1,8) 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(1,8) a list object. It returns the count by storing it in(1,8) the address objcPtr. Similarly, it returns the array pointer by storing it in(1,8) the address objvPtr. The memory pointed to is managed by Tcl and should not be freed by the caller. If listPtr is not already a list object, Tcl_ListObjGetElements will attempt to convert it to one; if(3,n) the conversion fails, it returns TCL_ERROR and leaves an error(8,n) message in(1,8) the interpreter's result object if(3,n) interp is not NULL. Otherwise it returns TCL_OK after storing the count and array pointer. Tcl_ListObjLength returns the number of elements in(1,8) the list object referenced by listPtr. It returns this count by storing an integer in(1,8) the address intPtr. If the object is not already a list object, Tcl_ListObjLength will attempt to convert it to one; if(3,n) the conversion fails, it returns TCL_ERROR and leaves an error(8,n) message in(1,8) the inter- preter's result object if(3,n) 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 ele- ment index in(1,8) the list referenced by listPtr. It returns this object by storing a pointer to it in(1,8) the address objPtrPtr. If listPtr does not already refer to a list object, Tcl_ListObjIndex will attempt to convert it to one; if(3,n) the conversion fails, it returns TCL_ERROR and leaves an error(8,n) message in(1,8) the interpreter's result object if(3,n) 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(1,8) the list, Tcl_ListO- bjIndex stores a NULL in(1,8) objPtrPtr and returns TCL_OK. Otherwise it returns TCL_OK after storing the element's object pointer. The refer- ence count for the list element is not incremented; the caller must do that if(3,n) it needs to retain a pointer to the element. Tcl_ListObjReplace replaces zero or more elements of the list refer- enced by listPtr with the objc objects in(1,8) the array referenced by objv. If listPtr does not point to a list object, Tcl_ListObjReplace will attempt to convert it to one; if(3,n) the conversion fails, it returns TCL_ERROR and leaves an error(8,n) message in(1,8) the interpreter's result object if(3,n) 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(1,8) 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(3,n) 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 dele- tion, 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(3,n) (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(3,n) representation Tcl 8.0 Tcl_ListObj(3)