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;
81 memcpy( p, str, len*sizeof(WCHAR) );
86 memcpy( p, str, space*sizeof(WCHAR) );
91 LPSTR p = out->buf.A + out->used;
94 RtlUnicodeToMultiByteSize( &n, str, len * sizeof(WCHAR) );
97 RtlUnicodeToMultiByteN( p, n, NULL, str, len * sizeof(WCHAR) );
101 if (space > 0) RtlUnicodeToMultiByteN( p, space, NULL, str, len * sizeof(WCHAR) );
107 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
109 int space = out->len - out->used;
115 LPSTR p = out->buf.A + out->used;
119 memcpy( p, str, len );
124 memcpy( p, str, space );
129 LPWSTR p = out->buf.W + out->used;
132 RtlMultiByteToUnicodeSize( &n, str, len );
133 if (space >= n / sizeof(WCHAR))
135 RtlMultiByteToUnicodeN( p, n, NULL, str, len );
136 out->used += n / sizeof(WCHAR);
139 if (space > 0) RtlMultiByteToUnicodeN( p, space * sizeof(WCHAR), NULL, str, len );
145 /* pf_fill: takes care of signs, alignment, zero and field padding */
146 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
150 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
153 if( left && flags->Sign )
155 flags->FieldLength--;
157 r = pf_output_stringA( out, &flags->Sign, 1 );
160 if( ( !left && flags->LeftAlign ) ||
161 ( left && !flags->LeftAlign ))
163 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
165 if( left && flags->PadZero )
166 r = pf_output_stringA( out, "0", 1 );
168 r = pf_output_stringA( out, " ", 1 );
172 if( left && flags->Sign && !flags->PadZero )
173 r = pf_output_stringA( out, &flags->Sign, 1 );
178 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
179 int len, pf_flags *flags )
184 len = strlenW( str );
186 if (flags->Precision >= 0 && flags->Precision < len)
187 len = flags->Precision;
189 r = pf_fill( out, len, flags, 1 );
192 r = pf_output_stringW( out, str, len );
195 r = pf_fill( out, len, flags, 0 );
200 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
201 int len, pf_flags *flags )
208 if (flags->Precision >= 0 && flags->Precision < len)
209 len = flags->Precision;
211 r = pf_fill( out, len, flags, 1 );
214 r = pf_output_stringA( out, str, len );
217 r = pf_fill( out, len, flags, 0 );
222 static int pf_handle_string_format( pf_output *out, const void* str, int len,
223 pf_flags *flags, BOOL capital_letter)
225 if(str == NULL) /* catch NULL pointer */
226 return pf_output_format_A( out, "(null)", -1, flags);
228 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
229 if(flags->WideString || flags->IntegerLength == 'l')
230 return pf_output_format_W( out, str, len, flags);
231 if(flags->IntegerLength == 'h')
232 return pf_output_format_A( out, str, len, flags);
234 /* %s,%c -> chars in ansi functions & wchars in unicode
235 * %S,%C -> wchars in ansi functions & chars in unicode */
236 if( capital_letter == out->unicode) /* either both TRUE or both FALSE */
237 return pf_output_format_A( out, str, len, flags);
239 return pf_output_format_W( out, str, len, flags);
242 static inline BOOL pf_is_integer_format( char fmt )
244 static const char float_fmts[] = "diouxX";
247 return strchr( float_fmts, fmt ) != 0;
250 static inline BOOL pf_is_double_format( char fmt )
252 static const char float_fmts[] = "aeEfgG";
255 return strchr( float_fmts, fmt ) != 0;
258 static inline BOOL pf_is_valid_format( char fmt )
260 static const char float_fmts[] = "acCdeEfgGinouxX";
263 return strchr( float_fmts, fmt ) != 0;
266 static void pf_rebuild_format_string( char *p, pf_flags *flags )
271 if( flags->LeftAlign )
272 *p++ = flags->LeftAlign;
273 if( flags->Alternate )
274 *p++ = flags->Alternate;
276 *p++ = flags->PadZero;
277 if( flags->FieldLength )
279 sprintf(p, "%d", flags->FieldLength);
282 if( flags->Precision >= 0 )
284 sprintf(p, ".%d", flags->Precision);
287 *p++ = flags->Format;
291 /* pf_integer_conv: prints x to buf, including alternate formats and
292 additional precision digits, but not field characters or the sign */
293 static void pf_integer_conv( char *buf, int buf_len, pf_flags *flags,
300 char number[40], *tmp = number;
302 if( buf_len > sizeof number )
303 tmp = RtlAllocateHeap( GetProcessHeap(), 0, buf_len );
306 if( flags->Format == 'o' )
308 else if( flags->Format == 'x' || flags->Format == 'X' )
311 if( flags->Format == 'X' )
312 digits = "0123456789ABCDEFX";
314 digits = "0123456789abcdefx";
316 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
322 /* Do conversion (backwards) */
324 if( x == 0 && flags->Precision )
329 j = (ULONGLONG) x % base;
330 x = (ULONGLONG) x / base;
331 tmp[i++] = digits[j];
333 k = flags->Precision - i;
336 if( flags->Alternate )
340 tmp[i++] = digits[16];
343 else if( base == 8 && tmp[i-1] != '0' )
347 /* Reverse for buf */
353 /* Adjust precision so pf_fill won't truncate the number later */
354 flags->Precision = strlen( buf );
357 RtlFreeHeap( GetProcessHeap(), 0, tmp );
362 /* pf_fixup_exponent: convert a string containing a 2 digit exponent
363 to 3 digits, accounting for padding, in place. Needed to match
364 the native printf's which always use 3 digits. */
365 static void pf_fixup_exponent( char *buf )
369 while (tmp[0] && toupper(tmp[0]) != 'E')
372 if (tmp[0] && (tmp[1] == '+' || tmp[1] == '-') &&
373 isdigit(tmp[2]) && isdigit(tmp[3]))
378 return; /* Exponent already 3 digits */
380 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
388 /* We didn't expand into trailing space, so this string isn't left
389 * justified. Terminate the string and strip a ' ' at the start of
390 * the string if there is one (as there may be if the string is
395 memmove(buf, buf + 1, (tmp - buf) + 3);
397 /* Otherwise, we expanded into trailing space -> nothing to do */
401 /*********************************************************************
402 * pf_vsnprintf (INTERNAL)
404 * implements both A and W vsnprintf functions
406 static int pf_vsnprintf( pf_output *out, const WCHAR *format, __ms_va_list valist )
409 LPCWSTR q, p = format;
412 TRACE("format is %s\n",debugstr_w(format));
415 q = strchrW( p, '%' );
417 /* there's no % characters left, output the rest of the string */
420 r = pf_output_stringW(out, p, -1);
427 /* there's characters before the %, output them */
430 r = pf_output_stringW(out, p, q - p);
436 /* we must be at a % now, skip over it */
440 /* output a single % character */
443 r = pf_output_stringW(out, p++, 1);
449 /* parse the flags */
450 memset( &flags, 0, sizeof flags );
453 if( *p == '+' || *p == ' ' )
455 if ( flags.Sign != '+' )
459 flags.LeftAlign = *p;
463 flags.Alternate = *p;
469 /* deal with the field width specifier */
470 flags.FieldLength = 0;
473 flags.FieldLength = va_arg( valist, int );
474 if (flags.FieldLength < 0)
476 flags.LeftAlign = '-';
477 flags.FieldLength = -flags.FieldLength;
481 else while( isdigit(*p) )
483 flags.FieldLength *= 10;
484 flags.FieldLength += *p++ - '0';
487 /* deal with precision */
488 flags.Precision = -1;
495 flags.Precision = va_arg( valist, int );
498 else while( isdigit(*p) )
500 flags.Precision *= 10;
501 flags.Precision += *p++ - '0';
505 /* deal with integer width modifier */
508 if( *p == 'h' || *p == 'l' || *p == 'L' )
510 flags.IntegerLength = *p;
515 if( *(p+1) == '6' && *(p+2) == '4' )
517 flags.IntegerDouble++;
520 else if( *(p+1) == '3' && *(p+2) == '2' )
522 else if( isdigit(*(p+1)) || *(p+1) == 0 )
528 flags.WideString = *p++;
538 if (flags.Format == '$')
540 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
543 /* output a string */
544 if( flags.Format == 's' || flags.Format == 'S' )
545 r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
546 &flags, (flags.Format == 'S') );
548 /* output a single character */
549 else if( flags.Format == 'c' || flags.Format == 'C' )
551 INT ch = va_arg( valist, int );
553 r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
556 /* output a pointer */
557 else if( flags.Format == 'p' )
560 void *ptr = va_arg( valist, void * );
563 if( flags.Alternate )
564 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
566 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
567 r = pf_output_format_A( out, pointer, -1, &flags );
571 else if( flags.Format == 'n' )
573 int *x = va_arg(valist, int *);
577 /* deal with 64-bit integers */
578 else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
580 char number[40], *x = number;
582 /* Estimate largest possible required buffer size:
583 * Chooses the larger of the field or precision
584 * Includes extra bytes: 1 byte for null, 1 byte for sign,
585 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
586 for a decimal, and 1 byte for an additional float digit. */
587 int x_len = ((flags.FieldLength > flags.Precision) ?
588 flags.FieldLength : flags.Precision) + 10;
590 if( x_len >= sizeof number)
591 x = RtlAllocateHeap( GetProcessHeap(), 0, x_len );
593 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
595 r = pf_output_format_A( out, x, -1, &flags );
597 RtlFreeHeap( GetProcessHeap(), 0, x );
600 /* deal with integers and floats using libc's printf */
601 else if( pf_is_valid_format( flags.Format ) )
603 char fmt[20], number[40], *x = number;
605 /* Estimate largest possible required buffer size:
606 * Chooses the larger of the field or precision
607 * Includes extra bytes: 1 byte for null, 1 byte for sign,
608 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
609 for a decimal, and 1 byte for an additional float digit. */
610 int x_len = ((flags.FieldLength > flags.Precision) ?
611 flags.FieldLength : flags.Precision) + 10;
613 if( x_len >= sizeof number)
614 x = RtlAllocateHeap( GetProcessHeap(), 0, x_len );
616 pf_rebuild_format_string( fmt, &flags );
618 if( pf_is_double_format( flags.Format ) )
620 sprintf( x, fmt, va_arg(valist, double) );
621 if (toupper(flags.Format) == 'E' || toupper(flags.Format) == 'G')
622 pf_fixup_exponent( x );
625 sprintf( x, fmt, va_arg(valist, int) );
627 r = pf_output_stringA( out, x, -1 );
629 RtlFreeHeap( GetProcessHeap(), 0, x );
639 /* check we reached the end, and null terminate the string */
641 pf_output_stringW( out, p, 1 );
643 return out->used - 1;
647 /*********************************************************************
648 * _vsnprintf (NTDLL.@)
650 int CDECL NTDLL__vsnprintf( char *str, SIZE_T len, const char *format, __ms_va_list args )
653 LPWSTR formatW = NULL;
664 RtlMultiByteToUnicodeSize( &sz, format, strlen(format) + 1 );
665 if (!(formatW = RtlAllocateHeap( GetProcessHeap(), 0, sz ))) return -1;
666 RtlMultiByteToUnicodeN( formatW, sz, NULL, format, strlen(format) + 1 );
668 r = pf_vsnprintf( &out, formatW, args );
669 RtlFreeHeap( GetProcessHeap(), 0, formatW );
674 /***********************************************************************
675 * _vsnwprintf (NTDLL.@)
677 int CDECL NTDLL__vsnwprintf( WCHAR *str, SIZE_T len, const WCHAR *format, __ms_va_list args )
686 return pf_vsnprintf( &out, format, args );
690 /*********************************************************************
691 * _snprintf (NTDLL.@)
693 int CDECL NTDLL__snprintf( char *str, SIZE_T len, const char *format, ... )
698 __ms_va_start( valist, format );
699 ret = NTDLL__vsnprintf( str, len, format, valist );
700 __ms_va_end( valist );
705 /***********************************************************************
706 * _snwprintf (NTDLL.@)
708 int CDECL NTDLL__snwprintf( WCHAR *str, SIZE_T len, const WCHAR *format, ... )
713 __ms_va_start(valist, format);
714 ret = NTDLL__vsnwprintf( str, len, format, valist );
720 /*********************************************************************
723 int CDECL NTDLL_vsprintf( char *str, const char *format, __ms_va_list args )
725 return NTDLL__vsnprintf( str, size_max, format, args );
729 /*********************************************************************
732 int CDECL NTDLL_sprintf( char *str, const char *format, ... )
737 __ms_va_start( valist, format );
738 ret = NTDLL__vsnprintf( str, size_max, format, valist );
739 __ms_va_end( valist );
744 /***********************************************************************
747 int CDECL NTDLL_swprintf( WCHAR *str, const WCHAR *format, ... )
752 __ms_va_start(valist, format);
753 ret = NTDLL__vsnwprintf( str, size_max, format, valist );