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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33 /*********************************************************************
36 MSVCRT_wchar_t* CDECL _wcsdup( const MSVCRT_wchar_t* str )
38 MSVCRT_wchar_t* ret = NULL;
41 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
42 ret = MSVCRT_malloc( size );
43 if (ret) memcpy( ret, str, size );
48 /*********************************************************************
49 * _wcsicoll (MSVCRT.@)
51 INT CDECL _wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
53 /* FIXME: handle collates */
54 return strcmpiW( str1, str2 );
57 /*********************************************************************
60 MSVCRT_wchar_t* CDECL _wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
62 MSVCRT_wchar_t* ret = str;
63 while ((n-- > 0) && *str) *str++ = c;
67 /*********************************************************************
70 MSVCRT_wchar_t* CDECL _wcsrev( MSVCRT_wchar_t* str )
72 MSVCRT_wchar_t* ret = str;
73 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
76 MSVCRT_wchar_t t = *end;
83 /*********************************************************************
86 MSVCRT_wchar_t* CDECL _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
88 MSVCRT_wchar_t* ret = str;
89 while (*str) *str++ = c;
93 /*********************************************************************
96 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
98 const MSVCRT_wchar_t* str = lpszStr;
100 double ret = 0, divisor = 10.0;
102 TRACE("(%s,%p) semi-stub\n", debugstr_w(lpszStr), end);
105 * - Should set errno on failure
106 * - Should fail on overflow
107 * - Need to check which input formats are allowed
109 while (isspaceW(*str))
118 while (isdigitW(*str))
120 ret = ret * 10.0 + (*str - '0');
125 while (isdigitW(*str))
127 ret = ret + (*str - '0') / divisor;
132 if (*str == 'E' || *str == 'e' || *str == 'D' || *str == 'd')
134 int negativeExponent = 0;
138 negativeExponent = 1;
141 while (isdigitW(*str))
143 exponent = exponent * 10 + (*str - '0');
148 if (negativeExponent)
149 ret = ret / pow(10.0, exponent);
151 ret = ret * pow(10.0, exponent);
159 *end = (MSVCRT_wchar_t*)str;
161 TRACE("returning %g\n", ret);
166 typedef struct pf_output_t
177 typedef struct pf_flags_t
179 char Sign, LeftAlign, Alternate, PadZero;
180 int FieldLength, Precision;
181 char IntegerLength, IntegerDouble;
187 * writes a string of characters to the output
188 * returns -1 if the string doesn't fit in the output buffer
189 * return the length of the string if all characters were written
191 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
193 int space = out->len - out->used;
196 len = strlenW( str );
199 LPWSTR p = out->buf.W + out->used;
203 memcpy( p, str, len*sizeof(WCHAR) );
208 memcpy( p, str, space*sizeof(WCHAR) );
213 int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
214 LPSTR p = out->buf.A + out->used;
218 WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
223 WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
229 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
231 int space = out->len - out->used;
237 LPSTR p = out->buf.A + out->used;
241 memcpy( p, str, len );
246 memcpy( p, str, space );
251 int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
252 LPWSTR p = out->buf.W + out->used;
256 MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
261 MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
267 /* pf_fill: takes care of signs, alignment, zero and field padding */
268 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
272 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
275 if( left && flags->Sign )
277 flags->FieldLength--;
279 r = pf_output_stringA( out, &flags->Sign, 1 );
282 if( ( !left && flags->LeftAlign ) ||
283 ( left && !flags->LeftAlign ))
285 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
287 if( left && flags->PadZero )
288 r = pf_output_stringA( out, "0", 1 );
290 r = pf_output_stringA( out, " ", 1 );
294 if( left && flags->Sign && !flags->PadZero )
295 r = pf_output_stringA( out, &flags->Sign, 1 );
300 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
301 int len, pf_flags *flags )
306 len = strlenW( str );
308 if (flags->Precision >= 0 && flags->Precision < len)
309 len = flags->Precision;
311 r = pf_fill( out, len, flags, 1 );
314 r = pf_output_stringW( out, str, len );
317 r = pf_fill( out, len, flags, 0 );
322 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
323 int len, pf_flags *flags )
330 if (flags->Precision >= 0 && flags->Precision < len)
331 len = flags->Precision;
333 r = pf_fill( out, len, flags, 1 );
336 r = pf_output_stringA( out, str, len );
339 r = pf_fill( out, len, flags, 0 );
344 static int pf_handle_string_format( pf_output *out, const void* str, int len,
345 pf_flags *flags, BOOL capital_letter)
347 if(str == NULL) /* catch NULL pointer */
348 return pf_output_format_A( out, "(null)", -1, flags);
350 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
351 if(flags->WideString || flags->IntegerLength == 'l')
352 return pf_output_format_W( out, str, len, flags);
353 if(flags->IntegerLength == 'h')
354 return pf_output_format_A( out, str, len, flags);
356 /* %s,%c -> chars in ansi functions & wchars in unicode
357 * %S,%C -> wchars in ansi functions & chars in unicode */
358 if( capital_letter == out->unicode) /* either both TRUE or both FALSE */
359 return pf_output_format_A( out, str, len, flags);
361 return pf_output_format_W( out, str, len, flags);
364 static inline BOOL pf_is_integer_format( char fmt )
366 static const char float_fmts[] = "diouxX";
369 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
372 static inline BOOL pf_is_double_format( char fmt )
374 static const char float_fmts[] = "aeEfgG";
377 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
380 static inline BOOL pf_is_valid_format( char fmt )
382 static const char float_fmts[] = "acCdeEfgGinouxX";
385 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
388 static void pf_rebuild_format_string( char *p, pf_flags *flags )
393 if( flags->LeftAlign )
394 *p++ = flags->LeftAlign;
395 if( flags->Alternate )
396 *p++ = flags->Alternate;
398 *p++ = flags->PadZero;
399 if( flags->FieldLength )
401 sprintf(p, "%d", flags->FieldLength);
404 if( flags->Precision >= 0 )
406 sprintf(p, ".%d", flags->Precision);
409 *p++ = flags->Format;
413 /* pf_integer_conv: prints x to buf, including alternate formats and
414 additional precision digits, but not field characters or the sign */
415 static void pf_integer_conv( char *buf, int buf_len, pf_flags *flags,
422 char number[40], *tmp = number;
424 if( buf_len > sizeof number )
425 tmp = HeapAlloc( GetProcessHeap(), 0, buf_len );
428 if( flags->Format == 'o' )
430 else if( flags->Format == 'x' || flags->Format == 'X' )
433 if( flags->Format == 'X' )
434 digits = "0123456789ABCDEFX";
436 digits = "0123456789abcdefx";
438 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
444 /* Do conversion (backwards) */
446 if( x == 0 && flags->Precision )
451 j = (ULONGLONG) x % base;
452 x = (ULONGLONG) x / base;
453 tmp[i++] = digits[j];
455 k = flags->Precision - i;
458 if( flags->Alternate )
462 tmp[i++] = digits[16];
465 else if( base == 8 && tmp[i-1] != '0' )
469 /* Reverse for buf */
475 /* Adjust precision so pf_fill won't truncate the number later */
476 flags->Precision = strlen( buf );
479 HeapFree( GetProcessHeap(), 0, tmp );
484 /* pf_fixup_exponent: convert a string containing a 2 digit exponent
485 to 3 digits, accounting for padding, in place. Needed to match
486 the native printf's which always use 3 digits. */
487 static void pf_fixup_exponent( char *buf )
491 while (tmp[0] && toupper(tmp[0]) != 'E')
494 if (tmp[0] && (tmp[1] == '+' || tmp[1] == '-') &&
495 isdigit(tmp[2]) && isdigit(tmp[3]))
500 return; /* Exponent already 3 digits */
502 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
510 /* We didn't expand into trailing space, so this string isn't left
511 * justified. Terminate the string and strip a ' ' at the start of
512 * the string if there is one (as there may be if the string is
517 memmove(buf, buf + 1, (tmp - buf) + 3);
519 /* Otherwise, we expanded into trailing space -> nothing to do */
523 /*********************************************************************
524 * pf_vsnprintf (INTERNAL)
526 * implements both A and W vsnprintf functions
528 static int pf_vsnprintf( pf_output *out, const WCHAR *format, va_list valist )
531 LPCWSTR q, p = format;
534 TRACE("format is %s\n",debugstr_w(format));
537 q = strchrW( p, '%' );
539 /* there's no % characters left, output the rest of the string */
542 r = pf_output_stringW(out, p, -1);
549 /* there's characters before the %, output them */
552 r = pf_output_stringW(out, p, q - p);
558 /* we must be at a % now, skip over it */
562 /* output a single % character */
565 r = pf_output_stringW(out, p++, 1);
571 /* parse the flags */
572 memset( &flags, 0, sizeof flags );
575 if( *p == '+' || *p == ' ' )
577 if ( flags.Sign != '+' )
581 flags.LeftAlign = *p;
585 flags.Alternate = *p;
591 /* deal with the field width specifier */
592 flags.FieldLength = 0;
595 flags.FieldLength = va_arg( valist, int );
596 if (flags.FieldLength < 0)
598 flags.LeftAlign = '-';
599 flags.FieldLength = -flags.FieldLength;
603 else while( isdigit(*p) )
605 flags.FieldLength *= 10;
606 flags.FieldLength += *p++ - '0';
609 /* deal with precision */
610 flags.Precision = -1;
617 flags.Precision = va_arg( valist, int );
620 else while( isdigit(*p) )
622 flags.Precision *= 10;
623 flags.Precision += *p++ - '0';
627 /* deal with integer width modifier */
630 if( *p == 'h' || *p == 'l' || *p == 'L' )
632 flags.IntegerLength = *p;
637 if( *(p+1) == '6' && *(p+2) == '4' )
639 flags.IntegerDouble++;
642 else if( *(p+1) == '3' && *(p+2) == '2' )
644 else if( isdigit(*(p+1)) || *(p+1) == 0 )
650 flags.WideString = *p++;
660 if (flags.Format == '$')
662 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
665 /* output a string */
666 if( flags.Format == 's' || flags.Format == 'S' )
667 r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
668 &flags, (flags.Format == 'S') );
670 /* output a single character */
671 else if( flags.Format == 'c' || flags.Format == 'C' )
673 INT ch = va_arg( valist, int );
675 r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
678 /* output a pointer */
679 else if( flags.Format == 'p' )
684 if( flags.Alternate )
685 sprintf(pointer, "0X%08lX", va_arg(valist, long));
687 sprintf(pointer, "%08lX", va_arg(valist, long));
688 r = pf_output_format_A( out, pointer, -1, &flags );
692 else if( flags.Format == 'n' )
694 int *x = va_arg(valist, int *);
698 /* deal with 64-bit integers */
699 else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
701 char number[40], *x = number;
703 /* Estimate largest possible required buffer size:
704 * Chooses the larger of the field or precision
705 * Includes extra bytes: 1 byte for null, 1 byte for sign,
706 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
707 for a decimal, and 1 byte for an additional float digit. */
708 int x_len = ((flags.FieldLength > flags.Precision) ?
709 flags.FieldLength : flags.Precision) + 10;
711 if( x_len >= sizeof number)
712 x = HeapAlloc( GetProcessHeap(), 0, x_len );
714 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
716 r = pf_output_format_A( out, x, -1, &flags );
718 HeapFree( GetProcessHeap(), 0, x );
721 /* deal with integers and floats using libc's printf */
722 else if( pf_is_valid_format( flags.Format ) )
724 char fmt[20], number[40], *x = number;
726 /* Estimate largest possible required buffer size:
727 * Chooses the larger of the field or precision
728 * Includes extra bytes: 1 byte for null, 1 byte for sign,
729 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
730 for a decimal, and 1 byte for an additional float digit. */
731 int x_len = ((flags.FieldLength > flags.Precision) ?
732 flags.FieldLength : flags.Precision) + 10;
734 if( x_len >= sizeof number)
735 x = HeapAlloc( GetProcessHeap(), 0, x_len );
737 pf_rebuild_format_string( fmt, &flags );
739 if( pf_is_double_format( flags.Format ) )
741 sprintf( x, fmt, va_arg(valist, double) );
742 if (toupper(flags.Format) == 'E' || toupper(flags.Format) == 'G')
743 pf_fixup_exponent( x );
746 sprintf( x, fmt, va_arg(valist, int) );
748 r = pf_output_stringA( out, x, -1 );
750 HeapFree( GetProcessHeap(), 0, x );
760 /* check we reached the end, and null terminate the string */
762 pf_output_stringW( out, p, 1 );
764 return out->used - 1;
767 /*********************************************************************
768 * _vsnprintf (MSVCRT.@)
770 int CDECL MSVCRT_vsnprintf( char *str, unsigned int len,
771 const char *format, va_list valist )
774 LPWSTR formatW = NULL;
785 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
786 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
787 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
790 r = pf_vsnprintf( &out, formatW, valist );
792 HeapFree( GetProcessHeap(), 0, formatW );
797 /*********************************************************************
798 * vsprintf (MSVCRT.@)
800 int CDECL MSVCRT_vsprintf( char *str, const char *format, va_list valist)
802 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
805 /*********************************************************************
806 * _snprintf (MSVCRT.@)
808 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
812 va_start(valist, format);
813 retval = MSVCRT_vsnprintf(str, len, format, valist);
818 /*********************************************************************
819 * _vsnwsprintf (MSVCRT.@)
821 int CDECL MSVCRT_vsnwprintf( MSVCRT_wchar_t *str, unsigned int len,
822 const MSVCRT_wchar_t *format, va_list valist )
831 return pf_vsnprintf( &out, format, valist );
834 /*********************************************************************
835 * _snwprintf (MSVCRT.@)
837 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
841 va_start(valist, format);
842 retval = MSVCRT_vsnwprintf(str, len, format, valist);
847 /*********************************************************************
850 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
855 va_start( ap, format );
856 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
861 /*********************************************************************
862 * swprintf (MSVCRT.@)
864 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
869 va_start( ap, format );
870 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
875 /*********************************************************************
876 * vswprintf (MSVCRT.@)
878 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, va_list args )
880 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
883 /*********************************************************************
884 * vswprintf_s (MSVCRT.@)
886 int CDECL MSVCRT_vswprintf_s( MSVCRT_wchar_t* str, MSVCRT_size_t num, const MSVCRT_wchar_t* format, va_list args )
888 /* FIXME: must handle positional arguments */
889 return MSVCRT_vsnwprintf( str, num, format, args );
892 /*********************************************************************
895 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
897 /* FIXME: handle collates */
898 return strcmpW( str1, str2 );
901 /*********************************************************************
904 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
906 const MSVCRT_wchar_t* p;
909 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
915 /*********************************************************************
918 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
920 thread_data_t *data = msvcrt_get_thread_data();
924 if (!(str = data->wcstok_next)) return NULL;
926 while (*str && strchrW( delim, *str )) str++;
927 if (!*str) return NULL;
929 while (*str && !strchrW( delim, *str )) str++;
930 if (*str) *str++ = 0;
931 data->wcstok_next = str;
936 /*********************************************************************
939 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
941 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
944 /*********************************************************************
945 * iswalnum (MSVCRT.@)
947 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
949 return isalnumW( wc );
952 /*********************************************************************
953 * iswalpha (MSVCRT.@)
955 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
957 return isalphaW( wc );
960 /*********************************************************************
961 * iswcntrl (MSVCRT.@)
963 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
965 return iscntrlW( wc );
968 /*********************************************************************
969 * iswdigit (MSVCRT.@)
971 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
973 return isdigitW( wc );
976 /*********************************************************************
977 * iswgraph (MSVCRT.@)
979 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
981 return isgraphW( wc );
984 /*********************************************************************
985 * iswlower (MSVCRT.@)
987 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
989 return islowerW( wc );
992 /*********************************************************************
993 * iswprint (MSVCRT.@)
995 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
997 return isprintW( wc );
1000 /*********************************************************************
1001 * iswpunct (MSVCRT.@)
1003 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1005 return ispunctW( wc );
1008 /*********************************************************************
1009 * iswspace (MSVCRT.@)
1011 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1013 return isspaceW( wc );
1016 /*********************************************************************
1017 * iswupper (MSVCRT.@)
1019 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1021 return isupperW( wc );
1024 /*********************************************************************
1025 * iswxdigit (MSVCRT.@)
1027 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1029 return isxdigitW( wc );
1032 /*********************************************************************
1033 * wcscpy_s (MSVCRT.@)
1035 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1037 MSVCRT_size_t size = 0;
1039 if(!wcDest || !numElement)
1040 return MSVCRT_EINVAL;
1046 return MSVCRT_EINVAL;
1049 size = strlenW(wcSrc) + 1;
1051 if(size > numElement)
1053 return MSVCRT_ERANGE;
1056 if(size > numElement)
1059 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1064 /******************************************************************
1065 * wcsncpy_s (MSVCRT.@)
1067 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1068 MSVCRT_size_t count )
1070 MSVCRT_size_t size = 0;
1072 if (!wcDest || !numElement)
1073 return MSVCRT_EINVAL;
1079 return MSVCRT_EINVAL;
1082 size = min(strlenW(wcSrc), count);
1084 if (size >= numElement)
1086 return MSVCRT_ERANGE;
1089 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1090 wcDest[size] = '\0';
1095 /******************************************************************
1096 * wcscat_s (MSVCRT.@)
1099 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1101 MSVCRT_wchar_t* ptr = dst;
1103 if (!dst || elem == 0) return MSVCRT_EINVAL;
1107 return MSVCRT_EINVAL;
1110 /* seek to end of dst string (or elem if no end of string is found */
1111 while (ptr < dst + elem && *ptr != '\0') ptr++;
1112 while (ptr < dst + elem)
1114 if ((*ptr++ = *src++) == '\0') return 0;
1116 /* not enough space */
1118 return MSVCRT_ERANGE;