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

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

Explicitly mark fall-throughs.

  • 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 /* fall through */
151
152 case 'd':
153 decimal:
154 c = 12;
155 base = 10;
156 goto getval;
157
158 case 'X':
159 lflag = TRUE;
160 /* fall through */
161
162 case 'x':
163 c = 0;
164 base = 16;
165 goto getval;
166
167 case 'O':
168 lflag = TRUE;
169 /* fall through */
170
171 case 'o':
172 c = 14;
173 base = 8;
174 getval:
175 if (skipblk())
176 goto ateof;
177
178 lv = getnum(&list[c], &vals[c], base);
179
180 if (!dontdo) {
181
182 if (lflag)
183 *(int32_t *)(*args++) = lv;
184 else
185 **args++ = (int16_t)lv;
186 ++count;
187 }
188
189 break;
190
191#ifdef FLOAT
192 case 'E':
193 case 'F':
194 lflag = TRUE;
195
196 case 'e':
197 case 'f':
198 if (skipblk())
199 goto ateof;
200
201 d = getflt(tlist);
202
203 if (!dontdo) {
204
205 if (lflag)
206 *(double *)(*args++) = d;
207 else
208 *(float *)(*args++) = d;
209 ++count;
210 }
211
212 break;
213#endif
214
215 case '[':
216 lflag = FALSE;
217
218 if (*fmt EQ '^' || *fmt EQ '~') {
219
220 ++fmt;
221 lflag = TRUE;
222 }
223
224 for (cp = tlist ; (c = *fmt++) != ']' ; )
225 *cp++ = (int8_t)c;
226
227 *cp = 0;
228 goto string;
229
230 case 's':
231 lflag = TRUE;
232 tlist[0] = ' ';
233 tlist[1] = '\t';
234 tlist[2] = '\n';
235 tlist[3] = 0;
236 string:
237 if (skipblk())
238 goto ateof;
239
240 if (!dontdo)
241 cp = (int8_t *)*args++;
242 else
243 cp = NULL; /* fix compiler warning */
244
245 while (maxwide--) {
246
247 if ((c = (*gsub)(0)) EQ EOF)
248 break;
249
250 if (lflag ?
251 (index(tlist, (int8_t)c) NE 0) :
252 (index(tlist, (int8_t)c) EQ 0)) {
253
254 (*gsub)(1); /* unget last character */
255 break;
256 }
257
258 if (!dontdo)
259 *cp++ = (int8_t)c;
260 }
261
262 if (!dontdo) {
263
264 *cp = 0;
265 ++count;
266 }
267
268 break;
269
270 case 'c':
271 if ((c = (*gsub)(0)) EQ EOF)
272 goto ateof;
273
274 if (!dontdo) {
275
276 *(int8_t *)(*args++) = (int8_t)c;
277 ++count;
278 }
279
280 break;
281 }
282
283 } else if (isspace(c)) {
284
285 if (skipblk()) {
286ateof:
287 if (count EQ 0)
288 return(EOF);
289
290 return(count);
291 }
292
293 } else {
294
295matchit:
296 if (skipblk())
297 goto ateof;
298
299 if ((*gsub)(0) != c)
300 return(count);
301 }
302 }
303
304 return(count);
305}
306
307#ifdef FLOAT
308
309double
310getflt(buffer)
311char *buffer;
312{
313 register int c;
314 char decpt, sign, exp;
315 register char *cp;
316 double atof();
317
318 cp = buffer;
319 sign = exp = decpt = 0;
320
321 while (maxwide--) {
322
323 c = (*gsub)(0);
324
325 if (!sign AND (c EQ '-' OR c EQ '+'))
326 sign = 1;
327 else if (!decpt AND c EQ '.')
328 decpt = 1;
329 else if (!exp AND (c EQ 'e' OR c EQ 'E')) {
330
331 sign = 0;
332 exp = decpt = 1;
333
334 } else if (!isdigit(c)) {
335
336 (*gsub)(1);
337 break;
338 }
339
340 *cp++ = c;
341 }
342
343 *cp = 0;
344 return(atof(buffer));
345}
346
347#endif
348
349int16_t sscanf(int8_t *string, int8_t *fmt, int16_t *args)
350{
351 scnstr = string;
352 quit = FALSE;
353 return(scanfmt(sgetc, fmt, &args));
354}
355
356
Note: See TracBrowser for help on using the repository browser.