1 | |
---|
2 | /* file FILEOPEN.C |
---|
3 | * ========== |
---|
4 | * |
---|
5 | * version 5, 2-May-2006 |
---|
6 | * |
---|
7 | * Opens files. Uses logical names |
---|
8 | * K. Stammler, 4-Jan-92 |
---|
9 | */ |
---|
10 | |
---|
11 | |
---|
12 | /* |
---|
13 | * |
---|
14 | * SeismicHandler, seismic analysis software |
---|
15 | * Copyright (C) 1992, Klaus Stammler, Federal Institute for Geosciences |
---|
16 | * and Natural Resources (BGR), Germany |
---|
17 | * |
---|
18 | * This program is free software; you can redistribute it and/or modify |
---|
19 | * it under the terms of the GNU General Public License as published by |
---|
20 | * the Free Software Foundation; either version 2 of the License, or |
---|
21 | * (at your option) any later version. |
---|
22 | * |
---|
23 | * This program is distributed in the hope that it will be useful, |
---|
24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
26 | * GNU General Public License for more details. |
---|
27 | * |
---|
28 | * You should have received a copy of the GNU General Public License |
---|
29 | * along with this program; if not, write to the Free Software |
---|
30 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
31 | * |
---|
32 | */ |
---|
33 | |
---|
34 | |
---|
35 | #include <stdio.h> |
---|
36 | #include <string.h> |
---|
37 | #include "basecnst.h" |
---|
38 | #include BC_SYSBASE |
---|
39 | #ifdef BC_INC_STDLIB |
---|
40 | #include BC_INC_STDLIB |
---|
41 | #endif |
---|
42 | #include "fousrdef.h" |
---|
43 | #include "foerrors.h" |
---|
44 | |
---|
45 | |
---|
46 | /* constants */ |
---|
47 | #define MAXSTRLTH 300 |
---|
48 | #define MAXLOGNAME 20 |
---|
49 | #define MAXEQUIV 5 |
---|
50 | #define LNONE -1 |
---|
51 | |
---|
52 | |
---|
53 | /* global variables */ |
---|
54 | static char fov_log[MAXLOGNAME][MAXSTRLTH+1]; /* logical name table */ |
---|
55 | static int fov_offset[MAXLOGNAME][MAXEQUIV]; /* name offsets */ |
---|
56 | static unsigned fov_logcnt; /* number of defined names */ |
---|
57 | static BOOLEAN fov_init=FALSE; /* initialisation done */ |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | /* prototypes of local routines */ |
---|
62 | |
---|
63 | |
---|
64 | /*----------------------------------------------------------------------*/ |
---|
65 | |
---|
66 | |
---|
67 | void fo_readtable( char table[], STATUS *status ) |
---|
68 | |
---|
69 | /* initializes global variables |
---|
70 | * |
---|
71 | * parameters of routine |
---|
72 | * char table[]; input; logical name table |
---|
73 | * STATUS *status; output; return status |
---|
74 | */ |
---|
75 | { |
---|
76 | /* local variables */ |
---|
77 | unsigned i, j; /* counters */ |
---|
78 | char *ls; /* pointer to logical name string */ |
---|
79 | FILE *fp; /* pointer to table file */ |
---|
80 | BOOLEAN inword; /* is in a word */ |
---|
81 | |
---|
82 | /* executable code */ |
---|
83 | |
---|
84 | for (i=0; i<MAXLOGNAME; i++) |
---|
85 | for (j=0; j<MAXEQUIV; j++) |
---|
86 | fov_offset[i][j] = LNONE; |
---|
87 | |
---|
88 | fov_logcnt = 0; |
---|
89 | fov_init = TRUE; |
---|
90 | |
---|
91 | fp = fopen( table, "r" ); |
---|
92 | if (fp == NULL) { |
---|
93 | *status = FOE_FOPNIN; |
---|
94 | fov_init = FALSE; |
---|
95 | return; |
---|
96 | } /*endif*/ |
---|
97 | |
---|
98 | while (fgets(fov_log[fov_logcnt],MAXSTRLTH,fp) != NULL) { |
---|
99 | ls = fov_log[fov_logcnt]; |
---|
100 | if (*ls > '!') { /* accept logical name */ |
---|
101 | i = j = 0; |
---|
102 | inword = TRUE; |
---|
103 | while (ls[i] != '\0') { |
---|
104 | if (ls[i] == ' ' || ls[i] == '\n') { |
---|
105 | ls[i] = '\0'; |
---|
106 | inword = FALSE; |
---|
107 | } else { |
---|
108 | if (!inword) { |
---|
109 | fov_offset[fov_logcnt][j++] = i; |
---|
110 | inword = TRUE; |
---|
111 | } /*endif*/ |
---|
112 | } /*endif*/ |
---|
113 | i++; |
---|
114 | } /*endwhile*/ |
---|
115 | fov_logcnt++; |
---|
116 | } /*endif*/ |
---|
117 | } /*endif*/ |
---|
118 | |
---|
119 | fclose( fp ); |
---|
120 | |
---|
121 | } /* end of fo_readtable */ |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | /*----------------------------------------------------------------------*/ |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | FILE *fo_fopen( char fname[], char access[] ) |
---|
130 | |
---|
131 | /* opens a file, using the paths of the actual logical name table |
---|
132 | * |
---|
133 | * parameters of routine |
---|
134 | * char fname[]; input; name of file to be opened |
---|
135 | * char access[]; input; access string, as in usual fopen |
---|
136 | * returns file pointer |
---|
137 | */ |
---|
138 | { |
---|
139 | /* local variables */ |
---|
140 | char str[MAXSTRLTH+1]; /* translated filename */ |
---|
141 | char logname[BC_FILELTH+1];/* logical name */ |
---|
142 | int slth; /* string length */ |
---|
143 | int pos; /* string position */ |
---|
144 | int i, j; /* counters */ |
---|
145 | FILE *fp; /* file pointer */ |
---|
146 | char *cptr; /* pointer to char */ |
---|
147 | |
---|
148 | /* executable code */ |
---|
149 | |
---|
150 | if (!fov_init) return fopen( fname, access ); |
---|
151 | |
---|
152 | /* search colon */ |
---|
153 | pos = 0; |
---|
154 | while (fname[pos] != '\0' && fname[pos] != ':') |
---|
155 | pos++; |
---|
156 | if (fname[pos] == '\0') /* no colon */ |
---|
157 | return fopen( fname, access ); |
---|
158 | |
---|
159 | if (pos > BC_FILELTH) return NULL; |
---|
160 | strncpy( logname, fname, pos ); |
---|
161 | logname[pos++] = '\0'; |
---|
162 | slth = (int)strlen( fname ) - pos; |
---|
163 | /* the filename starts at fname+pos and contains slth chars */ |
---|
164 | |
---|
165 | /* find logical name and try to open file */ |
---|
166 | i = 0; |
---|
167 | while (fov_offset[i][0] != LNONE) { |
---|
168 | if (strcmp(fov_log[i],logname) == 0) { /* name found */ |
---|
169 | /* try all definitions */ |
---|
170 | j = 0; |
---|
171 | while (fov_offset[i][j] != LNONE) { |
---|
172 | strcpy( str, fov_log[i]+fov_offset[i][j] ); |
---|
173 | if (strlen(str)+slth > MAXSTRLTH) return NULL; |
---|
174 | strcat( str, fname+pos ); |
---|
175 | fp = fopen( str, access ); |
---|
176 | if (fp != NULL) return fp; |
---|
177 | j++; |
---|
178 | } /*endwhile*/ |
---|
179 | } /*endif*/ |
---|
180 | i++; |
---|
181 | } /*endwhile*/ |
---|
182 | |
---|
183 | cptr = getenv( logname ); |
---|
184 | if (cptr != NULL) { |
---|
185 | if (strlen(cptr)+slth > MAXSTRLTH-1) return NULL; |
---|
186 | strcpy( str, cptr ); |
---|
187 | strcat( str, "/" ); |
---|
188 | strcat( str, fname+pos ); |
---|
189 | fp = fopen( str, access ); |
---|
190 | if (fp != NULL) return fp; |
---|
191 | } /*endif*/ |
---|
192 | |
---|
193 | /* not found, open without translation */ |
---|
194 | return fopen( fname, access ); |
---|
195 | |
---|
196 | } /* end of fo_fopen */ |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | /*----------------------------------------------------------------------*/ |
---|
201 | |
---|
202 | |
---|
203 | |
---|
204 | void fo_translate( char in[], BOOLEAN first, int maxlth, char out[], |
---|
205 | int *result ) |
---|
206 | |
---|
207 | /* If first=TRUE the filename is translated using logical name table. |
---|
208 | * Otherwise the parameter "in" is ignored and the next possible |
---|
209 | * translation of the previous call is returned. "*result" can have |
---|
210 | * the values FOC_NOTMATCH (no matching logical name found, -> |
---|
211 | * out=in), FOC_ANOTHER (logical name was translated and it exists |
---|
212 | * another translation for the same logical name) and FOC_LAST (the |
---|
213 | * logical name was translated, there exist no more equivalences). |
---|
214 | * If the resulting string is too long FOC_OVFL is returned. |
---|
215 | * |
---|
216 | * parameters of routine |
---|
217 | * char in[]; input; input filename |
---|
218 | * BOOLEAN first; input; first equivalence or not |
---|
219 | * int maxlth; input; maximum length of output string |
---|
220 | * char out[]; output; translated filename |
---|
221 | * int *result; output; status message |
---|
222 | */ |
---|
223 | { |
---|
224 | /* local variables */ |
---|
225 | char logname[BC_FILELTH+1]; /* logical name */ |
---|
226 | int pos; /* string position */ |
---|
227 | static int tcnt; /* translation counter */ |
---|
228 | static int lcnt; /* logical name counter */ |
---|
229 | static int slth; /* string length */ |
---|
230 | static char fname[BC_FILELTH+1]; /* rest of filename */ |
---|
231 | |
---|
232 | /* executable code */ |
---|
233 | |
---|
234 | if (!fov_init) { |
---|
235 | if (strlen(in) > maxlth) { |
---|
236 | *result = FOC_OVFL; |
---|
237 | return; |
---|
238 | } /*endif*/ |
---|
239 | strcpy( out, in ); |
---|
240 | *result = FOC_NOMATCH; |
---|
241 | return; |
---|
242 | } /*endif*/ |
---|
243 | |
---|
244 | if (!first) { |
---|
245 | if (++tcnt >= MAXEQUIV || fov_offset[lcnt][tcnt] == LNONE) { |
---|
246 | *result = FOC_NOMATCH; |
---|
247 | return; |
---|
248 | } /*endif*/ |
---|
249 | if (strlen(fov_log[lcnt]+fov_offset[lcnt][tcnt])+slth > maxlth) { |
---|
250 | *result = FOC_OVFL; |
---|
251 | return; |
---|
252 | } /*endif*/ |
---|
253 | strcpy( out, fov_log[lcnt]+fov_offset[lcnt][tcnt] ); |
---|
254 | strcat( out, fname ); |
---|
255 | if (tcnt < MAXEQUIV-1 && fov_offset[lcnt][tcnt+1] != LNONE) { |
---|
256 | *result = FOC_ANOTHER; |
---|
257 | } else { |
---|
258 | *result = FOC_LAST; |
---|
259 | } /*endif*/ |
---|
260 | return; |
---|
261 | } /*endif*/ |
---|
262 | |
---|
263 | /* search colon */ |
---|
264 | pos = 0; |
---|
265 | while (in[pos] != '\0' && in[pos] != ':') |
---|
266 | pos++; |
---|
267 | if (in[pos] == '\0') { /* no colon */ |
---|
268 | if (strlen(in) > maxlth) { |
---|
269 | *result = FOC_OVFL; |
---|
270 | return; |
---|
271 | } /*endif*/ |
---|
272 | strcpy( out, in ); |
---|
273 | *result = FOC_NOMATCH; |
---|
274 | return; |
---|
275 | } /*endif*/ |
---|
276 | |
---|
277 | if (pos > BC_FILELTH) {*result = FOC_OVFL; return;} |
---|
278 | strncpy( logname, in, pos ); |
---|
279 | logname[pos++] = '\0'; |
---|
280 | slth = (int)strlen( in ) - pos; |
---|
281 | if (slth > BC_FILELTH) { |
---|
282 | *result = FOC_OVFL; |
---|
283 | return; |
---|
284 | } /*endif*/ |
---|
285 | strcpy( fname, in+pos ); |
---|
286 | |
---|
287 | /* find logical name and try to open file */ |
---|
288 | lcnt = 0; |
---|
289 | while (fov_offset[lcnt][0] != LNONE) { |
---|
290 | if (strcmp(fov_log[lcnt],logname) == 0) { /* name found */ |
---|
291 | tcnt = 0; |
---|
292 | if (strlen(fov_log[lcnt]+fov_offset[lcnt][0])+slth > maxlth) { |
---|
293 | *result = FOC_OVFL; |
---|
294 | return; |
---|
295 | } /*endif*/ |
---|
296 | strcpy( out, fov_log[lcnt]+fov_offset[lcnt][0] ); |
---|
297 | strcat( out, fname ); |
---|
298 | if (tcnt < MAXEQUIV-1 && fov_offset[lcnt][tcnt+1] != LNONE) { |
---|
299 | *result = FOC_ANOTHER; |
---|
300 | } else { |
---|
301 | *result = FOC_LAST; |
---|
302 | } /*endif*/ |
---|
303 | return; |
---|
304 | } /*endif*/ |
---|
305 | lcnt++; |
---|
306 | } /*endwhile */ |
---|
307 | |
---|
308 | /* not found */ |
---|
309 | if (strlen(in) > maxlth) { |
---|
310 | *result = FOC_OVFL; |
---|
311 | return; |
---|
312 | } /*endif*/ |
---|
313 | strcpy( out, in ); |
---|
314 | *result = FOC_NOMATCH; |
---|
315 | |
---|
316 | } /* end of fo_translate */ |
---|
317 | |
---|
318 | |
---|
319 | |
---|
320 | /*----------------------------------------------------------------------*/ |
---|
321 | |
---|