msvcrt: Improve multibyte characters support in printf.
[wine.git] / dlls / msvcrt / printf.h
blob392ead8312bc4cd8c06c7a9e984db0875d10c9e3
1 /*
2 * Copyright 2011 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #ifdef PRINTF_WIDE
20 #define APICHAR MSVCRT_wchar_t
21 #define CONVCHAR char
22 #define FUNC_NAME(func) func ## _w
23 #else
24 #define APICHAR char
25 #define CONVCHAR MSVCRT_wchar_t
26 #define FUNC_NAME(func) func ## _a
27 #endif
29 #ifndef signbit
30 #define signbit(x) ((x) < 0)
31 #endif
33 typedef struct FUNC_NAME(pf_flags_t)
35 APICHAR Sign, LeftAlign, Alternate, PadZero;
36 int FieldLength, Precision;
37 APICHAR IntegerLength, IntegerDouble, IntegerNative;
38 APICHAR WideString, NaturalString;
39 APICHAR Format;
40 } FUNC_NAME(pf_flags);
42 struct FUNC_NAME(_str_ctx) {
43 MSVCRT_size_t len;
44 APICHAR *buf;
47 static int FUNC_NAME(puts_clbk_str)(void *ctx, int len, const APICHAR *str)
49 struct FUNC_NAME(_str_ctx) *out = ctx;
51 if(!out->buf)
52 return len;
54 if(out->len < len) {
55 memcpy(out->buf, str, out->len*sizeof(APICHAR));
56 out->buf += out->len;
57 out->len = 0;
58 return -1;
61 memcpy(out->buf, str, len*sizeof(APICHAR));
62 out->buf += len;
63 out->len -= len;
64 return len;
67 static inline const APICHAR* FUNC_NAME(pf_parse_int)(const APICHAR *fmt, int *val)
69 *val = 0;
71 while(isdigit(*fmt)) {
72 *val *= 10;
73 *val += *fmt++ - '0';
76 return fmt;
79 /* pf_fill: takes care of signs, alignment, zero and field padding */
80 static inline int FUNC_NAME(pf_fill)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
81 int len, FUNC_NAME(pf_flags) *flags, BOOL left)
83 int i, r = 0, written;
85 if(flags->Sign && !strchr("diaeEfFgG", flags->Format))
86 flags->Sign = 0;
88 if(left && flags->Sign) {
89 flags->FieldLength--;
90 if(flags->PadZero)
91 r = pf_puts(puts_ctx, 1, &flags->Sign);
93 written = r;
95 if((!left && flags->LeftAlign) || (left && !flags->LeftAlign)) {
96 APICHAR ch;
98 if(left && flags->PadZero)
99 ch = '0';
100 else
101 ch = ' ';
103 for(i=0; i<flags->FieldLength-len && r>=0; i++) {
104 r = pf_puts(puts_ctx, 1, &ch);
105 written += r;
110 if(r>=0 && left && flags->Sign && !flags->PadZero) {
111 r = pf_puts(puts_ctx, 1, &flags->Sign);
112 written += r;
115 return r>=0 ? written : r;
118 static inline int FUNC_NAME(pf_output_wstr)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
119 const MSVCRT_wchar_t *str, int len, MSVCRT_pthreadlocinfo locinfo)
121 #ifdef PRINTF_WIDE
122 return pf_puts(puts_ctx, len, str);
123 #else
124 LPSTR out;
125 BOOL def_char;
126 int len_a = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
127 str, len, NULL, 0, NULL, &def_char);
128 if(def_char)
129 return 0;
131 out = HeapAlloc(GetProcessHeap(), 0, len_a);
132 if(!out)
133 return -1;
135 WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
136 str, len, out, len_a, NULL, NULL);
137 len = pf_puts(puts_ctx, len_a, out);
138 HeapFree(GetProcessHeap(), 0, out);
139 return len;
140 #endif
143 static inline int FUNC_NAME(pf_output_str)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
144 const char *str, int len, MSVCRT_pthreadlocinfo locinfo)
146 #ifdef PRINTF_WIDE
147 LPWSTR out;
148 int len_w = MultiByteToWideChar(locinfo->lc_codepage, 0, str, len, NULL, 0);
150 out = HeapAlloc(GetProcessHeap(), 0, len_w*sizeof(WCHAR));
151 if(!out)
152 return -1;
154 MultiByteToWideChar(locinfo->lc_codepage, 0, str, len, out, len_w);
155 len = pf_puts(puts_ctx, len_w, out);
156 HeapFree(GetProcessHeap(), 0, out);
157 return len;
158 #else
159 return pf_puts(puts_ctx, len, str);
160 #endif
163 static inline int FUNC_NAME(pf_output_format_wstr)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
164 const MSVCRT_wchar_t *str, int len, FUNC_NAME(pf_flags) *flags, MSVCRT_pthreadlocinfo locinfo)
166 int r, ret;
168 if(len < 0)
169 len = strlenW(str);
171 if(flags->Precision>=0 && flags->Precision<len)
172 len = flags->Precision;
174 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
175 ret = r;
176 if(r >= 0) {
177 r = FUNC_NAME(pf_output_wstr)(pf_puts, puts_ctx, str, len, locinfo);
178 ret += r;
180 if(r >= 0) {
181 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
182 ret += r;
185 return r>=0 ? ret : r;
188 static inline int FUNC_NAME(pf_output_format_str)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
189 const char *str, int len, FUNC_NAME(pf_flags) *flags, MSVCRT_pthreadlocinfo locinfo)
191 int r, ret;
193 if(len < 0)
194 len = strlen(str);
196 if(flags->Precision>=0 && flags->Precision<len)
197 len = flags->Precision;
199 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, TRUE);
200 ret = r;
201 if(r >= 0) {
202 r = FUNC_NAME(pf_output_str)(pf_puts, puts_ctx, str, len, locinfo);
203 ret += r;
205 if(r >= 0) {
206 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, flags, FALSE);
207 ret += r;
210 return r>=0 ? ret : r;
213 static inline int FUNC_NAME(pf_handle_string)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx,
214 const void *str, int len, FUNC_NAME(pf_flags) *flags, MSVCRT_pthreadlocinfo locinfo, BOOL legacy_wide)
216 BOOL api_is_wide = sizeof(APICHAR) == sizeof(MSVCRT_wchar_t);
217 BOOL complement_is_narrow = legacy_wide ? api_is_wide : FALSE;
218 #ifdef PRINTF_WIDE
219 static const MSVCRT_wchar_t nullW[] = {'(','n','u','l','l',')',0};
221 if(!str)
222 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, nullW, 6, flags, locinfo);
223 #else
224 if(!str)
225 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, "(null)", 6, flags, locinfo);
226 #endif
228 if((flags->NaturalString && api_is_wide) || flags->WideString || flags->IntegerLength=='l')
229 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, str, len, flags, locinfo);
230 if((flags->NaturalString && !api_is_wide) || flags->IntegerLength == 'h')
231 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, len, flags, locinfo);
233 if((flags->Format=='S' || flags->Format=='C') == complement_is_narrow)
234 return FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, str, len, flags, locinfo);
235 else
236 return FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, str, len, flags, locinfo);
239 static inline void FUNC_NAME(pf_rebuild_format_string)(char *p, FUNC_NAME(pf_flags) *flags)
241 *p++ = '%';
242 if(flags->Alternate)
243 *p++ = flags->Alternate;
244 if(flags->Precision >= 0) {
245 sprintf(p, ".%d", flags->Precision);
246 p += strlen(p);
248 *p++ = flags->Format;
249 *p++ = 0;
252 /* pf_integer_conv: prints x to buf, including alternate formats and
253 additional precision digits, but not field characters or the sign */
254 static inline void FUNC_NAME(pf_integer_conv)(APICHAR *buf, int buf_len,
255 FUNC_NAME(pf_flags) *flags, LONGLONG x)
257 unsigned int base;
258 const char *digits;
259 int i, j, k;
261 if(flags->Format == 'o')
262 base = 8;
263 else if(flags->Format=='x' || flags->Format=='X')
264 base = 16;
265 else
266 base = 10;
268 if(flags->Format == 'X')
269 digits = "0123456789ABCDEFX";
270 else
271 digits = "0123456789abcdefx";
273 if(x<0 && (flags->Format=='d' || flags->Format=='i')) {
274 x = -x;
275 flags->Sign = '-';
278 i = 0;
279 if(x == 0) {
280 flags->Alternate = 0;
281 if(flags->Precision)
282 buf[i++] = '0';
283 } else {
284 while(x != 0) {
285 j = (ULONGLONG)x%base;
286 x = (ULONGLONG)x/base;
287 buf[i++] = digits[j];
290 k = flags->Precision-i;
291 while(k-- > 0)
292 buf[i++] = '0';
293 if(flags->Alternate) {
294 if(base == 16) {
295 buf[i++] = digits[16];
296 buf[i++] = '0';
297 } else if(base==8 && buf[i-1]!='0')
298 buf[i++] = '0';
301 /* Adjust precision so pf_fill won't truncate the number later */
302 flags->Precision = i;
304 buf[i] = '\0';
305 j = 0;
306 while(--i > j) {
307 APICHAR tmp = buf[j];
308 buf[j] = buf[i];
309 buf[i] = tmp;
310 j++;
314 static inline void FUNC_NAME(pf_fixup_exponent)(char *buf, BOOL three_digit_exp)
316 char* tmp = buf;
318 while(tmp[0] && toupper(tmp[0])!='E')
319 tmp++;
321 if(tmp[0] && (tmp[1]=='+' || tmp[1]=='-') &&
322 isdigit(tmp[2]) && isdigit(tmp[3])) {
323 #if _MSVCR_VER >= 140
324 BOOL two_digit_exp = !three_digit_exp;
325 #else
326 BOOL two_digit_exp = (MSVCRT__get_output_format() == MSVCRT__TWO_DIGIT_EXPONENT);
327 #endif
329 tmp += 2;
330 if(isdigit(tmp[2])) {
331 if(two_digit_exp && tmp[0]=='0') {
332 tmp[0] = tmp[1];
333 tmp[1] = tmp[2];
334 tmp[2] = tmp[3];
337 return; /* Exponent already 3 digits */
338 }else if(two_digit_exp) {
339 return;
342 tmp[3] = tmp[2];
343 tmp[2] = tmp[1];
344 tmp[1] = tmp[0];
345 tmp[0] = '0';
349 int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const APICHAR *fmt,
350 MSVCRT__locale_t locale, DWORD options,
351 args_clbk pf_args, void *args_ctx, __ms_va_list *valist)
353 MSVCRT_pthreadlocinfo locinfo;
354 const APICHAR *q, *p = fmt;
355 APICHAR buf[32];
356 int written = 0, pos, i;
357 FUNC_NAME(pf_flags) flags;
358 BOOL positional_params = options & MSVCRT_PRINTF_POSITIONAL_PARAMS;
359 BOOL invoke_invalid_param_handler = options & MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER;
360 #if _MSVCR_VER >= 140
361 BOOL legacy_wide = options & UCRTBASE_PRINTF_LEGACY_WIDE_SPECIFIERS;
362 BOOL legacy_msvcrt_compat = options & UCRTBASE_PRINTF_LEGACY_MSVCRT_COMPATIBILITY;
363 BOOL three_digit_exp = options & UCRTBASE_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS;
364 #else
365 BOOL legacy_wide = TRUE, legacy_msvcrt_compat = TRUE, three_digit_exp = TRUE;
366 #endif
368 TRACE("Format is: %s\n", FUNC_NAME(debugstr)(fmt));
370 if(!locale)
371 locinfo = get_locinfo();
372 else
373 locinfo = locale->locinfo;
375 while(*p) {
376 /* output characters before '%' */
377 for(q=p; *q && *q!='%'; q++);
378 if(p != q) {
379 i = pf_puts(puts_ctx, q-p, p);
380 if(i < 0)
381 return i;
383 written += i;
384 p = q;
385 continue;
388 /* *p == '%' here */
389 p++;
391 /* output a single '%' character */
392 if(*p == '%') {
393 i = pf_puts(puts_ctx, 1, p++);
394 if(i < 0)
395 return i;
397 written += i;
398 continue;
401 /* check parameter position */
402 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &pos)) && *q=='$')
403 p = q+1;
404 else
405 pos = -1;
407 /* parse the flags */
408 memset(&flags, 0, sizeof(flags));
409 while(*p) {
410 if(*p=='+' || *p==' ') {
411 if(flags.Sign != '+')
412 flags.Sign = *p;
413 } else if(*p == '-')
414 flags.LeftAlign = *p;
415 else if(*p == '0')
416 flags.PadZero = *p;
417 else if(*p == '#')
418 flags.Alternate = *p;
419 else
420 break;
422 p++;
425 /* parse the width */
426 if(*p == '*') {
427 p++;
428 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
429 p = q+1;
430 else
431 i = -1;
433 flags.FieldLength = pf_args(args_ctx, i, VT_INT, valist).get_int;
434 if(flags.FieldLength < 0) {
435 flags.LeftAlign = '-';
436 flags.FieldLength = -flags.FieldLength;
438 } else while(isdigit(*p)) {
439 flags.FieldLength *= 10;
440 flags.FieldLength += *p++ - '0';
443 /* parse the precision */
444 flags.Precision = -1;
445 if(*p == '.') {
446 flags.Precision = 0;
447 p++;
448 if(*p == '*') {
449 p++;
450 if(positional_params && (q = FUNC_NAME(pf_parse_int)(p, &i)) && *q=='$')
451 p = q+1;
452 else
453 i = -1;
455 flags.Precision = pf_args(args_ctx, i, VT_INT, valist).get_int;
456 } else while(isdigit(*p)) {
457 flags.Precision *= 10;
458 flags.Precision += *p++ - '0';
462 /* parse argument size modifier */
463 while(*p) {
464 if(*p=='l' && *(p+1)=='l') {
465 flags.IntegerDouble++;
466 p += 2;
467 } else if(*p=='h' || *p=='l' || *p=='L') {
468 flags.IntegerLength = *p;
469 p++;
470 } else if(*p == 'I') {
471 if(*(p+1)=='6' && *(p+2)=='4') {
472 flags.IntegerDouble++;
473 p += 3;
474 } else if(*(p+1)=='3' && *(p+2)=='2')
475 p += 3;
476 else if(isdigit(*(p+1)) || !*(p+1))
477 break;
478 else
479 flags.IntegerNative = *p++;
480 } else if(*p == 'w')
481 flags.WideString = *p++;
482 #if _MSVCR_VER >= 140
483 else if(*p == 'z')
484 flags.IntegerNative = *p++;
485 else if(*p == 'T')
486 flags.NaturalString = *p++;
487 #endif
488 else if((*p == 'F' || *p == 'N') && legacy_msvcrt_compat)
489 p++; /* ignore */
490 else
491 break;
494 flags.Format = *p;
496 if(flags.Format == 's' || flags.Format == 'S') {
497 i = FUNC_NAME(pf_handle_string)(pf_puts, puts_ctx,
498 pf_args(args_ctx, pos, VT_PTR, valist).get_ptr,
499 -1, &flags, locinfo, legacy_wide);
500 } else if(flags.Format == 'c' || flags.Format == 'C') {
501 int ch = pf_args(args_ctx, pos, VT_INT, valist).get_int;
503 i = FUNC_NAME(pf_handle_string)(pf_puts, puts_ctx, &ch, 1, &flags, locinfo, legacy_wide);
504 } else if(flags.Format == 'p') {
505 flags.Format = 'X';
506 flags.PadZero = '0';
507 i = flags.Precision;
508 flags.Precision = 2*sizeof(void*);
509 FUNC_NAME(pf_integer_conv)(buf, sizeof(buf)/sizeof(APICHAR), &flags,
510 (ULONG_PTR)pf_args(args_ctx, pos, VT_PTR, valist).get_ptr);
511 flags.PadZero = 0;
512 flags.Precision = i;
514 #ifdef PRINTF_WIDE
515 i = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, buf, -1, &flags, locinfo);
516 #else
517 i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, buf, -1, &flags, locinfo);
518 #endif
519 } else if(flags.Format == 'n') {
520 int *used;
522 if(!n_format_enabled) {
523 MSVCRT_INVALID_PMT("\'n\' format specifier disabled", MSVCRT_EINVAL);
524 return -1;
527 used = pf_args(args_ctx, pos, VT_PTR, valist).get_ptr;
528 *used = written;
529 i = 0;
530 } else if(flags.Format && strchr("diouxX", flags.Format)) {
531 APICHAR *tmp = buf;
532 int max_len;
534 /* 0 padding is added after '0x' if Alternate flag is in use */
535 if((flags.Format=='x' || flags.Format=='X') && flags.PadZero && flags.Alternate
536 && !flags.LeftAlign && flags.Precision<flags.FieldLength-2)
537 flags.Precision = flags.FieldLength - 2;
539 max_len = (flags.FieldLength>flags.Precision ? flags.FieldLength : flags.Precision) + 10;
540 if(max_len > sizeof(buf)/sizeof(APICHAR))
541 tmp = HeapAlloc(GetProcessHeap(), 0, max_len);
542 if(!tmp)
543 return -1;
545 if(flags.IntegerDouble || (flags.IntegerNative && sizeof(void*) == 8))
546 FUNC_NAME(pf_integer_conv)(tmp, max_len, &flags, pf_args(args_ctx, pos,
547 VT_I8, valist).get_longlong);
548 else if(flags.Format=='d' || flags.Format=='i')
549 FUNC_NAME(pf_integer_conv)(tmp, max_len, &flags, flags.IntegerLength!='h' ?
550 pf_args(args_ctx, pos, VT_INT, valist).get_int :
551 (short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
552 else
553 FUNC_NAME(pf_integer_conv)(tmp, max_len, &flags, flags.IntegerLength!='h' ?
554 (unsigned)pf_args(args_ctx, pos, VT_INT, valist).get_int :
555 (unsigned short)pf_args(args_ctx, pos, VT_INT, valist).get_int);
557 #ifdef PRINTF_WIDE
558 i = FUNC_NAME(pf_output_format_wstr)(pf_puts, puts_ctx, tmp, -1, &flags, locinfo);
559 #else
560 i = FUNC_NAME(pf_output_format_str)(pf_puts, puts_ctx, tmp, -1, &flags, locinfo);
561 #endif
562 if(tmp != buf)
563 HeapFree(GetProcessHeap(), 0, tmp);
564 } else if(flags.Format && strchr("aeEfFgG", flags.Format)) {
565 char float_fmt[20], buf_a[32], *tmp = buf_a, *decimal_point;
566 int len = flags.Precision + 10;
567 double val = pf_args(args_ctx, pos, VT_R8, valist).get_double;
568 int r;
569 BOOL inf = FALSE, nan = FALSE, ind = FALSE;
571 if(isinf(val))
572 inf = TRUE;
573 else if(isnan(val)) {
574 if(!signbit(val))
575 nan = TRUE;
576 else
577 ind = TRUE;
580 if(inf || nan || ind) {
581 if(ind || val<0)
582 flags.Sign = '-';
584 if(flags.Format=='g' || flags.Format=='G')
585 val = (nan ? 1.12345 : 1.1234); /* fraction will be overwritten with #INF, #IND or #QNAN string */
586 else
587 val = 1;
589 if(flags.Format=='a') {
590 if(flags.Precision==-1)
591 flags.Precision = 6; /* strlen("#INF00") */
595 if(flags.Format=='f' || flags.Format=='F') {
596 if(val>-10.0 && val<10.0)
597 i = 1;
598 else
599 i = 1 + log10(val<0 ? -val : val);
600 /* Default precision is 6, additional space for sign, separator and nullbyte is required */
601 i += (flags.Precision==-1 ? 6 : flags.Precision) + 3;
603 if(i > len)
604 len = i;
607 if(len > sizeof(buf_a))
608 tmp = HeapAlloc(GetProcessHeap(), 0, len);
609 if(!tmp)
610 return -1;
612 FUNC_NAME(pf_rebuild_format_string)(float_fmt, &flags);
613 if(val < 0) {
614 flags.Sign = '-';
615 val = -val;
618 if((inf || nan || ind) && !legacy_msvcrt_compat) {
619 static const char inf_str[] = "inf";
620 static const char ind_str[] = "nan(ind)";
621 static const char nan_str[] = "nan";
622 if(inf)
623 sprintf(tmp, inf_str);
624 else if(ind)
625 sprintf(tmp, ind_str);
626 else
627 sprintf(tmp, nan_str);
628 if (strchr("EFG", flags.Format))
629 for(i=0; tmp[i]; i++)
630 tmp[i] = toupper(tmp[i]);
631 } else {
632 sprintf(tmp, float_fmt, val);
633 if(toupper(flags.Format)=='E' || toupper(flags.Format)=='G')
634 FUNC_NAME(pf_fixup_exponent)(tmp, three_digit_exp);
637 decimal_point = strchr(tmp, '.');
638 if(decimal_point) {
639 *decimal_point = *locinfo->lconv->decimal_point;
641 if(inf || nan || ind) {
642 static const char inf_str[] = "#INF";
643 static const char ind_str[] = "#IND";
644 static const char nan_str[] = "#QNAN";
646 const char *str;
647 int size;
649 if(inf) {
650 str = inf_str;
651 size = sizeof(inf_str);
652 }else if(ind) {
653 str = ind_str;
654 size = sizeof(ind_str);
655 }else {
656 str = nan_str;
657 size = sizeof(nan_str);
660 for(i=1; i<size; i++) {
661 if(decimal_point[i]<'0' || decimal_point[i]>'9')
662 break;
664 decimal_point[i] = str[i-1];
667 if(i!=size && i!=1)
668 decimal_point[i-1]++;
672 len = strlen(tmp);
673 i = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, &flags, TRUE);
674 if(i < 0)
675 return i;
676 r = FUNC_NAME(pf_output_str)(pf_puts, puts_ctx, tmp, len, locinfo);
677 if(r < 0)
678 return r;
679 i += r;
680 if(tmp != buf_a)
681 HeapFree(GetProcessHeap(), 0, tmp);
682 r = FUNC_NAME(pf_fill)(pf_puts, puts_ctx, len, &flags, FALSE);
683 if(r < 0)
684 return r;
685 i += r;
686 } else {
687 if(invoke_invalid_param_handler) {
688 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
689 *MSVCRT__errno() = MSVCRT_EINVAL;
690 return -1;
693 continue;
696 if(i < 0)
697 return i;
698 written += i;
699 p++;
702 return written;
705 #ifndef PRINTF_WIDE
706 enum types_clbk_flags {
707 TYPE_CLBK_VA_LIST = 1,
708 TYPE_CLBK_POSITIONAL = 2,
709 TYPE_CLBK_ERROR_POS = 4,
710 TYPE_CLBK_ERROR_TYPE = 8
713 /* This functions stores types of arguments. It uses args[0] internally */
714 static printf_arg arg_clbk_type(void *ctx, int pos, int type, __ms_va_list *valist)
716 static const printf_arg ret;
717 printf_arg *args = ctx;
719 if(pos == -1) {
720 args[0].get_int |= TYPE_CLBK_VA_LIST;
721 return ret;
722 } else
723 args[0].get_int |= TYPE_CLBK_POSITIONAL;
725 if(pos<1 || pos>MSVCRT__ARGMAX)
726 args[0].get_int |= TYPE_CLBK_ERROR_POS;
727 else if(args[pos].get_int && args[pos].get_int!=type)
728 args[0].get_int |= TYPE_CLBK_ERROR_TYPE;
729 else
730 args[pos].get_int = type;
732 return ret;
734 #endif
736 static int FUNC_NAME(create_positional_ctx)(void *args_ctx, const APICHAR *format, __ms_va_list valist)
738 struct FUNC_NAME(_str_ctx) puts_ctx = {INT_MAX, NULL};
739 printf_arg *args = args_ctx;
740 int i, j;
742 i = FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk_str), &puts_ctx, format, NULL,
743 MSVCRT_PRINTF_POSITIONAL_PARAMS, arg_clbk_type, args_ctx, NULL);
744 if(i < 0)
745 return i;
747 if(args[0].get_int==0 || args[0].get_int==TYPE_CLBK_VA_LIST)
748 return 0;
749 if(args[0].get_int != TYPE_CLBK_POSITIONAL)
750 return -1;
752 for(i=MSVCRT__ARGMAX; i>0; i--)
753 if(args[i].get_int)
754 break;
756 for(j=1; j<=i; j++) {
757 switch(args[j].get_int) {
758 case VT_I8:
759 args[j].get_longlong = va_arg(valist, LONGLONG);
760 break;
761 case VT_INT:
762 args[j].get_int = va_arg(valist, int);
763 break;
764 case VT_R8:
765 args[j].get_double = va_arg(valist, double);
766 break;
767 case VT_PTR:
768 args[j].get_ptr = va_arg(valist, void*);
769 break;
770 default:
771 return -1;
775 return j;
778 #undef APICHAR
779 #undef CONVCHAR
780 #undef FUNC_NAME