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

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

Removed form-feed comments.

  • Property mode set to 100644
File size: 4.6 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, (*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++ = 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++ = 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 = *args++;
239
240 while (maxwide--) {
241
242 if ((c = (*gsub)(0)) EQ EOF)
243 break;
244
245 if (lflag ?
246 (index(tlist, c) NE 0) :
247 (index(tlist, c) EQ 0)) {
248
249 (*gsub)(1); /* unget last character */
250 break;
251 }
252
253 if (!dontdo)
254 *cp++ = c;
255 }
256
257 if (!dontdo) {
258
259 *cp = 0;
260 ++count;
261 }
262
263 break;
264
265 case 'c':
266 if ((c = (*gsub)(0)) EQ EOF)
267 goto ateof;
268
269 if (!dontdo) {
270
271 *(int8_t *)(*args++) = c;
272 ++count;
273 }
274
275 break;
276 }
277
278 } else if (isspace(c)) {
279
280 if (skipblk()) {
281ateof:
282 if (count EQ 0)
283 return(EOF);
284
285 return(count);
286 }
287
288 } else {
289
290matchit:
291 if (skipblk())
292 goto ateof;
293
294 if ((*gsub)(0) != c)
295 return(count);
296 }
297 }
298
299 return(count);
300}
301
302#ifdef FLOAT
303
304double
305getflt(buffer)
306char *buffer;
307{
308 register int c;
309 char decpt, sign, exp;
310 register char *cp;
311 double atof();
312
313 cp = buffer;
314 sign = exp = decpt = 0;
315
316 while (maxwide--) {
317
318 c = (*gsub)(0);
319
320 if (!sign AND (c EQ '-' OR c EQ '+'))
321 sign = 1;
322 else if (!decpt AND c EQ '.')
323 decpt = 1;
324 else if (!exp AND (c EQ 'e' OR c EQ 'E')) {
325
326 sign = 0;
327 exp = decpt = 1;
328
329 } else if (!isdigit(c)) {
330
331 (*gsub)(1);
332 break;
333 }
334
335 *cp++ = c;
336 }
337
338 *cp = 0;
339 return(atof(buffer));
340}
341
342#endif
343
344int16_t sscanf(int8_t *string, int8_t *fmt, int16_t *args)
345{
346 scnstr = string;
347 quit = FALSE;
348 return(scanfmt(sgetc, fmt, &args));
349}
350
351
Note: See TracBrowser for help on using the repository browser.