Internal: Creating nested structures in C code linked to IDL with LINKIMAGE
Anonym
Here is an example on how to create nested structures in C code linked to IDL with LINKIMAGE:
#include <stdio.h>
#include <stdlib.h>
#include "export.h"
#ifdef WIN32
#include <windows.h>
#include <io.h>
#include <direct.h>
#define IDL_CDECL __declspec(dllexport)
#else
#include <unistd.h>
#endif
/* function prototypes */
extern IDL_CDECL IDL_VPTR nested_struct(int, IDL_VPTR *, char *);
IDL_CDECL IDL_VPTR nested_struct(int argc, IDL_VPTR argv[], char *argk)
{
typedef struct mystruct {
IDL_LONG tag1_data[4];
float tag2_data;
IDL_LONG tag3_data;
IDL_LONG tag4_data;
} MYSTRUCT;
static IDL_LONG tag1_dims[] = {1,4};
void *v;
static IDL_STRUCT_TAG_DEF m_tags[] = {
{"TAG1",tag1_dims,(void *) IDL_TYP_LONG},
{"TAG2",0,(void *) IDL_TYP_FLOAT},
{"TAG3",0,(void *) IDL_TYP_LONG},
{"TAG4",0,(void *) IDL_TYP_LONG},
{ 0 }
};
static IDL_LONG one = 1;
static IDL_STRUCT_TAG_DEF s_tags[5];
typedef struct data_struct {
IDL_LONG tag1_data[4];
float tag2_data;
IDL_LONG tag3_data;
MYSTRUCT my_data;
} DATA_STRUCT;
static DATA_STRUCT s_data;
void *s;
IDL_VPTR retstruct,retstruct2;
char *tstruct, *tstruct2;
/* Before doing anything, store a zero value into the second argument
this will zero out the memory and make sure that the variable is
storable (not an expression, str, field, arr subscript). */
IDL_StoreScalarZero( argv[1], IDL_TYP_INT);
/* Create the structure definition */
v = IDL_MakeStruct(0, m_tags);
s_tags[0].name = "TAG1";
s_tags[0].dims = tag1_dims;
s_tags[0].type = (void *) IDL_TYP_LONG;
s_tags[0].flags = 0;
s_tags[1].name = "TAG2";
s_tags[1].dims = 0;
s_tags[1].type = (void *) IDL_TYP_FLOAT;
s_tags[1].flags = 0;
s_tags[2].name = "TAG3";
s_tags[2].dims = 0;
s_tags[2].type = (void *) IDL_TYP_LONG;
s_tags[2].flags = 0;
s_tags[3].name = "TAG4";
s_tags[3].dims = 0;
s_tags[3].type = (IDL_StructDefPtr *) v;
s_tags[3].flags = 0;
s_data.tag1_data[0] = 1L;
s_data.tag1_data[1] = 2L;
s_data.tag1_data[2] = 3L;
s_data.tag1_data[3] = 4L;
s_data.tag2_data = 2.0;
s_data.tag3_data = 3;
s_data.my_data.tag1_data[0] = 14L;
s_data.my_data.tag1_data[1] = 15L;
s_data.my_data.tag1_data[2] = 16L;
s_data.my_data.tag1_data[3] = 17L;
s_data.my_data.tag2_data = 5.0;
s_data.my_data.tag3_data = 6;
s_data.my_data.tag4_data = 7;
/* Create the structure definition */
s = IDL_MakeStruct(0, s_tags);
tstruct = IDL_MakeTempStruct(s, 1, &one,
&retstruct, IDL_TRUE);
tstruct2 = IDL_MakeTempStruct(s, 1, &one,
&retstruct2, IDL_TRUE);
/* copy the stat structure memory to the return variables
tstruct represents the data area of the temporary
structure that is also the return structure */
memcpy( (void *) tstruct, (void *) &s_data, sizeof(DATA_STRUCT));
memcpy( (void *) tstruct2, (void *) &s_data, sizeof(DATA_STRUCT));
/* copy the temporary variable into the second argument */
IDL_VarCopy(retstruct2, argv[1]);
return(retstruct);
}
</unistd.h></direct.h></io.h></windows.h></stdlib.h></stdio.h>