| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1988-2009 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 Massachusetss Ave, Cambridge, |
| 17 |
MA 02139, USA. |
| 18 |
|
| 19 |
Corresponding 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 |
/*+++++++++ |
| 29 |
.MODULE traf.c |
| 30 |
.AUTHOR Francois Ochsenbein [ESO] |
| 31 |
.LANGUAGE C |
| 32 |
.CATEGORY Conversion from character to double. |
| 33 |
|
| 34 |
.COMMENTS |
| 35 |
|
| 36 |
.VERSION 1.0 20-Oct-1988: Creation |
| 37 |
|
| 38 |
090831 last modif |
| 39 |
-----------------------------------------------*/ |
| 40 |
|
| 41 |
#include <midas_def.h> |
| 42 |
#include <tra.h> |
| 43 |
#include <atype.h> |
| 44 |
|
| 45 |
extern int tr_error(); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#define issign(c) ((c == '+') || (c == '-')) |
| 50 |
#define FINISH goto FIN |
| 51 |
#define GobbleSpaces(s,l) s += oscspan((unsigned char *)s, l, _SPACE_, main_ascii) |
| 52 |
|
| 53 |
MID_EXTERN struct trerr_s *trerror; |
| 54 |
|
| 55 |
MID_STATIC char *stopped; /* To communicate the stopping char */ |
| 56 |
|
| 57 |
/*===========================================================================*/ |
| 58 |
static int traf(str, len, value) |
| 59 |
/*++++++ |
| 60 |
.PURPOSE Converts a string to a floating point. |
| 61 |
Input is assumed to be in decimal. |
| 62 |
.RETURNS Number of significant digits / -1 for |
| 63 |
error (message logged) |
| 64 |
.REMARKS Return 0 if string contains only spaces. |
| 65 |
Stopped index returned in static `stopped'. |
| 66 |
--------*/ |
| 67 |
char *str; /* IN: String to scan */ |
| 68 |
int len; /* IN: Length of str */ |
| 69 |
double *value; /* OUT: result */ |
| 70 |
{ |
| 71 |
char *p, *pe, *ps, x; |
| 72 |
int i, nd; |
| 73 |
double atof(); |
| 74 |
|
| 75 |
trerror->errno = 0; |
| 76 |
|
| 77 |
*value = 0.0e0, nd = 0; |
| 78 |
p = str, pe = p + len; |
| 79 |
GobbleSpaces(p,len); |
| 80 |
|
| 81 |
if (p == pe) { trerror->errno = TRERR_VOID; FINISH; } |
| 82 |
|
| 83 |
ps = p; /* Starting point of string */ |
| 84 |
if (issign(*p)) p++; |
| 85 |
p += oscskip(p, pe-p, '0'); /* Skip leading zeroes, not significant */ |
| 86 |
if (p == pe) FINISH; /* Zero */ |
| 87 |
|
| 88 |
nd = oscspan((unsigned char *)p, pe-p, _DIGIT_, main_ascii), p += nd; |
| 89 |
if (p == pe) goto GET_VALUE; |
| 90 |
|
| 91 |
if (*p == '.') p++; |
| 92 |
if (nd == 0) /* Skip leading zeroes */ |
| 93 |
p += oscskip(p, pe-p, '0'); |
| 94 |
i = oscspan((unsigned char *)p, pe-p, _DIGIT_, main_ascii); /* Decimals */ |
| 95 |
p += i, nd += i; |
| 96 |
|
| 97 |
/* Look for Exponent */ |
| 98 |
|
| 99 |
if ((p < pe) && (isalpha(*p))) |
| 100 |
{ x = toupper(*p); |
| 101 |
if ((x == 'E') || (x == 'D')) /* Exponent */ |
| 102 |
{ if (p == ps) trerror->errno = TRERR_VOID; |
| 103 |
p++; |
| 104 |
if (issign(*p)) p++; |
| 105 |
p += oscspan((unsigned char *)p, pe-p, _DIGIT_, main_ascii); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
GET_VALUE: |
| 110 |
if (p == ps) trerror->errno = TRERR_VOID; |
| 111 |
x = *p, *p = '\0', *value = atof(str), *p = x; |
| 112 |
|
| 113 |
FIN: |
| 114 |
stopped = p; |
| 115 |
return(nd); |
| 116 |
} |
| 117 |
|
| 118 |
/*===========================================================================*/ |
| 119 |
int tr_af(str, len, value) |
| 120 |
/*++++++ |
| 121 |
.PURPOSE Converts a string to a floating point. |
| 122 |
Input is assumed to be in decimal. |
| 123 |
.RETURNS Number of significant digits / -1 for |
| 124 |
error (message logged) |
| 125 |
.REMARKS Return 0 if string contains only spaces. |
| 126 |
--------*/ |
| 127 |
char *str; /* IN: String to scan */ |
| 128 |
int len; /* IN: Length of str */ |
| 129 |
double *value; /* OUT: result */ |
| 130 |
{ |
| 131 |
char *p, *pe; |
| 132 |
int nd; |
| 133 |
|
| 134 |
p = str, pe = p + len; |
| 135 |
nd = traf(p, len, value); |
| 136 |
if (nd < 0) FINISH; |
| 137 |
|
| 138 |
p = stopped; |
| 139 |
GobbleSpaces(p, pe-p); |
| 140 |
|
| 141 |
if (p != pe) |
| 142 |
{ trerror->errno = TRERR_DIGIT, |
| 143 |
trerror->str = str, |
| 144 |
trerror->len = len, |
| 145 |
trerror->offset = p - str, |
| 146 |
nd = -1; |
| 147 |
tr_error(); |
| 148 |
FINISH; |
| 149 |
} |
| 150 |
|
| 151 |
FIN: |
| 152 |
if ( (nd == 0) && (trerror->errno == 0)) nd = 1; |
| 153 |
return(nd); |
| 154 |
} |
| 155 |
|
| 156 |
/*===========================================================================*/ |
| 157 |
int tr_af1(str, len, value, n) |
| 158 |
/*++++++ |
| 159 |
.PURPOSE Converts a string to a vector of numbers. |
| 160 |
Numbers may be separated by blanks or any punctuation character, |
| 161 |
.RETURNS Number of found numbers (<= n) / -1 for |
| 162 |
error (message logged) |
| 163 |
.REMARKS Return 0 if string contains only spaces. |
| 164 |
--------*/ |
| 165 |
char *str; /* IN: String to scan */ |
| 166 |
int len; /* IN: Length of str */ |
| 167 |
double *value; /* OUT: result */ |
| 168 |
int n; /* IN: Number of values */ |
| 169 |
{ |
| 170 |
char *p, *pe; |
| 171 |
int i; |
| 172 |
|
| 173 |
trerror->errno = 0; |
| 174 |
|
| 175 |
p = str, pe = p + len; |
| 176 |
|
| 177 |
for (i=0; (p<pe) && (i < n); i++) |
| 178 |
{ GobbleSpaces(p, pe-p); |
| 179 |
if (p == pe) break; |
| 180 |
traf(p, pe-p, value+i); |
| 181 |
if (trerror->errno) break; /* Void number */ |
| 182 |
p = stopped; |
| 183 |
if (p == pe) continue; |
| 184 |
if (isspace(*p)) continue; |
| 185 |
if (issign(*p)) continue; |
| 186 |
if (*p == '.') continue; |
| 187 |
p++; |
| 188 |
} |
| 189 |
|
| 190 |
if (trerror->errno == 0) |
| 191 |
{ GobbleSpaces(p, pe-p); |
| 192 |
if (p < pe) trerror->errno = TRERR_TOOMANY; |
| 193 |
} |
| 194 |
if (trerror->errno) |
| 195 |
{ trerror->str = str, trerror->len = len, |
| 196 |
trerror->offset = (p - str); |
| 197 |
tr_error(); |
| 198 |
} |
| 199 |
|
| 200 |
return((trerror->errno ? -1 : i)); |
| 201 |
} |
| 202 |
|
| 203 |
/*===========================================================================*/ |
| 204 |
int tr_afn(str, len, value, n) |
| 205 |
/*++++++ |
| 206 |
.PURPOSE Converts a string to a vector of double numbers. |
| 207 |
Numbers may be separated by blanks or any punctuation character. |
| 208 |
.RETURNS n for success / -1 for failure |
| 209 |
.REMARKS |
| 210 |
--------*/ |
| 211 |
char *str; /* IN: String to scan */ |
| 212 |
int len; /* IN: Length of str */ |
| 213 |
double *value; /* OUT: array result */ |
| 214 |
int n; /* IN: Number of values */ |
| 215 |
{ |
| 216 |
int i; |
| 217 |
|
| 218 |
i = tr_af1(str, len, value, n); |
| 219 |
|
| 220 |
if ((i >= 0) && (i < n)) /* Too few numbers */ |
| 221 |
{ trerror->errno = TRERR_TOOFEW; |
| 222 |
trerror->str = str, trerror->len = len, |
| 223 |
trerror->offset = len; |
| 224 |
tr_error(); |
| 225 |
} |
| 226 |
|
| 227 |
return((trerror->errno ? -1 : i)); |
| 228 |
} |