1
/*===========================================================================
2
Copyright (C) 1995-2010 European Southern Observatory (ESO)
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.
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.
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,
19
Correspondence concerning ESO-MIDAS should be addressed as follows:
20
Internet e-mail: midas@eso.org
21
Postal address: European Southern Observatory
22
Data Management Division
23
Karl-Schwarzschild-Strasse 2
24
D 85748 Garching bei Muenchen
26
===========================================================================*/
28
/*+++++++++++++ CGN interfaces - part B +++++++++++++++++++++++++++++++++
30
.IDENTIFICATION Characters string handling + general functions
31
.AUTHOR K. Banse [ESO/IPG - Garching]
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
37
name translation, parsing, filling, (((seq-file read)))
42
---------------------------------------------------------------------------*/
49
static int cdifU = 'A' - 'a';
50
static int cdifL = 'a' - 'A';
57
/*++++++++++++++++++++++++++++++++++++++++++++++++++
59
this function converts all uppercase to lowercase
60
.RETURN the modified character.
61
--------------------------------------------------*/
62
char c; /* character to be eventually translated */
65
if ( (c >= 'A') && (c <= 'Z') )
76
int CGN_NINT(float rval)
79
/*++++++++++++++++++++++++++++++++++++++++++++++++++
81
return nearest integer of a floating point number
83
--------------------------------------------------*/
84
float rval; /* input real number */
92
ival = (int) (rval + 0.5);
93
else if (rval < -1.e-30)
94
ival = (int) (rval - 0.5);
103
int CGN_DNINT(double dval)
106
/*++++++++++++++++++++++++++++++++++++++++++++++++++
108
return nearest integer of a double precision number
110
--------------------------------------------------*/
111
double dval; /* input real number */
119
ival = (int) (dval + 0.5);
120
else if (dval < -1.e-30)
121
ival = (int) (dval - 0.5);
133
int CGN_NUMBER(string)
134
/*++++++++++++++++++++++++++++++++++++++++++++++++++
136
this function checks, if a given character string begins as a valid number
137
.RETURN = 1, if a number
139
--------------------------------------------------*/
140
char *string; /* input string */
151
if ((cr == '+') || (cr == '-'))
161
if (*cp == 'x') /* check for hex. constant */
169
if ((kr < '0') || (kr > '9'))
172
if ((cr < 'a') || (cr > 'f')) return (0);
175
return (1); /* hex. number: 0xabc... */
180
if (*cp == '.') cp ++;
181
goto step_a; /* starts with 0, check next chars */
186
if (cr == '.') cp ++;
188
if ((*cp < '0') || (*cp > '9')) return (0);
190
/* cp ++; what the hell is this ??? 060331 */
193
step_a: /* here we found first digit */
197
if ((cr < '0') || (cr > '9') )
199
if ((cr != '.') && (cr != 'D') && (cr != 'd')
200
&& (cr != 'E') && (cr != 'e')
201
&& (cr != '+') && (cr != '-')
202
&& (cr != ',')) /* for several numbers */
214
int CGN_OPEN(file,option)
215
/*++++++++++++++++++++++++++++++++++++++++++++++++++
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
--------------------------------------------------*/
222
char *file; /* IN: file name */
223
int option; /* IN: open mode as described for osaopen */
229
CGN_LOGNAM(file,tempo,400);
230
return (osaopen(tempo,option));
238
int CGN_EQUAL(namea,nameb)
239
/*++++++++++++++++++++++++++++++++++++++++++++++++++
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) */
249
char tempa[400], tempb[400];
252
CGN_CLEANF(namea,F_IMA_TYPE,tempa,399,&m,&extflg);
253
CGN_CLEANF(nameb,F_IMA_TYPE,tempb,399,&m,&extflg);
255
if (strcmp(tempa,tempb) != 0)
265
void CGN_REPLA(string,lstring,chara,charb)
266
/*++++++++++++++++++++++++++++++++++++++++++++++++++
268
this function replaces a given character by another
270
--------------------------------------------------*/
271
char *string; /* input string */
272
int lstring; /* length of string */
273
char chara; /* character to be replaced */
274
char charb; /* replacing char */
280
for (nr=0; nr<lstring; nr++)
282
if (string[nr] == chara) string[nr] = charb;
291
int CGN_SKIP(char * string , char skipchar , char flag , int * indx)
293
int CGN_SKIP(string,skipchar,flag,indx)
294
/*++++++++++++++++++++++++++++++++++++++++++++++++++
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
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 */
315
if (flag == 'f') /* forward skip */
317
for (i=0; string[i] != '\0'; i++)
319
if (string[i] != skipchar)
327
else /* reverse skip */
330
for (i=0; string[i] != '\0'; i++)
332
if (string[i] != skipchar) mm = i;
342
/* no other character found - so we return total length of input string */
353
/*++++++++++++++++++++++++++++++++++++++++++++++++++
355
this function converts all lowercase to uppercase
357
--------------------------------------------------*/
358
char c; /* character to eventually be 'uppercased' */
361
if ( (c >= 'a') && (c <= 'z') )
371
int CGN_UPCOPY(strb,stra,lim)
372
/*++++++++++++++++++++++++++++++++++++++++++++++++++
374
this function copies + converts all lowercase to uppercase for a given
375
number of chars. of input string
378
--------------------------------------------------*/
380
char *stra; /* IN: input string */
381
char *strb; /* IN: output string in uppercase */
382
int lim; /* IN: no. of chars. to do */
391
for (count=0; count<lim; count++)
400
if ((rp >= 'a') && (rp <= 'z')) rp += cdifU;
412
void CGN_UPSTR(string)
413
/*++++++++++++++++++++++++++++++++++++++++++++++++++
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' */
425
for (n=0; string[n] != '\0'; n++)
428
if ((c >= 'a') && (c <= 'z'))
429
string[n] = c + cdifU;
437
void CGN_LOWSTR(string)
438
/*++++++++++++++++++++++++++++++++++++++++++++++++++
440
this function converts all uppercase to lowercase for a complete string
441
.RETURN lowercase character string
442
--------------------------------------------------*/
444
char *string; /* character string to eventually be 'lowercased' */
451
for (n=0; string[n] != '\0'; n++)
454
if ((c >= 'A') && (c <= 'Z'))
455
string[n] = c + cdifL;
463
int CGN_LOWCOPY(strb,stra,lim)
464
/*++++++++++++++++++++++++++++++++++++++++++++++++++
466
this function copies + converts all lowercase to uppercase for a given
467
number of chars. of input string
470
--------------------------------------------------*/
472
char *stra; /* IN: input string */
473
char *strb; /* IN: output string in uppercase */
474
int lim; /* IN: no. of chars. to do */
483
for (count=0; count<lim; count++)
492
if ((rp >= 'A') && (rp <= 'Z')) rp += cdifL;
504
void CGN_COPYALL(cpntra,cpntrb,slen)
506
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
508
copy entire string b on string a.
509
(Doesn't take '\0' characters as end-of-string).
511
----------------------------------------------------------------------*/
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 */;
520
for (n=0; n<slen; n++)
521
*cpntra++ = *cpntrb++;
529
void CGN_strcpy(dest,src)
531
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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)
538
----------------------------------------------------------------------*/
540
char *dest; /* OUT: dest. string */
541
char *src; /* IN: source string */
545
register char *da, *sa;
559
*da = '\0'; /* terminate dest string */
566
void CGN_strncpy(dest,src,n)
568
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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)
575
----------------------------------------------------------------------*/
577
char *dest; /* OUT: dest. string */
578
char *src; /* IN: source string */
579
size_t n; /* no. of elements to copy */
584
for (i=0; i<n && src[i]!='\0'; i++)
587
for ( ; i<n; i++) dest[i] = '\0'; /* fill up with '\0' */
594
void CGN_FUNC(fno,dval)
596
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
598
support calculation of ln, log10, exp, exp10, sin, cos, tan
601
----------------------------------------------------------------------*/
603
int fno; /* IN : function no. */
604
double *dval; /* IN/OUT: function argument + result */
608
static double facto = 0.0174532925; /* Pi/180.0 */
616
case 1: /* y = log(x) */
624
case 2: /* y = log10(x) */
632
case 3: /* y = exp(x) */
637
case 4: /* y = exp10(x) */
639
x *= log(10.0); /* use: 10**x = e**(x*ln(10)) */
643
case 5: /* y = sin(x) */
649
case 6: /* y = cos(x) */
655
case 7: /* y = tan(x) */
661
case 8: /* y = sqrt(x) */
666
case 9: /* y = asin(x) in degrees */
672
case 10: /* y = acos(x) in degrees */
678
case 11: /* y = atan(x) in degrees */
692
void CGN_CUTOFF(instr,outstr)
693
/*++++++++++++++++++++++++++++++++++++++++++++++++++
695
cut off trailing blanks
697
--------------------------------------------------*/
698
char *instr; /* input string */
699
char *outstr; /* output string */
705
mr = -1; /* we'll set `mr' to last non-blank char. */
709
outstr[nr] = instr[nr];
710
if (outstr[nr] != ' ')
712
if (outstr[nr] == '\0')