13 #include "stdio_impl.h"
16 #include "floatscan.h"
25 static void store_int(void *dest, int size, unsigned long long i)
42 *(long long *)dest = i;
47 static void *arg_n(va_list ap, unsigned int n)
53 for (i=n; i>1; i--) va_arg(ap2, void *);
54 p = va_arg(ap2, void *);
59 static int in_set(const wchar_t *set, int c)
62 const wchar_t *p = set;
66 } else if (*p == ']') {
70 for (; *p && *p != ']'; p++) {
71 if (*p=='-' && p[1] && p[1] != ']')
72 for (j=p++[-1]; j<*p; j++)
82 ((f)->rpos < (f)->rend && *(f)->rpos < 128 ? *(f)->rpos++ : (getwc)(f))
85 #define ungetwc(c,f) \
86 ((f)->rend && (c)<128U ? *--(f)->rpos : ungetwc((c),(f)))
89 int vfwscanf(FILE *f, const wchar_t *fmt, va_list ap)
102 static const char size_pfx[][3] = { "hh", "h", "", "l", "L", "ll" };
103 char tmp[3*sizeof(int)+10];
107 for (p=fmt; *p; p++) {
110 while (iswspace(p[1])) p++;
111 while (iswspace((c=getwc(f)))) pos++;
115 if (*p != '%' || p[1] == '%') {
120 if (c<0) goto input_fail;
130 } else if (iswdigit(*p) && p[1]=='$') {
131 dest = arg_n(ap, *p-'0'); p+=2;
133 dest = va_arg(ap, void *);
136 for (width=0; iswdigit(*p); p++) {
137 width = 10*width + *p - '0';
150 if (*p == 'h') p++, size = SIZE_hh;
154 if (*p == 'l') p++, size = SIZE_ll;
167 case 'd': case 'i': case 'o': case 'u': case 'x':
168 case 'a': case 'e': case 'f': case 'g':
169 case 'A': case 'E': case 'F': case 'G': case 'X':
170 case 's': case 'c': case '[':
181 /* Transform ls,lc -> S,C */
182 if (size==SIZE_l && (t&15)==3) t&=~32;
184 if (t != 'n' && t != '[' && (t|32) != 'c') {
185 while (iswspace((c=getwc(f)))) pos++;
186 if (c < 0) goto input_fail;
192 store_int(dest, size, pos);
193 /* do not increment match count, etc! */
197 if (width < 1) width = 1;
199 for (; width && (c=getwc(f)) >= 0; width--) {
200 int l = wctomb(s?s:tmp, c);
201 if (l<0) goto input_fail;
205 if (width) goto match_fail;
209 if (width < 1) width = 1;
211 for (; width && (c=getwc(f)) >= 0; width--)
212 pos++, wcs && (*wcs++ = c);
213 if (width) goto match_fail;
217 if (width < 1) width = -1;
219 while (width && !iswspace(c=getwc(f)) && c!=EOF) {
220 int l = wctomb(s?s:tmp, c);
221 if (l<0) goto input_fail;
226 if (width) ungetwc(c, f);
232 if (width < 1) width = -1;
233 while (width && !iswspace(c=getwc(f)) && c!=EOF)
234 width-=(width>0), pos++, *wcs++ = c;
235 if (width) ungetwc(c, f);
240 s = (size == SIZE_def) ? dest : 0;
241 wcs = (size == SIZE_l) ? dest : 0;
243 if (*++p == '^') p++, invert = 1;
248 if (width < 1) width = -1;
251 if ((c=getwc(f))<0) break;
252 if (in_set(p, c) == invert)
256 } else if (size != SIZE_l) {
257 int l = wctomb(s?s:tmp, c);
258 if (l<0) goto input_fail;
265 if (width) ungetwc(c, f);
268 if (c>=0) goto match_fail;
269 else goto input_fail;
273 if (!*p) goto fmt_fail;
281 case 'd': case 'i': case 'o': case 'u': case 'x':
282 case 'a': case 'e': case 'f': case 'g':
283 case 'A': case 'E': case 'F': case 'G': case 'X':
285 if (width < 1) width = 0;
286 snprintf(tmp, sizeof tmp, "%.*s%.0d%s%c%%lln",
287 1+!dest, "%*", width, size_pfx[size+2], t);
289 if (fscanf(f, tmp, dest?dest:&cnt, &cnt) == -1)
304 if (!matches) matches--;