MOOCHO (Single Doxygen Collection) Version of the Day
Namespaces
FortranTypes_CppFortranStrings.hpp File Reference
#include "Moocho_ConfigDefs.hpp"
#include "Teuchos_F77_wrappers.h"
Include dependency graph for FortranTypes_CppFortranStrings.hpp:

Go to the source code of this file.

Namespaces

namespace  FortranTypes

Pass strings to and from Fortran and C++.

This is a portable method to pass strings from C++ to Fortran. The basic idea is to convert a C++ (or C) null terminated #char[]# string to a Fortran compatable integer array #f_int[]# with an integer length #f_int#. Then in Fortran this integer array is converted into a Fortran CHARACTER* string. For this to work the ASCII numeric representation in the target C++ and Fortran compilers should be the same. If they are not then these encapsulation routines may be specialized to make the mapping work , either on the C++ side or the Fortran side.

The Mapping routines are:

C++\ ----------\

  int convert_to_f_int_string( const char string[], f_int** i_string
    , f_int* string_len );
  int convert_from_f_int_string( const f_int i_string[], f_int string_len
    , char string[] );
  

Fortran\ ----------\

        INTEGER FUNCTION CONVERT_FROM_C_INT_STR( I_STRING, STRING_LEN, STRING )
        INTEGER I_STRING(*), STRING_LEN
      CHARACTER*(*) STRING(*)
      ...
      END

        INTEGER FUNCTION CONVERT_TO_C_INT_STR( STRING, I_STRING, STRING_LEN )
      CHARACTER*(*) STRING(*)
        INTEGER I_STRING(*), STRING_LEN
      ...
      END
  

When using this care must be taken that enough memory is allocated to hold the target strings and integer arrays before calling these converstion precedures in each language.

For example, here is how a you can pass a string from C++ to Fortran in order to open up a Fortran file:

C++ \ ----------\

  char file_name = "myfile.opt";
  f_int i_file_name[20];
  f_int file_name_len;
  convert_to_f_int_string( file_name, &i_file_name, &file_name_len );
  f_int f_file_num;
  F_OPEN_FILE( i_file_name, file_name_len, &f_file_num );
  // Now we can call Fortran routines to read and write to the
  // Fortran file with the unit number f_file_num  
  

Fortran \ ----------\

        SUBROUTINE F_OPEN_FILE( I_FILE_NAME, FILE_NAME_LEN, I_FILE_NUM )
      *** Formal parameters
      INTEGER I_FILE_NAME(*), FILE_NAME_LEN, I_FILE_NUM
      *** Local variables
      CHARACTER*20 FILE_NAME
      CALL CONVERT_FROM_C_INT_STR( I_FILE_NAME, FILE_NAME_LEN, FILE_NAME )
      OPEN( I_FILE_NUM, FILE = FILE_NAME )
      END 
  
int FortranTypes::convert_to_f_int_string (const char string[], f_int i_string[], f_int *string_len)
 Convert from a C++ (or C) null terminated string to an integer array suitable for passing to Fortran and later conversion to a CHARACTER string.
int FortranTypes::convert_from_f_int_string (const f_int i_string[], f_int string_len, char string[])
 Convert from a C++ (or C) null terminated string to an integer array suitable for passing to Fortran and later conversion to a CHARACTER string.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines