source: buchla-68k/libcio/scan.c@ 8973acd

Last change on this file since 8973acd was 8973acd, checked in by Thomas Lopatic <thomas@…>, 7 years ago

No more warnings in libcio.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 =============================================================================
3 scan.c -- input scan conversion for the portable C I/O Library
4 Version 3 -- 1989-01-16 -- D.N. Lynx Crowe
5 =============================================================================
6*/
7
8#include "ram.h"
9
10static int16_t maxwide;
11static int16_t (*gsub)(int16_t what);
12
13static int8_t *scnstr;
14static int8_t quit;
15
16static int32_t getnum(int8_t *list, int8_t *values, int16_t base)
17{
18 register int32_t val;
19 register int8_t *cp;
20 int16_t c;
21 int16_t sign;
22
23 if (maxwide LE 0)
24 return(0L);
25
26 val = sign = 0;
27
28 if ((c = (*gsub)(0)) EQ '-') {
29
30 sign = 1;
31 --maxwide;
32
33 } else if (c EQ '+')
34 --maxwide;
35 else
36 (*gsub)(1);
37
38 while (maxwide--) {
39
40 if ((cp = index(list, (int8_t)(*gsub)(0))) EQ NULL) {
41
42 (*gsub)(1);
43 break;
44 }
45
46 val *= base;
47 val += values[(int32_t)cp - (int32_t)list];
48 }
49
50 if (sign)
51 val = -val;
52
53 return(val);
54}
55
56static int16_t skipblk(void)
57{
58 while (isspace((*gsub)(0)))
59 ;
60
61 if ((*gsub)(1) EQ EOF)
62 return(EOF);
63
64 return(0);
65}
66
67static int16_t sgetc(int16_t what)
68{
69 if (what EQ 0) {
70
71 if (*scnstr)
72 return(*scnstr++ & 0x00FF);
73
74 quit = TRUE;
75
76 } else {
77
78 if (!quit)
79 return(*--scnstr & 0x00FF);
80 }
81
82 return(EOF);
83}
84
85int16_t scanfmt(int16_t (*getsub)(int16_t what), int8_t *fmt, int16_t **args)
86{
87
88#ifdef FLOAT
89 double getflt(), d;
90#endif
91
92 int32_t lv;
93 int16_t c, count, dontdo, lflag, base;
94 int8_t *cp;
95 int8_t tlist[130];
96
97 static int8_t list[] = "ABCDEFabcdef9876543210";
98
99 static int8_t vals[] = {
100
101 10,11,12,13,14,15,10,11,12,13,14,15,9,8,7,6,5,4,3,2,1,0
102 };
103
104
105 count = 0;
106 gsub = getsub;
107
108 while ((c = *fmt++)) {
109
110 if (c EQ '%') {
111
112 lflag = dontdo = FALSE;
113 maxwide = 127;
114
115 if (*fmt EQ '*') {
116 ++fmt;
117 dontdo = 1;
118 }
119
120 if (isdigit(*fmt)) {
121
122 maxwide = 0;
123
124 do {
125
126 maxwide = maxwide*10 + *fmt - '0';
127
128 } while (isdigit(*++fmt));
129 }
130
131 if (*fmt EQ 'l') {
132
133 lflag = TRUE;
134 ++fmt;
135 }
136
137
138 switch (*fmt++) {
139
140 case '%':
141 c = '%';
142 goto matchit;
143
144 case 'h': /* specify short (for compatibility) */
145 lflag = FALSE;
146 goto decimal;
147
148 case 'D':
149 lflag = TRUE;
150
151 case 'd':
152 decimal:
153 c = 12;
154 base = 10;
155 goto getval;
156
157 case 'X':
158 lflag = TRUE;
159
160 case 'x':
161 c = 0;
162 base = 16;
163 goto getval;
164
165 case 'O':
166 lflag = TRUE;
167
168 case 'o':
169 c = 14;
170 base = 8;
171 getval:
172 if (skipblk())
173 goto ateof;
174
175 lv = getnum(&list[c], &vals[c], base);
176
177 if (!dontdo) {
178
179 if (lflag)
180 *(int32_t *)(*args++) = lv;
181 else
182 **args++ = (int16_t)lv;
183 ++count;
184 }
185
186 break;
187
188#ifdef FLOAT
189 case 'E':
190 case 'F':
191 lflag = TRUE;
192
193 case 'e':
194 case 'f':
195 if (skipblk())
196 goto ateof;
197
198 d = getflt(tlist);
199
200 if (!dontdo) {
201
202 if (lflag)
203 *(double *)(*args++) = d;
204 else
205 *(float *)(*args++) = d;
206 ++count;
207 }
208
209 break;
210#endif
211
212 case '[':
213 lflag = FALSE;
214
215 if (*fmt EQ '^' || *fmt EQ '~') {
216
217 ++fmt;
218 lflag = TRUE;
219 }
220
221 for (cp = tlist ; (c = *fmt++) != ']' ; )
222 *cp++ = (int8_t)c;
223
224 *cp = 0;
225 goto string;
226
227 case 's':
228 lflag = TRUE;
229 tlist[0] = ' ';
230 tlist[1] = '\t';
231 tlist[2] = '\n';
232 tlist[3] = 0;
233 string:
234 if (skipblk())
235 goto ateof;
236
237 if (!dontdo)
238 cp = (int8_t *)*args++;
239 else
240 cp = NULL; /* fix compiler warning */
241
242 while (maxwide--) {
243
244 if ((c = (*gsub)(0)) EQ EOF)
245 break;
246
247 if (lflag ?
248 (index(tlist, (int8_t)c) NE 0) :
249 (index(tlist, (int8_t)c) EQ 0)) {
250
251 (*gsub)(1); /* unget last character */
252 break;
253 }
254
255 if (!dontdo)
256 *cp++ = (int8_t)c;
257 }
258
259 if (!dontdo) {
260
261 *cp = 0;
262 ++count;
263 }
264
265 break;
266
267 case 'c':
268 if ((c = (*gsub)(0)) EQ EOF)
269 goto ateof;
270
271 if (!dontdo) {
272
273 *(int8_t *)(*args++) = (int8_t)c;
274 ++count;
275 }
276
277 break;
278 }
279
280 } else if (isspace(c)) {
281
282 if (skipblk()) {
283ateof:
284 if (count EQ 0)
285 return(EOF);
286
287 return(count);
288 }
289
290 } else {
291
292matchit:
293 if (skipblk())
294 goto ateof;
295
296 if ((*gsub)(0) != c)
297 return(count);
298 }
299 }
300
301 return(count);
302}
303
304#ifdef FLOAT
305
306double
307getflt(buffer)
308char *buffer;
309{
310 register int c;
311 char decpt, sign, exp;
312 register char *cp;
313 double atof();
314
315 cp = buffer;
316 sign = exp = decpt = 0;
317
318 while (maxwide--) {
319
320 c = (*gsub)(0);
321
322 if (!sign AND (c EQ '-' OR c EQ '+'))
323 sign = 1;
324 else if (!decpt AND c EQ '.')
325 decpt = 1;
326 else if (!exp AND (c EQ 'e' OR c EQ 'E')) {
327
328 sign = 0;
329 exp = decpt = 1;
330
331 } else if (!isdigit(c)) {
332
333 (*gsub)(1);
334 break;
335 }
336
337 *cp++ = c;
338 }
339
340 *cp = 0;
341 return(atof(buffer));
342}
343
344#endif
345
346int16_t sscanf(int8_t *string, int8_t *fmt, int16_t *args)
347{
348 scnstr = string;
349 quit = FALSE;
350 return(scanfmt(sgetc, fmt, &args));
351}
352
353
Note: See TracBrowser for help on using the repository browser.