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 /*********************************************************************
61 * _wcsnicoll (MSVCRT.@)
63 INT CDECL _wcsnicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count )
65 /* FIXME: handle collates */
66 return strncmpiW( str1, str2, count );
69 /*********************************************************************
72 MSVCRT_wchar_t* CDECL MSVCRT__wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
74 MSVCRT_wchar_t* ret = str;
75 while ((n-- > 0) && *str) *str++ = c;
79 /*********************************************************************
82 MSVCRT_wchar_t* CDECL _wcsrev( MSVCRT_wchar_t* str )
84 MSVCRT_wchar_t* ret = str;
85 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
88 MSVCRT_wchar_t t = *end;
95 /*********************************************************************
98 MSVCRT_wchar_t* CDECL _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
100 MSVCRT_wchar_t* ret = str;
101 while (*str) *str++ = c;
105 /******************************************************************
106 * _wcsupr_s_l (MSVCRT.@)
108 int CDECL MSVCRT__wcsupr_s_l( MSVCRT_wchar_t* str, MSVCRT_size_t n,
109 MSVCRT__locale_t locale )
111 MSVCRT_wchar_t* ptr = str;
115 if (str) *str = '\0';
116 *MSVCRT__errno() = MSVCRT_EINVAL;
117 return MSVCRT_EINVAL;
123 /* FIXME: add locale support */
124 *ptr = toupperW(*ptr);
128 /* MSDN claims that the function should return and set errno to
129 * ERANGE, which doesn't seem to be true based on the tests. */
131 *MSVCRT__errno() = MSVCRT_EINVAL;
132 return MSVCRT_EINVAL;
135 /******************************************************************
136 * _wcsupr_s (MSVCRT.@)
139 INT CDECL MSVCRT__wcsupr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
141 return MSVCRT__wcsupr_s_l( str, n, NULL );
144 /******************************************************************
145 * _wcslwr_s (MSVCRT.@)
147 int CDECL MSVCRT__wcslwr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
149 MSVCRT_wchar_t* ptr = str;
153 if (str) *str = '\0';
154 *MSVCRT__errno() = MSVCRT_EINVAL;
155 return MSVCRT_EINVAL;
161 *ptr = tolowerW(*ptr);
165 /* MSDN claims that the function should return and set errno to
166 * ERANGE, which doesn't seem to be true based on the tests. */
168 *MSVCRT__errno() = MSVCRT_EINVAL;
169 return MSVCRT_EINVAL;
172 /*********************************************************************
173 * _wcstod_l - not exported in native msvcrt
175 double CDECL MSVCRT__wcstod_l(const MSVCRT_wchar_t* str, MSVCRT_wchar_t** end,
176 MSVCRT__locale_t locale)
178 unsigned __int64 d=0, hlp;
181 const MSVCRT_wchar_t *p;
183 BOOL found_digit = FALSE;
185 if (!MSVCRT_CHECK_PMT(str != NULL)) {
186 *MSVCRT__errno() = MSVCRT_EINVAL;
191 locale = get_locale();
203 while(isdigitW(*p)) {
205 hlp = d*10+*(p++)-'0';
206 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
212 while(isdigitW(*p)) {
216 if(*p == *locale->locinfo->lconv->decimal_point)
219 while(isdigitW(*p)) {
221 hlp = d*10+*(p++)-'0';
222 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
233 *end = (MSVCRT_wchar_t*)str;
237 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
248 while(isdigitW(*p)) {
249 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
255 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
256 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
259 if(*p=='-' || *p=='+')
265 fpcontrol = _control87(0, 0);
266 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
267 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
270 ret = (double)sign*d*pow(10, exp);
272 ret = (double)sign*d/pow(10, -exp);
274 _control87(fpcontrol, 0xffffffff);
276 if((d && ret==0.0) || isinf(ret))
277 *MSVCRT__errno() = MSVCRT_ERANGE;
280 *end = (MSVCRT_wchar_t*)p;
285 /*********************************************************************
286 * _wcstombs_l (MSVCRT.@)
288 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
289 MSVCRT_size_t count, MSVCRT__locale_t locale)
291 char default_char = '\0';
292 MSVCRT_size_t tmp = 0;
296 locale = get_locale();
299 return WideCharToMultiByte(locale->locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
300 wcstr, -1, NULL, 0, &default_char, &used_default)-1;
304 MSVCRT_size_t i, size;
306 size = WideCharToMultiByte(locale->locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
307 wcstr, 1, buf, 3, &default_char, &used_default);
313 for(i=0; i<size; i++)
314 mbstr[tmp++] = buf[i];
323 /*********************************************************************
324 * wcstombs (MSVCRT.@)
326 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
329 return MSVCRT__wcstombs_l(mbstr, wcstr, count, NULL);
332 /*********************************************************************
333 * _wcstombs_s_l (MSVCRT.@)
335 MSVCRT_size_t CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
336 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
337 MSVCRT_size_t count, MSVCRT__locale_t locale)
341 if(!mbstr && !size) {
342 conv = MSVCRT__wcstombs_l(NULL, wcstr, 0, locale);
348 if (!MSVCRT_CHECK_PMT(wcstr != NULL) || !MSVCRT_CHECK_PMT(mbstr != NULL)) {
351 *MSVCRT__errno() = MSVCRT_EINVAL;
352 return MSVCRT_EINVAL;
355 if(count==MSVCRT__TRUNCATE || size<count)
360 conv = MSVCRT__wcstombs_l(mbstr, wcstr, conv, locale);
362 mbstr[conv++] = '\0';
363 else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
364 mbstr[conv-1] = '\0';
366 MSVCRT_INVALID_PMT("mbstr[size] is too small");
369 *MSVCRT__errno() = MSVCRT_ERANGE;
370 return MSVCRT_ERANGE;
378 /*********************************************************************
379 * wcstombs_s (MSVCRT.@)
381 MSVCRT_size_t CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
382 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
384 return MSVCRT__wcstombs_s_l(ret, mbstr, size, wcstr, count, NULL);
387 /*********************************************************************
390 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
392 return MSVCRT__wcstod_l(lpszStr, end, NULL);
395 /*********************************************************************
398 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
400 return MSVCRT__wcstod_l(str, NULL, NULL);
403 /*********************************************************************
406 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
408 return MSVCRT__wcstod_l(str, NULL, locale);
411 typedef struct pf_flags_t
413 char Sign, LeftAlign, Alternate, PadZero;
414 int FieldLength, Precision;
415 char IntegerLength, IntegerDouble;
420 static inline BOOL pf_is_auto_grow(pf_output *out)
422 return (out->unicode) ? !!out->grow.W : !!out->grow.A;
425 static inline int pf_check_auto_grow(pf_output *out, unsigned delta)
427 if (pf_is_auto_grow(out) && out->used + delta > out->len)
429 out->len = max(out->len * 2, out->used + delta);
432 if (out->buf.W != out->grow.W)
433 out->buf.W = MSVCRT_realloc(out->buf.W, out->len * sizeof(WCHAR));
435 out->buf.W = MSVCRT_malloc(out->len * sizeof(WCHAR));
436 if (!out->buf.W) return -1;
440 if (out->buf.A != out->grow.A)
441 out->buf.A = MSVCRT_realloc(out->buf.A, out->len * sizeof(char));
443 out->buf.A = MSVCRT_malloc(out->len * sizeof(char));
444 if (!out->buf.A) return -1;
451 * writes a string of characters to the output
452 * returns -1 if the string doesn't fit in the output buffer
453 * return the length of the string if all characters were written
455 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
460 len = strlenW( str );
465 if(pf_check_auto_grow(out, len) == -1) return -1;
466 space = out->len - out->used;
467 p = out->buf.W + out->used;
470 if (out->buf.W) memcpy( p, str, len*sizeof(WCHAR) );
474 if( space > 0 && out->buf.W )
475 memcpy( p, str, space*sizeof(WCHAR) );
480 int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
483 if(pf_check_auto_grow(out, n) == -1) return -1;
484 space = out->len - out->used;
485 p = out->buf.A + out->used;
488 if (out->buf.A) WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
492 if( space > 0 && out->buf.A )
493 WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
499 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
509 if (pf_check_auto_grow(out, len) == -1) return -1;
510 p = out->buf.A + out->used;
511 space = out->len - out->used;
514 if (out->buf.A) memcpy( p, str, len );
518 if( space > 0 && out->buf.A )
519 memcpy( p, str, space );
524 int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
527 if (pf_check_auto_grow(out, n) == -1) return -1;
528 p = out->buf.W + out->used;
529 space = out->len - out->used;
532 if (out->buf.W) MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
536 if( space > 0 && out->buf.W )
537 MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
543 /* pf_fill: takes care of signs, alignment, zero and field padding */
544 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
548 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
551 if( left && flags->Sign )
553 flags->FieldLength--;
555 r = pf_output_stringA( out, &flags->Sign, 1 );
558 if( ( !left && flags->LeftAlign ) ||
559 ( left && !flags->LeftAlign ))
561 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
563 if( left && flags->PadZero )
564 r = pf_output_stringA( out, "0", 1 );
566 r = pf_output_stringA( out, " ", 1 );
570 if( left && flags->Sign && !flags->PadZero )
571 r = pf_output_stringA( out, &flags->Sign, 1 );
576 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
577 int len, pf_flags *flags )
582 len = strlenW( str );
584 if (flags->Precision >= 0 && flags->Precision < len)
585 len = flags->Precision;
587 r = pf_fill( out, len, flags, 1 );
590 r = pf_output_stringW( out, str, len );
593 r = pf_fill( out, len, flags, 0 );
598 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
599 int len, pf_flags *flags )
606 if (flags->Precision >= 0 && flags->Precision < len)
607 len = flags->Precision;
609 r = pf_fill( out, len, flags, 1 );
612 r = pf_output_stringA( out, str, len );
615 r = pf_fill( out, len, flags, 0 );
620 static int pf_handle_string_format( pf_output *out, const void* str, int len,
621 pf_flags *flags, BOOL capital_letter)
623 if(str == NULL) /* catch NULL pointer */
624 return pf_output_format_A( out, "(null)", -1, flags);
626 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
627 if(flags->WideString || flags->IntegerLength == 'l')
628 return pf_output_format_W( out, str, len, flags);
629 if(flags->IntegerLength == 'h')
630 return pf_output_format_A( out, str, len, flags);
632 /* %s,%c -> chars in ansi functions & wchars in unicode
633 * %S,%C -> wchars in ansi functions & chars in unicode */
634 if( capital_letter == out->unicode) /* either both TRUE or both FALSE */
635 return pf_output_format_A( out, str, len, flags);
637 return pf_output_format_W( out, str, len, flags);
640 static inline BOOL pf_is_integer_format( char fmt )
642 static const char float_fmts[] = "diouxX";
645 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
648 static inline BOOL pf_is_double_format( char fmt )
650 static const char float_fmts[] = "aeEfgG";
653 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
656 static inline BOOL pf_is_valid_format( char fmt )
658 static const char float_fmts[] = "acCdeEfgGinouxX";
661 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
664 static void pf_rebuild_format_string( char *p, pf_flags *flags )
669 if( flags->LeftAlign )
670 *p++ = flags->LeftAlign;
671 if( flags->Alternate )
672 *p++ = flags->Alternate;
674 *p++ = flags->PadZero;
675 if( flags->FieldLength )
677 sprintf(p, "%d", flags->FieldLength);
680 if( flags->Precision >= 0 )
682 sprintf(p, ".%d", flags->Precision);
685 *p++ = flags->Format;
689 /* pf_integer_conv: prints x to buf, including alternate formats and
690 additional precision digits, but not field characters or the sign */
691 static void pf_integer_conv( char *buf, int buf_len, pf_flags *flags,
698 char number[40], *tmp = number;
700 if( buf_len > sizeof number )
701 tmp = HeapAlloc( GetProcessHeap(), 0, buf_len );
704 if( flags->Format == 'o' )
706 else if( flags->Format == 'x' || flags->Format == 'X' )
709 if( flags->Format == 'X' )
710 digits = "0123456789ABCDEFX";
712 digits = "0123456789abcdefx";
714 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
720 /* Do conversion (backwards) */
722 if( x == 0 && flags->Precision )
727 j = (ULONGLONG) x % base;
728 x = (ULONGLONG) x / base;
729 tmp[i++] = digits[j];
731 k = flags->Precision - i;
734 if( flags->Alternate )
738 tmp[i++] = digits[16];
741 else if( base == 8 && tmp[i-1] != '0' )
745 /* Reverse for buf */
751 /* Adjust precision so pf_fill won't truncate the number later */
752 flags->Precision = strlen( buf );
755 HeapFree( GetProcessHeap(), 0, tmp );
760 /* pf_fixup_exponent: convert a string containing a 2 digit exponent
761 to 3 digits, accounting for padding, in place. Needed to match
762 the native printf's which always use 3 digits. */
763 static void pf_fixup_exponent( char *buf )
767 while (tmp[0] && toupper(tmp[0]) != 'E')
770 if (tmp[0] && (tmp[1] == '+' || tmp[1] == '-') &&
771 isdigit(tmp[2]) && isdigit(tmp[3]))
776 return; /* Exponent already 3 digits */
778 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
786 /* We didn't expand into trailing space, so this string isn't left
787 * justified. Terminate the string and strip a ' ' at the start of
788 * the string if there is one (as there may be if the string is
793 memmove(buf, buf + 1, (tmp - buf) + 3);
795 /* Otherwise, we expanded into trailing space -> nothing to do */
799 /*********************************************************************
800 * pf_vsnprintf (INTERNAL)
802 * implements both A and W vsnprintf functions
804 int pf_vsnprintf( pf_output *out, const WCHAR *format,
805 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
808 LPCWSTR q, p = format;
812 locale = get_locale();
814 TRACE("format is %s\n",debugstr_w(format));
817 q = strchrW( p, '%' );
819 /* there's no % characters left, output the rest of the string */
822 r = pf_output_stringW(out, p, -1);
829 /* there's characters before the %, output them */
832 r = pf_output_stringW(out, p, q - p);
838 /* we must be at a % now, skip over it */
842 /* output a single % character */
845 r = pf_output_stringW(out, p++, 1);
851 /* parse the flags */
852 memset( &flags, 0, sizeof flags );
855 if( *p == '+' || *p == ' ' )
857 if ( flags.Sign != '+' )
861 flags.LeftAlign = *p;
865 flags.Alternate = *p;
871 /* deal with the field width specifier */
872 flags.FieldLength = 0;
875 flags.FieldLength = va_arg( valist, int );
876 if (flags.FieldLength < 0)
878 flags.LeftAlign = '-';
879 flags.FieldLength = -flags.FieldLength;
883 else while( isdigit(*p) )
885 flags.FieldLength *= 10;
886 flags.FieldLength += *p++ - '0';
889 /* deal with precision */
890 flags.Precision = -1;
897 flags.Precision = va_arg( valist, int );
900 else while( isdigit(*p) )
902 flags.Precision *= 10;
903 flags.Precision += *p++ - '0';
907 /* deal with integer width modifier */
910 if( *p == 'l' && *(p+1) == 'l' )
912 flags.IntegerDouble++;
915 else if( *p == 'h' || *p == 'l' || *p == 'L' )
917 flags.IntegerLength = *p;
922 if( *(p+1) == '6' && *(p+2) == '4' )
924 flags.IntegerDouble++;
927 else if( *(p+1) == '3' && *(p+2) == '2' )
929 else if( isdigit(*(p+1)) || *(p+1) == 0 )
935 flags.WideString = *p++;
945 if (flags.Format == '$')
947 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
950 /* output a string */
951 if( flags.Format == 's' || flags.Format == 'S' )
952 r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
953 &flags, (flags.Format == 'S') );
955 /* output a single character */
956 else if( flags.Format == 'c' || flags.Format == 'C' )
958 INT ch = va_arg( valist, int );
960 r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
963 /* output a pointer */
964 else if( flags.Format == 'p' )
967 void *ptr = va_arg( valist, void * );
970 if( flags.Alternate )
971 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
973 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
974 r = pf_output_format_A( out, pointer, -1, &flags );
978 else if( flags.Format == 'n' )
980 int *x = va_arg(valist, int *);
984 /* deal with 64-bit integers */
985 else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
987 char number[40], *x = number;
989 /* Estimate largest possible required buffer size:
990 * Chooses the larger of the field or precision
991 * Includes extra bytes: 1 byte for null, 1 byte for sign,
992 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
993 for a decimal, and 1 byte for an additional float digit. */
994 int x_len = ((flags.FieldLength > flags.Precision) ?
995 flags.FieldLength : flags.Precision) + 10;
997 if( x_len >= sizeof number)
998 x = HeapAlloc( GetProcessHeap(), 0, x_len );
1000 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
1002 r = pf_output_format_A( out, x, -1, &flags );
1004 HeapFree( GetProcessHeap(), 0, x );
1007 /* deal with integers and floats using libc's printf */
1008 else if( pf_is_valid_format( flags.Format ) )
1010 char fmt[20], number[40], *x = number, *decimal_point;
1012 /* Estimate largest possible required buffer size:
1013 * Chooses the larger of the field or precision
1014 * Includes extra bytes: 1 byte for null, 1 byte for sign,
1015 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
1016 for a decimal, and 1 byte for an additional float digit. */
1017 int x_len = ((flags.FieldLength > flags.Precision) ?
1018 flags.FieldLength : flags.Precision) + 10;
1020 if( x_len >= sizeof number)
1021 x = HeapAlloc( GetProcessHeap(), 0, x_len );
1023 pf_rebuild_format_string( fmt, &flags );
1025 if( pf_is_double_format( flags.Format ) )
1027 sprintf( x, fmt, va_arg(valist, double) );
1028 if (toupper(flags.Format) == 'E' || toupper(flags.Format) == 'G')
1029 pf_fixup_exponent( x );
1032 sprintf( x, fmt, va_arg(valist, int) );
1034 decimal_point = strchr(x, '.');
1036 *decimal_point = *locale->locinfo->lconv->decimal_point;
1038 r = pf_output_stringA( out, x, -1 );
1040 HeapFree( GetProcessHeap(), 0, x );
1048 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
1049 *MSVCRT__errno() = MSVCRT_EINVAL;
1061 /* check we reached the end, and null terminate the string */
1063 pf_output_stringW( out, p, 1 );
1065 return out->used - 1;
1068 /*********************************************************************
1069 * vsnprintf_internal (INTERNAL)
1071 static inline int vsnprintf_internal( char *str, MSVCRT_size_t len, const char *format,
1072 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
1075 LPWSTR formatW = NULL;
1079 out.unicode = FALSE;
1085 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
1086 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
1087 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
1089 r = pf_vsnprintf( &out, formatW, locale, valid, valist );
1091 HeapFree( GetProcessHeap(), 0, formatW );
1096 /*********************************************************************
1097 * _vsnprintf (MSVCRT.@)
1099 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
1100 const char *format, __ms_va_list valist )
1102 return vsnprintf_internal(str, len, format, NULL, FALSE, valist);
1105 /*********************************************************************
1106 * _vsnprintf_l (MSVCRT.@)
1108 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
1109 MSVCRT__locale_t locale, __ms_va_list valist )
1111 return vsnprintf_internal(str, len, format, locale, FALSE, valist);
1114 /*********************************************************************
1115 * _vsnprintf_s_l (MSVCRT.@)
1117 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
1118 MSVCRT_size_t count, const char *format,
1119 MSVCRT__locale_t locale, __ms_va_list valist )
1123 if(sizeOfBuffer<count+1 || count==-1)
1128 ret = vsnprintf_internal(str, len, format, locale, TRUE, valist);
1130 if(ret<0 || ret==len) {
1131 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
1132 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small");
1133 *MSVCRT__errno() = MSVCRT_ERANGE;
1134 memset(str, 0, sizeOfBuffer);
1144 /*********************************************************************
1145 * _vsnprintf_s (MSVCRT.@)
1147 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
1148 MSVCRT_size_t count, const char *format, __ms_va_list valist )
1150 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
1153 /*********************************************************************
1154 * vsprintf (MSVCRT.@)
1156 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
1158 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
1161 /*********************************************************************
1162 * vsprintf_s (MSVCRT.@)
1164 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
1166 return MSVCRT_vsnprintf(str, num, format, valist);
1169 /*********************************************************************
1170 * _vscprintf (MSVCRT.@)
1172 int CDECL _vscprintf( const char *format, __ms_va_list valist )
1174 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
1177 /*********************************************************************
1178 * _snprintf (MSVCRT.@)
1180 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
1183 __ms_va_list valist;
1184 __ms_va_start(valist, format);
1185 retval = MSVCRT_vsnprintf(str, len, format, valist);
1186 __ms_va_end(valist);
1190 /*********************************************************************
1191 * _snprintf_s (MSVCRT.@)
1193 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
1194 const char *format, ...)
1197 __ms_va_list valist;
1198 __ms_va_start(valist, format);
1199 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
1200 __ms_va_end(valist);
1204 /*********************************************************************
1205 * _scprintf (MSVCRT.@)
1207 int CDECL MSVCRT__scprintf(const char *format, ...)
1210 __ms_va_list valist;
1211 __ms_va_start(valist, format);
1212 retval = _vscprintf(format, valist);
1213 __ms_va_end(valist);
1217 /*********************************************************************
1218 * vsnwprintf_internal (INTERNAL)
1220 static inline int vsnwprintf_internal(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1221 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, BOOL valid,
1222 __ms_va_list valist)
1232 return pf_vsnprintf( &out, format, locale, valid, valist );
1235 /*********************************************************************
1236 * _vsnwprintf (MSVCRT.@)
1238 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1239 const MSVCRT_wchar_t *format, __ms_va_list valist)
1241 return vsnwprintf_internal(str, len, format, NULL, FALSE, valist);
1244 /*********************************************************************
1245 * _vsnwprintf_l (MSVCRT.@)
1247 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1248 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
1249 __ms_va_list valist)
1251 return vsnwprintf_internal(str, len, format, locale, FALSE, valist);
1254 /*********************************************************************
1255 * _vsnwprintf_s_l (MSVCRT.@)
1257 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1258 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1259 MSVCRT__locale_t locale, __ms_va_list valist)
1264 if(count!=-1 && len>count+1)
1267 ret = vsnwprintf_internal(str, len, format, locale, TRUE, valist);
1269 if(ret<0 || ret==len) {
1270 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
1271 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small");
1272 *MSVCRT__errno() = MSVCRT_ERANGE;
1273 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
1283 /*********************************************************************
1284 * _vsnwprintf_s (MSVCRT.@)
1286 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1287 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
1289 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
1290 format, NULL, valist);
1293 /*********************************************************************
1294 * _snwprintf (MSVCRT.@)
1296 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
1299 __ms_va_list valist;
1300 __ms_va_start(valist, format);
1301 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1302 __ms_va_end(valist);
1306 /*********************************************************************
1307 * _snwprintf_s (MSVCRT.@)
1309 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1310 const MSVCRT_wchar_t *format, ...)
1313 __ms_va_list valist;
1314 __ms_va_start(valist, format);
1315 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
1316 __ms_va_end(valist);
1320 /*********************************************************************
1321 * sprintf (MSVCRT.@)
1323 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
1328 __ms_va_start( ap, format );
1329 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1334 /*********************************************************************
1335 * sprintf_s (MSVCRT.@)
1337 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1342 __ms_va_start( ap, format );
1343 r = MSVCRT_vsnprintf( str, num, format, ap );
1348 /*********************************************************************
1349 * _scwprintf (MSVCRT.@)
1351 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
1356 __ms_va_start( ap, format );
1357 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
1362 /*********************************************************************
1363 * swprintf (MSVCRT.@)
1365 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1370 __ms_va_start( ap, format );
1371 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1376 /*********************************************************************
1377 * swprintf_s (MSVCRT.@)
1379 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1380 const MSVCRT_wchar_t *format, ... )
1385 __ms_va_start(ap, format);
1386 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
1392 /*********************************************************************
1393 * vswprintf (MSVCRT.@)
1395 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1397 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1400 /*********************************************************************
1401 * _vscwprintf (MSVCRT.@)
1403 int CDECL _vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1405 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1408 /*********************************************************************
1409 * vswprintf_s (MSVCRT.@)
1411 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1412 const MSVCRT_wchar_t* format, __ms_va_list args)
1414 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1417 /*********************************************************************
1418 * _vswprintf_s_l (MSVCRT.@)
1420 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1421 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1423 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1424 format, locale, args );
1427 /*********************************************************************
1428 * wcscoll (MSVCRT.@)
1430 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1432 /* FIXME: handle collates */
1433 return strcmpW( str1, str2 );
1436 /*********************************************************************
1437 * wcspbrk (MSVCRT.@)
1439 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1441 const MSVCRT_wchar_t* p;
1444 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1450 /*********************************************************************
1451 * wcstok_s (MSVCRT.@)
1453 MSVCRT_wchar_t * CDECL wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
1454 MSVCRT_wchar_t **next_token )
1456 MSVCRT_wchar_t *ret;
1458 if (!MSVCRT_CHECK_PMT(delim != NULL) || !MSVCRT_CHECK_PMT(next_token != NULL) ||
1459 !MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL))
1461 *MSVCRT__errno() = MSVCRT_EINVAL;
1464 if (!str) str = *next_token;
1466 while (*str && strchrW( delim, *str )) str++;
1467 if (!*str) return NULL;
1469 while (*str && !strchrW( delim, *str )) str++;
1470 if (*str) *str++ = 0;
1475 /*********************************************************************
1478 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1480 return wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1483 /*********************************************************************
1486 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1488 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
1491 /*********************************************************************
1492 * iswalnum (MSVCRT.@)
1494 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1496 return isalnumW( wc );
1499 /*********************************************************************
1500 * iswalpha (MSVCRT.@)
1502 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1504 return isalphaW( wc );
1507 /*********************************************************************
1508 * iswalpha_l (MSVCRT.@)
1510 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1512 return isalphaW( wc );
1515 /*********************************************************************
1516 * iswcntrl (MSVCRT.@)
1518 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1520 return iscntrlW( wc );
1523 /*********************************************************************
1524 * iswdigit (MSVCRT.@)
1526 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1528 return isdigitW( wc );
1531 /*********************************************************************
1532 * iswgraph (MSVCRT.@)
1534 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1536 return isgraphW( wc );
1539 /*********************************************************************
1540 * iswlower (MSVCRT.@)
1542 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1544 return islowerW( wc );
1547 /*********************************************************************
1548 * iswprint (MSVCRT.@)
1550 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1552 return isprintW( wc );
1555 /*********************************************************************
1556 * iswpunct (MSVCRT.@)
1558 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1560 return ispunctW( wc );
1563 /*********************************************************************
1564 * iswspace (MSVCRT.@)
1566 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1568 return isspaceW( wc );
1571 /*********************************************************************
1572 * iswupper (MSVCRT.@)
1574 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1576 return isupperW( wc );
1579 /*********************************************************************
1580 * iswxdigit (MSVCRT.@)
1582 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1584 return isxdigitW( wc );
1587 /*********************************************************************
1588 * wcscpy_s (MSVCRT.@)
1590 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1592 MSVCRT_size_t size = 0;
1594 if(!wcDest || !numElement)
1595 return MSVCRT_EINVAL;
1601 return MSVCRT_EINVAL;
1604 size = strlenW(wcSrc) + 1;
1606 if(size > numElement)
1608 return MSVCRT_ERANGE;
1611 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1616 /******************************************************************
1617 * wcsncpy_s (MSVCRT.@)
1619 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1620 MSVCRT_size_t count )
1622 MSVCRT_size_t size = 0;
1624 if (!wcDest || !numElement)
1625 return MSVCRT_EINVAL;
1631 return MSVCRT_EINVAL;
1634 size = min(strlenW(wcSrc), count);
1636 if (size >= numElement)
1638 return MSVCRT_ERANGE;
1641 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1642 wcDest[size] = '\0';
1647 /******************************************************************
1648 * wcscat_s (MSVCRT.@)
1651 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1653 MSVCRT_wchar_t* ptr = dst;
1655 if (!dst || elem == 0) return MSVCRT_EINVAL;
1659 return MSVCRT_EINVAL;
1662 /* seek to end of dst string (or elem if no end of string is found */
1663 while (ptr < dst + elem && *ptr != '\0') ptr++;
1664 while (ptr < dst + elem)
1666 if ((*ptr++ = *src++) == '\0') return 0;
1668 /* not enough space */
1670 return MSVCRT_ERANGE;
1673 /*********************************************************************
1674 * wcsncat_s (MSVCRT.@)
1677 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
1678 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
1680 MSVCRT_size_t srclen;
1681 MSVCRT_wchar_t dststart;
1684 if (!MSVCRT_CHECK_PMT(dst != NULL) || !MSVCRT_CHECK_PMT(elem > 0))
1686 *MSVCRT__errno() = MSVCRT_EINVAL;
1687 return MSVCRT_EINVAL;
1689 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0))
1690 return MSVCRT_EINVAL;
1694 for (dststart = 0; dststart < elem; dststart++)
1696 if (dst[dststart] == '\0')
1699 if (dststart == elem)
1701 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n");
1702 return MSVCRT_EINVAL;
1705 if (count == MSVCRT__TRUNCATE)
1707 srclen = strlenW(src);
1708 if (srclen >= (elem - dststart))
1710 srclen = elem - dststart - 1;
1711 ret = MSVCRT_STRUNCATE;
1715 srclen = min(strlenW(src), count);
1716 if (srclen < (elem - dststart))
1718 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
1722 MSVCRT_INVALID_PMT("dst[elem] is too small");
1724 return MSVCRT_ERANGE;
1727 /*********************************************************************
1728 * _wcstoi64_l (MSVCRT.@)
1730 * FIXME: locale parameter is ignored
1732 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1733 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1735 BOOL negative = FALSE;
1738 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1740 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
1741 !MSVCRT_CHECK_PMT(base <= 36)) {
1742 *MSVCRT__errno() = MSVCRT_EINVAL;
1746 while(isspaceW(*nptr)) nptr++;
1751 } else if(*nptr == '+')
1754 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1767 char cur = tolowerW(*nptr);
1775 if(cur<'a' || cur>='a'+base-10)
1785 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
1786 ret = MSVCRT_I64_MAX;
1787 *MSVCRT__errno() = MSVCRT_ERANGE;
1788 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
1789 ret = MSVCRT_I64_MIN;
1790 *MSVCRT__errno() = MSVCRT_ERANGE;
1796 *endptr = (MSVCRT_wchar_t*)nptr;
1801 /*********************************************************************
1802 * _wcstoi64 (MSVCRT.@)
1804 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
1805 MSVCRT_wchar_t **endptr, int base)
1807 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
1810 /*********************************************************************
1811 * _wcstoui64_l (MSVCRT.@)
1813 * FIXME: locale parameter is ignored
1815 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
1816 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1818 BOOL negative = FALSE;
1819 unsigned __int64 ret = 0;
1821 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1823 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
1824 !MSVCRT_CHECK_PMT(base <= 36)) {
1825 *MSVCRT__errno() = MSVCRT_EINVAL;
1829 while(isspaceW(*nptr)) nptr++;
1834 } else if(*nptr == '+')
1837 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1850 char cur = tolowerW(*nptr);
1858 if(cur<'a' || cur>='a'+base-10)
1865 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
1866 ret = MSVCRT_UI64_MAX;
1867 *MSVCRT__errno() = MSVCRT_ERANGE;
1873 *endptr = (MSVCRT_wchar_t*)nptr;
1875 return negative ? -ret : ret;
1878 /*********************************************************************
1879 * _wcstoui64 (MSVCRT.@)
1881 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
1882 MSVCRT_wchar_t **endptr, int base)
1884 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
1887 /******************************************************************
1888 * wcsnlen (MSVCRT.@)
1890 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
1894 for (i = 0; i < maxlen; i++)