📄 listobj.3
字号:
'\"'\" 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.'\" '\" SCCS: @(#) @(#) ListObj.3 1.10 97/10/08 11:36:58'\" .so man.macros.TH Tcl_ListObj 3 8.0 Tcl "Tcl Library Procedures".BS.SH NAMETcl_ListObjAppendList, Tcl_ListObjAppendElement, Tcl_NewListObj, Tcl_SetListObj, Tcl_ListObjGetElements, Tcl_ListObjLength, Tcl_ListObjIndex, Tcl_ListObjReplace \- manipulate Tcl objects as lists.SH SYNOPSIS.nf\fB#include <tcl.h>\fR.spint\fBTcl_ListObjAppendList\fR(\fIinterp, listPtr, elemListPtr\fR).spint\fBTcl_ListObjAppendElement\fR(\fIinterp, listPtr, objPtr\fR).spTcl_Obj *\fBTcl_NewListObj\fR(\fIobjc, objv\fR).sp\fBTcl_SetListObj\fR(\fIobjPtr, objc, objv\fR).spint\fBTcl_ListObjGetElements\fR(\fIinterp, listPtr, objcPtr, objvPtr\fR).spint\fBTcl_ListObjLength\fR(\fIinterp, listPtr, intPtr\fR).spint\fBTcl_ListObjIndex\fR(\fIinterp, listPtr, index, objPtrPtr\fR).spint\fBTcl_ListObjReplace\fR(\fIinterp, listPtr, first, count, objc, objv\fR).SH ARGUMENTS.AS Tcl_Interp "*CONST objv[]" out.AP Tcl_Interp *interp inIf an error occurs while converting an object to be a list object,an error message is left in the interpreter's result objectunless \fIinterp\fR is NULL..AP Tcl_Obj *listPtr in/outPoints to the list object to be manipulated.If \fIlistPtr\fR does not already point to a list object,an attempt will be made to convert it to one..AP Tcl_Obj *elemListPtr in/outFor \fBTcl_ListObjAppendList\fR, this points to a list objectcontaining elements to be appended onto \fIlistPtr\fR.Each element of *\fIelemListPtr\fR willbecome a new element of \fIlistPtr\fR.If *\fIelemListPtr\fR is not NULL anddoes not already point to a list object,an attempt will be made to convert it to one..AP Tcl_Obj *objPtr inFor \fBTcl_ListObjAppendElement\fR,points to the Tcl object that will be appended to \fIlistPtr\fR.For \fBTcl_SetListObj\fR,this points to the Tcl object that will be converted to a list objectcontaining the \fIobjc\fR elements of the array referenced by \fIobjv\fR..AP int *objcPtr inPoints to location where \fBTcl_ListObjGetElements\fRstores the number of element objects in \fIlistPtr\fR..AP Tcl_Obj ***objvPtr outA location where \fBTcl_ListObjGetElements\fR stores a pointer to an arrayof pointers to the element objects of \fIlistPtr\fR. .AP int objc inThe number of Tcl objects that \fBTcl_NewListObj\fRwill insert into a new list object,and \fBTcl_ListObjReplace\fR will insert into \fIlistPtr\fR.For \fBTcl_SetListObj\fR,the number of Tcl objects to insert into \fIobjPtr\fR..VS.TP Tcl_Obj *CONST \fIobjv\fR[] (in).An array of pointers to objects.\fBTcl_NewListObj\fR will insert these objects into a new list objectand \fBTcl_ListObjReplace\fR will insert them into an existing \fIlistPtr\fR.Each object will become a separate list element. .VE.AP int *intPtr outPoints to location where \fBTcl_ListObjLength\fRstores the length of the list..AP int index inIndex of the list element that \fBTcl_ListObjIndex\fRis to return.The first element has index 0..AP Tcl_Obj **objPtrPtr outPoints to place where \fBTcl_ListObjIndex\fR is to storea pointer to the resulting list element object..AP int first inIndex of the starting list element that \fBTcl_ListObjReplace\fRis to replace.The list's first element has index 0..AP int count inThe number of elements that \fBTcl_ListObjReplace\fRis to replace..BE.SH DESCRIPTION.PPTcl list objects have an internal representation that supportsthe efficient indexing and appending.The procedures described in this man page are used tocreate, modify, index, and append to Tcl list objects from C code..PP\fBTcl_ListObjAppendList\fR and \fBTcl_ListObjAppendElement\fRboth add one or more objectsto the end of the list object referenced by \fIlistPtr\fR.\fBTcl_ListObjAppendList\fR appends each element of the list objectreferenced by \fIelemListPtr\fR while\fBTcl_ListObjAppendElement\fR appends the single objectreferenced by \fIobjPtr\fR.Both procedures will convert the object referenced by \fIlistPtr\fRto a list object if necessary.If an error occurs during conversion,both procedures return \fBTCL_ERROR\fR and leave an error messagein the interpreter's result object if \fIinterp\fR is not NULL.Similarly, if \fIelemListPtr\fR does not already refer to a list object,\fBTcl_ListObjAppendList\fR will attempt to convert it to oneand if an error occurs during conversion,will return \fBTCL_ERROR\fRand leave an error message in the interpreter's result objectif interp is not NULL.Both procedures invalidate any old string representation of \fIlistPtr\fRand, if it was converted to a list object,free any old internal representation.Similarly, \fBTcl_ListObjAppendList\fR frees any old internal representationof \fIelemListPtr\fR if it converts it to a list object.After appending each element in \fIelemListPtr\fR,\fBTcl_ListObjAppendList\fR increments the element's reference countsince \fIlistPtr\fR now also refers to it.For the same reason, \fBTcl_ListObjAppendElement\fRincrements \fIobjPtr\fR's reference count.If no error occurs,the two procedures return \fBTCL_OK\fR after appending the objects..PP\fBTcl_NewListObj\fR and \fBTcl_SetListObj\fRcreate a new object or modify an existing object to hold the \fIobjc\fR elements of the array referenced by \fIobjv\fRwhere each element is a pointer to a Tcl object.If \fIobjc\fR 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 countsof the elements in \fIobjc\fR since the list object now refers to them.The new list object returned by \fBTcl_NewListObj\fRhas reference count zero..PP\fBTcl_ListObjGetElements\fR returns a count anda pointer to an array of the elements in a list object.It returns the count by storing it in the address \fIobjcPtr\fR.Similarly, it returns the array pointer by storing itin the address \fIobjvPtr\fR.If \fIlistPtr\fR is not already a list object,\fBTcl_ListObjGetElements\fR will attempt to convert it to one;if the conversion fails, it returns \fBTCL_ERROR\fRand leaves an error message in the interpreter's result objectif \fIinterp\fR is not NULL.Otherwise it returns \fBTCL_OK\fR after storing the count and array pointer..PP\fBTcl_ListObjLength\fR returns the number of elements in the list objectreferenced by \fIlistPtr\fR.It returns this count by storing an integer in the address \fIintPtr\fR.If the object is not already a list object,\fBTcl_ListObjLength\fR will attempt to convert it to one;if the conversion fails, it returns \fBTCL_ERROR\fRand leaves an error message in the interpreter's result objectif \fIinterp\fR is not NULL.Otherwise it returns \fBTCL_OK\fR after storing the list's length..PPThe procedure \fBTcl_ListObjIndex\fR returns a pointer to the objectat element \fIindex\fR in the list referenced by \fIlistPtr\fR.It returns this object by storing a pointer to itin the address \fIobjPtrPtr\fR.If \fIlistPtr\fR does not already refer to a list object,\fBTcl_ListObjIndex\fR will attempt to convert it to one;if the conversion fails, it returns \fBTCL_ERROR\fRand leaves an error message in the interpreter's result objectif \fIinterp\fR is not NULL.If the index is out of range,that is, \fIindex\fR is negative orgreater than or equal to the number of elements in the list,\fBTcl_ListObjIndex\fR stores a NULL in \fIobjPtrPtr\fRand returns \fBTCL_OK\fR.Otherwise it returns \fBTCL_OK\fR after storing the element'sobject 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..PP\fBTcl_ListObjReplace\fR replaces zero or more elementsof the list referenced by \fIlistPtr\fRwith the \fIobjc\fR objects in the array referenced by \fIobjv\fR.If \fIlistPtr\fR does not point to a list object,\fBTcl_ListObjReplace\fR will attempt to convert it to one;if the conversion fails, it returns \fBTCL_ERROR\fRand leaves an error message in the interpreter's result objectif \fIinterp\fR is not NULL.Otherwise, it returns \fBTCL_OK\fR after replacing the objects.If \fIobjv\fR is NULL, no new elements are added.If the argument \fIfirst\fR is zero or negative,it refers to the first element.If \fIfirst\fR is greater than or equal to thenumber of elements in the list, then no elements are deleted;the new elements are appended to the list.\fIcount\fR gives the number of elements to replace.If \fIcount\fR is zero or negative then no elements are deleted;the new elements are simply inserted before the onedesignated by \fIfirst\fR.\fBTcl_ListObjReplace\fR invalidates \fIlistPtr\fR'sold string representation.The reference counts of any elements inserted from \fIobjv\fRare incremented since the resulting list now refers to them.Similarly, the reference counts for any replaced objects are decremented..PPBecause \fBTcl_ListObjReplace\fR combinesboth element insertion and deletion,it can be used to implement a number of list operations.For example, the following code inserts the \fIobjc\fR objectsreferenced by the array of object pointers \fIobjv\fRjust before the element \fIindex\fR of the list referenced by \fIlistPtr\fR:.CSresult = Tcl_ListObjReplace(interp, listPtr, index, 0, objc, objv);.CESimilarly, the following code appends the \fIobjc\fR objectsreferenced by the array \fIobjv\fRto the end of the list \fIlistPtr\fR:.CSresult = Tcl_ListObjLength(interp, listPtr, &length);if (result == TCL_OK) { result = Tcl_ListObjReplace(interp, listPtr, length, 0, objc, objv);}.CEThe \fIcount\fR list elements starting at \fIfirst\fR can be deletedby simply calling \fBTcl_ListObjReplace\fRwith a NULL \fIobjvPtr\fR:.CSresult = Tcl_ListObjReplace(interp, listPtr, first, count, 0, NULL);.CE.SH "SEE ALSO"Tcl_NewObj, Tcl_DecrRefCount, Tcl_IncrRefCount, Tcl_GetObjResult.SH KEYWORDSappend, index, insert, internal representation, length, list, list object, list type, object, object type, replace, string representation
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -