Debian Codegraph @

prim/tc3/libsrc/trpic.c

raw
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 trpic.c
30 .AUTHOR Francois Ochsenbein [ESO]
31 .LANGUAGE C
32 .CATEGORY Conversion from character to binary using a picture.
33
34 .COMMENTS
35 Definition of pictures are in edpic.c
36
37
38 .VERSION 1.0 27-May-1988: Creation
39 .VERSION 1.1 08-Jul-1988: Modified for separator = '.':
40 following is left-adjusted...
41 .VERSION 1.2 21-Oct-1988: Cosmetic Modifications.
42 .VERSION 1.3 13-Jun-1989: Allow dashes (-) in middle of a number
43 .VERSION 1.4 22-Mar-1990: Added stkpic (tokenisation)
44 .VERSION 1.5 01-Aug-1990: Modified tr_pic, to allow e.g. +995959 pictures
45 (no blanks embedded)
46
47 090831 last modif
48 -----------------------------------------*/
49
50 #include <midas_def.h>
51 #include <macrogen.h>
52 #include <tra.h>
53 #include <atype.h>
54 #include <string.h>
55
56 extern int tr_error();
57
58 MID_EXTERN struct trerr_s *trerror;
59
60 #define LEN 132
61
62 #define FINISH goto FIN
63 #define GobbleSpaces(s,l) s += oscspan((unsigned char *)s, l, _SPACE_, main_ascii)
64
65 MID_STATIC long accu;
66 MID_RSTATIC long p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
67 100000000, 1000000000};
68 MID_STATIC int base = 10;
69 MID_STATIC char sign;
70 MID_STATIC char stk = 0; /* Indicator for stkpic */
71
72 /*===========================================================================*/
73 static int Number(str, len)
74 /*++++++
75 .PURPOSE Just scan a string until non-valid characters are found.
76 The number is stored in accu.
77 .RETURNS Number of valid characters
78 .REMARKS Not traced.
79 --------*/
80 char *str; /* IN: The string to scan */
81 int len; /* IN: Length of the string */
82 {
83 char *p, *pe;
84 int i;
85
86 accu = 0;
87 if (len <= 0) return(0);
88
89 for (p = str, pe = p + len; (p < pe) && (isxdigit(*p)); p++)
90 { if (isdigit(*p)) i = '0';
91 else if (isupper(*p)) i = 'A' - 10;
92 else i = 'a' - 10;
93 i = *p - i;
94 if (i >= base) break;
95 accu = accu * base + i;
96 }
97 i = p - str; /* Number of Digits */
98
99 return(i);
100 }
101
102 /*===========================================================================*/
103 static int Sign(str, len)
104 /*++++++
105 .PURPOSE Just scan the beginning of a string: skip leading blanks,
106 and get the sign.
107 .RETURNS Number of valid characters
108 .REMARKS Not traced.
109 --------*/
110 char *str; /* IN: The string to scan */
111 int len; /* IN: Length of the string */
112 {
113 char *p, *pe;
114
115 sign = 0;
116 if (len <= 0) return(0);
117
118 p = str, pe = p + len;
119 GobbleSpaces(p, len);
120
121 if (p < pe)
122 { switch(*p)
123 { case '-': sign = 1;
124 case '+': p++; break;
125 }
126 GobbleSpaces(p, pe-p); /* Skip blanks */
127 }
128
129 return(p-str);
130 }
131
132 /*===========================================================================*/
133 int tr_pic(str, len, pic, value)
134 /*++++++
135 .PURPOSE Converts a string to a long number according to a picture
136 (see module edpic.c)
137 .RETURNS Length of matched characters in picture / -1 for error or no number
138 .REMARKS returned value is e.g. 101 for 1.01 under picture 9.99
139 --------*/
140 char *str; /* IN: String to scan */
141 int len; /* IN: Length of str */
142 char *pic; /* IN: The picture */
143 long *value; /* OUT: result */
144 {
145 char *p, *pe, *a, *astop, b, separator, sign_found;
146 int i, nd, nda;
147 long limit;
148
149 b = ' ' ;
150 p = str, pe = p + len;
151 GobbleSpaces(p, len);
152 *value = 0;
153 sign = 0;
154 separator = 0;
155 sign_found = 0;
156 trerror->errno= 0;
157
158 for (a = pic, astop = a; *a; a += i, p += nd) {
159 /* if ((p < pe) && (*p == '&')) pe = p; */
160 i = 0, nd = 0, limit = 0, base = 0;
161 switch(*a)
162 { case '+': a++; /* No digit allowed */
163 if (sign_found) goto case_other;
164 i = oscskip(a, LEN, '+');
165 goto case_sign;
166 case '-': /* Look for sign */
167 if (sign_found) goto case_other;
168 if (*p == '-') a++;
169 i = oscskip(a, LEN, '-');
170 case_sign:
171 sign_found = 1;
172 p += Sign(p, pe-p);
173 b = '9';
174 if (a[i] == '0') goto case_0;
175 goto case_other;
176 #if 0
177 case '&': /* It's a stop */
178 GobbleSpaces(p, pe-p); /* Skip trailing spaces */
179 pe = p;
180 FINISH;
181 #endif
182 case ' ':
183 GobbleSpaces(p, pe-p);
184 break;
185
186 case '\\': i = 2; /* Escape */
187 if ((p < pe) && (*p != a[1])) goto BAD;
188 nd = 1;
189 break;
190
191 case_hexa:
192 case 'X':
193 sign_found = 1;
194 i += oscskip(a, LEN, 'X');
195 if (i < 8) limit = 1 << (4*i);
196 base = 16;
197 break;
198
199 case_0:
200 case '0':
201 sign_found = 1;
202 i += oscskip(a, LEN, '0');
203 b = '9'; /* Default base */
204
205 case_other:
206 default:
207 if ((i > 0) && (a[i] == 'X'))
208 goto case_hexa;
209 if (isdigit(a[i]))
210 { sign_found = 1;
211 b = a[i++];
212 while (b == a[i]) i++;
213 }
214 if (i > 0) /* Must get a number ... */
215 { base = b - '0' + 1;
216 if ((isdigit(*a)) && (*a != '0'))
217 Number(a, i), limit = accu + 1;
218 else if ((base == 10) && (i < ITEMS(p10)))
219 limit = p10[i];
220 break;
221 }
222 if (p >= pe) break;
223 i = 1;
224 if (isspace(*p)) /* Skip trailing spaces */
225 { GobbleSpaces(p, pe-p);
226 /* if (*p == '&') pe = p; */
227 }
228 else nd = 1;
229 if (p >= pe) break;
230 if (*a == '.') sign_found = 1;
231 if (*p != *a) goto BAD;
232 separator = *p;
233 nd = 1;
234 }
235 if (i == 0) i = oscskip(a, LEN, *a);
236 nda = i; /* Number of bytes taken into account in picture */
237 if (base) /* Must get a number... */
238 { nd = Number(p, MIN(nda, pe-p));
239 if (nd > i) /* Too many digits */
240 FINISH;
241 if (limit)
242 { if (accu >= limit) FINISH; /* Bad... */
243 *value = *value * limit;
244 }
245 if ((separator == '.') && (base == 10))
246 /* Decimal part left adjusted... */
247 accu *= p10[i-nd], nda = nd;
248 *value += accu;
249 }
250 if (p < pe) astop = a+nda;
251 }
252 FIN:
253 GobbleSpaces(p, pe-p); /* Skip trailing spaces */
254
255 BAD:
256 /* We've finished... When stkpic was called, we've to return
257 the length of the matched string --- otherwise the
258 length of the matched pattern (picture)
259 */
260
261 if (stk) return(p - str); /* Called from stkpic */
262
263 if (sign) *value = -(*value);
264
265 if (p < pe) /* Bad number */
266 { trerror->errno = TRERR_UNDEF;
267 trerror->msg = pic;
268 trerror->str = str;
269 trerror->len = len;
270 trerror->offset = (p-str);
271 tr_error();
272 i = -1;
273 }
274 else i = astop - pic;
275
276 return(i);
277 }
278
279 /*===========================================================================*/
280 int stkpic(str, pic)
281 /*++++++
282 .PURPOSE Compute the length of the tokenized string
283 .RETURNS Length of str matched for specified picture
284 .REMARKS
285 --------*/
286 char *str; /* IN: String to scan */
287 char *pic; /* IN: The picture */
288 {
289 int status;
290 long value;
291
292 stk = 1; /* Tells tr_pic that we just tokenize */
293 status = tr_pic (str, strlen(str), pic, &value);
294 stk = 0;
295
296 return(status);
297 }