home *** CD-ROM | disk | FTP | other *** search
- /*
- ** $Id: simple.c,v 1.1 1993/11/16 23:36:16 idl Exp $
- **
- ** NAME:
- **
- ** simple
- **
- ** PURPOSE:
- ** This C function is used to demonstrate how to pass simple
- ** variables from IDL to a C function using the IDL function
- ** CALL_EXTERNAL. The variables values are squared and
- ** returned to show how variable values can be changed.
- **
- ** CATEGORY:
- ** Dynamic Link
- **
- ** CALLING SEQUENCE:
- ** This function is called in IDL by using the following command
- **
- ** IDL> result = CALL_EXTERNAL('simple.so', 'simple', $
- ** IDL> byte_var, short_var, long_var, float_var, $
- ** IDL> double_var, string_var, /S_VALUE )
- **
- ** INPUTS:
- **
- ** Byte_var: A scalar byte variable
- **
- ** Short_var: A scalar short integer variable
- **
- ** Long_var: A scalar long integer variable
- **
- ** Float_var: A scalar float variable
- **
- ** Double_var: A scalar float variable
- **
- ** String_var: A scalar string value (Actally an IDL STRING struct)
- **
- ** OUTPUTS:
- ** The function returns the passed in string value "Squared" and
- ** each numeric variables are squared.
- **
- ** SIDE EFFECTS:
- ** None.
- **
- ** RESTRICTIONS:
- ** When dealing with IDL Strings, the user should not directly
- ** manipulate the IDL string, but make a copy of the string,
- ** manipulate the copy and , return the new string as a result of
- ** the function.
- **
- ** EXAMPLE:
- **-----------------------------------------------------------------------------
- ;; The following is the commands that would be used to call this
- ;; routine in IDL
- ;;
- byte_var = 1b
- short_var = 2
- long_var = 3l
- float_var = 4.0
- double_var = 5d0
- string_var = "SIX"
-
- result = CALL_EXTERNAL('simple.so', 'simple', $
- byte_var, short_var, long_var, float_var, $
- double_var, string_var, /S_VALUE )
-
- **-----------------------------------------------------------------------------
- **
- ** MODIFICATION HISTORY:
- ** Written October, 1993 KDB
- **
- ** Declare header file
- */
-
- #include <stdio.h>
- #include <string.h>
-
- /*
- ** Declare the structure for an IDL string (From IDL User's Guide).
- */
-
- typedef struct {
- unsigned short slen; /* length of the string */
- short stype; /* Type of string */
- char *s; /* Pointer to chararcter array */
- } STRING;
-
- /*
- ** Declare the function
- */
-
- char *
- simple(argc, argv)
- int argc;
- void *argv[];
- {
- /*
- ** Declare variable
- */
- char *byte_var; /* Pointer to a char ( One byte ) */
- short *short_var; /* Pointer to short integer */
- long *long_var; /* Pointer to long */
- float *float_var; /* Pointer to float */
- double *double_var; /* Pointer to double */
- STRING *string_var; /* Pointer to IDL string structure */
- char *string2; /* Will hold the value of the string^2 */
-
- /*
- ** Insure that the correct number of arguments were passed in (argc = 6)
- */
- if(argc != 6)
- {
- /*
- ** Print an error message and return
- */
- fprintf(stderr,"simple: Incorrect number of arguments\r\n");
- return((char*)NULL); /* Signal an error */
- }
- /*
- ** Cast the pointer in argv to the pointer variables
- */
- byte_var = (char *) argv[0];
- short_var = (short *) argv[1];
- long_var = (long *) argv[2];
- float_var = (float *) argv[3];
- double_var = (double *) argv[4];
- string_var = (STRING *) argv[5];
- /*
- ** Now print a header indicating that we are in the called C function
- ** The "\r" was added to insure correct operation on Solaris systems
- */
- fprintf(stdout,
- "\r\n-----------------------------------------------------\r\n");
- fprintf(stdout,"Inside C function simple ");
- fprintf(stdout,"(Called from IDL using CALL_EXTERNAL)\r\n\r\n");
-
- /*
- ** Now print the values of each variable that was passed in from IDL
- */
- fprintf(stdout, "Scalar Values Passed in From IDL:\r\n");
- fprintf(stdout, "\tBYTE Parameter: \t%d\r\n", (short)(*byte_var));
- fprintf(stdout, "\tSHORT Parameter: \t%d\r\n", *short_var);
- fprintf(stdout, "\tLONG Parameter: \t%d\r\n", *long_var);
- fprintf(stdout, "\tFLOAT Parameter: \t%f\r\n", *float_var);
- fprintf(stdout, "\tDOUBLE Parameter: \t%f\r\n", *double_var);
-
- /*
- ** To print the string, de-reference the character string that is part
- ** of the IDL string struct.
- */
- fprintf(stdout, "\tSTRING Parameter: \t%s\r\n", string_var->s );
- /*
- ** Now print a line to seperate the output from this function and others
- */
- fprintf(stdout,
- "\r\n-----------------------------------------------------\r\n");
- /*
- ** Square each variable.
- */
- *byte_var = *byte_var * (*byte_var);
- *short_var = *short_var * (*short_var);
- *long_var = *long_var * (*long_var);
- *float_var = *float_var * (*float_var);
- *double_var = *double_var * (*double_var);
-
- /*
- ** To square the string, just create a string that contains two copies
- ** of the IDL string value. Allocate memory for the string and a NULL.
- */
- if( (string2 = (char*)malloc(2*(string_var->slen)+1) )
- == (char *)NULL)
- {
- /*
- ** Had a malloc error. write an error message and return a NULL
- */
- fprintf(stderr,"simple: malloc error.\r\n");
- return((char*)NULL);
- }
-
- /*
- ** Copy the IDL string to the just malloced string variable
- */
- string2 = strcpy(string2, string_var->s);
-
- /*
- ** Now add another copy of the IDL string
- */
- string2 = strcat(string2, string_var->s);
-
- /*
- ** now return the value of string2
- */
- return(string2);
- }
-