msvcrt: Ignore PadZero when LeftAlign is true in printf conversions.
[wine/dibdrv.git] / dlls / msvcrt / wcs.c
blobefb93d8b9ea52eb5f14da48661cfb4e3c1f2747e
1 /*
2 * msvcrt.dll wide-char functions
4 * Copyright 1999 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <limits.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include <assert.h>
25 #include "msvcrt.h"
26 #include "winnls.h"
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33 /* INTERNAL: MSVCRT_malloc() based wstrndup */
34 MSVCRT_wchar_t* msvcrt_wstrndup(const MSVCRT_wchar_t *buf, unsigned int size)
36 MSVCRT_wchar_t* ret;
37 unsigned int len = strlenW(buf), max_len;
39 max_len = size <= len? size : len + 1;
41 ret = MSVCRT_malloc(max_len * sizeof (MSVCRT_wchar_t));
42 if (ret)
44 memcpy(ret,buf,max_len * sizeof (MSVCRT_wchar_t));
45 ret[max_len] = 0;
47 return ret;
50 /*********************************************************************
51 * _wcsdup (MSVCRT.@)
53 MSVCRT_wchar_t* _wcsdup( const MSVCRT_wchar_t* str )
55 MSVCRT_wchar_t* ret = NULL;
56 if (str)
58 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
59 ret = MSVCRT_malloc( size );
60 if (ret) memcpy( ret, str, size );
62 return ret;
65 /*********************************************************************
66 * _wcsicoll (MSVCRT.@)
68 INT _wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
70 /* FIXME: handle collates */
71 return strcmpiW( str1, str2 );
74 /*********************************************************************
75 * _wcsnset (MSVCRT.@)
77 MSVCRT_wchar_t* _wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
79 MSVCRT_wchar_t* ret = str;
80 while ((n-- > 0) && *str) *str++ = c;
81 return ret;
84 /*********************************************************************
85 * _wcsrev (MSVCRT.@)
87 MSVCRT_wchar_t* _wcsrev( MSVCRT_wchar_t* str )
89 MSVCRT_wchar_t* ret = str;
90 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
91 while (end > str)
93 MSVCRT_wchar_t t = *end;
94 *end-- = *str;
95 *str++ = t;
97 return ret;
100 /*********************************************************************
101 * _wcsset (MSVCRT.@)
103 MSVCRT_wchar_t* _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
105 MSVCRT_wchar_t* ret = str;
106 while (*str) *str++ = c;
107 return ret;
110 /*********************************************************************
111 * wcstod (MSVCRT.@)
113 double MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
115 const MSVCRT_wchar_t* str = lpszStr;
116 int negative = 0;
117 double ret = 0, divisor = 10.0;
119 TRACE("(%s,%p) semi-stub\n", debugstr_w(lpszStr), end);
121 /* FIXME:
122 * - Should set errno on failure
123 * - Should fail on overflow
124 * - Need to check which input formats are allowed
126 while (isspaceW(*str))
127 str++;
129 if (*str == '-')
131 negative = 1;
132 str++;
135 while (isdigitW(*str))
137 ret = ret * 10.0 + (*str - '0');
138 str++;
140 if (*str == '.')
141 str++;
142 while (isdigitW(*str))
144 ret = ret + (*str - '0') / divisor;
145 divisor *= 10;
146 str++;
149 if (*str == 'E' || *str == 'e' || *str == 'D' || *str == 'd')
151 int negativeExponent = 0;
152 int exponent = 0;
153 if (*(++str) == '-')
155 negativeExponent = 1;
156 str++;
158 while (isdigitW(*str))
160 exponent = exponent * 10 + (*str - '0');
161 str++;
163 if (exponent != 0)
165 if (negativeExponent)
166 ret = ret / pow(10.0, exponent);
167 else
168 ret = ret * pow(10.0, exponent);
172 if (negative)
173 ret = -ret;
175 if (end)
176 *end = (MSVCRT_wchar_t*)str;
178 TRACE("returning %g\n", ret);
179 return ret;
183 typedef struct pf_output_t
185 int used;
186 int len;
187 BOOL unicode;
188 union {
189 LPWSTR W;
190 LPSTR A;
191 } buf;
192 } pf_output;
194 typedef struct pf_flags_t
196 char Sign, LeftAlign, Alternate, PadZero;
197 int FieldLength, Precision;
198 char IntegerLength, IntegerDouble;
199 char WideString;
200 char Format;
201 } pf_flags;
204 * writes a string of characters to the output
205 * returns -1 if the string doesn't fit in the output buffer
206 * return the length of the string if all characters were written
208 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
210 int space = out->len - out->used;
212 if( len < 0 )
213 len = strlenW( str );
214 if( out->unicode )
216 LPWSTR p = out->buf.W + out->used;
218 if( space >= len )
220 memcpy( p, str, len*sizeof(WCHAR) );
221 out->used += len;
222 return len;
224 if( space > 0 )
225 memcpy( p, str, space*sizeof(WCHAR) );
226 out->used += len;
228 else
230 int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
231 LPSTR p = out->buf.A + out->used;
233 if( space >= n )
235 WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
236 out->used += n;
237 return len;
239 if( space > 0 )
240 WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
241 out->used += n;
243 return -1;
246 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
248 int space = out->len - out->used;
250 if( len < 0 )
251 len = strlen( str );
252 if( !out->unicode )
254 LPSTR p = out->buf.A + out->used;
256 if( space >= len )
258 memcpy( p, str, len );
259 out->used += len;
260 return len;
262 if( space > 0 )
263 memcpy( p, str, space );
264 out->used += len;
266 else
268 int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
269 LPWSTR p = out->buf.W + out->used;
271 if( space >= n )
273 MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
274 out->used += n;
275 return len;
277 if( space > 0 )
278 MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
279 out->used += n;
281 return -1;
284 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
286 int i, r = 0;
288 if( ( !left && flags->LeftAlign ) ||
289 ( left && !flags->LeftAlign ))
291 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
293 if( left && flags->PadZero )
294 r = pf_output_stringA( out, "0", 1 );
295 else
296 r = pf_output_stringA( out, " ", 1 );
300 return r;
303 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
304 int len, pf_flags *flags )
306 int r = 0;
308 if( len < 0 )
309 len = strlenW( str );
311 if (flags->Precision >= 0 && flags->Precision < len)
312 len = flags->Precision;
314 r = pf_fill( out, len, flags, 1 );
316 if( r>=0 )
317 r = pf_output_stringW( out, str, len );
319 if( r>=0 )
320 r = pf_fill( out, len, flags, 0 );
322 return r;
325 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
326 int len, pf_flags *flags )
328 int r = 0;
330 if( len < 0 )
331 len = strlen( str );
333 if (flags->Precision >= 0 && flags->Precision < len)
334 len = flags->Precision;
336 r = pf_fill( out, len, flags, 1 );
338 if( r>=0 )
339 r = pf_output_stringA( out, str, len );
341 if( r>=0 )
342 r = pf_fill( out, len, flags, 0 );
344 return r;
347 static inline BOOL pf_is_double_format( char fmt )
349 static const char float_fmts[] = "aeEfgG";
350 if (!fmt)
351 return FALSE;
352 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
355 static inline BOOL pf_is_valid_format( char fmt )
357 static const char float_fmts[] = "acCdeEfgGinouxX";
358 if (!fmt)
359 return FALSE;
360 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
363 static void pf_rebuild_format_string( char *p, pf_flags *flags )
365 *p++ = '%';
366 if( flags->Sign )
367 *p++ = flags->Sign;
368 if( flags->LeftAlign )
369 *p++ = flags->LeftAlign;
370 if( flags->Alternate )
371 *p++ = flags->Alternate;
372 if( flags->PadZero )
373 *p++ = flags->PadZero;
374 if( flags->FieldLength )
376 sprintf(p, "%d", flags->FieldLength);
377 p += strlen(p);
379 if( flags->Precision >= 0 )
381 sprintf(p, ".%d", flags->Precision);
382 p += strlen(p);
384 *p++ = flags->Format;
385 *p++ = 0;
388 /*********************************************************************
389 * pf_vsnprintf (INTERNAL)
391 * implements both A and W vsnprintf functions
393 static int pf_vsnprintf( pf_output *out, const WCHAR *format, va_list valist )
395 int r;
396 LPCWSTR q, p = format;
397 pf_flags flags;
399 TRACE("format is %s\n",debugstr_w(format));
400 while (*p)
402 q = strchrW( p, '%' );
404 /* there's no % characters left, output the rest of the string */
405 if( !q )
407 r = pf_output_stringW(out, p, -1);
408 if( r<0 )
409 return r;
410 p += r;
411 continue;
414 /* there's characters before the %, output them */
415 if( q != p )
417 r = pf_output_stringW(out, p, q - p);
418 if( r<0 )
419 return r;
420 p = q;
423 /* we must be at a % now, skip over it */
424 assert( *p == '%' );
425 p++;
427 /* output a single % character */
428 if( *p == '%' )
430 r = pf_output_stringW(out, p++, 1);
431 if( r<0 )
432 return r;
433 continue;
436 /* parse the flags */
437 memset( &flags, 0, sizeof flags );
438 while (*p)
440 if( *p == '+' || *p == ' ' )
441 flags.Sign = '+';
442 else if( *p == '-' )
443 flags.LeftAlign = *p;
444 else if( *p == '0' )
445 flags.PadZero = *p;
446 else if( *p == '#' )
447 flags.Alternate = *p;
448 else
449 break;
450 p++;
453 /* deal with the field width specifier */
454 flags.FieldLength = 0;
455 if( *p == '*' )
457 flags.FieldLength = va_arg( valist, int );
458 p++;
460 else while( isdigit(*p) )
462 flags.FieldLength *= 10;
463 flags.FieldLength += *p++ - '0';
466 /* deal with precision */
467 flags.Precision = -1;
468 if( *p == '.' )
470 flags.Precision = 0;
471 p++;
472 if( *p == '*' )
474 flags.Precision = va_arg( valist, int );
475 p++;
477 else while( isdigit(*p) )
479 flags.Precision *= 10;
480 flags.Precision += *p++ - '0';
484 /* deal with integer width modifier */
485 while( *p )
487 if( *p == 'h' || *p == 'l' || *p == 'L' )
489 if( flags.IntegerLength == *p ) /* FIXME: this is wrong */
490 flags.IntegerDouble++;
491 else
492 flags.IntegerLength = *p;
493 p++;
495 else if( *p == 'w' )
496 flags.WideString = *p++;
497 else if( *p == 'F' )
498 p++; /* ignore */
499 else
500 break;
503 flags.Format = *p;
504 r = 0;
506 /* output a unicode string */
507 if( ( flags.Format == 's' && (flags.WideString || flags.IntegerLength == 'l' )) ||
508 ( !out->unicode && flags.Format == 'S' ) ||
509 ( out->unicode && flags.Format == 's' ) )
511 LPCWSTR str = va_arg( valist, const WCHAR * );
513 if( str )
514 r = pf_output_format_W( out, str, -1, &flags );
515 else
516 r = pf_output_format_A( out, "(null)", -1, &flags );
519 /* output a ASCII string */
520 else if( ( flags.Format == 's' && flags.IntegerLength == 'h' ) ||
521 ( out->unicode && flags.Format == 'S' ) ||
522 ( !out->unicode && flags.Format == 's' ) )
524 LPCSTR str = va_arg( valist, const CHAR * );
526 if( str )
527 r = pf_output_format_A( out, str, -1, &flags );
528 else
529 r = pf_output_format_A( out, "(null)", -1, &flags );
532 /* output a single wide character */
533 else if( ( flags.Format == 'c' && flags.IntegerLength == 'w' ) ||
534 ( out->unicode && flags.Format == 'c' ) ||
535 ( !out->unicode && flags.Format == 'C' ) )
537 WCHAR ch = va_arg( valist, int );
539 r = pf_output_format_W( out, &ch, 1, &flags );
542 /* output a single ascii character */
543 else if( ( flags.Format == 'c' && flags.IntegerLength == 'h' ) ||
544 ( out->unicode && flags.Format == 'C' ) ||
545 ( !out->unicode && flags.Format == 'c' ) )
547 CHAR ch = va_arg( valist, int );
549 r = pf_output_format_A( out, &ch, 1, &flags );
552 /* output a pointer */
553 else if( flags.Format == 'p' )
555 char pointer[11];
557 flags.PadZero = 0;
558 if( flags.Alternate )
559 sprintf(pointer, "0X%08lX", va_arg(valist, long));
560 else
561 sprintf(pointer, "%08lX", va_arg(valist, long));
562 r = pf_output_format_A( out, pointer, -1, &flags );
565 /* deal with %n */
566 else if( flags.Format == 'n' )
568 int *x = va_arg(valist, int *);
569 *x = out->used;
572 /* deal with integers and floats using libc's printf */
573 else if( pf_is_valid_format( flags.Format ) )
575 char fmt[20], number[40], *x = number;
577 if( flags.FieldLength >= sizeof number )
578 x = HeapAlloc( GetProcessHeap(), 0, flags.FieldLength+1 );
580 pf_rebuild_format_string( fmt, &flags );
582 if( pf_is_double_format( flags.Format ) )
583 sprintf( x, fmt, va_arg(valist, double) );
584 else
585 sprintf( x, fmt, va_arg(valist, int) );
587 r = pf_output_stringA( out, x, -1 );
588 if( x != number )
589 HeapFree( GetProcessHeap(), 0, number );
591 else
592 continue;
594 if( r<0 )
595 return r;
596 p++;
599 /* check we reached the end, and null terminate the string */
600 assert( *p == 0 );
601 r = pf_output_stringW( out, p, 1 );
602 if( r<0 )
603 return r;
605 return out->used - 1;
608 /*********************************************************************
609 * _vsnprintf (MSVCRT.@)
611 int MSVCRT_vsnprintf( char *str, unsigned int len,
612 const char *format, va_list valist )
614 DWORD sz;
615 LPWSTR formatW = NULL;
616 pf_output out;
617 int r;
619 out.unicode = FALSE;
620 out.buf.A = str;
621 out.used = 0;
622 out.len = len;
624 if( format )
626 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
627 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
628 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
631 r = pf_vsnprintf( &out, formatW, valist );
633 HeapFree( GetProcessHeap(), 0, formatW );
635 return r;
638 /*********************************************************************
639 * _vsnwsprintf (MSVCRT.@)
641 int MSVCRT_vsnwprintf( MSVCRT_wchar_t *str, unsigned int len,
642 const WCHAR *format, va_list valist )
644 pf_output out;
646 out.unicode = TRUE;
647 out.buf.W = str;
648 out.used = 0;
649 out.len = len;
651 return pf_vsnprintf( &out, format, valist );
654 /*********************************************************************
655 * sprintf (MSVCRT.@)
657 int MSVCRT_sprintf( char *str, const char *format, ... )
659 va_list ap;
660 int r;
662 va_start( ap, format );
663 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
664 va_end( ap );
665 return r;
668 /*********************************************************************
669 * swprintf (MSVCRT.@)
671 int MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
673 va_list ap;
674 int r;
676 va_start( ap, format );
677 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
678 va_end( ap );
679 return r;
682 /*********************************************************************
683 * vswprintf (MSVCRT.@)
685 int MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, va_list args )
687 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
690 /*********************************************************************
691 * wcscoll (MSVCRT.@)
693 int MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
695 /* FIXME: handle collates */
696 return strcmpW( str1, str2 );
699 /*********************************************************************
700 * wcspbrk (MSVCRT.@)
702 MSVCRT_wchar_t* MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
704 const MSVCRT_wchar_t* p;
705 while (*str)
707 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
708 str++;
710 return NULL;
713 /*********************************************************************
714 * wctomb (MSVCRT.@)
716 INT MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
718 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
721 /*********************************************************************
722 * iswalnum (MSVCRT.@)
724 INT MSVCRT_iswalnum( MSVCRT_wchar_t wc )
726 return isalnumW( wc );
729 /*********************************************************************
730 * iswalpha (MSVCRT.@)
732 INT MSVCRT_iswalpha( MSVCRT_wchar_t wc )
734 return isalphaW( wc );
737 /*********************************************************************
738 * iswcntrl (MSVCRT.@)
740 INT MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
742 return iscntrlW( wc );
745 /*********************************************************************
746 * iswdigit (MSVCRT.@)
748 INT MSVCRT_iswdigit( MSVCRT_wchar_t wc )
750 return isdigitW( wc );
753 /*********************************************************************
754 * iswgraph (MSVCRT.@)
756 INT MSVCRT_iswgraph( MSVCRT_wchar_t wc )
758 return isgraphW( wc );
761 /*********************************************************************
762 * iswlower (MSVCRT.@)
764 INT MSVCRT_iswlower( MSVCRT_wchar_t wc )
766 return islowerW( wc );
769 /*********************************************************************
770 * iswprint (MSVCRT.@)
772 INT MSVCRT_iswprint( MSVCRT_wchar_t wc )
774 return isprintW( wc );
777 /*********************************************************************
778 * iswpunct (MSVCRT.@)
780 INT MSVCRT_iswpunct( MSVCRT_wchar_t wc )
782 return ispunctW( wc );
785 /*********************************************************************
786 * iswspace (MSVCRT.@)
788 INT MSVCRT_iswspace( MSVCRT_wchar_t wc )
790 return isspaceW( wc );
793 /*********************************************************************
794 * iswupper (MSVCRT.@)
796 INT MSVCRT_iswupper( MSVCRT_wchar_t wc )
798 return isupperW( wc );
801 /*********************************************************************
802 * iswxdigit (MSVCRT.@)
804 INT MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
806 return isxdigitW( wc );