1
/* @(#)getval.c 19.1 (ES0-DMD) 02/25/03 13:59:31 */
2
/*===========================================================================
3
Copyright (C) 1995 European Southern Observatory (ESO)
5
This program is free software; you can redistribute it and/or
6
modify it under the terms of the GNU General Public License as
7
published by the Free Software Foundation; either version 2 of
8
the License, or (at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public
16
License along with this program; if not, write to the Free
17
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge,
20
Corresponding concerning ESO-MIDAS should be addressed as follows:
21
Internet e-mail: midas@eso.org
22
Postal address: European Southern Observatory
23
Data Management Division
24
Karl-Schwarzschild-Strasse 2
25
D 85748 Garching bei Muenchen
27
===========================================================================*/
29
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
.COPYRIGHT (c) 1989 European Southern Observatory
33
.AUTHOR P.Grosbol, ESO/IPG
34
.KEYWORDS conversion, double
35
.COMMENT Conversion of an ASCII string to a binary double real.
36
The routine checks if string could be read as a integer
38
.VERSION 1.0 1988-Sep-23 : Creation, PJG
39
.VERSION 1.1 1989-Jun-26 : Correct return char. count, PJG
40
-------------------------------------------------------------------------*/
42
int getval(pc,mc,pi,pdbl)
43
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
44
.PURPOSE convert string to 'double'
45
.RETURN no. of characters read from string
46
-------------------------------------------------------------------------*/
47
char *pc; /* string to be converted */
48
int mc; /* max. no. of char to scan */
49
int *pi; /* flag if value was integer */
50
double *pdbl; /* decoded value */
58
val = 0.0; power = 1.0; fac = 1.0;
59
exp = 0; sign = 1; *pi = 1;
60
if (!s || n<=0) return 0;
62
while ( *s==' ' || *s=='\t' ) { /* skip spaces */
63
if (!(--n)) { *pdbl = 0.0; return mc; }
67
if ( *s=='+' || *s=='-' ) { /* get sign */
68
sign = ( *s++ == '+' ) ? 1 : -1;
69
if (!(--n)) { *pdbl = 0.0; return mc; }
72
while ( (*s>='0' && *s<='9') || *s==' ' ) { /* decode number */
73
if (*s!=' ') val = 10.0 * val + ( *s - '0' );
74
if (!(--n)) { *pdbl = sign*val; return mc; }
78
if ( *s=='.' ) { /* fraction part */
80
if (!(--n)) { *pdbl = sign*val*power; return mc; }
81
while ( (*s>='0' && *s<='9') || *s==' ' ) {
83
val = 10.0 * val + (*s - '0'); power /= 10.0;
85
if (!(--n)) { *pdbl = sign*val*power; return mc; }
90
if ( *s=='e' || *s=='E' || *s=='d' || *s=='D' ) { /* exponent part */
91
s++; psign = 1; *pi = 0;
92
if (!(--n)) { *pdbl = sign*val*power; return mc; }
93
if ( *s=='-' || *s=='+' ) {
94
psign = (*s++ == '+') ? 1 : 0;
95
if (!(--n)) { *pdbl = sign*val*power; return mc; }
97
while ( (*s>='0' && *s<='9') || *s==' ' ) {
98
if (*s!=' ') exp = 10 * exp + ( *s - '0' );
103
while (exp--) power *= 10.0;
105
while (exp--) power /= 10.0;
108
*pdbl = sign*val*power;