2 * ntdll printf functions
4 * Copyright 1999, 2009 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
34 #include "ntdll_misc.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
40 static const SIZE_T size_max
= ~(SIZE_T
)0 >> 1;
42 /* FIXME: convert sizes to SIZE_T etc. */
44 typedef struct pf_output_t
55 typedef struct pf_flags_t
57 char Sign
, LeftAlign
, Alternate
, PadZero
;
58 int FieldLength
, Precision
;
59 char IntegerLength
, IntegerDouble
;
65 * writes a string of characters to the output
66 * returns -1 if the string doesn't fit in the output buffer
67 * return the length of the string if all characters were written
69 static inline int pf_output_stringW( pf_output
*out
, LPCWSTR str
, int len
)
71 int space
= out
->len
- out
->used
;
77 LPWSTR p
= out
->buf
.W
+ out
->used
;
84 memcpy( p
, str
, len
*sizeof(WCHAR
) );
88 memcpy( p
, str
, space
*sizeof(WCHAR
) );
92 LPSTR p
= out
->buf
.A
+ out
->used
;
95 RtlUnicodeToMultiByteSize( &n
, str
, len
* sizeof(WCHAR
) );
102 RtlUnicodeToMultiByteN( p
, n
, NULL
, str
, len
* sizeof(WCHAR
) );
105 if (space
> 0) RtlUnicodeToMultiByteN( p
, space
, NULL
, str
, len
* sizeof(WCHAR
) );
110 static inline int pf_output_stringA( pf_output
*out
, LPCSTR str
, int len
)
112 int space
= out
->len
- out
->used
;
118 LPSTR p
= out
->buf
.A
+ out
->used
;
125 memcpy( p
, str
, len
);
129 memcpy( p
, str
, space
);
133 LPWSTR p
= out
->buf
.W
+ out
->used
;
136 RtlMultiByteToUnicodeSize( &n
, str
, len
);
137 out
->used
+= n
/ sizeof(WCHAR
);
141 if (space
>= n
/ sizeof(WCHAR
))
143 RtlMultiByteToUnicodeN( p
, n
, NULL
, str
, len
);
146 if (space
> 0) RtlMultiByteToUnicodeN( p
, space
* sizeof(WCHAR
), NULL
, str
, len
);
151 /* pf_fill: takes care of signs, alignment, zero and field padding */
152 static inline int pf_fill( pf_output
*out
, int len
, pf_flags
*flags
, char left
)
156 if( flags
->Sign
&& !( flags
->Format
== 'd' || flags
->Format
== 'i' ) )
159 if( left
&& flags
->Sign
)
161 flags
->FieldLength
--;
163 r
= pf_output_stringA( out
, &flags
->Sign
, 1 );
166 if( ( !left
&& flags
->LeftAlign
) ||
167 ( left
&& !flags
->LeftAlign
))
169 for( i
=0; (i
<(flags
->FieldLength
-len
)) && (r
>=0); i
++ )
171 if( left
&& flags
->PadZero
)
172 r
= pf_output_stringA( out
, "0", 1 );
174 r
= pf_output_stringA( out
, " ", 1 );
178 if (left
&& flags
->Sign
&& !flags
->PadZero
&& r
>= 0)
179 r
= pf_output_stringA( out
, &flags
->Sign
, 1 );
184 static inline int pf_output_format_W( pf_output
*out
, LPCWSTR str
,
185 int len
, pf_flags
*flags
)
190 len
= strlenW( str
);
192 if (flags
->Precision
>= 0 && flags
->Precision
< len
)
193 len
= flags
->Precision
;
195 r
= pf_fill( out
, len
, flags
, 1 );
198 r
= pf_output_stringW( out
, str
, len
);
201 r
= pf_fill( out
, len
, flags
, 0 );
206 static inline int pf_output_format_A( pf_output
*out
, LPCSTR str
,
207 int len
, pf_flags
*flags
)
214 if (flags
->Precision
>= 0 && flags
->Precision
< len
)
215 len
= flags
->Precision
;
217 r
= pf_fill( out
, len
, flags
, 1 );
220 r
= pf_output_stringA( out
, str
, len
);
223 r
= pf_fill( out
, len
, flags
, 0 );
228 static int pf_handle_string_format( pf_output
*out
, const void* str
, int len
,
229 pf_flags
*flags
, BOOL capital_letter
)
231 if(str
== NULL
) /* catch NULL pointer */
232 return pf_output_format_A( out
, "(null)", -1, flags
);
234 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
235 if(flags
->WideString
|| flags
->IntegerLength
== 'l')
236 return pf_output_format_W( out
, str
, len
, flags
);
237 if(flags
->IntegerLength
== 'h')
238 return pf_output_format_A( out
, str
, len
, flags
);
240 /* %s,%c -> chars in ansi functions & wchars in unicode
241 * %S,%C -> wchars in ansi functions & chars in unicode */
242 if( capital_letter
== out
->unicode
) /* either both TRUE or both FALSE */
243 return pf_output_format_A( out
, str
, len
, flags
);
245 return pf_output_format_W( out
, str
, len
, flags
);
248 static inline BOOL
pf_is_integer_format( char fmt
)
250 static const char float_fmts
[] = "diouxX";
253 return strchr( float_fmts
, fmt
) != 0;
256 static inline BOOL
pf_is_double_format( char fmt
)
258 static const char float_fmts
[] = "aeEfgG";
261 return strchr( float_fmts
, fmt
) != 0;
264 static inline BOOL
pf_is_valid_format( char fmt
)
266 static const char float_fmts
[] = "acCdeEfgGinouxX";
269 return strchr( float_fmts
, fmt
) != 0;
272 static void pf_rebuild_format_string( char *p
, pf_flags
*flags
)
277 if( flags
->LeftAlign
)
278 *p
++ = flags
->LeftAlign
;
279 if( flags
->Alternate
)
280 *p
++ = flags
->Alternate
;
282 *p
++ = flags
->PadZero
;
283 if( flags
->FieldLength
)
285 sprintf(p
, "%d", flags
->FieldLength
);
288 if( flags
->Precision
>= 0 )
290 sprintf(p
, ".%d", flags
->Precision
);
293 *p
++ = flags
->Format
;
297 /* pf_integer_conv: prints x to buf, including alternate formats and
298 additional precision digits, but not field characters or the sign */
299 static void pf_integer_conv( char *buf
, int buf_len
, pf_flags
*flags
,
306 char number
[40], *tmp
= number
;
308 if( buf_len
> sizeof number
)
310 if (!(tmp
= RtlAllocateHeap( GetProcessHeap(), 0, buf_len
)))
318 if( flags
->Format
== 'o' )
320 else if( flags
->Format
== 'x' || flags
->Format
== 'X' )
323 if( flags
->Format
== 'X' )
324 digits
= "0123456789ABCDEFX";
326 digits
= "0123456789abcdefx";
328 if( x
< 0 && ( flags
->Format
== 'd' || flags
->Format
== 'i' ) )
334 /* Do conversion (backwards) */
336 if( x
== 0 && flags
->Precision
)
341 j
= (ULONGLONG
) x
% base
;
342 x
= (ULONGLONG
) x
/ base
;
343 tmp
[i
++] = digits
[j
];
345 k
= flags
->Precision
- i
;
348 if( flags
->Alternate
)
352 tmp
[i
++] = digits
[16];
355 else if( base
== 8 && tmp
[i
-1] != '0' )
359 /* Reverse for buf */
365 /* Adjust precision so pf_fill won't truncate the number later */
366 flags
->Precision
= strlen( buf
);
369 RtlFreeHeap( GetProcessHeap(), 0, tmp
);
374 /* pf_fixup_exponent: convert a string containing a 2 digit exponent
375 to 3 digits, accounting for padding, in place. Needed to match
376 the native printf's which always use 3 digits. */
377 static void pf_fixup_exponent( char *buf
)
381 while (tmp
[0] && toupper(tmp
[0]) != 'E')
384 if (tmp
[0] && (tmp
[1] == '+' || tmp
[1] == '-') &&
385 isdigit(tmp
[2]) && isdigit(tmp
[3]))
390 return; /* Exponent already 3 digits */
392 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
400 /* We didn't expand into trailing space, so this string isn't left
401 * justified. Terminate the string and strip a ' ' at the start of
402 * the string if there is one (as there may be if the string is
407 memmove(buf
, buf
+ 1, (tmp
- buf
) + 3);
409 /* Otherwise, we expanded into trailing space -> nothing to do */
413 /*********************************************************************
414 * pf_vsnprintf (INTERNAL)
416 * implements both A and W vsnprintf functions
418 static int pf_vsnprintf( pf_output
*out
, const WCHAR
*format
, __ms_va_list valist
)
421 LPCWSTR q
, p
= format
;
424 TRACE("format is %s\n",debugstr_w(format
));
427 q
= strchrW( p
, '%' );
429 /* there are no % characters left: output the rest of the string */
432 r
= pf_output_stringW(out
, p
, -1);
439 /* there are characters before the %: output them */
442 r
= pf_output_stringW(out
, p
, q
- p
);
448 /* we must be at a % now, skip over it */
452 /* output a single % character */
455 r
= pf_output_stringW(out
, p
++, 1);
461 /* parse the flags */
462 memset( &flags
, 0, sizeof flags
);
465 if( *p
== '+' || *p
== ' ' )
467 if ( flags
.Sign
!= '+' )
471 flags
.LeftAlign
= *p
;
475 flags
.Alternate
= *p
;
481 /* deal with the field width specifier */
482 flags
.FieldLength
= 0;
485 flags
.FieldLength
= va_arg( valist
, int );
486 if (flags
.FieldLength
< 0)
488 flags
.LeftAlign
= '-';
489 flags
.FieldLength
= -flags
.FieldLength
;
493 else while( isdigit(*p
) )
495 flags
.FieldLength
*= 10;
496 flags
.FieldLength
+= *p
++ - '0';
499 /* deal with precision */
500 flags
.Precision
= -1;
507 flags
.Precision
= va_arg( valist
, int );
510 else while( isdigit(*p
) )
512 flags
.Precision
*= 10;
513 flags
.Precision
+= *p
++ - '0';
517 /* deal with integer width modifier */
520 if( *p
== 'h' || *p
== 'l' || *p
== 'L' )
522 flags
.IntegerLength
= *p
;
527 if( *(p
+1) == '6' && *(p
+2) == '4' )
529 flags
.IntegerDouble
++;
532 else if( *(p
+1) == '3' && *(p
+2) == '2' )
534 else if( isdigit(*(p
+1)) || *(p
+1) == 0 )
540 flags
.WideString
= *p
++;
550 if (flags
.Format
== '$')
552 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format
));
555 /* output a string */
556 if( flags
.Format
== 's' || flags
.Format
== 'S' )
557 r
= pf_handle_string_format( out
, va_arg(valist
, const void*), -1,
558 &flags
, (flags
.Format
== 'S') );
560 /* output a single character */
561 else if( flags
.Format
== 'c' || flags
.Format
== 'C' )
563 INT ch
= va_arg( valist
, int );
565 r
= pf_handle_string_format( out
, &ch
, 1, &flags
, (flags
.Format
== 'C') );
568 /* output a pointer */
569 else if( flags
.Format
== 'p' )
572 void *ptr
= va_arg( valist
, void * );
575 if( flags
.Alternate
)
576 sprintf(pointer
, "0X%0*lX", 2 * (int)sizeof(ptr
), (ULONG_PTR
)ptr
);
578 sprintf(pointer
, "%0*lX", 2 * (int)sizeof(ptr
), (ULONG_PTR
)ptr
);
579 r
= pf_output_format_A( out
, pointer
, -1, &flags
);
583 else if( flags
.Format
== 'n' )
585 int *x
= va_arg(valist
, int *);
589 /* deal with 64-bit integers */
590 else if( pf_is_integer_format( flags
.Format
) && flags
.IntegerDouble
)
592 char number
[40], *x
= number
;
594 /* Estimate largest possible required buffer size:
595 * Chooses the larger of the field or precision
596 * Includes extra bytes: 1 byte for null, 1 byte for sign,
597 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
598 for a decimal, and 1 byte for an additional float digit. */
599 int x_len
= ((flags
.FieldLength
> flags
.Precision
) ?
600 flags
.FieldLength
: flags
.Precision
) + 10;
602 if( x_len
>= sizeof number
)
603 if (!(x
= RtlAllocateHeap( GetProcessHeap(), 0, x_len
)))
606 pf_integer_conv( x
, x_len
, &flags
, va_arg(valist
, LONGLONG
) );
608 r
= pf_output_format_A( out
, x
, -1, &flags
);
610 RtlFreeHeap( GetProcessHeap(), 0, x
);
613 /* deal with integers and floats using libc's printf */
614 else if( pf_is_valid_format( flags
.Format
) )
616 char fmt
[20], number
[40], *x
= number
;
618 /* Estimate largest possible required buffer size:
619 * Chooses the larger of the field or precision
620 * Includes extra bytes: 1 byte for null, 1 byte for sign,
621 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
622 for a decimal, and 1 byte for an additional float digit. */
623 int x_len
= ((flags
.FieldLength
> flags
.Precision
) ?
624 flags
.FieldLength
: flags
.Precision
) + 10;
626 if( x_len
>= sizeof number
)
627 if (!(x
= RtlAllocateHeap( GetProcessHeap(), 0, x_len
)))
630 pf_rebuild_format_string( fmt
, &flags
);
632 if( pf_is_double_format( flags
.Format
) )
634 sprintf( x
, fmt
, va_arg(valist
, double) );
635 if (toupper(flags
.Format
) == 'E' || toupper(flags
.Format
) == 'G')
636 pf_fixup_exponent( x
);
639 sprintf( x
, fmt
, va_arg(valist
, int) );
641 r
= pf_output_stringA( out
, x
, -1 );
643 RtlFreeHeap( GetProcessHeap(), 0, x
);
653 /* check we reached the end, and null terminate the string */
655 pf_output_stringW( out
, p
, 1 );
657 return out
->used
- 1;
661 /*********************************************************************
662 * _vsnprintf (NTDLL.@)
664 int CDECL
NTDLL__vsnprintf( char *str
, SIZE_T len
, const char *format
, __ms_va_list args
)
667 LPWSTR formatW
= NULL
;
678 RtlMultiByteToUnicodeSize( &sz
, format
, strlen(format
) + 1 );
679 if (!(formatW
= RtlAllocateHeap( GetProcessHeap(), 0, sz
))) return -1;
680 RtlMultiByteToUnicodeN( formatW
, sz
, NULL
, format
, strlen(format
) + 1 );
682 r
= pf_vsnprintf( &out
, formatW
, args
);
683 RtlFreeHeap( GetProcessHeap(), 0, formatW
);
688 /***********************************************************************
689 * _vsnwprintf (NTDLL.@)
691 int CDECL
NTDLL__vsnwprintf( WCHAR
*str
, SIZE_T len
, const WCHAR
*format
, __ms_va_list args
)
700 return pf_vsnprintf( &out
, format
, args
);
704 /*********************************************************************
705 * _snprintf (NTDLL.@)
707 int CDECL
NTDLL__snprintf( char *str
, SIZE_T len
, const char *format
, ... )
712 __ms_va_start( valist
, format
);
713 ret
= NTDLL__vsnprintf( str
, len
, format
, valist
);
714 __ms_va_end( valist
);
719 /***********************************************************************
720 * _snwprintf (NTDLL.@)
722 int CDECL
NTDLL__snwprintf( WCHAR
*str
, SIZE_T len
, const WCHAR
*format
, ... )
727 __ms_va_start(valist
, format
);
728 ret
= NTDLL__vsnwprintf( str
, len
, format
, valist
);
734 /*********************************************************************
737 int CDECL
NTDLL_vsprintf( char *str
, const char *format
, __ms_va_list args
)
739 return NTDLL__vsnprintf( str
, size_max
, format
, args
);
743 /*********************************************************************
746 int CDECL
NTDLL_sprintf( char *str
, const char *format
, ... )
751 __ms_va_start( valist
, format
);
752 ret
= NTDLL__vsnprintf( str
, size_max
, format
, valist
);
753 __ms_va_end( valist
);
758 /***********************************************************************
761 int CDECL
NTDLL_swprintf( WCHAR
*str
, const WCHAR
*format
, ... )
766 __ms_va_start(valist
, format
);
767 ret
= NTDLL__vsnwprintf( str
, size_max
, format
, valist
);