1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2005 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2010 Cisco Systems, Inc. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "ompi_config.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "ompi/constants.h"
#include "opal/util/argv.h"
#include "ompi/mpi/f77/f77_strings.h"
/*
* creates a C string from an F77 string
*/
int ompi_fortran_string_f2c(char *fstr, int len, char **cstr)
{
char *end;
int i;
/* Leading and trailing blanks are discarded. */
end = fstr + len - 1;
for (i = 0; (i < len) && (' ' == *fstr); ++i, ++fstr) {
continue;
}
if (i >= len) {
len = 0;
} else {
for (; (end > fstr) && (' ' == *end); --end) {
continue;
}
len = end - fstr + 1;
}
/* Allocate space for the C string. */
if (NULL == (*cstr = (char *) malloc(len + 1))) {
return OMPI_ERR_OUT_OF_RESOURCE;
}
/* Copy F77 string into C string and NULL terminate it. */
if (len > 0) {
strncpy(*cstr, fstr, len);
}
(*cstr)[len] = '\0';
return OMPI_SUCCESS;
}
/*
* Copy a C string into a Fortran string. Note that when Fortran
* copies strings, even if it operates on subsets of the strings, it
* is expected to zero out the rest of the string with spaces. Hence,
* when calling this function, the "len" parameter should be the
* compiler-passed length of the entire string, even if you're copying
* over less than the full string. Specifically:
*
* http://www.ibiblio.org/pub/languages/fortran/ch2-13.html
*
* "Whole operations 'using' only 'part' of it, e.g. assignment of a
* shorter string, or reading a shorter record, automatically pads the
* rest of the string with blanks."
*/
int ompi_fortran_string_c2f(char *cstr, char *fstr, int len)
{
int i;
strncpy(fstr, cstr, len);
for (i = strlen(cstr); i < len; ++i) {
fstr[i] = ' ';
}
return OMPI_SUCCESS;
}
/*
* creates a C argument vector from an F77 array of strings
* (terminated by a blank string)
*/
int ompi_fortran_argv_f2c(char *array, int string_len, int advance,
char ***argv)
{
int err, argc = 0;
char *cstr;
/* Fortran lines up strings in memory, each delimited by \0. So
just convert them until we hit an extra \0. */
*argv = NULL;
while (1) {
if (OMPI_SUCCESS != (err = ompi_fortran_string_f2c(array, string_len,
&cstr))) {
opal_argv_free(*argv);
return err;
}
if ('\0' == *cstr) {
break;
}
if (OMPI_SUCCESS != (err = opal_argv_append(&argc, argv, cstr))) {
opal_argv_free(*argv);
return err;
}
free(cstr);
array += advance;
}
return OMPI_SUCCESS;
}
/*
* Creates a set of C argv arrays from an F77 array of argv's. The
* returned arrays need to be freed by the caller.
*/
int ompi_fortran_multiple_argvs_f2c(int num_argv_arrays, char *array,
int string_len, char ****argv)
{
char ***argv_array;
int i;
char *current_array = array;
int ret;
argv_array = (char ***) malloc (num_argv_arrays * sizeof(char **));
for (i = 0; i < num_argv_arrays; ++i) {
ret = ompi_fortran_argv_f2c(current_array, string_len,
string_len * num_argv_arrays,
&argv_array[i]);
if (OMPI_SUCCESS != ret) {
return ret;
}
current_array += string_len;
}
*argv = argv_array;
return OMPI_SUCCESS;
}
|