d2d1/tests: Add a basic clipping test.
[wine.git] / dlls / ntdll / printf.c
blob65dc62dff4efd6e81b34f4c117136f8751f54ae4
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 /*********************************************************************
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 )
420 int r;
421 LPCWSTR q, p = format;
422 pf_flags flags;
424 TRACE("format is %s\n",debugstr_w(format));
425 while (*p)
427 q = strchrW( p, '%' );
429 /* there are no % characters left: output the rest of the string */
430 if( !q )
432 r = pf_output_stringW(out, p, -1);
433 if( r<0 )
434 return r;
435 p += r;
436 continue;
439 /* there are characters before the %: output them */
440 if( q != p )
442 r = pf_output_stringW(out, p, q - p);
443 if( r<0 )
444 return r;
445 p = q;
448 /* we must be at a % now, skip over it */
449 assert( *p == '%' );
450 p++;
452 /* output a single % character */
453 if( *p == '%' )
455 r = pf_output_stringW(out, p++, 1);
456 if( r<0 )
457 return r;
458 continue;
461 /* parse the flags */
462 memset( &flags, 0, sizeof flags );
463 while (*p)
465 if( *p == '+' || *p == ' ' )
467 if ( flags.Sign != '+' )
468 flags.Sign = *p;
470 else if( *p == '-' )
471 flags.LeftAlign = *p;
472 else if( *p == '0' )
473 flags.PadZero = *p;
474 else if( *p == '#' )
475 flags.Alternate = *p;
476 else
477 break;
478 p++;
481 /* deal with the field width specifier */
482 flags.FieldLength = 0;
483 if( *p == '*' )
485 flags.FieldLength = va_arg( valist, int );
486 if (flags.FieldLength < 0)
488 flags.LeftAlign = '-';
489 flags.FieldLength = -flags.FieldLength;
491 p++;
493 else while( isdigit(*p) )
495 flags.FieldLength *= 10;
496 flags.FieldLength += *p++ - '0';
499 /* deal with precision */
500 flags.Precision = -1;
501 if( *p == '.' )
503 flags.Precision = 0;
504 p++;
505 if( *p == '*' )
507 flags.Precision = va_arg( valist, int );
508 p++;
510 else while( isdigit(*p) )
512 flags.Precision *= 10;
513 flags.Precision += *p++ - '0';
517 /* deal with integer width modifier */
518 while( *p )
520 if( *p == 'h' || *p == 'l' || *p == 'L' )
522 flags.IntegerLength = *p;
523 p++;
525 else if( *p == 'I' )
527 if( *(p+1) == '6' && *(p+2) == '4' )
529 flags.IntegerDouble++;
530 p += 3;
532 else if( *(p+1) == '3' && *(p+2) == '2' )
533 p += 3;
534 else if( isdigit(*(p+1)) || *(p+1) == 0 )
535 break;
536 else
537 p++;
539 else if( *p == 'w' )
540 flags.WideString = *p++;
541 else if( *p == 'F' )
542 p++; /* ignore */
543 else
544 break;
547 flags.Format = *p;
548 r = 0;
550 if (flags.Format == '$')
552 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
553 return -1;
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' )
571 char pointer[32];
572 void *ptr = va_arg( valist, void * );
574 flags.PadZero = 0;
575 if( flags.Alternate )
576 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
577 else
578 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
579 r = pf_output_format_A( out, pointer, -1, &flags );
582 /* deal with %n */
583 else if( flags.Format == 'n' )
585 int *x = va_arg(valist, int *);
586 *x = out->used;
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 )))
604 return -1;
606 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
608 r = pf_output_format_A( out, x, -1, &flags );
609 if( x != number )
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 )))
628 return -1;
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 );
638 else
639 sprintf( x, fmt, va_arg(valist, int) );
641 r = pf_output_stringA( out, x, -1 );
642 if( x != number )
643 RtlFreeHeap( GetProcessHeap(), 0, x );
645 else
646 continue;
648 if( r<0 )
649 return r;
650 p++;
653 /* check we reached the end, and null terminate the string */
654 assert( *p == 0 );
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 )
666 DWORD sz;
667 LPWSTR formatW = NULL;
668 pf_output out;
669 int r;
671 out.unicode = FALSE;
672 out.buf.A = str;
673 out.used = 0;
674 out.len = len;
676 if (format)
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 );
684 return r;
688 /***********************************************************************
689 * _vsnwprintf (NTDLL.@)
691 int CDECL NTDLL__vsnwprintf( WCHAR *str, SIZE_T len, const WCHAR *format, __ms_va_list args )
693 pf_output out;
695 out.unicode = TRUE;
696 out.buf.W = str;
697 out.used = 0;
698 out.len = len;
700 return pf_vsnprintf( &out, format, args );
704 /*********************************************************************
705 * _snprintf (NTDLL.@)
707 int CDECL NTDLL__snprintf( char *str, SIZE_T len, const char *format, ... )
709 int ret;
710 __ms_va_list valist;
712 __ms_va_start( valist, format );
713 ret = NTDLL__vsnprintf( str, len, format, valist );
714 __ms_va_end( valist );
715 return ret;
719 /***********************************************************************
720 * _snwprintf (NTDLL.@)
722 int CDECL NTDLL__snwprintf( WCHAR *str, SIZE_T len, const WCHAR *format, ... )
724 int ret;
725 __ms_va_list valist;
727 __ms_va_start(valist, format);
728 ret = NTDLL__vsnwprintf( str, len, format, valist );
729 __ms_va_end(valist);
730 return ret;
734 /*********************************************************************
735 * vsprintf (NTDLL.@)
737 int CDECL NTDLL_vsprintf( char *str, const char *format, __ms_va_list args )
739 return NTDLL__vsnprintf( str, size_max, format, args );
743 /*********************************************************************
744 * sprintf (NTDLL.@)
746 int CDECL NTDLL_sprintf( char *str, const char *format, ... )
748 int ret;
749 __ms_va_list valist;
751 __ms_va_start( valist, format );
752 ret = NTDLL__vsnprintf( str, size_max, format, valist );
753 __ms_va_end( valist );
754 return ret;
758 /***********************************************************************
759 * swprintf (NTDLL.@)
761 int CDECL NTDLL_swprintf( WCHAR *str, const WCHAR *format, ... )
763 int ret;
764 __ms_va_list valist;
766 __ms_va_start(valist, format);
767 ret = NTDLL__vsnwprintf( str, size_max, format, valist );
768 __ms_va_end(valist);
769 return ret;