1
/*===========================================================================
2
Copyright (C) 1988-2009 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 Massachusetss Ave, Cambridge,
19
Corresponding 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
===========================================================================*/
30
.AUTHOR Francois Ochsenbein [ESO]
32
.CATEGORY Conversion from character to float (double) using a picture.
35
Definition of pictures are in edfpic.c
40
.VERSION 1.0 27-May-1988: Creation
41
.VERSION 1.1 21-Oct-1988: Modified error report
42
.VERSION 1.2 22-Mar-1990: Added stkfpic (tokenisation)
43
.VERSION 1.3 16-May-1990: Allow trailing blanks / spaces
46
-----------------------------------------------*/
48
#include <midas_def.h>
54
extern int tr_pic(), tr_error();
57
MID_EXTERN struct trerr_s *trerror;
59
#define FINISH goto FIN
62
MID_RSTATIC char emarks[] = {'.', 'E','e','D','d'};
63
MID_STATIC char stk = 0; /* Indicator for stkfpic */
64
MID_STATIC char edited[64];
66
/*===========================================================================*/
67
static int Sign(str, len)
69
.PURPOSE Just scan the beginning of a string: skip leading blanks,
71
.RETURNS Number of valid characters
74
char *str; /* IN: The string to scan */
75
int len; /* IN: Length of the string */
80
if (len <= 0) return(0);
82
p = str + oscspan((unsigned char *)str, len, _SPACE_, main_ascii);
90
p += oscspan((unsigned char *)p, pe-p, _SPACE_, main_ascii); /* Skip blanks */
96
/*===========================================================================*/
97
int tr_fpic(str, len, pic, value)
99
.PURPOSE Converts a string to a double number according to a picture
101
.RETURNS Number of decimals / -1 for error or no number
102
.REMARKS returned value is e.g. 101 for 1.01
104
char *str; /* IN: String to scan */
105
int len; /* IN: Length of str */
106
char *pic; /* IN: The picture */
107
double *value; /* OUT: result */
109
char *p, *pe, *pdot, *a, x;
114
p = str + oscspan((unsigned char *)str, len, _SPACE_, main_ascii);
123
p += Sign(p, pe-p); /* Locate Sign */
126
for (i=0; (i < sizeof(emarks)) && (pdot == pe); i++)
127
pdot = p + oscloc(p, pe-p, emarks[i]);
129
la = strlen(pic), a = pic + la;
130
for (i=0; (i < sizeof(emarks)) && (*a == EOS); i++)
131
a = pic + oscloc(pic, la, emarks[i]);
134
nd = tr_pic(p, pdot-p, pic, &int_part);
140
edited[0] = (sign ? '-' : ' ');
142
for (i=11; --i>0; int_part /= 10)
143
edited[i] = (int_part % 10) + '0';
146
/* Copy Remaining part. x is an indicator which takes values
147
1 when exponent starts
148
2 wneh number terminated
151
x = 0, p = pdot, i = 12;
152
if (p == pe) goto GET_VALUE;
153
if (*p == '.') nd++, p++;
155
for ( ; (p < pe) && (trerror->errno == 0); p++)
156
{ if (isspace(*p)) { x = 2; continue; }
157
if (x == 2) { trerror->errno = TRERR_FLOAT; break; }
159
{ case 'd': case 'D' : case 'e' : case 'E':
160
if (x) trerror->errno = TRERR_FLOAT;
165
if (x == 0) /* Error, E missing */
166
trerror->errno = TRERR_FLOAT;
171
edited[i++] = *p, nd++;
172
else trerror->errno = TRERR_DIGIT;
175
if (i >= (sizeof(edited)-1)) trerror->errno = TRERR_UNDEF;
177
if ((p < pe) && (trerror->errno == 0))
178
trerror->errno = TRERR_FLOAT;
183
if (trerror->errno) /* Bad number */
184
{ trerror->offset = (p-str);
189
*value = atof(edited);
192
if (stk) return(p - str);
196
/*===========================================================================*/
197
int stkfpic(str, pic)
199
.PURPOSE Compute the length of the tokenized string
200
.RETURNS Length of str matched for specified picture
203
char *str; /* IN: String to scan */
204
char *pic; /* IN: The picture */
209
stk = 1; /* Tells tr_fpic that we just tokenize */
210
status = tr_fpic (str, strlen(str), pic, &value);