Debian Codegraph @

libsrc/st/midcat.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2011 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 /*+++++++++++++++++++++ Module MIDCAT +++++++++++++++++++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION MID* interfaces related to Catalogs
31 .COMMENTS
32 holds MID_CCRE, MID_COPN, MID_CCLO + ascii_tst, readcat, rewicat, mergecat
33 .AUTHOR Klaus Banse
34 .KEYWORDS Midas utility routines.
35 .ENVIRONMENT independent
36
37 .VERSION [1.30] 861110: (K. Banse)
38
39 110303 last modif
40 ------------------------------------------------------------------------*/
41
42 #include <stdio.h>
43 #include <string.h>
44
45 #include <fileexts.h>
46
47
48
49 /*
50
51 */
52
53 int MID_CCLO(catno)
54
55 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56 .KEYWORDS
57 catalog file
58 .PURPOSE
59 close catalog
60 .ALGORITHM
61 straight forward
62 .RETURNS stat
63 -------------------------------------------------------------------------*/
64
65 int catno; /* IN: file id of opened catalog
66 if = -1, close all catalogs */
67
68 {
69 int catid, n;
70
71
72 if (catno < 0)
73 { /* CLEAR the complete catalog structure */
74 for (n=0; n<CAT_MAXNO; n++)
75 {
76 if (CATAL[n].NAME[0] != ' ')
77 {
78 CATAL[n].NAME[0] = ' ';
79 catid = CATAL[n].FID;
80 (void) osaclose(catid);
81 }
82 }
83 }
84
85 else
86 { /* CLOSE only specified catalog */
87 if (catno >= CAT_MAXNO) return (ERR_INPINV);
88
89 if (CATAL[catno].NAME[0] != ' ')
90 {
91 CATAL[catno].NAME[0] = ' ';
92 catid = CATAL[catno].FID;
93 if (osaclose(catid) != 0) return (ERR_CATBAD);
94 }
95 }
96
97 return (ERR_NORMAL);
98 }
99
100 /*
101
102 */
103
104 int MID_CKLO(catnam)
105
106 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
107 .KEYWORDS
108 catalog file
109 .PURPOSE
110 close catalog
111 .ALGORITHM
112 straight forward
113 .RETURNS stat
114 -------------------------------------------------------------------------*/
115
116 char *catnam; /* IN: name of catalog to close */
117
118 {
119 int n, catid, status;
120
121
122
123 status = ERR_INPINV;
124
125 for (n=0; n<CAT_MAXNO; n++)
126 {
127 if (strcmp(CATAL[n].NAME,catnam) == 0)
128 {
129 CATAL[n].NAME[0] = ' ';
130 catid = CATAL[n].FID;
131 if (osaclose(catid) != 0)
132 status = ERR_CATBAD;
133 else
134 status = ERR_NORMAL;
135 }
136 }
137
138 return (status);
139 }
140
141 /*
142
143 */
144
145 int MID_COPN(catfile,type,cimno)
146
147 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
148 .KEYWORDS
149 catalog file
150 .PURPOSE
151 open catalog file
152 .ALGORITHM
153 straight forward
154 .RETURNS stat
155 -------------------------------------------------------------------------*/
156
157 char *catfile; /* IN: catalog file (with \0 ) */
158 int *type; /* OUT: filetype of catalog */
159 int *cimno; /* OUT: catfile index in CATAL structure */
160
161 {
162 int mm, status, n, namtyp, part_no;
163 int catid, found;
164 register int nr, mr;
165
166 char work[100];
167 register char cc;
168
169
170
171 *cimno = -1; /* get cleaned frame name */
172 mm = FNAME_LEN;
173 status = CGN_CLEANF(catfile,7,work,mm,&namtyp,&part_no);
174 if (status != 0) return (ERR_FILNAM); /* invalid syntax */
175
176
177 /* first search for matching name, then for free slot */
178
179 for (n=0; n<CAT_MAXNO; n++)
180 {
181 if (CATAL[n].NAME[0] != ' ')
182 {
183 if (strcmp(work,CATAL[n].NAME) == 0) /* we found the catalog */
184 {
185 *type = CATAL[n].TYPE;
186 *cimno = n;
187 return (ERR_NORMAL);
188 }
189 }
190 }
191
192 catid = CGN_OPEN(work,READ_WRITE);
193 if (catid <= 0)
194 {
195 catid = CGN_OPEN(work,READ);
196 if (catid <= 0) return (ERR_FILNAM);
197 }
198
199
200 for (n=0; n<CAT_MAXNO; n++)
201 {
202 if (CATAL[n].NAME[0] == ' ')
203 {
204 (void) strcpy(CATAL[n].NAME,work);
205 found = n;
206 goto read_catal;
207 }
208 }
209
210 return (ERR_CATOVF);
211
212
213 read_catal:
214 status = osaread(catid,work,80);
215 if (status < 1) goto bad_file;
216
217 CATAL[found].TYPE_SET = 1;
218 n = CGN_INDEXC(work,'=');
219 if (n < 0)
220 { /* use image catalog as default */
221 CATAL[found].TYPE_SET = 0;
222 mm = F_IMA_TYPE;
223 (void) strcpy(CATAL[found].DESCR,"IDENT");
224 goto next_step;
225 }
226
227
228 cc = work[++n];
229 if ( (cc == 'I') || (cc == 'i') )
230 mm = F_IMA_TYPE;
231 else if ( (cc == 'T') || (cc == 't') )
232 mm = F_TBL_TYPE;
233 else if ( (cc == 'F') || (cc == 'f') )
234 mm = F_FIT_TYPE;
235 else if ( (cc == 'A') || (cc == 'a') )
236 mm = F_ASC_TYPE;
237 else
238 goto bad_file;
239
240
241 /* look for ", descr.name */
242
243 for (nr=(n+1); ;nr++) /* start at work[n+1] */
244 {
245 cc = work[nr];
246 if (cc == '\0') break;
247
248 else if (cc == ',')
249 {
250 mr = nr;
251 while(1)
252 {
253 cc = work[++mr];
254 if (cc != ' ') /* skip any leading blanks */
255 {
256 if (cc == '\0')
257 goto default_dsc;
258 else
259 {
260 (void) strcpy(CATAL[found].DESCR,&work[mr]);
261 goto next_step;
262 }
263 }
264 }
265 }
266 }
267 default_dsc:
268 (void) strcpy(CATAL[found].DESCR,"IDENT");
269
270 next_step:
271 CATAL[found].FID = catid;
272 CATAL[found].TYPE = mm; /* store type of catalog */
273 CATAL[found].RECNO = 1; /* next record will be # 1 */
274
275 *type = mm;
276 *cimno = found;
277 return (ERR_NORMAL);
278
279
280 bad_file:
281 osaclose(catid);
282 CATAL[found].NAME[0] = ' ';
283 return (ERR_CATBAD);
284 }
285
286 /*
287
288 */
289
290 int MID_CCRE(catfile,type,descr,cimno)
291
292 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
293 .KEYWORDS
294 catalog file
295 .PURPOSE
296 create up to 3 catalog files in one program
297 .ALGORITHM
298 straight forward
299 .RETURNS stat
300 -------------------------------------------------------------------------*/
301
302 char *catfile; /* IN: catalog file (with \0 ) */
303 int type; /* IN: same as filetype in SCFOPN */
304 char *descr; /* IN: char. descr used for Ident field */
305 int *cimno; /* OUT: catfile index in CATAL structure */
306
307 {
308 int catid;
309 int n, mm, found, namtyp, part_no, status;
310
311 char work[100];
312
313
314 /* get cleaned frame name */
315
316 *cimno = -1;
317
318 mm = FNAME_LEN;
319 CGN_CLEANF(catfile,7,work,mm,&namtyp,&part_no);
320
321
322 /* first search for matching name, then for free slot */
323
324 for (n=0; n<CAT_MAXNO; n++)
325 {
326 if (CATAL[n].NAME[0] != ' ')
327 {
328 if (strcmp(work,CATAL[n].NAME) == 0)
329 {
330 found = n;
331 status = osaclose(CATAL[found].FID);
332 if (status != 0) return (ERR_CATBAD);
333 goto sect_1000;
334 }
335 }
336 }
337
338 for (n=0; n<CAT_MAXNO; n++)
339 {
340 if (CATAL[n].NAME[0] == ' ')
341 {
342 (void) strcpy(CATAL[n].NAME,work);
343 found = n;
344 goto sect_1000;
345 }
346 }
347
348 return (ERR_CATOVF);
349
350
351 sect_1000:
352 catid = CGN_OPEN(work,WRITE);
353 CATAL[found].FID = catid;
354
355 if (catid > 0)
356 {
357 CATAL[found].TYPE = type; /* store type of catalog */
358 mm = (int)strlen(descr);
359 if (mm > 47)
360 {
361 CATAL[found].NAME[0] = ' ';
362 osaclose(catid);
363 return (ERR_INPINV);
364 }
365
366 (void) strcpy(CATAL[found].DESCR,descr);
367
368 if (type == F_IMA_TYPE)
369 (void) snprintf(work,(size_t)100," =Image catalog, %s",descr);
370 else if (type == F_TBL_TYPE)
371 (void) snprintf(work,(size_t)100," =Table catalog, %s",descr);
372 else if (type == F_FIT_TYPE)
373 (void) snprintf(work,(size_t)100," =Fit file catalog, %s",descr);
374 else
375 (void) strcpy(work," =ASCII file catalog");
376
377 status = osawrite(catid,work,(int)strlen(work));
378 CATAL[found].RECNO = 1; /* next record will be # 1 */
379 *cimno = found;
380 return (ERR_NORMAL);
381 }
382 else
383 {
384 CATAL[found].NAME[0] = ' ';
385 return (ERR_CATBAD);
386 }
387 }
388
389 /*
390
391 */
392
393 int ascii_tst(inpntr,outpntr)
394 char *inpntr, *outpntr;
395
396 {
397 char cbuf[88], ftype[12];
398
399 int reclen, totlen, istat, ascid;
400 register int nr;
401
402
403
404
405 reclen = CGN_INDEXC(inpntr,'.');
406 totlen = (int) strlen(inpntr) - 1;
407 if (*(inpntr+totlen) == ':') return (-9); /* now come the subdirs */
408
409 if ( (reclen <= 0) || ((totlen-reclen) > 8) )
410 goto asc_open;
411
412
413 /* isolate file type */
414
415 (void) strcpy(ftype,inpntr+reclen);
416 for (nr=1; nr<10; nr++)
417 { /* get rid of trailing blanks */
418 if (ftype[nr] == ' ')
419 {
420 ftype[nr] = '\0';
421 break;
422 }
423 }
424
425
426 /* first look for file types which are rejected */
427
428 if (strcmp(ftype,".obj") == 0) /* VAX/VMS object code */
429 return (1);
430 else if (strcmp(ftype,".o") == 0) /* Unix object code */
431 return (1);
432 else if (strcmp(ftype,".exe") == 0) /* VMS + MIDAS executable */
433 return (1);
434 else if (strcmp(ftype,".dvi") == 0) /* Tex/Dvi file */
435 return (1);
436 else if (strcmp(ftype,".jpg") == 0) /* JPEG file */
437 return (1);
438 else if (strcmp(ftype,".bdf") == 0) /* MIDAS image */
439 return (1);
440 else if (strcmp(ftype,".tbl") == 0) /* MIDAS table */
441 return (1);
442 else if (strcmp(ftype,".fit") == 0) /* MIDAS fit file */
443 return (1);
444 else if ( (strcmp(ftype,".fits") == 0) ||
445 (strcmp(ftype,".tfits") == 0) ||
446 (strcmp(ftype,".mt") == 0) ) /* FITS file */
447 return (1);
448
449
450 /* then look for known file types */
451
452 if (strcmp(ftype,".cat") == 0)
453 {
454 (void) strcpy(outpntr,"MIDAS catalog ^");
455 return (0);
456 }
457 else if (strcmp(ftype,".ctx") == 0)
458 {
459 (void) strcpy(outpntr,"MIDAS context file ^");
460 return (0);
461 }
462 else if (strcmp(ftype,".prg") == 0)
463 {
464 (void) strcpy(outpntr,"MIDAS procedure ^");
465 return (0);
466 }
467 else if (strcmp(ftype,".tex") == 0)
468 {
469 (void) strcpy(outpntr,"TEX file ^");
470 return (0);
471 }
472 else if (strcmp(ftype,".inc") == 0)
473 {
474 (void) strcpy(outpntr,"FORTRAN include file ^");
475 return (0);
476 }
477 else if (strcmp(ftype,".h") == 0)
478 {
479 (void) strcpy(outpntr,"C include file ^");
480 return (0);
481 }
482 else if (strcmp(ftype,".hh") == 0)
483 {
484 (void) strcpy(outpntr,"C++ include file ^");
485 return (0);
486 }
487 else if ( (strcmp(ftype,".f") == 0) ||
488 (strcmp(ftype,".for") == 0) )
489 {
490 (void) strcpy(outpntr,"FORTRAN source code ^");
491 return (0);
492 }
493 else if (strcmp(ftype,".java") == 0)
494 {
495 (void) strcpy(outpntr,"JAVA source code ^");
496 return (0);
497 }
498 else if (strcmp(ftype,".c") == 0)
499 {
500 (void) strcpy(outpntr,"C source code ^");
501 return (0);
502 }
503 else if ( (strcmp(ftype,".cc") == 0) ||
504 (strcmp(ftype,".C") == 0) )
505 {
506 (void) strcpy(outpntr,"C++ source code ^");
507 return (0);
508 }
509 else if (strcmp(ftype,".ps") == 0)
510 {
511 (void) strcpy(outpntr,"Postscript file ^");
512 return (0);
513 }
514 else if (strcmp(ftype,".sh") == 0)
515 {
516 (void) strcpy(outpntr,"shell script ^");
517 return (0);
518 }
519
520 /* for all other files we really have to read the first record */
521
522 asc_open:
523
524 ascid = CGN_OPEN(inpntr,READ);
525 if (ascid == -1) return (-1);
526
527 istat = 1;
528
529 asc_read:
530 reclen= osaread(ascid,cbuf,20);
531 if (reclen < 0) goto asc_close;
532
533 if (reclen == 0) goto asc_read;
534
535 if (0 < cbuf[0]) /* Yes, it's ASCII */
536 {
537 CGN_UPSTR(cbuf);
538 if (strncmp(cbuf,"SIMPLE =",9) == 0) /* FITS file */
539 istat = 1; /* don't use */
540 else if (strncmp(cbuf,"#!",2) == 0)
541 {
542 (void) strcpy(outpntr,"shell script ^");
543 istat = 0;
544 }
545 else
546 {
547 (void) strcpy(outpntr,"ASCII file ^");
548 istat = 0;
549 }
550 }
551
552 asc_close:
553 (void) osaclose(ascid);
554
555 return istat;
556 }
557
558 /*
559
560 */
561
562 int readcat(id,kno,buf,kk)
563 int id; /* IN: catalog file id */
564 int kno; /* IN: catalog id no. */
565 int *kk; /* OUT: rec_flag = 0, deleted record,
566 = 1, valid record */
567 char *buf; /* OUT: buffer with catalog record (if kk = 1) */
568
569 /* return actual length of catalog record */
570
571
572 {
573 int ilen;
574
575
576 *kk = 0; /* default to non-valid record */
577
578 ilen = osaread(id,buf,CATREC_LEN);
579 if (ilen > 0)
580 {
581 CATAL[kno].RECNO ++;
582 if (*buf != '!') *kk = 1; /* valid record */
583 }
584
585 return (ilen);
586 }
587
588
589
590
591
592 int rewicat(catid,kno)
593 int catid;
594 int kno;
595
596 {
597 int istat;
598 char cbuf[84];
599
600
601 istat = (int) osaseek(catid,0L,FILE_START);
602 if (istat >= 0)
603 {
604 if (CATAL[kno].TYPE_SET == 1)
605 istat = osaread(catid,cbuf,CATREC_LEN); /* skip over type record */
606 CATAL[kno].RECNO = 1;
607 }
608
609 return (istat);
610 }
611
612 /*
613
614 */
615
616 void mergecat(flag,buf1,off,buf2,lbuf2)
617 int flag; /* IN: 1 = Ident buffer,
618 0 = Num buffer */
619 char *buf1; /* IN/OUT: buffer with catalog record */
620 int *off; /* IN/OUT: offset of next char. in buf1 */
621 char *buf2; /* IN: buffer to be merged into catalog record */
622 int lbuf2; /* IN: length of buf2 */
623
624
625 {
626 int kk, actlen;
627 register int nr;
628
629
630
631 kk = *off;
632
633 if (flag == 1) /* handle Ident field */
634 {
635 if (lbuf2 >= CATIDENT_LEN)
636 {
637 (void) strncpy(&buf1[kk],buf2,CATIDENT_LEN);
638 kk += CATIDENT_LEN;
639 }
640 else
641 {
642 (void) strncpy(&buf1[kk],buf2,(size_t)lbuf2);
643 kk += lbuf2;
644 nr = CATIDENT_LEN - lbuf2;
645 memset(&buf1[kk],32,(size_t)nr);
646 kk += nr;
647 }
648 buf1[kk++] = '^';
649 }
650 else
651 {
652 actlen = 0;
653
654 for (nr=lbuf2-1; nr>(-1); nr--)
655 {
656 if (buf2[nr] != ' ')
657 {
658 actlen = nr + 1;
659 break;
660 }
661 }
662
663 nr = CATREC_LEN - kk; /* available space */
664 if (actlen > nr) actlen = nr;
665 if (actlen > 0)
666 {
667 (void) strncpy(&buf1[kk],buf2,(size_t)actlen);
668 kk += actlen;
669 }
670 }
671
672 *off = kk;
673 buf1[kk] = '\0';
674
675 }