![]() ![]() ![]() ![]() Next: Calling Fortran Routines from Up: Calls to different language Previous: Calling C Routines from Contents String Arguments from FortranFortran passes string arguments in a special, compiler-dependent, way. To facilitate this, the CCTK provides a set of macros to enable the translation to C strings. The macros are defined in cctk_FortranString.h, which should be included in your C file. String arguments must always come last in the argument list for these macros to be effective (some Fortran compilers automatically migrate the strings to the end, so there is no portable workaround). The macros to use depend upon the number of string arguments-we currently support up to three. The macros are <ONE|TWO|THREE>_FORTSTRING_ARG. Corresponding to each of these are two macros <ONE|TWO|THREE>_FORTSTRING_CREATE and <ONE|TWO|THREE>_FORTSTRING_PTR, which take one, two, or three arguments depending on the number of strings. The latter set is only necessary if a string is to be modified. In more detail:
The use of the macros is probably best explained with examples. For read-only access to the strings, only the first two macros are needed, the following example compares two strings passed in from Fortran.
#include <stdlib.h> #include <string.h> #include <cctk_FortranString.h> int CCTK_FCALL CCTK_FNAME(CompareStrings)(TWO_FORTSTRING_ARG) { int retval; /* Allocate and create C strings with \0 at end. */ /* This makes variable declarations, so it must be before any executable statements.*/ TWO_FORTSTRING_CREATE(arg1,arg2) /* Do some work with the strings */ retval = strcmp(arg1,arg2); /* Important, these must be freed after use */ free(arg1); free(arg2); return retval; } Since the null terminated strings may be copies of the strings passed from Fortran, they should be treated as read-only. To change the data in a string passed from Fortran, you need to use the FORTSTRING_PTR macros, which declare and set up pointers to the strings passed from C. Note that this macro must be used after the FORTSTRING_CREATE macro. For example, the following routine copies the contents of the second string to the first string
#include <stdlib.h> #include <string.h> #include <cctk_FortranString.h> int CCTK_FCALL CCTK_FNAME(CopyStrings)(TWO_FORTSTRING_ARG) { int retval; /* Allocate and create C strings with \0 at end. */ /* This makes variable declarations, so it must be before any executable statements. */ TWO_FORTSTRING_CREATE(arg1,arg2) TWO_FORTSTRING_PTR(farg1,farg2) /* Do some work with the strings */ retval = strncpy(farg1,arg2,cctk_strlen1); /* Important, these must be freed after use */ free(arg1); free(arg2); return retval; } Note that in the example above, two new variables, pointers to the Fortran strings, were created. These are just pointers and should not be freed. The example also illustrates the automatically-created variables, e.g. cctk_strlen1, which hold the sizes of original Fortran strings. When writing to a string its length should never be exceeded.
![]() ![]() ![]() ![]() Next: Calling Fortran Routines from Up: Calls to different language Previous: Calling C Routines from Contents |