33 #include <sys/types.h> 71 return CHAR_BIT *
sizeof(long);
77 while (isspace(p = fgetc(s)));
83 SetBit(
unsigned long *bitmap,
unsigned int bit) {
88 TestBit(
unsigned long *bitmap,
unsigned int bit) {
89 return static_cast<int>(bitmap[bit/
LongBit()] >> (bit%
LongBit())) & 1;
92 static inline int DigitValue(
int ch,
int base) {
93 if (ch >=
'0' && ch <=
'9') {
94 if (base >= 10 || ch <=
'7')
96 }
else if (ch >=
'A' && ch <=
'Z' && base == 16) {
98 }
else if (ch >=
'a' && ch <=
'z' && base == 16) {
111 isspace(static_cast<unsigned char>(c)) && (c != EOF);
115 if (c ==
'-' || c ==
'+') {
124 if (c ==
'x' || c ==
'X') {
131 }
else if (base == 16) {
134 if (c ==
'x' || c ==
'X') c = fgetc(s);
139 for (; (c != EOF) && (d = DigitValue(c, base)) >= 0; c = fgetc(s))
143 return minus ? -v : v;
154 isspace(static_cast<unsigned char>(c)) && (c != EOF);
158 if (c ==
'-' || c ==
'+') {
164 for (; c != EOF && (d = DigitValue(c, 10)) >= 0; c = fgetc(s))
167 for (c = fgetc(s); c != EOF && (d = DigitValue(c, 10)) >= 0; c = fgetc(s)) {
172 double f =
static_cast<double>(v)
173 + static_cast<double>(w) /
static_cast<double>(k);
174 if (c ==
'e' || c ==
'E') {
177 if (c ==
'-' || c ==
'+') {
178 expsign = (c ==
'-') ? -1 : 1;
182 for (; (c != EOF) && (d = DigitValue(c, 10)) >= 0; c = fgetc(s)) {
183 exponent = exponent * 10 + d;
186 f *= pow(10.0, static_cast<double>(exponent));
190 return minus ? -f : f;
200 while(*s && isspace(static_cast<unsigned char>(*s))) s++;
203 if (*s ==
'-' || *s ==
'+') {
209 for (; *s && (d = DigitValue(*s, 10)) >= 0; s++)
212 for (++s; *s && (d = DigitValue(*s, 10)) >= 0; s++) {
217 if (*s ==
'e' || *s ==
'E')
218 tprintf(
"WARNING: Scientific Notation not supported!");
220 double f =
static_cast<double>(v)
221 + static_cast<double>(w) /
static_cast<double>(k);
223 return minus ? -f : f;
226 static int tvfscanf(FILE* stream,
const char *format, va_list ap);
228 int tfscanf(FILE* stream,
const char *format, ...) {
232 va_start(ap, format);
233 rv = tvfscanf(stream, format, ap);
241 int fscanf(FILE* stream,
const char *format, ...) {
245 va_start(ap, format);
246 rv = tvfscanf(stream, format, ap);
252 int vfscanf(FILE* stream,
const char *format, ...) {
256 va_start(ap, format);
257 rv = tvfscanf(stream, format, ap);
264 static int tvfscanf(FILE* stream,
const char *format, va_list ap) {
265 const char *p = format;
270 unsigned int width = UINT_MAX;
286 unsigned long matchmap[((1 << CHAR_BIT)+(CHAR_BIT *
sizeof(
long) - 1)) /
287 (CHAR_BIT *
sizeof(long))];
289 unsigned char range_start = 0;
290 off_t start_off = ftell(stream);
295 while ((ch = *p++) && !bail) {
300 flags = 0; rank =
RANK_INT; width = UINT_MAX;
301 }
else if (isspace(static_cast<unsigned char>(ch))) {
304 if (fgetc(stream) != ch)
312 }
else if (
'0' <= ch && ch <=
'9') {
317 state = ST_MODIFIERS;
323 if (ch >=
'0' && ch <=
'9') {
324 width = width*10+(ch-
'0');
326 state = ST_MODIFIERS;
391 val = ftell(stream) - start_off;
395 q = SkipSpace(stream);
408 *va_arg(ap,
unsigned char *)
409 =
static_cast<unsigned char>(val);
412 *va_arg(ap,
unsigned short *)
413 =
static_cast<unsigned short>(val);
416 *va_arg(ap,
unsigned int *)
417 =
static_cast<unsigned int>(val);
420 *va_arg(ap,
unsigned long *)
421 =
static_cast<unsigned long>(val);
424 *va_arg(ap,
unsigned long long *)
425 =
static_cast<unsigned long long>(val);
429 =
reinterpret_cast<void *
>(
static_cast<uintptr_t
>(val));
440 q = SkipSpace(stream);
450 *va_arg(ap,
float *) =
static_cast<float>(fval);
452 *va_arg(ap,
double *) =
static_cast<double>(fval);
459 width = (flags &
FL_WIDTH) ? width : 1;
460 sarg = va_arg(ap,
char *);
462 if ((q = fgetc(stream)) <= 0) {
476 sp = sarg = va_arg(ap,
char *);
479 if (isspace(static_cast<unsigned char>(q)) || q <= 0) {
497 sarg = va_arg(ap,
char *);
498 state = ST_MATCH_INIT;
500 memset(matchmap, 0,
sizeof matchmap);
504 if (fgetc(stream) !=
'%' )
516 if (ch ==
'^' && !(flags &
FL_INV)) {
519 SetBit(matchmap, static_cast<unsigned char>(ch));
527 }
else if (ch ==
'-') {
528 range_start =
static_cast<unsigned char>(ch);
529 state = ST_MATCH_RANGE;
531 SetBit(matchmap, static_cast<unsigned char>(ch));
537 SetBit(matchmap, static_cast<unsigned char>(
'-'));
541 for (i = range_start ; i < (static_cast<unsigned char>(ch)) ; i++)
551 unsigned char qc =
static_cast<unsigned char>(q);
552 if (q <= 0 || !(TestBit(matchmap, qc)^matchinv)) {
576 int creat(
const char *pathname, mode_t
mode) {
577 return open(pathname, O_CREAT | O_TRUNC | O_WRONLY,
mode);
double strtofloat(const char *s)
int tfscanf(FILE *stream, const char *format,...)
uintmax_t streamtoumax(FILE *s, int base)
double streamtofloat(FILE *s)