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++) {
115 while (iswspace(p[1])) p++;
116 while (iswspace((c=getwc(f)))) pos++;
120 if (*p != '%' || p[1] == '%') {
125 if (c<0) goto input_fail;
135 } else if (iswdigit(*p) && p[1]=='$') {
136 dest = arg_n(ap, *p-'0'); p+=2;
138 dest = va_arg(ap, void *);
141 for (width=0; iswdigit(*p); p++) {
142 width = 10*width + *p - '0';
155 if (*p == 'h') p++, size = SIZE_hh;
159 if (*p == 'l') p++, size = SIZE_ll;
172 case 'd': case 'i': case 'o': case 'u': case 'x':
173 case 'a': case 'e': case 'f': case 'g':
174 case 'A': case 'E': case 'F': case 'G': case 'X':
175 case 's': case 'c': case '[':
186 /* Transform S,C -> ls,lc */
193 if (t != '[' && (t|32) != 'c')
194 while (iswspace((c=getwc(f)))) pos++;
197 if (c < 0) goto input_fail;
203 store_int(dest, size, pos);
204 /* do not increment match count, etc! */
211 if (width<1) width = 1;
214 } else if (t == 's') {
216 set = (const wchar_t[]){
217 ' ', '\t', '\n', '\r', 11, 12, 0x0085,
218 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005,
219 0x2006, 0x2008, 0x2009, 0x200a,
220 0x2028, 0x2029, 0x205f, 0x3000, 0 };
222 if (*++p == '^') p++, invert = 1;
227 if (!*p) goto fmt_fail;
232 s = (size == SIZE_def) ? dest : 0;
233 wcs = (size == SIZE_l) ? dest : 0;
237 if (width < 1) width = -1;
241 k = t=='c' ? width+1U : 31;
242 if (size == SIZE_l) {
243 wcs = malloc(k*sizeof(wchar_t));
244 if (!wcs) goto alloc_fail;
247 if (!s) goto alloc_fail;
251 if ((c=getwc(f))<0) break;
252 if (in_set(set, c) == invert)
258 wchar_t *tmp = realloc(wcs, k*sizeof(wchar_t));
259 if (!tmp) goto alloc_fail;
262 } else if (size != SIZE_l) {
263 int l = wctomb(s?s+i:tmp, c);
264 if (l<0) goto input_fail;
266 if (alloc && i > k-4) {
268 char *tmp = realloc(s, k);
269 if (!tmp) goto alloc_fail;
279 if (t == 'c' || !gotmatch) goto match_fail;
283 if (size == SIZE_l) *(wchar_t **)dest = wcs;
284 else *(char **)dest = s;
292 case 'd': case 'i': case 'o': case 'u': case 'x':
293 case 'a': case 'e': case 'f': case 'g':
294 case 'A': case 'E': case 'F': case 'G': case 'X':
296 if (width < 1) width = 0;
297 snprintf(tmp, sizeof tmp, "%.*s%.0d%s%c%%lln",
298 1+!dest, "%*", width, size_pfx[size+2], t);
300 if (fscanf(f, tmp, dest?dest:&cnt, &cnt) == -1)
316 if (!matches) matches--;
327 weak_alias(vfwscanf,__isoc99_vfwscanf);