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 (MSVCRT.@)
109 INT CDECL MSVCRT__wcsupr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
111 MSVCRT_wchar_t* ptr = str;
115 if (str) *str = '\0';
116 *MSVCRT__errno() = MSVCRT_EINVAL;
117 return MSVCRT_EINVAL;
123 *ptr = toupperW(*ptr);
127 /* MSDN claims that the function should return and set errno to
128 * ERANGE, which doesn't seem to be true based on the tests. */
130 *MSVCRT__errno() = MSVCRT_EINVAL;
131 return MSVCRT_EINVAL;
134 /*********************************************************************
135 * _wcstod_l - not exported in native msvcrt
137 double CDECL MSVCRT__wcstod_l(const MSVCRT_wchar_t* str, MSVCRT_wchar_t** end,
138 MSVCRT__locale_t locale)
140 unsigned __int64 d=0, hlp;
143 const MSVCRT_wchar_t *p;
145 BOOL found_digit = FALSE;
147 if (!MSVCRT_CHECK_PMT(str != NULL)) {
148 *MSVCRT__errno() = MSVCRT_EINVAL;
153 locale = get_locale();
165 while(isdigitW(*p)) {
167 hlp = d*10+*(p++)-'0';
168 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
174 while(isdigitW(*p)) {
178 if(*p == *locale->locinfo->lconv->decimal_point)
181 while(isdigitW(*p)) {
183 hlp = d*10+*(p++)-'0';
184 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
195 *end = (MSVCRT_wchar_t*)str;
199 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
210 while(isdigitW(*p)) {
211 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
217 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
218 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
221 if(*p=='-' || *p=='+')
227 fpcontrol = _control87(0, 0);
228 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
229 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
232 ret = (double)sign*d*pow(10, exp);
234 ret = (double)sign*d/pow(10, -exp);
236 _control87(fpcontrol, 0xffffffff);
238 if((d && ret==0.0) || isinf(ret))
239 *MSVCRT__errno() = MSVCRT_ERANGE;
242 *end = (MSVCRT_wchar_t*)p;
247 /*********************************************************************
248 * _wcstombs_l (MSVCRT.@)
250 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
251 MSVCRT_size_t count, MSVCRT__locale_t locale)
253 char default_char = '\0';
254 MSVCRT_size_t tmp = 0;
258 locale = get_locale();
261 return WideCharToMultiByte(locale->locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
262 wcstr, -1, NULL, 0, &default_char, &used_default)-1;
266 MSVCRT_size_t i, size;
268 size = WideCharToMultiByte(locale->locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
269 wcstr, 1, buf, 3, &default_char, &used_default);
275 for(i=0; i<size; i++)
276 mbstr[tmp++] = buf[i];
285 /*********************************************************************
286 * wcstombs (MSVCRT.@)
288 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
291 return MSVCRT__wcstombs_l(mbstr, wcstr, count, NULL);
294 /*********************************************************************
295 * _wcstombs_s_l (MSVCRT.@)
297 MSVCRT_size_t CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
298 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
299 MSVCRT_size_t count, MSVCRT__locale_t locale)
303 if(!mbstr && !size) {
304 conv = MSVCRT__wcstombs_l(NULL, wcstr, 0, locale);
310 if (!MSVCRT_CHECK_PMT(wcstr != NULL) || !MSVCRT_CHECK_PMT(mbstr != NULL)) {
313 *MSVCRT__errno() = MSVCRT_EINVAL;
314 return MSVCRT_EINVAL;
317 if(count==MSVCRT__TRUNCATE || size<count)
322 conv = MSVCRT__wcstombs_l(mbstr, wcstr, conv, locale);
324 mbstr[conv++] = '\0';
325 else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
326 mbstr[conv-1] = '\0';
328 MSVCRT_INVALID_PMT("mbstr[size] is too small");
331 *MSVCRT__errno() = MSVCRT_ERANGE;
332 return MSVCRT_ERANGE;
340 /*********************************************************************
341 * wcstombs_s (MSVCRT.@)
343 MSVCRT_size_t CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
344 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
346 return MSVCRT__wcstombs_s_l(ret, mbstr, size, wcstr, count, NULL);
349 /*********************************************************************
352 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
354 return MSVCRT__wcstod_l(lpszStr, end, NULL);
357 /*********************************************************************
360 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
362 return MSVCRT__wcstod_l(str, NULL, NULL);
365 /*********************************************************************
368 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
370 return MSVCRT__wcstod_l(str, NULL, locale);
373 typedef struct pf_flags_t
375 char Sign, LeftAlign, Alternate, PadZero;
376 int FieldLength, Precision;
377 char IntegerLength, IntegerDouble;
382 static inline BOOL pf_is_auto_grow(pf_output *out)
384 return (out->unicode) ? !!out->grow.W : !!out->grow.A;
387 static inline int pf_check_auto_grow(pf_output *out, unsigned delta)
389 if (pf_is_auto_grow(out) && out->used + delta > out->len)
391 out->len = max(out->len * 2, out->used + delta);
394 if (out->buf.W != out->grow.W)
395 out->buf.W = MSVCRT_realloc(out->buf.W, out->len * sizeof(WCHAR));
397 out->buf.W = MSVCRT_malloc(out->len * sizeof(WCHAR));
398 if (!out->buf.W) return -1;
402 if (out->buf.A != out->grow.A)
403 out->buf.A = MSVCRT_realloc(out->buf.A, out->len * sizeof(char));
405 out->buf.A = MSVCRT_malloc(out->len * sizeof(char));
406 if (!out->buf.A) return -1;
413 * writes a string of characters to the output
414 * returns -1 if the string doesn't fit in the output buffer
415 * return the length of the string if all characters were written
417 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
422 len = strlenW( str );
427 if(pf_check_auto_grow(out, len) == -1) return -1;
428 space = out->len - out->used;
429 p = out->buf.W + out->used;
432 if (out->buf.W) memcpy( p, str, len*sizeof(WCHAR) );
436 if( space > 0 && out->buf.W )
437 memcpy( p, str, space*sizeof(WCHAR) );
442 int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
445 if(pf_check_auto_grow(out, n) == -1) return -1;
446 space = out->len - out->used;
447 p = out->buf.A + out->used;
450 if (out->buf.A) WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
454 if( space > 0 && out->buf.A )
455 WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
461 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
471 if (pf_check_auto_grow(out, len) == -1) return -1;
472 p = out->buf.A + out->used;
473 space = out->len - out->used;
476 if (out->buf.A) memcpy( p, str, len );
480 if( space > 0 && out->buf.A )
481 memcpy( p, str, space );
486 int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
489 if (pf_check_auto_grow(out, n) == -1) return -1;
490 p = out->buf.W + out->used;
491 space = out->len - out->used;
494 if (out->buf.W) MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
498 if( space > 0 && out->buf.W )
499 MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
505 /* pf_fill: takes care of signs, alignment, zero and field padding */
506 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
510 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
513 if( left && flags->Sign )
515 flags->FieldLength--;
517 r = pf_output_stringA( out, &flags->Sign, 1 );
520 if( ( !left && flags->LeftAlign ) ||
521 ( left && !flags->LeftAlign ))
523 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
525 if( left && flags->PadZero )
526 r = pf_output_stringA( out, "0", 1 );
528 r = pf_output_stringA( out, " ", 1 );
532 if( left && flags->Sign && !flags->PadZero )
533 r = pf_output_stringA( out, &flags->Sign, 1 );
538 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
539 int len, pf_flags *flags )
544 len = strlenW( str );
546 if (flags->Precision >= 0 && flags->Precision < len)
547 len = flags->Precision;
549 r = pf_fill( out, len, flags, 1 );
552 r = pf_output_stringW( out, str, len );
555 r = pf_fill( out, len, flags, 0 );
560 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
561 int len, pf_flags *flags )
568 if (flags->Precision >= 0 && flags->Precision < len)
569 len = flags->Precision;
571 r = pf_fill( out, len, flags, 1 );
574 r = pf_output_stringA( out, str, len );
577 r = pf_fill( out, len, flags, 0 );
582 static int pf_handle_string_format( pf_output *out, const void* str, int len,
583 pf_flags *flags, BOOL capital_letter)
585 if(str == NULL) /* catch NULL pointer */
586 return pf_output_format_A( out, "(null)", -1, flags);
588 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
589 if(flags->WideString || flags->IntegerLength == 'l')
590 return pf_output_format_W( out, str, len, flags);
591 if(flags->IntegerLength == 'h')
592 return pf_output_format_A( out, str, len, flags);
594 /* %s,%c -> chars in ansi functions & wchars in unicode
595 * %S,%C -> wchars in ansi functions & chars in unicode */
596 if( capital_letter == out->unicode) /* either both TRUE or both FALSE */
597 return pf_output_format_A( out, str, len, flags);
599 return pf_output_format_W( out, str, len, flags);
602 static inline BOOL pf_is_integer_format( char fmt )
604 static const char float_fmts[] = "diouxX";
607 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
610 static inline BOOL pf_is_double_format( char fmt )
612 static const char float_fmts[] = "aeEfgG";
615 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
618 static inline BOOL pf_is_valid_format( char fmt )
620 static const char float_fmts[] = "acCdeEfgGinouxX";
623 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
626 static void pf_rebuild_format_string( char *p, pf_flags *flags )
631 if( flags->LeftAlign )
632 *p++ = flags->LeftAlign;
633 if( flags->Alternate )
634 *p++ = flags->Alternate;
636 *p++ = flags->PadZero;
637 if( flags->FieldLength )
639 sprintf(p, "%d", flags->FieldLength);
642 if( flags->Precision >= 0 )
644 sprintf(p, ".%d", flags->Precision);
647 *p++ = flags->Format;
651 /* pf_integer_conv: prints x to buf, including alternate formats and
652 additional precision digits, but not field characters or the sign */
653 static void pf_integer_conv( char *buf, int buf_len, pf_flags *flags,
660 char number[40], *tmp = number;
662 if( buf_len > sizeof number )
663 tmp = HeapAlloc( GetProcessHeap(), 0, buf_len );
666 if( flags->Format == 'o' )
668 else if( flags->Format == 'x' || flags->Format == 'X' )
671 if( flags->Format == 'X' )
672 digits = "0123456789ABCDEFX";
674 digits = "0123456789abcdefx";
676 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
682 /* Do conversion (backwards) */
684 if( x == 0 && flags->Precision )
689 j = (ULONGLONG) x % base;
690 x = (ULONGLONG) x / base;
691 tmp[i++] = digits[j];
693 k = flags->Precision - i;
696 if( flags->Alternate )
700 tmp[i++] = digits[16];
703 else if( base == 8 && tmp[i-1] != '0' )
707 /* Reverse for buf */
713 /* Adjust precision so pf_fill won't truncate the number later */
714 flags->Precision = strlen( buf );
717 HeapFree( GetProcessHeap(), 0, tmp );
722 /* pf_fixup_exponent: convert a string containing a 2 digit exponent
723 to 3 digits, accounting for padding, in place. Needed to match
724 the native printf's which always use 3 digits. */
725 static void pf_fixup_exponent( char *buf )
729 while (tmp[0] && toupper(tmp[0]) != 'E')
732 if (tmp[0] && (tmp[1] == '+' || tmp[1] == '-') &&
733 isdigit(tmp[2]) && isdigit(tmp[3]))
738 return; /* Exponent already 3 digits */
740 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
748 /* We didn't expand into trailing space, so this string isn't left
749 * justified. Terminate the string and strip a ' ' at the start of
750 * the string if there is one (as there may be if the string is
755 memmove(buf, buf + 1, (tmp - buf) + 3);
757 /* Otherwise, we expanded into trailing space -> nothing to do */
761 /*********************************************************************
762 * pf_vsnprintf (INTERNAL)
764 * implements both A and W vsnprintf functions
766 int pf_vsnprintf( pf_output *out, const WCHAR *format,
767 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
770 LPCWSTR q, p = format;
774 locale = get_locale();
776 TRACE("format is %s\n",debugstr_w(format));
779 q = strchrW( p, '%' );
781 /* there's no % characters left, output the rest of the string */
784 r = pf_output_stringW(out, p, -1);
791 /* there's characters before the %, output them */
794 r = pf_output_stringW(out, p, q - p);
800 /* we must be at a % now, skip over it */
804 /* output a single % character */
807 r = pf_output_stringW(out, p++, 1);
813 /* parse the flags */
814 memset( &flags, 0, sizeof flags );
817 if( *p == '+' || *p == ' ' )
819 if ( flags.Sign != '+' )
823 flags.LeftAlign = *p;
827 flags.Alternate = *p;
833 /* deal with the field width specifier */
834 flags.FieldLength = 0;
837 flags.FieldLength = va_arg( valist, int );
838 if (flags.FieldLength < 0)
840 flags.LeftAlign = '-';
841 flags.FieldLength = -flags.FieldLength;
845 else while( isdigit(*p) )
847 flags.FieldLength *= 10;
848 flags.FieldLength += *p++ - '0';
851 /* deal with precision */
852 flags.Precision = -1;
859 flags.Precision = va_arg( valist, int );
862 else while( isdigit(*p) )
864 flags.Precision *= 10;
865 flags.Precision += *p++ - '0';
869 /* deal with integer width modifier */
872 if( *p == 'l' && *(p+1) == 'l' )
874 flags.IntegerDouble++;
877 else if( *p == 'h' || *p == 'l' || *p == 'L' )
879 flags.IntegerLength = *p;
884 if( *(p+1) == '6' && *(p+2) == '4' )
886 flags.IntegerDouble++;
889 else if( *(p+1) == '3' && *(p+2) == '2' )
891 else if( isdigit(*(p+1)) || *(p+1) == 0 )
897 flags.WideString = *p++;
907 if (flags.Format == '$')
909 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
912 /* output a string */
913 if( flags.Format == 's' || flags.Format == 'S' )
914 r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
915 &flags, (flags.Format == 'S') );
917 /* output a single character */
918 else if( flags.Format == 'c' || flags.Format == 'C' )
920 INT ch = va_arg( valist, int );
922 r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
925 /* output a pointer */
926 else if( flags.Format == 'p' )
929 void *ptr = va_arg( valist, void * );
932 if( flags.Alternate )
933 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
935 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
936 r = pf_output_format_A( out, pointer, -1, &flags );
940 else if( flags.Format == 'n' )
942 int *x = va_arg(valist, int *);
946 /* deal with 64-bit integers */
947 else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
949 char number[40], *x = number;
951 /* Estimate largest possible required buffer size:
952 * Chooses the larger of the field or precision
953 * Includes extra bytes: 1 byte for null, 1 byte for sign,
954 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
955 for a decimal, and 1 byte for an additional float digit. */
956 int x_len = ((flags.FieldLength > flags.Precision) ?
957 flags.FieldLength : flags.Precision) + 10;
959 if( x_len >= sizeof number)
960 x = HeapAlloc( GetProcessHeap(), 0, x_len );
962 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
964 r = pf_output_format_A( out, x, -1, &flags );
966 HeapFree( GetProcessHeap(), 0, x );
969 /* deal with integers and floats using libc's printf */
970 else if( pf_is_valid_format( flags.Format ) )
972 char fmt[20], number[40], *x = number, *decimal_point;
974 /* Estimate largest possible required buffer size:
975 * Chooses the larger of the field or precision
976 * Includes extra bytes: 1 byte for null, 1 byte for sign,
977 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
978 for a decimal, and 1 byte for an additional float digit. */
979 int x_len = ((flags.FieldLength > flags.Precision) ?
980 flags.FieldLength : flags.Precision) + 10;
982 if( x_len >= sizeof number)
983 x = HeapAlloc( GetProcessHeap(), 0, x_len );
985 pf_rebuild_format_string( fmt, &flags );
987 if( pf_is_double_format( flags.Format ) )
989 sprintf( x, fmt, va_arg(valist, double) );
990 if (toupper(flags.Format) == 'E' || toupper(flags.Format) == 'G')
991 pf_fixup_exponent( x );
994 sprintf( x, fmt, va_arg(valist, int) );
996 decimal_point = strchr(x, '.');
998 *decimal_point = *locale->locinfo->lconv->decimal_point;
1000 r = pf_output_stringA( out, x, -1 );
1002 HeapFree( GetProcessHeap(), 0, x );
1010 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
1011 *MSVCRT__errno() = MSVCRT_EINVAL;
1023 /* check we reached the end, and null terminate the string */
1025 pf_output_stringW( out, p, 1 );
1027 return out->used - 1;
1030 /*********************************************************************
1031 * vsnprintf_internal (INTERNAL)
1033 static inline int vsnprintf_internal( char *str, MSVCRT_size_t len, const char *format,
1034 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
1037 LPWSTR formatW = NULL;
1041 out.unicode = FALSE;
1047 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
1048 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
1049 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
1051 r = pf_vsnprintf( &out, formatW, locale, valid, valist );
1053 HeapFree( GetProcessHeap(), 0, formatW );
1058 /*********************************************************************
1059 * _vsnprintf (MSVCRT.@)
1061 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
1062 const char *format, __ms_va_list valist )
1064 return vsnprintf_internal(str, len, format, NULL, FALSE, valist);
1067 /*********************************************************************
1068 * _vsnprintf_l (MSVCRT.@)
1070 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
1071 MSVCRT__locale_t locale, __ms_va_list valist )
1073 return vsnprintf_internal(str, len, format, locale, FALSE, valist);
1076 /*********************************************************************
1077 * _vsnprintf_s_l (MSVCRT.@)
1079 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
1080 MSVCRT_size_t count, const char *format,
1081 MSVCRT__locale_t locale, __ms_va_list valist )
1085 if(sizeOfBuffer<count+1 || count==-1)
1090 ret = vsnprintf_internal(str, len, format, locale, TRUE, valist);
1092 if(ret<0 || ret==len) {
1093 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
1094 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small");
1095 *MSVCRT__errno() = MSVCRT_ERANGE;
1096 memset(str, 0, sizeOfBuffer);
1106 /*********************************************************************
1107 * _vsnprintf_s (MSVCRT.@)
1109 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
1110 MSVCRT_size_t count, const char *format, __ms_va_list valist )
1112 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
1115 /*********************************************************************
1116 * vsprintf (MSVCRT.@)
1118 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
1120 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
1123 /*********************************************************************
1124 * vsprintf_s (MSVCRT.@)
1126 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
1128 return MSVCRT_vsnprintf(str, num, format, valist);
1131 /*********************************************************************
1132 * _vscprintf (MSVCRT.@)
1134 int CDECL _vscprintf( const char *format, __ms_va_list valist )
1136 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
1139 /*********************************************************************
1140 * _snprintf (MSVCRT.@)
1142 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
1145 __ms_va_list valist;
1146 __ms_va_start(valist, format);
1147 retval = MSVCRT_vsnprintf(str, len, format, valist);
1148 __ms_va_end(valist);
1152 /*********************************************************************
1153 * _snprintf_s (MSVCRT.@)
1155 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
1156 const char *format, ...)
1159 __ms_va_list valist;
1160 __ms_va_start(valist, format);
1161 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
1162 __ms_va_end(valist);
1166 /*********************************************************************
1167 * _scprintf (MSVCRT.@)
1169 int CDECL MSVCRT__scprintf(const char *format, ...)
1172 __ms_va_list valist;
1173 __ms_va_start(valist, format);
1174 retval = _vscprintf(format, valist);
1175 __ms_va_end(valist);
1179 /*********************************************************************
1180 * vsnwprintf_internal (INTERNAL)
1182 static inline int vsnwprintf_internal(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1183 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, BOOL valid,
1184 __ms_va_list valist)
1194 return pf_vsnprintf( &out, format, locale, valid, valist );
1197 /*********************************************************************
1198 * _vsnwprintf (MSVCRT.@)
1200 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1201 const MSVCRT_wchar_t *format, __ms_va_list valist)
1203 return vsnwprintf_internal(str, len, format, NULL, FALSE, valist);
1206 /*********************************************************************
1207 * _vsnwprintf_l (MSVCRT.@)
1209 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1210 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
1211 __ms_va_list valist)
1213 return vsnwprintf_internal(str, len, format, locale, FALSE, valist);
1216 /*********************************************************************
1217 * _vsnwprintf_s_l (MSVCRT.@)
1219 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1220 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1221 MSVCRT__locale_t locale, __ms_va_list valist)
1226 if(count!=-1 && len>count+1)
1229 ret = vsnwprintf_internal(str, len, format, locale, TRUE, valist);
1231 if(ret<0 || ret==len) {
1232 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
1233 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small");
1234 *MSVCRT__errno() = MSVCRT_ERANGE;
1235 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
1245 /*********************************************************************
1246 * _vsnwprintf_s (MSVCRT.@)
1248 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1249 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
1251 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
1252 format, NULL, valist);
1255 /*********************************************************************
1256 * _snwprintf (MSVCRT.@)
1258 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
1261 __ms_va_list valist;
1262 __ms_va_start(valist, format);
1263 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1264 __ms_va_end(valist);
1268 /*********************************************************************
1269 * _snwprintf_s (MSVCRT.@)
1271 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1272 const MSVCRT_wchar_t *format, ...)
1275 __ms_va_list valist;
1276 __ms_va_start(valist, format);
1277 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
1278 __ms_va_end(valist);
1282 /*********************************************************************
1283 * sprintf (MSVCRT.@)
1285 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
1290 __ms_va_start( ap, format );
1291 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1296 /*********************************************************************
1297 * sprintf_s (MSVCRT.@)
1299 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1304 __ms_va_start( ap, format );
1305 r = MSVCRT_vsnprintf( str, num, format, ap );
1310 /*********************************************************************
1311 * _scwprintf (MSVCRT.@)
1313 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
1318 __ms_va_start( ap, format );
1319 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
1324 /*********************************************************************
1325 * swprintf (MSVCRT.@)
1327 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1332 __ms_va_start( ap, format );
1333 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1338 /*********************************************************************
1339 * swprintf_s (MSVCRT.@)
1341 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1342 const MSVCRT_wchar_t *format, ... )
1347 __ms_va_start(ap, format);
1348 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
1354 /*********************************************************************
1355 * vswprintf (MSVCRT.@)
1357 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1359 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1362 /*********************************************************************
1363 * _vscwprintf (MSVCRT.@)
1365 int CDECL _vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1367 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1370 /*********************************************************************
1371 * vswprintf_s (MSVCRT.@)
1373 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1374 const MSVCRT_wchar_t* format, __ms_va_list args)
1376 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1379 /*********************************************************************
1380 * _vswprintf_s_l (MSVCRT.@)
1382 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1383 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1385 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1386 format, locale, args );
1389 /*********************************************************************
1390 * wcscoll (MSVCRT.@)
1392 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1394 /* FIXME: handle collates */
1395 return strcmpW( str1, str2 );
1398 /*********************************************************************
1399 * wcspbrk (MSVCRT.@)
1401 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1403 const MSVCRT_wchar_t* p;
1406 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1412 /*********************************************************************
1413 * wcstok_s (MSVCRT.@)
1415 MSVCRT_wchar_t * CDECL wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
1416 MSVCRT_wchar_t **next_token )
1418 MSVCRT_wchar_t *ret;
1420 if (!MSVCRT_CHECK_PMT(delim != NULL) || !MSVCRT_CHECK_PMT(next_token != NULL) ||
1421 !MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL))
1423 *MSVCRT__errno() = MSVCRT_EINVAL;
1426 if (!str) str = *next_token;
1428 while (*str && strchrW( delim, *str )) str++;
1429 if (!*str) return NULL;
1431 while (*str && !strchrW( delim, *str )) str++;
1432 if (*str) *str++ = 0;
1437 /*********************************************************************
1440 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1442 return wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1445 /*********************************************************************
1448 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1450 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
1453 /*********************************************************************
1454 * iswalnum (MSVCRT.@)
1456 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1458 return isalnumW( wc );
1461 /*********************************************************************
1462 * iswalpha (MSVCRT.@)
1464 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1466 return isalphaW( wc );
1469 /*********************************************************************
1470 * iswalpha_l (MSVCRT.@)
1472 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1474 return isalphaW( wc );
1477 /*********************************************************************
1478 * iswcntrl (MSVCRT.@)
1480 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1482 return iscntrlW( wc );
1485 /*********************************************************************
1486 * iswdigit (MSVCRT.@)
1488 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1490 return isdigitW( wc );
1493 /*********************************************************************
1494 * iswgraph (MSVCRT.@)
1496 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1498 return isgraphW( wc );
1501 /*********************************************************************
1502 * iswlower (MSVCRT.@)
1504 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1506 return islowerW( wc );
1509 /*********************************************************************
1510 * iswprint (MSVCRT.@)
1512 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1514 return isprintW( wc );
1517 /*********************************************************************
1518 * iswpunct (MSVCRT.@)
1520 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1522 return ispunctW( wc );
1525 /*********************************************************************
1526 * iswspace (MSVCRT.@)
1528 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1530 return isspaceW( wc );
1533 /*********************************************************************
1534 * iswupper (MSVCRT.@)
1536 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1538 return isupperW( wc );
1541 /*********************************************************************
1542 * iswxdigit (MSVCRT.@)
1544 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1546 return isxdigitW( wc );
1549 /*********************************************************************
1550 * wcscpy_s (MSVCRT.@)
1552 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1554 MSVCRT_size_t size = 0;
1556 if(!wcDest || !numElement)
1557 return MSVCRT_EINVAL;
1563 return MSVCRT_EINVAL;
1566 size = strlenW(wcSrc) + 1;
1568 if(size > numElement)
1570 return MSVCRT_ERANGE;
1573 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1578 /******************************************************************
1579 * wcsncpy_s (MSVCRT.@)
1581 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1582 MSVCRT_size_t count )
1584 MSVCRT_size_t size = 0;
1586 if (!wcDest || !numElement)
1587 return MSVCRT_EINVAL;
1593 return MSVCRT_EINVAL;
1596 size = min(strlenW(wcSrc), count);
1598 if (size >= numElement)
1600 return MSVCRT_ERANGE;
1603 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1604 wcDest[size] = '\0';
1609 /******************************************************************
1610 * wcscat_s (MSVCRT.@)
1613 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1615 MSVCRT_wchar_t* ptr = dst;
1617 if (!dst || elem == 0) return MSVCRT_EINVAL;
1621 return MSVCRT_EINVAL;
1624 /* seek to end of dst string (or elem if no end of string is found */
1625 while (ptr < dst + elem && *ptr != '\0') ptr++;
1626 while (ptr < dst + elem)
1628 if ((*ptr++ = *src++) == '\0') return 0;
1630 /* not enough space */
1632 return MSVCRT_ERANGE;
1635 /*********************************************************************
1636 * wcsncat_s (MSVCRT.@)
1639 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
1640 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
1642 MSVCRT_size_t srclen;
1643 MSVCRT_wchar_t dststart;
1646 if (!MSVCRT_CHECK_PMT(dst != NULL) || !MSVCRT_CHECK_PMT(elem > 0))
1648 *MSVCRT__errno() = MSVCRT_EINVAL;
1649 return MSVCRT_EINVAL;
1651 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0))
1652 return MSVCRT_EINVAL;
1656 for (dststart = 0; dststart < elem; dststart++)
1658 if (dst[dststart] == '\0')
1661 if (dststart == elem)
1663 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n");
1664 return MSVCRT_EINVAL;
1667 if (count == MSVCRT__TRUNCATE)
1669 srclen = strlenW(src);
1670 if (srclen >= (elem - dststart))
1672 srclen = elem - dststart - 1;
1673 ret = MSVCRT_STRUNCATE;
1677 srclen = min(strlenW(src), count);
1678 if (srclen < (elem - dststart))
1680 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
1684 MSVCRT_INVALID_PMT("dst[elem] is too small");
1686 return MSVCRT_ERANGE;
1689 /*********************************************************************
1690 * _wcstoi64_l (MSVCRT.@)
1692 * FIXME: locale parameter is ignored
1694 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1695 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1697 BOOL negative = FALSE;
1700 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1702 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
1703 !MSVCRT_CHECK_PMT(base <= 36)) {
1704 *MSVCRT__errno() = MSVCRT_EINVAL;
1708 while(isspaceW(*nptr)) nptr++;
1713 } else if(*nptr == '+')
1716 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1729 char cur = tolowerW(*nptr);
1737 if(cur<'a' || cur>='a'+base-10)
1747 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
1748 ret = MSVCRT_I64_MAX;
1749 *MSVCRT__errno() = MSVCRT_ERANGE;
1750 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
1751 ret = MSVCRT_I64_MIN;
1752 *MSVCRT__errno() = MSVCRT_ERANGE;
1758 *endptr = (MSVCRT_wchar_t*)nptr;
1763 /*********************************************************************
1764 * _wcstoi64 (MSVCRT.@)
1766 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
1767 MSVCRT_wchar_t **endptr, int base)
1769 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
1772 /*********************************************************************
1773 * _wcstoui64_l (MSVCRT.@)
1775 * FIXME: locale parameter is ignored
1777 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
1778 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1780 BOOL negative = FALSE;
1781 unsigned __int64 ret = 0;
1783 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1785 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
1786 !MSVCRT_CHECK_PMT(base <= 36)) {
1787 *MSVCRT__errno() = MSVCRT_EINVAL;
1791 while(isspaceW(*nptr)) nptr++;
1796 } else if(*nptr == '+')
1799 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1812 char cur = tolowerW(*nptr);
1820 if(cur<'a' || cur>='a'+base-10)
1827 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
1828 ret = MSVCRT_UI64_MAX;
1829 *MSVCRT__errno() = MSVCRT_ERANGE;
1835 *endptr = (MSVCRT_wchar_t*)nptr;
1837 return negative ? -ret : ret;
1840 /*********************************************************************
1841 * _wcstoui64 (MSVCRT.@)
1843 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
1844 MSVCRT_wchar_t **endptr, int base)
1846 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
1849 /******************************************************************
1850 * wcsnlen (MSVCRT.@)
1852 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
1856 for (i = 0; i < maxlen; i++)