13 #include "stdio_impl.h"
16 #include "floatscan.h"
26 static void store_int(void *dest, int size, unsigned long long i)
43 *(long long *)dest = i;
48 static void *arg_n(va_list ap, unsigned int n)
54 for (i=n; i>1; i--) va_arg(ap2, void *);
55 p = va_arg(ap2, void *);
60 static int in_set(const wchar_t *set, int c)
63 const wchar_t *p = set;
67 } else if (*p == ']') {
71 for (; *p && *p != ']'; p++) {
72 if (*p=='-' && p[1] && p[1] != ']')
73 for (j=p++[-1]; j<*p; j++)
83 ((f)->rpos < (f)->rend && *(f)->rpos < 128 ? *(f)->rpos++ : (getwc)(f))
86 #define ungetwc(c,f) \
87 ((f)->rend && (c)<128U ? *--(f)->rpos : ungetwc((c),(f)))
90 int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
103 static const char size_pfx[][3] = { "hh", "h", "", "l", "L", "ll" };
104 char tmp[3*sizeof(int)+10];
110 for (p=fmt; *p; p++) {
113 while (iswspace(p[1])) p++;
114 while (iswspace((c=getwc(f)))) pos++;
118 if (*p != '%' || p[1] == '%') {
123 if (c<0) goto input_fail;
133 } else if (iswdigit(*p) && p[1]=='$') {
134 dest = arg_n(ap, *p-'0'); p+=2;
136 dest = va_arg(ap, void *);
139 for (width=0; iswdigit(*p); p++) {
140 width = 10*width + *p - '0';
153 if (*p == 'h') p++, size = SIZE_hh;
157 if (*p == 'l') p++, size = SIZE_ll;
170 case 'd': case 'i': case 'o': case 'u': case 'x':
171 case 'a': case 'e': case 'f': case 'g':
172 case 'A': case 'E': case 'F': case 'G': case 'X':
173 case 's': case 'c': case '[':
184 /* Transform S,C -> ls,lc */
191 if (t != '[' && (t|32) != 'c')
192 while (iswspace((c=getwc(f)))) pos++;
195 if (c < 0) goto input_fail;
201 store_int(dest, size, pos);
202 /* do not increment match count, etc! */
209 if (width<1) width = 1;
212 } else if (t == 's') {
214 set = (const wchar_t[]){
215 ' ', '\t', '\n', '\r', 11, 12, 0x0085,
216 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005,
217 0x2006, 0x2008, 0x2009, 0x200a,
218 0x2028, 0x2029, 0x205f, 0x3000, 0 };
220 if (*++p == '^') p++, invert = 1;
225 if (!*p) goto fmt_fail;
230 s = (size == SIZE_def) ? dest : 0;
231 wcs = (size == SIZE_l) ? dest : 0;
235 if (width < 1) width = -1;
239 k = t=='c' ? width+1U : 31;
240 if (size == SIZE_l) {
241 wcs = malloc(k*sizeof(wchar_t));
242 if (!wcs) goto alloc_fail;
245 if (!s) goto alloc_fail;
249 if ((c=getwc(f))<0) break;
250 if (in_set(set, c) == invert)
256 wchar_t *tmp = realloc(wcs, k*sizeof(wchar_t));
257 if (!tmp) goto alloc_fail;
260 } else if (size != SIZE_l) {
261 int l = wctomb(s?s+i:tmp, c);
262 if (l<0) goto input_fail;
264 if (alloc && i > k-4) {
266 char *tmp = realloc(s, k);
267 if (!tmp) goto alloc_fail;
277 if (t == 'c' || !gotmatch) goto match_fail;
281 if (size == SIZE_l) *(wchar_t **)dest = wcs;
282 else *(char **)dest = s;
290 case 'd': case 'i': case 'o': case 'u': case 'x':
291 case 'a': case 'e': case 'f': case 'g':
292 case 'A': case 'E': case 'F': case 'G': case 'X':
294 if (width < 1) width = 0;
295 snprintf(tmp, sizeof tmp, "%.*s%.0d%s%c%%lln",
296 1+!dest, "%*", width, size_pfx[size+2], t);
298 if (fscanf(f, tmp, dest?dest:&cnt, &cnt) == -1)
314 if (!matches) matches--;
325 weak_alias(vfwscanf,__isoc99_vfwscanf);