X
64 Rate this article:
No rating

INTERNAL: Creating IDL structs in C (Callable IDL)

Anonym
Topic:
Creating IDL structs in C (Callable IDL)Discussion:
This program creates an array of IDL structures in C, and then manipulates them in IDL.Solution:
#include 
#include "export.h"


static void free_callback(UCHAR *addr)
{
printf("IDL released(%u)\r ", addr);
}



int main(int argc, char **argv)
{
int i, j;

IDL_LONG dim[IDL_MAX_ARRAY_DIM];

/* Set up the params for telling IDL the structure format. */
static IDL_LONG three = 3;
static IDL_LONG tag2_dims[] = {1,3}; /* Array of 3 IDL type FLOATs */
static IDL_STRUCT_TAG_DEF s_tags[] =
{{"TAG1", 0, (void *) IDL_TYP_LONG},
{"TAG2", tag2_dims, (void *) IDL_TYP_FLOAT},
{0}};

/* Make the equivalent C structure. */
typedef struct data_struct
{
IDL_LONG tag1_data;
float tag2_data[3];
} DATA_STRUCT;

static DATA_STRUCT c_data[] = {{1,{2.0,3.0,4.0}},{5,{6.0,7.0,8.0}},{9,{10.0,11.0,12.0}}};
void *s;
IDL_VPTR v;

static DATA_STRUCT *data;


if (IDL_Init(0, &argc, argv))
{

dim[0] = 10;

s = IDL_MakeStruct(0, s_tags);

IDL_ExecuteStr("PRINT,'Array of structures created.'");

/* HERE IS HOW TO GET AN ARRAY OF INITIALIZED STRUCTURES INTO IDL. */

if (v = IDL_ImportNamedArray("TMP", 1, &three, IDL_TYP_STRUCT, (UCHAR *) &c_data, 0, s))
{

IDL_ExecuteStr("PRINT,'Array of initialized structures now passed into IDL as variable: TMP'");
IDL_ExecuteStr("PRINT,'Pointed to by C variable \"v\"'");
IDL_ExecuteStr("HELP, TMP");
IDL_ExecuteStr("PRINT, TMP.TAG1, TMP.TAG2");

printf("Here in C, the tags are: ");
data = (DATA_STRUCT *) v->value.s.arr->data;
for(j = 0; j < 3; j++)
printf(" %8d ", data[j].tag1_data);
printf(" ");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
printf("%12.3f ", data[i].tag2_data[j]);
printf(" ");
}


IDL_ExecuteStr("PRINT,'Change variable: TMP inside of IDL'");
IDL_ExecuteStr("TMP.TAG1 = LINDGEN(3)*4");
IDL_ExecuteStr("TMP.TAG2 = FINDGEN(3,3)*3.0");
IDL_ExecuteStr("PRINT, TMP.TAG1, TMP.TAG2");

printf("Here in C, the changed tags are: ");
for(j = 0; j < 3; j++)
printf(" %8d ", data[j].tag1_data);
printf(" ");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
printf("%12.3f ", data[i].tag2_data[j]);
printf(" ");
}


IDL_Cleanup(FALSE);   /* Don't return */

}
}

return 1;
}