Debian Codegraph @

libsrc/st/midkeyb.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2009 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 MIDKEYB +++++++++++++++++++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION Module MIDKEYB
31 .AUTHOR Klaus Banse ESO - Garching
32 .KEYWORDS Midas keyword utility routines.
33 .ENVIRONMENT VMS and UNIX
34 .COMMENT
35 holds DEFKEY, DELKEY, PACKY, TSTKEY
36
37 .VERSION [1.00] 920211: pulled out from midkey.c
38
39 090318 last modif
40 ------------------------------------------------------------------------*/
41
42
43 #include <stdio.h>
44 #include <string.h>
45
46 #include <fileexts.h>
47
48
49 #define READ 0
50 #define WRITE 1
51 #define READ_WRITE 2
52 #define APPEND 3
53
54 #define MAX_LEVEL 25 /* synchronize with midback.h ...*/
55
56
57
58
59 static struct KEY_STRUCT *keypntr;
60
61 static char level[MAX_LEVEL+4] = "ABCDEFGHIJKLMNOPQRSTUVWXY";
62 /* synchronize with MID_FNDKEY + CLEAR_LOCAL... */
63
64
65 /*
66
67 */
68
69 int MID_TSTKEY(name)
70
71 /*++++++++++++++++++++++++++++++++++++++++++++++++++
72 .PURPOSE
73 check name of local keyword against reserved keyword names
74 .ALGORITHM
75 loop from index 0 to KEYALL->SYSNO and compare the names
76 .RETURNS
77 status: I*4 Return status
78 if = 0, everything is o.k.
79 --------------------------------------------------*/
80
81 char *name /* IN: Name of local keyword */;
82
83
84 {
85 register int nr, cdif, kstrn;
86
87 char kname[20];
88 register char nc;
89
90
91
92
93
94 cdif = 'A' - 'a';
95
96 for (nr=0; nr<=KEY_NAMELEN; nr++)
97 {
98 nc = name[nr];
99 if (nc == '\0')
100 {
101 kstrn = nr;
102 goto step_a;
103 }
104 if ((nc >= 'a') && (nc <= 'z')) /* convert to upper case */
105 nc += cdif;
106 kname[nr] = nc;
107 }
108 return (-1); /* bad (too long) keyname */
109
110
111 step_a:
112 if (kstrn < KEY_NAMELEN) kname[kstrn++] = ' '; /* append a blank */
113
114
115 /* loop through KEYNAMES from 0 till KEYALL.SYSNO and compare the key names */
116
117 keypntr = KEYALL.KEYNAMES;
118 for (nr=0; nr<=KEYALL.SYSNO; nr++)
119 {
120 if (strncmp(keypntr->IDENT,kname,(size_t)kstrn) == 0) return (-1);
121 keypntr ++;
122 }
123 return (0);
124 }
125
126
127 /*
128
129 */
130
131 #ifdef __STDC__
132 int MID_DEFKEY(char * name , char loco , char * type , int actval , int * unit)
133 #else
134 int MID_DEFKEY(name,loco,type,actval,unit)
135 /*++++++++++++++++++++++++++++++++++++++++++++++++++
136 .PURPOSE
137 define + create a user keyword
138 .ALGORITHM
139 check input parameters and update pointers
140 do not fill the data block
141 also take care of local keywords
142 .RETURNS
143 status: I*4 Return status
144 if = 0, everything is o.k.
145 --------------------------------------------------*/
146 char *name /* IN: Name of keyword */;
147 char loco /* IN: = 'L' or 'l' for local keyword */;
148 char *type /* IN: type of keyword = I or R or CHAR*len or D or S */;
149 int actval /* IN: No. of elements in keyword : actual length of
150 keyword depends on its type */;
151 int *unit /* IN: unit pointer of keyword */;
152 #endif
153
154 {
155 int status, noelem, bytelem, proglevel;
156 int byt_len, kstrn, cdif, koffs, pad, tot_len;
157 int sizbyt = 0;
158 register int nr;
159
160 char k_typ[4], kname[20];
161 char kc, loclevel, loclow;
162 register char nc;
163
164
165
166
167
168
169 /* name should not begin with '\' (that's how we mark deleted keys...) */
170
171 if ( *name == '\\')
172 {
173 status = ERR_INPINV;
174 goto end_of_it;
175 }
176
177 cdif = 'A' - 'a';
178 for (nr=0; nr<=KEY_NAMELEN; nr++)
179 {
180 nc = name[nr];
181 if (nc == '\0')
182 {
183 kstrn = nr;
184 goto step_a;
185 }
186 if ((nc >= 'a') && (nc <= 'z')) /* convert to upper case */
187 nc += cdif;
188 kname[nr] = nc;
189 }
190 return (-1); /* bad (too long) keyname */
191
192
193 /* check type + length */
194
195 step_a:
196 if (kstrn < KEY_NAMELEN) kname[kstrn++] = ' '; /* append a blank */
197
198 MID_TYPCHK(type,k_typ,&bytelem);
199 if (k_typ[0] == ' ') /* invalid type was given... */
200 {
201 status = ERR_INPINV;
202 goto end_of_it;
203 }
204
205 noelem = actval;
206
207
208 /* -------------------------- */
209 /* now for local keys */
210 /* -------------------------- */
211
212
213 if ((loco == 'L') || (loco == 'l'))
214 {
215 proglevel = KIWORDS[OFF_MODE+6] - 1; /* get current program level */
216 if (proglevel < 0)
217 {
218 status = ERR_INPINV; /* DEFINE/LOCAL only in procedure ! */
219 goto end_of_it;
220 }
221
222 loclevel = level[proglevel];
223 loclow = loclevel + ('a' - 'A'); /* e.g. 'B' -> 'b' */
224
225
226 /* loop through LOCNAMES from KEYALL.LOCNO till KEYALL.LOCENT
227 and compare the key names as well as the procedure level */
228
229 keypntr = KEYALL.KEYNAMES + KEYALL.GLOBENT;
230 for (nr=KEYALL.GLOBENT; nr<=KEYALL.LOCNO; nr++)
231 {
232 if ( (strncmp(keypntr->IDENT,kname,(size_t)kstrn) == 0) )
233 {
234 kc = keypntr->IDENT[16];
235 if ((kc == loclevel) || (kc == loclow))
236 { /* name already there... */
237 status = ERR_KEYBAD;
238 goto end_of_it;
239 }
240 }
241 keypntr ++;
242 }
243
244
245 /* calculate length in bytes + eventual front padding bytes */
246
247 if (k_typ[0] == 'C')
248 {
249 pad = 0;
250 byt_len = noelem*bytelem;
251 }
252 else
253 {
254 int mm;
255
256 if (k_typ[0] == 'I')
257 sizbyt = II_SIZE;
258 else if (k_typ[0] == 'R')
259 sizbyt = RR_SIZE;
260 else if (k_typ[0] == 'D')
261 sizbyt = DD_SIZE;
262 else
263 sizbyt = SS_SIZE;
264
265 /* pad = sizbyt - 1 - (KEYALL.LOCEND % sizbyt); */
266 mm = sizbyt - 1;
267 pad = mm - (KEYALL.LOCEND & mm);
268 byt_len = noelem * sizbyt;
269 }
270
271
272 /* check, if LOCNAMES full */
273
274 if (KEYALL.LOCNO >= KEYALL.LOCENT-1)
275 {
276 MID_PACKY(); /* try a garbage collection... */
277 if (KEYALL.LOCNO >= KEYALL.LOCENT-1)
278 {
279 status = ERR_KEYOVN;
280 goto end_of_it;
281 }
282 }
283
284
285 /* look, if LOCWORDS full */
286
287 tot_len = pad + byt_len;
288 if (KEYALL.LOCEND+tot_len >= KEYALL.LOCDAT)
289 {
290 MID_PACKY(); /* try a garbage collection... */
291 if (KEYALL.LOCEND+tot_len >= KEYALL.LOCDAT)
292 {
293 status = ERR_KEYOVL;
294 goto end_of_it;
295 }
296 }
297
298
299 /* everything o.k. - do what's necessary */
300
301 KEYALL.LOCNO ++;
302 koffs = KEYALL.LOCEND + 1;
303 KEYALL.LOCEND += tot_len;
304 keypntr = KEYALL.KEYNAMES + KEYALL.LOCNO; /* point to new entry */
305 if (loco == 'L')
306 keypntr->IDENT[16] = loclevel; /* only defined for this very level */
307 else
308 keypntr->IDENT[16] = loclow; /* also defined for lower levels */
309 }
310
311
312 /* -------------------------- */
313 /* now for global keys */
314 /* -------------------------- */
315
316
317 else
318 {
319
320 /* calculate length in bytes + eventual front padding bytes */
321
322 if (k_typ[0] == 'C')
323 {
324 pad = 0;
325 byt_len = noelem*bytelem;
326 }
327 else
328 {
329 if (k_typ[0] == 'I')
330 sizbyt = II_SIZE;
331 else if (k_typ[0] == 'R')
332 sizbyt = RR_SIZE;
333 else if (k_typ[0] == 'D')
334 sizbyt = DD_SIZE;
335 else
336 sizbyt = SS_SIZE;
337
338 pad = sizbyt - 1 - (KEYALL.GLOBEND % sizbyt);
339 byt_len = noelem * sizbyt;
340 }
341
342
343 /* check, if KEYNAMES full */
344
345 if (KEYALL.GLOBNO >= KEYALL.GLOBENT-1)
346 {
347 MID_PACKY(); /* try a garbage collection... */
348 if (KEYALL.GLOBNO >= KEYALL.GLOBENT-1)
349 {
350 status = ERR_KEYOVN;
351 goto end_of_it;
352 }
353 }
354
355
356 /* look, if KEYWORDS full */
357
358 tot_len = pad + byt_len;
359 if (KEYALL.GLOBEND+tot_len >= KEYALL.GLOBDAT)
360 {
361 MID_PACKY(); /* try a garbage collection... */
362 if (KEYALL.GLOBEND+tot_len >= KEYALL.GLOBDAT)
363 {
364 status = ERR_KEYOVL;
365 goto end_of_it;
366 }
367 }
368
369 /* everything o.k. - do what's necessary */
370
371 KEYALL.GLOBNO ++;
372 koffs = KEYALL.GLOBEND + 1;
373 KEYALL.GLOBEND += tot_len;
374 keypntr = KEYALL.KEYNAMES + KEYALL.GLOBNO; /* point to new entry */
375 keypntr->IDENT[16] = ' ';
376 }
377
378
379 /* -------------------------- */
380 /* now the common section */
381 /* -------------------------- */
382
383 memset((void *)keypntr->IDENT,32,(size_t)KEY_NAMELEN); /* int 32 = ' ' */
384 (void) memcpy(keypntr->IDENT,kname,(size_t)kstrn);
385 keypntr->IDENT[15] = k_typ[0];
386 keypntr->BYTELEM = (short int) bytelem;
387 keypntr->NOELEM = noelem;
388 keypntr->LEN = byt_len;
389 if (k_typ[0] == 'C')
390 keypntr->OFFSET = koffs;
391 else
392 keypntr->OFFSET = (koffs + pad) /sizbyt;
393 keypntr->FRPAD = (short int) pad;
394 keypntr->UNIT = 0; /* (keypntr->UNIT = *unit) not used yet... */
395 return ERR_NORMAL;
396
397
398 /* That's it folks... */
399
400 end_of_it:
401 MID_ERROR("MIDAS","MID_DEFKEY:",status,0);
402 return status;
403 }
404
405 /*
406
407 */
408
409 int MID_DELKEY(name)
410
411 /*++++++++++++++++++++++++++++++++++++++++++++++++++
412 .PURPOSE
413 delete a user defined keyword
414 .ALGORITHM
415 check input parameters and update pointers
416 do not fill the data block
417 .RETURN
418 status: Return status if = 0, everything is o.k.
419 --------------------------------------------------*/
420
421 char *name; /* single keyword name, or
422 list of keyword names separated by a comma, or
423 name of file containing keyword names (file.cat) */
424
425 {
426 int status, kentry, n, mm, nval, sta, all;
427 int kid=0, kunit;
428
429 char kname[18], k_typ[4], work[40], prombuf[84];
430
431
432
433 n = CGN_INDEXS(name,".cat"); /* check for keyword catalog */
434 if (n < 1)
435 {
436 sta = 0;
437 all = 0;
438 nval = CGN_COPY(prombuf,name);
439 goto extra_key;
440 }
441
442
443 /* so we have a catalog */
444
445 all = 2;
446 kid = CGN_OPEN(name,READ);
447 if (kid < 0) return (ERR_INPINV);
448
449 cat_loop:
450 sta = 0;
451 nval = osaread(kid,prombuf,80);
452 if (nval < 1) /* EOF - get out */
453 {
454 osaclose(kid);
455 return (ERR_NORMAL);
456 }
457
458 extra_key:
459 n = CGN_EXTRSS(prombuf,nval,',',&sta,kname,KEY_NAMELEN);
460 if (n <= 0) /* line finished */
461 {
462 if (all == 2) goto cat_loop; /* get next record from catalog */
463 return (ERR_NORMAL);
464 }
465
466
467 /* find keyword entry */
468
469 kentry = MID_FNDKEY(kname,k_typ,&mm,&mm,&kunit);
470 if (kentry <= KEYALL.SYSNO)
471 {
472 if (kentry < 0)
473 {
474 (void) snprintf(work,(size_t)40,"keyword %s not found...",kname);
475 SCTPUT(work);
476 goto extra_key; /* see, if we can extract more */
477 }
478 else
479 {
480 status = ERR_KEYBAD; /* system keys may not be deleted... */
481 goto erro_end;
482 }
483 }
484
485
486 keypntr = KEYALL.KEYNAMES + kentry; /* everything o.k. - point to entry */
487 keypntr->IDENT[0] = '\\'; /* mark keyname as deleted */
488
489
490 /* if we delete last key, just change pointers */
491
492 if (kentry == KEYALL.GLOBNO)
493 {
494 while(keypntr->IDENT[0] == '\\') /* may be more to delete... */
495 {
496 KEYALL.GLOBEND -= (keypntr->LEN + keypntr->FRPAD);
497 KEYALL.GLOBNO --;
498 keypntr --;
499 }
500 }
501
502 goto extra_key; /* see, if we can extract more */
503
504 erro_end:
505 if (all == 2) osaclose(kid);
506 return status;
507 }
508
509 /*
510
511 */
512
513 void MID_PACKY()
514 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++
515 .PURPOSE
516 remove delete keywords (header + data space)
517 .ALGORITHM
518 move through keyword structure + recalculate data offset
519 copy header + data into space freed by deleted keyword
520 .RETURNS
521 nothing.
522 -------------------------------------------------------*/
523
524 {
525 int globend, tot_len, pad;
526 int keyno, koffs;
527 int sizbyt = 0;
528 register int nr, mr;
529
530 char tt;
531 char *cpntra, *cpntrb;
532 int *ipntra, *ipntrb;
533 float *fpntra, *fpntrb;
534 double *dpntra, *dpntrb;
535 size_t *spntra, *spntrb;
536
537 struct KEY_STRUCT *keypntra;
538
539
540 /* loop through GLOBNAMES from 0 till current KEYALL.GLOBNO */
541
542 keypntr = KEYALL.KEYNAMES;
543 keyno = -1; globend = -1;
544
545 for (nr=0; nr<=KEYALL.GLOBNO; nr++) /* KEYALL.GLOBNO is last entry used */
546 {
547 if(keypntr->IDENT[0] == '\\')
548 {
549 keypntra = keypntr + 1;
550 for (mr=nr+1; mr<=KEYALL.GLOBNO; mr++)
551 { /* search for 1. rela entry */
552 if(keypntra->IDENT[0] != '\\')
553 {
554 koffs = globend + 1;
555
556 (void) strncpy(keypntr->IDENT,keypntra->IDENT,16);
557 keypntr->BYTELEM = keypntra->BYTELEM;
558 keypntr->NOELEM = keypntra->NOELEM;
559 keypntr->LEN = keypntra->LEN;
560 keypntr->UNIT = keypntra->UNIT;
561
562 tt = keypntr->IDENT[15];
563 if (tt == 'C')
564 {
565 keypntr->FRPAD = 0;
566 keypntr->OFFSET = koffs;
567 tot_len = keypntr->BYTELEM * keypntr->NOELEM;
568 cpntra = KCWORDS + keypntr->OFFSET;
569 cpntrb = KCWORDS + keypntra->OFFSET;
570 CGN_COPYALL(cpntra,cpntrb,tot_len);
571 }
572 else
573 {
574 register int kr;
575
576 if (tt == 'I')
577 sizbyt = II_SIZE;
578 else if (tt == 'R')
579 sizbyt = RR_SIZE;
580 else if (tt == 'D')
581 sizbyt = DD_SIZE;
582 else
583 sizbyt = SS_SIZE;
584
585 pad = sizbyt - 1 - (globend % sizbyt);
586 keypntr->FRPAD = pad;
587 keypntr->OFFSET = (koffs + pad) / sizbyt;
588 tot_len = pad + (keypntr->NOELEM * sizbyt);
589
590 if (tt == 'I')
591 {
592 ipntra = KIWORDS + keypntr->OFFSET;
593 ipntrb = KIWORDS + keypntra->OFFSET;
594 for (kr=0; kr<keypntr->NOELEM; kr++)
595 *ipntra++ = *ipntrb++;
596 }
597 else if (tt == 'R')
598 {
599 fpntra = KRWORDS + keypntr->OFFSET;
600 fpntrb = KRWORDS + keypntra->OFFSET;
601 for (kr=0; kr<keypntr->NOELEM; kr++)
602 *fpntra++ = *fpntrb++;
603 }
604 else if (tt == 'D')
605 {
606 dpntra = KDWORDS + keypntr->OFFSET;
607 dpntrb = KDWORDS + keypntra->OFFSET;
608 for (kr=0; kr<keypntr->NOELEM; kr++)
609 *dpntra++ = *dpntrb++;
610 }
611 else
612 {
613 spntra = KSWORDS + keypntr->OFFSET;
614 spntrb = KSWORDS + keypntra->OFFSET;
615 for (kr=0; kr<keypntr->NOELEM; kr++)
616 *spntra++ = *spntrb++;
617 }
618 }
619
620 keypntra->IDENT[0] = '\\'; /* mark copied key as deleted */
621 goto join;
622 }
623 keypntra ++;
624 }
625
626 goto end_of_it; /* only deleted keys in the end */
627 }
628
629 else
630 { /* calculate last byte of data space */
631 tt = keypntr->IDENT[15];
632 if (tt == 'C')
633 tot_len = keypntr->BYTELEM * keypntr->NOELEM;
634 else
635 {
636 if (tt == 'I')
637 sizbyt = II_SIZE;
638 else if (tt == 'R')
639 sizbyt = RR_SIZE;
640 else if (tt == 'D')
641 sizbyt = DD_SIZE;
642 else
643 sizbyt = SS_SIZE;
644
645 if (globend < 0)
646 pad = 0;
647 else
648 pad = sizbyt - 1 -(globend % sizbyt);
649 tot_len = pad + (keypntr->NOELEM * sizbyt);
650 }
651 }
652
653 join:
654 globend += tot_len;
655 keyno ++;
656
657 keypntr ++;
658 }
659
660
661 end_of_it:
662 KEYALL.GLOBNO = keyno;
663 KEYALL.GLOBEND = globend;
664 }
665
666