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
;
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("diaeEfgG", 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
)
212 static const MSVCRT_wchar_t nullW
[] = {'(','n','u','l','l',')',0};
215 return FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, nullW
, 6, flags
, locinfo
);
218 return FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, "(null)", 6, flags
, locinfo
);
221 if(flags
->WideString
|| flags
->IntegerLength
=='l')
222 return FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, str
, len
, flags
, locinfo
);
223 if(flags
->IntegerLength
== 'h')
224 return FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, str
, len
, flags
, locinfo
);
226 if((flags
->Format
=='S' || flags
->Format
=='C') == (sizeof(APICHAR
)==sizeof(MSVCRT_wchar_t
)))
227 return FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, str
, len
, flags
, locinfo
);
229 return FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, str
, len
, flags
, locinfo
);
232 static inline void FUNC_NAME(pf_rebuild_format_string
)(char *p
, FUNC_NAME(pf_flags
) *flags
)
236 *p
++ = flags
->Alternate
;
237 if(flags
->Precision
>= 0) {
238 sprintf(p
, ".%d", flags
->Precision
);
241 *p
++ = flags
->Format
;
245 /* pf_integer_conv: prints x to buf, including alternate formats and
246 additional precision digits, but not field characters or the sign */
247 static inline void FUNC_NAME(pf_integer_conv
)(APICHAR
*buf
, int buf_len
,
248 FUNC_NAME(pf_flags
) *flags
, LONGLONG x
)
254 if(flags
->Format
== 'o')
256 else if(flags
->Format
=='x' || flags
->Format
=='X')
261 if(flags
->Format
== 'X')
262 digits
= "0123456789ABCDEFX";
264 digits
= "0123456789abcdefx";
266 if(x
<0 && (flags
->Format
=='d' || flags
->Format
=='i')) {
273 flags
->Alternate
= 0;
278 j
= (ULONGLONG
)x
%base
;
279 x
= (ULONGLONG
)x
/base
;
280 buf
[i
++] = digits
[j
];
283 k
= flags
->Precision
-i
;
286 if(flags
->Alternate
) {
288 buf
[i
++] = digits
[16];
290 } else if(base
==8 && buf
[i
-1]!='0')
294 /* Adjust precision so pf_fill won't truncate the number later */
295 flags
->Precision
= i
;
300 APICHAR tmp
= buf
[j
];
307 static inline void FUNC_NAME(pf_fixup_exponent
)(char *buf
)
311 while(tmp
[0] && toupper(tmp
[0])!='E')
314 if(tmp
[0] && (tmp
[1]=='+' || tmp
[1]=='-') &&
315 isdigit(tmp
[2]) && isdigit(tmp
[3])) {
316 BOOL two_digit_exp
= (MSVCRT__get_output_format() == MSVCRT__TWO_DIGIT_EXPONENT
);
319 if(isdigit(tmp
[2])) {
320 if(two_digit_exp
&& tmp
[0]=='0') {
326 return; /* Exponent already 3 digits */
327 }else if(two_digit_exp
) {
338 int FUNC_NAME(pf_printf
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
, const APICHAR
*fmt
,
339 MSVCRT__locale_t locale
, BOOL positional_params
, BOOL invoke_invalid_param_handler
,
340 args_clbk pf_args
, void *args_ctx
, __ms_va_list
*valist
)
342 MSVCRT_pthreadlocinfo locinfo
;
343 const APICHAR
*q
, *p
= fmt
;
345 int written
= 0, pos
, i
;
346 FUNC_NAME(pf_flags
) flags
;
348 TRACE("Format is: %s\n", FUNC_NAME(debugstr
)(fmt
));
351 locinfo
= get_locinfo();
353 locinfo
= locale
->locinfo
;
356 /* output characters before '%' */
357 for(q
=p
; *q
&& *q
!='%'; q
++);
359 i
= pf_puts(puts_ctx
, q
-p
, p
);
371 /* output a single '%' character */
373 i
= pf_puts(puts_ctx
, 1, p
++);
381 /* check parameter position */
382 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &pos
)) && *q
=='$')
387 /* parse the flags */
388 memset(&flags
, 0, sizeof(flags
));
390 if(*p
=='+' || *p
==' ') {
391 if(flags
.Sign
!= '+')
394 flags
.LeftAlign
= *p
;
398 flags
.Alternate
= *p
;
405 /* parse the width */
408 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &i
)) && *q
=='$')
413 flags
.FieldLength
= pf_args(args_ctx
, i
, VT_INT
, valist
).get_int
;
414 if(flags
.FieldLength
< 0) {
415 flags
.LeftAlign
= '-';
416 flags
.FieldLength
= -flags
.FieldLength
;
418 } else while(isdigit(*p
)) {
419 flags
.FieldLength
*= 10;
420 flags
.FieldLength
+= *p
++ - '0';
423 /* parse the precision */
424 flags
.Precision
= -1;
430 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &i
)) && *q
=='$')
435 flags
.Precision
= pf_args(args_ctx
, i
, VT_INT
, valist
).get_int
;
436 } else while(isdigit(*p
)) {
437 flags
.Precision
*= 10;
438 flags
.Precision
+= *p
++ - '0';
442 /* parse argument size modifier */
444 if(*p
=='l' && *(p
+1)=='l') {
445 flags
.IntegerDouble
++;
447 } else if(*p
=='h' || *p
=='l' || *p
=='L') {
448 flags
.IntegerLength
= *p
;
450 } else if(*p
== 'I') {
451 if(*(p
+1)=='6' && *(p
+2)=='4') {
452 flags
.IntegerDouble
++;
454 } else if(*(p
+1)=='3' && *(p
+2)=='2')
456 else if(isdigit(*(p
+1)) || !*(p
+1))
461 flags
.WideString
= *p
++;
470 if(flags
.Format
== 's' || flags
.Format
== 'S') {
471 i
= FUNC_NAME(pf_handle_string
)(pf_puts
, puts_ctx
,
472 pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
,
473 -1, &flags
, locinfo
);
474 } else if(flags
.Format
== 'c' || flags
.Format
== 'C') {
475 int ch
= pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
;
478 FIXME("multibyte characters printing not supported\n");
480 i
= FUNC_NAME(pf_handle_string
)(pf_puts
, puts_ctx
, &ch
, 1, &flags
, locinfo
);
481 } else if(flags
.Format
== 'p') {
485 flags
.Precision
= 2*sizeof(void*);
486 FUNC_NAME(pf_integer_conv
)(buf
, sizeof(buf
)/sizeof(APICHAR
), &flags
,
487 (ULONG_PTR
)pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
);
492 i
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, buf
, -1, &flags
, locinfo
);
494 i
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, buf
, -1, &flags
, locinfo
);
496 } else if(flags
.Format
== 'n') {
499 if(!n_format_enabled
) {
500 MSVCRT_INVALID_PMT("\'n\' format specifier disabled", MSVCRT_EINVAL
);
504 used
= pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
;
507 } else if(flags
.Format
&& strchr("diouxX", flags
.Format
)) {
511 /* 0 padding is added after '0x' if Alternate flag is in use */
512 if((flags
.Format
=='x' || flags
.Format
=='X') && flags
.PadZero
&& flags
.Alternate
513 && !flags
.LeftAlign
&& flags
.Precision
<flags
.FieldLength
-2)
514 flags
.Precision
= flags
.FieldLength
- 2;
516 max_len
= (flags
.FieldLength
>flags
.Precision
? flags
.FieldLength
: flags
.Precision
) + 10;
517 if(max_len
> sizeof(buf
)/sizeof(APICHAR
))
518 tmp
= HeapAlloc(GetProcessHeap(), 0, max_len
);
522 if(flags
.IntegerDouble
)
523 FUNC_NAME(pf_integer_conv
)(tmp
, max_len
, &flags
, pf_args(args_ctx
, pos
,
524 VT_I8
, valist
).get_longlong
);
525 else if(flags
.Format
=='d' || flags
.Format
=='i')
526 FUNC_NAME(pf_integer_conv
)(tmp
, max_len
, &flags
, flags
.IntegerLength
!='h' ?
527 pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
:
528 (short)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
);
530 FUNC_NAME(pf_integer_conv
)(tmp
, max_len
, &flags
, flags
.IntegerLength
!='h' ?
531 (unsigned)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
:
532 (unsigned short)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
);
535 i
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, tmp
, -1, &flags
, locinfo
);
537 i
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, tmp
, -1, &flags
, locinfo
);
540 HeapFree(GetProcessHeap(), 0, tmp
);
541 } else if(flags
.Format
&& strchr("aeEfgG", flags
.Format
)) {
542 char float_fmt
[20], buf_a
[32], *tmp
= buf_a
, *decimal_point
;
543 int len
= flags
.Precision
+ 10;
544 double val
= pf_args(args_ctx
, pos
, VT_R8
, valist
).get_double
;
546 BOOL inf
= FALSE
, nan
= FALSE
, ind
= FALSE
;
550 else if(isnan(val
)) {
557 if(inf
|| nan
|| ind
) {
561 if(flags
.Format
=='g' || flags
.Format
=='G')
562 val
= (nan
? 1.12345 : 1.1234); /* fraction will be overwritten with #INF, #IND or #QNAN string */
566 if(flags
.Format
=='a') {
567 if(flags
.Precision
==-1)
568 flags
.Precision
= 6; /* strlen("#INF00") */
572 if(flags
.Format
=='f') {
573 if(val
>-10.0 && val
<10.0)
576 i
= 1 + log10(val
<0 ? -val
: val
);
577 /* Default precision is 6, additional space for sign, separator and nullbyte is required */
578 i
+= (flags
.Precision
==-1 ? 6 : flags
.Precision
) + 3;
584 if(len
> sizeof(buf_a
))
585 tmp
= HeapAlloc(GetProcessHeap(), 0, len
);
589 FUNC_NAME(pf_rebuild_format_string
)(float_fmt
, &flags
);
595 sprintf(tmp
, float_fmt
, val
);
596 if(toupper(flags
.Format
)=='E' || toupper(flags
.Format
)=='G')
597 FUNC_NAME(pf_fixup_exponent
)(tmp
);
599 decimal_point
= strchr(tmp
, '.');
601 *decimal_point
= *locinfo
->lconv
->decimal_point
;
603 if(inf
|| nan
|| ind
) {
604 static const char inf_str
[] = "#INF";
605 static const char ind_str
[] = "#IND";
606 static const char nan_str
[] = "#QNAN";
613 size
= sizeof(inf_str
);
616 size
= sizeof(ind_str
);
619 size
= sizeof(nan_str
);
622 for(i
=1; i
<size
; i
++) {
623 if(decimal_point
[i
]<'0' || decimal_point
[i
]>'9')
626 decimal_point
[i
] = str
[i
-1];
630 decimal_point
[i
-1]++;
635 i
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, &flags
, TRUE
);
638 r
= FUNC_NAME(pf_output_str
)(pf_puts
, puts_ctx
, tmp
, len
, locinfo
);
643 HeapFree(GetProcessHeap(), 0, tmp
);
644 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, &flags
, FALSE
);
649 if(invoke_invalid_param_handler
) {
650 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
651 *MSVCRT__errno() = MSVCRT_EINVAL
;
668 enum types_clbk_flags
{
669 TYPE_CLBK_VA_LIST
= 1,
670 TYPE_CLBK_POSITIONAL
= 2,
671 TYPE_CLBK_ERROR_POS
= 4,
672 TYPE_CLBK_ERROR_TYPE
= 8
675 /* This functions stores types of arguments. It uses args[0] internally */
676 static printf_arg
arg_clbk_type(void *ctx
, int pos
, int type
, __ms_va_list
*valist
)
678 static const printf_arg ret
;
679 printf_arg
*args
= ctx
;
682 args
[0].get_int
|= TYPE_CLBK_VA_LIST
;
685 args
[0].get_int
|= TYPE_CLBK_POSITIONAL
;
687 if(pos
<1 || pos
>MSVCRT__ARGMAX
)
688 args
[0].get_int
|= TYPE_CLBK_ERROR_POS
;
689 else if(args
[pos
].get_int
&& args
[pos
].get_int
!=type
)
690 args
[0].get_int
|= TYPE_CLBK_ERROR_TYPE
;
692 args
[pos
].get_int
= type
;
698 static int FUNC_NAME(create_positional_ctx
)(void *args_ctx
, const APICHAR
*format
, __ms_va_list valist
)
700 struct FUNC_NAME(_str_ctx
) puts_ctx
= {INT_MAX
, NULL
};
701 printf_arg
*args
= args_ctx
;
704 i
= FUNC_NAME(pf_printf
)(FUNC_NAME(puts_clbk_str
), &puts_ctx
, format
, NULL
, TRUE
, FALSE
,
705 arg_clbk_type
, args_ctx
, NULL
);
709 if(args
[0].get_int
==0 || args
[0].get_int
==TYPE_CLBK_VA_LIST
)
711 if(args
[0].get_int
!= TYPE_CLBK_POSITIONAL
)
714 for(i
=MSVCRT__ARGMAX
; i
>0; i
--)
718 for(j
=1; j
<=i
; j
++) {
719 switch(args
[j
].get_int
) {
721 args
[j
].get_longlong
= va_arg(valist
, LONGLONG
);
724 args
[j
].get_int
= va_arg(valist
, int);
727 args
[j
].get_double
= va_arg(valist
, double);
730 args
[j
].get_ptr
= va_arg(valist
, void*);