/*
* $Id: c_example.c,v 1.7 1997/07/10 16:18:16 cmm Exp $
*
* A program to illustrate calling a module from C
*
* Called from GCL with e.g.
MODULE:EXECUTE:C("my_c_module", PARS)
*
* Freddie Akeroyd, ISIS, 19/2/97
*
*/
/*
* These two genie includes must be present in any C module
* They include important definitions and typedefs
*/
#include <stdio.h>
#include <stdlib.h>
#include <genie_cmodule.h>
#include <genie_cmodule_ver.h>
static const char* sa[] = { "string1",
"string2", 0 };
void my_c_module(GenieWorkspace* pars_get, GenieWorkspace*
pars_put)
{
fort_real val1 = 3.4, val2 = 5.2;
char* s;
fort_real* ra;
fort_int len_ra;
fort_real val3[3][2] = { {1.6, 2.3}, {3.1, 4.7}, {7.4, 9.2} };
fort_int dims_array[] = { 3, 2}; /* dimensions of val3 */
int i;
/*
* All C modules should do the following test - it ensures that a
module
* linked against a recent version of the genie library is
* not executed by a program linked against an older version
*/
if (!cmodule_version_ok(CMODULE_MAJOR_VERSION,
CMODULE_MINOR_VERSION))
{
cmodule_print("Library version mismatch for
MY_C_MODULE",
CMODULE_PRINT_ERROR);
return;
}
cmodule_print("Running MY_C_MODULE",
CMODULE_PRINT_INFORMATION);
/* read some data */
cmodule_get_real(pars_get, "R1", &val1);
printf("val1 = %f\n", (double)val1);
ra = (void*)0; /* we want genie to malloc() for us */
cmodule_get_real_array(pars_get, "RA", &ra,
&len_ra);
for(i=0; i<len_ra; i++)
{
printf("%d = %f\n", i, ra[i]);
}
free(ra);
cmodule_get_string(pars_get, "S", &s);
printf("%s\n", s);
free(s);
/* write some data into fields of the returned Workspace */
cmodule_put_real(pars_put, "R2", val2);
cmodule_put_double(pars_put, "D2", val2);
cmodule_put_int(pars_put, "an_int", 5);
cmodule_put_string(pars_put, "junk",
"hello");
/* This allocates a size 10 string array, but only the first two
elements
will be occupied (as sa has two strings + a NULL third element).
If the size you pass is greater than the array length, the last
array
element MUST be NULL */
cmodule_put_string_array(pars_put, "sa", sa, 10);
/* We need a (fort_real*) cast as the program treats 2-D arrays
as 1-D ones */
cmodule_put_nd_real_array(pars_put, "ndra",
(fort_real*)val3, dims_array, 2)
;
cmodule_print("Finished MY_C_MODULE",
CMODULE_PRINT_INFORMATION);
}