Debian Codegraph @

libsrc/st/cgnb.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 B +++++++++++++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION Characters string handling + general functions
31 .AUTHOR K. Banse [ESO/IPG - Garching]
32 .COMMENTS
33 holds CGN_LOWER, CGN_UPPER, CGN_NINT, CGN_DNINT, CGN_NUMBER, CGN_OPEN
34 CGN_EQUAL, CGB_REPLA, CGN_SKIP, CGN_UPSTR, CGN_LOWSTR
35 CGN_UPCOPY, CGN_LOWCOPY, CGN_COPYALL, CGN_FUNC
36 .KEYWORDS
37 name translation, parsing, filling, (((seq-file read)))
38
39 .VERSION
40 100608 last modif
41
42 ---------------------------------------------------------------------------*/
43
44
45 #include <math.h>
46 #include <fileexts.h>
47
48
49 static int cdifU = 'A' - 'a';
50 static int cdifL = 'a' - 'A';
51
52 /*
53
54 */
55
56 char CGN_LOWER(c)
57 /*++++++++++++++++++++++++++++++++++++++++++++++++++
58 .PURPOSE
59 this function converts all uppercase to lowercase
60 .RETURN the modified character.
61 --------------------------------------------------*/
62 char c; /* character to be eventually translated */
63
64 {
65 if ( (c >= 'A') && (c <= 'Z') )
66 return (c + cdifL);
67 else
68 return c;
69 }
70
71 /*
72
73 */
74
75 #ifdef __STDC__
76 int CGN_NINT(float rval)
77 #else
78 int CGN_NINT(rval)
79 /*++++++++++++++++++++++++++++++++++++++++++++++++++
80 .PURPOSE
81 return nearest integer of a floating point number
82 .RETURN NINT(rval)
83 --------------------------------------------------*/
84 float rval; /* input real number */
85 #endif
86
87 {
88 int ival;
89
90
91 if (rval > 1.e-30)
92 ival = (int) (rval + 0.5);
93 else if (rval < -1.e-30)
94 ival = (int) (rval - 0.5);
95 else
96 ival = 0;
97
98 return (ival);
99
100 }
101
102 #ifdef __STDC__
103 int CGN_DNINT(double dval)
104 #else
105 int CGN_DNINT(dval)
106 /*++++++++++++++++++++++++++++++++++++++++++++++++++
107 .PURPOSE
108 return nearest integer of a double precision number
109 .RETURN NINT(dval)
110 --------------------------------------------------*/
111 double dval; /* input real number */
112 #endif
113
114 {
115 int ival;
116
117
118 if (dval > 1.e-30)
119 ival = (int) (dval + 0.5);
120 else if (dval < -1.e-30)
121 ival = (int) (dval - 0.5);
122 else
123 ival = 0;
124
125 return (ival);
126
127 }
128
129 /*
130
131 */
132
133 int CGN_NUMBER(string)
134 /*++++++++++++++++++++++++++++++++++++++++++++++++++
135 .PURPOSE
136 this function checks, if a given character string begins as a valid number
137 .RETURN = 1, if a number
138 = 0, else
139 --------------------------------------------------*/
140 char *string; /* input string */
141
142 {
143 char *cp;
144 register char cr;
145
146
147 cp = string;
148 cr = *cp;
149
150
151 if ((cr == '+') || (cr == '-'))
152 {
153 cp ++;
154 cr = *cp;
155 }
156
157 if (cr == '0')
158 {
159 cp ++;
160
161 if (*cp == 'x') /* check for hex. constant */
162 {
163 char kr;
164
165 cp ++;
166 while (*cp != '\0')
167 {
168 kr = *cp++;
169 if ((kr < '0') || (kr > '9'))
170 {
171 cr = CGN_LOWER(kr);
172 if ((cr < 'a') || (cr > 'f')) return (0);
173 }
174 }
175 return (1); /* hex. number: 0xabc... */
176 }
177
178 else
179 {
180 if (*cp == '.') cp ++;
181 goto step_a; /* starts with 0, check next chars */
182 }
183 }
184
185
186 if (cr == '.') cp ++;
187
188 if ((*cp < '0') || (*cp > '9')) return (0);
189
190 /* cp ++; what the hell is this ??? 060331 */
191
192
193 step_a: /* here we found first digit */
194 while (*cp != '\0')
195 {
196 cr = *cp++;
197 if ((cr < '0') || (cr > '9') )
198 {
199 if ((cr != '.') && (cr != 'D') && (cr != 'd')
200 && (cr != 'E') && (cr != 'e')
201 && (cr != '+') && (cr != '-')
202 && (cr != ',')) /* for several numbers */
203 return (0);
204 }
205 }
206
207 return (1);
208 }
209
210 /*
211
212 */
213
214 int CGN_OPEN(file,option)
215 /*++++++++++++++++++++++++++++++++++++++++++++++++++
216 .PURPOSE
217 this function returns the file id from osaopen,
218 but translates first all logical stuff...
219 .RETURN fid file no. as expected from osaopen
220 --------------------------------------------------*/
221
222 char *file; /* IN: file name */
223 int option; /* IN: open mode as described for osaopen */
224
225 {
226 char tempo[400];
227
228
229 CGN_LOGNAM(file,tempo,400);
230 return (osaopen(tempo,option));
231
232 }
233
234 /*
235
236 */
237
238 int CGN_EQUAL(namea,nameb)
239 /*++++++++++++++++++++++++++++++++++++++++++++++++++
240 .PURPOSE
241 compare two frame names and test, if they are equal
242 .RETURN 0 or 1, if frame names are equal or not
243 --------------------------------------------------*/
244 char *namea; /* 1. file name (terminated by \0) */
245 char *nameb; /* 2. file name (terminated by \0) */
246
247 {
248 int m, extflg;
249 char tempa[400], tempb[400];
250
251
252 CGN_CLEANF(namea,F_IMA_TYPE,tempa,399,&m,&extflg);
253 CGN_CLEANF(nameb,F_IMA_TYPE,tempb,399,&m,&extflg);
254
255 if (strcmp(tempa,tempb) != 0)
256 return (1);
257 else
258 return (0);
259 }
260
261 /*
262
263 */
264
265 void CGN_REPLA(string,lstring,chara,charb)
266 /*++++++++++++++++++++++++++++++++++++++++++++++++++
267 .PURPOSE
268 this function replaces a given character by another
269 .RETURN nothing
270 --------------------------------------------------*/
271 char *string; /* input string */
272 int lstring; /* length of string */
273 char chara; /* character to be replaced */
274 char charb; /* replacing char */
275
276 {
277 register int nr;
278
279
280 for (nr=0; nr<lstring; nr++)
281 {
282 if (string[nr] == chara) string[nr] = charb;
283 }
284 }
285
286 /*
287
288 */
289
290 #ifdef __STDC__
291 int CGN_SKIP(char * string , char skipchar , char flag , int * indx)
292 #else
293 int CGN_SKIP(string,skipchar,flag,indx)
294 /*++++++++++++++++++++++++++++++++++++++++++++++++++
295 .PURPOSE
296 finds position of first character different from skip_char.
297 depending upon flag (= 'f' or 'r') we start at the beginning
298 or the end of the input string
299 .RETURN
300 returns 1 if o.k.
301 returns 0 and length(string) if no other char. found
302 --------------------------------------------------*/
303 char *string; /* IN : Input string */
304 char skipchar; /* IN : skip-character */
305 char flag; /* IN : start atthe end or at the beginning of the string
306 'f' => from the beginning, else from the end */
307 int *indx; /* OUT: position of the first char != from CHAR
308 or length of string if only skip chars in string */
309 #endif
310
311 {
312 register int mm, i;
313
314
315 if (flag == 'f') /* forward skip */
316 {
317 for (i=0; string[i] != '\0'; i++)
318 {
319 if (string[i] != skipchar)
320 {
321 *indx = i;
322 return (1);
323 }
324 }
325 }
326
327 else /* reverse skip */
328 {
329 mm = -1;
330 for (i=0; string[i] != '\0'; i++)
331 {
332 if (string[i] != skipchar) mm = i;
333 }
334 if (mm >= 0)
335 {
336 *indx = mm;
337 return (1);
338 }
339 }
340
341
342 /* no other character found - so we return total length of input string */
343
344 *indx = i;
345 return (0);
346 }
347
348 /*
349
350 */
351
352 char CGN_UPPER(c)
353 /*++++++++++++++++++++++++++++++++++++++++++++++++++
354 .PURPOSE
355 this function converts all lowercase to uppercase
356 .RETURN character.
357 --------------------------------------------------*/
358 char c; /* character to eventually be 'uppercased' */
359
360 {
361 if ( (c >= 'a') && (c <= 'z') )
362 return (c + cdifU);
363 else
364 return (c);
365 }
366
367 /*
368
369 */
370
371 int CGN_UPCOPY(strb,stra,lim)
372 /*++++++++++++++++++++++++++++++++++++++++++++++++++
373 .PURPOSE
374 this function copies + converts all lowercase to uppercase for a given
375 number of chars. of input string
376 .RETURN
377 nothing
378 --------------------------------------------------*/
379
380 char *stra; /* IN: input string */
381 char *strb; /* IN: output string in uppercase */
382 int lim; /* IN: no. of chars. to do */
383
384 {
385 register int count;
386
387 register char rp;
388
389
390
391 for (count=0; count<lim; count++)
392 {
393 rp = *stra++;
394 if (rp == '\0')
395 {
396 *strb = rp;
397 return count;
398 }
399
400 if ((rp >= 'a') && (rp <= 'z')) rp += cdifU;
401
402 *strb++ = rp;
403 }
404
405 return lim;
406 }
407
408 /*
409
410 */
411
412 void CGN_UPSTR(string)
413 /*++++++++++++++++++++++++++++++++++++++++++++++++++
414 .PURPOSE
415 this function converts all lowercase to uppercase for a complete string
416 .RETURN uppercase character string
417 --------------------------------------------------*/
418 char *string; /* character string to eventually be 'uppercased' */
419
420 {
421 register int c, n;
422
423
424
425 for (n=0; string[n] != '\0'; n++)
426 {
427 c = string[n];
428 if ((c >= 'a') && (c <= 'z'))
429 string[n] = c + cdifU;
430 }
431 }
432
433 /*
434
435 */
436
437 void CGN_LOWSTR(string)
438 /*++++++++++++++++++++++++++++++++++++++++++++++++++
439 .PURPOSE
440 this function converts all uppercase to lowercase for a complete string
441 .RETURN lowercase character string
442 --------------------------------------------------*/
443
444 char *string; /* character string to eventually be 'lowercased' */
445
446 {
447 register int c, n;
448
449
450
451 for (n=0; string[n] != '\0'; n++)
452 {
453 c = string[n];
454 if ((c >= 'A') && (c <= 'Z'))
455 string[n] = c + cdifL;
456 }
457 }
458
459 /*
460
461 */
462
463 int CGN_LOWCOPY(strb,stra,lim)
464 /*++++++++++++++++++++++++++++++++++++++++++++++++++
465 .PURPOSE
466 this function copies + converts all lowercase to uppercase for a given
467 number of chars. of input string
468 .RETURN
469 nothing
470 --------------------------------------------------*/
471
472 char *stra; /* IN: input string */
473 char *strb; /* IN: output string in uppercase */
474 int lim; /* IN: no. of chars. to do */
475
476 {
477 register char rp;
478
479 register int count;
480
481
482
483 for (count=0; count<lim; count++)
484 {
485 rp = *stra++;
486 if (rp == '\0')
487 {
488 *strb = rp;
489 return count;
490 }
491
492 if ((rp >= 'A') && (rp <= 'Z')) rp += cdifL;
493
494 *strb++ = rp;
495 }
496
497 return lim;
498 }
499
500 /*
501
502 */
503
504 void CGN_COPYALL(cpntra,cpntrb,slen)
505
506 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
507 .PURPOSE
508 copy entire string b on string a.
509 (Doesn't take '\0' characters as end-of-string).
510 .RETURNS nothing.
511 ----------------------------------------------------------------------*/
512
513 char *cpntra /* OUT: string to receive the result) */;
514 char *cpntrb /* IN: string to be copied */;
515 int slen /* IN: number of bytes to copy */;
516
517 {
518 register int n;
519
520 for (n=0; n<slen; n++)
521 *cpntra++ = *cpntrb++;
522
523 }
524
525 /*
526
527 */
528
529 void CGN_strcpy(dest,src)
530
531 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
532 .PURPOSE
533 copy '\0' limited string
534 like strcpy (except the const source) but copies in a controlled
535 way, i.e. starting with 1st char.
536 thus, src and dest may overlap (unlike system strcpy)
537 .RETURNS nothing.
538 ----------------------------------------------------------------------*/
539
540 char *dest; /* OUT: dest. string */
541 char *src; /* IN: source string */
542
543 {
544 register char cc;
545 register char *da, *sa;
546
547
548
549 da = dest;
550 sa = src;
551
552 cc = *sa++;
553 while (cc != '\0')
554 {
555 *da++ = cc;
556 cc = *sa++;
557 }
558
559 *da = '\0'; /* terminate dest string */
560 }
561
562 /*
563
564 */
565
566 void CGN_strncpy(dest,src,n)
567
568 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
569 .PURPOSE
570 copy exactly n elements of source string
571 like strcnpy (except the const source) but copies in a controlled
572 way, i.e. starting with 1st char.
573 thus, src and dest may overlap (unlike system strncpy)
574 .RETURNS nothing.
575 ----------------------------------------------------------------------*/
576
577 char *dest; /* OUT: dest. string */
578 char *src; /* IN: source string */
579 size_t n; /* no. of elements to copy */
580
581 {
582 size_t i;
583
584 for (i=0; i<n && src[i]!='\0'; i++)
585 dest[i] = src[i];
586
587 for ( ; i<n; i++) dest[i] = '\0'; /* fill up with '\0' */
588 }
589
590 /*
591
592 */
593
594 void CGN_FUNC(fno,dval)
595
596 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
597 .PURPOSE
598 support calculation of ln, log10, exp, exp10, sin, cos, tan
599 asin, acos, atan
600 .RETURNS nothing.
601 ----------------------------------------------------------------------*/
602
603 int fno; /* IN : function no. */
604 double *dval; /* IN/OUT: function argument + result */
605
606 {
607 double x, y;
608 static double facto = 0.0174532925; /* Pi/180.0 */
609
610
611
612 x = *dval;
613
614 switch(fno)
615 {
616 case 1: /* y = log(x) */
617
618 if (x <= 0.0)
619 y = 0.0;
620 else
621 y = log(x);
622 break;
623
624 case 2: /* y = log10(x) */
625
626 if (x <= 0.0)
627 y = 0.0;
628 else
629 y = log10(x);
630 break;
631
632 case 3: /* y = exp(x) */
633
634 y = exp(x);
635 break;
636
637 case 4: /* y = exp10(x) */
638
639 x *= log(10.0); /* use: 10**x = e**(x*ln(10)) */
640 y = exp(x);
641 break;
642
643 case 5: /* y = sin(x) */
644
645 x *= facto;
646 y = sin(x);
647 break;
648
649 case 6: /* y = cos(x) */
650
651 x *= facto;
652 y = cos(x);
653 break;
654
655 case 7: /* y = tan(x) */
656
657 x *= facto;
658 y = tan(x);
659 break;
660
661 case 8: /* y = sqrt(x) */
662
663 y = sqrt(x);
664 break;
665
666 case 9: /* y = asin(x) in degrees */
667
668 y = asin(x);
669 y /= facto;
670 break;
671
672 case 10: /* y = acos(x) in degrees */
673
674 y = acos(x);
675 y /= facto;
676 break;
677
678 case 11: /* y = atan(x) in degrees */
679 default:
680
681 y = atan(x);
682 y /= facto;
683 }
684
685 *dval = y;
686 }
687
688 /*
689
690 */
691
692 void CGN_CUTOFF(instr,outstr)
693 /*++++++++++++++++++++++++++++++++++++++++++++++++++
694 .PURPOSE
695 cut off trailing blanks
696 .RETURN nothing
697 --------------------------------------------------*/
698 char *instr; /* input string */
699 char *outstr; /* output string */
700
701 {
702 register int nr, mr;
703
704
705 mr = -1; /* we'll set `mr' to last non-blank char. */
706 nr = 0;
707 while (nr > -1)
708 {
709 outstr[nr] = instr[nr];
710 if (outstr[nr] != ' ')
711 {
712 if (outstr[nr] == '\0')
713 {
714 outstr[mr+1] = '\0';
715 return;
716 }
717 else
718 mr = nr;
719 }
720 nr ++;
721 }
722 }
723