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
20 #define APICHAR MSVCRT_wchar_t
22 #define FUNC_NAME(func) func ## _w
25 #define CONVCHAR MSVCRT_wchar_t
26 #define FUNC_NAME(func) func ## _a
30 #define signbit(x) ((x) < 0)
33 typedef struct FUNC_NAME(pf_flags_t
)
35 APICHAR Sign
, LeftAlign
, Alternate
, PadZero
;
36 int FieldLength
, Precision
;
37 APICHAR IntegerLength
, IntegerDouble
, IntegerNative
;
40 } FUNC_NAME(pf_flags
);
42 struct FUNC_NAME(_str_ctx
) {
47 static int FUNC_NAME(puts_clbk_str
)(void *ctx
, int len
, const APICHAR
*str
)
49 struct FUNC_NAME(_str_ctx
) *out
= ctx
;
55 memcpy(out
->buf
, str
, out
->len
*sizeof(APICHAR
));
61 memcpy(out
->buf
, str
, len
*sizeof(APICHAR
));
67 static inline const APICHAR
* FUNC_NAME(pf_parse_int
)(const APICHAR
*fmt
, int *val
)
71 while(isdigit(*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
))
88 if(left
&& flags
->Sign
) {
91 r
= pf_puts(puts_ctx
, 1, &flags
->Sign
);
95 if((!left
&& flags
->LeftAlign
) || (left
&& !flags
->LeftAlign
)) {
98 if(left
&& flags
->PadZero
)
103 for(i
=0; i
<flags
->FieldLength
-len
&& r
>=0; i
++) {
104 r
= pf_puts(puts_ctx
, 1, &ch
);
110 if(r
>=0 && left
&& flags
->Sign
&& !flags
->PadZero
) {
111 r
= pf_puts(puts_ctx
, 1, &flags
->Sign
);
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
)
122 return pf_puts(puts_ctx
, len
, str
);
125 int len_a
= WideCharToMultiByte(locinfo
->lc_codepage
, 0, str
, len
, NULL
, 0, NULL
, NULL
);
127 out
= HeapAlloc(GetProcessHeap(), 0, len_a
);
131 WideCharToMultiByte(locinfo
->lc_codepage
, 0, str
, len
, out
, len_a
, NULL
, NULL
);
132 len
= pf_puts(puts_ctx
, len_a
, out
);
133 HeapFree(GetProcessHeap(), 0, out
);
138 static inline int FUNC_NAME(pf_output_str
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
139 const char *str
, int len
, MSVCRT_pthreadlocinfo locinfo
)
143 int len_w
= MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, len
, NULL
, 0);
145 out
= HeapAlloc(GetProcessHeap(), 0, len_w
*sizeof(WCHAR
));
149 MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, len
, out
, len_w
);
150 len
= pf_puts(puts_ctx
, len_w
, out
);
151 HeapFree(GetProcessHeap(), 0, out
);
154 return pf_puts(puts_ctx
, len
, str
);
158 static inline int FUNC_NAME(pf_output_format_wstr
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
159 const MSVCRT_wchar_t
*str
, int len
, FUNC_NAME(pf_flags
) *flags
, MSVCRT_pthreadlocinfo locinfo
)
166 if(flags
->Precision
>=0 && flags
->Precision
<len
)
167 len
= flags
->Precision
;
169 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, TRUE
);
172 r
= FUNC_NAME(pf_output_wstr
)(pf_puts
, puts_ctx
, str
, len
, locinfo
);
176 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, FALSE
);
180 return r
>=0 ? ret
: r
;
183 static inline int FUNC_NAME(pf_output_format_str
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
184 const char *str
, int len
, FUNC_NAME(pf_flags
) *flags
, MSVCRT_pthreadlocinfo locinfo
)
191 if(flags
->Precision
>=0 && flags
->Precision
<len
)
192 len
= flags
->Precision
;
194 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, TRUE
);
197 r
= FUNC_NAME(pf_output_str
)(pf_puts
, puts_ctx
, str
, len
, locinfo
);
201 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, FALSE
);
205 return r
>=0 ? ret
: r
;
208 static inline int FUNC_NAME(pf_handle_string
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
209 const void *str
, int len
, FUNC_NAME(pf_flags
) *flags
, MSVCRT_pthreadlocinfo locinfo
, BOOL legacy_wide
)
211 BOOL complement_is_narrow
= legacy_wide
? (sizeof(APICHAR
)==sizeof(MSVCRT_wchar_t
)) : FALSE
;
213 static const MSVCRT_wchar_t nullW
[] = {'(','n','u','l','l',')',0};
216 return FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, nullW
, 6, flags
, locinfo
);
219 return FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, "(null)", 6, flags
, locinfo
);
222 if(flags
->WideString
|| flags
->IntegerLength
=='l')
223 return FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, str
, len
, flags
, locinfo
);
224 if(flags
->IntegerLength
== 'h')
225 return FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, str
, len
, flags
, locinfo
);
227 if((flags
->Format
=='S' || flags
->Format
=='C') == complement_is_narrow
)
228 return FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, str
, len
, flags
, locinfo
);
230 return FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, str
, len
, flags
, locinfo
);
233 static inline void FUNC_NAME(pf_rebuild_format_string
)(char *p
, FUNC_NAME(pf_flags
) *flags
)
237 *p
++ = flags
->Alternate
;
238 if(flags
->Precision
>= 0) {
239 sprintf(p
, ".%d", flags
->Precision
);
242 *p
++ = flags
->Format
;
246 /* pf_integer_conv: prints x to buf, including alternate formats and
247 additional precision digits, but not field characters or the sign */
248 static inline void FUNC_NAME(pf_integer_conv
)(APICHAR
*buf
, int buf_len
,
249 FUNC_NAME(pf_flags
) *flags
, LONGLONG x
)
255 if(flags
->Format
== 'o')
257 else if(flags
->Format
=='x' || flags
->Format
=='X')
262 if(flags
->Format
== 'X')
263 digits
= "0123456789ABCDEFX";
265 digits
= "0123456789abcdefx";
267 if(x
<0 && (flags
->Format
=='d' || flags
->Format
=='i')) {
274 flags
->Alternate
= 0;
279 j
= (ULONGLONG
)x
%base
;
280 x
= (ULONGLONG
)x
/base
;
281 buf
[i
++] = digits
[j
];
284 k
= flags
->Precision
-i
;
287 if(flags
->Alternate
) {
289 buf
[i
++] = digits
[16];
291 } else if(base
==8 && buf
[i
-1]!='0')
295 /* Adjust precision so pf_fill won't truncate the number later */
296 flags
->Precision
= i
;
301 APICHAR tmp
= buf
[j
];
308 static inline void FUNC_NAME(pf_fixup_exponent
)(char *buf
, BOOL three_digit_exp
)
312 while(tmp
[0] && toupper(tmp
[0])!='E')
315 if(tmp
[0] && (tmp
[1]=='+' || tmp
[1]=='-') &&
316 isdigit(tmp
[2]) && isdigit(tmp
[3])) {
317 #if _MSVCR_VER >= 140
318 BOOL two_digit_exp
= !three_digit_exp
;
320 BOOL two_digit_exp
= (MSVCRT__get_output_format() == MSVCRT__TWO_DIGIT_EXPONENT
);
324 if(isdigit(tmp
[2])) {
325 if(two_digit_exp
&& tmp
[0]=='0') {
331 return; /* Exponent already 3 digits */
332 }else if(two_digit_exp
) {
343 int FUNC_NAME(pf_printf
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
, const APICHAR
*fmt
,
344 MSVCRT__locale_t locale
, DWORD options
,
345 args_clbk pf_args
, void *args_ctx
, __ms_va_list
*valist
)
347 MSVCRT_pthreadlocinfo locinfo
;
348 const APICHAR
*q
, *p
= fmt
;
350 int written
= 0, pos
, i
;
351 FUNC_NAME(pf_flags
) flags
;
352 BOOL positional_params
= options
& MSVCRT_PRINTF_POSITIONAL_PARAMS
;
353 BOOL invoke_invalid_param_handler
= options
& MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER
;
354 #if _MSVCR_VER >= 140
355 BOOL legacy_wide
= options
& UCRTBASE_PRINTF_LEGACY_WIDE_SPECIFIERS
;
356 BOOL legacy_msvcrt_compat
= options
& UCRTBASE_PRINTF_LEGACY_MSVCRT_COMPATIBILITY
;
357 BOOL three_digit_exp
= options
& UCRTBASE_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS
;
359 BOOL legacy_wide
= TRUE
, legacy_msvcrt_compat
= TRUE
, three_digit_exp
= TRUE
;
362 TRACE("Format is: %s\n", FUNC_NAME(debugstr
)(fmt
));
365 locinfo
= get_locinfo();
367 locinfo
= locale
->locinfo
;
370 /* output characters before '%' */
371 for(q
=p
; *q
&& *q
!='%'; q
++);
373 i
= pf_puts(puts_ctx
, q
-p
, p
);
385 /* output a single '%' character */
387 i
= pf_puts(puts_ctx
, 1, p
++);
395 /* check parameter position */
396 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &pos
)) && *q
=='$')
401 /* parse the flags */
402 memset(&flags
, 0, sizeof(flags
));
404 if(*p
=='+' || *p
==' ') {
405 if(flags
.Sign
!= '+')
408 flags
.LeftAlign
= *p
;
412 flags
.Alternate
= *p
;
419 /* parse the width */
422 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &i
)) && *q
=='$')
427 flags
.FieldLength
= pf_args(args_ctx
, i
, VT_INT
, valist
).get_int
;
428 if(flags
.FieldLength
< 0) {
429 flags
.LeftAlign
= '-';
430 flags
.FieldLength
= -flags
.FieldLength
;
432 } else while(isdigit(*p
)) {
433 flags
.FieldLength
*= 10;
434 flags
.FieldLength
+= *p
++ - '0';
437 /* parse the precision */
438 flags
.Precision
= -1;
444 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &i
)) && *q
=='$')
449 flags
.Precision
= pf_args(args_ctx
, i
, VT_INT
, valist
).get_int
;
450 } else while(isdigit(*p
)) {
451 flags
.Precision
*= 10;
452 flags
.Precision
+= *p
++ - '0';
456 /* parse argument size modifier */
458 if(*p
=='l' && *(p
+1)=='l') {
459 flags
.IntegerDouble
++;
461 } else if(*p
=='h' || *p
=='l' || *p
=='L') {
462 flags
.IntegerLength
= *p
;
464 } else if(*p
== 'I') {
465 if(*(p
+1)=='6' && *(p
+2)=='4') {
466 flags
.IntegerDouble
++;
468 } else if(*(p
+1)=='3' && *(p
+2)=='2')
470 else if(isdigit(*(p
+1)) || !*(p
+1))
473 flags
.IntegerNative
= *p
++;
475 flags
.WideString
= *p
++;
476 #if _MSVCR_VER >= 140
478 flags
.IntegerNative
= *p
++;
480 else if((*p
== 'F' || *p
== 'N') && legacy_msvcrt_compat
)
488 if(flags
.Format
== 's' || flags
.Format
== 'S') {
489 i
= FUNC_NAME(pf_handle_string
)(pf_puts
, puts_ctx
,
490 pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
,
491 -1, &flags
, locinfo
, legacy_wide
);
492 } else if(flags
.Format
== 'c' || flags
.Format
== 'C') {
493 int ch
= pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
;
496 FIXME("multibyte characters printing not supported\n");
498 i
= FUNC_NAME(pf_handle_string
)(pf_puts
, puts_ctx
, &ch
, 1, &flags
, locinfo
, legacy_wide
);
499 } else if(flags
.Format
== 'p') {
503 flags
.Precision
= 2*sizeof(void*);
504 FUNC_NAME(pf_integer_conv
)(buf
, sizeof(buf
)/sizeof(APICHAR
), &flags
,
505 (ULONG_PTR
)pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
);
510 i
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, buf
, -1, &flags
, locinfo
);
512 i
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, buf
, -1, &flags
, locinfo
);
514 } else if(flags
.Format
== 'n') {
517 if(!n_format_enabled
) {
518 MSVCRT_INVALID_PMT("\'n\' format specifier disabled", MSVCRT_EINVAL
);
522 used
= pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
;
525 } else if(flags
.Format
&& strchr("diouxX", flags
.Format
)) {
529 /* 0 padding is added after '0x' if Alternate flag is in use */
530 if((flags
.Format
=='x' || flags
.Format
=='X') && flags
.PadZero
&& flags
.Alternate
531 && !flags
.LeftAlign
&& flags
.Precision
<flags
.FieldLength
-2)
532 flags
.Precision
= flags
.FieldLength
- 2;
534 max_len
= (flags
.FieldLength
>flags
.Precision
? flags
.FieldLength
: flags
.Precision
) + 10;
535 if(max_len
> sizeof(buf
)/sizeof(APICHAR
))
536 tmp
= HeapAlloc(GetProcessHeap(), 0, max_len
);
540 if(flags
.IntegerDouble
|| (flags
.IntegerNative
&& sizeof(void*) == 8))
541 FUNC_NAME(pf_integer_conv
)(tmp
, max_len
, &flags
, pf_args(args_ctx
, pos
,
542 VT_I8
, valist
).get_longlong
);
543 else if(flags
.Format
=='d' || flags
.Format
=='i')
544 FUNC_NAME(pf_integer_conv
)(tmp
, max_len
, &flags
, flags
.IntegerLength
!='h' ?
545 pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
:
546 (short)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
);
548 FUNC_NAME(pf_integer_conv
)(tmp
, max_len
, &flags
, flags
.IntegerLength
!='h' ?
549 (unsigned)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
:
550 (unsigned short)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
);
553 i
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, tmp
, -1, &flags
, locinfo
);
555 i
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, tmp
, -1, &flags
, locinfo
);
558 HeapFree(GetProcessHeap(), 0, tmp
);
559 } else if(flags
.Format
&& strchr("aeEfFgG", flags
.Format
)) {
560 char float_fmt
[20], buf_a
[32], *tmp
= buf_a
, *decimal_point
;
561 int len
= flags
.Precision
+ 10;
562 double val
= pf_args(args_ctx
, pos
, VT_R8
, valist
).get_double
;
564 BOOL inf
= FALSE
, nan
= FALSE
, ind
= FALSE
;
568 else if(isnan(val
)) {
575 if(inf
|| nan
|| ind
) {
579 if(flags
.Format
=='g' || flags
.Format
=='G')
580 val
= (nan
? 1.12345 : 1.1234); /* fraction will be overwritten with #INF, #IND or #QNAN string */
584 if(flags
.Format
=='a') {
585 if(flags
.Precision
==-1)
586 flags
.Precision
= 6; /* strlen("#INF00") */
590 if(flags
.Format
=='f' || flags
.Format
=='F') {
591 if(val
>-10.0 && val
<10.0)
594 i
= 1 + log10(val
<0 ? -val
: val
);
595 /* Default precision is 6, additional space for sign, separator and nullbyte is required */
596 i
+= (flags
.Precision
==-1 ? 6 : flags
.Precision
) + 3;
602 if(len
> sizeof(buf_a
))
603 tmp
= HeapAlloc(GetProcessHeap(), 0, len
);
607 FUNC_NAME(pf_rebuild_format_string
)(float_fmt
, &flags
);
613 if((inf
|| nan
|| ind
) && !legacy_msvcrt_compat
) {
614 static const char inf_str
[] = "inf";
615 static const char ind_str
[] = "nan(ind)";
616 static const char nan_str
[] = "nan";
618 sprintf(tmp
, inf_str
);
620 sprintf(tmp
, ind_str
);
622 sprintf(tmp
, nan_str
);
623 if (strchr("EFG", flags
.Format
))
624 for(i
=0; tmp
[i
]; i
++)
625 tmp
[i
] = toupper(tmp
[i
]);
627 sprintf(tmp
, float_fmt
, val
);
628 if(toupper(flags
.Format
)=='E' || toupper(flags
.Format
)=='G')
629 FUNC_NAME(pf_fixup_exponent
)(tmp
, three_digit_exp
);
632 decimal_point
= strchr(tmp
, '.');
634 *decimal_point
= *locinfo
->lconv
->decimal_point
;
636 if(inf
|| nan
|| ind
) {
637 static const char inf_str
[] = "#INF";
638 static const char ind_str
[] = "#IND";
639 static const char nan_str
[] = "#QNAN";
646 size
= sizeof(inf_str
);
649 size
= sizeof(ind_str
);
652 size
= sizeof(nan_str
);
655 for(i
=1; i
<size
; i
++) {
656 if(decimal_point
[i
]<'0' || decimal_point
[i
]>'9')
659 decimal_point
[i
] = str
[i
-1];
663 decimal_point
[i
-1]++;
668 i
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, &flags
, TRUE
);
671 r
= FUNC_NAME(pf_output_str
)(pf_puts
, puts_ctx
, tmp
, len
, locinfo
);
676 HeapFree(GetProcessHeap(), 0, tmp
);
677 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, &flags
, FALSE
);
682 if(invoke_invalid_param_handler
) {
683 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
684 *MSVCRT__errno() = MSVCRT_EINVAL
;
701 enum types_clbk_flags
{
702 TYPE_CLBK_VA_LIST
= 1,
703 TYPE_CLBK_POSITIONAL
= 2,
704 TYPE_CLBK_ERROR_POS
= 4,
705 TYPE_CLBK_ERROR_TYPE
= 8
708 /* This functions stores types of arguments. It uses args[0] internally */
709 static printf_arg
arg_clbk_type(void *ctx
, int pos
, int type
, __ms_va_list
*valist
)
711 static const printf_arg ret
;
712 printf_arg
*args
= ctx
;
715 args
[0].get_int
|= TYPE_CLBK_VA_LIST
;
718 args
[0].get_int
|= TYPE_CLBK_POSITIONAL
;
720 if(pos
<1 || pos
>MSVCRT__ARGMAX
)
721 args
[0].get_int
|= TYPE_CLBK_ERROR_POS
;
722 else if(args
[pos
].get_int
&& args
[pos
].get_int
!=type
)
723 args
[0].get_int
|= TYPE_CLBK_ERROR_TYPE
;
725 args
[pos
].get_int
= type
;
731 static int FUNC_NAME(create_positional_ctx
)(void *args_ctx
, const APICHAR
*format
, __ms_va_list valist
)
733 struct FUNC_NAME(_str_ctx
) puts_ctx
= {INT_MAX
, NULL
};
734 printf_arg
*args
= args_ctx
;
737 i
= FUNC_NAME(pf_printf
)(FUNC_NAME(puts_clbk_str
), &puts_ctx
, format
, NULL
,
738 MSVCRT_PRINTF_POSITIONAL_PARAMS
, arg_clbk_type
, args_ctx
, NULL
);
742 if(args
[0].get_int
==0 || args
[0].get_int
==TYPE_CLBK_VA_LIST
)
744 if(args
[0].get_int
!= TYPE_CLBK_POSITIONAL
)
747 for(i
=MSVCRT__ARGMAX
; i
>0; i
--)
751 for(j
=1; j
<=i
; j
++) {
752 switch(args
[j
].get_int
) {
754 args
[j
].get_longlong
= va_arg(valist
, LONGLONG
);
757 args
[j
].get_int
= va_arg(valist
, int);
760 args
[j
].get_double
= va_arg(valist
, double);
763 args
[j
].get_ptr
= va_arg(valist
, void*);