Stan
Stan

Reputation: 38255

Tcl API how to get a list from Tcl

Tcl 8.4

In my Tcl script:

set foo1 false
set foo2 "yes"
set foo3 [list item1 item2 item3]

There's a API to get scalars like foo1 or foo2. Eg: Tcl_GetVar(tcl_interp, string("foo1").c_str(), flags). I was wondering if there's any API to get list (like foo3) from Tcl?

Upvotes: 6

Views: 3365

Answers (3)

ARK91
ARK91

Reputation: 373

Hi i have found this useful link containing an example for dealing the list:

Reference:: https://www.tcl.tk/man/tclx8.2/TclCommandWriting.3.html

int Tcl_LreverseObjCmd(notUsed, interp, objc, objv)
    ClientData    notUsed;              /* Not used. */
    Tcl_Interp   *interp;               /* Current interpreter. */
    int           objc;                 /* Number of arguments. */
    Tcl_Obj     **obj;                  /* Argument strings. */
{
    int listObjc, lowListIndex, hiListIndex;
    Tcl_Obj **listObjv;
    char *temp, *resultList;
    Tcl_Obj **newListObjv;

    /* Verify argument count.  Since we take only one argument, argument
     * count must be 2 (command plus one argument).
     */
    if (objc != 2)
        return TclX_WrongArgs (interp, objv [0], "list");

    /* Create an object to handle the new list we're creating */
    newListObjv = Tcl_NewObj();

    /* Crack the list at objv[1] into its own count and array of object
     * pointers.
     */
    if (Tcl_ListObjGetElements (interp, objv[1], &listObjc, &listObjv) != TCL_OK) {
        return TCL_ERROR;
    }

    /* For each element in the source list from last to first, append an
     * element to the new list.
     */
    for (listIndex = listObjc - 1; listIndex >= 0; listIndex--) {
        Tcl_ListObjAppendElement (interp, newListObjv, listObjv[listIndex]);
    }
FIX: NEED TO RETURN THE LIST.
    return TCL_OK;
}

Upvotes: -1

Donal Fellows
Donal Fellows

Reputation: 137567

It's a two-stage thing. You first fetch the value with one of the Tcl_GetVar family of functions, then you get the pieces of the list that you're interested in (with Tcl_SplitList or Tcl_ListObjGetElements, normally).

As a more concrete example:

////// FETCH FROM VARIABLE //////
// The NULL is conventional when you're dealing with scalar variable,
// and the 0 could be TCL_GLOBAL_ONLY or 
Tcl_Obj *theList = Tcl_GetVar2Ex(interp, string("foo1").c_str(), NULL, TCL_LEAVE_ERR_MSG);
if (theList == NULL) {
    // Was an error; message in interpreter result...
}

////// EXTRACT ELEMENTS //////
int objc;
Tcl_Obj **objv;
if (Tcl_ListObjGetElements(interp, theList, &objc, &objv) == TCL_ERROR) {
    // Not a list! error message in interpreter result...
}

////// WORK WITH VALUES //////
for (int i=0 ; i<objc ; i++) {
    const char *value = Tcl_GetString(objv[i]);
    // Whatever...
}

Upvotes: 7

kostix
kostix

Reputation: 55453

I'm not sure, but Tcl_ListObjGetElements looks like what you want. Or, alternatively, Tcl_ObjGetVar2 would return a Tcl_Obj which you could then manipulate using the rest of the Tcl API for working with list objects.

Upvotes: 1

Related Questions