-
Notifications
You must be signed in to change notification settings - Fork 9
/
sc2for.c
executable file
·111 lines (88 loc) · 3.63 KB
/
sc2for.c
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
#include "xvmaininc.h"
#include "ftnbridge.h"
#include "applic.h"
#include "rtlintproto.h"
#if FORSTR_METHOD_A
#include <descrip>
#endif
#if FORSTR_METHOD_D
#include <fortran.h>
#endif
/************************************************************************/
/* sc2for transforms an standard C null-terminated string to an output */
/* Fortran string. It is the inverse of sfor2c. */
/* */
/* See sfor2c.c for comments on how the various Fortran string types */
/* are handled, the three API's, and detailed descriptions of the */
/* parameters. The same rules apply for macros, etc. */
/* */
/* Max_length is optional. If it's not used, it should be passed in */
/* as 0. It is an alternate maximum length if one should happen to be */
/* passed in as another parameter. The actual maximum Fortran length */
/* is the minimum of the passed in max_length (if present) and the */
/* length automatically passed with the Fortran string. */
/************************************************************************/
/* OLD API */
void sc2for(
char *c_string, /* In: string in C format */
int max_length, /* In: optional max size of for_string */
/* buffer (length used is min of */
/* this and the Fortran string len */
char *for_string, /* Out: the Fortran string argument */
void *argptr, /* In: ptr to original argument list */
int nargs, /* In: total number of arguments */
int argno, /* In: this argument number (start at 1)*/
int strno /* In: this string number (start at 1) */
)
{
int which = 0;
v2_sc2for(c_string, max_length, for_string, argptr, nargs, argno, strno,
NULL, &which);
}
/* NEW API */
void v2_sc2for(
char *c_string, /* In: string in C format */
int max_length, /* In: optional max size of for_string */
/* buffer (length used is min of */
/* this and the Fortran string len */
char *for_string, /* Out: the Fortran string argument */
void *argptr, /* In: ptr to original argument list */
int nargs, /* In: total number of arguments */
int argno, /* In: this argument number (start at 1)*/
int strno, /* In: this string number (start at 1) */
va_list *valist, /* In/Out: stdarg ptr past arg list (see*/
/* description in sfor2c.c). */
/* Modified by this routine. */
int *which /* In/Out: ptr to int indicating which */
/* strlen is pointed at by valist */
)
{
int i, len;
int null_term = FALSE;
char *output_string; /* the actual text address */
int fortran_length; /* the length Fortran claims */
/* It's so simple this way: relegate all the crap to ptr/len. Sigh. */
output_string = v2_sfor2ptr(for_string);
fortran_length=v2_sfor2len(for_string, argptr, nargs, argno, strno,
valist, which);
/************************************************************************/
if (fortran_length == 0) /* whoops */
fortran_length = max_length;
if (max_length > 0)
fortran_length = MIN(fortran_length, max_length);
if (fortran_length <= 0) /* sanity check on the length */
fortran_length = strlen(c_string)+1;
/* Now that we have the string pointer and the length, copy the string */
/* over to the destination, using either blank fill or null termination */
len = MIN((int) strlen(c_string), fortran_length); /* don't copy too much! */
strncpy(output_string, c_string, len);
if (null_term) { /* Null-terminate if there's room */
if (len < fortran_length)
output_string[len] = '\0';
}
else { /* Blank-fill the string */
for (i=len; i<fortran_length; i++)
output_string[i] = ' ';
}
return;
}