29 #include <sys/types.h>
37 #if !defined(off_t) && !defined(__APPLE__) && !defined(__CYGWIN__)
72 return CHAR_BIT *
sizeof(long);
78 while (isspace(p = fgetc(s)));
84 SetBit(
unsigned long *bitmap,
unsigned int bit) {
89 TestBit(
unsigned long *bitmap,
unsigned int bit) {
90 return static_cast<int>(bitmap[bit/
LongBit()] >> (bit%
LongBit())) & 1;
93 static inline int DigitValue(
int ch,
int base) {
94 if (ch >=
'0' && ch <=
'9') {
95 if (base >= 10 || ch <=
'7')
97 }
else if (ch >=
'A' && ch <=
'Z' && base == 16) {
99 }
else if (ch >=
'a' && ch <=
'z' && base == 16) {
112 isspace(static_cast<unsigned char>(c)) && (c != EOF);
116 if (c ==
'-' || c ==
'+') {
125 if (c ==
'x' || c ==
'X') {
132 }
else if (base == 16) {
135 if (c ==
'x' || c ==
'X') c = fgetc(s);
140 for (; (c != EOF) && (d = DigitValue(c, base)) >= 0; c = fgetc(s))
144 return minus ? -v : v;
155 isspace(static_cast<unsigned char>(c)) && (c != EOF);
159 if (c ==
'-' || c ==
'+') {
165 for (; c != EOF && (d = DigitValue(c, 10)) >= 0; c = fgetc(s))
168 for (c = fgetc(s); c != EOF && (d = DigitValue(c, 10)) >= 0; c = fgetc(s)) {
173 double f =
static_cast<double>(v)
174 + static_cast<double>(w) /
static_cast<double>(k);
175 if (c ==
'e' || c ==
'E') {
178 if (c ==
'-' || c ==
'+') {
179 expsign = (c ==
'-') ? -1 : 1;
183 for (; (c != EOF) && (d = DigitValue(c, 10)) >= 0; c = fgetc(s)) {
184 exponent = exponent * 10 + d;
187 f *= pow(10.0, static_cast<double>(exponent));
191 return minus ? -f : f;
201 while(*s && isspace(static_cast<unsigned char>(*s))) s++;
204 if (*s ==
'-' || *s ==
'+') {
210 for (; *s && (d = DigitValue(*s, 10)) >= 0; s++)
213 for (++s; *s && (d = DigitValue(*s, 10)) >= 0; s++) {
218 if (*s ==
'e' || *s ==
'E')
219 tprintf(
"WARNING: Scientific Notation not supported!");
221 double f =
static_cast<double>(v)
222 + static_cast<double>(w) /
static_cast<double>(k);
224 return minus ? -f : f;
227 static int tvfscanf(FILE* stream,
const char *format, va_list ap);
229 int tfscanf(FILE* stream,
const char *format, ...) {
233 va_start(ap, format);
234 rv = tvfscanf(stream, format, ap);
242 int fscanf(FILE* stream,
const char *format, ...) {
246 va_start(ap, format);
247 rv = tvfscanf(stream, format, ap);
253 int vfscanf(FILE* stream,
const char *format, ...) {
257 va_start(ap, format);
258 rv = tvfscanf(stream, format, ap);
265 static int tvfscanf(FILE* stream,
const char *format, va_list ap) {
266 const char *p = format;
271 unsigned int width = UINT_MAX;
287 unsigned long matchmap[((1 << CHAR_BIT)+(CHAR_BIT *
sizeof(
long) - 1)) /
288 (CHAR_BIT *
sizeof(long))];
290 unsigned char range_start = 0;
291 off_t start_off = ftell(stream);
296 while ((ch = *p++) && !bail) {
301 flags = 0; rank =
RANK_INT; width = UINT_MAX;
302 }
else if (isspace(static_cast<unsigned char>(ch))) {
305 if (fgetc(stream) != ch)
313 }
else if (
'0' <= ch && ch <=
'9') {
318 state = ST_MODIFIERS;
324 if (ch >=
'0' && ch <=
'9') {
325 width = width*10+(ch-
'0');
327 state = ST_MODIFIERS;
392 val = ftell(stream) - start_off;
396 q = SkipSpace(stream);
409 *va_arg(ap,
unsigned char *)
410 =
static_cast<unsigned char>(val);
413 *va_arg(ap,
unsigned short *)
414 =
static_cast<unsigned short>(val);
417 *va_arg(ap,
unsigned int *)
418 =
static_cast<unsigned int>(val);
421 *va_arg(ap,
unsigned long *)
422 =
static_cast<unsigned long>(val);
425 *va_arg(ap,
unsigned long long *)
426 =
static_cast<unsigned long long>(val);
430 =
reinterpret_cast<void *
>(
static_cast<uintptr_t
>(val));
441 q = SkipSpace(stream);
449 if (!(flags & FL_SPLAT)) {
451 *va_arg(ap,
float *) =
static_cast<float>(fval);
453 *va_arg(ap,
double *) =
static_cast<double>(fval);
460 width = (flags &
FL_WIDTH) ? width : 1;
461 sarg = va_arg(ap,
char *);
463 if ((q = fgetc(stream)) <= 0) {
467 if (!(flags & FL_SPLAT)) {
477 sp = sarg = va_arg(ap,
char *);
480 if (isspace(static_cast<unsigned char>(q)) || q <= 0) {
484 if (!(flags & FL_SPLAT)) *sp = q;
489 }
else if (!(flags & FL_SPLAT)) {
498 sarg = va_arg(ap,
char *);
499 state = ST_MATCH_INIT;
501 memset(matchmap, 0,
sizeof matchmap);
505 if (fgetc(stream) !=
'%' )
517 if (ch ==
'^' && !(flags &
FL_INV)) {
520 SetBit(matchmap, static_cast<unsigned char>(ch));
528 }
else if (ch ==
'-') {
529 range_start =
static_cast<unsigned char>(ch);
530 state = ST_MATCH_RANGE;
532 SetBit(matchmap, static_cast<unsigned char>(ch));
538 SetBit(matchmap, static_cast<unsigned char>(
'-'));
542 for (i = range_start ; i < (static_cast<unsigned char>(ch)) ; i++)
552 unsigned char qc =
static_cast<unsigned char>(q);
553 if (q <= 0 || !(TestBit(matchmap, qc)^matchinv)) {
557 if (!(flags & FL_SPLAT)) *sarg = q;
562 }
else if (!(flags & FL_SPLAT)) {
577 int creat(
const char *pathname, mode_t
mode) {
578 return open(pathname, O_CREAT | O_TRUNC | O_WRONLY, mode);
double strtofloat(const char *s)
int tfscanf(FILE *stream, const char *format,...)
double streamtofloat(FILE *s)
uintmax_t streamtoumax(FILE *s, int base)