| 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 trfpic.c |
| 30 |
.AUTHOR Francois Ochsenbein [ESO] |
| 31 |
.LANGUAGE C |
| 32 |
.CATEGORY Conversion from character to float (double) using a picture. |
| 33 |
|
| 34 |
.COMMENTS |
| 35 |
Definition of pictures are in edfpic.c |
| 36 |
|
| 37 |
\begin{TeX} |
| 38 |
\end{TeX} |
| 39 |
|
| 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 |
| 44 |
|
| 45 |
090831 last modif |
| 46 |
-----------------------------------------------*/ |
| 47 |
|
| 48 |
#include <midas_def.h> |
| 49 |
#include <macrogen.h> |
| 50 |
#include <tra.h> |
| 51 |
#include <atype.h> |
| 52 |
#include <string.h> |
| 53 |
|
| 54 |
extern int tr_pic(), tr_error(); |
| 55 |
|
| 56 |
|
| 57 |
MID_EXTERN struct trerr_s *trerror; |
| 58 |
|
| 59 |
#define FINISH goto FIN |
| 60 |
|
| 61 |
MID_STATIC char sign; |
| 62 |
MID_RSTATIC char emarks[] = {'.', 'E','e','D','d'}; |
| 63 |
MID_STATIC char stk = 0; /* Indicator for stkfpic */ |
| 64 |
MID_STATIC char edited[64]; |
| 65 |
|
| 66 |
/*===========================================================================*/ |
| 67 |
static int Sign(str, len) |
| 68 |
/*++++++ |
| 69 |
.PURPOSE Just scan the beginning of a string: skip leading blanks, |
| 70 |
and get the sign. |
| 71 |
.RETURNS Number of valid characters |
| 72 |
.REMARKS Not traced. |
| 73 |
--------*/ |
| 74 |
char *str; /* IN: The string to scan */ |
| 75 |
int len; /* IN: Length of the string */ |
| 76 |
{ |
| 77 |
char *p, *pe; |
| 78 |
|
| 79 |
sign = 0; |
| 80 |
if (len <= 0) return(0); |
| 81 |
|
| 82 |
p = str + oscspan((unsigned char *)str, len, _SPACE_, main_ascii); |
| 83 |
pe = str + len; |
| 84 |
|
| 85 |
if (p < pe) |
| 86 |
{ switch(*p) |
| 87 |
{ case '-': sign = 1; |
| 88 |
case '+': p++; break; |
| 89 |
} |
| 90 |
p += oscspan((unsigned char *)p, pe-p, _SPACE_, main_ascii); /* Skip blanks */ |
| 91 |
} |
| 92 |
|
| 93 |
return(p-str); |
| 94 |
} |
| 95 |
|
| 96 |
/*===========================================================================*/ |
| 97 |
int tr_fpic(str, len, pic, value) |
| 98 |
/*++++++ |
| 99 |
.PURPOSE Converts a string to a double number according to a picture |
| 100 |
(see module edpic.c) |
| 101 |
.RETURNS Number of decimals / -1 for error or no number |
| 102 |
.REMARKS returned value is e.g. 101 for 1.01 |
| 103 |
--------*/ |
| 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 */ |
| 108 |
{ |
| 109 |
char *p, *pe, *pdot, *a, x; |
| 110 |
int nd, i, la; |
| 111 |
long int_part; |
| 112 |
double atof(); |
| 113 |
|
| 114 |
p = str + oscspan((unsigned char *)str, len, _SPACE_, main_ascii); |
| 115 |
pe = str + len; |
| 116 |
*value = 0.e0; |
| 117 |
sign = 0; |
| 118 |
trerror->errno = 0; |
| 119 |
trerror->msg = pic; |
| 120 |
trerror->str = str; |
| 121 |
trerror->len = len; |
| 122 |
|
| 123 |
p += Sign(p, pe-p); /* Locate Sign */ |
| 124 |
pdot = pe; |
| 125 |
|
| 126 |
for (i=0; (i < sizeof(emarks)) && (pdot == pe); i++) |
| 127 |
pdot = p + oscloc(p, pe-p, emarks[i]); |
| 128 |
|
| 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]); |
| 132 |
|
| 133 |
x = *a, *a = '\0'; |
| 134 |
nd = tr_pic(p, pdot-p, pic, &int_part); |
| 135 |
*a = x; |
| 136 |
if (nd < 0) FINISH; |
| 137 |
|
| 138 |
/* Edit Number */ |
| 139 |
|
| 140 |
edited[0] = (sign ? '-' : ' '); |
| 141 |
|
| 142 |
for (i=11; --i>0; int_part /= 10) |
| 143 |
edited[i] = (int_part % 10) + '0'; |
| 144 |
edited[11] = '.'; |
| 145 |
|
| 146 |
/* Copy Remaining part. x is an indicator which takes values |
| 147 |
1 when exponent starts |
| 148 |
2 wneh number terminated |
| 149 |
*/ |
| 150 |
|
| 151 |
x = 0, p = pdot, i = 12; |
| 152 |
if (p == pe) goto GET_VALUE; |
| 153 |
if (*p == '.') nd++, p++; |
| 154 |
|
| 155 |
for ( ; (p < pe) && (trerror->errno == 0); p++) |
| 156 |
{ if (isspace(*p)) { x = 2; continue; } |
| 157 |
if (x == 2) { trerror->errno = TRERR_FLOAT; break; } |
| 158 |
switch(*p) |
| 159 |
{ case 'd': case 'D' : case 'e' : case 'E': |
| 160 |
if (x) trerror->errno = TRERR_FLOAT; |
| 161 |
edited[i++] = 'e'; |
| 162 |
x = 1; |
| 163 |
break; |
| 164 |
case '+': case '-': |
| 165 |
if (x == 0) /* Error, E missing */ |
| 166 |
trerror->errno = TRERR_FLOAT; |
| 167 |
edited[i++] = *p; |
| 168 |
break; |
| 169 |
default: |
| 170 |
if (isdigit(*p)) |
| 171 |
edited[i++] = *p, nd++; |
| 172 |
else trerror->errno = TRERR_DIGIT; |
| 173 |
break; |
| 174 |
} |
| 175 |
if (i >= (sizeof(edited)-1)) trerror->errno = TRERR_UNDEF; |
| 176 |
} |
| 177 |
if ((p < pe) && (trerror->errno == 0)) |
| 178 |
trerror->errno = TRERR_FLOAT; |
| 179 |
|
| 180 |
|
| 181 |
GET_VALUE: |
| 182 |
edited[i] = '\0'; |
| 183 |
if (trerror->errno) /* Bad number */ |
| 184 |
{ trerror->offset = (p-str); |
| 185 |
tr_error(); |
| 186 |
nd = -1; |
| 187 |
} |
| 188 |
else if (!stk) |
| 189 |
*value = atof(edited); |
| 190 |
|
| 191 |
FIN: |
| 192 |
if (stk) return(p - str); |
| 193 |
return(nd); |
| 194 |
} |
| 195 |
|
| 196 |
/*===========================================================================*/ |
| 197 |
int stkfpic(str, pic) |
| 198 |
/*++++++ |
| 199 |
.PURPOSE Compute the length of the tokenized string |
| 200 |
.RETURNS Length of str matched for specified picture |
| 201 |
.REMARKS |
| 202 |
--------*/ |
| 203 |
char *str; /* IN: String to scan */ |
| 204 |
char *pic; /* IN: The picture */ |
| 205 |
{ |
| 206 |
int status; |
| 207 |
double value; |
| 208 |
|
| 209 |
stk = 1; /* Tells tr_fpic that we just tokenize */ |
| 210 |
status = tr_fpic (str, strlen(str), pic, &value); |
| 211 |
stk = 0; |
| 212 |
|
| 213 |
return(status); |
| 214 |
} |