- Subject: Re: [slang-users] Some Collaboration Requested ....
- From: "John E. Davis" <davis@xxxxxxxxxxxxx>
- Date: Tue, 19 Apr 2005 16:28:16 -0400
Ben Duncan <ben@xxxxxxxxxxxxxxxxxx> wrote:
>Sooo... what I am trying to do is use a "generic" File Module that can be passed
>the values, and return the record into a structure defined in S-Lang. But I
> think
>for the time, such a Module is above my ability to write it.
Returning data as a slang structure from C code is rather simple. How
to do this is described in slang/doc/text/cslang.txt in the section
dealing with "Interpreter Structures".
Here is an example of this technique from another project:
slsh> print (maplib_new ("linear"));
{name="linear", x0=0, y0=0, x1=0, y1=0, A=Double_Type[2,2]}
The function "maplib_new" is defined in a module and returns a
structure that varies with its argument ("linear" in this case). As
you can see, the structure conists of 6 fields (name, x0, x1,...).
Here is the C code responsible for this. Note how it is mapped onto a
C structure. See cslang.txt for more details. Good luck, --John
typedef struct
{
char *name; /* required field */
double x_0, y_0;
double x_1, y_1;
SLang_Array_Type *at;
double a[4];
double a_inv[4];
}
Linear_Projection_Type;
static SLang_CStruct_Field_Type Linear_Projection_Struct[] =
{
MAKE_CSTRUCT_FIELD(Linear_Projection_Type, name, "name", SLANG_STRING_TYPE, 0),
MAKE_CSTRUCT_FIELD(Linear_Projection_Type, x_0, "x0", SLANG_DOUBLE_TYPE, 0),
MAKE_CSTRUCT_FIELD(Linear_Projection_Type, y_0, "y0", SLANG_DOUBLE_TYPE, 0),
MAKE_CSTRUCT_FIELD(Linear_Projection_Type, x_1, "x1", SLANG_DOUBLE_TYPE, 0),
MAKE_CSTRUCT_FIELD(Linear_Projection_Type, y_1, "y1", SLANG_DOUBLE_TYPE, 0),
MAKE_CSTRUCT_FIELD(Linear_Projection_Type, at, "A", SLANG_ARRAY_TYPE, 0),
SLANG_END_CSTRUCT_TABLE
};
static int linear_new (char *name)
{
Linear_Projection_Type g;
SLang_Array_Type *at;
int dims[2];
int ret;
dims[0] = 2;
dims[1] = 2;
if (NULL == (at = SLang_create_array (SLANG_DOUBLE_TYPE, 0, NULL, dims, 2)))
return -1;
((double *)at->data)[0] = 1.0;
((double *)at->data)[1] = 0.0;
((double *)at->data)[2] = 0.0;
((double *)at->data)[3] = 1.0;
g.at = at;
g.name = name;
g.x_0 = g.y_0 = 0.0;
g.x_1 = g.y_1 = 0.0;
ret = SLang_push_cstruct (&g, Linear_Projection_Struct);
SLang_free_array (at);
return ret;
}
_______________________________________________
To unsubscribe, visit http://jedsoft.org/slang/mailinglists.html
[2005 date index]
[2005 thread index]
[Thread Prev] [Thread Next]
[Date Prev] [Date Next]