shell32: Add printers CLSID to test, clean up a bit.
[wine/testsucceed.git] / dlls / msvcrt / wcs.c
blobdf0ac03db270e35e375c490a79ccbf2378e5a2d0
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 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;
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,
558 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
560 int r;
561 LPCWSTR q, p = format;
562 pf_flags flags;
564 if(!locale)
565 locale = get_locale();
567 TRACE("format is %s\n",debugstr_w(format));
568 while (*p)
570 q = strchrW( p, '%' );
572 /* there's no % characters left, output the rest of the string */
573 if( !q )
575 r = pf_output_stringW(out, p, -1);
576 if( r<0 )
577 return r;
578 p += r;
579 continue;
582 /* there's characters before the %, output them */
583 if( q != p )
585 r = pf_output_stringW(out, p, q - p);
586 if( r<0 )
587 return r;
588 p = q;
591 /* we must be at a % now, skip over it */
592 assert( *p == '%' );
593 p++;
595 /* output a single % character */
596 if( *p == '%' )
598 r = pf_output_stringW(out, p++, 1);
599 if( r<0 )
600 return r;
601 continue;
604 /* parse the flags */
605 memset( &flags, 0, sizeof flags );
606 while (*p)
608 if( *p == '+' || *p == ' ' )
610 if ( flags.Sign != '+' )
611 flags.Sign = *p;
613 else if( *p == '-' )
614 flags.LeftAlign = *p;
615 else if( *p == '0' )
616 flags.PadZero = *p;
617 else if( *p == '#' )
618 flags.Alternate = *p;
619 else
620 break;
621 p++;
624 /* deal with the field width specifier */
625 flags.FieldLength = 0;
626 if( *p == '*' )
628 flags.FieldLength = va_arg( valist, int );
629 if (flags.FieldLength < 0)
631 flags.LeftAlign = '-';
632 flags.FieldLength = -flags.FieldLength;
634 p++;
636 else while( isdigit(*p) )
638 flags.FieldLength *= 10;
639 flags.FieldLength += *p++ - '0';
642 /* deal with precision */
643 flags.Precision = -1;
644 if( *p == '.' )
646 flags.Precision = 0;
647 p++;
648 if( *p == '*' )
650 flags.Precision = va_arg( valist, int );
651 p++;
653 else while( isdigit(*p) )
655 flags.Precision *= 10;
656 flags.Precision += *p++ - '0';
660 /* deal with integer width modifier */
661 while( *p )
663 if( *p == 'h' || *p == 'l' || *p == 'L' )
665 flags.IntegerLength = *p;
666 p++;
668 else if( *p == 'I' )
670 if( *(p+1) == '6' && *(p+2) == '4' )
672 flags.IntegerDouble++;
673 p += 3;
675 else if( *(p+1) == '3' && *(p+2) == '2' )
676 p += 3;
677 else if( isdigit(*(p+1)) || *(p+1) == 0 )
678 break;
679 else
680 p++;
682 else if( *p == 'w' )
683 flags.WideString = *p++;
684 else if( *p == 'F' )
685 p++; /* ignore */
686 else
687 break;
690 flags.Format = *p;
691 r = 0;
693 if (flags.Format == '$')
695 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
696 return -1;
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' )
714 char pointer[32];
715 void *ptr = va_arg( valist, void * );
717 flags.PadZero = 0;
718 if( flags.Alternate )
719 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
720 else
721 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
722 r = pf_output_format_A( out, pointer, -1, &flags );
725 /* deal with %n */
726 else if( flags.Format == 'n' )
728 int *x = va_arg(valist, int *);
729 *x = out->used;
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 );
751 if( x != number )
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 );
779 else
780 sprintf( x, fmt, va_arg(valist, int) );
782 decimal_point = strchr(x, '.');
783 if(decimal_point)
784 *decimal_point = *locale->locinfo->lconv->decimal_point;
786 r = pf_output_stringA( out, x, -1 );
787 if( x != number )
788 HeapFree( GetProcessHeap(), 0, x );
789 if(r < 0)
790 return r;
792 else
794 if( valid )
796 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
797 *MSVCRT__errno() = MSVCRT_EINVAL;
798 return -1;
801 continue;
804 if( r<0 )
805 return r;
806 p++;
809 /* check we reached the end, and null terminate the string */
810 assert( *p == 0 );
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 )
822 DWORD sz;
823 LPWSTR formatW = NULL;
824 pf_output out;
825 int r;
827 out.unicode = FALSE;
828 out.buf.A = str;
829 out.used = 0;
830 out.len = len;
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 );
840 return r;
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 )
868 int len, ret;
870 if(sizeOfBuffer<count+1 || count==-1)
871 len = sizeOfBuffer;
872 else
873 len = 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);
882 } else
883 str[len-1] = '\0';
885 return -1;
888 return ret;
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, ...)
929 int retval;
930 __ms_va_list valist;
931 __ms_va_start(valist, format);
932 retval = MSVCRT_vsnprintf(str, len, format, valist);
933 __ms_va_end(valist);
934 return retval;
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,
942 __ms_va_list valist)
944 pf_output out;
946 out.unicode = TRUE;
947 out.buf.W = str;
948 out.used = 0;
949 out.len = len;
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,
968 __ms_va_list valist)
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)
980 int len, ret;
982 len = sizeOfBuffer/sizeof(MSVCRT_wchar_t);
983 if(count!=-1 && len>count+1)
984 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);
993 } else
994 str[len-1] = '\0';
996 return -1;
999 return ret;
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, ...)
1017 int retval;
1018 __ms_va_list valist;
1019 __ms_va_start(valist, format);
1020 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1021 __ms_va_end(valist);
1022 return retval;
1025 /*********************************************************************
1026 * sprintf (MSVCRT.@)
1028 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
1030 __ms_va_list ap;
1031 int r;
1033 __ms_va_start( ap, format );
1034 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1035 __ms_va_end( ap );
1036 return r;
1039 /*********************************************************************
1040 * sprintf_s (MSVCRT.@)
1042 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1044 __ms_va_list ap;
1045 int r;
1047 __ms_va_start( ap, format );
1048 r = MSVCRT_vsnprintf( str, num, format, ap );
1049 __ms_va_end( ap );
1050 return r;
1053 /*********************************************************************
1054 * swprintf (MSVCRT.@)
1056 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1058 __ms_va_list ap;
1059 int r;
1061 __ms_va_start( ap, format );
1062 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1063 __ms_va_end( ap );
1064 return r;
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;
1118 while (*str)
1120 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1121 str++;
1123 return NULL;
1126 /*********************************************************************
1127 * wcstok (MSVCRT.@)
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;
1134 if (!str)
1135 if (!(str = data->wcstok_next)) return NULL;
1137 while (*str && strchrW( delim, *str )) str++;
1138 if (!*str) return NULL;
1139 ret = str++;
1140 while (*str && !strchrW( delim, *str )) str++;
1141 if (*str) *str++ = 0;
1142 data->wcstok_next = str;
1143 return ret;
1147 /*********************************************************************
1148 * wctomb (MSVCRT.@)
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;
1261 wcDest[0] = 0;
1263 if(!wcSrc)
1265 return MSVCRT_EINVAL;
1268 size = strlenW(wcSrc) + 1;
1270 if(size > numElement)
1272 return MSVCRT_ERANGE;
1275 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1277 return 0;
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;
1291 wcDest[0] = 0;
1293 if (!wcSrc)
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';
1308 return 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;
1320 if (!src)
1322 dst[0] = '\0';
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 */
1333 dst[0] = '\0';
1334 return MSVCRT_ERANGE;