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] == '%') {
124 if (c<0) goto input_fail;
134 } else if (iswdigit(*p) && p[1]=='$') {
135 dest = arg_n(ap, *p-'0'); p+=2;
137 dest = va_arg(ap, void *);
140 for (width=0; iswdigit(*p); p++) {
141 width = 10*width + *p - '0';
156 if (*p == 'h') p++, size = SIZE_hh;
160 if (*p == 'l') p++, size = SIZE_ll;
173 case 'd': case 'i': case 'o': case 'u': case 'x':
174 case 'a': case 'e': case 'f': case 'g':
175 case 'A': case 'E': case 'F': case 'G': case 'X':
176 case 's': case 'c': case '[':
187 /* Transform S,C -> ls,lc */
194 if (t != '[' && (t|32) != 'c')
195 while (iswspace((c=getwc(f)))) pos++;
198 if (c < 0) goto input_fail;
204 store_int(dest, size, pos);
205 /* do not increment match count, etc! */
212 if (width<1) width = 1;
215 } else if (t == 's') {
217 set = (const wchar_t[]){
218 ' ', '\t', '\n', '\r', 11, 12, 0x0085,
219 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005,
220 0x2006, 0x2008, 0x2009, 0x200a,
221 0x2028, 0x2029, 0x205f, 0x3000, 0 };
223 if (*++p == '^') p++, invert = 1;
228 if (!*p) goto fmt_fail;
233 s = (size == SIZE_def) ? dest : 0;
234 wcs = (size == SIZE_l) ? dest : 0;
238 if (width < 1) width = -1;
242 k = t=='c' ? width+1U : 31;
243 if (size == SIZE_l) {
244 wcs = malloc(k*sizeof(wchar_t));
245 if (!wcs) goto alloc_fail;
248 if (!s) goto alloc_fail;
252 if ((c=getwc(f))<0) break;
253 if (in_set(set, c) == invert)
259 wchar_t *tmp = realloc(wcs, k*sizeof(wchar_t));
260 if (!tmp) goto alloc_fail;
263 } else if (size != SIZE_l) {
264 int l = wctomb(s?s+i:tmp, c);
265 if (l<0) goto input_fail;
267 if (alloc && i > k-4) {
269 char *tmp = realloc(s, k);
270 if (!tmp) goto alloc_fail;
280 if (t == 'c' || !gotmatch) goto match_fail;
284 if (size == SIZE_l) *(wchar_t **)dest = wcs;
285 else *(char **)dest = s;
293 case 'd': case 'i': case 'o': case 'u': case 'x':
294 case 'a': case 'e': case 'f': case 'g':
295 case 'A': case 'E': case 'F': case 'G': case 'X':
297 if (width < 1) width = 0;
298 snprintf(tmp, sizeof tmp, "%.*s%.0d%s%c%%lln",
299 1+!dest, "%*", width, size_pfx[size+2], t);
301 if (fscanf(f, tmp, dest?dest:&cnt, &cnt) == -1)
317 if (!matches) matches--;
328 weak_alias(vfwscanf,__isoc99_vfwscanf);