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
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
33 /*********************************************************************
36 MSVCRT_wchar_t
* CDECL
_wcsdup( const MSVCRT_wchar_t
* str
)
38 MSVCRT_wchar_t
* ret
= NULL
;
41 int size
= (strlenW(str
) + 1) * sizeof(MSVCRT_wchar_t
);
42 ret
= MSVCRT_malloc( size
);
43 if (ret
) memcpy( ret
, str
, size
);
48 /*********************************************************************
49 * _wcsicoll (MSVCRT.@)
51 INT CDECL
_wcsicoll( const MSVCRT_wchar_t
* str1
, const MSVCRT_wchar_t
* str2
)
53 /* FIXME: handle collates */
54 return strcmpiW( str1
, str2
);
57 /*********************************************************************
60 MSVCRT_wchar_t
* CDECL
MSVCRT__wcsnset( MSVCRT_wchar_t
* str
, MSVCRT_wchar_t c
, MSVCRT_size_t n
)
62 MSVCRT_wchar_t
* ret
= str
;
63 while ((n
-- > 0) && *str
) *str
++ = c
;
67 /*********************************************************************
70 MSVCRT_wchar_t
* CDECL
_wcsrev( MSVCRT_wchar_t
* str
)
72 MSVCRT_wchar_t
* ret
= str
;
73 MSVCRT_wchar_t
* end
= str
+ strlenW(str
) - 1;
76 MSVCRT_wchar_t t
= *end
;
83 /*********************************************************************
86 MSVCRT_wchar_t
* CDECL
_wcsset( MSVCRT_wchar_t
* str
, MSVCRT_wchar_t c
)
88 MSVCRT_wchar_t
* ret
= str
;
89 while (*str
) *str
++ = c
;
93 /******************************************************************
94 * _wcsupr_s (MSVCRT.@)
97 INT CDECL
MSVCRT__wcsupr_s( MSVCRT_wchar_t
* str
, MSVCRT_size_t n
)
99 MSVCRT_wchar_t
* ptr
= str
;
103 if (str
) *str
= '\0';
104 *MSVCRT__errno() = MSVCRT_EINVAL
;
105 return MSVCRT_EINVAL
;
111 *ptr
= toupperW(*ptr
);
115 /* MSDN claims that the function should return and set errno to
116 * ERANGE, which doesn't seem to be true based on the tests. */
118 *MSVCRT__errno() = MSVCRT_EINVAL
;
119 return MSVCRT_EINVAL
;
122 /*********************************************************************
125 double CDECL
MSVCRT_wcstod(const MSVCRT_wchar_t
* lpszStr
, MSVCRT_wchar_t
** end
)
127 const MSVCRT_wchar_t
* str
= lpszStr
;
129 double ret
= 0, divisor
= 10.0;
131 TRACE("(%s,%p) semi-stub\n", debugstr_w(lpszStr
), end
);
134 * - Should set errno on failure
135 * - Should fail on overflow
136 * - Need to check which input formats are allowed
138 while (isspaceW(*str
))
147 while (isdigitW(*str
))
149 ret
= ret
* 10.0 + (*str
- '0');
154 while (isdigitW(*str
))
156 ret
= ret
+ (*str
- '0') / divisor
;
161 if (*str
== 'E' || *str
== 'e' || *str
== 'D' || *str
== 'd')
163 int negativeExponent
= 0;
167 negativeExponent
= 1;
170 while (isdigitW(*str
))
172 exponent
= exponent
* 10 + (*str
- '0');
177 if (negativeExponent
)
178 ret
= ret
/ pow(10.0, exponent
);
180 ret
= ret
* pow(10.0, exponent
);
188 *end
= (MSVCRT_wchar_t
*)str
;
190 TRACE("returning %g\n", ret
);
195 typedef struct pf_output_t
206 typedef struct pf_flags_t
208 char Sign
, LeftAlign
, Alternate
, PadZero
;
209 int FieldLength
, Precision
;
210 char IntegerLength
, IntegerDouble
;
216 * writes a string of characters to the output
217 * returns -1 if the string doesn't fit in the output buffer
218 * return the length of the string if all characters were written
220 static inline int pf_output_stringW( pf_output
*out
, LPCWSTR str
, int len
)
222 int space
= out
->len
- out
->used
;
225 len
= strlenW( str
);
228 LPWSTR p
= out
->buf
.W
+ out
->used
;
232 if (out
->buf
.W
) memcpy( p
, str
, len
*sizeof(WCHAR
) );
236 if( space
> 0 && out
->buf
.W
)
237 memcpy( p
, str
, space
*sizeof(WCHAR
) );
242 int n
= WideCharToMultiByte( CP_ACP
, 0, str
, len
, NULL
, 0, NULL
, NULL
);
243 LPSTR p
= out
->buf
.A
+ out
->used
;
247 if (out
->buf
.A
) WideCharToMultiByte( CP_ACP
, 0, str
, len
, p
, n
, NULL
, NULL
);
251 if( space
> 0 && out
->buf
.A
)
252 WideCharToMultiByte( CP_ACP
, 0, str
, len
, p
, space
, NULL
, NULL
);
258 static inline int pf_output_stringA( pf_output
*out
, LPCSTR str
, int len
)
260 int space
= out
->len
- out
->used
;
266 LPSTR p
= out
->buf
.A
+ out
->used
;
270 if (out
->buf
.A
) memcpy( p
, str
, len
);
274 if( space
> 0 && out
->buf
.A
)
275 memcpy( p
, str
, space
);
280 int n
= MultiByteToWideChar( CP_ACP
, 0, str
, len
, NULL
, 0 );
281 LPWSTR p
= out
->buf
.W
+ out
->used
;
285 if (out
->buf
.W
) MultiByteToWideChar( CP_ACP
, 0, str
, len
, p
, n
);
289 if( space
> 0 && out
->buf
.W
)
290 MultiByteToWideChar( CP_ACP
, 0, str
, len
, p
, space
);
296 /* pf_fill: takes care of signs, alignment, zero and field padding */
297 static inline int pf_fill( pf_output
*out
, int len
, pf_flags
*flags
, char left
)
301 if( flags
->Sign
&& !( flags
->Format
== 'd' || flags
->Format
== 'i' ) )
304 if( left
&& flags
->Sign
)
306 flags
->FieldLength
--;
308 r
= pf_output_stringA( out
, &flags
->Sign
, 1 );
311 if( ( !left
&& flags
->LeftAlign
) ||
312 ( left
&& !flags
->LeftAlign
))
314 for( i
=0; (i
<(flags
->FieldLength
-len
)) && (r
>=0); i
++ )
316 if( left
&& flags
->PadZero
)
317 r
= pf_output_stringA( out
, "0", 1 );
319 r
= pf_output_stringA( out
, " ", 1 );
323 if( left
&& flags
->Sign
&& !flags
->PadZero
)
324 r
= pf_output_stringA( out
, &flags
->Sign
, 1 );
329 static inline int pf_output_format_W( pf_output
*out
, LPCWSTR str
,
330 int len
, pf_flags
*flags
)
335 len
= strlenW( str
);
337 if (flags
->Precision
>= 0 && flags
->Precision
< len
)
338 len
= flags
->Precision
;
340 r
= pf_fill( out
, len
, flags
, 1 );
343 r
= pf_output_stringW( out
, str
, len
);
346 r
= pf_fill( out
, len
, flags
, 0 );
351 static inline int pf_output_format_A( pf_output
*out
, LPCSTR str
,
352 int len
, pf_flags
*flags
)
359 if (flags
->Precision
>= 0 && flags
->Precision
< len
)
360 len
= flags
->Precision
;
362 r
= pf_fill( out
, len
, flags
, 1 );
365 r
= pf_output_stringA( out
, str
, len
);
368 r
= pf_fill( out
, len
, flags
, 0 );
373 static int pf_handle_string_format( pf_output
*out
, const void* str
, int len
,
374 pf_flags
*flags
, BOOL capital_letter
)
376 if(str
== NULL
) /* catch NULL pointer */
377 return pf_output_format_A( out
, "(null)", -1, flags
);
379 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
380 if(flags
->WideString
|| flags
->IntegerLength
== 'l')
381 return pf_output_format_W( out
, str
, len
, flags
);
382 if(flags
->IntegerLength
== 'h')
383 return pf_output_format_A( out
, str
, len
, flags
);
385 /* %s,%c -> chars in ansi functions & wchars in unicode
386 * %S,%C -> wchars in ansi functions & chars in unicode */
387 if( capital_letter
== out
->unicode
) /* either both TRUE or both FALSE */
388 return pf_output_format_A( out
, str
, len
, flags
);
390 return pf_output_format_W( out
, str
, len
, flags
);
393 static inline BOOL
pf_is_integer_format( char fmt
)
395 static const char float_fmts
[] = "diouxX";
398 return strchr( float_fmts
, fmt
) ? TRUE
: FALSE
;
401 static inline BOOL
pf_is_double_format( char fmt
)
403 static const char float_fmts
[] = "aeEfgG";
406 return strchr( float_fmts
, fmt
) ? TRUE
: FALSE
;
409 static inline BOOL
pf_is_valid_format( char fmt
)
411 static const char float_fmts
[] = "acCdeEfgGinouxX";
414 return strchr( float_fmts
, fmt
) ? TRUE
: FALSE
;
417 static void pf_rebuild_format_string( char *p
, pf_flags
*flags
)
422 if( flags
->LeftAlign
)
423 *p
++ = flags
->LeftAlign
;
424 if( flags
->Alternate
)
425 *p
++ = flags
->Alternate
;
427 *p
++ = flags
->PadZero
;
428 if( flags
->FieldLength
)
430 sprintf(p
, "%d", flags
->FieldLength
);
433 if( flags
->Precision
>= 0 )
435 sprintf(p
, ".%d", flags
->Precision
);
438 *p
++ = flags
->Format
;
442 /* pf_integer_conv: prints x to buf, including alternate formats and
443 additional precision digits, but not field characters or the sign */
444 static void pf_integer_conv( char *buf
, int buf_len
, pf_flags
*flags
,
451 char number
[40], *tmp
= number
;
453 if( buf_len
> sizeof number
)
454 tmp
= HeapAlloc( GetProcessHeap(), 0, buf_len
);
457 if( flags
->Format
== 'o' )
459 else if( flags
->Format
== 'x' || flags
->Format
== 'X' )
462 if( flags
->Format
== 'X' )
463 digits
= "0123456789ABCDEFX";
465 digits
= "0123456789abcdefx";
467 if( x
< 0 && ( flags
->Format
== 'd' || flags
->Format
== 'i' ) )
473 /* Do conversion (backwards) */
475 if( x
== 0 && flags
->Precision
)
480 j
= (ULONGLONG
) x
% base
;
481 x
= (ULONGLONG
) x
/ base
;
482 tmp
[i
++] = digits
[j
];
484 k
= flags
->Precision
- i
;
487 if( flags
->Alternate
)
491 tmp
[i
++] = digits
[16];
494 else if( base
== 8 && tmp
[i
-1] != '0' )
498 /* Reverse for buf */
504 /* Adjust precision so pf_fill won't truncate the number later */
505 flags
->Precision
= strlen( buf
);
508 HeapFree( GetProcessHeap(), 0, tmp
);
513 /* pf_fixup_exponent: convert a string containing a 2 digit exponent
514 to 3 digits, accounting for padding, in place. Needed to match
515 the native printf's which always use 3 digits. */
516 static void pf_fixup_exponent( char *buf
)
520 while (tmp
[0] && toupper(tmp
[0]) != 'E')
523 if (tmp
[0] && (tmp
[1] == '+' || tmp
[1] == '-') &&
524 isdigit(tmp
[2]) && isdigit(tmp
[3]))
529 return; /* Exponent already 3 digits */
531 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
539 /* We didn't expand into trailing space, so this string isn't left
540 * justified. Terminate the string and strip a ' ' at the start of
541 * the string if there is one (as there may be if the string is
546 memmove(buf
, buf
+ 1, (tmp
- buf
) + 3);
548 /* Otherwise, we expanded into trailing space -> nothing to do */
552 /*********************************************************************
553 * pf_vsnprintf (INTERNAL)
555 * implements both A and W vsnprintf functions
557 static int pf_vsnprintf( pf_output
*out
, const WCHAR
*format
,
558 MSVCRT__locale_t locale
, BOOL valid
, __ms_va_list valist
)
561 LPCWSTR q
, p
= format
;
565 locale
= get_locale();
567 TRACE("format is %s\n",debugstr_w(format
));
570 q
= strchrW( p
, '%' );
572 /* there's no % characters left, output the rest of the string */
575 r
= pf_output_stringW(out
, p
, -1);
582 /* there's characters before the %, output them */
585 r
= pf_output_stringW(out
, p
, q
- p
);
591 /* we must be at a % now, skip over it */
595 /* output a single % character */
598 r
= pf_output_stringW(out
, p
++, 1);
604 /* parse the flags */
605 memset( &flags
, 0, sizeof flags
);
608 if( *p
== '+' || *p
== ' ' )
610 if ( flags
.Sign
!= '+' )
614 flags
.LeftAlign
= *p
;
618 flags
.Alternate
= *p
;
624 /* deal with the field width specifier */
625 flags
.FieldLength
= 0;
628 flags
.FieldLength
= va_arg( valist
, int );
629 if (flags
.FieldLength
< 0)
631 flags
.LeftAlign
= '-';
632 flags
.FieldLength
= -flags
.FieldLength
;
636 else while( isdigit(*p
) )
638 flags
.FieldLength
*= 10;
639 flags
.FieldLength
+= *p
++ - '0';
642 /* deal with precision */
643 flags
.Precision
= -1;
650 flags
.Precision
= va_arg( valist
, int );
653 else while( isdigit(*p
) )
655 flags
.Precision
*= 10;
656 flags
.Precision
+= *p
++ - '0';
660 /* deal with integer width modifier */
663 if( *p
== 'h' || *p
== 'l' || *p
== 'L' )
665 flags
.IntegerLength
= *p
;
670 if( *(p
+1) == '6' && *(p
+2) == '4' )
672 flags
.IntegerDouble
++;
675 else if( *(p
+1) == '3' && *(p
+2) == '2' )
677 else if( isdigit(*(p
+1)) || *(p
+1) == 0 )
683 flags
.WideString
= *p
++;
693 if (flags
.Format
== '$')
695 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format
));
698 /* output a string */
699 if( flags
.Format
== 's' || flags
.Format
== 'S' )
700 r
= pf_handle_string_format( out
, va_arg(valist
, const void*), -1,
701 &flags
, (flags
.Format
== 'S') );
703 /* output a single character */
704 else if( flags
.Format
== 'c' || flags
.Format
== 'C' )
706 INT ch
= va_arg( valist
, int );
708 r
= pf_handle_string_format( out
, &ch
, 1, &flags
, (flags
.Format
== 'C') );
711 /* output a pointer */
712 else if( flags
.Format
== 'p' )
715 void *ptr
= va_arg( valist
, void * );
718 if( flags
.Alternate
)
719 sprintf(pointer
, "0X%0*lX", 2 * (int)sizeof(ptr
), (ULONG_PTR
)ptr
);
721 sprintf(pointer
, "%0*lX", 2 * (int)sizeof(ptr
), (ULONG_PTR
)ptr
);
722 r
= pf_output_format_A( out
, pointer
, -1, &flags
);
726 else if( flags
.Format
== 'n' )
728 int *x
= va_arg(valist
, int *);
732 /* deal with 64-bit integers */
733 else if( pf_is_integer_format( flags
.Format
) && flags
.IntegerDouble
)
735 char number
[40], *x
= number
;
737 /* Estimate largest possible required buffer size:
738 * Chooses the larger of the field or precision
739 * Includes extra bytes: 1 byte for null, 1 byte for sign,
740 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
741 for a decimal, and 1 byte for an additional float digit. */
742 int x_len
= ((flags
.FieldLength
> flags
.Precision
) ?
743 flags
.FieldLength
: flags
.Precision
) + 10;
745 if( x_len
>= sizeof number
)
746 x
= HeapAlloc( GetProcessHeap(), 0, x_len
);
748 pf_integer_conv( x
, x_len
, &flags
, va_arg(valist
, LONGLONG
) );
750 r
= pf_output_format_A( out
, x
, -1, &flags
);
752 HeapFree( GetProcessHeap(), 0, x
);
755 /* deal with integers and floats using libc's printf */
756 else if( pf_is_valid_format( flags
.Format
) )
758 char fmt
[20], number
[40], *x
= number
, *decimal_point
;
760 /* Estimate largest possible required buffer size:
761 * Chooses the larger of the field or precision
762 * Includes extra bytes: 1 byte for null, 1 byte for sign,
763 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
764 for a decimal, and 1 byte for an additional float digit. */
765 int x_len
= ((flags
.FieldLength
> flags
.Precision
) ?
766 flags
.FieldLength
: flags
.Precision
) + 10;
768 if( x_len
>= sizeof number
)
769 x
= HeapAlloc( GetProcessHeap(), 0, x_len
);
771 pf_rebuild_format_string( fmt
, &flags
);
773 if( pf_is_double_format( flags
.Format
) )
775 sprintf( x
, fmt
, va_arg(valist
, double) );
776 if (toupper(flags
.Format
) == 'E' || toupper(flags
.Format
) == 'G')
777 pf_fixup_exponent( x
);
780 sprintf( x
, fmt
, va_arg(valist
, int) );
782 decimal_point
= strchr(x
, '.');
784 *decimal_point
= *locale
->locinfo
->lconv
->decimal_point
;
786 r
= pf_output_stringA( out
, x
, -1 );
788 HeapFree( GetProcessHeap(), 0, x
);
796 MSVCRT__invalid_parameter( NULL
, NULL
, NULL
, 0, 0 );
797 *MSVCRT__errno() = MSVCRT_EINVAL
;
809 /* check we reached the end, and null terminate the string */
811 pf_output_stringW( out
, p
, 1 );
813 return out
->used
- 1;
816 /*********************************************************************
817 * vsnprintf_internal (INTERNAL)
819 static inline int vsnprintf_internal( char *str
, MSVCRT_size_t len
, const char *format
,
820 MSVCRT__locale_t locale
, BOOL valid
, __ms_va_list valist
)
823 LPWSTR formatW
= NULL
;
832 sz
= MultiByteToWideChar( CP_ACP
, 0, format
, -1, NULL
, 0 );
833 formatW
= HeapAlloc( GetProcessHeap(), 0, sz
*sizeof(WCHAR
) );
834 MultiByteToWideChar( CP_ACP
, 0, format
, -1, formatW
, sz
);
836 r
= pf_vsnprintf( &out
, formatW
, locale
, valid
, valist
);
838 HeapFree( GetProcessHeap(), 0, formatW
);
843 /*********************************************************************
844 * _vsnprintf (MSVCRT.@)
846 int CDECL
MSVCRT_vsnprintf( char *str
, MSVCRT_size_t len
,
847 const char *format
, __ms_va_list valist
)
849 return vsnprintf_internal(str
, len
, format
, NULL
, FALSE
, valist
);
852 /*********************************************************************
853 * _vsnprintf_l (MSVCRT.@)
855 int CDECL
MSVCRT_vsnprintf_l( char *str
, MSVCRT_size_t len
, const char *format
,
856 MSVCRT__locale_t locale
, __ms_va_list valist
)
858 return vsnprintf_internal(str
, len
, format
, locale
, FALSE
, valist
);
861 /*********************************************************************
862 * _vsnprintf_s_l (MSVCRT.@)
864 int CDECL
MSVCRT_vsnprintf_s_l( char *str
, MSVCRT_size_t sizeOfBuffer
,
865 MSVCRT_size_t count
, const char *format
,
866 MSVCRT__locale_t locale
, __ms_va_list valist
)
870 if(sizeOfBuffer
<count
+1 || count
==-1)
875 ret
= vsnprintf_internal(str
, len
, format
, locale
, TRUE
, valist
);
877 if(ret
<0 || ret
==len
) {
878 if(count
!=_TRUNCATE
&& count
>sizeOfBuffer
) {
879 MSVCRT__invalid_parameter( NULL
, NULL
, NULL
, 0, 0 );
880 *MSVCRT__errno() = MSVCRT_ERANGE
;
881 memset(str
, 0, sizeOfBuffer
);
891 /*********************************************************************
892 * _vsnprintf_s (MSVCRT.@)
894 int CDECL
MSVCRT_vsnprintf_s( char *str
, MSVCRT_size_t sizeOfBuffer
,
895 MSVCRT_size_t count
, const char *format
, __ms_va_list valist
)
897 return MSVCRT_vsnprintf_s_l(str
,sizeOfBuffer
, count
, format
, NULL
, valist
);
900 /*********************************************************************
901 * vsprintf (MSVCRT.@)
903 int CDECL
MSVCRT_vsprintf( char *str
, const char *format
, __ms_va_list valist
)
905 return MSVCRT_vsnprintf(str
, INT_MAX
, format
, valist
);
908 /*********************************************************************
909 * vsprintf_s (MSVCRT.@)
911 int CDECL
MSVCRT_vsprintf_s( char *str
, MSVCRT_size_t num
, const char *format
, __ms_va_list valist
)
913 return MSVCRT_vsnprintf(str
, num
, format
, valist
);
916 /*********************************************************************
917 * _vscprintf (MSVCRT.@)
919 int CDECL
_vscprintf( const char *format
, __ms_va_list valist
)
921 return MSVCRT_vsnprintf( NULL
, INT_MAX
, format
, valist
);
924 /*********************************************************************
925 * _snprintf (MSVCRT.@)
927 int CDECL
MSVCRT__snprintf(char *str
, unsigned int len
, const char *format
, ...)
931 __ms_va_start(valist
, format
);
932 retval
= MSVCRT_vsnprintf(str
, len
, format
, valist
);
937 /*********************************************************************
938 * vsnwsprintf_internal (INTERNAL)
940 static inline int vsnwprintf_internal(MSVCRT_wchar_t
*str
, MSVCRT_size_t len
,
941 const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
, BOOL valid
,
951 return pf_vsnprintf( &out
, format
, locale
, valid
, valist
);
954 /*********************************************************************
955 * _vsnwsprintf (MSVCRT.@)
957 int CDECL
MSVCRT_vsnwprintf(MSVCRT_wchar_t
*str
, MSVCRT_size_t len
,
958 const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
960 return vsnwprintf_internal(str
, len
, format
, NULL
, FALSE
, valist
);
963 /*********************************************************************
964 * _vsnwsprintf_l (MSVCRT.@)
966 int CDECL
MSVCRT_vsnwprintf_l(MSVCRT_wchar_t
*str
, MSVCRT_size_t len
,
967 const MSVCRT_wchar_t
*format
, MSVCRT__locale_t locale
,
970 return vsnwprintf_internal(str
, len
, format
, locale
, FALSE
, valist
);
973 /*********************************************************************
974 * _vsnwsprintf_s_l (MSVCRT.@)
976 int CDECL
MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t
*str
, MSVCRT_size_t sizeOfBuffer
,
977 MSVCRT_size_t count
, const MSVCRT_wchar_t
*format
,
978 MSVCRT__locale_t locale
, __ms_va_list valist
)
982 len
= sizeOfBuffer
/sizeof(MSVCRT_wchar_t
);
983 if(count
!=-1 && len
>count
+1)
986 ret
= vsnwprintf_internal(str
, len
, format
, locale
, TRUE
, valist
);
988 if(ret
<0 || ret
==len
) {
989 if(count
!=_TRUNCATE
&& count
>sizeOfBuffer
/sizeof(MSVCRT_wchar_t
)) {
990 MSVCRT__invalid_parameter( NULL
, NULL
, NULL
, 0, 0 );
991 *MSVCRT__errno() = MSVCRT_ERANGE
;
992 memset(str
, 0, sizeOfBuffer
);
1002 /*********************************************************************
1003 * _vsnwsprintf_s (MSVCRT.@)
1005 int CDECL
MSVCRT_vsnwprintf_s(MSVCRT_wchar_t
*str
, MSVCRT_size_t sizeOfBuffer
,
1006 MSVCRT_size_t count
, const MSVCRT_wchar_t
*format
, __ms_va_list valist
)
1008 return MSVCRT_vsnwprintf_s_l(str
, sizeOfBuffer
, count
,
1009 format
, NULL
, valist
);
1012 /*********************************************************************
1013 * _snwprintf (MSVCRT.@)
1015 int CDECL
MSVCRT__snwprintf( MSVCRT_wchar_t
*str
, unsigned int len
, const MSVCRT_wchar_t
*format
, ...)
1018 __ms_va_list valist
;
1019 __ms_va_start(valist
, format
);
1020 retval
= MSVCRT_vsnwprintf(str
, len
, format
, valist
);
1021 __ms_va_end(valist
);
1025 /*********************************************************************
1026 * sprintf (MSVCRT.@)
1028 int CDECL
MSVCRT_sprintf( char *str
, const char *format
, ... )
1033 __ms_va_start( ap
, format
);
1034 r
= MSVCRT_vsnprintf( str
, INT_MAX
, format
, ap
);
1039 /*********************************************************************
1040 * sprintf_s (MSVCRT.@)
1042 int CDECL
MSVCRT_sprintf_s( char *str
, MSVCRT_size_t num
, const char *format
, ... )
1047 __ms_va_start( ap
, format
);
1048 r
= MSVCRT_vsnprintf( str
, num
, format
, ap
);
1053 /*********************************************************************
1054 * swprintf (MSVCRT.@)
1056 int CDECL
MSVCRT_swprintf( MSVCRT_wchar_t
*str
, const MSVCRT_wchar_t
*format
, ... )
1061 __ms_va_start( ap
, format
);
1062 r
= MSVCRT_vsnwprintf( str
, INT_MAX
, format
, ap
);
1067 /*********************************************************************
1068 * vswprintf (MSVCRT.@)
1070 int CDECL
MSVCRT_vswprintf( MSVCRT_wchar_t
* str
, const MSVCRT_wchar_t
* format
, __ms_va_list args
)
1072 return MSVCRT_vsnwprintf( str
, INT_MAX
, format
, args
);
1075 /*********************************************************************
1076 * _vscwprintf (MSVCRT.@)
1078 int CDECL
_vscwprintf( const MSVCRT_wchar_t
*format
, __ms_va_list args
)
1080 return MSVCRT_vsnwprintf( NULL
, INT_MAX
, format
, args
);
1083 /*********************************************************************
1084 * vswprintf_s (MSVCRT.@)
1086 int CDECL
MSVCRT_vswprintf_s(MSVCRT_wchar_t
* str
, MSVCRT_size_t numberOfElements
,
1087 const MSVCRT_wchar_t
* format
, __ms_va_list args
)
1089 return MSVCRT_vsnwprintf_s(str
, numberOfElements
*sizeof(MSVCRT_wchar_t
),
1090 INT_MAX
, format
, args
);
1093 /*********************************************************************
1094 * _vswprintf_s_l (MSVCRT.@)
1096 int CDECL
MSVCRT_vswprintf_s_l(MSVCRT_wchar_t
* str
, MSVCRT_size_t numberOfElements
,
1097 const MSVCRT_wchar_t
* format
, MSVCRT__locale_t locale
, __ms_va_list args
)
1099 return MSVCRT_vsnwprintf_s_l(str
, numberOfElements
*sizeof(MSVCRT_wchar_t
),
1100 INT_MAX
, format
, locale
, args
);
1103 /*********************************************************************
1104 * wcscoll (MSVCRT.@)
1106 int CDECL
MSVCRT_wcscoll( const MSVCRT_wchar_t
* str1
, const MSVCRT_wchar_t
* str2
)
1108 /* FIXME: handle collates */
1109 return strcmpW( str1
, str2
);
1112 /*********************************************************************
1113 * wcspbrk (MSVCRT.@)
1115 MSVCRT_wchar_t
* CDECL
MSVCRT_wcspbrk( const MSVCRT_wchar_t
* str
, const MSVCRT_wchar_t
* accept
)
1117 const MSVCRT_wchar_t
* p
;
1120 for (p
= accept
; *p
; p
++) if (*p
== *str
) return (MSVCRT_wchar_t
*)str
;
1126 /*********************************************************************
1129 MSVCRT_wchar_t
* CDECL
MSVCRT_wcstok( MSVCRT_wchar_t
*str
, const MSVCRT_wchar_t
*delim
)
1131 thread_data_t
*data
= msvcrt_get_thread_data();
1132 MSVCRT_wchar_t
*ret
;
1135 if (!(str
= data
->wcstok_next
)) return NULL
;
1137 while (*str
&& strchrW( delim
, *str
)) str
++;
1138 if (!*str
) return NULL
;
1140 while (*str
&& !strchrW( delim
, *str
)) str
++;
1141 if (*str
) *str
++ = 0;
1142 data
->wcstok_next
= str
;
1147 /*********************************************************************
1150 INT CDECL
MSVCRT_wctomb( char *dst
, MSVCRT_wchar_t ch
)
1152 return WideCharToMultiByte( CP_ACP
, 0, &ch
, 1, dst
, 6, NULL
, NULL
);
1155 /*********************************************************************
1156 * iswalnum (MSVCRT.@)
1158 INT CDECL
MSVCRT_iswalnum( MSVCRT_wchar_t wc
)
1160 return isalnumW( wc
);
1163 /*********************************************************************
1164 * iswalpha (MSVCRT.@)
1166 INT CDECL
MSVCRT_iswalpha( MSVCRT_wchar_t wc
)
1168 return isalphaW( wc
);
1171 /*********************************************************************
1172 * iswalpha_l (MSVCRT.@)
1174 INT CDECL
MSVCRT__iswalpha_l( MSVCRT_wchar_t wc
, MSVCRT__locale_t locale
)
1176 return isalphaW( wc
);
1179 /*********************************************************************
1180 * iswcntrl (MSVCRT.@)
1182 INT CDECL
MSVCRT_iswcntrl( MSVCRT_wchar_t wc
)
1184 return iscntrlW( wc
);
1187 /*********************************************************************
1188 * iswdigit (MSVCRT.@)
1190 INT CDECL
MSVCRT_iswdigit( MSVCRT_wchar_t wc
)
1192 return isdigitW( wc
);
1195 /*********************************************************************
1196 * iswgraph (MSVCRT.@)
1198 INT CDECL
MSVCRT_iswgraph( MSVCRT_wchar_t wc
)
1200 return isgraphW( wc
);
1203 /*********************************************************************
1204 * iswlower (MSVCRT.@)
1206 INT CDECL
MSVCRT_iswlower( MSVCRT_wchar_t wc
)
1208 return islowerW( wc
);
1211 /*********************************************************************
1212 * iswprint (MSVCRT.@)
1214 INT CDECL
MSVCRT_iswprint( MSVCRT_wchar_t wc
)
1216 return isprintW( wc
);
1219 /*********************************************************************
1220 * iswpunct (MSVCRT.@)
1222 INT CDECL
MSVCRT_iswpunct( MSVCRT_wchar_t wc
)
1224 return ispunctW( wc
);
1227 /*********************************************************************
1228 * iswspace (MSVCRT.@)
1230 INT CDECL
MSVCRT_iswspace( MSVCRT_wchar_t wc
)
1232 return isspaceW( wc
);
1235 /*********************************************************************
1236 * iswupper (MSVCRT.@)
1238 INT CDECL
MSVCRT_iswupper( MSVCRT_wchar_t wc
)
1240 return isupperW( wc
);
1243 /*********************************************************************
1244 * iswxdigit (MSVCRT.@)
1246 INT CDECL
MSVCRT_iswxdigit( MSVCRT_wchar_t wc
)
1248 return isxdigitW( wc
);
1251 /*********************************************************************
1252 * wcscpy_s (MSVCRT.@)
1254 INT CDECL
MSVCRT_wcscpy_s( MSVCRT_wchar_t
* wcDest
, MSVCRT_size_t numElement
, const MSVCRT_wchar_t
*wcSrc
)
1256 MSVCRT_size_t size
= 0;
1258 if(!wcDest
|| !numElement
)
1259 return MSVCRT_EINVAL
;
1265 return MSVCRT_EINVAL
;
1268 size
= strlenW(wcSrc
) + 1;
1270 if(size
> numElement
)
1272 return MSVCRT_ERANGE
;
1275 memcpy( wcDest
, wcSrc
, size
*sizeof(WCHAR
) );
1280 /******************************************************************
1281 * wcsncpy_s (MSVCRT.@)
1283 INT CDECL
MSVCRT_wcsncpy_s( MSVCRT_wchar_t
* wcDest
, MSVCRT_size_t numElement
, const MSVCRT_wchar_t
*wcSrc
,
1284 MSVCRT_size_t count
)
1286 MSVCRT_size_t size
= 0;
1288 if (!wcDest
|| !numElement
)
1289 return MSVCRT_EINVAL
;
1295 return MSVCRT_EINVAL
;
1298 size
= min(strlenW(wcSrc
), count
);
1300 if (size
>= numElement
)
1302 return MSVCRT_ERANGE
;
1305 memcpy( wcDest
, wcSrc
, size
*sizeof(WCHAR
) );
1306 wcDest
[size
] = '\0';
1311 /******************************************************************
1312 * wcscat_s (MSVCRT.@)
1315 INT CDECL
MSVCRT_wcscat_s(MSVCRT_wchar_t
* dst
, MSVCRT_size_t elem
, const MSVCRT_wchar_t
* src
)
1317 MSVCRT_wchar_t
* ptr
= dst
;
1319 if (!dst
|| elem
== 0) return MSVCRT_EINVAL
;
1323 return MSVCRT_EINVAL
;
1326 /* seek to end of dst string (or elem if no end of string is found */
1327 while (ptr
< dst
+ elem
&& *ptr
!= '\0') ptr
++;
1328 while (ptr
< dst
+ elem
)
1330 if ((*ptr
++ = *src
++) == '\0') return 0;
1332 /* not enough space */
1334 return MSVCRT_ERANGE
;