jscript: Update French translation.
[wine/multimedia.git] / dlls / ntdll / printf.c
blob1aca399efa8913572ded9956a98a1c2efe3bc7d5
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;
79 if( space >= len )
81 memcpy( p, str, len*sizeof(WCHAR) );
82 out->used += len;
83 return len;
85 if( space > 0 )
86 memcpy( p, str, space*sizeof(WCHAR) );
87 out->used += len;
89 else
91 LPSTR p = out->buf.A + out->used;
92 ULONG n;
94 RtlUnicodeToMultiByteSize( &n, str, len * sizeof(WCHAR) );
95 if( space >= n )
97 RtlUnicodeToMultiByteN( p, n, NULL, str, len * sizeof(WCHAR) );
98 out->used += n;
99 return len;
101 if (space > 0) RtlUnicodeToMultiByteN( p, space, NULL, str, len * sizeof(WCHAR) );
102 out->used += n;
104 return -1;
107 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
109 int space = out->len - out->used;
111 if( len < 0 )
112 len = strlen( str );
113 if( !out->unicode )
115 LPSTR p = out->buf.A + out->used;
117 if( space >= len )
119 memcpy( p, str, len );
120 out->used += len;
121 return len;
123 if( space > 0 )
124 memcpy( p, str, space );
125 out->used += len;
127 else
129 LPWSTR p = out->buf.W + out->used;
130 ULONG n;
132 RtlMultiByteToUnicodeSize( &n, str, len );
133 if (space >= n / sizeof(WCHAR))
135 RtlMultiByteToUnicodeN( p, n, NULL, str, len );
136 out->used += n / sizeof(WCHAR);
137 return len;
139 if (space > 0) RtlMultiByteToUnicodeN( p, space * sizeof(WCHAR), NULL, str, len );
140 out->used += n;
142 return -1;
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 )
148 int i, r = 0;
150 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
151 flags->Sign = 0;
153 if( left && flags->Sign )
155 flags->FieldLength--;
156 if( flags->PadZero )
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 );
167 else
168 r = pf_output_stringA( out, " ", 1 );
172 if( left && flags->Sign && !flags->PadZero )
173 r = pf_output_stringA( out, &flags->Sign, 1 );
175 return r;
178 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
179 int len, pf_flags *flags )
181 int r = 0;
183 if( len < 0 )
184 len = strlenW( str );
186 if (flags->Precision >= 0 && flags->Precision < len)
187 len = flags->Precision;
189 r = pf_fill( out, len, flags, 1 );
191 if( r>=0 )
192 r = pf_output_stringW( out, str, len );
194 if( r>=0 )
195 r = pf_fill( out, len, flags, 0 );
197 return r;
200 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
201 int len, pf_flags *flags )
203 int r = 0;
205 if( len < 0 )
206 len = strlen( str );
208 if (flags->Precision >= 0 && flags->Precision < len)
209 len = flags->Precision;
211 r = pf_fill( out, len, flags, 1 );
213 if( r>=0 )
214 r = pf_output_stringA( out, str, len );
216 if( r>=0 )
217 r = pf_fill( out, len, flags, 0 );
219 return r;
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);
238 else
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";
245 if (!fmt)
246 return FALSE;
247 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
250 static inline BOOL pf_is_double_format( char fmt )
252 static const char float_fmts[] = "aeEfgG";
253 if (!fmt)
254 return FALSE;
255 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
258 static inline BOOL pf_is_valid_format( char fmt )
260 static const char float_fmts[] = "acCdeEfgGinouxX";
261 if (!fmt)
262 return FALSE;
263 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
266 static void pf_rebuild_format_string( char *p, pf_flags *flags )
268 *p++ = '%';
269 if( flags->Sign )
270 *p++ = flags->Sign;
271 if( flags->LeftAlign )
272 *p++ = flags->LeftAlign;
273 if( flags->Alternate )
274 *p++ = flags->Alternate;
275 if( flags->PadZero )
276 *p++ = flags->PadZero;
277 if( flags->FieldLength )
279 sprintf(p, "%d", flags->FieldLength);
280 p += strlen(p);
282 if( flags->Precision >= 0 )
284 sprintf(p, ".%d", flags->Precision);
285 p += strlen(p);
287 *p++ = flags->Format;
288 *p++ = 0;
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,
294 LONGLONG x )
296 unsigned int base;
297 const char *digits;
299 int i, j, k;
300 char number[40], *tmp = number;
302 if( buf_len > sizeof number )
303 tmp = RtlAllocateHeap( GetProcessHeap(), 0, buf_len );
305 base = 10;
306 if( flags->Format == 'o' )
307 base = 8;
308 else if( flags->Format == 'x' || flags->Format == 'X' )
309 base = 16;
311 if( flags->Format == 'X' )
312 digits = "0123456789ABCDEFX";
313 else
314 digits = "0123456789abcdefx";
316 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
318 x = -x;
319 flags->Sign = '-';
322 /* Do conversion (backwards) */
323 i = 0;
324 if( x == 0 && flags->Precision )
325 tmp[i++] = '0';
326 else
327 while( x != 0 )
329 j = (ULONGLONG) x % base;
330 x = (ULONGLONG) x / base;
331 tmp[i++] = digits[j];
333 k = flags->Precision - i;
334 while( k-- > 0 )
335 tmp[i++] = '0';
336 if( flags->Alternate )
338 if( base == 16 )
340 tmp[i++] = digits[16];
341 tmp[i++] = '0';
343 else if( base == 8 && tmp[i-1] != '0' )
344 tmp[i++] = '0';
347 /* Reverse for buf */
348 j = 0;
349 while( i-- > 0 )
350 buf[j++] = tmp[i];
351 buf[j] = '\0';
353 /* Adjust precision so pf_fill won't truncate the number later */
354 flags->Precision = strlen( buf );
356 if( tmp != number )
357 RtlFreeHeap( GetProcessHeap(), 0, tmp );
359 return;
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 )
367 char* tmp = buf;
369 while (tmp[0] && toupper(tmp[0]) != 'E')
370 tmp++;
372 if (tmp[0] && (tmp[1] == '+' || tmp[1] == '-') &&
373 isdigit(tmp[2]) && isdigit(tmp[3]))
375 char final;
377 if (isdigit(tmp[4]))
378 return; /* Exponent already 3 digits */
380 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
381 tmp += 2;
382 final = tmp[2];
383 tmp[2] = tmp[1];
384 tmp[1] = tmp[0];
385 tmp[0] = '0';
386 if (final == '\0')
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
391 * right justified).
393 tmp[3] = '\0';
394 if (buf[0] == ' ')
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 )
408 int r;
409 LPCWSTR q, p = format;
410 pf_flags flags;
412 TRACE("format is %s\n",debugstr_w(format));
413 while (*p)
415 q = strchrW( p, '%' );
417 /* there's no % characters left, output the rest of the string */
418 if( !q )
420 r = pf_output_stringW(out, p, -1);
421 if( r<0 )
422 return r;
423 p += r;
424 continue;
427 /* there's characters before the %, output them */
428 if( q != p )
430 r = pf_output_stringW(out, p, q - p);
431 if( r<0 )
432 return r;
433 p = q;
436 /* we must be at a % now, skip over it */
437 assert( *p == '%' );
438 p++;
440 /* output a single % character */
441 if( *p == '%' )
443 r = pf_output_stringW(out, p++, 1);
444 if( r<0 )
445 return r;
446 continue;
449 /* parse the flags */
450 memset( &flags, 0, sizeof flags );
451 while (*p)
453 if( *p == '+' || *p == ' ' )
455 if ( flags.Sign != '+' )
456 flags.Sign = *p;
458 else if( *p == '-' )
459 flags.LeftAlign = *p;
460 else if( *p == '0' )
461 flags.PadZero = *p;
462 else if( *p == '#' )
463 flags.Alternate = *p;
464 else
465 break;
466 p++;
469 /* deal with the field width specifier */
470 flags.FieldLength = 0;
471 if( *p == '*' )
473 flags.FieldLength = va_arg( valist, int );
474 if (flags.FieldLength < 0)
476 flags.LeftAlign = '-';
477 flags.FieldLength = -flags.FieldLength;
479 p++;
481 else while( isdigit(*p) )
483 flags.FieldLength *= 10;
484 flags.FieldLength += *p++ - '0';
487 /* deal with precision */
488 flags.Precision = -1;
489 if( *p == '.' )
491 flags.Precision = 0;
492 p++;
493 if( *p == '*' )
495 flags.Precision = va_arg( valist, int );
496 p++;
498 else while( isdigit(*p) )
500 flags.Precision *= 10;
501 flags.Precision += *p++ - '0';
505 /* deal with integer width modifier */
506 while( *p )
508 if( *p == 'h' || *p == 'l' || *p == 'L' )
510 flags.IntegerLength = *p;
511 p++;
513 else if( *p == 'I' )
515 if( *(p+1) == '6' && *(p+2) == '4' )
517 flags.IntegerDouble++;
518 p += 3;
520 else if( *(p+1) == '3' && *(p+2) == '2' )
521 p += 3;
522 else if( isdigit(*(p+1)) || *(p+1) == 0 )
523 break;
524 else
525 p++;
527 else if( *p == 'w' )
528 flags.WideString = *p++;
529 else if( *p == 'F' )
530 p++; /* ignore */
531 else
532 break;
535 flags.Format = *p;
536 r = 0;
538 if (flags.Format == '$')
540 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
541 return -1;
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' )
559 char pointer[32];
560 void *ptr = va_arg( valist, void * );
562 flags.PadZero = 0;
563 if( flags.Alternate )
564 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
565 else
566 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
567 r = pf_output_format_A( out, pointer, -1, &flags );
570 /* deal with %n */
571 else if( flags.Format == 'n' )
573 int *x = va_arg(valist, int *);
574 *x = out->used;
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 );
596 if( x != number )
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 );
624 else
625 sprintf( x, fmt, va_arg(valist, int) );
627 r = pf_output_stringA( out, x, -1 );
628 if( x != number )
629 RtlFreeHeap( GetProcessHeap(), 0, x );
631 else
632 continue;
634 if( r<0 )
635 return r;
636 p++;
639 /* check we reached the end, and null terminate the string */
640 assert( *p == 0 );
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 )
652 DWORD sz;
653 LPWSTR formatW = NULL;
654 pf_output out;
655 int r;
657 out.unicode = FALSE;
658 out.buf.A = str;
659 out.used = 0;
660 out.len = len;
662 if (format)
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 );
670 return r;
674 /***********************************************************************
675 * _vsnwprintf (NTDLL.@)
677 int CDECL NTDLL__vsnwprintf( WCHAR *str, SIZE_T len, const WCHAR *format, __ms_va_list args )
679 pf_output out;
681 out.unicode = TRUE;
682 out.buf.W = str;
683 out.used = 0;
684 out.len = len;
686 return pf_vsnprintf( &out, format, args );
690 /*********************************************************************
691 * _snprintf (NTDLL.@)
693 int CDECL NTDLL__snprintf( char *str, SIZE_T len, const char *format, ... )
695 int ret;
696 __ms_va_list valist;
698 __ms_va_start( valist, format );
699 ret = NTDLL__vsnprintf( str, len, format, valist );
700 __ms_va_end( valist );
701 return ret;
705 /***********************************************************************
706 * _snwprintf (NTDLL.@)
708 int CDECL NTDLL__snwprintf( WCHAR *str, SIZE_T len, const WCHAR *format, ... )
710 int ret;
711 __ms_va_list valist;
713 __ms_va_start(valist, format);
714 ret = NTDLL__vsnwprintf( str, len, format, valist );
715 __ms_va_end(valist);
716 return ret;
720 /*********************************************************************
721 * vsprintf (NTDLL.@)
723 int CDECL NTDLL_vsprintf( char *str, const char *format, __ms_va_list args )
725 return NTDLL__vsnprintf( str, size_max, format, args );
729 /*********************************************************************
730 * sprintf (NTDLL.@)
732 int CDECL NTDLL_sprintf( char *str, const char *format, ... )
734 int ret;
735 __ms_va_list valist;
737 __ms_va_start( valist, format );
738 ret = NTDLL__vsnprintf( str, size_max, format, valist );
739 __ms_va_end( valist );
740 return ret;
744 /***********************************************************************
745 * swprintf (NTDLL.@)
747 int CDECL NTDLL_swprintf( WCHAR *str, const WCHAR *format, ... )
749 int ret;
750 __ms_va_list valist;
752 __ms_va_start(valist, format);
753 ret = NTDLL__vsnwprintf( str, size_max, format, valist );
754 __ms_va_end(valist);
755 return ret;