Debian Codegraph @

libsrc/st/cgna.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2007 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*+++++++++++++++++++ CGN interfaces part A +++++++++++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION Module CGNA
31 .COMMENTS
32 holds CLEANF, INDEXC, INDEXS
33 .AUTHOR K. Banse ESO - Garching
34 .KEYWORDS
35 name translation, parsing, filling
36 .ENVIRONMENT VMS and UNIX
37
38 .VERSION [1.40] 870715: C-version
39 070523 last modif
40
41 -----------------------------------------------------------------------------*/
42
43
44 #include <stdlib.h>
45 #include <fsydef.h>
46 #include <fileexts.h>
47 #include <osfile.h>
48
49
50 /*
51
52 */
53
54 int CGN_CLEANF(infile,deftypno,outfile,lout,namtype,ext_flg)
55
56 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57 .PURPOSE
58 translate file name and append default type, if required.
59 also indicate, if extracted file is wanted
60 cleaned filename is terminated by \0
61 .ALGORITHM
62 straight forward via CGN_INDEXC
63 we have to use FSYDEF.H, which defines file-system dependant values !!
64 .RETURNS
65 return status = 0, if all o.k.
66 = -1, if bad syntax
67 ------------------------------------------------------------------------*/
68
69 char *infile; /* IN : (complete) file name (terminated by \0) */
70 int deftypno; /* IN : default_type_no. 0 = file type will decide
71 1 = image (.bdf),
72 3 = table (.tbl), 4 = fit file (.fit)
73 5 = logfile (.log),
74 6 = procedure (.prg),
75 7 = catalog (.cat) */
76 int lout; /* IN : max length of outfile */
77 char *outfile; /* OUT: "cleaned" file name */
78 int *namtype; /* OUT: type of frame:
79 1 = image (.bdf), 2 = any (.any)
80 3 = table (.tbl), 4 = fit file (.fit)
81 5 = logfile (.log), 6 = procedure (.prg)
82 7 = catalog (.cat), 8 = test (.tst)
83 9 = executable (.exe)
84 0 = if something wrong... */
85 int *ext_flg; /* OUT: = 0, if none;
86 > 0 if index of '[' or '@'
87 < 0 if index of [extens] */
88
89
90 {
91 int k, tt, inlen, typeno;
92 register int nr;
93
94 char work[200];
95 char *cpntr;
96 register char cr;
97
98
99
100
101 *ext_flg = 0;
102 *namtype = 0;
103
104 cr = *infile;
105 if ((cr == ' ') || (cr == '\0')) return (-2);
106
107 if ((deftypno < 1) || (deftypno > 7))
108 typeno = 0;
109 else
110 typeno = deftypno - 1;
111
112 inlen = (int)strlen(infile);
113 k = inlen - 1;
114
115
116 /* check if name is enclosed in special char. XFNAMC (currently = ")
117 then we just strip them off ... */
118
119 if (cr == XFNAMC)
120 {
121 if (infile[k] == XFNAMC) /* it's "name" */
122 {
123 if (lout < k)
124 k = lout - 1;
125 else
126 k --; /* new length of string */
127 (void) memcpy(outfile,&infile[1],(size_t)k);
128 outfile[k] = '\0';
129 *namtype = 99; /* we don't check ... */
130 return (0);
131 }
132 }
133
134
135 /* first look for file[... : ...] or file[extens] */
136
137 cpntr = infile;
138 if (infile[k] == ']')
139 {
140 for (nr=(k-1); nr>0; nr--)
141 {
142 if (infile[nr] == '[')
143 {
144 k = nr;
145 goto ext_yes;
146 }
147 }
148 return (-1);
149
150 ext_yes:
151 *ext_flg = k;
152 (void) memcpy(work,infile,(size_t)k);
153 work[k] = '\0';
154 cpntr = work;
155
156 for (nr=(k+1); nr<inlen; nr++)
157 {
158 if (infile[nr] == ':') goto get_logname;
159 }
160 *ext_flg = -k; /* we have a FITS extension */
161 }
162
163 /* now check for file@plane/line/pixel */
164
165 else
166 {
167 k = CGN_INDEXC(infile,'@');
168 if (k > 0)
169 {
170 *ext_flg = k;
171 (void) memcpy(work,infile,(size_t)k);
172 work[k] = '\0';
173 cpntr = work;
174 }
175 }
176
177 get_logname:
178 CGN_LOGNAM(cpntr,outfile,lout); /* returns outfile with \0 */
179
180 tt = CGN_APPNDTYP(outfile,typeno);
181
182 if (deftypno == 0)
183 {
184 if (tt > -1)
185 {
186 cpntr = outfile + tt;
187 for (nr=0; nr<9; nr++)
188 {
189 if (strcmp(cpntr,FSY_DEFPNTR[nr]) == 0)
190 {
191 *namtype = nr + 1;
192 return(0);
193 }
194 }
195 }
196 *namtype = 1; /* default to images */
197 }
198
199 else
200 *namtype = deftypno;
201
202
203 return (0);
204 }
205
206 /*
207
208 */
209
210 #ifdef __STDC__
211 int CGN_APPNDTYP(char *s, int typeno)
212 #else
213 int CGN_APPNDTYP(s, typeno)
214 char *s;
215 int typeno;
216 #endif
217
218 /* ++++++++++++++++++++++++++++++++++++++++++++++
219
220 a) append correct file type if not set yet
221 b) return index of .filetype if any
222
223 ----------------------------------------------- */
224
225 {
226 int k, tt, inlen;
227 register int nr;
228
229 char work[24], *defpntr;
230
231
232
233
234 k = (int) strlen(s);
235 tt = -1;
236 for (nr=k; nr>0; nr--)
237 {
238 if (s[nr] == FSY_TYPMARK) /* now, look for type */
239 {
240 tt = nr; /* we found no dir_specs before */
241 break;
242 }
243 else if (s[nr] == FSY_DIREND) /* and directory endmarker */
244 break;
245 }
246
247 if (tt < 0)
248 {
249 defpntr = FSY_DEFPNTR[typeno]; /* point to correct deftype */
250 if (KIWORDS[OFF_AUX+12] == 1)
251 { /* we work with FITS, */
252 work[0] = '.';
253 if (typeno == 0) /* so look for default */
254 { /* image/table types */
255 (void) SCKGETC("MID$TYPES",1,8,&inlen,work+1);
256 defpntr = work;
257 }
258 else if (typeno == 2)
259 {
260 (void) SCKGETC("MID$TYPES",9,8,&inlen,work+1);
261 defpntr = work;
262 }
263 }
264
265 (void) strcpy(s+k,defpntr); /* append def type to file */
266 }
267
268 return tt;
269 }
270 /*
271
272 */
273
274 #ifdef __STDC__
275 int CGN_INDEXC(char *s, char t)
276 #else
277 int CGN_INDEXC(s,t)
278 /*++++++++++++++++++++++++++++++++++++++++++++++++++
279 .PURPOSE
280 returns position of single character in input string
281 .RETURN
282 returns position of single character in input string,
283 -length(s) if not there
284 --------------------------------------------------*/
285
286 char *s; /* input string */
287 char t; /* test character */
288 #endif
289
290 {
291 register int i;
292
293 char *cp;
294
295
296 cp = s;
297 for (i=0; *cp != '\0'; i++)
298 {
299 if (t == *cp++) return (i);
300 }
301
302 return (-1); /* not found, return length of string (negative) */
303 }
304
305 /*
306
307 */
308
309 int CGN_INDEXS(s,t)
310
311 /*++++++++++++++++++++++++++++++++++++++++++++++++++
312 .PURPOSE
313 find position of substring in input string.
314
315 .RETURN
316 returns index of substring in input string (= 0,1,2,...),
317 -length(s) if not there
318 --------------------------------------------------*/
319
320 char *s; /* input string */
321 char *t; /* substring */
322
323 {
324 char *ptr;
325
326
327
328 /* library routine strstr() returns pointer to substring or NULL */
329
330 ptr = strstr(s,t);
331
332 if (ptr == (char *) 0)
333 return (-1);
334 else
335 return (ptr - s);
336 }
337 /*
338
339 */
340
341 int CGN_FILINFO(file,size,time,prot)
342
343 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
344 .PURPOSE
345 get file info
346 .ALGORITHM
347 use osfinfo
348 .RETURNS
349 return status = 0, if all o.k.
350 = -1, if file could not be accessed
351 ------------------------------------------------------------------------*/
352 char *file;
353 long int *size, *time;
354 int *prot;
355
356 {
357 int n;
358
359 struct filestatus fstat;
360
361
362
363 n = osfinfo(file,&fstat);
364 if (n != 0) return (-1);
365
366 *size = fstat.filesize;
367 *time = (long int) fstat.date;
368 *prot = fstat.protection;
369
370 return 0;
371 }
372