Subject: Re: Urgent: multiple return vals from FooObjCmd - DN [1]
David Gravereaux <davygrvy@bigfoot.com> - 24 Jan 2000 - comp.lang.tcl
Dave O'Rourke <dorourke@ll.mit.edu> wrote:
>To the extension writers in the audience...
>
>What is the best way to return multiple values to Tcl from a C command
>procedure? For example, suppose I have...
>
>int FooObjCmd(ClientData cd, Tcl_interp *interp, int objc, Tcl_Obj
>*CONST objv[]);
>
>...which is intended to wrap another C function...
>
>int foo(int index, /* (in) index into data source */
> double startTime, /* (in) 1st time */
> double stopTime, /* (in) last time */
> int *nvals, /* (out) number of values */
> double *times, /* (out) the time array */
> double *values); /* (out) the values array */
>
>So given this setup, what is the best way to get the data (the times
>and values arrays) into Tcl? Is there a way to return more than just
>interp->result to Tcl? Tcl_UpVar? Tcl_LinkVar? Is there a better way?
> What am I missing?
Here's my guess. Please pardon an grossly neglected errors as i'm just typing
this :)
int
FooObjCmd(ClientData cd, Tcl_interp *interp, int objc, Tcl_Obj *CONST objv[])
{
Tcl_Obj *resultPtr = Tcl_GetObjResult(interp);
int index, nvals, i;
double start, stop, times, values;
if (Tcl_GetIntFromObj(interp, objv[1], &index) == TCL_ERROR) {
/* error */
}
if (Tcl_GetDoubleFromObj(interp, objv[2], &index) == TCL_ERROR) {
/* error */
}
if (Tcl_GetDoubleFromObj(interp, objv[3], &index) == TCL_ERROR) {
/* error */
}
foo(index, start, stop, &nvals, ×, &values);
for (i=0; i<nvals; i++)
{
Tcl_Obj *oData[2], *oList;
oData[0] = Tcl_NewDoubleObj(times[i]);
oData[1] = Tcl_NewDoubleObj(values[i]);
oList = Tcl_NewListObj(2,oData);
Tcl_ListObjAppendElement(interp, resultPtr, oList);
}
return TCL_OK;
}
That would return everything as a list within a list like this:
{{time} {val}} {{time} {val}} {{time} {val}} {{time} {val}} ...
set fooresult [foo $x $y $z]
# time of third
puts [lindex [lindex $fooresult 3] 0]
# value of third
puts [lindex [lindex $fooresult 3] 1]
--
* David Gravereaux *
Tomahawk Software Group
"Things should be as simple as possible, but not simpler." -- Albert E.
Last modified
2000-02-10
2000-02-10
(195.108.246.50)
Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated
