comctl32/tests: Replace asserts with regular test checks for Edit tests.
[wine.git] / dlls / ntdll / printf.c
blob8a966304633bbfd85f7e83d39e9d50b15404ab82
1 /*
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include "windef.h"
33 #include "winternl.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
46 int used;
47 int len;
48 BOOL unicode;
49 union {
50 LPWSTR W;
51 LPSTR A;
52 } buf;
53 } pf_output;
55 typedef struct pf_flags_t
57 char Sign, LeftAlign, Alternate, PadZero;
58 int FieldLength, Precision;
59 char IntegerLength, IntegerDouble;
60 char WideString;
61 char Format;
62 } pf_flags;
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;
73 if( len < 0 )
74 len = strlenW( str );
75 if( out->unicode )
77 LPWSTR p = out->buf.W + out->used;
78 out->used += len;
80 if (!out->buf.W)
81 return len;
82 if( space >= len )
84 memcpy( p, str, len*sizeof(WCHAR) );
85 return len;
87 if( space > 0 )
88 memcpy( p, str, space*sizeof(WCHAR) );
90 else
92 LPSTR p = out->buf.A + out->used;
93 ULONG n;
95 RtlUnicodeToMultiByteSize( &n, str, len * sizeof(WCHAR) );
96 out->used += n;
98 if (!out->buf.A)
99 return len;
100 if( space >= n )
102 RtlUnicodeToMultiByteN( p, n, NULL, str, len * sizeof(WCHAR) );
103 return len;
105 if (space > 0) RtlUnicodeToMultiByteN( p, space, NULL, str, len * sizeof(WCHAR) );
107 return -1;
110 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
112 int space = out->len - out->used;
114 if( len < 0 )
115 len = strlen( str );
116 if( !out->unicode )
118 LPSTR p = out->buf.A + out->used;
119 out->used += len;
121 if (!out->buf.A)
122 return len;
123 if( space >= len )
125 memcpy( p, str, len );
126 return len;
128 if( space > 0 )
129 memcpy( p, str, space );
131 else
133 LPWSTR p = out->buf.W + out->used;
134 ULONG n;
136 RtlMultiByteToUnicodeSize( &n, str, len );
137 out->used += n / sizeof(WCHAR);
139 if (!out->buf.W)
140 return len;
141 if (space >= n / sizeof(WCHAR))
143 RtlMultiByteToUnicodeN( p, n, NULL, str, len );
144 return len;
146 if (space > 0) RtlMultiByteToUnicodeN( p, space * sizeof(WCHAR), NULL, str, len );
148 return -1;
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 )
154 int i, r = 0;
156 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
157 flags->Sign = 0;
159 if( left && flags->Sign )
161 flags->FieldLength--;
162 if( flags->PadZero )
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 );
173 else
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 );
181 return r;
184 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
185 int len, pf_flags *flags )
187 int r = 0;
189 if( len < 0 )
190 len = strlenW( str );
192 if (flags->Precision >= 0 && flags->Precision < len)
193 len = flags->Precision;
195 r = pf_fill( out, len, flags, 1 );
197 if( r>=0 )
198 r = pf_output_stringW( out, str, len );
200 if( r>=0 )
201 r = pf_fill( out, len, flags, 0 );
203 return r;
206 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
207 int len, pf_flags *flags )
209 int r = 0;
211 if( len < 0 )
212 len = strlen( str );
214 if (flags->Precision >= 0 && flags->Precision < len)
215 len = flags->Precision;
217 r = pf_fill( out, len, flags, 1 );
219 if( r>=0 )
220 r = pf_output_stringA( out, str, len );
222 if( r>=0 )
223 r = pf_fill( out, len, flags, 0 );
225 return r;
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);
244 else
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";
251 if (!fmt)
252 return FALSE;
253 return strchr( float_fmts, fmt ) != 0;
256 static inline BOOL pf_is_double_format( char fmt )
258 static const char float_fmts[] = "aeEfgG";
259 if (!fmt)
260 return FALSE;
261 return strchr( float_fmts, fmt ) != 0;
264 static inline BOOL pf_is_valid_format( char fmt )
266 static const char float_fmts[] = "acCdeEfgGinouxX";
267 if (!fmt)
268 return FALSE;
269 return strchr( float_fmts, fmt ) != 0;
272 static void pf_rebuild_format_string( char *p, pf_flags *flags )
274 *p++ = '%';
275 if( flags->Sign )
276 *p++ = flags->Sign;
277 if( flags->LeftAlign )
278 *p++ = flags->LeftAlign;
279 if( flags->Alternate )
280 *p++ = flags->Alternate;
281 if( flags->PadZero )
282 *p++ = flags->PadZero;
283 if( flags->FieldLength )
285 sprintf(p, "%d", flags->FieldLength);
286 p += strlen(p);
288 if( flags->Precision >= 0 )
290 sprintf(p, ".%d", flags->Precision);
291 p += strlen(p);
293 *p++ = flags->Format;
294 *p++ = 0;
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,
300 LONGLONG x )
302 unsigned int base;
303 const char *digits;
305 int i, j, k;
306 char number[40], *tmp = number;
308 if( buf_len > sizeof number )
310 if (!(tmp = RtlAllocateHeap( GetProcessHeap(), 0, buf_len )))
312 buf[0] = '\0';
313 return;
317 base = 10;
318 if( flags->Format == 'o' )
319 base = 8;
320 else if( flags->Format == 'x' || flags->Format == 'X' )
321 base = 16;
323 if( flags->Format == 'X' )
324 digits = "0123456789ABCDEFX";
325 else
326 digits = "0123456789abcdefx";
328 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
330 x = -x;
331 flags->Sign = '-';
334 /* Do conversion (backwards) */
335 i = 0;
336 if( x == 0 && flags->Precision )
337 tmp[i++] = '0';
338 else
339 while( x != 0 )
341 j = (ULONGLONG) x % base;
342 x = (ULONGLONG) x / base;
343 tmp[i++] = digits[j];
345 k = flags->Precision - i;
346 while( k-- > 0 )
347 tmp[i++] = '0';
348 if( flags->Alternate )
350 if( base == 16 )
352 tmp[i++] = digits[16];
353 tmp[i++] = '0';
355 else if( base == 8 && tmp[i-1] != '0' )
356 tmp[i++] = '0';
359 /* Reverse for buf */
360 j = 0;
361 while( i-- > 0 )
362 buf[j++] = tmp[i];
363 buf[j] = '\0';
365 /* Adjust precision so pf_fill won't truncate the number later */
366 flags->Precision = strlen( buf );
368 if( tmp != number )
369 RtlFreeHeap( GetProcessHeap(), 0, tmp );
371 return;
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 )
379 char* tmp = buf;
381 while (tmp[0] && toupper(tmp[0]) != 'E')
382 tmp++;
384 if (tmp[0] && (tmp[1] == '+' || tmp[1] == '-') &&
385 isdigit(tmp[2]) && isdigit(tmp[3]))
387 char final;
389 if (isdigit(tmp[4]))
390 return; /* Exponent already 3 digits */
392 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
393 tmp += 2;
394 final = tmp[2];
395 tmp[2] = tmp[1];
396 tmp[1] = tmp[0];
397 tmp[0] = '0';
398 if (final == '\0')
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
403 * right justified).
405 tmp[3] = '\0';
406 if (buf[0] == ' ')
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 )
425 int r;
426 LPCWSTR q, p = format;
427 pf_flags flags;
429 TRACE("format is %s\n",debugstr_w(format));
430 while (*p)
432 q = strchrW( p, '%' );
434 /* there are no % characters left: output the rest of the string */
435 if( !q )
437 r = pf_output_stringW(out, p, -1);
438 if( r<0 )
439 return r;
440 p += r;
441 continue;
444 /* there are characters before the %: output them */
445 if( q != p )
447 r = pf_output_stringW(out, p, q - p);
448 if( r<0 )
449 return r;
450 p = q;
453 /* we must be at a % now, skip over it */
454 assert( *p == '%' );
455 p++;
457 /* output a single % character */
458 if( *p == '%' )
460 r = pf_output_stringW(out, p++, 1);
461 if( r<0 )
462 return r;
463 continue;
466 /* parse the flags */
467 memset( &flags, 0, sizeof flags );
468 while (*p)
470 if( *p == '+' || *p == ' ' )
472 if ( flags.Sign != '+' )
473 flags.Sign = *p;
475 else if( *p == '-' )
476 flags.LeftAlign = *p;
477 else if( *p == '0' )
478 flags.PadZero = *p;
479 else if( *p == '#' )
480 flags.Alternate = *p;
481 else
482 break;
483 p++;
486 /* deal with the field width specifier */
487 flags.FieldLength = 0;
488 if( *p == '*' )
490 flags.FieldLength = va_arg( valist, int );
491 if (flags.FieldLength < 0)
493 flags.LeftAlign = '-';
494 flags.FieldLength = -flags.FieldLength;
496 p++;
498 else while( isDigit(*p) )
500 flags.FieldLength *= 10;
501 flags.FieldLength += *p++ - '0';
504 /* deal with precision */
505 flags.Precision = -1;
506 if( *p == '.' )
508 flags.Precision = 0;
509 p++;
510 if( *p == '*' )
512 flags.Precision = va_arg( valist, int );
513 p++;
515 else while( isDigit(*p) )
517 flags.Precision *= 10;
518 flags.Precision += *p++ - '0';
522 /* deal with integer width modifier */
523 while( *p )
525 if( *p == 'h' || *p == 'l' || *p == 'L' )
527 flags.IntegerLength = *p;
528 p++;
530 else if( *p == 'I' )
532 if( *(p+1) == '6' && *(p+2) == '4' )
534 flags.IntegerDouble++;
535 p += 3;
537 else if( *(p+1) == '3' && *(p+2) == '2' )
538 p += 3;
539 else if( isDigit(*(p+1)) || *(p+1) == 0 )
540 break;
541 else
542 p++;
544 else if( *p == 'w' )
545 flags.WideString = *p++;
546 else if( *p == 'F' )
547 p++; /* ignore */
548 else
549 break;
552 flags.Format = *p;
553 r = 0;
555 if (flags.Format == '$')
557 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
558 return -1;
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' )
576 char pointer[32];
577 void *ptr = va_arg( valist, void * );
579 flags.PadZero = 0;
580 if( flags.Alternate )
581 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
582 else
583 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
584 r = pf_output_format_A( out, pointer, -1, &flags );
587 /* deal with %n */
588 else if( flags.Format == 'n' )
590 int *x = va_arg(valist, int *);
591 *x = out->used;
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 )))
609 return -1;
611 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
613 r = pf_output_format_A( out, x, -1, &flags );
614 if( x != number )
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 )))
633 return -1;
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 );
643 else
644 sprintf( x, fmt, va_arg(valist, int) );
646 r = pf_output_stringA( out, x, -1 );
647 if( x != number )
648 RtlFreeHeap( GetProcessHeap(), 0, x );
650 else
651 continue;
653 if( r<0 )
654 return r;
655 p++;
658 /* check we reached the end, and null terminate the string */
659 assert( *p == 0 );
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 )
671 DWORD sz;
672 LPWSTR formatW = NULL;
673 pf_output out;
674 int r;
676 out.unicode = FALSE;
677 out.buf.A = str;
678 out.used = 0;
679 out.len = len;
681 if (format)
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 );
689 return r;
693 /***********************************************************************
694 * _vsnwprintf (NTDLL.@)
696 int CDECL NTDLL__vsnwprintf( WCHAR *str, SIZE_T len, const WCHAR *format, __ms_va_list args )
698 pf_output out;
700 out.unicode = TRUE;
701 out.buf.W = str;
702 out.used = 0;
703 out.len = len;
705 return pf_vsnprintf( &out, format, args );
709 /*********************************************************************
710 * _snprintf (NTDLL.@)
712 int WINAPIV NTDLL__snprintf( char *str, SIZE_T len, const char *format, ... )
714 int ret;
715 __ms_va_list valist;
717 __ms_va_start( valist, format );
718 ret = NTDLL__vsnprintf( str, len, format, valist );
719 __ms_va_end( valist );
720 return ret;
724 /***********************************************************************
725 * _snwprintf (NTDLL.@)
727 int WINAPIV NTDLL__snwprintf( WCHAR *str, SIZE_T len, const WCHAR *format, ... )
729 int ret;
730 __ms_va_list valist;
732 __ms_va_start(valist, format);
733 ret = NTDLL__vsnwprintf( str, len, format, valist );
734 __ms_va_end(valist);
735 return ret;
739 /*********************************************************************
740 * vsprintf (NTDLL.@)
742 int CDECL NTDLL_vsprintf( char *str, const char *format, __ms_va_list args )
744 return NTDLL__vsnprintf( str, size_max, format, args );
748 /*********************************************************************
749 * sprintf (NTDLL.@)
751 int WINAPIV NTDLL_sprintf( char *str, const char *format, ... )
753 int ret;
754 __ms_va_list valist;
756 __ms_va_start( valist, format );
757 ret = NTDLL__vsnprintf( str, size_max, format, valist );
758 __ms_va_end( valist );
759 return ret;
763 /***********************************************************************
764 * swprintf (NTDLL.@)
766 int WINAPIV NTDLL_swprintf( WCHAR *str, const WCHAR *format, ... )
768 int ret;
769 __ms_va_list valist;
771 __ms_va_start(valist, format);
772 ret = NTDLL__vsnwprintf( str, size_max, format, valist );
773 __ms_va_end(valist);
774 return ret;