~ubuntu-branches/debian/jessie/eso-midas/jessie

« back to all changes in this revision

Viewing changes to prim/dio/libsrc/getval.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2014-04-22 14:44:58 UTC
  • Revision ID: package-import@ubuntu.com-20140422144458-okiwi1assxkkiz39
Tags: upstream-13.09pl1.2+dfsg
ImportĀ upstreamĀ versionĀ 13.09pl1.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* @(#)getval.c 19.1 (ES0-DMD) 02/25/03 13:59:31 */
 
2
/*===========================================================================
 
3
  Copyright (C) 1995 European Southern Observatory (ESO)
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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, 
 
18
  MA 02139, USA.
 
19
 
 
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 
 
26
                        GERMANY
 
27
===========================================================================*/
 
28
 
 
29
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
30
.COPYRIGHT  (c) 1989   European Southern Observatory
 
31
.IDENT      getval.c
 
32
.LANGUAGE   C
 
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
 
37
            and flags it.
 
38
.VERSION    1.0   1988-Sep-23 : Creation,  PJG
 
39
.VERSION    1.1   1989-Jun-26 : Correct return char. count,  PJG
 
40
-------------------------------------------------------------------------*/
 
41
 
 
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             */
 
51
{
 
52
  register  char    *s;
 
53
  register  int     n;
 
54
  int               psign,exp,sign;
 
55
  double            fac,val,power;
 
56
 
 
57
  s = pc; n = mc;
 
58
  val = 0.0; power = 1.0; fac = 1.0;
 
59
  exp = 0; sign = 1; *pi = 1;
 
60
  if (!s || n<=0) return 0;
 
61
 
 
62
  while ( *s==' ' || *s=='\t' ) {                 /* skip spaces        */
 
63
     if (!(--n)) { *pdbl = 0.0; return mc; }
 
64
     s++;
 
65
  }
 
66
 
 
67
  if ( *s=='+' || *s=='-' ) {                     /* get sign           */
 
68
     sign = ( *s++ == '+' ) ? 1 : -1;
 
69
     if (!(--n)) { *pdbl = 0.0; return mc; }
 
70
  }
 
71
 
 
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; }
 
75
     s++;
 
76
  }
 
77
 
 
78
  if ( *s=='.' ) {                                /* fraction part      */
 
79
     s++; *pi = 0;
 
80
     if (!(--n)) { *pdbl = sign*val*power; return mc; }
 
81
     while ( (*s>='0' && *s<='9') || *s==' ' ) {
 
82
       if (*s!=' ') { 
 
83
          val = 10.0 * val + (*s - '0'); power /= 10.0;
 
84
       }
 
85
       if (!(--n)) { *pdbl = sign*val*power; return mc; }
 
86
       s++;
 
87
     }
 
88
  }
 
89
 
 
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; }
 
96
     }
 
97
     while ( (*s>='0' && *s<='9') || *s==' ' ) {
 
98
        if (*s!=' ') exp = 10 * exp + ( *s - '0' );
 
99
        if (!(--n)) break;
 
100
        s++;
 
101
     }
 
102
     if (psign)
 
103
        while (exp--) power *= 10.0;
 
104
     else
 
105
        while (exp--) power /= 10.0;
 
106
  }
 
107
 
 
108
  *pdbl = sign*val*power;
 
109
  return mc-n;
 
110
}