X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Finternal%2Fshgetc.h;h=9435381a61d1ee45c373c87bf7bcb5b43b370676;hb=fa4a8abd06a401822cc8ba4e352a219544c0118d;hp=3434cdaa132e197c36753c9efea8907066345e5f;hpb=2162541f38d3f642f5a643010548d62220d55a4d;p=musl diff --git a/src/internal/shgetc.h b/src/internal/shgetc.h index 3434cdaa..9435381a 100644 --- a/src/internal/shgetc.h +++ b/src/internal/shgetc.h @@ -1,25 +1,32 @@ #include "stdio_impl.h" -void __shlim(FILE *, off_t); -int __shgetc(FILE *); +/* Scan helper "stdio" functions for use by scanf-family and strto*-family + * functions. These accept either a valid stdio FILE, or a minimal pseudo + * FILE whose buffer pointers point into a null-terminated string. In the + * latter case, the sh_fromstring macro should be used to setup the FILE; + * the rest of the structure can be left uninitialized. + * + * To begin using these functions, shlim must first be called on the FILE + * to set a field width limit, or 0 for no limit. For string pseudo-FILEs, + * a nonzero limit is not valid and produces undefined behavior. After that, + * shgetc, shunget, and shcnt are valid as long as no other stdio functions + * are called on the stream. + * + * When used with a real FILE object, shunget has only one byte of pushback + * available. Further shunget (up to a limit of the stdio UNGET buffer size) + * will adjust the position but will not restore the data to be read again. + * This functionality is needed for the wcsto*-family functions, where it's + * okay because the FILE will be discarded immediately anyway. When used + * with string pseudo-FILEs, shunget has unlimited pushback, back to the + * beginning of the string. */ -static inline off_t shcnt(FILE *f) -{ - return f->shcnt + (f->rpos - f->rend); -} +hidden void __shlim(FILE *, off_t); +hidden int __shgetc(FILE *); -static inline void shlim(FILE *f, off_t lim) -{ - __shlim(f, lim); -} +#define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf)) +#define shlim(f, lim) __shlim((f), (lim)) +#define shgetc(f) (((f)->rpos != (f)->shend) ? *(f)->rpos++ : __shgetc(f)) +#define shunget(f) ((f)->shlim>=0 ? (void)(f)->rpos-- : (void)0) -static inline int shgetc(FILE *f) -{ - if (f->rpos < f->shend) return *f->rpos++; - return __shgetc(f); -} - -static inline void shunget(FILE *f) -{ - if (f->rend) f->rpos--; -} +#define sh_fromstring(f, s) \ + ((f)->buf = (f)->rpos = (void *)(s), (f)->rend = (void*)-1)