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];
109 for (p=fmt; *p; p++) {
114 while (iswspace(p[1])) p++;
115 while (iswspace((c=getwc(f)))) pos++;
119 if (*p != '%' || p[1] == '%') {
122 while (iswspace((c=getwc(f)))) pos++;
128 if (c<0) goto input_fail;
138 } else if (iswdigit(*p) && p[1]=='$') {
139 dest = arg_n(ap, *p-'0'); p+=2;
141 dest = va_arg(ap, void *);
144 for (width=0; iswdigit(*p); p++) {
145 width = 10*width + *p - '0';
160 if (*p == 'h') p++, size = SIZE_hh;
164 if (*p == 'l') p++, size = SIZE_ll;
177 case 'd': case 'i': case 'o': case 'u': case 'x':
178 case 'a': case 'e': case 'f': case 'g':
179 case 'A': case 'E': case 'F': case 'G': case 'X':
180 case 's': case 'c': case '[':
191 /* Transform S,C -> ls,lc */
198 if (t != '[' && (t|32) != 'c')
199 while (iswspace((c=getwc(f)))) pos++;
202 if (c < 0) goto input_fail;
208 store_int(dest, size, pos);
209 /* do not increment match count, etc! */
216 if (width<1) width = 1;
219 } else if (t == 's') {
221 static const wchar_t spaces[] = {
222 ' ', '\t', '\n', '\r', 11, 12, 0x0085,
223 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005,
224 0x2006, 0x2008, 0x2009, 0x200a,
225 0x2028, 0x2029, 0x205f, 0x3000, 0 };
228 if (*++p == '^') p++, invert = 1;
233 if (!*p) goto fmt_fail;
238 s = (size == SIZE_def) ? dest : 0;
239 wcs = (size == SIZE_l) ? dest : 0;
243 if (width < 1) width = -1;
247 k = t=='c' ? width+1U : 31;
248 if (size == SIZE_l) {
249 wcs = malloc(k*sizeof(wchar_t));
250 if (!wcs) goto alloc_fail;
253 if (!s) goto alloc_fail;
257 if ((c=getwc(f))<0) break;
258 if (in_set(set, c) == invert)
264 wchar_t *tmp = realloc(wcs, k*sizeof(wchar_t));
265 if (!tmp) goto alloc_fail;
268 } else if (size != SIZE_l) {
269 int l = wctomb(s?s+i:tmp, c);
270 if (l<0) goto input_fail;
272 if (alloc && i > k-4) {
274 char *tmp = realloc(s, k);
275 if (!tmp) goto alloc_fail;
285 if (t == 'c' || !gotmatch) goto match_fail;
289 if (size == SIZE_l) *(wchar_t **)dest = wcs;
290 else *(char **)dest = s;
298 case 'd': case 'i': case 'o': case 'u': case 'x':
299 case 'a': case 'e': case 'f': case 'g':
300 case 'A': case 'E': case 'F': case 'G': case 'X':
302 if (width < 1) width = 0;
303 snprintf(tmp, sizeof tmp, "%.*s%.0d%s%c%%lln",
304 1+!dest, "%*", width, size_pfx[size+2], t);
306 if (fscanf(f, tmp, dest?dest:&cnt, &cnt) == -1)
322 if (!matches) matches--;
333 weak_alias(vfwscanf,__isoc99_vfwscanf);