1 | /*GRFILEIO -- Fast low-level UNIX I/O routines |
---|
2 | * + |
---|
3 | * |
---|
4 | * GRFILEIO is a set of functions that makes fast, low-level Unix I/O routines |
---|
5 | * available to a Fortran program. |
---|
6 | * |
---|
7 | *------- |
---|
8 | * 2-Dec-92 - fastio.c: John L. Lillibridge, NOAA/NOS/OES Geosciences Lab |
---|
9 | * 11-Nov-93 - Addition of seekf and warning by Remko Scharroo, DUT/SSR&T |
---|
10 | * 17-May-94 - Nice manual |
---|
11 | * 13-Oct-94 - Bits not required by PGPLOT stripped out; routine names |
---|
12 | * changed [TJP]. |
---|
13 | * 09-Nov-94 - Tidied and ported to Cray [mcs] (untested). |
---|
14 | * 10-Nov-94 - Added GRFCH() routine to write FORTRAN CHARACTER sub-strings. |
---|
15 | * 19-Jun-95 - File name "-" means stdout. |
---|
16 | *------- |
---|
17 | */ |
---|
18 | |
---|
19 | #include <stdlib.h> |
---|
20 | #include <stdio.h> |
---|
21 | #include <sys/types.h> |
---|
22 | #include <string.h> |
---|
23 | #include <unistd.h> |
---|
24 | #include <fcntl.h> |
---|
25 | |
---|
26 | #ifdef PG_PPU |
---|
27 | #define GROFIL grofil_ |
---|
28 | #define GRWFIL grwfil_ |
---|
29 | #define GRCFIL grcfil_ |
---|
30 | #define GRWFCH grwfch_ |
---|
31 | #else |
---|
32 | #define GROFIL grofil |
---|
33 | #define GRWFIL grwfil |
---|
34 | #define GRCFIL grcfil |
---|
35 | #define GRWFCH grwfch |
---|
36 | #endif |
---|
37 | |
---|
38 | /* |
---|
39 | **&GROFIL -- Open file for writing with GRFILEIO |
---|
40 | *+ |
---|
41 | * FUNCTION GROFIL (FNAME) |
---|
42 | * INTEGER GROFIL |
---|
43 | * CHARACTER*(*) FNAME |
---|
44 | * |
---|
45 | * Opens file FNAME for writing. |
---|
46 | * GROFIL returns the file descriptor for use in subsequent calls to |
---|
47 | * grwfil or grcfil. If GROFIL is negative, an error occurred while |
---|
48 | * opening the file. |
---|
49 | * |
---|
50 | ** |
---|
51 | * Usage: |
---|
52 | * |
---|
53 | * FD = GROFIL ('output_file') |
---|
54 | * CALL GRWFIL (FD, 4, ARRAY) |
---|
55 | * |
---|
56 | * Arguments: |
---|
57 | * FNAME (input) : File name of the input or output file |
---|
58 | * GROFIL (output) : Contains the file descriptor on return. If GROFIL < 0 |
---|
59 | * an error occurred while opening the file. |
---|
60 | *- |
---|
61 | */ |
---|
62 | int GROFIL(fname, fname_len) |
---|
63 | char *fname; |
---|
64 | int fname_len; |
---|
65 | { |
---|
66 | char *name = fname; /* C pointer to FORTRAN string */ |
---|
67 | int slen = fname_len; /* Length of the FORTRAN string */ |
---|
68 | char *buff=0; /* Dynamically allocated copy of name[] */ |
---|
69 | int fd = -1; /* File descriptor to be returned */ |
---|
70 | /* |
---|
71 | * Determine how long the FORTRAN string is by searching for the last |
---|
72 | * non-blank character in the string. |
---|
73 | */ |
---|
74 | while(slen>0 && name[slen-1]==' ') |
---|
75 | slen--; |
---|
76 | /* |
---|
77 | * Dynamically allocate a buffer to copy the FORTRAN string into. |
---|
78 | */ |
---|
79 | buff = (char *) malloc((slen+1) * sizeof(char)); |
---|
80 | if(buff) { |
---|
81 | /* |
---|
82 | * Make a C string copy of the FORTRAN string. |
---|
83 | */ |
---|
84 | strncpy(buff, name, slen); |
---|
85 | buff[slen] = '\0'; |
---|
86 | /* |
---|
87 | * Check for stdout. |
---|
88 | */ |
---|
89 | if (slen == 1 && buff[0] == '-') { |
---|
90 | fd = 1; |
---|
91 | } else { |
---|
92 | /* |
---|
93 | * Open the file and return its descriptor. |
---|
94 | */ |
---|
95 | fd = open(buff, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
---|
96 | } |
---|
97 | free(buff); |
---|
98 | } else { |
---|
99 | fprintf(stderr, "grofil: Insufficient memory\n"); |
---|
100 | }; |
---|
101 | return fd; |
---|
102 | } |
---|
103 | |
---|
104 | /* |
---|
105 | **&GRCFIL -- Close file from GRFILEIO access |
---|
106 | *+ |
---|
107 | * FUNCTION GRCFIL (FD) |
---|
108 | * INTEGER GRCFIL (FD) |
---|
109 | * |
---|
110 | * Closes the file with descriptor FD from GRFILEIO access. GRCFIL returns |
---|
111 | * 0 when properly closed. Otherwise, use PERRORF to report the error. |
---|
112 | * |
---|
113 | * Usage: |
---|
114 | * IOS = GRCFIL (FD) |
---|
115 | * or: |
---|
116 | * CALL GRCFIL (FD) |
---|
117 | * |
---|
118 | * In the last case the return code is ignored. |
---|
119 | * |
---|
120 | * Arguments: |
---|
121 | * FD (input) : File descriptor returned by GROFIL. |
---|
122 | * GRCFIL (output) : Error code or 0 on proper closing. |
---|
123 | *- |
---|
124 | */ |
---|
125 | int GRCFIL(fd) |
---|
126 | int *fd; |
---|
127 | { |
---|
128 | if ((*fd) == 1) { |
---|
129 | return 0; |
---|
130 | } else{ |
---|
131 | return close(*fd); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | /* |
---|
136 | **&GRWFIL -- GRFILEIO write routine |
---|
137 | *+ |
---|
138 | * FUNCTION GRWFIL (FD, NBYTE, BUFFER) |
---|
139 | * INTEGER FD, NBYTE, GRWFIL |
---|
140 | * BYTE BUFFER(NBYTE) |
---|
141 | * |
---|
142 | * Writes NBYTE bytes into the file associated by descriptor FD (which is |
---|
143 | * returned by the GROFIL call. The array BUFFER contains the data that has |
---|
144 | * to be written, but can (of course) also be associated with any other |
---|
145 | * string, scalar, or n-dimensional array. |
---|
146 | * The function returns the number of bytes actually written in GRWFIL. If |
---|
147 | * GRWFIL < 0, a write error occurred. |
---|
148 | * |
---|
149 | * Arguments: |
---|
150 | * FD (input) : File descriptor returned by GROFIL |
---|
151 | * NBYTE (input) : Number of bytes to be written |
---|
152 | * BUFFER (input) : Buffer containing the bytes that have to be written |
---|
153 | * GRWFIL (output) : Number of bytes written, or (if negative) error code. |
---|
154 | *- |
---|
155 | */ |
---|
156 | int GRWFIL(fd, nbytes, buf) |
---|
157 | int *fd, *nbytes; |
---|
158 | char *buf; |
---|
159 | { |
---|
160 | return write(*fd, (void *) buf, *nbytes); |
---|
161 | } |
---|
162 | |
---|
163 | /* |
---|
164 | **&GRWFCH -- GRFILEIO write FORTRAN character STRING routine |
---|
165 | *+ |
---|
166 | * FUNCTION GRWFCH (FD, STRING) |
---|
167 | * INTEGER FD, GRWFCH |
---|
168 | * CHARACTER*(*) STRING |
---|
169 | * |
---|
170 | * Writes NBYTE bytes into the file associated by descriptor FD (which is |
---|
171 | * returned by the GROFIL call). The string STRING contains the data that has |
---|
172 | * to be written. |
---|
173 | * The function returns the number of bytes actually written in GRWFCH. If |
---|
174 | * GRWFCH < 0, a write error occurred. |
---|
175 | * |
---|
176 | * Arguments: |
---|
177 | * FD (input) : File descriptor returned by GROFIL |
---|
178 | * STRING (input) : String containing the characterst to be written |
---|
179 | * GRWFCH (output) : Number of bytes written, or (if negative) error code. |
---|
180 | *- |
---|
181 | */ |
---|
182 | int GRWFCH(fd, buf, buf_len) |
---|
183 | int *fd; |
---|
184 | char *buf; |
---|
185 | int buf_len; |
---|
186 | { |
---|
187 | return write(*fd, (void *) buf, buf_len); |
---|
188 | } |
---|