Debian Codegraph @

libsrc/st/midkeyc.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2010 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 MIDKEYA +++++++++++++++++++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION Module MIDKEYA
31 .AUTHOR Klaus Banse ESO - Garching
32 .KEYWORDS Midas keyword utility routines.
33 .ENVIRONMENT VMS and UNIX
34 .COMMENT
35 holds IPROMPT, RPROMPT, CPROMPT, DPROMPT
36 KEYFILE
37
38 .VERSION [3.00] 920129: split off original midkey.c
39
40 100616 last modif
41 ------------------------------------------------------------------------*/
42
43 #include <string.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46
47 #include <fileexts.h>
48
49 /*
50
51 */
52
53 int MID_CPROMPT(p_string,noval,carray,nullo)
54
55 /*++++++++++++++++++++++++++++++++++++++++++++++++++
56 .PURPOSE
57 display a prompt string on the terminal + read the user input
58 .ALGORITHM
59 use MID_TPRO to display prompt string + get user input as character string
60 convert this string to relevant data via CGN_CNVT
61 .RETURNS
62 status: I*4 return status
63 --------------------------------------------------*/
64
65 char *p_string /* IN : prompt string (null terminated) */;
66 int *noval /* IO : length of expected input */;
67 char *carray /* OUT: input data for type = 3 */;
68 int *nullo /* OUT: no. of null values in input */;
69
70 {
71 int save_val, count;
72 register int nr;
73
74 char work[96], prombuf[96];
75
76
77 save_val = *noval; /* save noval */
78 *noval = 0; /* init to 0 ... */
79
80 /* store prompt string in logfile */
81
82 (void) MID_LOG('G',p_string,(int)strlen(p_string));
83
84
85 /* cut off trailing blanks of prompt + read a line from the terminal */
86
87 CGN_CUTOFF(p_string,prombuf);
88 MID_TPRO(prombuf,work,80);
89
90 count = (int) strlen(work); /* test, if something came in */
91 if (count <= 0) return ERR_NODATA;
92
93
94 /* do not pass more values than required */
95
96 (void) MID_LOG('G',work,count);
97 if (save_val <= count)
98 count = save_val;
99 else
100 memset((void *)carray,32,(size_t)save_val); /* fill first with blanks */
101 *noval = count;
102
103 *nullo = 0;
104 for (nr=0; nr<count; nr++)
105 {
106 *carray++ = work[nr];
107 if (work[nr] == NUL_CVAL) (*nullo) ++;
108 }
109
110 return ERR_NORMAL;
111 }
112 /*
113
114 */
115
116 int MID_DPROMPT(p_string,noval,array,nullo)
117
118 /*++++++++++++++++++++++++++++++++++++++++++++++++++
119 .PURPOSE
120 display a prompt string on the terminal + read the user input
121 .ALGORITHM
122 use MID_TPRO to display prompt string + get user input as character string
123 convert this string to relevant data via CGN_CNVT
124 .RETURNS
125 status: I*4 return status
126 --------------------------------------------------*/
127
128 char *p_string /* IN : prompt string (null terminated) */;
129 int *noval /* IO : length of expected input */;
130 double *array /* OUT: input data */;
131 int *nullo /* OUT: no. of null values in input */;
132
133 {
134 int save_val, status, mm, iar;
135 register int nr;
136
137 float rar;
138
139 char work[96], prombuf[96];
140
141
142
143 save_val = *noval; /* save noval */
144 *noval = 0; /* init to 0 ... */
145
146 /* store prompt string in logfile */
147
148 (void) MID_LOG('G',p_string,(int)strlen(p_string));
149
150
151 /* cut off trailing blanks of prompt + read a line from the terminal */
152
153 CGN_CUTOFF(p_string,prombuf);
154 MID_TPRO(prombuf,work,80);
155
156 mm = (int) strlen(work); /* test, if something came in */
157 if (mm <= 0) return ERR_NODATA;
158
159 (void) MID_LOG('G',work,mm);
160 mm = CGN_CNVT(work,4,save_val,&iar,&rar,array);
161 if (mm <= 0)
162 {
163 status = ERR_INPINV; /* conversion error... */
164 goto end_of_it;
165 }
166 else
167 status = ERR_NORMAL;
168
169 *noval = mm;
170 *nullo = 0;
171 for (nr=0; nr<mm; nr++) /* look for null values */
172 {
173 if (array[nr] == NUL_DVAL) (*nullo) ++;
174 }
175
176 end_of_it:
177 if (status != ERR_NORMAL)
178 MID_ERROR("MIDAS","MID_DPROMPT",status,0);
179
180 return status;
181 }
182 /*
183
184 */
185
186 int MID_IPROMPT(p_string,noval,array,nullo)
187
188 /*++++++++++++++++++++++++++++++++++++++++++++++++++
189 .PURPOSE
190 display a prompt string on the terminal + read the user input
191 .ALGORITHM
192 use MID_TPRO to display prompt string + get user input as character string
193 convert this string to relevant data via CGN_CNVT
194 .RETURNS
195 status: I*4 return status
196 --------------------------------------------------*/
197
198 char *p_string /* IN : prompt string (null terminated) */;
199 int *noval /* IO : length of expected input */;
200 int *array /* OUT: input data */;
201 int *nullo /* OUT: no. of null values in input */;
202
203 {
204 int save_val, status, mm;
205 register int nr;
206
207 float rar;
208 double dar;
209
210 char work[96], prombuf[96];
211
212
213 save_val = *noval; /* save noval */
214 *noval = 0; /* init to 0 ... */
215
216 /* store prompt string in logfile */
217
218 (void) MID_LOG('G',p_string,(int)strlen(p_string));
219
220
221 /* cut off trailing blanks of prompt + read a line from the terminal */
222
223 CGN_CUTOFF(p_string,prombuf);
224 MID_TPRO(prombuf,work,80);
225
226 mm = (int) strlen(work); /* test, if something came in */
227 if (mm <= 0) return ERR_NODATA;
228
229 (void) MID_LOG('G',work,mm);
230 mm = CGN_CNVT(work,1,save_val,array,&rar,&dar);
231 if (mm <= 0)
232 {
233 status = ERR_INPINV; /* conversion error... */
234 goto end_of_it;
235 }
236 else
237 status = ERR_NORMAL;
238
239 *noval = mm;
240 *nullo = 0;
241 for (nr=0; nr<mm; nr++) /* look for null values */
242 {
243 if (array[nr] == NUL_IVAL) (*nullo) ++;
244 }
245
246 end_of_it:
247 if (status != ERR_NORMAL)
248 MID_ERROR("MIDAS","MID_IPROMPT",status,0);
249
250 return status;
251 }
252 /*
253
254 */
255
256 int MID_RPROMPT(p_string,noval,array,nullo)
257
258 /*++++++++++++++++++++++++++++++++++++++++++++++++++
259 .PURPOSE
260 display a prompt string on the terminal + read the user input
261 .ALGORITHM
262 use MID_TPRO to display prompt string + get user input as character string
263 convert this string to relevant data via CGN_CNVT
264 .RETURNS
265 status: I*4 return status
266 --------------------------------------------------*/
267
268 char *p_string /* IN : prompt string (null terminated) */;
269 int *noval /* IO : length of expected input */;
270 float *array /* OUT: input data */;
271 int *nullo /* OUT: no. of null values in input */;
272
273 {
274 int save_val, status, mm, iar;
275 register int nr;
276
277 char work[96], prombuf[96];
278
279 double dar;
280
281
282 save_val = *noval; /* save noval */
283 *noval = 0; /* init to 0 ... */
284
285 /* store prompt string in logfile */
286
287 (void) MID_LOG('G',p_string,(int)strlen(p_string));
288
289
290 /* cut off trailing blanks of prompt + read a line from the terminal */
291
292 CGN_CUTOFF(p_string,prombuf);
293 MID_TPRO(prombuf,work,80);
294
295 mm = (int) strlen(work); /* test, if something came in */
296 if (mm <= 0) return ERR_NODATA;
297
298 (void) MID_LOG('G',work,mm);
299 mm = CGN_CNVT(work,2,save_val,&iar,array,&dar);
300 if (mm <= 0)
301 {
302 status = ERR_INPINV; /* conversion error... */
303 goto end_of_it;
304 }
305 else
306 status = ERR_NORMAL;
307
308 *noval = mm;
309 *nullo = 0;
310 for (nr=0; nr<mm; nr++) /* look for null values */
311 {
312 if (array[nr] == NUL_DVAL) (*nullo) ++;
313 }
314
315 end_of_it:
316 if (status != ERR_NORMAL)
317 MID_ERROR("MIDAS","MID_RPROMPT",status,0);
318
319 return status;
320 }
321 /*
322
323 */
324
325 int MID_KEYFILE(progname)
326
327 /*++++++++++++++++++++++++++++++++++++++++++++++++++
328 .PURPOSE
329 read keyword values from ASCII file progname.key
330 .ALGORITHM
331 .RETURNS
332 status: int return status
333 --------------------------------------------------*/
334
335 char *progname; /* IN : name of calling program */
336
337 {
338 int slen, cl, nc, nb, fid, m, n;
339 int *ibuf, unit, start, first, noelem, sbytelem, bytelem;
340 int stat = 0;
341 register int nr;
342
343 float *rbuf, rwa;
344
345 double *dbuf, dwa;
346
347 char *cbuf, cc[4], buff[128];
348 char keyname[32], type[16], k_type[4], substr[24];
349
350 void *work_str;
351
352
353
354
355 (void) strncpy(buff,progname,120);
356 buff[120] = '\0'; /* so we have an end... */
357 m = CGN_INDEXC(buff,' ');
358 if (m < 0)
359 (void) strcat(buff,".KEY");
360 else
361 (void) strcpy(&buff[m],".KEY");
362
363 nc = 0;
364 fid = osaopen(buff,READ);
365 if (fid < 0) return (ERR_FRMNAC);
366
367 work_str = malloc((size_t)200);
368
369 dbuf = (double *)work_str;
370 ibuf = (int *)work_str;
371 rbuf = (float *)work_str;
372 cbuf = (char *)work_str;
373
374 read_loop:
375 memset((void *)buff,32,(size_t)80); /* clear read buffer */
376 slen = osaread(fid,buff,80);
377 if (slen < 0)
378 {
379 stat = 0;
380 goto file_end; /* EOF reached */
381 }
382
383 nc ++;
384 if (slen == 0) goto read_loop;
385
386 /*
387 printf("record read: %s\n",buff);
388 */
389
390 /* we've got some stuff */
391
392 for (nr=0; nr<slen; nr++)
393 {
394 if ((buff[nr] != ' ') && (buff[nr] != '\t')) goto text_a;
395 }
396 goto read_loop; /* skip empty lines */
397
398 text_a:
399 CGN_strcpy(buff,&buff[nr]);
400 if (buff[0] == '!') goto read_loop; /* skip comments */
401
402 cl = CGN_INDEXC(buff,' ');
403 if (cl < 0)
404 {
405 printf("invalid syntax - line %d skipped...\n",nc);
406 goto read_loop;
407 }
408
409 buff[cl] = '\0';
410 CGN_UPSTR(buff);
411
412 slen = CGN_INDEXC(buff,'/');
413 (void) strncpy(keyname,buff,(size_t)slen);
414 keyname[slen] = '\0';
415 start = slen + 1;
416 slen = CGN_EXTRSS(buff,cl,'/',&start,type,15);
417
418 MID_TYPCHK(type,cc,&bytelem);
419 if (type[0] == ' ')
420 {
421 printf("bad keytype - line %d skipped...\n",nc);
422 goto read_loop;
423 }
424
425 slen = CGN_EXTRSS(buff,cl,'/',&start,substr,20);
426 if (slen < 1)
427 {
428 printf("bad start element - line %d skipped...\n",nc);
429 goto read_loop;
430 }
431
432 n = CGN_CNVT(substr,1,1,&first,&rwa,&dwa);
433 if (n < 1)
434 {
435 printf("bad start element - line %d skipped...\n",nc);
436 goto read_loop;
437 }
438
439 slen = CGN_EXTRSS(buff,cl,'/',&start,substr,20);
440 n = CGN_CNVT(substr,1,1,&noelem,&rwa,&dwa);
441 if (n < 1)
442 {
443 printf("bad noelem - line %d skipped...\n",nc);
444 goto read_loop;
445 }
446
447 n = MID_FNDKEY(keyname,k_type,&sbytelem,&nb,&unit);
448 if (n >= 0)
449 {
450 if ( (type[0] != k_type[0]) || (bytelem != sbytelem) )
451 {
452 printf("wrong keytype - line %d skipped...\n",nc);
453 goto read_loop;
454 }
455 }
456 else
457 {
458 slen = noelem + first - 1;
459 m = MID_DEFKEY(keyname,' ',type,slen,&unit);
460 if (stat != ERR_NORMAL)
461 {
462 printf("problem creating keyword - line %d skipped...\n",nc);
463 goto read_loop;
464 }
465 }
466
467 if (type[0] == 'I')
468 nb = 1;
469 else if (type[0] == 'R')
470 nb = 2;
471 else if (type[0] == 'C')
472 nb = 3;
473 else
474 nb = 4;
475
476 CGN_strcpy(buff,&buff[cl+1]);
477 slen = (int) strlen(buff);
478 for (nr=0; nr<slen; nr++)
479 {
480 if ((buff[nr] != ' ') && (buff[nr] != '\t')) goto text_b;
481 }
482 printf("missing data - line %d skipped...\n",nc);
483 goto read_loop;
484
485 text_b:
486 if (nr > 0) CGN_strcpy(buff,&buff[nr]);
487
488 /*
489 printf("data: %s\nnb = %d, first = %d, noelem = %d\n",buff,nb,first,noelem);
490 */
491
492 if (nb != 3)
493 {
494 m = CGN_CNVT(buff,nb,noelem,ibuf,rbuf,dbuf);
495 if (m > noelem) m = noelem;
496
497 if (nb == 1)
498 stat = SCKWRI(keyname,ibuf,first,m,&unit);
499 else if (nb == 2)
500 stat = SCKWRR(keyname,rbuf,first,m,&unit);
501 else
502 stat = SCKWRD(keyname,dbuf,first,m,&unit);
503 }
504
505 else
506 {
507 m = noelem * bytelem; /* total size */
508 if (m > 200)
509 {
510 printf("data overflow (> 200) - line %d skipped...\n",nc);
511 goto read_loop;
512 }
513
514 slen = (int) strlen(buff) - 1;
515 if ( (buff[0] == '"') && (slen > 1) && (buff[slen] == '"') )
516 {
517 cbuf = &buff[1];
518 buff[slen--] = '\0';
519 }
520 else
521 {
522 cbuf = &buff[0];
523 slen++;
524 }
525
526 if (slen < m)
527 {
528 for (nr=slen; nr<m; nr++) cbuf[nr] = ' '; /* pad with blanks */
529 }
530
531 stat = SCKWRC(keyname,bytelem,cbuf,first,noelem,&unit);
532 }
533 if (stat != ERR_NORMAL)
534 printf("problem filling keyword - line %d skipped...\n",nc);
535
536 goto read_loop;
537
538
539 file_end:
540 osaclose(fid);
541
542 return stat;
543 }
544 /*
545
546 */
547
548 int MID_SPROMPT(p_string,noval,array,nullo)
549
550 /*++++++++++++++++++++++++++++++++++++++++++++++++++
551 .PURPOSE
552 display a prompt string on the terminal + read the user input
553 .ALGORITHM
554 use MID_TPRO to display prompt string + get user input as character string
555 convert this string to relevant data via CGN_xCNVT
556 .RETURNS
557 return status
558 --------------------------------------------------*/
559
560 char *p_string /* IN : prompt string (null terminated) */;
561 int *noval /* IO : length of expected input */;
562 size_t *array /* OUT: input data */;
563 int *nullo /* OUT: no. of null values in input */;
564
565 {
566 int save_val, status, mm, iar;
567 register int nr;
568
569 float rar;
570 double dar;
571
572 char work[96], prombuf[96];
573
574 size_t snul;
575
576
577
578
579 save_val = *noval; /* save noval */
580 *noval = 0; /* init to 0 ... */
581
582 /* store prompt string in logfile */
583
584 (void) MID_LOG('G',p_string,(int)strlen(p_string));
585
586
587 /* cut off trailing blanks of prompt + read a line from the terminal */
588
589 CGN_CUTOFF(p_string,prombuf);
590 MID_TPRO(prombuf,work,80);
591
592 mm = (int) strlen(work); /* test, if something came in */
593 if (mm <= 0) return ERR_NODATA;
594
595 (void) MID_LOG('G',work,mm);
596 mm = CGN_xCNVT(work,5,save_val,&iar,&rar,&dar,array);
597 if (mm <= 0)
598 {
599 status = ERR_INPINV; /* conversion error... */
600 goto end_of_it;
601 }
602 else
603 status = ERR_NORMAL;
604
605 *noval = mm;
606 *nullo = 0;
607 snul = (size_t)NUL_DVAL;
608 for (nr=0; nr<mm; nr++) /* look for null values */
609 {
610 if (array[nr] == snul) (*nullo) ++;
611 }
612
613 end_of_it:
614 if (status != ERR_NORMAL)
615 MID_ERROR("MIDAS","MID_DPROMPT",status,0);
616
617 return status;
618 }
619