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
22 #include "wine/port.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
36 /*********************************************************************
39 MSVCRT_wchar_t* CDECL _wcsdup( const MSVCRT_wchar_t* str )
41 MSVCRT_wchar_t* ret = NULL;
44 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
45 ret = MSVCRT_malloc( size );
46 if (ret) memcpy( ret, str, size );
51 /*********************************************************************
52 * _wcsicoll (MSVCRT.@)
54 INT CDECL _wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
56 /* FIXME: handle collates */
57 return strcmpiW( str1, str2 );
60 /*********************************************************************
63 MSVCRT_wchar_t* CDECL MSVCRT__wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
65 MSVCRT_wchar_t* ret = str;
66 while ((n-- > 0) && *str) *str++ = c;
70 /*********************************************************************
73 MSVCRT_wchar_t* CDECL _wcsrev( MSVCRT_wchar_t* str )
75 MSVCRT_wchar_t* ret = str;
76 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
79 MSVCRT_wchar_t t = *end;
86 /*********************************************************************
89 MSVCRT_wchar_t* CDECL _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
91 MSVCRT_wchar_t* ret = str;
92 while (*str) *str++ = c;
96 /******************************************************************
97 * _wcsupr_s (MSVCRT.@)
100 INT CDECL MSVCRT__wcsupr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
102 MSVCRT_wchar_t* ptr = str;
106 if (str) *str = '\0';
107 *MSVCRT__errno() = MSVCRT_EINVAL;
108 return MSVCRT_EINVAL;
114 *ptr = toupperW(*ptr);
118 /* MSDN claims that the function should return and set errno to
119 * ERANGE, which doesn't seem to be true based on the tests. */
121 *MSVCRT__errno() = MSVCRT_EINVAL;
122 return MSVCRT_EINVAL;
125 /*********************************************************************
126 * _wcstod_l - not exported in native msvcrt
128 double CDECL MSVCRT__wcstod_l(const MSVCRT_wchar_t* str, MSVCRT_wchar_t** end,
129 MSVCRT__locale_t locale)
131 unsigned __int64 d=0, hlp;
133 const MSVCRT_wchar_t *p;
137 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
138 *MSVCRT__errno() = MSVCRT_EINVAL;
143 locale = get_locale();
155 while(isdigitW(*p)) {
156 hlp = d*10+*(p++)-'0';
157 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
163 while(isdigitW(*p)) {
167 if(*p == *locale->locinfo->lconv->decimal_point)
170 while(isdigitW(*p)) {
171 hlp = d*10+*(p++)-'0';
172 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
183 *end = (MSVCRT_wchar_t*)str;
187 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
198 while(isdigitW(*p)) {
199 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
205 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
206 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
209 if(*p=='-' || *p=='+')
216 ret = (double)sign*d*pow(10, exp);
218 ret = (double)sign*d/pow(10, -exp);
220 if((d && ret==0.0) || isinf(ret))
221 *MSVCRT__errno() = MSVCRT_ERANGE;
224 *end = (MSVCRT_wchar_t*)p;
229 /*********************************************************************
230 * _wcstombs_s_l (MSVCRT.@)
232 int CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
233 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
234 MSVCRT_size_t count, MSVCRT__locale_t locale)
236 char default_char = '\0', *p;
242 if(!mbstr || !wcstr) {
243 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
246 *MSVCRT__errno() = MSVCRT_EINVAL;
247 return MSVCRT_EINVAL;
251 locale = get_locale();
255 else if(count==_TRUNCATE)
271 hlp = WideCharToMultiByte(locale->locinfo->lc_codepage,
272 WC_NO_BEST_FIT_CHARS, wcstr, 1, p, len, &default_char, NULL);
283 if(!len && size<=count) {
284 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
286 *MSVCRT__errno() = MSVCRT_ERANGE;
287 return MSVCRT_ERANGE;
296 /*********************************************************************
297 * wcstombs_s (MSVCRT.@)
299 int CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
300 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
302 return MSVCRT__wcstombs_s_l(ret, mbstr, size, wcstr, count, NULL);
305 /*********************************************************************
308 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
310 return MSVCRT__wcstod_l(lpszStr, end, NULL);
313 /*********************************************************************
316 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
318 return MSVCRT__wcstod_l(str, NULL, NULL);
321 /*********************************************************************
324 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
326 return MSVCRT__wcstod_l(str, NULL, locale);
329 typedef struct pf_output_t
340 typedef struct pf_flags_t
342 char Sign, LeftAlign, Alternate, PadZero;
343 int FieldLength, Precision;
344 char IntegerLength, IntegerDouble;
350 * writes a string of characters to the output
351 * returns -1 if the string doesn't fit in the output buffer
352 * return the length of the string if all characters were written
354 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
356 int space = out->len - out->used;
359 len = strlenW( str );
362 LPWSTR p = out->buf.W + out->used;
366 if (out->buf.W) memcpy( p, str, len*sizeof(WCHAR) );
370 if( space > 0 && out->buf.W )
371 memcpy( p, str, space*sizeof(WCHAR) );
376 int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
377 LPSTR p = out->buf.A + out->used;
381 if (out->buf.A) WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
385 if( space > 0 && out->buf.A )
386 WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
392 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
394 int space = out->len - out->used;
400 LPSTR p = out->buf.A + out->used;
404 if (out->buf.A) memcpy( p, str, len );
408 if( space > 0 && out->buf.A )
409 memcpy( p, str, space );
414 int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
415 LPWSTR p = out->buf.W + out->used;
419 if (out->buf.W) MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
423 if( space > 0 && out->buf.W )
424 MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
430 /* pf_fill: takes care of signs, alignment, zero and field padding */
431 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
435 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
438 if( left && flags->Sign )
440 flags->FieldLength--;
442 r = pf_output_stringA( out, &flags->Sign, 1 );
445 if( ( !left && flags->LeftAlign ) ||
446 ( left && !flags->LeftAlign ))
448 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
450 if( left && flags->PadZero )
451 r = pf_output_stringA( out, "0", 1 );
453 r = pf_output_stringA( out, " ", 1 );
457 if( left && flags->Sign && !flags->PadZero )
458 r = pf_output_stringA( out, &flags->Sign, 1 );
463 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
464 int len, pf_flags *flags )
469 len = strlenW( str );
471 if (flags->Precision >= 0 && flags->Precision < len)
472 len = flags->Precision;
474 r = pf_fill( out, len, flags, 1 );
477 r = pf_output_stringW( out, str, len );
480 r = pf_fill( out, len, flags, 0 );
485 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
486 int len, pf_flags *flags )
493 if (flags->Precision >= 0 && flags->Precision < len)
494 len = flags->Precision;
496 r = pf_fill( out, len, flags, 1 );
499 r = pf_output_stringA( out, str, len );
502 r = pf_fill( out, len, flags, 0 );
507 static int pf_handle_string_format( pf_output *out, const void* str, int len,
508 pf_flags *flags, BOOL capital_letter)
510 if(str == NULL) /* catch NULL pointer */
511 return pf_output_format_A( out, "(null)", -1, flags);
513 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
514 if(flags->WideString || flags->IntegerLength == 'l')
515 return pf_output_format_W( out, str, len, flags);
516 if(flags->IntegerLength == 'h')
517 return pf_output_format_A( out, str, len, flags);
519 /* %s,%c -> chars in ansi functions & wchars in unicode
520 * %S,%C -> wchars in ansi functions & chars in unicode */
521 if( capital_letter == out->unicode) /* either both TRUE or both FALSE */
522 return pf_output_format_A( out, str, len, flags);
524 return pf_output_format_W( out, str, len, flags);
527 static inline BOOL pf_is_integer_format( char fmt )
529 static const char float_fmts[] = "diouxX";
532 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
535 static inline BOOL pf_is_double_format( char fmt )
537 static const char float_fmts[] = "aeEfgG";
540 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
543 static inline BOOL pf_is_valid_format( char fmt )
545 static const char float_fmts[] = "acCdeEfgGinouxX";
548 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
551 static void pf_rebuild_format_string( char *p, pf_flags *flags )
556 if( flags->LeftAlign )
557 *p++ = flags->LeftAlign;
558 if( flags->Alternate )
559 *p++ = flags->Alternate;
561 *p++ = flags->PadZero;
562 if( flags->FieldLength )
564 sprintf(p, "%d", flags->FieldLength);
567 if( flags->Precision >= 0 )
569 sprintf(p, ".%d", flags->Precision);
572 *p++ = flags->Format;
576 /* pf_integer_conv: prints x to buf, including alternate formats and
577 additional precision digits, but not field characters or the sign */
578 static void pf_integer_conv( char *buf, int buf_len, pf_flags *flags,
585 char number[40], *tmp = number;
587 if( buf_len > sizeof number )
588 tmp = HeapAlloc( GetProcessHeap(), 0, buf_len );
591 if( flags->Format == 'o' )
593 else if( flags->Format == 'x' || flags->Format == 'X' )
596 if( flags->Format == 'X' )
597 digits = "0123456789ABCDEFX";
599 digits = "0123456789abcdefx";
601 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
607 /* Do conversion (backwards) */
609 if( x == 0 && flags->Precision )
614 j = (ULONGLONG) x % base;
615 x = (ULONGLONG) x / base;
616 tmp[i++] = digits[j];
618 k = flags->Precision - i;
621 if( flags->Alternate )
625 tmp[i++] = digits[16];
628 else if( base == 8 && tmp[i-1] != '0' )
632 /* Reverse for buf */
638 /* Adjust precision so pf_fill won't truncate the number later */
639 flags->Precision = strlen( buf );
642 HeapFree( GetProcessHeap(), 0, tmp );
647 /* pf_fixup_exponent: convert a string containing a 2 digit exponent
648 to 3 digits, accounting for padding, in place. Needed to match
649 the native printf's which always use 3 digits. */
650 static void pf_fixup_exponent( char *buf )
654 while (tmp[0] && toupper(tmp[0]) != 'E')
657 if (tmp[0] && (tmp[1] == '+' || tmp[1] == '-') &&
658 isdigit(tmp[2]) && isdigit(tmp[3]))
663 return; /* Exponent already 3 digits */
665 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
673 /* We didn't expand into trailing space, so this string isn't left
674 * justified. Terminate the string and strip a ' ' at the start of
675 * the string if there is one (as there may be if the string is
680 memmove(buf, buf + 1, (tmp - buf) + 3);
682 /* Otherwise, we expanded into trailing space -> nothing to do */
686 /*********************************************************************
687 * pf_vsnprintf (INTERNAL)
689 * implements both A and W vsnprintf functions
691 static int pf_vsnprintf( pf_output *out, const WCHAR *format,
692 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
695 LPCWSTR q, p = format;
699 locale = get_locale();
701 TRACE("format is %s\n",debugstr_w(format));
704 q = strchrW( p, '%' );
706 /* there's no % characters left, output the rest of the string */
709 r = pf_output_stringW(out, p, -1);
716 /* there's characters before the %, output them */
719 r = pf_output_stringW(out, p, q - p);
725 /* we must be at a % now, skip over it */
729 /* output a single % character */
732 r = pf_output_stringW(out, p++, 1);
738 /* parse the flags */
739 memset( &flags, 0, sizeof flags );
742 if( *p == '+' || *p == ' ' )
744 if ( flags.Sign != '+' )
748 flags.LeftAlign = *p;
752 flags.Alternate = *p;
758 /* deal with the field width specifier */
759 flags.FieldLength = 0;
762 flags.FieldLength = va_arg( valist, int );
763 if (flags.FieldLength < 0)
765 flags.LeftAlign = '-';
766 flags.FieldLength = -flags.FieldLength;
770 else while( isdigit(*p) )
772 flags.FieldLength *= 10;
773 flags.FieldLength += *p++ - '0';
776 /* deal with precision */
777 flags.Precision = -1;
784 flags.Precision = va_arg( valist, int );
787 else while( isdigit(*p) )
789 flags.Precision *= 10;
790 flags.Precision += *p++ - '0';
794 /* deal with integer width modifier */
797 if( *p == 'h' || *p == 'l' || *p == 'L' )
799 flags.IntegerLength = *p;
804 if( *(p+1) == '6' && *(p+2) == '4' )
806 flags.IntegerDouble++;
809 else if( *(p+1) == '3' && *(p+2) == '2' )
811 else if( isdigit(*(p+1)) || *(p+1) == 0 )
817 flags.WideString = *p++;
827 if (flags.Format == '$')
829 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
832 /* output a string */
833 if( flags.Format == 's' || flags.Format == 'S' )
834 r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
835 &flags, (flags.Format == 'S') );
837 /* output a single character */
838 else if( flags.Format == 'c' || flags.Format == 'C' )
840 INT ch = va_arg( valist, int );
842 r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
845 /* output a pointer */
846 else if( flags.Format == 'p' )
849 void *ptr = va_arg( valist, void * );
852 if( flags.Alternate )
853 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
855 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
856 r = pf_output_format_A( out, pointer, -1, &flags );
860 else if( flags.Format == 'n' )
862 int *x = va_arg(valist, int *);
866 /* deal with 64-bit integers */
867 else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
869 char number[40], *x = number;
871 /* Estimate largest possible required buffer size:
872 * Chooses the larger of the field or precision
873 * Includes extra bytes: 1 byte for null, 1 byte for sign,
874 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
875 for a decimal, and 1 byte for an additional float digit. */
876 int x_len = ((flags.FieldLength > flags.Precision) ?
877 flags.FieldLength : flags.Precision) + 10;
879 if( x_len >= sizeof number)
880 x = HeapAlloc( GetProcessHeap(), 0, x_len );
882 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
884 r = pf_output_format_A( out, x, -1, &flags );
886 HeapFree( GetProcessHeap(), 0, x );
889 /* deal with integers and floats using libc's printf */
890 else if( pf_is_valid_format( flags.Format ) )
892 char fmt[20], number[40], *x = number, *decimal_point;
894 /* Estimate largest possible required buffer size:
895 * Chooses the larger of the field or precision
896 * Includes extra bytes: 1 byte for null, 1 byte for sign,
897 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
898 for a decimal, and 1 byte for an additional float digit. */
899 int x_len = ((flags.FieldLength > flags.Precision) ?
900 flags.FieldLength : flags.Precision) + 10;
902 if( x_len >= sizeof number)
903 x = HeapAlloc( GetProcessHeap(), 0, x_len );
905 pf_rebuild_format_string( fmt, &flags );
907 if( pf_is_double_format( flags.Format ) )
909 sprintf( x, fmt, va_arg(valist, double) );
910 if (toupper(flags.Format) == 'E' || toupper(flags.Format) == 'G')
911 pf_fixup_exponent( x );
914 sprintf( x, fmt, va_arg(valist, int) );
916 decimal_point = strchr(x, '.');
918 *decimal_point = *locale->locinfo->lconv->decimal_point;
920 r = pf_output_stringA( out, x, -1 );
922 HeapFree( GetProcessHeap(), 0, x );
930 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
931 *MSVCRT__errno() = MSVCRT_EINVAL;
943 /* check we reached the end, and null terminate the string */
945 pf_output_stringW( out, p, 1 );
947 return out->used - 1;
950 /*********************************************************************
951 * vsnprintf_internal (INTERNAL)
953 static inline int vsnprintf_internal( char *str, MSVCRT_size_t len, const char *format,
954 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
957 LPWSTR formatW = NULL;
966 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
967 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
968 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
970 r = pf_vsnprintf( &out, formatW, locale, valid, valist );
972 HeapFree( GetProcessHeap(), 0, formatW );
977 /*********************************************************************
978 * _vsnprintf (MSVCRT.@)
980 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
981 const char *format, __ms_va_list valist )
983 return vsnprintf_internal(str, len, format, NULL, FALSE, valist);
986 /*********************************************************************
987 * _vsnprintf_l (MSVCRT.@)
989 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
990 MSVCRT__locale_t locale, __ms_va_list valist )
992 return vsnprintf_internal(str, len, format, locale, FALSE, valist);
995 /*********************************************************************
996 * _vsnprintf_s_l (MSVCRT.@)
998 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
999 MSVCRT_size_t count, const char *format,
1000 MSVCRT__locale_t locale, __ms_va_list valist )
1004 if(sizeOfBuffer<count+1 || count==-1)
1009 ret = vsnprintf_internal(str, len, format, locale, TRUE, valist);
1011 if(ret<0 || ret==len) {
1012 if(count!=_TRUNCATE && count>sizeOfBuffer) {
1013 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
1014 *MSVCRT__errno() = MSVCRT_ERANGE;
1015 memset(str, 0, sizeOfBuffer);
1025 /*********************************************************************
1026 * _vsnprintf_s (MSVCRT.@)
1028 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
1029 MSVCRT_size_t count, const char *format, __ms_va_list valist )
1031 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
1034 /*********************************************************************
1035 * vsprintf (MSVCRT.@)
1037 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
1039 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
1042 /*********************************************************************
1043 * vsprintf_s (MSVCRT.@)
1045 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
1047 return MSVCRT_vsnprintf(str, num, format, valist);
1050 /*********************************************************************
1051 * _vscprintf (MSVCRT.@)
1053 int CDECL _vscprintf( const char *format, __ms_va_list valist )
1055 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
1058 /*********************************************************************
1059 * _snprintf (MSVCRT.@)
1061 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
1064 __ms_va_list valist;
1065 __ms_va_start(valist, format);
1066 retval = MSVCRT_vsnprintf(str, len, format, valist);
1067 __ms_va_end(valist);
1071 /*********************************************************************
1072 * vsnwsprintf_internal (INTERNAL)
1074 static inline int vsnwprintf_internal(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1075 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, BOOL valid,
1076 __ms_va_list valist)
1085 return pf_vsnprintf( &out, format, locale, valid, valist );
1088 /*********************************************************************
1089 * _vsnwsprintf (MSVCRT.@)
1091 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1092 const MSVCRT_wchar_t *format, __ms_va_list valist)
1094 return vsnwprintf_internal(str, len, format, NULL, FALSE, valist);
1097 /*********************************************************************
1098 * _vsnwsprintf_l (MSVCRT.@)
1100 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1101 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
1102 __ms_va_list valist)
1104 return vsnwprintf_internal(str, len, format, locale, FALSE, valist);
1107 /*********************************************************************
1108 * _vsnwsprintf_s_l (MSVCRT.@)
1110 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1111 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1112 MSVCRT__locale_t locale, __ms_va_list valist)
1116 len = sizeOfBuffer/sizeof(MSVCRT_wchar_t);
1117 if(count!=-1 && len>count+1)
1120 ret = vsnwprintf_internal(str, len, format, locale, TRUE, valist);
1122 if(ret<0 || ret==len) {
1123 if(count!=_TRUNCATE && count>sizeOfBuffer/sizeof(MSVCRT_wchar_t)) {
1124 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
1125 *MSVCRT__errno() = MSVCRT_ERANGE;
1126 memset(str, 0, sizeOfBuffer);
1136 /*********************************************************************
1137 * _vsnwsprintf_s (MSVCRT.@)
1139 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1140 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
1142 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
1143 format, NULL, valist);
1146 /*********************************************************************
1147 * _snwprintf (MSVCRT.@)
1149 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
1152 __ms_va_list valist;
1153 __ms_va_start(valist, format);
1154 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1155 __ms_va_end(valist);
1159 /*********************************************************************
1160 * sprintf (MSVCRT.@)
1162 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
1167 __ms_va_start( ap, format );
1168 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1173 /*********************************************************************
1174 * sprintf_s (MSVCRT.@)
1176 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1181 __ms_va_start( ap, format );
1182 r = MSVCRT_vsnprintf( str, num, format, ap );
1187 /*********************************************************************
1188 * swprintf (MSVCRT.@)
1190 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1195 __ms_va_start( ap, format );
1196 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1201 /*********************************************************************
1202 * swprintf_s (MSVCRT.@)
1204 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1205 const MSVCRT_wchar_t *format, ... )
1210 __ms_va_start(ap, format);
1211 r = MSVCRT_vsnwprintf_s(str, numberOfElements*sizeof(MSVCRT_wchar_t),
1212 INT_MAX, format, ap);
1218 /*********************************************************************
1219 * vswprintf (MSVCRT.@)
1221 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1223 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1226 /*********************************************************************
1227 * _vscwprintf (MSVCRT.@)
1229 int CDECL _vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1231 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1234 /*********************************************************************
1235 * vswprintf_s (MSVCRT.@)
1237 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1238 const MSVCRT_wchar_t* format, __ms_va_list args)
1240 return MSVCRT_vsnwprintf_s(str, numberOfElements*sizeof(MSVCRT_wchar_t),
1241 INT_MAX, format, args);
1244 /*********************************************************************
1245 * _vswprintf_s_l (MSVCRT.@)
1247 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1248 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1250 return MSVCRT_vsnwprintf_s_l(str, numberOfElements*sizeof(MSVCRT_wchar_t),
1251 INT_MAX, format, locale, args );
1254 /*********************************************************************
1255 * wcscoll (MSVCRT.@)
1257 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1259 /* FIXME: handle collates */
1260 return strcmpW( str1, str2 );
1263 /*********************************************************************
1264 * wcspbrk (MSVCRT.@)
1266 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1268 const MSVCRT_wchar_t* p;
1271 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1277 /*********************************************************************
1280 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1282 thread_data_t *data = msvcrt_get_thread_data();
1283 MSVCRT_wchar_t *ret;
1286 if (!(str = data->wcstok_next)) return NULL;
1288 while (*str && strchrW( delim, *str )) str++;
1289 if (!*str) return NULL;
1291 while (*str && !strchrW( delim, *str )) str++;
1292 if (*str) *str++ = 0;
1293 data->wcstok_next = str;
1298 /*********************************************************************
1301 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1303 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
1306 /*********************************************************************
1307 * iswalnum (MSVCRT.@)
1309 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1311 return isalnumW( wc );
1314 /*********************************************************************
1315 * iswalpha (MSVCRT.@)
1317 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1319 return isalphaW( wc );
1322 /*********************************************************************
1323 * iswalpha_l (MSVCRT.@)
1325 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1327 return isalphaW( wc );
1330 /*********************************************************************
1331 * iswcntrl (MSVCRT.@)
1333 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1335 return iscntrlW( wc );
1338 /*********************************************************************
1339 * iswdigit (MSVCRT.@)
1341 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1343 return isdigitW( wc );
1346 /*********************************************************************
1347 * iswgraph (MSVCRT.@)
1349 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1351 return isgraphW( wc );
1354 /*********************************************************************
1355 * iswlower (MSVCRT.@)
1357 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1359 return islowerW( wc );
1362 /*********************************************************************
1363 * iswprint (MSVCRT.@)
1365 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1367 return isprintW( wc );
1370 /*********************************************************************
1371 * iswpunct (MSVCRT.@)
1373 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1375 return ispunctW( wc );
1378 /*********************************************************************
1379 * iswspace (MSVCRT.@)
1381 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1383 return isspaceW( wc );
1386 /*********************************************************************
1387 * iswupper (MSVCRT.@)
1389 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1391 return isupperW( wc );
1394 /*********************************************************************
1395 * iswxdigit (MSVCRT.@)
1397 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1399 return isxdigitW( wc );
1402 /*********************************************************************
1403 * wcscpy_s (MSVCRT.@)
1405 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1407 MSVCRT_size_t size = 0;
1409 if(!wcDest || !numElement)
1410 return MSVCRT_EINVAL;
1416 return MSVCRT_EINVAL;
1419 size = strlenW(wcSrc) + 1;
1421 if(size > numElement)
1423 return MSVCRT_ERANGE;
1426 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1431 /******************************************************************
1432 * wcsncpy_s (MSVCRT.@)
1434 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1435 MSVCRT_size_t count )
1437 MSVCRT_size_t size = 0;
1439 if (!wcDest || !numElement)
1440 return MSVCRT_EINVAL;
1446 return MSVCRT_EINVAL;
1449 size = min(strlenW(wcSrc), count);
1451 if (size >= numElement)
1453 return MSVCRT_ERANGE;
1456 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1457 wcDest[size] = '\0';
1462 /******************************************************************
1463 * wcscat_s (MSVCRT.@)
1466 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1468 MSVCRT_wchar_t* ptr = dst;
1470 if (!dst || elem == 0) return MSVCRT_EINVAL;
1474 return MSVCRT_EINVAL;
1477 /* seek to end of dst string (or elem if no end of string is found */
1478 while (ptr < dst + elem && *ptr != '\0') ptr++;
1479 while (ptr < dst + elem)
1481 if ((*ptr++ = *src++) == '\0') return 0;
1483 /* not enough space */
1485 return MSVCRT_ERANGE;
1488 /*********************************************************************
1489 * _wctoi64_l (MSVCR90.@)
1491 * FIXME: locale parameter is ignored
1493 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1494 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1496 BOOL negative = FALSE;
1499 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1501 if(!nptr || base<0 || base>36 || base==1) {
1502 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1506 while(isspaceW(*nptr)) nptr++;
1511 } else if(*nptr == '+')
1514 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1527 char cur = tolowerW(*nptr);
1535 if(cur<'a' || cur>='a'+base-10)
1545 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
1546 ret = MSVCRT_I64_MAX;
1547 *MSVCRT__errno() = MSVCRT_ERANGE;
1548 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
1549 ret = MSVCRT_I64_MIN;
1550 *MSVCRT__errno() = MSVCRT_ERANGE;
1556 *endptr = (MSVCRT_wchar_t*)nptr;
1561 /*********************************************************************
1562 * _wcstoi64 (MSVCR90.@)
1564 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
1565 MSVCRT_wchar_t **endptr, int base)
1567 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
1570 /*********************************************************************
1571 * _wcstoui64_l (MSVCR90.@)
1573 * FIXME: locale parameter is ignored
1575 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
1576 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1578 BOOL negative = FALSE;
1579 unsigned __int64 ret = 0;
1581 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1583 if(!nptr || base<0 || base>36 || base==1) {
1584 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1588 while(isspaceW(*nptr)) nptr++;
1593 } else if(*nptr == '+')
1596 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1609 char cur = tolowerW(*nptr);
1617 if(cur<'a' || cur>='a'+base-10)
1624 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
1625 ret = MSVCRT_UI64_MAX;
1626 *MSVCRT__errno() = MSVCRT_ERANGE;
1632 *endptr = (MSVCRT_wchar_t*)nptr;
1634 return negative ? -ret : ret;
1637 /*********************************************************************
1638 * _wcstoui64 (MSVCR90.@)
1640 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
1641 MSVCRT_wchar_t **endptr, int base)
1643 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);