MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes
FortranTypes Namespace Reference

Classes

class  InvalidFileNameException
 Thrown if the file name is not a valid ASCII string. More...
 
class  OpenException
 Thrown if the open operation fails. More...
 

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

Open a Fortran file.

enum  EOpenStatus { OPEN_OLD = 0, OPEN_NEW = 1, OPEN_SCRATCH = 2, OPEN_UNKNOWN = 3 }
 
enum  EOpenForm { OPEN_FORMATTED = 0, OPEN_UNFORMATTED = 1 }
 
enum  EOpenBlank { OPEN_NULL = 0, OPEN_ZERO = 1 }
 
enum  EOpenAccess { OPEN_SEQUENTIAL = 0, OPEN_DIRECT = 1 }
 
void f_open_file (const f_int iunit, const char file[], EOpenStatus status=OPEN_UNKNOWN, EOpenForm form=OPEN_FORMATTED, EOpenBlank blank=OPEN_NULL, EOpenAccess access=OPEN_SEQUENTIAL, f_int recl=-1)
 
void f_close_file (const f_int iunit, bool keep=true)
 

Enumeration Type Documentation

Enumerator
OPEN_OLD 
OPEN_NEW 
OPEN_SCRATCH 
OPEN_UNKNOWN 

Definition at line 55 of file FortranTypes_f_open_file.hpp.

Enumerator
OPEN_FORMATTED 
OPEN_UNFORMATTED 

Definition at line 58 of file FortranTypes_f_open_file.hpp.

Enumerator
OPEN_NULL 
OPEN_ZERO 

Definition at line 60 of file FortranTypes_f_open_file.hpp.

Enumerator
OPEN_SEQUENTIAL 
OPEN_DIRECT 

Definition at line 62 of file FortranTypes_f_open_file.hpp.

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.
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.
void FortranTypes::f_open_file ( const f_int  iunit,
const char  file[],
EOpenStatus  status = OPEN_UNKNOWN,
EOpenForm  form = OPEN_FORMATTED,
EOpenBlank  blank = OPEN_NULL,
EOpenAccess  access = OPEN_SEQUENTIAL,
f_int  recl = -1 
)

Open a Fortran file given its name and unit number.

If successful #iunit# is returned for the opened file.

The standard options to Fortran OPEN(...) are included. The only mandatory option to set is for the file name in FILE.

The length of the file name must be <= 100 characters long.

Note that all of the options are optional but if you set access == OPEN_DIRECT you must set recl to some value greater than zero. See Metcalf, 1990, p. 127.

If #file# is not a valid ASCII string then the exception InvalidFileNameException will be thrown

If the file could not be opened for some reason then the exception OpenException will be thrown.

void FortranTypes::f_close_file ( const f_int  iunit,
bool  keep = true 
)

Close a Fortran file given its unit number.

If successful #iunit# is returned for the opened file.

The standard options to Fortran OPEN(...) are included. The only mandatory option to set is for the file name in FILE.

The length of the file name must be <= 100 characters long.

Note that all of the options are optional but if you set access == OPEN_DIRECT you must set recl to some value greater than zero. See Metcalf, 1990, p. 127.

If #file# is not a valid ASCII string then the exception InvalidFileNameException will be thrown

If the file could not be opened for some reason then the exception OpenException will be thrown.