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
29 typedef struct FUNC_NAME(pf_flags_t
)
31 APICHAR Sign
, LeftAlign
, Alternate
, PadZero
;
32 int FieldLength
, Precision
;
33 APICHAR IntegerLength
, IntegerDouble
;
36 } FUNC_NAME(pf_flags
);
38 struct FUNC_NAME(_str_ctx
) {
43 static int FUNC_NAME(puts_clbk_str
)(void *ctx
, int len
, const APICHAR
*str
)
45 struct FUNC_NAME(_str_ctx
) *out
= ctx
;
51 memcpy(out
->buf
, str
, out
->len
);
57 memcpy(out
->buf
, str
, len
*sizeof(APICHAR
));
63 static inline const APICHAR
* FUNC_NAME(pf_parse_int
)(const APICHAR
*fmt
, int *val
)
67 while(isdigit(*fmt
)) {
75 /* pf_fill: takes care of signs, alignment, zero and field padding */
76 static inline int FUNC_NAME(pf_fill
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
77 int len
, FUNC_NAME(pf_flags
) *flags
, BOOL left
)
79 int i
, r
= 0, written
;
81 if(flags
->Sign
&& !(flags
->Format
=='d' || flags
->Format
=='i'))
84 if(left
&& flags
->Sign
) {
87 r
= pf_puts(puts_ctx
, 1, &flags
->Sign
);
91 if((!left
&& flags
->LeftAlign
) || (left
&& !flags
->LeftAlign
)) {
94 if(left
&& flags
->PadZero
)
99 for(i
=0; i
<flags
->FieldLength
-len
&& r
>=0; i
++) {
100 r
= pf_puts(puts_ctx
, 1, &ch
);
106 if(r
>=0 && left
&& flags
->Sign
&& !flags
->PadZero
) {
107 r
= pf_puts(puts_ctx
, 1, &flags
->Sign
);
111 return r
>=0 ? written
: r
;
114 static inline int FUNC_NAME(pf_output_wstr
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
115 const MSVCRT_wchar_t
*str
, int len
, MSVCRT_pthreadlocinfo locinfo
)
118 return pf_puts(puts_ctx
, len
, str
);
121 int len_a
= WideCharToMultiByte(locinfo
->lc_codepage
, 0, str
, len
, NULL
, 0, NULL
, NULL
);
123 out
= HeapAlloc(GetProcessHeap(), 0, len_a
);
127 WideCharToMultiByte(locinfo
->lc_codepage
, 0, str
, len
, out
, len_a
, NULL
, NULL
);
128 len
= pf_puts(puts_ctx
, len_a
, out
);
129 HeapFree(GetProcessHeap(), 0, out
);
134 static inline int FUNC_NAME(pf_output_str
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
135 const char *str
, int len
, MSVCRT_pthreadlocinfo locinfo
)
139 int len_w
= MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, len
, NULL
, 0);
141 out
= HeapAlloc(GetProcessHeap(), 0, len_w
*sizeof(WCHAR
));
145 MultiByteToWideChar(locinfo
->lc_codepage
, 0, str
, len
, out
, len_w
);
146 len
= pf_puts(puts_ctx
, len_w
, out
);
147 HeapFree(GetProcessHeap(), 0, out
);
150 return pf_puts(puts_ctx
, len
, str
);
154 static inline int FUNC_NAME(pf_output_format_wstr
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
155 const MSVCRT_wchar_t
*str
, int len
, FUNC_NAME(pf_flags
) *flags
, MSVCRT_pthreadlocinfo locinfo
)
162 if(flags
->Precision
>=0 && flags
->Precision
<len
)
163 len
= flags
->Precision
;
165 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, TRUE
);
168 r
= FUNC_NAME(pf_output_wstr
)(pf_puts
, puts_ctx
, str
, len
, locinfo
);
172 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, FALSE
);
176 return r
>=0 ? ret
: r
;
179 static inline int FUNC_NAME(pf_output_format_str
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
180 const char *str
, int len
, FUNC_NAME(pf_flags
) *flags
, MSVCRT_pthreadlocinfo locinfo
)
187 if(flags
->Precision
>=0 && flags
->Precision
<len
)
188 len
= flags
->Precision
;
190 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, TRUE
);
193 r
= FUNC_NAME(pf_output_str
)(pf_puts
, puts_ctx
, str
, len
, locinfo
);
197 r
= FUNC_NAME(pf_fill
)(pf_puts
, puts_ctx
, len
, flags
, FALSE
);
201 return r
>=0 ? ret
: r
;
204 static inline int FUNC_NAME(pf_handle_string
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
,
205 const void *str
, int len
, FUNC_NAME(pf_flags
) *flags
, MSVCRT_pthreadlocinfo locinfo
)
208 static const MSVCRT_wchar_t nullW
[] = {'(','n','u','l','l',')',0};
211 return FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, nullW
, 6, flags
, locinfo
);
214 return FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, "(null)", 6, flags
, locinfo
);
217 if(flags
->WideString
|| flags
->IntegerLength
=='l')
218 return FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, str
, len
, flags
, locinfo
);
219 if(flags
->IntegerLength
== 'h')
220 return FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, str
, len
, flags
, locinfo
);
222 if((flags
->Format
=='S' || flags
->Format
=='C') == (sizeof(APICHAR
)==sizeof(MSVCRT_wchar_t
)))
223 return FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, str
, len
, flags
, locinfo
);
225 return FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, str
, len
, flags
, locinfo
);
228 static inline void FUNC_NAME(pf_rebuild_format_string
)(char *p
, FUNC_NAME(pf_flags
) *flags
)
234 *p
++ = flags
->LeftAlign
;
236 *p
++ = flags
->Alternate
;
238 *p
++ = flags
->PadZero
;
239 if(flags
->FieldLength
) {
240 sprintf(p
, "%d", flags
->FieldLength
);
243 if(flags
->Precision
>= 0) {
244 sprintf(p
, ".%d", flags
->Precision
);
247 *p
++ = flags
->Format
;
251 /* pf_integer_conv: prints x to buf, including alternate formats and
252 additional precision digits, but not field characters or the sign */
253 static inline void FUNC_NAME(pf_integer_conv
)(APICHAR
*buf
, int buf_len
,
254 FUNC_NAME(pf_flags
) *flags
, LONGLONG x
)
260 if(flags
->Format
== 'o')
262 else if(flags
->Format
=='x' || flags
->Format
=='X')
267 if(flags
->Format
== 'X')
268 digits
= "0123456789ABCDEFX";
270 digits
= "0123456789abcdefx";
272 if(x
<0 && (flags
->Format
=='d' || flags
->Format
=='i')) {
278 if(x
==0 && flags
->Precision
)
282 j
= (ULONGLONG
)x
%base
;
283 x
= (ULONGLONG
)x
/base
;
284 buf
[i
++] = digits
[j
];
287 k
= flags
->Precision
-i
;
290 if(flags
->Alternate
) {
292 buf
[i
++] = digits
[16];
294 } else if(base
==8 && buf
[i
-1]!='0')
298 /* Adjust precision so pf_fill won't truncate the number later */
299 flags
->Precision
= i
;
304 APICHAR tmp
= buf
[j
];
311 static inline void FUNC_NAME(pf_fixup_exponent
)(char *buf
)
315 while(tmp
[0] && toupper(tmp
[0])!='E')
318 if(tmp
[0] && (tmp
[1]=='+' || tmp
[1]=='-') &&
319 isdigit(tmp
[2]) && isdigit(tmp
[3])) {
323 return; /* Exponent already 3 digits */
334 memmove(buf
, buf
+ 1, (tmp
- buf
) + 3);
339 int FUNC_NAME(pf_printf
)(FUNC_NAME(puts_clbk
) pf_puts
, void *puts_ctx
, const APICHAR
*fmt
,
340 MSVCRT__locale_t locale
, BOOL positional_params
, BOOL invoke_invalid_param_handler
,
341 args_clbk pf_args
, void *args_ctx
, __ms_va_list
*valist
)
343 MSVCRT_pthreadlocinfo locinfo
;
344 const APICHAR
*q
, *p
= fmt
;
346 int written
= 0, pos
, i
;
347 FUNC_NAME(pf_flags
) flags
;
349 TRACE("Format is: %s\n", FUNC_NAME(debugstr
)(fmt
));
352 locinfo
= get_locinfo();
354 locinfo
= locale
->locinfo
;
357 /* output characters before '%' */
358 for(q
=p
; *q
&& *q
!='%'; q
++);
360 i
= pf_puts(puts_ctx
, q
-p
, p
);
372 /* output a single '%' character */
374 i
= pf_puts(puts_ctx
, 1, p
++);
382 /* check parameter position */
383 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &pos
)) && *q
=='$')
388 /* parse the flags */
389 memset(&flags
, 0, sizeof(flags
));
391 if(*p
=='+' || *p
==' ') {
392 if(flags
.Sign
!= '+')
395 flags
.LeftAlign
= *p
;
399 flags
.Alternate
= *p
;
409 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &i
)) && *q
=='$')
414 flags
.FieldLength
= pf_args(args_ctx
, i
, VT_INT
, valist
).get_int
;
415 if(flags
.FieldLength
< 0) {
416 flags
.LeftAlign
= '-';
417 flags
.FieldLength
= -flags
.FieldLength
;
419 } else while(isdigit(*p
)) {
420 flags
.FieldLength
*= 10;
421 flags
.FieldLength
+= *p
++ - '0';
424 /* parse the precision */
425 flags
.Precision
= -1;
431 if(positional_params
&& (q
= FUNC_NAME(pf_parse_int
)(p
, &i
)) && *q
=='$')
436 flags
.Precision
= pf_args(args_ctx
, i
, VT_INT
, valist
).get_int
;
437 } else while(isdigit(*p
)) {
438 flags
.Precision
*= 10;
439 flags
.Precision
+= *p
++ - '0';
443 /* parse argument size modifier */
445 if(*p
=='l' && *(p
+1)=='l') {
446 flags
.IntegerDouble
++;
448 } else if(*p
=='h' || *p
=='l' || *p
=='L') {
449 flags
.IntegerLength
= *p
;
451 } else if(*p
== 'I') {
452 if(*(p
+1)=='6' && *(p
+2)=='4') {
453 flags
.IntegerDouble
++;
455 } else if(*(p
+1)=='3' && *(p
+2)=='2')
457 else if(isdigit(*(p
+1)) || !*(p
+1))
462 flags
.WideString
= *p
++;
471 if(flags
.Format
== 's' || flags
.Format
== 'S') {
472 i
= FUNC_NAME(pf_handle_string
)(pf_puts
, puts_ctx
,
473 pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
,
474 -1, &flags
, locinfo
);
475 } else if(flags
.Format
== 'c' || flags
.Format
== 'C') {
476 int ch
= pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
;
479 FIXME("multibyte characters printing not supported\n");
481 i
= FUNC_NAME(pf_handle_string
)(pf_puts
, puts_ctx
, &ch
, 1, &flags
, locinfo
);
482 } else if(flags
.Format
== 'p') {
486 flags
.Precision
= 2*sizeof(void*);
487 FUNC_NAME(pf_integer_conv
)(buf
, sizeof(buf
)/sizeof(APICHAR
), &flags
,
488 (ULONG_PTR
)pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
);
493 i
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, buf
, -1, &flags
, locinfo
);
495 i
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, buf
, -1, &flags
, locinfo
);
497 } else if(flags
.Format
== 'n') {
500 if(!n_format_enabled
) {
501 MSVCRT_INVALID_PMT("\'n\' format specifier disabled", MSVCRT_EINVAL
);
505 used
= pf_args(args_ctx
, pos
, VT_PTR
, valist
).get_ptr
;
508 } else if(flags
.Format
&& strchr("diouxX", flags
.Format
)) {
512 /* 0 padding is added after '0x' if Alternate flag is in use */
513 if((flags
.Format
=='x' || flags
.Format
=='X') && flags
.PadZero
&& flags
.Alternate
514 && !flags
.LeftAlign
&& flags
.Precision
<flags
.FieldLength
-2)
515 flags
.Precision
= flags
.FieldLength
- 2;
517 max_len
= (flags
.FieldLength
>flags
.Precision
? flags
.FieldLength
: flags
.Precision
) + 10;
518 if(max_len
> sizeof(buf
)/sizeof(APICHAR
))
519 tmp
= HeapAlloc(GetProcessHeap(), 0, max_len
);
523 if(flags
.IntegerDouble
)
524 FUNC_NAME(pf_integer_conv
)(tmp
, max_len
, &flags
, pf_args(args_ctx
, pos
,
525 VT_I8
, valist
).get_longlong
);
526 else if(flags
.Format
=='d' || flags
.Format
=='i')
527 FUNC_NAME(pf_integer_conv
)(tmp
, max_len
, &flags
, flags
.IntegerLength
!='h' ?
528 pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
:
529 (short)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
);
531 FUNC_NAME(pf_integer_conv
)(tmp
, max_len
, &flags
, flags
.IntegerLength
!='h' ?
532 (unsigned)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
:
533 (unsigned short)pf_args(args_ctx
, pos
, VT_INT
, valist
).get_int
);
536 i
= FUNC_NAME(pf_output_format_wstr
)(pf_puts
, puts_ctx
, tmp
, -1, &flags
, locinfo
);
538 i
= FUNC_NAME(pf_output_format_str
)(pf_puts
, puts_ctx
, tmp
, -1, &flags
, locinfo
);
541 HeapFree(GetProcessHeap(), 0, tmp
);
542 } else if(flags
.Format
&& strchr("aeEfgG", flags
.Format
)) {
543 char float_fmt
[20], buf_a
[32], *tmp
= buf_a
, *decimal_point
;
544 int max_len
= (flags
.FieldLength
>flags
.Precision
? flags
.FieldLength
: flags
.Precision
) + 10;
545 double val
= pf_args(args_ctx
, pos
, VT_R8
, valist
).get_double
;
547 if(flags
.Format
=='f') {
548 if(val
>-10.0 && val
<10.0)
551 i
= 1 + log10(val
<0 ? -val
: val
);
552 /* Default precision is 6, additional space for sign, separator and nullbyte is required */
553 i
+= (flags
.Precision
==-1 ? 6 : flags
.Precision
) + 3;
559 if(max_len
> sizeof(buf_a
))
560 tmp
= HeapAlloc(GetProcessHeap(), 0, max_len
);
564 FUNC_NAME(pf_rebuild_format_string
)(float_fmt
, &flags
);
566 sprintf(tmp
, float_fmt
, val
);
567 if(toupper(flags
.Format
)=='E' || toupper(flags
.Format
)=='G')
568 FUNC_NAME(pf_fixup_exponent
)(tmp
);
570 decimal_point
= strchr(tmp
, '.');
572 *decimal_point
= *locinfo
->lconv
->decimal_point
;
574 i
= FUNC_NAME(pf_output_str
)(pf_puts
, puts_ctx
, tmp
, strlen(tmp
), locinfo
);
576 HeapFree(GetProcessHeap(), 0, tmp
);
578 if(invoke_invalid_param_handler
) {
579 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
580 *MSVCRT__errno() = MSVCRT_EINVAL
;
597 enum types_clbk_flags
{
598 TYPE_CLBK_VA_LIST
= 1,
599 TYPE_CLBK_POSITIONAL
= 2,
600 TYPE_CLBK_ERROR_POS
= 4,
601 TYPE_CLBK_ERROR_TYPE
= 8
604 /* This functions stores types of arguments. It uses args[0] internally */
605 static printf_arg
arg_clbk_type(void *ctx
, int pos
, int type
, __ms_va_list
*valist
)
607 static const printf_arg ret
;
608 printf_arg
*args
= ctx
;
611 args
[0].get_int
|= TYPE_CLBK_VA_LIST
;
614 args
[0].get_int
|= TYPE_CLBK_POSITIONAL
;
616 if(pos
<1 || pos
>MSVCRT__ARGMAX
)
617 args
[0].get_int
|= TYPE_CLBK_ERROR_POS
;
618 else if(args
[pos
].get_int
&& args
[pos
].get_int
!=type
)
619 args
[0].get_int
|= TYPE_CLBK_ERROR_TYPE
;
621 args
[pos
].get_int
= type
;
627 static int FUNC_NAME(create_positional_ctx
)(void *args_ctx
, const APICHAR
*format
, __ms_va_list valist
)
629 struct FUNC_NAME(_str_ctx
) puts_ctx
= {INT_MAX
, NULL
};
630 printf_arg
*args
= args_ctx
;
633 i
= FUNC_NAME(pf_printf
)(FUNC_NAME(puts_clbk_str
), &puts_ctx
, format
, NULL
, TRUE
, FALSE
,
634 arg_clbk_type
, args_ctx
, NULL
);
638 if(args
[0].get_int
==0 || args
[0].get_int
==TYPE_CLBK_VA_LIST
)
640 if(args
[0].get_int
!= TYPE_CLBK_POSITIONAL
)
643 for(i
=MSVCRT__ARGMAX
; i
>0; i
--)
647 for(j
=1; j
<=i
; j
++) {
648 switch(args
[j
].get_int
) {
650 args
[j
].get_longlong
= va_arg(valist
, LONGLONG
);
653 args
[j
].get_int
= va_arg(valist
, int);
656 args
[j
].get_double
= va_arg(valist
, double);
659 args
[j
].get_ptr
= va_arg(valist
, void*);