10 #include "stdio_impl.h"
13 #include "floatscan.h"
23 static void store_int(void *dest, int size, unsigned long long i)
40 *(long long *)dest = i;
45 static void *arg_n(va_list ap, unsigned int n)
51 for (i=n; i>1; i--) va_arg(ap2, void *);
52 p = va_arg(ap2, void *);
57 static int in_set(const wchar_t *set, int c)
60 const wchar_t *p = set;
64 } else if (*p == ']') {
68 for (; *p && *p != ']'; p++) {
69 if (*p=='-' && p[1] && p[1] != ']')
70 for (j=p++[-1]; j<*p; j++)
80 ((f)->rpos < (f)->rend && *(f)->rpos < 128 ? *(f)->rpos++ : (getwc)(f))
83 #define ungetwc(c,f) \
84 ((f)->rend && (c)<128U ? *--(f)->rpos : ungetwc((c),(f)))
87 int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
100 static const char size_pfx[][3] = { "hh", "h", "", "l", "L", "ll" };
101 char tmp[3*sizeof(int)+10];
107 for (p=fmt; *p; p++) {
112 while (iswspace(p[1])) p++;
113 while (iswspace((c=getwc(f)))) pos++;
117 if (*p != '%' || p[1] == '%') {
122 if (c<0) goto input_fail;
132 } else if (iswdigit(*p) && p[1]=='$') {
133 dest = arg_n(ap, *p-'0'); p+=2;
135 dest = va_arg(ap, void *);
138 for (width=0; iswdigit(*p); p++) {
139 width = 10*width + *p - '0';
154 if (*p == 'h') p++, size = SIZE_hh;
158 if (*p == 'l') p++, size = SIZE_ll;
171 case 'd': case 'i': case 'o': case 'u': case 'x':
172 case 'a': case 'e': case 'f': case 'g':
173 case 'A': case 'E': case 'F': case 'G': case 'X':
174 case 's': case 'c': case '[':
185 /* Transform S,C -> ls,lc */
192 if (t != '[' && (t|32) != 'c')
193 while (iswspace((c=getwc(f)))) pos++;
196 if (c < 0) goto input_fail;
202 store_int(dest, size, pos);
203 /* do not increment match count, etc! */
210 if (width<1) width = 1;
213 } else if (t == 's') {
215 set = (const wchar_t[]){
216 ' ', '\t', '\n', '\r', 11, 12, 0x0085,
217 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005,
218 0x2006, 0x2008, 0x2009, 0x200a,
219 0x2028, 0x2029, 0x205f, 0x3000, 0 };
221 if (*++p == '^') p++, invert = 1;
226 if (!*p) goto fmt_fail;
231 s = (size == SIZE_def) ? dest : 0;
232 wcs = (size == SIZE_l) ? dest : 0;
236 if (width < 1) width = -1;
240 k = t=='c' ? width+1U : 31;
241 if (size == SIZE_l) {
242 wcs = malloc(k*sizeof(wchar_t));
243 if (!wcs) goto alloc_fail;
246 if (!s) goto alloc_fail;
250 if ((c=getwc(f))<0) break;
251 if (in_set(set, c) == invert)
257 wchar_t *tmp = realloc(wcs, k*sizeof(wchar_t));
258 if (!tmp) goto alloc_fail;
261 } else if (size != SIZE_l) {
262 int l = wctomb(s?s+i:tmp, c);
263 if (l<0) goto input_fail;
265 if (alloc && i > k-4) {
267 char *tmp = realloc(s, k);
268 if (!tmp) goto alloc_fail;
278 if (t == 'c' || !gotmatch) goto match_fail;
282 if (size == SIZE_l) *(wchar_t **)dest = wcs;
283 else *(char **)dest = s;
291 case 'd': case 'i': case 'o': case 'u': case 'x':
292 case 'a': case 'e': case 'f': case 'g':
293 case 'A': case 'E': case 'F': case 'G': case 'X':
295 if (width < 1) width = 0;
296 snprintf(tmp, sizeof tmp, "%.*s%.0d%s%c%%lln",
297 1+!dest, "%*", width, size_pfx[size+2], t);
299 if (fscanf(f, tmp, dest?dest:&cnt, &cnt) == -1)
315 if (!matches) matches--;
326 weak_alias(vfwscanf,__isoc99_vfwscanf);