msi: MsiSetMode returns a UINT system error code, not a BOOL.
[wine/multimedia.git] / dlls / msvcrt / wcs.c
blob0f3e9eecf25b699b98061ee611fbc8ad60296f22
1 /*
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
21 #include <limits.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include <assert.h>
25 #include "msvcrt.h"
26 #include "winnls.h"
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33 /*********************************************************************
34 * _wcsdup (MSVCRT.@)
36 MSVCRT_wchar_t* CDECL _wcsdup( const MSVCRT_wchar_t* str )
38 MSVCRT_wchar_t* ret = NULL;
39 if (str)
41 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
42 ret = MSVCRT_malloc( size );
43 if (ret) memcpy( ret, str, size );
45 return ret;
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 /*********************************************************************
58 * _wcsnset (MSVCRT.@)
60 MSVCRT_wchar_t* CDECL _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;
64 return ret;
67 /*********************************************************************
68 * _wcsrev (MSVCRT.@)
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;
74 while (end > str)
76 MSVCRT_wchar_t t = *end;
77 *end-- = *str;
78 *str++ = t;
80 return ret;
83 /*********************************************************************
84 * _wcsset (MSVCRT.@)
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;
90 return ret;
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;
101 if (!str || !n)
103 if (str) *str = '\0';
104 *MSVCRT__errno() = MSVCRT_EINVAL;
105 return MSVCRT_EINVAL;
108 while (n--)
110 if (!*ptr) return 0;
111 *ptr = toupperW(*ptr);
112 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. */
117 *str = '\0';
118 *MSVCRT__errno() = MSVCRT_EINVAL;
119 return MSVCRT_EINVAL;
122 /*********************************************************************
123 * wcstod (MSVCRT.@)
125 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
127 const MSVCRT_wchar_t* str = lpszStr;
128 int negative = 0;
129 double ret = 0, divisor = 10.0;
131 TRACE("(%s,%p) semi-stub\n", debugstr_w(lpszStr), end);
133 /* FIXME:
134 * - Should set errno on failure
135 * - Should fail on overflow
136 * - Need to check which input formats are allowed
138 while (isspaceW(*str))
139 str++;
141 if (*str == '-')
143 negative = 1;
144 str++;
147 while (isdigitW(*str))
149 ret = ret * 10.0 + (*str - '0');
150 str++;
152 if (*str == '.')
153 str++;
154 while (isdigitW(*str))
156 ret = ret + (*str - '0') / divisor;
157 divisor *= 10;
158 str++;
161 if (*str == 'E' || *str == 'e' || *str == 'D' || *str == 'd')
163 int negativeExponent = 0;
164 int exponent = 0;
165 if (*(++str) == '-')
167 negativeExponent = 1;
168 str++;
170 while (isdigitW(*str))
172 exponent = exponent * 10 + (*str - '0');
173 str++;
175 if (exponent != 0)
177 if (negativeExponent)
178 ret = ret / pow(10.0, exponent);
179 else
180 ret = ret * pow(10.0, exponent);
184 if (negative)
185 ret = -ret;
187 if (end)
188 *end = (MSVCRT_wchar_t*)str;
190 TRACE("returning %g\n", ret);
191 return ret;
195 typedef struct pf_output_t
197 int used;
198 int len;
199 BOOL unicode;
200 union {
201 LPWSTR W;
202 LPSTR A;
203 } buf;
204 } pf_output;
206 typedef struct pf_flags_t
208 char Sign, LeftAlign, Alternate, PadZero;
209 int FieldLength, Precision;
210 char IntegerLength, IntegerDouble;
211 char WideString;
212 char Format;
213 } pf_flags;
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;
224 if( len < 0 )
225 len = strlenW( str );
226 if( out->unicode )
228 LPWSTR p = out->buf.W + out->used;
230 if( space >= len )
232 if (out->buf.W) memcpy( p, str, len*sizeof(WCHAR) );
233 out->used += len;
234 return len;
236 if( space > 0 && out->buf.W )
237 memcpy( p, str, space*sizeof(WCHAR) );
238 out->used += len;
240 else
242 int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
243 LPSTR p = out->buf.A + out->used;
245 if( space >= n )
247 if (out->buf.A) WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
248 out->used += n;
249 return len;
251 if( space > 0 && out->buf.A )
252 WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
253 out->used += n;
255 return -1;
258 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
260 int space = out->len - out->used;
262 if( len < 0 )
263 len = strlen( str );
264 if( !out->unicode )
266 LPSTR p = out->buf.A + out->used;
268 if( space >= len )
270 if (out->buf.A) memcpy( p, str, len );
271 out->used += len;
272 return len;
274 if( space > 0 && out->buf.A )
275 memcpy( p, str, space );
276 out->used += len;
278 else
280 int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
281 LPWSTR p = out->buf.W + out->used;
283 if( space >= n )
285 if (out->buf.W) MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
286 out->used += n;
287 return len;
289 if( space > 0 && out->buf.W )
290 MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
291 out->used += n;
293 return -1;
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 )
299 int i, r = 0;
301 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
302 flags->Sign = 0;
304 if( left && flags->Sign )
306 flags->FieldLength--;
307 if( flags->PadZero )
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 );
318 else
319 r = pf_output_stringA( out, " ", 1 );
323 if( left && flags->Sign && !flags->PadZero )
324 r = pf_output_stringA( out, &flags->Sign, 1 );
326 return r;
329 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
330 int len, pf_flags *flags )
332 int r = 0;
334 if( len < 0 )
335 len = strlenW( str );
337 if (flags->Precision >= 0 && flags->Precision < len)
338 len = flags->Precision;
340 r = pf_fill( out, len, flags, 1 );
342 if( r>=0 )
343 r = pf_output_stringW( out, str, len );
345 if( r>=0 )
346 r = pf_fill( out, len, flags, 0 );
348 return r;
351 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
352 int len, pf_flags *flags )
354 int r = 0;
356 if( len < 0 )
357 len = strlen( str );
359 if (flags->Precision >= 0 && flags->Precision < len)
360 len = flags->Precision;
362 r = pf_fill( out, len, flags, 1 );
364 if( r>=0 )
365 r = pf_output_stringA( out, str, len );
367 if( r>=0 )
368 r = pf_fill( out, len, flags, 0 );
370 return r;
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);
389 else
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";
396 if (!fmt)
397 return FALSE;
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";
404 if (!fmt)
405 return FALSE;
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";
412 if (!fmt)
413 return FALSE;
414 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
417 static void pf_rebuild_format_string( char *p, pf_flags *flags )
419 *p++ = '%';
420 if( flags->Sign )
421 *p++ = flags->Sign;
422 if( flags->LeftAlign )
423 *p++ = flags->LeftAlign;
424 if( flags->Alternate )
425 *p++ = flags->Alternate;
426 if( flags->PadZero )
427 *p++ = flags->PadZero;
428 if( flags->FieldLength )
430 sprintf(p, "%d", flags->FieldLength);
431 p += strlen(p);
433 if( flags->Precision >= 0 )
435 sprintf(p, ".%d", flags->Precision);
436 p += strlen(p);
438 *p++ = flags->Format;
439 *p++ = 0;
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,
445 LONGLONG x )
447 unsigned int base;
448 const char *digits;
450 int i, j, k;
451 char number[40], *tmp = number;
453 if( buf_len > sizeof number )
454 tmp = HeapAlloc( GetProcessHeap(), 0, buf_len );
456 base = 10;
457 if( flags->Format == 'o' )
458 base = 8;
459 else if( flags->Format == 'x' || flags->Format == 'X' )
460 base = 16;
462 if( flags->Format == 'X' )
463 digits = "0123456789ABCDEFX";
464 else
465 digits = "0123456789abcdefx";
467 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
469 x = -x;
470 flags->Sign = '-';
473 /* Do conversion (backwards) */
474 i = 0;
475 if( x == 0 && flags->Precision )
476 tmp[i++] = '0';
477 else
478 while( x != 0 )
480 j = (ULONGLONG) x % base;
481 x = (ULONGLONG) x / base;
482 tmp[i++] = digits[j];
484 k = flags->Precision - i;
485 while( k-- > 0 )
486 tmp[i++] = '0';
487 if( flags->Alternate )
489 if( base == 16 )
491 tmp[i++] = digits[16];
492 tmp[i++] = '0';
494 else if( base == 8 && tmp[i-1] != '0' )
495 tmp[i++] = '0';
498 /* Reverse for buf */
499 j = 0;
500 while( i-- > 0 )
501 buf[j++] = tmp[i];
502 buf[j] = '\0';
504 /* Adjust precision so pf_fill won't truncate the number later */
505 flags->Precision = strlen( buf );
507 if( tmp != number )
508 HeapFree( GetProcessHeap(), 0, tmp );
510 return;
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 )
518 char* tmp = buf;
520 while (tmp[0] && toupper(tmp[0]) != 'E')
521 tmp++;
523 if (tmp[0] && (tmp[1] == '+' || tmp[1] == '-') &&
524 isdigit(tmp[2]) && isdigit(tmp[3]))
526 char final;
528 if (isdigit(tmp[4]))
529 return; /* Exponent already 3 digits */
531 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
532 tmp += 2;
533 final = tmp[2];
534 tmp[2] = tmp[1];
535 tmp[1] = tmp[0];
536 tmp[0] = '0';
537 if (final == '\0')
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
542 * right justified).
544 tmp[3] = '\0';
545 if (buf[0] == ' ')
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, __ms_va_list valist )
559 int r;
560 LPCWSTR q, p = format;
561 pf_flags flags;
563 TRACE("format is %s\n",debugstr_w(format));
564 while (*p)
566 q = strchrW( p, '%' );
568 /* there's no % characters left, output the rest of the string */
569 if( !q )
571 r = pf_output_stringW(out, p, -1);
572 if( r<0 )
573 return r;
574 p += r;
575 continue;
578 /* there's characters before the %, output them */
579 if( q != p )
581 r = pf_output_stringW(out, p, q - p);
582 if( r<0 )
583 return r;
584 p = q;
587 /* we must be at a % now, skip over it */
588 assert( *p == '%' );
589 p++;
591 /* output a single % character */
592 if( *p == '%' )
594 r = pf_output_stringW(out, p++, 1);
595 if( r<0 )
596 return r;
597 continue;
600 /* parse the flags */
601 memset( &flags, 0, sizeof flags );
602 while (*p)
604 if( *p == '+' || *p == ' ' )
606 if ( flags.Sign != '+' )
607 flags.Sign = *p;
609 else if( *p == '-' )
610 flags.LeftAlign = *p;
611 else if( *p == '0' )
612 flags.PadZero = *p;
613 else if( *p == '#' )
614 flags.Alternate = *p;
615 else
616 break;
617 p++;
620 /* deal with the field width specifier */
621 flags.FieldLength = 0;
622 if( *p == '*' )
624 flags.FieldLength = va_arg( valist, int );
625 if (flags.FieldLength < 0)
627 flags.LeftAlign = '-';
628 flags.FieldLength = -flags.FieldLength;
630 p++;
632 else while( isdigit(*p) )
634 flags.FieldLength *= 10;
635 flags.FieldLength += *p++ - '0';
638 /* deal with precision */
639 flags.Precision = -1;
640 if( *p == '.' )
642 flags.Precision = 0;
643 p++;
644 if( *p == '*' )
646 flags.Precision = va_arg( valist, int );
647 p++;
649 else while( isdigit(*p) )
651 flags.Precision *= 10;
652 flags.Precision += *p++ - '0';
656 /* deal with integer width modifier */
657 while( *p )
659 if( *p == 'h' || *p == 'l' || *p == 'L' )
661 flags.IntegerLength = *p;
662 p++;
664 else if( *p == 'I' )
666 if( *(p+1) == '6' && *(p+2) == '4' )
668 flags.IntegerDouble++;
669 p += 3;
671 else if( *(p+1) == '3' && *(p+2) == '2' )
672 p += 3;
673 else if( isdigit(*(p+1)) || *(p+1) == 0 )
674 break;
675 else
676 p++;
678 else if( *p == 'w' )
679 flags.WideString = *p++;
680 else if( *p == 'F' )
681 p++; /* ignore */
682 else
683 break;
686 flags.Format = *p;
687 r = 0;
689 if (flags.Format == '$')
691 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
692 return -1;
694 /* output a string */
695 if( flags.Format == 's' || flags.Format == 'S' )
696 r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
697 &flags, (flags.Format == 'S') );
699 /* output a single character */
700 else if( flags.Format == 'c' || flags.Format == 'C' )
702 INT ch = va_arg( valist, int );
704 r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
707 /* output a pointer */
708 else if( flags.Format == 'p' )
710 char pointer[32];
711 void *ptr = va_arg( valist, void * );
713 flags.PadZero = 0;
714 if( flags.Alternate )
715 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
716 else
717 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
718 r = pf_output_format_A( out, pointer, -1, &flags );
721 /* deal with %n */
722 else if( flags.Format == 'n' )
724 int *x = va_arg(valist, int *);
725 *x = out->used;
728 /* deal with 64-bit integers */
729 else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
731 char number[40], *x = number;
733 /* Estimate largest possible required buffer size:
734 * Chooses the larger of the field or precision
735 * Includes extra bytes: 1 byte for null, 1 byte for sign,
736 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
737 for a decimal, and 1 byte for an additional float digit. */
738 int x_len = ((flags.FieldLength > flags.Precision) ?
739 flags.FieldLength : flags.Precision) + 10;
741 if( x_len >= sizeof number)
742 x = HeapAlloc( GetProcessHeap(), 0, x_len );
744 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
746 r = pf_output_format_A( out, x, -1, &flags );
747 if( x != number )
748 HeapFree( GetProcessHeap(), 0, x );
751 /* deal with integers and floats using libc's printf */
752 else if( pf_is_valid_format( flags.Format ) )
754 char fmt[20], number[40], *x = number;
756 /* Estimate largest possible required buffer size:
757 * Chooses the larger of the field or precision
758 * Includes extra bytes: 1 byte for null, 1 byte for sign,
759 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
760 for a decimal, and 1 byte for an additional float digit. */
761 int x_len = ((flags.FieldLength > flags.Precision) ?
762 flags.FieldLength : flags.Precision) + 10;
764 if( x_len >= sizeof number)
765 x = HeapAlloc( GetProcessHeap(), 0, x_len );
767 pf_rebuild_format_string( fmt, &flags );
769 if( pf_is_double_format( flags.Format ) )
771 sprintf( x, fmt, va_arg(valist, double) );
772 if (toupper(flags.Format) == 'E' || toupper(flags.Format) == 'G')
773 pf_fixup_exponent( x );
775 else
776 sprintf( x, fmt, va_arg(valist, int) );
778 r = pf_output_stringA( out, x, -1 );
779 if( x != number )
780 HeapFree( GetProcessHeap(), 0, x );
782 else
783 continue;
785 if( r<0 )
786 return r;
787 p++;
790 /* check we reached the end, and null terminate the string */
791 assert( *p == 0 );
792 pf_output_stringW( out, p, 1 );
794 return out->used - 1;
797 /*********************************************************************
798 * _vsnprintf (MSVCRT.@)
800 int CDECL MSVCRT_vsnprintf( char *str, unsigned int len,
801 const char *format, __ms_va_list valist )
803 DWORD sz;
804 LPWSTR formatW = NULL;
805 pf_output out;
806 int r;
808 out.unicode = FALSE;
809 out.buf.A = str;
810 out.used = 0;
811 out.len = len;
813 if( format )
815 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
816 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
817 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
820 r = pf_vsnprintf( &out, formatW, valist );
822 HeapFree( GetProcessHeap(), 0, formatW );
824 return r;
827 /*********************************************************************
828 * vsprintf (MSVCRT.@)
830 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
832 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
835 /*********************************************************************
836 * _vscprintf (MSVCRT.@)
838 int CDECL _vscprintf( const char *format, __ms_va_list valist )
840 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
843 /*********************************************************************
844 * _snprintf (MSVCRT.@)
846 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
848 int retval;
849 __ms_va_list valist;
850 __ms_va_start(valist, format);
851 retval = MSVCRT_vsnprintf(str, len, format, valist);
852 __ms_va_end(valist);
853 return retval;
856 /*********************************************************************
857 * _vsnwsprintf (MSVCRT.@)
859 int CDECL MSVCRT_vsnwprintf( MSVCRT_wchar_t *str, unsigned int len,
860 const MSVCRT_wchar_t *format, __ms_va_list valist )
862 pf_output out;
864 out.unicode = TRUE;
865 out.buf.W = str;
866 out.used = 0;
867 out.len = len;
869 return pf_vsnprintf( &out, format, valist );
872 /*********************************************************************
873 * _snwprintf (MSVCRT.@)
875 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
877 int retval;
878 __ms_va_list valist;
879 __ms_va_start(valist, format);
880 retval = MSVCRT_vsnwprintf(str, len, format, valist);
881 __ms_va_end(valist);
882 return retval;
885 /*********************************************************************
886 * sprintf (MSVCRT.@)
888 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
890 __ms_va_list ap;
891 int r;
893 __ms_va_start( ap, format );
894 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
895 __ms_va_end( ap );
896 return r;
899 /*********************************************************************
900 * swprintf (MSVCRT.@)
902 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
904 __ms_va_list ap;
905 int r;
907 __ms_va_start( ap, format );
908 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
909 __ms_va_end( ap );
910 return r;
913 /*********************************************************************
914 * vswprintf (MSVCRT.@)
916 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
918 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
921 /*********************************************************************
922 * _vscwprintf (MSVCRT.@)
924 int CDECL _vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
926 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
929 /*********************************************************************
930 * vswprintf_s (MSVCRT.@)
932 int CDECL MSVCRT_vswprintf_s( MSVCRT_wchar_t* str, MSVCRT_size_t num, const MSVCRT_wchar_t* format, __ms_va_list args )
934 /* FIXME: must handle positional arguments */
935 return MSVCRT_vsnwprintf( str, num, format, args );
938 /*********************************************************************
939 * wcscoll (MSVCRT.@)
941 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
943 /* FIXME: handle collates */
944 return strcmpW( str1, str2 );
947 /*********************************************************************
948 * wcspbrk (MSVCRT.@)
950 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
952 const MSVCRT_wchar_t* p;
953 while (*str)
955 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
956 str++;
958 return NULL;
961 /*********************************************************************
962 * wcstok (MSVCRT.@)
964 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
966 thread_data_t *data = msvcrt_get_thread_data();
967 MSVCRT_wchar_t *ret;
969 if (!str)
970 if (!(str = data->wcstok_next)) return NULL;
972 while (*str && strchrW( delim, *str )) str++;
973 if (!*str) return NULL;
974 ret = str++;
975 while (*str && !strchrW( delim, *str )) str++;
976 if (*str) *str++ = 0;
977 data->wcstok_next = str;
978 return ret;
982 /*********************************************************************
983 * wctomb (MSVCRT.@)
985 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
987 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
990 /*********************************************************************
991 * iswalnum (MSVCRT.@)
993 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
995 return isalnumW( wc );
998 /*********************************************************************
999 * iswalpha (MSVCRT.@)
1001 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1003 return isalphaW( wc );
1006 /*********************************************************************
1007 * iswcntrl (MSVCRT.@)
1009 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1011 return iscntrlW( wc );
1014 /*********************************************************************
1015 * iswdigit (MSVCRT.@)
1017 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1019 return isdigitW( wc );
1022 /*********************************************************************
1023 * iswgraph (MSVCRT.@)
1025 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1027 return isgraphW( wc );
1030 /*********************************************************************
1031 * iswlower (MSVCRT.@)
1033 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1035 return islowerW( wc );
1038 /*********************************************************************
1039 * iswprint (MSVCRT.@)
1041 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1043 return isprintW( wc );
1046 /*********************************************************************
1047 * iswpunct (MSVCRT.@)
1049 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1051 return ispunctW( wc );
1054 /*********************************************************************
1055 * iswspace (MSVCRT.@)
1057 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1059 return isspaceW( wc );
1062 /*********************************************************************
1063 * iswupper (MSVCRT.@)
1065 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1067 return isupperW( wc );
1070 /*********************************************************************
1071 * iswxdigit (MSVCRT.@)
1073 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1075 return isxdigitW( wc );
1078 /*********************************************************************
1079 * wcscpy_s (MSVCRT.@)
1081 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1083 MSVCRT_size_t size = 0;
1085 if(!wcDest || !numElement)
1086 return MSVCRT_EINVAL;
1088 wcDest[0] = 0;
1090 if(!wcSrc)
1092 return MSVCRT_EINVAL;
1095 size = strlenW(wcSrc) + 1;
1097 if(size > numElement)
1099 return MSVCRT_ERANGE;
1102 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1104 return 0;
1107 /******************************************************************
1108 * wcsncpy_s (MSVCRT.@)
1110 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1111 MSVCRT_size_t count )
1113 MSVCRT_size_t size = 0;
1115 if (!wcDest || !numElement)
1116 return MSVCRT_EINVAL;
1118 wcDest[0] = 0;
1120 if (!wcSrc)
1122 return MSVCRT_EINVAL;
1125 size = min(strlenW(wcSrc), count);
1127 if (size >= numElement)
1129 return MSVCRT_ERANGE;
1132 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1133 wcDest[size] = '\0';
1135 return 0;
1138 /******************************************************************
1139 * wcscat_s (MSVCRT.@)
1142 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1144 MSVCRT_wchar_t* ptr = dst;
1146 if (!dst || elem == 0) return MSVCRT_EINVAL;
1147 if (!src)
1149 dst[0] = '\0';
1150 return MSVCRT_EINVAL;
1153 /* seek to end of dst string (or elem if no end of string is found */
1154 while (ptr < dst + elem && *ptr != '\0') ptr++;
1155 while (ptr < dst + elem)
1157 if ((*ptr++ = *src++) == '\0') return 0;
1159 /* not enough space */
1160 dst[0] = '\0';
1161 return MSVCRT_ERANGE;