FortranTypes_CppFortranStrings.hpp File Reference

#include "Moocho_ConfigDefs.hpp"
#include "Teuchos_F77_wrappers.h"

Include dependency graph for FortranTypes_CppFortranStrings.hpp:

This graph shows which files directly or indirectly include this file:

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 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 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.


Function Documentation

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.

Parameters:
string [I] Null terminated C++ (C) string.
i_string [O] Integer array containing the string
string_len [O] Length of the string.
Returns:
Returns 0 if successful. Otherwise it returns the indice (1,...) of the ith character that is not ASCII convertable.

Definition at line 35 of file FortranTypes_CppFortranStrings.cpp.

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.

Parameters:
i_string [I] Integer array containing the string
string_len [I] Length of the string.
string [O] Null terminated C++ (C) string.
Returns:
Returns 0 if successful. Otherwise it returns the indice (1,...) of the ith character that is not ASCII convertable.

Definition at line 44 of file FortranTypes_CppFortranStrings.cpp.


Generated on Thu Sep 18 12:35:42 2008 for MOOCHO (Single Doxygen Collection) by doxygen 1.3.9.1