34 #define _CRT_SECURE_NO_WARNINGS
43 #define MAXLN (10 * 1024)
45 #define ISSPACE " \t\n\r\f\v"
51 val = (int) (val & 0xFF);
53 int ret = isspace(val);
87 unsigned long value, v1, v2;
91 if (p[0] ==
'0' && (p[1] ==
'x' || p[1] ==
'X'))
97 if (base == 16 && (q = strchr(p,
'.')) != 0)
99 if (q - p >
sizeof(tmp) - 1)
104 strncpy(tmp, p, q - p);
121 *vp = (v1 << 16) + v2;
128 if (*p >=
'0' && *p <=
'9')
132 else if (*p >=
'a' && *p <=
'f')
134 digit = *p -
'a' + 10;
136 else if (*p >=
'A' && *p <=
'F')
138 digit = *p -
'A' + 10;
160 int binAtob(
unsigned long *vp,
char *p,
int base)
188 count = vfscanf(stdin, fmt, ap);
203 count = vfscanf(fp, fmt, ap);
218 count = vsscanf(buf, fmt, ap);
232 if (fgets(buf,
MAXLN, fp) == 0)
236 count = vsscanf(buf, fmt, ap);
252 binVsscanf(
const char *bufOrg,
const char *
s, va_list ap,
int bufLen)
254 int count, noassign, base, lflag;
261 bufEnd = buf + bufLen;
263 count = noassign = width = lflag = base = 0;
265 while (*
s && (buf < bufEnd))
274 if (strchr(
"dibouxycsefg%", *
s))
282 else if (*
s ==
'l' || *
s ==
'L')
286 else if (*
s >=
'1' && *
s <=
'9')
288 for (tc =
s; isdigit(*
s);
s++);
289 strncpy(tmp, tc,
s - tc);
291 binAtob((
unsigned long *) &width, tmp, 10);
303 width = (int) strcspn(buf,
ISSPACE);
307 strncpy(t = va_arg(ap,
char *), buf, width);
320 strncpy(t = va_arg(ap,
char *), buf, width);
325 else if (strchr(
"dobxyu", *
s))
331 if (*
s ==
'd' || *
s ==
'u')
355 width = (int) strcspn(buf,
ISSPACE);
359 width = (int) (strchr(buf, *(
s + 1)) - buf);
364 memcpy(tmp, buf, width);
365 unsigned char *destAdr = va_arg(ap,
unsigned char *);
366 unsigned long destVal = 0;
367 for (
int i = 0; i < width; i++)
370 destVal |= (
unsigned char) (0xFF & tmp[i]);
372 for (
int i = 0; i < width; i++)
374 unsigned char val = (
unsigned char) (0xFF & (destVal >> (i * 8)));
380 strncpy(tmp, buf, width);
384 binAtob(va_arg(ap,
unsigned long *), tmp, base);
393 width = noassign = lflag = 0;
417 binScanfVec(
const std::vector<unsigned char> *vec,
const char *fmt, ...)
419 if (vec->size() == 0)
423 const char *buf = (
const char *) (&(*vec)[0]);
425 int bufLen = (int) vec->size();
445 if (scanfMask == NULL)
450 const char *
s = scanfMask;
451 const char *tc = NULL;
464 if (strchr(
"dibouxycsefg%", *
s))
472 else if (*
s ==
'l' || *
s ==
'L')
476 else if (*
s >=
'1' && *
s <=
'9')
478 for (tc =
s; isdigit(*
s);
s++);
479 strncpy(tmp, tc,
s - tc);
481 sscanf(tmp,
"%d", &width);