X
33 Rate this article:
No rating

Internal: Passing C defined strings into IDL via Callable IDL

Anonym

Passing C defined strings into IDL can also be done by treating the C char array (the string array) as a byte array in IDL. This is conisderably easier than trying to create an IDL_STRING array with previously C defined strings, but is a somewhat roundabout process of getting strings into IDL.

The code also passes in the same C defined string array into the IDL_STRING variable. The key to this is that the C defined string must also be defined as static since the IDL_STRING variable is as well.
The basic necessary steps are:

    IDL_VPTR u;
    char junk[]="junk";
    IDL_LONG dim[IDL_MAX_ARRAY_DIM];
    dim[0]=6;
    u = IDL_ImportNamedArray( "myarray", 1,dim, IDL_TYP_BYTE, (UCHAR *) junk, NULL, (void *) 0 );
    IDL_ExecuteStr( "print, string(myarray)" );
Below, I have placed some example code. This code includes an example written by Doug Loucks that shows the creation of IDL strings with IDL_STRING on the C side. Solution:
#include <stdio.h>
#include <string.h>

/* The following header file is located in the external directory of
the IDL installation tree. */
#include "export.h"

/* Handy Macro that computes the number of elements in an array. */
#define ARRLEN(arr) (sizeof(arr)/sizeof(arr[0]))

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

IDL_VPTR v;
IDL_VPTR u;

/* Create some random C string */
static char junk[]="junk";


/* Declare and initialize a local IDL string array variable. Each
element is a structure of type IDL_STRING. */
static IDL_STRING idl_stringarray[] = {
{0, 0, "Hello World."},
{0, 0, "This is a demonstration."},
{0, 0, "This is *only* a demonstration."},
{0, 0, junk}
};


/* Initialize the dimension argument for the IDL_ImportNamedArray
function. In this case, the IDL string array has one dimension. */
IDL_LONG dims[] = {ARRLEN(idl_stringarray)};
IDL_LONG dim[IDL_MAX_ARRAY_DIM];


/* Initialize the slen tags of the IDL string array elements. */
for (j=0; j<arrlen(idl_stringarray);> idl_stringarray[j].slen = strlen(idl_stringarray[j].s);
}


/* Initialize Callable IDL. */
if (IDL_Init( 0, &argc, argv ) == FALSE ) {
printf( "Error Initializing Callable IDL. " );
exit(0);
}
dim[0]=6;

/* Import the IDL string array variable. This will allow Callable
IDL to use the local copy. The name that IDL will "know" for the string array is given in the first argument. */

v = IDL_ImportNamedArray( "stringarray", 1,
dims, IDL_TYP_STRING,
(UCHAR *) idl_stringarray, NULL, (void *) 0 );

/* Import the C defined string as an IDL byte array*/
u = IDL_ImportNamedArray( "myarray", 1,
dim, IDL_TYP_BYTE,
(UCHAR *) junk, NULL, (void *) 0 );


/* If the import was successful, execute IDL statements to print
information about the string array variable and its contents.

NOTE: If you have more than a few IDL statements to run, the
IDL_Execute function should be used with a string array of
commands. Since this example runs only two commands,
IDL_ExecuteStr is used, instead. */

if (v != NULL) {
IDL_ExecuteStr( "help");
IDL_ExecuteStr( "print, stringarray" );
/* Use this command to convert to an IDL string */
IDL_ExecuteStr( "print, string(myarray)" );
IDL_Cleanup( IDL_FALSE );
} else {
printf( "Error Importing the IDL string array. " );
}

return 1;
}</arrlen(idl_stringarray);></string.h></stdio.h>