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;
134 const MSVCRT_wchar_t *p;
136 BOOL found_digit = FALSE;
139 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
140 *MSVCRT__errno() = MSVCRT_EINVAL;
145 locale = get_locale();
157 while(isdigitW(*p)) {
159 hlp = d*10+*(p++)-'0';
160 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
166 while(isdigitW(*p)) {
170 if(*p == *locale->locinfo->lconv->decimal_point)
173 while(isdigitW(*p)) {
175 hlp = d*10+*(p++)-'0';
176 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
187 *end = (MSVCRT_wchar_t*)str;
191 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
202 while(isdigitW(*p)) {
203 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
209 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
210 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
213 if(*p=='-' || *p=='+')
219 fpcontrol = _control87(0, 0);
220 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
221 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
224 ret = (double)sign*d*pow(10, exp);
226 ret = (double)sign*d/pow(10, -exp);
228 _control87(fpcontrol, 0xffffffff);
230 if((d && ret==0.0) || isinf(ret))
231 *MSVCRT__errno() = MSVCRT_ERANGE;
234 *end = (MSVCRT_wchar_t*)p;
239 /*********************************************************************
240 * _wcstombs_l (MSVCRT.@)
242 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
243 MSVCRT_size_t count, MSVCRT__locale_t locale)
245 char default_char = '\0';
246 MSVCRT_size_t tmp = 0;
250 locale = get_locale();
253 return WideCharToMultiByte(locale->locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
254 wcstr, -1, NULL, 0, &default_char, &used_default)-1;
258 MSVCRT_size_t i, size;
260 size = WideCharToMultiByte(locale->locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
261 wcstr, 1, buf, 3, &default_char, &used_default);
267 for(i=0; i<size; i++)
268 mbstr[tmp++] = buf[i];
277 /*********************************************************************
278 * wcstombs (MSVCRT.@)
280 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
283 return MSVCRT__wcstombs_l(mbstr, wcstr, count, NULL);
286 /*********************************************************************
287 * _wcstombs_s_l (MSVCRT.@)
289 MSVCRT_size_t CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
290 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
291 MSVCRT_size_t count, MSVCRT__locale_t locale)
295 if(!mbstr && !size) {
296 conv = MSVCRT__wcstombs_l(NULL, wcstr, 0, locale);
302 if(!wcstr || !mbstr) {
303 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
306 *MSVCRT__errno() = MSVCRT_EINVAL;
307 return MSVCRT_EINVAL;
310 if(count==MSVCRT__TRUNCATE || size<count)
315 conv = MSVCRT__wcstombs_l(mbstr, wcstr, conv, locale);
317 mbstr[conv++] = '\0';
318 else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
319 mbstr[conv-1] = '\0';
321 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
324 *MSVCRT__errno() = MSVCRT_ERANGE;
325 return MSVCRT_ERANGE;
333 /*********************************************************************
334 * wcstombs_s (MSVCRT.@)
336 MSVCRT_size_t CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
337 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
339 return MSVCRT__wcstombs_s_l(ret, mbstr, size, wcstr, count, NULL);
342 /*********************************************************************
345 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
347 return MSVCRT__wcstod_l(lpszStr, end, NULL);
350 /*********************************************************************
353 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
355 return MSVCRT__wcstod_l(str, NULL, NULL);
358 /*********************************************************************
361 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
363 return MSVCRT__wcstod_l(str, NULL, locale);
366 typedef struct pf_output_t
377 typedef struct pf_flags_t
379 char Sign, LeftAlign, Alternate, PadZero;
380 int FieldLength, Precision;
381 char IntegerLength, IntegerDouble;
387 * writes a string of characters to the output
388 * returns -1 if the string doesn't fit in the output buffer
389 * return the length of the string if all characters were written
391 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
393 int space = out->len - out->used;
396 len = strlenW( str );
399 LPWSTR p = out->buf.W + out->used;
403 if (out->buf.W) memcpy( p, str, len*sizeof(WCHAR) );
407 if( space > 0 && out->buf.W )
408 memcpy( p, str, space*sizeof(WCHAR) );
413 int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
414 LPSTR p = out->buf.A + out->used;
418 if (out->buf.A) WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
422 if( space > 0 && out->buf.A )
423 WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
429 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
431 int space = out->len - out->used;
437 LPSTR p = out->buf.A + out->used;
441 if (out->buf.A) memcpy( p, str, len );
445 if( space > 0 && out->buf.A )
446 memcpy( p, str, space );
451 int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
452 LPWSTR p = out->buf.W + out->used;
456 if (out->buf.W) MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
460 if( space > 0 && out->buf.W )
461 MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
467 /* pf_fill: takes care of signs, alignment, zero and field padding */
468 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
472 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
475 if( left && flags->Sign )
477 flags->FieldLength--;
479 r = pf_output_stringA( out, &flags->Sign, 1 );
482 if( ( !left && flags->LeftAlign ) ||
483 ( left && !flags->LeftAlign ))
485 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
487 if( left && flags->PadZero )
488 r = pf_output_stringA( out, "0", 1 );
490 r = pf_output_stringA( out, " ", 1 );
494 if( left && flags->Sign && !flags->PadZero )
495 r = pf_output_stringA( out, &flags->Sign, 1 );
500 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
501 int len, pf_flags *flags )
506 len = strlenW( str );
508 if (flags->Precision >= 0 && flags->Precision < len)
509 len = flags->Precision;
511 r = pf_fill( out, len, flags, 1 );
514 r = pf_output_stringW( out, str, len );
517 r = pf_fill( out, len, flags, 0 );
522 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
523 int len, pf_flags *flags )
530 if (flags->Precision >= 0 && flags->Precision < len)
531 len = flags->Precision;
533 r = pf_fill( out, len, flags, 1 );
536 r = pf_output_stringA( out, str, len );
539 r = pf_fill( out, len, flags, 0 );
544 static int pf_handle_string_format( pf_output *out, const void* str, int len,
545 pf_flags *flags, BOOL capital_letter)
547 if(str == NULL) /* catch NULL pointer */
548 return pf_output_format_A( out, "(null)", -1, flags);
550 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
551 if(flags->WideString || flags->IntegerLength == 'l')
552 return pf_output_format_W( out, str, len, flags);
553 if(flags->IntegerLength == 'h')
554 return pf_output_format_A( out, str, len, flags);
556 /* %s,%c -> chars in ansi functions & wchars in unicode
557 * %S,%C -> wchars in ansi functions & chars in unicode */
558 if( capital_letter == out->unicode) /* either both TRUE or both FALSE */
559 return pf_output_format_A( out, str, len, flags);
561 return pf_output_format_W( out, str, len, flags);
564 static inline BOOL pf_is_integer_format( char fmt )
566 static const char float_fmts[] = "diouxX";
569 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
572 static inline BOOL pf_is_double_format( char fmt )
574 static const char float_fmts[] = "aeEfgG";
577 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
580 static inline BOOL pf_is_valid_format( char fmt )
582 static const char float_fmts[] = "acCdeEfgGinouxX";
585 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
588 static void pf_rebuild_format_string( char *p, pf_flags *flags )
593 if( flags->LeftAlign )
594 *p++ = flags->LeftAlign;
595 if( flags->Alternate )
596 *p++ = flags->Alternate;
598 *p++ = flags->PadZero;
599 if( flags->FieldLength )
601 sprintf(p, "%d", flags->FieldLength);
604 if( flags->Precision >= 0 )
606 sprintf(p, ".%d", flags->Precision);
609 *p++ = flags->Format;
613 /* pf_integer_conv: prints x to buf, including alternate formats and
614 additional precision digits, but not field characters or the sign */
615 static void pf_integer_conv( char *buf, int buf_len, pf_flags *flags,
622 char number[40], *tmp = number;
624 if( buf_len > sizeof number )
625 tmp = HeapAlloc( GetProcessHeap(), 0, buf_len );
628 if( flags->Format == 'o' )
630 else if( flags->Format == 'x' || flags->Format == 'X' )
633 if( flags->Format == 'X' )
634 digits = "0123456789ABCDEFX";
636 digits = "0123456789abcdefx";
638 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
644 /* Do conversion (backwards) */
646 if( x == 0 && flags->Precision )
651 j = (ULONGLONG) x % base;
652 x = (ULONGLONG) x / base;
653 tmp[i++] = digits[j];
655 k = flags->Precision - i;
658 if( flags->Alternate )
662 tmp[i++] = digits[16];
665 else if( base == 8 && tmp[i-1] != '0' )
669 /* Reverse for buf */
675 /* Adjust precision so pf_fill won't truncate the number later */
676 flags->Precision = strlen( buf );
679 HeapFree( GetProcessHeap(), 0, tmp );
684 /* pf_fixup_exponent: convert a string containing a 2 digit exponent
685 to 3 digits, accounting for padding, in place. Needed to match
686 the native printf's which always use 3 digits. */
687 static void pf_fixup_exponent( char *buf )
691 while (tmp[0] && toupper(tmp[0]) != 'E')
694 if (tmp[0] && (tmp[1] == '+' || tmp[1] == '-') &&
695 isdigit(tmp[2]) && isdigit(tmp[3]))
700 return; /* Exponent already 3 digits */
702 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
710 /* We didn't expand into trailing space, so this string isn't left
711 * justified. Terminate the string and strip a ' ' at the start of
712 * the string if there is one (as there may be if the string is
717 memmove(buf, buf + 1, (tmp - buf) + 3);
719 /* Otherwise, we expanded into trailing space -> nothing to do */
723 /*********************************************************************
724 * pf_vsnprintf (INTERNAL)
726 * implements both A and W vsnprintf functions
728 static int pf_vsnprintf( pf_output *out, const WCHAR *format,
729 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
732 LPCWSTR q, p = format;
736 locale = get_locale();
738 TRACE("format is %s\n",debugstr_w(format));
741 q = strchrW( p, '%' );
743 /* there's no % characters left, output the rest of the string */
746 r = pf_output_stringW(out, p, -1);
753 /* there's characters before the %, output them */
756 r = pf_output_stringW(out, p, q - p);
762 /* we must be at a % now, skip over it */
766 /* output a single % character */
769 r = pf_output_stringW(out, p++, 1);
775 /* parse the flags */
776 memset( &flags, 0, sizeof flags );
779 if( *p == '+' || *p == ' ' )
781 if ( flags.Sign != '+' )
785 flags.LeftAlign = *p;
789 flags.Alternate = *p;
795 /* deal with the field width specifier */
796 flags.FieldLength = 0;
799 flags.FieldLength = va_arg( valist, int );
800 if (flags.FieldLength < 0)
802 flags.LeftAlign = '-';
803 flags.FieldLength = -flags.FieldLength;
807 else while( isdigit(*p) )
809 flags.FieldLength *= 10;
810 flags.FieldLength += *p++ - '0';
813 /* deal with precision */
814 flags.Precision = -1;
821 flags.Precision = va_arg( valist, int );
824 else while( isdigit(*p) )
826 flags.Precision *= 10;
827 flags.Precision += *p++ - '0';
831 /* deal with integer width modifier */
834 if( *p == 'l' && *(p+1) == 'l' )
836 flags.IntegerDouble++;
839 else if( *p == 'h' || *p == 'l' || *p == 'L' )
841 flags.IntegerLength = *p;
846 if( *(p+1) == '6' && *(p+2) == '4' )
848 flags.IntegerDouble++;
851 else if( *(p+1) == '3' && *(p+2) == '2' )
853 else if( isdigit(*(p+1)) || *(p+1) == 0 )
859 flags.WideString = *p++;
869 if (flags.Format == '$')
871 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
874 /* output a string */
875 if( flags.Format == 's' || flags.Format == 'S' )
876 r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
877 &flags, (flags.Format == 'S') );
879 /* output a single character */
880 else if( flags.Format == 'c' || flags.Format == 'C' )
882 INT ch = va_arg( valist, int );
884 r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
887 /* output a pointer */
888 else if( flags.Format == 'p' )
891 void *ptr = va_arg( valist, void * );
894 if( flags.Alternate )
895 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
897 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
898 r = pf_output_format_A( out, pointer, -1, &flags );
902 else if( flags.Format == 'n' )
904 int *x = va_arg(valist, int *);
908 /* deal with 64-bit integers */
909 else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
911 char number[40], *x = number;
913 /* Estimate largest possible required buffer size:
914 * Chooses the larger of the field or precision
915 * Includes extra bytes: 1 byte for null, 1 byte for sign,
916 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
917 for a decimal, and 1 byte for an additional float digit. */
918 int x_len = ((flags.FieldLength > flags.Precision) ?
919 flags.FieldLength : flags.Precision) + 10;
921 if( x_len >= sizeof number)
922 x = HeapAlloc( GetProcessHeap(), 0, x_len );
924 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
926 r = pf_output_format_A( out, x, -1, &flags );
928 HeapFree( GetProcessHeap(), 0, x );
931 /* deal with integers and floats using libc's printf */
932 else if( pf_is_valid_format( flags.Format ) )
934 char fmt[20], number[40], *x = number, *decimal_point;
936 /* Estimate largest possible required buffer size:
937 * Chooses the larger of the field or precision
938 * Includes extra bytes: 1 byte for null, 1 byte for sign,
939 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
940 for a decimal, and 1 byte for an additional float digit. */
941 int x_len = ((flags.FieldLength > flags.Precision) ?
942 flags.FieldLength : flags.Precision) + 10;
944 if( x_len >= sizeof number)
945 x = HeapAlloc( GetProcessHeap(), 0, x_len );
947 pf_rebuild_format_string( fmt, &flags );
949 if( pf_is_double_format( flags.Format ) )
951 sprintf( x, fmt, va_arg(valist, double) );
952 if (toupper(flags.Format) == 'E' || toupper(flags.Format) == 'G')
953 pf_fixup_exponent( x );
956 sprintf( x, fmt, va_arg(valist, int) );
958 decimal_point = strchr(x, '.');
960 *decimal_point = *locale->locinfo->lconv->decimal_point;
962 r = pf_output_stringA( out, x, -1 );
964 HeapFree( GetProcessHeap(), 0, x );
972 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
973 *MSVCRT__errno() = MSVCRT_EINVAL;
985 /* check we reached the end, and null terminate the string */
987 pf_output_stringW( out, p, 1 );
989 return out->used - 1;
992 /*********************************************************************
993 * vsnprintf_internal (INTERNAL)
995 static inline int vsnprintf_internal( char *str, MSVCRT_size_t len, const char *format,
996 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
999 LPWSTR formatW = NULL;
1003 out.unicode = FALSE;
1008 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
1009 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
1010 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
1012 r = pf_vsnprintf( &out, formatW, locale, valid, valist );
1014 HeapFree( GetProcessHeap(), 0, formatW );
1019 /*********************************************************************
1020 * _vsnprintf (MSVCRT.@)
1022 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
1023 const char *format, __ms_va_list valist )
1025 return vsnprintf_internal(str, len, format, NULL, FALSE, valist);
1028 /*********************************************************************
1029 * _vsnprintf_l (MSVCRT.@)
1031 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
1032 MSVCRT__locale_t locale, __ms_va_list valist )
1034 return vsnprintf_internal(str, len, format, locale, FALSE, valist);
1037 /*********************************************************************
1038 * _vsnprintf_s_l (MSVCRT.@)
1040 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
1041 MSVCRT_size_t count, const char *format,
1042 MSVCRT__locale_t locale, __ms_va_list valist )
1046 if(sizeOfBuffer<count+1 || count==-1)
1051 ret = vsnprintf_internal(str, len, format, locale, TRUE, valist);
1053 if(ret<0 || ret==len) {
1054 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
1055 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
1056 *MSVCRT__errno() = MSVCRT_ERANGE;
1057 memset(str, 0, sizeOfBuffer);
1067 /*********************************************************************
1068 * _vsnprintf_s (MSVCRT.@)
1070 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
1071 MSVCRT_size_t count, const char *format, __ms_va_list valist )
1073 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
1076 /*********************************************************************
1077 * vsprintf (MSVCRT.@)
1079 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
1081 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
1084 /*********************************************************************
1085 * vsprintf_s (MSVCRT.@)
1087 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
1089 return MSVCRT_vsnprintf(str, num, format, valist);
1092 /*********************************************************************
1093 * _vscprintf (MSVCRT.@)
1095 int CDECL _vscprintf( const char *format, __ms_va_list valist )
1097 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
1100 /*********************************************************************
1101 * _snprintf (MSVCRT.@)
1103 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
1106 __ms_va_list valist;
1107 __ms_va_start(valist, format);
1108 retval = MSVCRT_vsnprintf(str, len, format, valist);
1109 __ms_va_end(valist);
1113 /*********************************************************************
1114 * _snprintf_s (MSVCRT.@)
1116 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
1117 const char *format, ...)
1120 __ms_va_list valist;
1121 __ms_va_start(valist, format);
1122 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
1123 __ms_va_end(valist);
1127 /*********************************************************************
1128 * _scprintf (MSVCRT.@)
1130 int CDECL MSVCRT__scprintf(const char *format, ...)
1133 __ms_va_list valist;
1134 __ms_va_start(valist, format);
1135 retval = _vscprintf(format, valist);
1136 __ms_va_end(valist);
1140 /*********************************************************************
1141 * vsnwprintf_internal (INTERNAL)
1143 static inline int vsnwprintf_internal(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1144 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, BOOL valid,
1145 __ms_va_list valist)
1154 return pf_vsnprintf( &out, format, locale, valid, valist );
1157 /*********************************************************************
1158 * _vsnwprintf (MSVCRT.@)
1160 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1161 const MSVCRT_wchar_t *format, __ms_va_list valist)
1163 return vsnwprintf_internal(str, len, format, NULL, FALSE, valist);
1166 /*********************************************************************
1167 * _vsnwprintf_l (MSVCRT.@)
1169 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1170 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
1171 __ms_va_list valist)
1173 return vsnwprintf_internal(str, len, format, locale, FALSE, valist);
1176 /*********************************************************************
1177 * _vsnwprintf_s_l (MSVCRT.@)
1179 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1180 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1181 MSVCRT__locale_t locale, __ms_va_list valist)
1186 if(count!=-1 && len>count+1)
1189 ret = vsnwprintf_internal(str, len, format, locale, TRUE, valist);
1191 if(ret<0 || ret==len) {
1192 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
1193 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
1194 *MSVCRT__errno() = MSVCRT_ERANGE;
1195 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
1205 /*********************************************************************
1206 * _vsnwprintf_s (MSVCRT.@)
1208 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1209 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
1211 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
1212 format, NULL, valist);
1215 /*********************************************************************
1216 * _snwprintf (MSVCRT.@)
1218 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
1221 __ms_va_list valist;
1222 __ms_va_start(valist, format);
1223 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1224 __ms_va_end(valist);
1228 /*********************************************************************
1229 * _snwprintf_s (MSVCRT.@)
1231 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1232 const MSVCRT_wchar_t *format, ...)
1235 __ms_va_list valist;
1236 __ms_va_start(valist, format);
1237 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
1238 __ms_va_end(valist);
1242 /*********************************************************************
1243 * sprintf (MSVCRT.@)
1245 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
1250 __ms_va_start( ap, format );
1251 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1256 /*********************************************************************
1257 * sprintf_s (MSVCRT.@)
1259 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1264 __ms_va_start( ap, format );
1265 r = MSVCRT_vsnprintf( str, num, format, ap );
1270 /*********************************************************************
1271 * _scwprintf (MSVCRT.@)
1273 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
1278 __ms_va_start( ap, format );
1279 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
1284 /*********************************************************************
1285 * swprintf (MSVCRT.@)
1287 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1292 __ms_va_start( ap, format );
1293 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1298 /*********************************************************************
1299 * swprintf_s (MSVCRT.@)
1301 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1302 const MSVCRT_wchar_t *format, ... )
1307 __ms_va_start(ap, format);
1308 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
1314 /*********************************************************************
1315 * vswprintf (MSVCRT.@)
1317 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1319 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1322 /*********************************************************************
1323 * _vscwprintf (MSVCRT.@)
1325 int CDECL _vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1327 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1330 /*********************************************************************
1331 * vswprintf_s (MSVCRT.@)
1333 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1334 const MSVCRT_wchar_t* format, __ms_va_list args)
1336 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1339 /*********************************************************************
1340 * _vswprintf_s_l (MSVCRT.@)
1342 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1343 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1345 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1346 format, locale, args );
1349 /*********************************************************************
1350 * wcscoll (MSVCRT.@)
1352 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1354 /* FIXME: handle collates */
1355 return strcmpW( str1, str2 );
1358 /*********************************************************************
1359 * wcspbrk (MSVCRT.@)
1361 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1363 const MSVCRT_wchar_t* p;
1366 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1372 /*********************************************************************
1375 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1377 thread_data_t *data = msvcrt_get_thread_data();
1378 MSVCRT_wchar_t *ret;
1381 if (!(str = data->wcstok_next)) return NULL;
1383 while (*str && strchrW( delim, *str )) str++;
1384 if (!*str) return NULL;
1386 while (*str && !strchrW( delim, *str )) str++;
1387 if (*str) *str++ = 0;
1388 data->wcstok_next = str;
1393 /*********************************************************************
1396 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1398 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
1401 /*********************************************************************
1402 * iswalnum (MSVCRT.@)
1404 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1406 return isalnumW( wc );
1409 /*********************************************************************
1410 * iswalpha (MSVCRT.@)
1412 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1414 return isalphaW( wc );
1417 /*********************************************************************
1418 * iswalpha_l (MSVCRT.@)
1420 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1422 return isalphaW( wc );
1425 /*********************************************************************
1426 * iswcntrl (MSVCRT.@)
1428 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1430 return iscntrlW( wc );
1433 /*********************************************************************
1434 * iswdigit (MSVCRT.@)
1436 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1438 return isdigitW( wc );
1441 /*********************************************************************
1442 * iswgraph (MSVCRT.@)
1444 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1446 return isgraphW( wc );
1449 /*********************************************************************
1450 * iswlower (MSVCRT.@)
1452 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1454 return islowerW( wc );
1457 /*********************************************************************
1458 * iswprint (MSVCRT.@)
1460 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1462 return isprintW( wc );
1465 /*********************************************************************
1466 * iswpunct (MSVCRT.@)
1468 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1470 return ispunctW( wc );
1473 /*********************************************************************
1474 * iswspace (MSVCRT.@)
1476 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1478 return isspaceW( wc );
1481 /*********************************************************************
1482 * iswupper (MSVCRT.@)
1484 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1486 return isupperW( wc );
1489 /*********************************************************************
1490 * iswxdigit (MSVCRT.@)
1492 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1494 return isxdigitW( wc );
1497 /*********************************************************************
1498 * wcscpy_s (MSVCRT.@)
1500 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1502 MSVCRT_size_t size = 0;
1504 if(!wcDest || !numElement)
1505 return MSVCRT_EINVAL;
1511 return MSVCRT_EINVAL;
1514 size = strlenW(wcSrc) + 1;
1516 if(size > numElement)
1518 return MSVCRT_ERANGE;
1521 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1526 /******************************************************************
1527 * wcsncpy_s (MSVCRT.@)
1529 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1530 MSVCRT_size_t count )
1532 MSVCRT_size_t size = 0;
1534 if (!wcDest || !numElement)
1535 return MSVCRT_EINVAL;
1541 return MSVCRT_EINVAL;
1544 size = min(strlenW(wcSrc), count);
1546 if (size >= numElement)
1548 return MSVCRT_ERANGE;
1551 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1552 wcDest[size] = '\0';
1557 /******************************************************************
1558 * wcscat_s (MSVCRT.@)
1561 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1563 MSVCRT_wchar_t* ptr = dst;
1565 if (!dst || elem == 0) return MSVCRT_EINVAL;
1569 return MSVCRT_EINVAL;
1572 /* seek to end of dst string (or elem if no end of string is found */
1573 while (ptr < dst + elem && *ptr != '\0') ptr++;
1574 while (ptr < dst + elem)
1576 if ((*ptr++ = *src++) == '\0') return 0;
1578 /* not enough space */
1580 return MSVCRT_ERANGE;
1583 /*********************************************************************
1584 * _wcstoi64_l (MSVCRT.@)
1586 * FIXME: locale parameter is ignored
1588 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1589 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1591 BOOL negative = FALSE;
1594 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1596 if(!nptr || base<0 || base>36 || base==1) {
1597 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1601 while(isspaceW(*nptr)) nptr++;
1606 } else if(*nptr == '+')
1609 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1622 char cur = tolowerW(*nptr);
1630 if(cur<'a' || cur>='a'+base-10)
1640 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
1641 ret = MSVCRT_I64_MAX;
1642 *MSVCRT__errno() = MSVCRT_ERANGE;
1643 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
1644 ret = MSVCRT_I64_MIN;
1645 *MSVCRT__errno() = MSVCRT_ERANGE;
1651 *endptr = (MSVCRT_wchar_t*)nptr;
1656 /*********************************************************************
1657 * _wcstoi64 (MSVCRT.@)
1659 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
1660 MSVCRT_wchar_t **endptr, int base)
1662 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
1665 /*********************************************************************
1666 * _wcstoui64_l (MSVCRT.@)
1668 * FIXME: locale parameter is ignored
1670 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
1671 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1673 BOOL negative = FALSE;
1674 unsigned __int64 ret = 0;
1676 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1678 if(!nptr || base<0 || base>36 || base==1) {
1679 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1683 while(isspaceW(*nptr)) nptr++;
1688 } else if(*nptr == '+')
1691 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1704 char cur = tolowerW(*nptr);
1712 if(cur<'a' || cur>='a'+base-10)
1719 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
1720 ret = MSVCRT_UI64_MAX;
1721 *MSVCRT__errno() = MSVCRT_ERANGE;
1727 *endptr = (MSVCRT_wchar_t*)nptr;
1729 return negative ? -ret : ret;
1732 /*********************************************************************
1733 * _wcstoui64 (MSVCRT.@)
1735 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
1736 MSVCRT_wchar_t **endptr, int base)
1738 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
1741 /******************************************************************
1742 * wcsnlen (MSVCRT.@)
1744 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
1748 for (i = 0; i < maxlen; i++)