Debian Codegraph @

libsrc/ftoc-old/ftocc

raw
1 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 .TYPE Module
3 .IDENTIFICATION ftoc.c
4 .AUTHOR Francois Ochsenbein [ESO-IPG]
5 .LANGUAGE C
6 .KEYWORDS Fortran to C strings
7 .ENVIRONMENT VAX / VMS
8 .COMMENTS
9 Fortran / C interface.
10 These routines allow to convert Fortran strings (blank-filled)
11 to C standards (nul-terminated). It is assumed that the
12 strings are stacked (last in, first out). The size of the stack
13 is defined by the parameter FTOC_STACKSIZE.
14
15 The normal way to use these interfaces is to get first a ``mark''
16 with the routine ftoc_mark, then use repeatidly ftoc_get
17 if strings are needed (and eventually ftoc_cpy to reformat
18 string for FORTRAN usage), and finally ftoc_free(mark)
19 to release the memory allocated.
20
21 A copy in a static area (size FTOC_LOCALSIZE) is done when possible;
22 a dynamic allocation is made if there is not enough room.
23
24
25 .VERSION 1.0 12-Jun-1987: Extracted from OS_FORIF (VAX/VMS)
26 .VERSION 2.0 15-Oct-1990: Generalized
27
28 051111 last modif
29
30 ----------------------------------------------------------------------------*/
31
32 #include <string.h>
33
34 #define FTOC_LOCALSIZE 1024 /* Up to this amount is stored here */
35 #define FTOC_STACKSIZE 32 /* How many strings can be stacked */
36
37 /* The local buffer stores short strings, up to 128 bytes;
38 the upper value of the stack is the index in buffer.
39 */
40
41 #define INTP int
42 #ifdef LinuxAMD64
43 #define INTP long int
44 #endif
45 #ifdef LinuxAlpha
46 #define INTP long int
47 #endif
48
49
50 static char buffer[FTOC_LOCALSIZE];
51 static INTP mindex = 0; /* Index of free space in buffer */
52
53 static char *stack[FTOC_STACKSIZE];
54 static int marker = 0; /* Current index in stack */
55
56 /*==========================================================================*/
57 int ftoc_mark()
58 /*+++
59 .PURPOSE Get a ``marker'' for Fortran to C
60 .RETURNS A number to be used in ftoc_free
61 ---*/
62 {
63 static char err_text[] = "**** ftoc stack full ****\n";
64
65 if (marker < FTOC_STACKSIZE)
66 stack[marker] = (char *)mindex;
67 else write(2, err_text, sizeof(err_text)-1);
68 return(marker++);
69 }
70
71 /*==========================================================================*/
72 int ftoc_free(mark)
73 /*+++
74 .PURPOSE Free the memory allocated since ftoc_mark call
75 .RETURNS 0 / -1
76 ---*/
77 int mark; /* IN: Marker returned from ftoc_mark */
78 {
79 static char err_text[] = "**** ftoc_free: bad argument\n";
80
81 if (mark >= marker) {
82 write(2, err_text, sizeof(err_text)-1);
83 return(-1);
84 }
85 while (--marker > mark) {
86 if (marker < FTOC_STACKSIZE) osmmfree(stack[marker]);
87 }
88 if (marker < FTOC_STACKSIZE) mindex = (INTP) (stack[marker]);
89 return(0);
90 }
91
92 /*==========================================================================*/
93 char *ftoc_get(fs, length, option)
94 /*+++
95 .PURPOSE Convert a FORTRAN string to a C string, in a new piece of memory.
96 .RETURNS C address of string completed with trailing '\0'
97 .METHOD Local copy if string not too long; dynaminc memory allocation
98 otherwise.
99 ---*/
100 char *fs; /* IN: Fortran string */
101 int length; /* IN: The length of the string */
102 int option; /* IN: 1 for suppressing trailing blanks */
103 {
104 char *p, *osmmget();
105 int len;
106
107 /* Count the number of bytes required to store the string */
108 if (option) {
109 for (p = fs + length - 1; (p >= fs) && (*p == ' '); p--) ;
110 len = 1 + (p - fs);
111 }
112 else len = length;
113
114 /* Is it possible to store it locally ? */
115 if ((len < 128) && (len < (FTOC_LOCALSIZE - 1 - mindex))) {
116 p = &buffer[mindex],
117 mindex += len + 1;
118 mindex = (mindex+3) & ~3; /* Make a multiple of 4 */
119 }
120 else if (marker++ < FTOC_STACKSIZE) {
121 p = osmmget(len+1);
122 stack[marker-1] = p;
123 }
124 else p = (char *)0;
125
126 if (p)
127 oscopy(p, fs, len),
128 p[len] = '\0';
129
130 return(p);
131 }
132
133 /*==========================================================================*/
134 int ftoc_cpy(dest, source, length)
135 /*+++
136 .PURPOSE Copy a C string to FORTRAN.
137 .RETURNS The original length of the C string
138 .METHOD Fill with blanks.
139 ---*/
140 char *dest; /* OUT: FORTRAN string */
141 char *source; /* IN: C string */
142 int length; /* IN: Length of FORTRAN string */
143 {
144 int len;
145
146 len = strlen(source);
147 if (len > length) len = length;
148
149 oscopy (dest, source, len);
150 oscfill(dest+len, length-len, ' ');
151
152 return(len);
153 }