Debian Codegraph @

libsrc/st/cgnc.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 /*+++++++++++++++++++ CGN interfaces part C +++++++++++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION Module CGNC
31 .COMMENTS
32 holds DISPFIL, EXTRSS, COPY, GETLIN, GETLINE, DATE
33 .AUTHOR K. Banse ESO - Garching
34 .KEYWORDS
35 name translation, parsing, filling
36 .ENVIRONMENT VMS and UNIX
37
38 .VERSION [1.00] 920213: pulled over from cgna.c
39
40 100616 last modif
41 -----------------------------------------------------------------------------*/
42
43 #include <fileexts.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46
47 #define READ 0
48 /*
49
50 */
51
52 /*++++++++++++++++++++++++++++++++++++++++++++++++++
53 .PURPOSE
54 display part of a file
55 .ALGORITHM
56 look for prefix//label
57 the display until prefix//... is encountered
58 .RETURN
59 stat: I*4 = 0, if ok, else -1
60 --------------------------------------------------*/
61
62 #ifdef __STDC__
63 int CGN_DISPFIL(int color, char *file ,char *prefix ,char *label)
64
65 #else
66 int CGN_DISPFIL(color,file,prefix,label)
67 int color; /* IN: color no. for string, like for SET/GRAPH:
68 0 = default, i.e. no colour setting,
69 1 = black, 2 = red, 3 = green,
70 4 = blue, 5 = yellow, 6 = magenta,
71 7 = cyan, 8 = white */
72 char *file; /* IN : file name */
73 char *prefix; /* IN : prefix string */
74 char *label; /* IN : label string */
75 #endif
76
77 {
78 int reclen, jmm, jkk, fp, reto;
79
80 char compo[32];
81 char work[168];
82
83
84
85 reto = -1; /* init to BAD */
86
87
88 /* open file */
89
90 fp = CGN_OPEN(file,READ);
91 if (fp == -1) return (reto);
92
93 jmm = CGN_COPY(compo,prefix);
94 jkk = CGN_COPY(&compo[jmm],label) + jmm; /* length of search string */
95
96
97 read_loop:
98 reclen = osaread(fp,work,80);
99
100 if (reclen == 0)
101 goto read_loop;
102 else if (reclen == -1)
103 goto end_of_it;
104
105
106 if (jmm == 0)
107 {
108 SCTPUT(work);
109 reto = 0;
110 goto read_loop;
111 }
112
113
114 if (strncmp(work,compo,jkk) == 0)
115 {
116 inner_loop:
117 reclen = osaread(fp,work,80);
118
119 if (reclen == 0)
120 goto inner_loop;
121 else if ( (reclen == -1) || (strncmp(work,prefix,jmm) == 0) )
122 goto end_of_it;
123
124 SCTMES(color,work); /* so we also log */
125 reto = 0;
126 goto inner_loop;
127 }
128
129 goto read_loop;
130
131
132 end_of_it:
133 (void) osaclose(fp);
134 return (reto);
135 }
136 /*
137
138 */
139
140 /*++++++++++++++++++++++++++++++++++++++++++++++++++
141 .PURPOSE
142 get one line of a file
143 .ALGORITHM
144 look for prefix//label
145 then get the following line
146 .RETURN
147 status = 0, if ok, else -1
148 --------------------------------------------------*/
149
150 #ifdef __STDC__
151 int CGN_GETLIN(char *file ,char *prefix ,char *label, char *line)
152
153 #else
154 int CGN_GETLIN(file,prefix,label,line)
155 char *file; /* IN : file name */
156 char *prefix; /* IN : prefix string */
157 char *label; /* IN : label string */
158 char *line; /* OUT : buffer where the file record is copied to */
159 #endif
160
161 {
162 int reclen, jmm, jkk, fp, reto;
163
164 char compo[32], work[168];
165
166
167
168
169 reto = -1;
170
171
172 /* open file */
173
174 fp = CGN_OPEN(file,READ);
175 if (fp == -1) return (reto);
176
177 jmm = CGN_COPY(compo,prefix);
178 jkk = CGN_COPY(&compo[jmm],label) + jmm; /* length of search string */
179
180
181 read_loop:
182 reclen = osaread(fp,work,80);
183
184 if (reclen == 0)
185 goto read_loop;
186 else if (reclen == -1)
187 goto end_of_it;
188
189
190 if (jmm == 0)
191 {
192 (void) strcpy(line,work);
193 reto = 0;
194 goto end_of_it;
195 }
196
197
198 if (strncmp(work,compo,jkk) == 0)
199 {
200 inner_loop:
201 reclen = osaread(fp,work,80);
202
203 if (reclen == 0)
204 goto inner_loop;
205 else if (reclen == -1)
206 goto end_of_it;
207
208 (void) strcpy(line,work);
209 reto = 0;
210 goto end_of_it;
211 }
212
213 goto read_loop;
214
215
216 end_of_it:
217 (void) osaclose(fp);
218 return (reto);
219 }
220 /*
221
222 */
223
224 /*++++++++++++++++++++++++++++++++++++++++++++++++++
225 .PURPOSE
226 from a given string extract a substring limited by given delimiter
227 .ALGORITHM
228 find the given delimiter in the string
229 copy the relevant part into the substring
230 spaces in front of delimiter are ignored
231 .RETURN
232 -3 if input string only blanks
233 -2 if extracted substring would be larger than `ssl' chars.
234 -1 if end of main string reached,
235 else the length of extracted substring
236 if two consecutive delimiters => length = 0
237 ------------------------------------------------------------------*/
238
239 #ifdef __STDC__
240 int CGN_EXTRSS(char *string, int lstring, char delim, int *start, char *ss,
241 int ssl)
242 #else
243 int CGN_EXTRSS(string,lstring,delim,start,ss,ssl)
244 char *string; /* IN: character string which is to be parsed */
245 int lstring; /* IN: length of above */
246 char delim; /* IN: delimiter char. */
247 int *start; /* IO: starting index within the input
248 string - will be set to the position
249 just following the next delimiter
250 upon return */
251 char *ss; /* OUT: extracted substring,
252 will be set to null string if nothing there */
253 int ssl; /* IN: length of 'ss' */
254 #endif
255
256 {
257 int backsl;
258 register int slen, off, soss, mm, aapo, apo, bpo, eos;
259
260 register char cc, sc, *cptr, *tptr;
261
262
263
264 *ss = '\0'; /* init substr `ss' to zero length */
265 mm = *start;
266 if (lstring <= mm) return (-1); /* already end of string reached */
267
268
269 /* skip leading blanks */
270
271 soss = mm;
272 while(1)
273 {
274 cc = string[soss];
275 if (cc == '\0')
276 return (-3); /* only blank string */
277 else if (cc != ' ')
278 break;
279 soss ++;
280 }
281
282
283 eos = 1;
284 backsl = 0; /* backslash flag */
285 aapo = 0; /* start_with_quote flag */
286 apo = bpo = 0; /* quote switches */
287
288
289 /* skip over characters until we hit delimiter or end of string */
290
291 off = soss;
292 while(1)
293 { /* 1st time: `cc' already set above */
294 if (cc == delim)
295 {
296 if (apo == 0) goto done_it;
297 }
298
299 else if (cc == '\0')
300 break;
301
302 else if (cc == '"')
303 {
304 if ((off > 0) && (string[off-1] == '\\'))
305 backsl ++;
306 else
307 { /* look at next char. */
308 sc = string[off+1];
309
310 if (aapo == 1) /* we started with `"' */
311 {
312 if (sc == '\0')
313 apo = 0;
314 else if (sc == delim)
315 { /* test for _____" in the end */
316 bpo = 1 - bpo;
317
318 if (bpo == 1)
319 apo = 0; /* even no. of ", o.k. */
320 else
321 {
322 register int nr;
323
324 nr = off + 2;
325 sc = string[nr];
326 while (sc == delim)
327 {
328 nr ++;
329 sc = string[nr];
330 }
331 if (sc == '"') /* we found trailing blank */
332 {
333 sc = string[nr+1];
334 if (sc != '\0') apo = 0;
335 }
336 }
337 }
338 else if (sc != '"') /* otherwise, update switch */
339 bpo = 1 - bpo;
340 }
341
342 else
343 {
344 if (sc != '"') apo = 1 - apo;
345 if (off == soss) aapo = 1; /* indicate "... string */
346 }
347 }
348 }
349 cc = string[++off];
350 }
351
352
353 /* no delimiter found or `apo' was 1 */
354
355 if (apo == 1) /* uneven no. of quotes */
356 {
357 for (off=soss; ; off++) /* pass through string again */
358 {
359 cc = string[off];
360 if (cc == '\0')
361 break;
362 else if (cc == delim)
363 goto done_it;
364 }
365 }
366
367 eos = 0;
368
369
370 /* yes, delimiter found */
371
372 done_it:
373 slen = off - soss; /* length of substring */
374
375 if (slen > ssl) /* substring too long ... */
376 { /* copy only what's possible */
377 if (backsl > 0)
378 {
379 if ((slen-backsl) < ssl) goto ok_end;
380
381 tptr = ss;
382 cptr = string + soss;
383 for (mm=0; mm<ssl; mm++)
384 {
385 if ((*cptr == '\\') && (*(cptr+1) == '"'))
386 cptr++;
387 else
388 *tptr++ = *cptr++;
389 }
390 }
391 else
392 (void) memcpy(ss,(string+soss),(size_t)ssl);
393 return(-2);
394 }
395
396
397
398 /* all o.k. - copy string + update `start' */
399
400 ok_end:
401 if (backsl > 0)
402 {
403 tptr = ss;
404 cptr = string + soss;
405 for (mm=0; mm<slen; mm++)
406 {
407 if ((*cptr == '\\') && (*(cptr+1) == '"'))
408 cptr++;
409 else
410 *tptr++ = *cptr++;
411 }
412 slen -= backsl; /* update length of extracted substring */
413 *tptr = '\0';
414 }
415 else
416 {
417 (void) memcpy(ss,(string+soss),(size_t)slen);
418 ss[slen] = '\0';
419 }
420
421 *start = off + eos;
422 return (slen);
423 }
424 /*
425
426 */
427
428 int CGN_COPY(outstr,instr)
429
430 /*++++++++++++++++++++++++++++++++++++++++++++++++++
431 .PURPOSE
432 copy char. strings + return no. of chars copied (excluding the \0 )
433 .RETURN
434 no. of chars. copied (may be = 0)
435 --------------------------------------------------*/
436
437 char *instr; /* input string */
438 char *outstr; /* output string */
439
440 {
441 register char cc, *base;
442
443
444
445 base = outstr;
446
447 while ((cc = *instr++)) *outstr++ = cc;
448
449 *outstr = '\0';
450 return ((int)(outstr-base));
451
452 }
453 /*
454
455 */
456
457 /*++++++++++++++++++++++++++++++++++++++++++++++++++
458 .PURPOSE
459 check, if year is leap year
460
461 "Most years ending in "00" are not leap
462 years, but those divisible by 400 (including 2000)
463 are. The Julian calendar, authorized by Julius
464 Caesar in 46 B.C., assumed that the year had
465 365 1/4 days, with a 366-day leap year added
466 every fourth year.
467 In A.D. 730, an Anglo-Saxon monk, the Venerable
468 Bede, calculated that the Julian year was 11 minutes
469 and 14 seconds too long, an error of about one day
470 every 128 years. But nothing was done about it for
471 800 years. In 1582, the accumulated error was
472 estimated at 10 days, and Pope Gregory XIII decreed
473 that the day following Oct. 4 would be Oct. 15.
474 To make future adjustments for the error
475 (about three days every 400 years), it was decided
476 that years ending in "00" would be common years rather
477 than leap years -- except those divisible by 400.
478 So 1600 was a leap year and 2000 also will be,
479 but 1700, 1800 and 1900 were not."
480
481 .RETURN return 1 if leap year, else 0
482 --------------------------------------------------*/
483
484 #ifdef __STDC__
485 int leapyr(int year)
486
487 #else
488 int leapyr(year)
489 int year;
490 #endif
491
492 {
493
494 if ( ( ((year%4) == 0) && ((year%100) != 0) ) ||
495 ((year%400) == 0) )
496 return 1;
497 else
498 return 0;
499
500 }
501 /*
502
503 */
504
505 /*++++++++++++++++++++++++++++++++++++++++++++++++++
506 .KEYWORDS
507 current date
508 .PURPOSE
509 return current date from host system incremented by `daydiff'
510 .ALGORITHM
511 get current date via OSY_ASCTIM and convert it
512 .RETURNS
513 actual length of date string
514 --------------------------------------------------*/
515
516 #ifdef __STDC__
517 int CGN_DATE(int isoflag, int daydiff, char *s)
518
519 #else
520 int CGN_DATE(isoflag,daydiff,s)
521 int isoflag; /* IN: 0 -> get date as yyyy-mm-dd
522 1 -> get date as yyyy-mm-ddThh:mm:ss (ISO 8601)
523 in this case `daydiff' is ignored... */
524 int daydiff; /* IN: 0 -> get current date, else get date + daydiff */
525 char *s; /* OUT: date string */
526 #endif
527
528 {
529 char base[40], newbase[12], daysign;
530 register char cc;
531
532 int leng, adiff;
533 int ibuf[3], iyear, imonth, iday;
534 static int modays[12]={31,28,31,30,31,30,31,31,30,31,30,31};
535 register int mr, nr;
536
537 float rval;
538
539 double dval;
540
541
542
543
544 OSY_ASCTIM(base);
545 mr = CGN_INDEXC(base,' ');
546 CGN_strcpy(base,&base[mr+1]);
547
548
549 mr = 7; /* point to begin of year string */
550 for (nr=0; nr<4; nr++) newbase[nr] = base[mr++];
551
552 newbase[4] = '-';
553 newbase[5] = '0';
554 cc = base[3];
555
556 if (cc == 'J')
557 {
558 if (base[4] == 'a')
559 newbase[6] = '1'; /* January */
560 else
561 {
562 if (base[5] == 'n')
563 newbase[6] = '6'; /* June */
564 else
565 newbase[6] = '7'; /* July */
566 }
567 }
568 else if (cc == 'F') /* February */
569 newbase[6] = '2';
570 else if (cc == 'M')
571 {
572 if (base[5] == 'r')
573 newbase[6] = '3'; /* March */
574 else
575 newbase[6] = '5'; /* May */
576 }
577 else if (cc == 'A')
578 {
579 if (base[4] == 'p')
580 newbase[6] = '4'; /* April */
581 else
582 newbase[6] = '8'; /* August */
583 }
584 else if (cc == 'S') /* September */
585 newbase[6] = '9';
586 else if (cc == 'O') /* October */
587 {
588 newbase[5] = '1';
589 newbase[6] = '0';
590 }
591 else if (cc == 'N') /* November */
592 {
593 newbase[5] = '1';
594 newbase[6] = '1';
595 }
596 else /* December */
597 {
598 newbase[5] = '1';
599 newbase[6] = '2';
600 }
601
602 newbase[7] = '-';
603 newbase[8] = base[0];
604 newbase[9] = base[1];
605 newbase[10] = '\0';
606
607
608 if (isoflag == 1) /* ISO date only for today! */
609 {
610 newbase[10] = 'T';
611 newbase[11] = '\0';
612 leng = CGN_COPY(s,newbase);
613
614 while(base[mr] == ' ') mr++;
615 nr = CGN_COPY(s+leng,&base[mr]);
616 leng += nr;
617 return(leng);
618 }
619
620 else if (daydiff == 0)
621 {
622 leng = CGN_COPY(s,newbase);
623 return(leng);
624 }
625
626
627 /* here the real work starts */
628
629 if (daydiff < 0)
630 {
631 daysign = '-';
632 adiff = -daydiff;
633 }
634 else
635 {
636 daysign = '+';
637 adiff = daydiff;
638 }
639
640
641 more_days:
642 if (daysign == '-')
643 daydiff = -1;
644 else
645 daydiff = 1;
646 adiff --;
647
648 (void) strcpy(base,newbase);
649 base[4] = ','; base[7] = ',';
650
651 (void) CGN_CNVT(base,1,3,ibuf,&rval,&dval);
652 iyear = ibuf[0]; imonth = ibuf[1]; iday = ibuf[2];
653
654
655 /* here we handle the one day difference */
656
657 if (daysign == '+') /* tomorrow */
658 {
659 iday ++; /* get day after */
660 if (imonth != 2) /* worry about February */
661 nr = modays[imonth-1] + 1; /* nr = last day + 1 */
662 else
663 {
664 nr = leapyr(iyear);
665 if (nr == 1) /* leap year */
666 nr = 30;
667 else
668 nr = 29;
669 }
670 if (iday < nr) /* still in same month? */
671 {
672 (void) sprintf(&newbase[8],"%2.2d",iday);
673 goto final_task;
674 }
675
676 imonth ++; /* get month after */
677 if (imonth < 13)
678 {
679 (void) sprintf(&newbase[5],"%2.2d-01",imonth);
680 goto final_task;
681 }
682
683 iyear ++;
684 (void) sprintf(newbase,"%d-01-01",iyear);
685 }
686
687 else /* yesterday */
688 {
689 iday --; /* get day before */
690 if (iday > 0)
691 {
692 (void) sprintf(&newbase[8],"%2.2d",iday);
693 goto final_task;
694 }
695
696 imonth --; /* get month before */
697 if (imonth > 0)
698 {
699 if (imonth != 2) /* worry about February */
700 (void) sprintf(&newbase[5],"%2.2d-%d",imonth,modays[imonth-1]);
701 else
702 {
703 nr = leapyr(iyear);
704 if (nr == 1)
705 (void) strcpy(&newbase[5],"02-29"); /* leap year */
706 else
707 (void) strcpy(&newbase[5],"02-28");
708 }
709 goto final_task;
710 }
711
712 iyear --;
713 (void) sprintf(newbase,"%d-12-31",iyear);
714 }
715
716
717 final_task:
718 if (adiff > 0) goto more_days;
719
720 leng = CGN_COPY(s,newbase);
721 return(leng);
722 }
723 /*
724
725 */
726
727 void CGN_GETLINE(s,lim)
728
729 /*++++++++++++++++++++++++++++++++++++++++++++++++++
730 .KEYWORDS
731 terminal
732 .PURPOSE
733 get an input line from the terminal
734 .ALGORITHM
735 use C-function getchar
736 .RETURNS
737 nothing
738 --------------------------------------------------*/
739
740 char s[]; /* OUT: input line from terminal */
741 int lim; /* IN: max. length of input string */
742
743 {
744 register int nr;
745
746
747 lim --;
748 for (nr=0; nr<lim; nr++)
749 {
750 s[nr] = getchar();
751 if ( (s[nr] == '\n') || (s[nr] == '\r') || (s[nr] == EOF) )
752 {
753 s[nr] = '\0';
754 return;
755 }
756 }
757
758 if (lim < 0) /* check for bad input value */
759 s[0] = '\0';
760 else
761 s[lim] = '\0';
762 }
763
764