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 static inline BOOL
isDigit(WCHAR c
)
415 return c
>= '0' && c
<= '9';
418 /*********************************************************************
419 * pf_vsnprintf (INTERNAL)
421 * implements both A and W vsnprintf functions
423 static int pf_vsnprintf( pf_output
*out
, const WCHAR
*format
, __ms_va_list valist
)
426 LPCWSTR q
, p
= format
;
429 TRACE("format is %s\n",debugstr_w(format
));
432 q
= strchrW( p
, '%' );
434 /* there are no % characters left: output the rest of the string */
437 r
= pf_output_stringW(out
, p
, -1);
444 /* there are characters before the %: output them */
447 r
= pf_output_stringW(out
, p
, q
- p
);
453 /* we must be at a % now, skip over it */
457 /* output a single % character */
460 r
= pf_output_stringW(out
, p
++, 1);
466 /* parse the flags */
467 memset( &flags
, 0, sizeof flags
);
470 if( *p
== '+' || *p
== ' ' )
472 if ( flags
.Sign
!= '+' )
476 flags
.LeftAlign
= *p
;
480 flags
.Alternate
= *p
;
486 /* deal with the field width specifier */
487 flags
.FieldLength
= 0;
490 flags
.FieldLength
= va_arg( valist
, int );
491 if (flags
.FieldLength
< 0)
493 flags
.LeftAlign
= '-';
494 flags
.FieldLength
= -flags
.FieldLength
;
498 else while( isDigit(*p
) )
500 flags
.FieldLength
*= 10;
501 flags
.FieldLength
+= *p
++ - '0';
504 /* deal with precision */
505 flags
.Precision
= -1;
512 flags
.Precision
= va_arg( valist
, int );
515 else while( isDigit(*p
) )
517 flags
.Precision
*= 10;
518 flags
.Precision
+= *p
++ - '0';
522 /* deal with integer width modifier */
525 if( *p
== 'h' || *p
== 'l' || *p
== 'L' )
527 flags
.IntegerLength
= *p
;
532 if( *(p
+1) == '6' && *(p
+2) == '4' )
534 flags
.IntegerDouble
++;
537 else if( *(p
+1) == '3' && *(p
+2) == '2' )
539 else if( isDigit(*(p
+1)) || *(p
+1) == 0 )
545 flags
.WideString
= *p
++;
555 if (flags
.Format
== '$')
557 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format
));
560 /* output a string */
561 if( flags
.Format
== 's' || flags
.Format
== 'S' )
562 r
= pf_handle_string_format( out
, va_arg(valist
, const void*), -1,
563 &flags
, (flags
.Format
== 'S') );
565 /* output a single character */
566 else if( flags
.Format
== 'c' || flags
.Format
== 'C' )
568 INT ch
= va_arg( valist
, int );
570 r
= pf_handle_string_format( out
, &ch
, 1, &flags
, (flags
.Format
== 'C') );
573 /* output a pointer */
574 else if( flags
.Format
== 'p' )
577 void *ptr
= va_arg( valist
, void * );
580 if( flags
.Alternate
)
581 sprintf(pointer
, "0X%0*lX", 2 * (int)sizeof(ptr
), (ULONG_PTR
)ptr
);
583 sprintf(pointer
, "%0*lX", 2 * (int)sizeof(ptr
), (ULONG_PTR
)ptr
);
584 r
= pf_output_format_A( out
, pointer
, -1, &flags
);
588 else if( flags
.Format
== 'n' )
590 int *x
= va_arg(valist
, int *);
594 /* deal with 64-bit integers */
595 else if( pf_is_integer_format( flags
.Format
) && flags
.IntegerDouble
)
597 char number
[40], *x
= number
;
599 /* Estimate largest possible required buffer size:
600 * Chooses the larger of the field or precision
601 * Includes extra bytes: 1 byte for null, 1 byte for sign,
602 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
603 for a decimal, and 1 byte for an additional float digit. */
604 int x_len
= ((flags
.FieldLength
> flags
.Precision
) ?
605 flags
.FieldLength
: flags
.Precision
) + 10;
607 if( x_len
>= sizeof number
)
608 if (!(x
= RtlAllocateHeap( GetProcessHeap(), 0, x_len
)))
611 pf_integer_conv( x
, x_len
, &flags
, va_arg(valist
, LONGLONG
) );
613 r
= pf_output_format_A( out
, x
, -1, &flags
);
615 RtlFreeHeap( GetProcessHeap(), 0, x
);
618 /* deal with integers and floats using libc's printf */
619 else if( pf_is_valid_format( flags
.Format
) )
621 char fmt
[20], number
[40], *x
= number
;
623 /* Estimate largest possible required buffer size:
624 * Chooses the larger of the field or precision
625 * Includes extra bytes: 1 byte for null, 1 byte for sign,
626 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
627 for a decimal, and 1 byte for an additional float digit. */
628 int x_len
= ((flags
.FieldLength
> flags
.Precision
) ?
629 flags
.FieldLength
: flags
.Precision
) + 10;
631 if( x_len
>= sizeof number
)
632 if (!(x
= RtlAllocateHeap( GetProcessHeap(), 0, x_len
)))
635 pf_rebuild_format_string( fmt
, &flags
);
637 if( pf_is_double_format( flags
.Format
) )
639 sprintf( x
, fmt
, va_arg(valist
, double) );
640 if (toupper(flags
.Format
) == 'E' || toupper(flags
.Format
) == 'G')
641 pf_fixup_exponent( x
);
644 sprintf( x
, fmt
, va_arg(valist
, int) );
646 r
= pf_output_stringA( out
, x
, -1 );
648 RtlFreeHeap( GetProcessHeap(), 0, x
);
658 /* check we reached the end, and null terminate the string */
660 pf_output_stringW( out
, p
, 1 );
662 return out
->used
- 1;
666 /*********************************************************************
667 * _vsnprintf (NTDLL.@)
669 int CDECL
NTDLL__vsnprintf( char *str
, SIZE_T len
, const char *format
, __ms_va_list args
)
672 LPWSTR formatW
= NULL
;
683 RtlMultiByteToUnicodeSize( &sz
, format
, strlen(format
) + 1 );
684 if (!(formatW
= RtlAllocateHeap( GetProcessHeap(), 0, sz
))) return -1;
685 RtlMultiByteToUnicodeN( formatW
, sz
, NULL
, format
, strlen(format
) + 1 );
687 r
= pf_vsnprintf( &out
, formatW
, args
);
688 RtlFreeHeap( GetProcessHeap(), 0, formatW
);
693 /***********************************************************************
694 * _vsnwprintf (NTDLL.@)
696 int CDECL
NTDLL__vsnwprintf( WCHAR
*str
, SIZE_T len
, const WCHAR
*format
, __ms_va_list args
)
705 return pf_vsnprintf( &out
, format
, args
);
709 /*********************************************************************
710 * _snprintf (NTDLL.@)
712 int WINAPIV
NTDLL__snprintf( char *str
, SIZE_T len
, const char *format
, ... )
717 __ms_va_start( valist
, format
);
718 ret
= NTDLL__vsnprintf( str
, len
, format
, valist
);
719 __ms_va_end( valist
);
724 /***********************************************************************
725 * _snwprintf (NTDLL.@)
727 int WINAPIV
NTDLL__snwprintf( WCHAR
*str
, SIZE_T len
, const WCHAR
*format
, ... )
732 __ms_va_start(valist
, format
);
733 ret
= NTDLL__vsnwprintf( str
, len
, format
, valist
);
739 /*********************************************************************
742 int CDECL
NTDLL_vsprintf( char *str
, const char *format
, __ms_va_list args
)
744 return NTDLL__vsnprintf( str
, size_max
, format
, args
);
748 /*********************************************************************
751 int WINAPIV
NTDLL_sprintf( char *str
, const char *format
, ... )
756 __ms_va_start( valist
, format
);
757 ret
= NTDLL__vsnprintf( str
, size_max
, format
, valist
);
758 __ms_va_end( valist
);
763 /***********************************************************************
766 int WINAPIV
NTDLL_swprintf( WCHAR
*str
, const WCHAR
*format
, ... )
771 __ms_va_start(valist
, format
);
772 ret
= NTDLL__vsnwprintf( str
, size_max
, format
, valist
);