xref: /third_party/musl/src/internal/shgetc.h (revision 570af302)
1570af302Sopenharmony_ci#include "stdio_impl.h"
2570af302Sopenharmony_ci
3570af302Sopenharmony_ci/* Scan helper "stdio" functions for use by scanf-family and strto*-family
4570af302Sopenharmony_ci * functions. These accept either a valid stdio FILE, or a minimal pseudo
5570af302Sopenharmony_ci * FILE whose buffer pointers point into a null-terminated string. In the
6570af302Sopenharmony_ci * latter case, the sh_fromstring macro should be used to setup the FILE;
7570af302Sopenharmony_ci * the rest of the structure can be left uninitialized.
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * To begin using these functions, shlim must first be called on the FILE
10570af302Sopenharmony_ci * to set a field width limit, or 0 for no limit. For string pseudo-FILEs,
11570af302Sopenharmony_ci * a nonzero limit is not valid and produces undefined behavior. After that,
12570af302Sopenharmony_ci * shgetc, shunget, and shcnt are valid as long as no other stdio functions
13570af302Sopenharmony_ci * are called on the stream.
14570af302Sopenharmony_ci *
15570af302Sopenharmony_ci * When used with a real FILE object, shunget has only one byte of pushback
16570af302Sopenharmony_ci * available. Further shunget (up to a limit of the stdio UNGET buffer size)
17570af302Sopenharmony_ci * will adjust the position but will not restore the data to be read again.
18570af302Sopenharmony_ci * This functionality is needed for the wcsto*-family functions, where it's
19570af302Sopenharmony_ci * okay because the FILE will be discarded immediately anyway. When used
20570af302Sopenharmony_ci * with string pseudo-FILEs, shunget has unlimited pushback, back to the
21570af302Sopenharmony_ci * beginning of the string. */
22570af302Sopenharmony_ci
23570af302Sopenharmony_cihidden void __shlim(FILE *, off_t);
24570af302Sopenharmony_cihidden int __shgetc(FILE *);
25570af302Sopenharmony_ci
26570af302Sopenharmony_ci#define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf))
27570af302Sopenharmony_ci#define shlim(f, lim) __shlim((f), (lim))
28570af302Sopenharmony_ci#define shgetc(f) (((f)->rpos != (f)->shend) ? *(f)->rpos++ : __shgetc(f))
29570af302Sopenharmony_ci#define shunget(f) ((f)->shlim>=0 ? (void)(f)->rpos-- : (void)0)
30570af302Sopenharmony_ci
31570af302Sopenharmony_ci#define sh_fromstring(f, s) \
32570af302Sopenharmony_ci	((f)->buf = (f)->rpos = (void *)(s), (f)->rend = (void*)-1)
33