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
== 'h' || *p
== 'l' || *p
== 'L' )
836 flags
.IntegerLength
= *p
;
841 if( *(p
+1) == '6' && *(p
+2) == '4' )
843 flags
.IntegerDouble
++;
846 else if( *(p
+1) == '3' && *(p
+2) == '2' )
848 else if( isdigit(*(p
+1)) || *(p
+1) == 0 )
854 flags
.WideString
= *p
++;
864 if (flags
.Format
== '$')
866 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format
));
869 /* output a string */
870 if( flags
.Format
== 's' || flags
.Format
== 'S' )
871 r
= pf_handle_string_format( out
, va_arg(valist
, const void*), -1,
872 &flags
, (flags
.Format
== 'S') );
874 /* output a single character */
875 else if( flags
.Format
== 'c' || flags
.Format
== 'C' )
877 INT ch
= va_arg( valist
, int );
879 r
= pf_handle_string_format( out
, &ch
, 1, &flags
, (flags
.Format
== 'C') );
882 /* output a pointer */
883 else if( flags
.Format
== 'p' )
886 void *ptr
= va_arg( valist
, void * );
889 if( flags
.Alternate
)
890 sprintf(pointer
, "0X%0*lX", 2 * (int)sizeof(ptr
), (ULONG_PTR
)ptr
);
892 sprintf(pointer
, "%0*lX", 2 * (int)sizeof(ptr
), (ULONG_PTR
)ptr
);
893 r
= pf_output_format_A( out
, pointer
, -1, &flags
);
897 else if( flags
.Format
== 'n' )
899 int *x
= va_arg(valist
, int *);
903 /* deal with 64-bit integers */
904 else if( pf_is_integer_format( flags
.Format
) && flags
.IntegerDouble
)
906 char number
[40], *x
= number
;
908 /* Estimate largest possible required buffer size:
909 * Chooses the larger of the field or precision
910 * Includes extra bytes: 1 byte for null, 1 byte for sign,
911 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
912 for a decimal, and 1 byte for an additional float digit. */
913 int x_len
= ((flags
.FieldLength
> flags
.Precision
) ?
914 flags
.FieldLength
: flags
.Precision
) + 10;
916 if( x_len
>= sizeof number
)
917 x
= HeapAlloc( GetProcessHeap(), 0, x_len
);
919 pf_integer_conv( x
, x_len
, &flags
, va_arg(valist
, LONGLONG
) );
921 r
= pf_output_format_A( out
, x
, -1, &flags
);
923 HeapFree( GetProcessHeap(), 0, x
);
926 /* deal with integers and floats using libc's printf */
927 else if( pf_is_valid_format( flags
.Format
) )
929 char fmt
[20], number
[40], *x
= number
, *decimal_point
;
931 /* Estimate largest possible required buffer size:
932 * Chooses the larger of the field or precision
933 * Includes extra bytes: 1 byte for null, 1 byte for sign,
934 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
935 for a decimal, and 1 byte for an additional float digit. */
936 int x_len
= ((flags
.FieldLength
> flags
.Precision
) ?
937 flags
.FieldLength
: flags
.Precision
) + 10;
939 if( x_len
>= sizeof number
)
940 x
= HeapAlloc( GetProcessHeap(), 0, x_len
);
942 pf_rebuild_format_string( fmt
, &flags
);
944 if( pf_is_double_format( flags
.Format
) )
946 sprintf( x
, fmt
, va_arg(valist
, double) );
947 if (toupper(flags
.Format
) == 'E' || toupper(flags
.Format
) == 'G')
948 pf_fixup_exponent( x
);
951 sprintf( x
, fmt
, va_arg(valist
, int) );
953 decimal_point
= strchr(x
, '.');
955 *decimal_point
= *locale
->locinfo
->lconv
->decimal_point
;
957 r
= pf_output_stringA( out
, x
, -1 );
959 HeapFree( GetProcessHeap(), 0, x
);
967 MSVCRT__invalid_parameter( NULL
, NULL
, NULL
, 0, 0 );
968 *MSVCRT__errno() = MSVCRT_EINVAL
;
980 /* check we reached the end, and null terminate the string */
982 pf_output_stringW( out
, p
, 1 );
984 return out
->used
- 1;
987 /*********************************************************************
988 * vsnprintf_internal (INTERNAL)
990 static inline int vsnprintf_internal( char *str
, MSVCRT_size_t len
, const char *format
,
991 MSVCRT__locale_t locale
, BOOL valid
, __ms_va_list valist
)
994 LPWSTR formatW
= NULL
;
1003 sz
= MultiByteToWideChar( CP_ACP
, 0, format
, -1, NULL
, 0 );
1004 formatW
= HeapAlloc( GetProcessHeap(), 0, sz
*sizeof(WCHAR
) );
1005 MultiByteToWideChar( CP_ACP
, 0, format
, -1, formatW
, sz
);
1007 r
= pf_vsnprintf( &out
, formatW
, locale
, valid
, valist
);
1009 HeapFree( GetProcessHeap(), 0, formatW
);
1014 /*********************************************************************
1015 * _vsnprintf (MSVCRT.@)
1017 int CDECL
MSVCRT_vsnprintf( char *str
, MSVCRT_size_t len
,
1018 const char *format
, __ms_va_list valist
)
1020 return vsnprintf_internal(str
, len
, format
, NULL
, FALSE
, valist
);
1023 /*********************************************************************
1024 * _vsnprintf_l (MSVCRT.@)
1026 int CDECL
MSVCRT_vsnprintf_l( char *str
, MSVCRT_size_t len
, const char *format
,
1027 MSVCRT__locale_t locale
, __ms_va_list valist
)
1029 return vsnprintf_internal(str
, len
, format
, locale
, FALSE
, valist
);
1032 /*********************************************************************
1033 * _vsnprintf_s_l (MSVCRT.@)
1035 int CDECL
MSVCRT_vsnprintf_s_l( char *str
, MSVCRT_size_t sizeOfBuffer
,
1036 MSVCRT_size_t count
, const char *format
,
1037 MSVCRT__locale_t locale
, __ms_va_list valist
)
1041 if(sizeOfBuffer
<count
+1 || count
==-1)
1046 ret
= vsnprintf_internal(str
, len
, format
, locale
, TRUE
, valist
);
1048 if(ret
<0 || ret
==len
) {
1049 if(count
!=MSVCRT__TRUNCATE
&& count
>sizeOfBuffer
) {
1050 MSVCRT__invalid_parameter( NULL
, NULL
, NULL
, 0, 0 );
1051 *MSVCRT__errno() = MSVCRT_ERANGE
;
1052 memset(str
, 0, sizeOfBuffer
);
1062 /*********************************************************************
1063 * _vsnprintf_s (MSVCRT.@)
1065 int CDECL
MSVCRT_vsnprintf_s( char *str
, MSVCRT_size_t sizeOfBuffer
,
1066 MSVCRT_size_t count
, const char *format
, __ms_va_list valist
)
1068 return MSVCRT_vsnprintf_s_l(str
,sizeOfBuffer
, count
, format
, NULL
, valist
);
1071 /*********************************************************************
1072 * vsprintf (MSVCRT.@)
1074 int CDECL
MSVCRT_vsprintf( char *str
, const char *format
, __ms_va_list valist
)
1076 return MSVCRT_vsnprintf(str
, INT_MAX
, format
, valist
);
1079 /*********************************************************************
1080 * vsprintf_s (MSVCRT.@)
1082 int CDECL
MSVCRT_vsprintf_s( char *str
, MSVCRT_size_t num
, const char *format
, __ms_va_list valist
)
1084 return MSVCRT_vsnprintf(str
, num
, format
, valist
);
1087 /*********************************************************************
1088 * _vscprintf (MSVCRT.@)
1090 int CDECL
_vscprintf( const char *format
, __ms_va_list valist
)
1092 return MSVCRT_vsnprintf( NULL
, INT_MAX
, format
, valist
);
1095 /*********************************************************************
1096 * _snprintf (MSVCRT.@)
1098 int CDECL
MSVCRT__snprintf(char *str
, unsigned int len
, const char *format
, ...)
1101 __ms_va_list valist
;
1102 __ms_va_start(valist
, format
);
1103 retval
= MSVCRT_vsnprintf(str
, len
, format
, valist
);
1104 __ms_va_end(valist
);
1108 /*********************************************************************
1109 * _snprintf_s (MSVCRT.@)
1111 int CDECL
MSVCRT__snprintf_s(char *str
, unsigned int len
, unsigned int count
,
1112 const char *format
, ...)
1115 __ms_va_list valist
;
1116 __ms_va_start(valist
, format
);
1117 retval
= MSVCRT_vsnprintf_s_l(str
, len
, count
, format
, NULL
, valist
);
1118 __ms_va_end(valist
);
1122 /*********************************************************************
1123 * _scprintf (MSVCRT.@)
1125 int CDECL
MSVCRT__scprintf(const char *format
, ...)
1128 __ms_va_list valist
;
1129 __ms_va_start(valist
, format
);
1130 retval
= _vscprintf(format
, valist
);
1131 __ms_va_end(valist
);
1135 /*********************************************************************
1136 * vsnwprintf_internal (INTERNAL)
1138 static inline int vsnwprintf_internal(MSVCRT_wchar_t
*str
, MSVCRT_size_t len
,
1139 const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, BOOL valid
,
1140 __ms_va_list valist
)
1149 return pf_vsnprintf( &out
, format
, locale
, valid
, valist
);
1152 /*********************************************************************
1153 * _vsnwprintf (MSVCRT.@)
1155 int CDECL
MSVCRT_vsnwprintf(MSVCRT_wchar_t
*str
, MSVCRT_size_t len
,
1156 const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
1158 return vsnwprintf_internal(str
, len
, format
, NULL
, FALSE
, valist
);
1161 /*********************************************************************
1162 * _vsnwprintf_l (MSVCRT.@)
1164 int CDECL
MSVCRT_vsnwprintf_l(MSVCRT_wchar_t
*str
, MSVCRT_size_t len
,
1165 const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
,
1166 __ms_va_list valist
)
1168 return vsnwprintf_internal(str
, len
, format
, locale
, FALSE
, valist
);
1171 /*********************************************************************
1172 * _vsnwprintf_s_l (MSVCRT.@)
1174 int CDECL
MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t
*str
, MSVCRT_size_t sizeOfBuffer
,
1175 MSVCRT_size_t count
, const MSVCRT_wchar_t
*format
,
1176 MSVCRT__locale_t locale
, __ms_va_list valist
)
1181 if(count
!=-1 && len
>count
+1)
1184 ret
= vsnwprintf_internal(str
, len
, format
, locale
, TRUE
, valist
);
1186 if(ret
<0 || ret
==len
) {
1187 if(count
!=MSVCRT__TRUNCATE
&& count
>sizeOfBuffer
) {
1188 MSVCRT__invalid_parameter( NULL
, NULL
, NULL
, 0, 0 );
1189 *MSVCRT__errno() = MSVCRT_ERANGE
;
1190 memset(str
, 0, sizeOfBuffer
*sizeof(MSVCRT_wchar_t
));
1200 /*********************************************************************
1201 * _vsnwprintf_s (MSVCRT.@)
1203 int CDECL
MSVCRT_vsnwprintf_s(MSVCRT_wchar_t
*str
, MSVCRT_size_t sizeOfBuffer
,
1204 MSVCRT_size_t count
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
1206 return MSVCRT_vsnwprintf_s_l(str
, sizeOfBuffer
, count
,
1207 format
, NULL
, valist
);
1210 /*********************************************************************
1211 * _snwprintf (MSVCRT.@)
1213 int CDECL
MSVCRT__snwprintf( MSVCRT_wchar_t
*str
, unsigned int len
, const MSVCRT_wchar_t
*format
, ...)
1216 __ms_va_list valist
;
1217 __ms_va_start(valist
, format
);
1218 retval
= MSVCRT_vsnwprintf(str
, len
, format
, valist
);
1219 __ms_va_end(valist
);
1223 /*********************************************************************
1224 * _snwprintf_s (MSVCRT.@)
1226 int CDECL
MSVCRT__snwprintf_s( MSVCRT_wchar_t
*str
, unsigned int len
, unsigned int count
,
1227 const MSVCRT_wchar_t
*format
, ...)
1230 __ms_va_list valist
;
1231 __ms_va_start(valist
, format
);
1232 retval
= MSVCRT_vsnwprintf_s_l(str
, len
, count
, format
, NULL
, valist
);
1233 __ms_va_end(valist
);
1237 /*********************************************************************
1238 * sprintf (MSVCRT.@)
1240 int CDECL
MSVCRT_sprintf( char *str
, const char *format
, ... )
1245 __ms_va_start( ap
, format
);
1246 r
= MSVCRT_vsnprintf( str
, INT_MAX
, format
, ap
);
1251 /*********************************************************************
1252 * sprintf_s (MSVCRT.@)
1254 int CDECL
MSVCRT_sprintf_s( char *str
, MSVCRT_size_t num
, const char *format
, ... )
1259 __ms_va_start( ap
, format
);
1260 r
= MSVCRT_vsnprintf( str
, num
, format
, ap
);
1265 /*********************************************************************
1266 * _scwprintf (MSVCRT.@)
1268 int CDECL
MSVCRT__scwprintf( const MSVCRT_wchar_t
*format
, ... )
1273 __ms_va_start( ap
, format
);
1274 r
= MSVCRT_vsnwprintf( NULL
, INT_MAX
, format
, ap
);
1279 /*********************************************************************
1280 * swprintf (MSVCRT.@)
1282 int CDECL
MSVCRT_swprintf( MSVCRT_wchar_t
*str
, const MSVCRT_wchar_t
*format
, ... )
1287 __ms_va_start( ap
, format
);
1288 r
= MSVCRT_vsnwprintf( str
, INT_MAX
, format
, ap
);
1293 /*********************************************************************
1294 * swprintf_s (MSVCRT.@)
1296 int CDECL
MSVCRT_swprintf_s(MSVCRT_wchar_t
*str
, MSVCRT_size_t numberOfElements
,
1297 const MSVCRT_wchar_t
*format
, ... )
1302 __ms_va_start(ap
, format
);
1303 r
= MSVCRT_vsnwprintf_s(str
, numberOfElements
, INT_MAX
, format
, ap
);
1309 /*********************************************************************
1310 * vswprintf (MSVCRT.@)
1312 int CDECL
MSVCRT_vswprintf( MSVCRT_wchar_t
* str
, const MSVCRT_wchar_t
* format
, __ms_va_list args
)
1314 return MSVCRT_vsnwprintf( str
, INT_MAX
, format
, args
);
1317 /*********************************************************************
1318 * _vscwprintf (MSVCRT.@)
1320 int CDECL
_vscwprintf( const MSVCRT_wchar_t
*format
, __ms_va_list args
)
1322 return MSVCRT_vsnwprintf( NULL
, INT_MAX
, format
, args
);
1325 /*********************************************************************
1326 * vswprintf_s (MSVCRT.@)
1328 int CDECL
MSVCRT_vswprintf_s(MSVCRT_wchar_t
* str
, MSVCRT_size_t numberOfElements
,
1329 const MSVCRT_wchar_t
* format
, __ms_va_list args
)
1331 return MSVCRT_vsnwprintf_s(str
, numberOfElements
, INT_MAX
, format
, args
);
1334 /*********************************************************************
1335 * _vswprintf_s_l (MSVCRT.@)
1337 int CDECL
MSVCRT_vswprintf_s_l(MSVCRT_wchar_t
* str
, MSVCRT_size_t numberOfElements
,
1338 const MSVCRT_wchar_t
* format
, MSVCRT__locale_t locale
, __ms_va_list args
)
1340 return MSVCRT_vsnwprintf_s_l(str
, numberOfElements
, INT_MAX
,
1341 format
, locale
, args
);
1344 /*********************************************************************
1345 * wcscoll (MSVCRT.@)
1347 int CDECL
MSVCRT_wcscoll( const MSVCRT_wchar_t
* str1
, const MSVCRT_wchar_t
* str2
)
1349 /* FIXME: handle collates */
1350 return strcmpW( str1
, str2
);
1353 /*********************************************************************
1354 * wcspbrk (MSVCRT.@)
1356 MSVCRT_wchar_t
* CDECL
MSVCRT_wcspbrk( const MSVCRT_wchar_t
* str
, const MSVCRT_wchar_t
* accept
)
1358 const MSVCRT_wchar_t
* p
;
1361 for (p
= accept
; *p
; p
++) if (*p
== *str
) return (MSVCRT_wchar_t
*)str
;
1367 /*********************************************************************
1370 MSVCRT_wchar_t
* CDECL
MSVCRT_wcstok( MSVCRT_wchar_t
*str
, const MSVCRT_wchar_t
*delim
)
1372 thread_data_t
*data
= msvcrt_get_thread_data();
1373 MSVCRT_wchar_t
*ret
;
1376 if (!(str
= data
->wcstok_next
)) return NULL
;
1378 while (*str
&& strchrW( delim
, *str
)) str
++;
1379 if (!*str
) return NULL
;
1381 while (*str
&& !strchrW( delim
, *str
)) str
++;
1382 if (*str
) *str
++ = 0;
1383 data
->wcstok_next
= str
;
1388 /*********************************************************************
1391 INT CDECL
MSVCRT_wctomb( char *dst
, MSVCRT_wchar_t ch
)
1393 return WideCharToMultiByte( CP_ACP
, 0, &ch
, 1, dst
, 6, NULL
, NULL
);
1396 /*********************************************************************
1397 * iswalnum (MSVCRT.@)
1399 INT CDECL
MSVCRT_iswalnum( MSVCRT_wchar_t wc
)
1401 return isalnumW( wc
);
1404 /*********************************************************************
1405 * iswalpha (MSVCRT.@)
1407 INT CDECL
MSVCRT_iswalpha( MSVCRT_wchar_t wc
)
1409 return isalphaW( wc
);
1412 /*********************************************************************
1413 * iswalpha_l (MSVCRT.@)
1415 INT CDECL
MSVCRT__iswalpha_l( MSVCRT_wchar_t wc
, MSVCRT__locale_t locale
)
1417 return isalphaW( wc
);
1420 /*********************************************************************
1421 * iswcntrl (MSVCRT.@)
1423 INT CDECL
MSVCRT_iswcntrl( MSVCRT_wchar_t wc
)
1425 return iscntrlW( wc
);
1428 /*********************************************************************
1429 * iswdigit (MSVCRT.@)
1431 INT CDECL
MSVCRT_iswdigit( MSVCRT_wchar_t wc
)
1433 return isdigitW( wc
);
1436 /*********************************************************************
1437 * iswgraph (MSVCRT.@)
1439 INT CDECL
MSVCRT_iswgraph( MSVCRT_wchar_t wc
)
1441 return isgraphW( wc
);
1444 /*********************************************************************
1445 * iswlower (MSVCRT.@)
1447 INT CDECL
MSVCRT_iswlower( MSVCRT_wchar_t wc
)
1449 return islowerW( wc
);
1452 /*********************************************************************
1453 * iswprint (MSVCRT.@)
1455 INT CDECL
MSVCRT_iswprint( MSVCRT_wchar_t wc
)
1457 return isprintW( wc
);
1460 /*********************************************************************
1461 * iswpunct (MSVCRT.@)
1463 INT CDECL
MSVCRT_iswpunct( MSVCRT_wchar_t wc
)
1465 return ispunctW( wc
);
1468 /*********************************************************************
1469 * iswspace (MSVCRT.@)
1471 INT CDECL
MSVCRT_iswspace( MSVCRT_wchar_t wc
)
1473 return isspaceW( wc
);
1476 /*********************************************************************
1477 * iswupper (MSVCRT.@)
1479 INT CDECL
MSVCRT_iswupper( MSVCRT_wchar_t wc
)
1481 return isupperW( wc
);
1484 /*********************************************************************
1485 * iswxdigit (MSVCRT.@)
1487 INT CDECL
MSVCRT_iswxdigit( MSVCRT_wchar_t wc
)
1489 return isxdigitW( wc
);
1492 /*********************************************************************
1493 * wcscpy_s (MSVCRT.@)
1495 INT CDECL
MSVCRT_wcscpy_s( MSVCRT_wchar_t
* wcDest
, MSVCRT_size_t numElement
, const MSVCRT_wchar_t
*wcSrc
)
1497 MSVCRT_size_t size
= 0;
1499 if(!wcDest
|| !numElement
)
1500 return MSVCRT_EINVAL
;
1506 return MSVCRT_EINVAL
;
1509 size
= strlenW(wcSrc
) + 1;
1511 if(size
> numElement
)
1513 return MSVCRT_ERANGE
;
1516 memcpy( wcDest
, wcSrc
, size
*sizeof(WCHAR
) );
1521 /******************************************************************
1522 * wcsncpy_s (MSVCRT.@)
1524 INT CDECL
MSVCRT_wcsncpy_s( MSVCRT_wchar_t
* wcDest
, MSVCRT_size_t numElement
, const MSVCRT_wchar_t
*wcSrc
,
1525 MSVCRT_size_t count
)
1527 MSVCRT_size_t size
= 0;
1529 if (!wcDest
|| !numElement
)
1530 return MSVCRT_EINVAL
;
1536 return MSVCRT_EINVAL
;
1539 size
= min(strlenW(wcSrc
), count
);
1541 if (size
>= numElement
)
1543 return MSVCRT_ERANGE
;
1546 memcpy( wcDest
, wcSrc
, size
*sizeof(WCHAR
) );
1547 wcDest
[size
] = '\0';
1552 /******************************************************************
1553 * wcscat_s (MSVCRT.@)
1556 INT CDECL
MSVCRT_wcscat_s(MSVCRT_wchar_t
* dst
, MSVCRT_size_t elem
, const MSVCRT_wchar_t
* src
)
1558 MSVCRT_wchar_t
* ptr
= dst
;
1560 if (!dst
|| elem
== 0) return MSVCRT_EINVAL
;
1564 return MSVCRT_EINVAL
;
1567 /* seek to end of dst string (or elem if no end of string is found */
1568 while (ptr
< dst
+ elem
&& *ptr
!= '\0') ptr
++;
1569 while (ptr
< dst
+ elem
)
1571 if ((*ptr
++ = *src
++) == '\0') return 0;
1573 /* not enough space */
1575 return MSVCRT_ERANGE
;
1578 /*********************************************************************
1579 * _wcstoi64_l (MSVCRT.@)
1581 * FIXME: locale parameter is ignored
1583 __int64 CDECL
MSVCRT__wcstoi64_l(const MSVCRT_wchar_t
*nptr
,
1584 MSVCRT_wchar_t
**endptr
, int base
, MSVCRT__locale_t locale
)
1586 BOOL negative
= FALSE
;
1589 TRACE("(%s %p %d %p)\n", debugstr_w(nptr
), endptr
, base
, locale
);
1591 if(!nptr
|| base
<0 || base
>36 || base
==1) {
1592 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1596 while(isspaceW(*nptr
)) nptr
++;
1601 } else if(*nptr
== '+')
1604 if((base
==0 || base
==16) && *nptr
=='0' && tolowerW(*(nptr
+1))=='x') {
1617 char cur
= tolowerW(*nptr
);
1625 if(cur
<'a' || cur
>='a'+base
-10)
1635 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
1636 ret
= MSVCRT_I64_MAX
;
1637 *MSVCRT__errno() = MSVCRT_ERANGE
;
1638 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
1639 ret
= MSVCRT_I64_MIN
;
1640 *MSVCRT__errno() = MSVCRT_ERANGE
;
1646 *endptr
= (MSVCRT_wchar_t
*)nptr
;
1651 /*********************************************************************
1652 * _wcstoi64 (MSVCRT.@)
1654 __int64 CDECL
MSVCRT__wcstoi64(const MSVCRT_wchar_t
*nptr
,
1655 MSVCRT_wchar_t
**endptr
, int base
)
1657 return MSVCRT__wcstoi64_l(nptr
, endptr
, base
, NULL
);
1660 /*********************************************************************
1661 * _wcstoui64_l (MSVCRT.@)
1663 * FIXME: locale parameter is ignored
1665 unsigned __int64 CDECL
MSVCRT__wcstoui64_l(const MSVCRT_wchar_t
*nptr
,
1666 MSVCRT_wchar_t
**endptr
, int base
, MSVCRT__locale_t locale
)
1668 BOOL negative
= FALSE
;
1669 unsigned __int64 ret
= 0;
1671 TRACE("(%s %p %d %p)\n", debugstr_w(nptr
), endptr
, base
, locale
);
1673 if(!nptr
|| base
<0 || base
>36 || base
==1) {
1674 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
1678 while(isspaceW(*nptr
)) nptr
++;
1683 } else if(*nptr
== '+')
1686 if((base
==0 || base
==16) && *nptr
=='0' && tolowerW(*(nptr
+1))=='x') {
1699 char cur
= tolowerW(*nptr
);
1707 if(cur
<'a' || cur
>='a'+base
-10)
1714 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
1715 ret
= MSVCRT_UI64_MAX
;
1716 *MSVCRT__errno() = MSVCRT_ERANGE
;
1722 *endptr
= (MSVCRT_wchar_t
*)nptr
;
1724 return negative
? -ret
: ret
;
1727 /*********************************************************************
1728 * _wcstoui64 (MSVCRT.@)
1730 unsigned __int64 CDECL
MSVCRT__wcstoui64(const MSVCRT_wchar_t
*nptr
,
1731 MSVCRT_wchar_t
**endptr
, int base
)
1733 return MSVCRT__wcstoui64_l(nptr
, endptr
, base
, NULL
);