Debian Codegraph @

libsrc/st/scc.c

raw
1 /*===========================================================================
2 Copyright (C) 1985-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 SCC ++++++++++++++++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION Module SCC.C
31 .COMMENTS
32 Holds catalog interfaces:
33 SCCAD, SCCSUB, SCCCRE, SCCCRA, create_cat
34 .AUTHOR Klaus Banse ESO - Garching
35 .KEYWORDS MIDAS catalogues
36 .ENVIRONMENT independant
37
38 .VERSION [1.00] 870911: creation from FORTRAN version 3.55 as of 850919
39
40 110303 last modif
41 ------------------------------------------------------------------------*/
42
43 #include <fileexts.h>
44 #include <fsydef.h>
45
46 #include <stdio.h>
47 #include <string.h>
48
49 /*
50
51 */
52
53 int SCCSUB(catfile,name)
54
55 /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56 .PURPOSE
57 remove given entry from catalog
58 .ALGORITHM
59 search for given entry + change first 2 chars to //
60 .RETURNS
61 int return status
62 ------------------------------------------------------------------------- */
63
64 char *catfile; /* IN: catalog file */
65 char *name; /* IN: name of frame to be removed */
66
67 {
68 char output[84];
69 char catrec[CATREC_LEN+4], catrc[CATREC_LEN+4];
70
71 int catid, catlen, loop_cnt;
72 int n, ll, stat, cattyp;
73 int saveoff, cimno, valid;
74
75
76 stat = MID_COPN(catfile,&cattyp,&cimno);
77 if (stat != ERR_NORMAL)
78 {
79 if (stat == ERR_FILNAM)
80 SCTPUT("(ERR) SCCSUB: - FILNAM");
81 else
82 SCTPUT("(ERR) SCCSUB: - CATOVF");
83 return stat;
84 }
85
86 catid = CATAL[cimno].FID;
87
88
89 /* position record pointer to beginning of catalog file */
90
91 if (CATAL[cimno].RECNO > 1)
92 {
93 stat = rewicat(catid,cimno); /* that also skips over type record */
94 if (stat < 0) goto osa_error;
95 }
96
97 (void)strcpy(output,name);
98
99 #if vms
100 CGN_LOWSTR(output);
101 #endif
102
103 n = CGN_INDEXC(output,' '); /* cutoff trailing blanks */
104 if (n > 0) output[n] = '\0';
105
106 n = CGN_JNDEXC(output,FSY_DIREND); /* search backwards */
107 ll = CGN_JNDEXC(output,FSY_TYPMARK); /* search backwards for type */
108
109 if (ll <= n)
110 (void)strcat(output,FSY_DEFPNTR[cattyp-1]); /* append default file type */
111
112 loop_cnt = 0;
113
114
115 /* loop through catalog + search for given name */
116
117 read_loop:
118 catlen = readcat(catid,cimno,catrec,&valid);
119 if (catlen < 0) goto not_found; /* EOF reached */
120
121 loop_cnt ++;
122 if (valid == 0) goto read_loop;
123
124
125 /* compare name with name_field of catal record */
126
127 ll = CGN_INDEXC(catrec,' '); /* find end of name */
128 if (ll < 1)
129 {
130 (void) printf("SCCSUB: no file delimiter...\n");
131 ll = 1;
132 }
133 (void) strncpy(catrc,catrec,ll);
134 catrc[ll] = '\0';
135
136 if (strcmp(catrc,output) == 0)
137 {
138 ll = CGN_COPY(catrc,catrec); /* save record */
139
140 #if vms
141 stat = (int) osaseek(catid,0L,FILE_START); /* rewind file */
142 if (stat < 0) goto osa_error;
143
144 for (n=0; n<loop_cnt; n++) /* read previous records again */
145 (void) osaread(catid,catrec,CATREC_LEN); /* including catal_type_rec */
146
147 stat = (int) osaseek(catid,0L,FILE_CURRENT);
148
149 #else
150 saveoff = (int) osaseek(catid,0L,FILE_CURRENT); /* get current position */
151 if (saveoff < 0) goto osa_error;
152 saveoff -= (ll + 1); /* move to beginning of line */
153
154 stat = (int) osaseek(catid,0L,FILE_START); /* rewind file */
155 if (stat < 0) goto osa_error;
156
157 stat = (int) osaseek(catid,(long)saveoff,FILE_START);
158 #endif
159
160 if (stat < 0) goto osa_error;
161
162 catrc[0] = '!'; /* mark as deleted */
163 catrc[1] = ' ';
164 stat = osawrite(catid,catrc,catlen);
165 if (stat < catlen)
166 goto osa_error;
167 else
168 goto all_ok;
169 }
170 goto read_loop;
171
172 all_ok:
173 stat = MID_CCLO(cimno);
174 return stat;
175
176 not_found:
177 stat = ERR_INPINV;
178 SCTPUT("(ERR) SCCSUB: - INPINV");
179 return stat;
180
181 osa_error:
182 stat = ERR_CATBAD;
183 SCTPUT("(ERR) SCCSUB: - CATBAD");
184 return stat;
185 }
186
187 /*
188
189 */
190
191 int SCCCRE(catfile,type,flag)
192
193 /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
194 .PURPOSE
195 create a catalog file
196 .ALGORITHM
197 1. read each filename from directory output file `directory.dat'
198 2. read relevant descriptors from that file and store in catalog file
199 .RETURNS
200 int return status
201 ------------------------------------------------------------------------- */
202
203 char *catfile; /* IN: catalog file (with \0) */
204 int type; /* IN: type of files to use, same as in SCFCRE */
205 int flag; /* IN: = 1, if file 'dirfile.ascii' exists */
206
207 {
208
209 /* use SCCCRA with descr IDENT */
210
211 return (SCCCRA(catfile,type,flag,"IDENT"));
212
213 }
214
215 /*
216
217 */
218
219 int SCCCRA(catfile,type,flag,catdsc)
220
221 /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
222 .PURPOSE
223 create a catalog file
224 .ALGORITHM
225 1. read each filename from directory output file `directory.dat'
226 2. read relevant descriptors from that file and store in catalog file
227 .RETURNS
228 int return status
229 ------------------------------------------------------------------------- */
230
231 char *catfile; /* IN: catalog file (with \0) */
232 int type; /* IN: type of files to use, same as in SCFCRE */
233 int flag; /* IN: = 1, if file 'dirfile.ascii' exists */
234 char *catdsc; /* IN: char. descr. used for Ident field */
235
236 {
237 int stat, mycno;
238
239
240 /* get catalog file access */
241
242 stat = MID_CCRE(catfile,type,catdsc,&mycno);
243 if (stat != ERR_NORMAL)
244 {
245 if (stat == ERR_INPINV)
246 SCTPUT("(ERR) SCCCRE: - INPINV");
247 else if (stat == ERR_CATBAD)
248 SCTPUT("(ERR) SCCCRE: - CATBAD");
249 else
250 SCTPUT("(ERR) SCCCRE: - CATOVF");
251 return stat;
252 }
253 else
254 return (create_cat(catfile,type,flag,mycno));
255
256 }
257
258 /*
259
260 */
261
262 int create_cat(catfile,type,flag,cno)
263
264 /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
265 .PURPOSE
266 actual routine to create a catalog file
267 .ALGORITHM
268 1. read each filename from directory output file `directory.dat'
269 2. read relevant descriptors from that file and store in catalog file
270 .RETURNS
271 int return status
272 ------------------------------------------------------------------------- */
273
274 char *catfile; /* IN: catalog file (with \0) */
275 int type; /* IN: type of files to use, same as in SCFCRE */
276 int flag; /* IN: = 1, if file 'dirfile.ascii' exists */
277 int cno; /* IN: index into catalog structure */
278
279 {
280 char output[84], *catdsc, cbuf[CATIDENT_LEN+4];
281 char catrec[CATREC_LEN+4];
282
283 int e_c, e_l, e_d, deja, typflg;
284 int kcount, iav, nullo, ibuf[5];
285 int ival[3], nax, imno=-1;
286 int catid, uni;
287 int did, kk, n, stat;
288 int catoff, flag_FITS;
289
290 void mergecat();
291
292
293
294 catid = CATAL[cno].FID;
295 catdsc = CATAL[cno].DESCR; /* point to catalog descr. */
296 kcount = 0;
297 if (flag != 1) goto catalog_close; /* nothing to be done...*/
298
299
300 /* open directory file 'dirfile.ascii' */
301
302 did = CGN_OPEN("dirfile.ascii",READ);
303 if (did == -1)
304 {
305 SCTPUT("No file `dirfile.ascii' found...");
306 flag = 0;
307 goto catalog_close;
308 }
309
310 e_c = ERRO_CONT; /* save error continuation flag */
311 e_l = ERRO_LOG; /* and error log flag */
312 e_d = ERRO_DISP; /* and display flag */
313 ERRO_CONT = 1; /* continue on errors */
314 ERRO_LOG = 0; /* and log no errors */
315 ERRO_DISP = 0; /* and display no errors... */
316
317
318 read_loop:
319 stat = osaread(did,output,60); /* max. 60 chars filenames... */
320 if (stat < 0)
321 goto end_of_file; /* EOF reached */
322 else if (stat == 0)
323 goto read_loop;
324
325
326 #if vms
327 n = CGN_JNDEXC(output,FSY_TESTA); /* remove version no. */
328 if (n > 0) output[n] = '\0';
329 CGN_LOWSTR(output);
330 #endif
331
332 n = CGN_JNDEXC(output,FSY_DIREND); /* search backwards */
333 kk = CGN_JNDEXC(output,FSY_TYPMARK); /* search backwards */
334 if ((type != F_ASC_TYPE) && (kk <= n))
335 {
336 char newrec[160];
337
338 (void) sprintf(newrec,
339 "\"%s\" not added to catalog - missing type...",output);
340 SCTPUT(newrec);
341 goto read_loop;
342 }
343
344 if (strncmp(output,"middumm",7) == 0)
345 goto read_loop; /* avoid middummX frames ... */
346
347
348 /* fill name field of catalog record */
349
350 memset((void *)catrec,32,(size_t)CATREC_LEN);
351 catrec[CATREC_LEN] = '\0';
352 catoff = CGN_COPY(catrec,output);
353 catrec[catoff++] = ' '; /* remove the `\0' again */
354
355
356 /* handle ASCII files specially... */
357
358 if (type == F_ASC_TYPE)
359 {
360 iav = ascii_tst(output,&catrec[catoff]); /* check, what ASCII file */
361 if (iav == 0)
362 {
363 kcount ++;
364 stat = osawrite(catid,catrec,(int)strlen(catrec));
365 CATAL[cno].RECNO ++;
366 }
367 else
368 {
369 char newrec[160];
370
371 if (iav == -9) /* we reached a subdirectory... */
372 goto end_of_file;
373
374 (void) sprintf(newrec,
375 "\"%s\" no text file, omitted ...",output);
376 SCTPUT(newrec);
377 }
378 goto read_loop;
379 }
380
381
382 /* look for relevant descriptors */
383
384 n = SCFINF(output,0,ibuf);
385 if (n == ERR_NORMAL) /* see, if already opened before */
386 deja = 0;
387 else
388 deja = -1;
389
390 flag_FITS = 0; /* default to no FITS format */
391 ibuf[1] = -999;
392 stat = SCFINF(output,9,ibuf); /* get type of frame */
393 if (stat != ERR_NORMAL)
394 {
395 char newrec[160];
396
397 (void) sprintf(newrec,"Warning: Could not open file %s ...",output);
398 SCTPUT(newrec);
399 goto read_loop;
400 }
401
402 kcount ++;
403
404 if (type != ibuf[1])
405 {
406 char newrec[160];
407
408 (void) sprintf(newrec,
409 "Warning: File %s not of same type as catalog ...",output);
410 SCTPUT(newrec);
411
412 if (ibuf[1] == F_IMA_TYPE)
413 {
414 (void) SCFOPN(output,D_OLD_FORMAT,0,F_IMA_TYPE,&imno);
415 typflg = -1;
416 }
417 else if (ibuf[1] == F_TBL_TYPE)
418 {
419 (void) SCFOPN(output,D_OLD_FORMAT,0,F_TBL_TYPE,&imno);
420 typflg = -2;
421 flag_FITS = ibuf[0]; /* save TBL (0) or FITS (>0) format */
422 }
423 else /* only FIT files left */
424 {
425 (void) SCFOPN(output,D_OLD_FORMAT,0,F_FIT_TYPE,&imno);
426 typflg = -3;
427 }
428 }
429
430 else
431 {
432 typflg = 0;
433 (void) SCFOPN(output,D_OLD_FORMAT,0,type,&imno); /* SCFINF was o.k. */
434 if (type == F_TBL_TYPE)
435 flag_FITS = ibuf[0]; /* save TBL (0) or FITS (>0) format */
436 }
437
438 stat = SCDGETC(imno,catdsc,1,CATIDENT_LEN,&iav,cbuf);
439 if (stat != ERR_NORMAL)
440 {
441 if (typflg == 0) /* matching frame type */
442 iav = CGN_COPY(cbuf," ");
443 else if (typflg == -1)
444 iav = CGN_COPY(cbuf,"is image");
445 else if (typflg == -2)
446 iav = CGN_COPY(cbuf,"is table");
447 else /* only typflg = -3 possible */
448 iav = CGN_COPY(cbuf,"is Fit_file");
449 }
450
451
452 /* merge CATAL.DESCR buffer into catalog record */
453
454 mergecat(1,catrec,&catoff,cbuf,iav);
455
456 if (typflg == 0) /* for matching file types */
457 { /* get auxiliary info */
458 if (type == F_IMA_TYPE) /* handle images */
459 {
460 stat = SCDRDI(imno,"NAXIS",1,1,&iav,&nax,&uni,&nullo);
461 if (stat == ERR_NORMAL)
462 {
463 kk = nax;
464 if (kk > 3) kk = 3; /* only space for three Npix */
465 stat = SCDRDI(imno,"NPIX",1,kk,&iav,ival,&uni,&nullo);
466 if (stat == ERR_NORMAL)
467 {
468 if (nax == 1)
469 (void)sprintf(cbuf,"%d %d",nax,ival[0]);
470 else if (nax == 2)
471 (void)sprintf(cbuf,"%d %d,%d",nax,ival[0],ival[1]);
472 else
473 (void)sprintf(cbuf,"%d %d,%d,%d",nax,ival[0],ival[1],ival[2]);
474
475 iav = (int) strlen(cbuf);
476 mergecat(0,catrec,&catoff,cbuf,iav); /* merge Num field */
477 }
478 else
479 (void) sprintf(cbuf,"NPIX");
480 }
481 else
482 (void) sprintf(cbuf,"NAXIS");
483 }
484
485 else if (type == F_TBL_TYPE) /* and tables */
486 {
487 stat = SCDRDI(imno,"TBLCONTR",3,2,&iav,ival,&uni,&nullo);
488 if (stat == ERR_NORMAL)
489 {
490 (void)sprintf(cbuf," %5d %5d",ival[0],ival[1]);
491 iav = (int) strlen(cbuf);
492 mergecat(0,catrec,&catoff,cbuf,iav); /* merge Num field */
493 }
494 else
495 (void) sprintf(cbuf,"TBLCONTR");
496 }
497
498 if (stat != ERR_NORMAL)
499 {
500 char newrec[160];
501
502 (void) sprintf(newrec,
503 "Warning: descr %s of %s is corrupted...",cbuf,output);
504 SCTPUT(newrec);
505 }
506 }
507
508
509 /* clear up, but release frame only if it was not opened before... */
510
511 if (deja == -1)
512 {
513 if (flag_FITS > 0)
514 stat = TCTCLO(imno);
515 else
516 stat = SCFCLO(imno);
517 if (stat != ERR_NORMAL)
518 {
519 char newrec[160];
520
521 (void) sprintf(newrec,
522 "Warning: could not close correctly file: %s ...",output);
523 SCTPUT(newrec);
524 }
525 }
526
527 stat = osawrite(catid,catrec,catoff); /* write new record */
528 CATAL[cno].RECNO ++;
529
530 goto read_loop; /* look for next file */
531
532
533 /* come here at EOF - and reset keywords */
534
535 end_of_file:
536 osaclose(did);
537 ERRO_CONT = e_c;
538 ERRO_LOG = e_l;
539 ERRO_DISP = e_d;
540
541 catalog_close:
542 stat = MID_CCLO(cno);
543 (void) SCKWRI("OUTPUTI",&kcount,10,1,&uni);
544 return stat;
545 }
546
547 /*
548
549 */
550
551 int SCCADD(catfile,name,ident)
552 /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
553 .PURPOSE
554 add catalog entry
555 .ALGORITHM
556 add an entry with given name + identification field to the catalog
557 .RETURNS
558 int return status
559 ------------------------------------------------------------------------- */
560
561 char *catfile; /* IN: catalog file */
562 char *name; /* IN: name of frame to be added */
563 char *ident; /* IN: Ident info of frame to be added */
564
565 {
566 char output[200], *catdsc, cbuf[80];
567 char catrec[CATREC_LEN+4], catrc[CATREC_LEN+4];
568 char *osmsg();
569
570 int ibuf[5], iav, nullo, nax, ival[3], e_c, e_l;
571 int uni, deja;
572 int imno=-1;
573 int catid, catoff, catlen, n, stat, kk;
574 int cattyp, typflg, saverec, cimno, valid, noblank;
575
576 void mergecat();
577
578
579
580 stat = MID_COPN(catfile,&cattyp,&cimno);
581 if (stat != ERR_NORMAL)
582 {
583 if (stat == ERR_FILNAM)
584 SCTPUT("(ERR) SCCADD: - FILNAM");
585 else
586 SCTPUT("(ERR) SCCADD: - CATOVF");
587 return stat;
588 }
589
590 catid = CATAL[cimno].FID;
591 catdsc = CATAL[cimno].DESCR;
592
593
594 /* position record pointer to begin of catalog file */
595
596 if (CATAL[cimno].RECNO > 1)
597 {
598 stat = rewicat(catid,cimno); /* that also skips over type record */
599 if (stat < 0) goto osa_error;
600 }
601
602 (void)strcpy(output,name);
603
604 #if vms
605 CGN_LOWSTR(output);
606 #endif
607
608 n = CGN_INDEXC(output,' '); /* cutoff trailing blanks */
609 if (n > 0) output[n] = '\0';
610
611 n = CGN_JNDEXC(output,FSY_DIREND); /* search backwards */
612 kk = CGN_JNDEXC(output,FSY_TYPMARK); /* search backwards for type */
613
614 if (kk <= n)
615 (void)strcat(output,FSY_DEFPNTR[cattyp-1]); /* append default file type */
616
617 if (strncmp(output,"middumm",7) == 0) /* avoid middummX frames ... */
618 {
619 char newrec[160];
620
621 (void) sprintf(newrec,
622 "Warning: dummy file %s not stored in catalog...",output);
623 SCTPUT(newrec);
624 return (ERR_NORMAL);
625 }
626
627
628 /* check that file is of correct type */
629
630 typflg = 0; /* init to success */
631 if (cattyp != F_ASC_TYPE)
632 {
633 n = SCFINF(output,0,ibuf);
634 if (n == ERR_NORMAL) /* see, if already opened before */
635 deja = 0;
636 else
637 deja = -1;
638
639 ibuf[1] = -999;
640 stat = SCFINF(output,1,ibuf); /* get type of frame */
641 if (stat != ERR_NORMAL)
642 {
643 char newrec[160];
644
645 (void) sprintf(newrec,
646 "Could not open file %s ",output);
647 SCTPUT(newrec);
648 SCTPUT("(ERR) SCCADD: - INPINV");
649 return stat;
650 }
651
652 else if (cattyp != ibuf[1])
653 {
654 char newrec[160];
655
656 (void) sprintf(newrec,
657 "Warning: File %s not of same type as catalog ...",output);
658 SCTPUT(newrec);
659
660 if (ibuf[1] == F_IMA_TYPE)
661 {
662 (void) SCFOPN(output,D_OLD_FORMAT,0,F_IMA_TYPE,&imno);
663 typflg = -1;
664 }
665 else if (ibuf[1] == F_TBL_TYPE)
666 {
667 (void) SCFOPN(output,D_OLD_FORMAT,0,F_TBL_TYPE,&imno);
668 typflg = -2;
669 }
670 else /* only FIT files left */
671 {
672 (void) SCFOPN(output,D_OLD_FORMAT,0,F_FIT_TYPE,&imno);
673 typflg = -3;
674 }
675 }
676 }
677
678 else
679 {
680 deja = 0; /* avoid SCFCLO later on */
681 kk = ascii_tst(output,catrec); /* catrec is overwritten later on */
682 if (kk != 0)
683 {
684 char newrec[160];
685
686 (void) sprintf(newrec,
687 "Warning: File %s not an ASCII file ...",output);
688 SCTPUT(newrec);
689 iav = CGN_COPY(cbuf,"no ASCII file");
690 typflg = 1;
691 }
692 }
693
694
695 /* copy name into 1. field of catalog record */
696
697 memset((void *)catrec,32,(size_t)CATREC_LEN);
698 catrec[CATREC_LEN] = '\0';
699 catoff = CGN_COPY(catrec,output);
700 catrec[catoff++] = ' '; /* remove the \0 again */
701
702 if (typflg == 1)
703 { /* we don't have Ident + other info */
704 mergecat(1,catrec,&catoff,cbuf,iav); /* merge Ident field */
705 mergecat(0,catrec,&catoff," ",1); /* merge Num field */
706 goto find_loop; /* now store it in catalog */
707 }
708
709
710 /* work on catalog descriptor */
711
712 e_c = ERRO_CONT; /* save error continuation flag */
713 e_l = ERRO_LOG; /* and error log flag... */
714 ERRO_CONT = 1; /* continue on errors */
715 ERRO_LOG = 0; /* and log no errors... */
716
717 iav = (int)strlen(ident); /* is it just blank? */
718 noblank = 0;
719 for (n=0; n<iav; n++)
720 {
721 if (ident[n] != ' ')
722 {
723 noblank = 1;
724 break;
725 }
726 }
727
728 if (typflg == 0) /* SCFINF was o.k. */
729 (void) SCFOPN(output,D_OLD_FORMAT,0,CATAL[cimno].TYPE,&imno);
730
731 if (noblank != 0)
732 {
733 if (iav > CATIDENT_LEN) iav = CATIDENT_LEN;
734 (void) strncpy(cbuf,ident,iav);
735 }
736
737 else
738 {
739 stat = SCDGETC(imno,catdsc,1,CATIDENT_LEN,&iav,cbuf);
740 if (stat != ERR_NORMAL)
741 {
742 if (typflg == 0) /* matching frame type */
743 iav = CGN_COPY(cbuf," ");
744 else if (typflg == -1)
745 iav = CGN_COPY(cbuf,"is image");
746 else if (typflg == -2)
747 iav = CGN_COPY(cbuf,"is table");
748 else
749 iav = CGN_COPY(cbuf,"is Fit_file");
750 }
751 }
752 /* merge Ident buffer into catalog record */
753 mergecat(1,catrec,&catoff,cbuf,iav);
754
755 if (typflg == 0) /* for matching file types */
756 { /* get auxiliary info */
757 if (CATAL[cimno].TYPE == F_IMA_TYPE) /* handle images */
758 {
759 nax = -1;
760 (void) SCDRDI(imno,"NAXIS",1,1,&iav,&nax,&uni,&nullo);
761 if (nax < 1) /* empty header file */
762 iav = sprintf(cbuf,"%d",nax);
763 else
764 {
765 ival[0] = ival[1] = ival[2] = -1;
766 if (nax > 3)
767 kk = 3; /* only space for three Npix */
768 else
769 kk = nax;
770 stat = SCDRDI(imno,"NPIX",1,kk,&iav,ival,&uni,&nullo);
771 if (nax == 1)
772 iav = sprintf(cbuf,"%d %d",nax,ival[0]);
773 else if (nax == 2)
774 iav = sprintf(cbuf,"%d %d,%d",nax,ival[0],ival[1]);
775 else
776 iav = sprintf
777 (cbuf,"%d %d,%d,%d",nax,ival[0],ival[1],ival[2]);
778 }
779 mergecat(0,catrec,&catoff,cbuf,iav); /* merge Num field */
780 }
781
782
783 else if (CATAL[cimno].TYPE == F_TBL_TYPE) /* and tables */
784 {
785 stat = SCDRDI(imno,"TBLCONTR",3,2,&iav,ival,&uni,&nullo);
786 if (stat == ERR_NORMAL)
787 {
788 (void)sprintf(cbuf," %5d %5d",ival[0],ival[1]);
789 iav = (int) strlen(cbuf);
790 mergecat(0,catrec,&catoff,cbuf,iav); /* merge Num field */
791 }
792 else
793 (void) sprintf(cbuf,"TBLCONTR");
794 }
795
796 if (stat != ERR_NORMAL)
797 {
798 char newrec[160];
799
800 (void) sprintf(newrec,
801 "Warning: descr %s of %s is corrupted...",cbuf,output);
802 SCTPUT(newrec);
803 }
804 }
805
806 /* clear up, but release frame only if it was not opened before... */
807
808 if (deja == -1) (void) SCFCLO(imno);
809
810 ERRO_CONT = e_c;
811 ERRO_LOG = e_l;
812
813
814 /* search through complete file, if entry already there ... */
815
816 find_loop:
817 catlen = readcat(catid,cimno,catrc,&valid);
818 if (catlen < 0)
819 { /* EOF reached */
820 stat = (int) osaseek(catid,0L,FILE_END); /* position write pointer */
821 if (stat < 0)
822 goto osa_error;
823 else
824 goto not_found;
825 }
826 else if (valid == 0) /* was a deleted record */
827 goto find_loop;
828
829
830 /* compare name with name_field of catal record */
831
832 n = CGN_INDEXC(catrc,' '); /* find end of name */
833 if (n < 1)
834 {
835 (void) printf("SCCADD: no file delimiter...\n");
836 n = 1;
837 }
838 (void) strncpy(cbuf,catrc,n);
839 cbuf[n] = '\0';
840
841 if (strcmp(cbuf,output) == 0)
842 {
843 saverec = CATAL[cimno].RECNO - 1; /* save record no. */
844 if ((stat = rewicat(catid,cimno)) < 0) goto osa_error;
845
846 while (saverec != CATAL[cimno].RECNO)
847 {
848 stat = readcat(catid,cimno,catrc,&valid);
849 if (stat < 0) goto osa_error;
850 }
851 stat = (int) osaseek(catid,0L,FILE_CURRENT); /* set write pointer */
852 if (stat < 0) goto osa_error;
853
854 CATAL[cimno].RECNO = saverec;
855
856 if (catlen < catoff) /* new length is larger */
857 {
858 catrc[0] = '!';
859 catrc[1] = ' ';
860 stat = osawrite(catid,catrc,catlen); /* delete this entry */
861 stat = (int) osaseek(catid,0L,FILE_END); /* position write pointer */
862 if (stat < 0) goto osa_error; /* to EOF */
863
864 (void)
865 sprintf(output,"updated entry #%-4.4d moved to end of catalog",saverec);
866 SCTPUT(output);
867 CATAL[cimno].RECNO = 9999999; /* force rewind next time */
868 }
869
870 else if (catlen > catoff) /* new length is smaller */
871 {
872 memset((void *)catrc,32,(size_t)CATREC_LEN);
873 (void) strncpy(catrc,catrec,catoff); /* new record -> old record */
874 catrc[catlen] = '\0';
875 (void) strcpy(catrec,catrc);
876 }
877 }
878 else
879 goto find_loop;
880
881
882 /* write new record */
883
884 not_found:
885 stat = osawrite(catid,catrec,catoff);
886 if (stat < catoff)
887 {
888 puts(osmsg());
889 stat = ERR_INPINV;
890 SCTPUT("(ERR) SCCADD: - INPINV");
891 return stat;
892 }
893 else
894 {
895 CATAL[cimno].RECNO ++;
896 return ERR_NORMAL;
897 }
898
899 osa_error:
900 stat = ERR_CATBAD;
901 SCTPUT("(ERR) SCCADD: - CATBAD");
902 return stat;
903 }
904