From 2162541f38d3f642f5a643010548d62220d55a4d Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Tue, 10 Apr 2012 21:47:37 -0400 Subject: add "scan helper getc" and rework strtod, etc. to use it the immediate benefit is a significant debloating of the float parsing code by moving the responsibility for keeping track of the number of characters read to a different module. by linking shgetc with the stdio buffer logic, counting logic is defered to buffer refill time, keeping the calls to shgetc fast and light. in the future, shgetc will also be useful for integrating the new float code with scanf, which needs to not only count the characters consumed, but also limit the number of characters read based on field width specifiers. shgetc may also become a useful tool for simplifying the integer parsing code. --- src/internal/shgetc.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/internal/shgetc.c (limited to 'src/internal/shgetc.c') diff --git a/src/internal/shgetc.c b/src/internal/shgetc.c new file mode 100644 index 00000000..7c4e58c1 --- /dev/null +++ b/src/internal/shgetc.c @@ -0,0 +1,24 @@ +#include "shgetc.h" + +void __shlim(FILE *f, off_t lim) +{ + f->shlim = lim; + f->shcnt = f->rend ? f->rend - f->buf : 0; + if (lim && f->rend - f->rpos > lim) + f->shend = f->rpos + lim; + else + f->shend = f->rend; +} + +int __shgetc(FILE *f) +{ + int c; + if (f->shcnt >= f->shlim) return EOF; + c = __uflow(f); + if (f->shlim && f->rend - f->rpos > f->shlim - f->shcnt - 1) + f->shend = f->rpos + (f->shlim - f->shcnt - 1); + else + f->shend = f->rend; + if (f->rend) f->shcnt += f->rend - f->buf; + return c; +} -- cgit v1.2.1