| 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 tral.c |
| 30 |
.AUTHOR Francois Ochsenbein [ESO] |
| 31 |
.LANGUAGE C |
| 32 |
.CATEGORY Conversion from character to long integer. |
| 33 |
|
| 34 |
.COMMENTS |
| 35 |
|
| 36 |
.VERSION 1.0 20-Oct-1988: Creation |
| 37 |
.VERSION 1.1 08-Dec-1989: Numbers may be followed by K. |
| 38 |
Also allow characters (e.g. a) and Controls (^X) |
| 39 |
within apostrophes |
| 40 |
|
| 41 |
090831 last modif |
| 42 |
-----------------------------------------------*/ |
| 43 |
|
| 44 |
#include <midas_def.h> |
| 45 |
#include <tra.h> |
| 46 |
#include <atype.h> |
| 47 |
|
| 48 |
#define LEN 80 |
| 49 |
|
| 50 |
#define issign(c) ((c == '+') || (c == '-')) |
| 51 |
#define FINISH goto FIN |
| 52 |
#define GobbleSpaces(s,l) s += oscspan((unsigned char *)s, l, _SPACE_, main_ascii) |
| 53 |
|
| 54 |
extern int tr_error(); |
| 55 |
|
| 56 |
|
| 57 |
MID_EXTERN struct trerr_s *trerror; |
| 58 |
|
| 59 |
MID_STATIC long accu; |
| 60 |
MID_STATIC int base = 10; |
| 61 |
MID_STATIC char sign; |
| 62 |
|
| 63 |
/*===========================================================================*/ |
| 64 |
static int Number(str, len) |
| 65 |
/*++++++ |
| 66 |
.PURPOSE Just scan a string until non-valid characters are found. |
| 67 |
The number is stored in accu. |
| 68 |
.RETURNS Number of valid characters |
| 69 |
.REMARKS Not traced. |
| 70 |
--------*/ |
| 71 |
char *str; /* IN: The string to scan */ |
| 72 |
int len; /* IN: Length of the string */ |
| 73 |
{ |
| 74 |
char *p, *pe; |
| 75 |
int i; |
| 76 |
|
| 77 |
accu = 0; |
| 78 |
if (len <= 0) return(0); |
| 79 |
|
| 80 |
for (p = str, pe = p + len; (p < pe) && (isxdigit(*p)); p++) |
| 81 |
{ if (isdigit(*p)) i = '0'; |
| 82 |
else if (isupper(*p)) i = 'A' - 10; |
| 83 |
else i = 'a' - 10; |
| 84 |
i = *p - i; |
| 85 |
if (i >= base) break; |
| 86 |
accu = accu * base + i; |
| 87 |
} |
| 88 |
i = p - str; /* Number of Digits */ |
| 89 |
|
| 90 |
return(i); |
| 91 |
} |
| 92 |
|
| 93 |
/*===========================================================================*/ |
| 94 |
static int Sign(str, len) |
| 95 |
/*++++++ |
| 96 |
.PURPOSE Just scan the beginning of a string: skip leading blanks, |
| 97 |
and get the sign. |
| 98 |
.RETURNS Number of valid characters |
| 99 |
.REMARKS Not traced. |
| 100 |
--------*/ |
| 101 |
char *str; /* IN: The string to scan */ |
| 102 |
int len; /* IN: Length of the string */ |
| 103 |
{ |
| 104 |
char *p, *pe; |
| 105 |
|
| 106 |
sign = 0; |
| 107 |
if (len <= 0) return(0); |
| 108 |
|
| 109 |
p = str, pe = p + len; |
| 110 |
GobbleSpaces(p, len); |
| 111 |
|
| 112 |
if (p < pe) |
| 113 |
{ switch(*p) |
| 114 |
{ case '-': sign = 1; |
| 115 |
case '+': p++; break; |
| 116 |
} |
| 117 |
GobbleSpaces(p, pe-p); /* Skip blanks */ |
| 118 |
} |
| 119 |
|
| 120 |
return(p-str); |
| 121 |
} |
| 122 |
|
| 123 |
/*===========================================================================*/ |
| 124 |
int tr_al(str, len, value) |
| 125 |
/*++++++ |
| 126 |
.PURPOSE Converts a string to a long number. |
| 127 |
Input is assumed to be in decimal, unless the string starts |
| 128 |
with 'X' (hexadecimal) or 'O' (octal). |
| 129 |
The digits may be followed by K (implies a 1024 factor) |
| 130 |
.RETURNS Number of digits (excluding spaces) scanned, -1 for |
| 131 |
error (message logged) |
| 132 |
.REMARKS Return 0 if string contains only spaces or a sign. |
| 133 |
Value is then set to zero. |
| 134 |
--------*/ |
| 135 |
char *str; /* IN: String to scan */ |
| 136 |
int len; /* IN: Length of str */ |
| 137 |
long *value; /* OUT: result */ |
| 138 |
{ |
| 139 |
char *p, *pe; |
| 140 |
int nd; |
| 141 |
|
| 142 |
trerror->errno = 0; |
| 143 |
|
| 144 |
base = 10; |
| 145 |
nd = 1; |
| 146 |
accu = 0; |
| 147 |
|
| 148 |
p = str + Sign(str, len); /* Skip trailer + sign */ |
| 149 |
pe = str + len; |
| 150 |
|
| 151 |
if (*p == '\'') /* Get a Character */ |
| 152 |
{ p++; |
| 153 |
if (*p != '\'') accu = *(p++); |
| 154 |
if (*p == '\'') p++; |
| 155 |
goto check_remaining_bytes; |
| 156 |
} |
| 157 |
if (*p == '^') /* Get a Control Character */ |
| 158 |
{ p++; |
| 159 |
accu = tocntrl(*p), p++; |
| 160 |
goto check_remaining_bytes; |
| 161 |
} |
| 162 |
|
| 163 |
nd = Number(p, pe-p); /* Get number */ |
| 164 |
p += nd; /* End of number */ |
| 165 |
|
| 166 |
/* If the string is not completely understood, look if |
| 167 |
it can be due to O(octal), X(hexadecimal), k (k-factor) |
| 168 |
Note that X and O can only be at the beginning, i.e. |
| 169 |
when accu == 0; k can only be a suffix. |
| 170 |
*/ |
| 171 |
|
| 172 |
if (p < pe) switch(toupper(*p)) |
| 173 |
{ case 'O': base = 8; |
| 174 |
if (accu == 0) goto special_base; |
| 175 |
break; |
| 176 |
case 'X': base = 16; |
| 177 |
if (accu == 0) goto special_base; |
| 178 |
break; |
| 179 |
special_base: |
| 180 |
p++; |
| 181 |
nd = Number(p, pe-p); |
| 182 |
p += nd; |
| 183 |
break; |
| 184 |
case 'K': |
| 185 |
accu *= 1024; |
| 186 |
p++; |
| 187 |
break; |
| 188 |
} |
| 189 |
|
| 190 |
/* Take Sign into account, and check remaining bytes are only |
| 191 |
white spaces |
| 192 |
*/ |
| 193 |
|
| 194 |
check_remaining_bytes: |
| 195 |
*value = (sign ? -accu : accu); |
| 196 |
|
| 197 |
GobbleSpaces(p, pe-p); |
| 198 |
|
| 199 |
if (p < pe) /* Bad String */ |
| 200 |
{ switch(base) |
| 201 |
{ case 8: trerror->errno = TRERR_OCTAL; break; |
| 202 |
case 10: trerror->errno = TRERR_DIGIT; break; |
| 203 |
case 16: trerror->errno = TRERR_HEXA; break; |
| 204 |
} |
| 205 |
trerror->str = str; |
| 206 |
trerror->len = len; |
| 207 |
trerror->offset = (p-str); |
| 208 |
nd = -1; |
| 209 |
tr_error(); |
| 210 |
} |
| 211 |
|
| 212 |
return(nd); |
| 213 |
} |
| 214 |
|
| 215 |
/*===========================================================================*/ |
| 216 |
int tr_al1(str, len, value, n) |
| 217 |
/*++++++ |
| 218 |
.PURPOSE Converts a string to a vector of numbers. |
| 219 |
Numbers may be separated by blanks, commas, or any punctuation character, |
| 220 |
and may start with 'X' (hexadecimal) or 'O' (octal). |
| 221 |
.RETURNS Number of found numbers (<= n) / -1 for |
| 222 |
error (message logged) |
| 223 |
.REMARKS Return 0 if string contains only spaces. |
| 224 |
--------*/ |
| 225 |
char *str; /* IN: String to scan */ |
| 226 |
int len; /* IN: Length of str */ |
| 227 |
long *value; /* OUT: array result */ |
| 228 |
int n; /* IN: Number of values */ |
| 229 |
{ |
| 230 |
char *p, *pe, *q; |
| 231 |
int i, nd; |
| 232 |
|
| 233 |
trerror->errno = 0; |
| 234 |
|
| 235 |
p = str, pe = p + len; |
| 236 |
|
| 237 |
for (i=0; (p<pe) && (i < n); i++) |
| 238 |
{ GobbleSpaces(p, pe-p), q = p; |
| 239 |
if (p == pe) break; |
| 240 |
if (issign(*q)) q++; |
| 241 |
q += oscscan((unsigned char *)q, pe-q, _PUNCT_|_SPACE_, main_ascii); |
| 242 |
nd = tr_al(p, q-p, value+i); |
| 243 |
if (nd < 0) FINISH; /* Error Encountered */ |
| 244 |
if (nd == 0) /* No number */ |
| 245 |
{ trerror->errno = TRERR_VOID; break; } |
| 246 |
p = q; |
| 247 |
if (p == pe) continue; |
| 248 |
if (issign(*p)) continue; |
| 249 |
if (ispunct(*p)) p++; |
| 250 |
} |
| 251 |
|
| 252 |
/* Check string exhausted */ |
| 253 |
|
| 254 |
if (trerror->errno == 0) |
| 255 |
{ GobbleSpaces(p, pe-p); |
| 256 |
if (p < pe) trerror->errno = TRERR_TOOMANY; |
| 257 |
} |
| 258 |
if (trerror->errno) |
| 259 |
{ trerror->str = str, trerror->len = len, |
| 260 |
trerror->offset = (p - str); |
| 261 |
tr_error(); |
| 262 |
} |
| 263 |
|
| 264 |
FIN: |
| 265 |
return((trerror->errno ? -1 : i)); |
| 266 |
} |
| 267 |
|
| 268 |
/*===========================================================================*/ |
| 269 |
int tr_aln(str, len, value, n) |
| 270 |
/*++++++ |
| 271 |
.PURPOSE Converts a string to a vector of numbers. |
| 272 |
Numbers may be separated by blanks or any punctuation character, |
| 273 |
and may start with 'X' (hexadecimal) or 'O' (octal). |
| 274 |
There must be exactly the required number of values. |
| 275 |
.RETURNS n for success / -1 for failure |
| 276 |
.REMARKS Return 0 if string contains only spaces or a sign. |
| 277 |
Value is then set to zero. |
| 278 |
--------*/ |
| 279 |
char *str; /* IN: String to scan */ |
| 280 |
int len; /* IN: Length of str */ |
| 281 |
long *value; /* OUT: array result */ |
| 282 |
int n; /* IN: Number of values */ |
| 283 |
{ |
| 284 |
int i; |
| 285 |
|
| 286 |
i = tr_al1(str, len, value, n); |
| 287 |
|
| 288 |
if ((i >= 0) && (i < n)) /* Too few numbers */ |
| 289 |
{ trerror->errno = TRERR_TOOFEW; |
| 290 |
trerror->str = str, trerror->len = len, |
| 291 |
trerror->offset = len; |
| 292 |
tr_error(); |
| 293 |
} |
| 294 |
|
| 295 |
return((trerror->errno ? -1 : i)); |
| 296 |
} |