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