[CMAKE]
[reactos.git] / lib / sdk / crt / string / wcs.c
1 /*
2 * PROJECT: ReactOS CRT library
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: lib/sdk/crt/string/wcs.c
5 * PURPOSE: wcs* CRT functions
6 * PROGRAMMERS: Wine team
7 * Ported to ReactOS by Aleksey Bragin (aleksey@reactos.org)
8 */
9
10 /*
11 * msvcrt.dll wide-char functions
12 *
13 * Copyright 1999 Alexandre Julliard
14 * Copyright 2000 Jon Griffiths
15 *
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 */
30 #include <precomp.h>
31 #include <assert.h>
32
33 #ifndef _LIBCNT_
34 #include <internal/wine/msvcrt.h>
35 #endif
36
37 #include "wine/unicode.h"
38 //#include "wine/debug.h"
39
40
41 //WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
42
43 #undef sprintf
44 #undef wsprintf
45 #undef snprintf
46 #undef vsnprintf
47 #undef vprintf
48 #undef vwprintf
49
50 #ifdef _MSC_VER
51 #pragma function(_wcsset)
52 #endif
53
54 #ifndef _LIBCNT_
55 /*********************************************************************
56 * _wcsdup (MSVCRT.@)
57 */
58 wchar_t* CDECL _wcsdup( const wchar_t* str )
59 {
60 wchar_t* ret = NULL;
61 if (str)
62 {
63 int size = (strlenW(str) + 1) * sizeof(wchar_t);
64 ret = malloc( size );
65 if (ret) memcpy( ret, str, size );
66 }
67 return ret;
68 }
69 /*********************************************************************
70 * _wcsicoll (MSVCRT.@)
71 */
72 INT CDECL _wcsicoll( const wchar_t* str1, const wchar_t* str2 )
73 {
74 /* FIXME: handle collates */
75 return strcmpiW( str1, str2 );
76 }
77 #endif
78
79 /*********************************************************************
80 * _wcsnset (MSVCRT.@)
81 */
82 wchar_t* CDECL _wcsnset( wchar_t* str, wchar_t c, size_t n )
83 {
84 wchar_t* ret = str;
85 while ((n-- > 0) && *str) *str++ = c;
86 return ret;
87 }
88
89 /*********************************************************************
90 * _wcsrev (MSVCRT.@)
91 */
92 wchar_t* CDECL _wcsrev( wchar_t* str )
93 {
94 wchar_t* ret = str;
95 wchar_t* end = str + strlenW(str) - 1;
96 while (end > str)
97 {
98 wchar_t t = *end;
99 *end-- = *str;
100 *str++ = t;
101 }
102 return ret;
103 }
104
105 #ifndef _LIBCNT_
106 /*********************************************************************
107 * _wcsset (MSVCRT.@)
108 */
109 wchar_t* CDECL _wcsset( wchar_t* str, wchar_t c )
110 {
111 wchar_t* ret = str;
112 while (*str) *str++ = c;
113 return ret;
114 }
115
116 /******************************************************************
117 * _wcsupr_s (MSVCRT.@)
118 *
119 */
120 INT CDECL _wcsupr_s( wchar_t* str, size_t n )
121 {
122 wchar_t* ptr = str;
123
124 if (!str || !n)
125 {
126 if (str) *str = '\0';
127 __set_errno(EINVAL);
128 return EINVAL;
129 }
130
131 while (n--)
132 {
133 if (!*ptr) return 0;
134 *ptr = toupperW(*ptr);
135 ptr++;
136 }
137
138 /* MSDN claims that the function should return and set errno to
139 * ERANGE, which doesn't seem to be true based on the tests. */
140 *str = '\0';
141 __set_errno(EINVAL);
142 return EINVAL;
143 }
144
145 /*********************************************************************
146 * wcstod (MSVCRT.@)
147 */
148 double CDECL wcstod(const wchar_t* lpszStr, wchar_t** end)
149 {
150 const wchar_t* str = lpszStr;
151 int negative = 0;
152 double ret = 0, divisor = 10.0;
153
154 TRACE("(%s,%p) semi-stub\n", debugstr_w(lpszStr), end);
155
156 /* FIXME:
157 * - Should set errno on failure
158 * - Should fail on overflow
159 * - Need to check which input formats are allowed
160 */
161 while (isspaceW(*str))
162 str++;
163
164 if (*str == '-')
165 {
166 negative = 1;
167 str++;
168 }
169
170 while (isdigitW(*str))
171 {
172 ret = ret * 10.0 + (*str - '0');
173 str++;
174 }
175 if (*str == '.')
176 str++;
177 while (isdigitW(*str))
178 {
179 ret = ret + (*str - '0') / divisor;
180 divisor *= 10;
181 str++;
182 }
183
184 if (*str == 'E' || *str == 'e' || *str == 'D' || *str == 'd')
185 {
186 int negativeExponent = 0;
187 int exponent = 0;
188 if (*(++str) == '-')
189 {
190 negativeExponent = 1;
191 str++;
192 }
193 while (isdigitW(*str))
194 {
195 exponent = exponent * 10 + (*str - '0');
196 str++;
197 }
198 if (exponent != 0)
199 {
200 if (negativeExponent)
201 ret = ret / pow(10.0, exponent);
202 else
203 ret = ret * pow(10.0, exponent);
204 }
205 }
206
207 if (negative)
208 ret = -ret;
209
210 if (end)
211 *end = (wchar_t*)str;
212
213 TRACE("returning %g\n", ret);
214 return ret;
215 }
216
217 typedef struct pf_output_t
218 {
219 int used;
220 int len;
221 BOOL unicode;
222 union {
223 LPWSTR W;
224 LPSTR A;
225 } buf;
226 } pf_output;
227
228 typedef struct pf_flags_t
229 {
230 char Sign, LeftAlign, Alternate, PadZero;
231 int FieldLength, Precision;
232 char IntegerLength, IntegerDouble;
233 char WideString;
234 char Format;
235 } pf_flags;
236
237 /*
238 * writes a string of characters to the output
239 * returns -1 if the string doesn't fit in the output buffer
240 * return the length of the string if all characters were written
241 */
242 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
243 {
244 int space = out->len - out->used;
245
246 if( len < 0 )
247 len = strlenW( str );
248 if( out->unicode )
249 {
250 LPWSTR p = out->buf.W + out->used;
251
252 if( space >= len )
253 {
254 memcpy( p, str, len*sizeof(WCHAR) );
255 out->used += len;
256 return len;
257 }
258 if( space > 0 )
259 memcpy( p, str, space*sizeof(WCHAR) );
260 out->used += len;
261 }
262 else
263 {
264 int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
265 LPSTR p = out->buf.A + out->used;
266
267 if( space >= n )
268 {
269 WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
270 out->used += n;
271 return len;
272 }
273 if( space > 0 )
274 WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
275 out->used += n;
276 }
277 return -1;
278 }
279
280 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
281 {
282 int space = out->len - out->used;
283
284 if( len < 0 )
285 len = strlen( str );
286 if( !out->unicode )
287 {
288 LPSTR p = out->buf.A + out->used;
289
290 if( space >= len )
291 {
292 memcpy( p, str, len );
293 out->used += len;
294 return len;
295 }
296 if( space > 0 )
297 memcpy( p, str, space );
298 out->used += len;
299 }
300 else
301 {
302 int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
303 LPWSTR p = out->buf.W + out->used;
304
305 if( space >= n )
306 {
307 MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
308 out->used += n;
309 return len;
310 }
311 if( space > 0 )
312 MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
313 out->used += n;
314 }
315 return -1;
316 }
317
318 /* pf_fill: takes care of signs, alignment, zero and field padding */
319 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
320 {
321 int i, r = 0;
322
323 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
324 flags->Sign = 0;
325
326 if( left && flags->Sign )
327 {
328 flags->FieldLength--;
329 if( flags->PadZero )
330 r = pf_output_stringA( out, &flags->Sign, 1 );
331 }
332
333 if( ( !left && flags->LeftAlign ) ||
334 ( left && !flags->LeftAlign ))
335 {
336 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
337 {
338 if( left && flags->PadZero )
339 r = pf_output_stringA( out, "0", 1 );
340 else
341 r = pf_output_stringA( out, " ", 1 );
342 }
343 }
344
345 if( left && flags->Sign && !flags->PadZero )
346 r = pf_output_stringA( out, &flags->Sign, 1 );
347
348 return r;
349 }
350
351 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
352 int len, pf_flags *flags )
353 {
354 int r = 0;
355
356 if( len < 0 )
357 len = strlenW( str );
358
359 if (flags->Precision >= 0 && flags->Precision < len)
360 len = flags->Precision;
361
362 r = pf_fill( out, len, flags, 1 );
363
364 if( r>=0 )
365 r = pf_output_stringW( out, str, len );
366
367 if( r>=0 )
368 r = pf_fill( out, len, flags, 0 );
369
370 return r;
371 }
372
373 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
374 int len, pf_flags *flags )
375 {
376 int r = 0;
377
378 if( len < 0 )
379 len = strlen( str );
380
381 if (flags->Precision >= 0 && flags->Precision < len)
382 len = flags->Precision;
383
384 r = pf_fill( out, len, flags, 1 );
385
386 if( r>=0 )
387 r = pf_output_stringA( out, str, len );
388
389 if( r>=0 )
390 r = pf_fill( out, len, flags, 0 );
391
392 return r;
393 }
394
395 #ifndef USE_NEW_SPRINTF
396 static int pf_handle_string_format( pf_output *out, const void* str, int len,
397 pf_flags *flags, BOOL capital_letter)
398 {
399 if(str == NULL) /* catch NULL pointer */
400 return pf_output_format_A( out, "(null)", -1, flags);
401
402 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
403 if(flags->WideString || flags->IntegerLength == 'l')
404 return pf_output_format_W( out, str, len, flags);
405 if(flags->IntegerLength == 'h')
406 return pf_output_format_A( out, str, len, flags);
407
408 /* %s,%c -> chars in ansi functions & wchars in unicode
409 * %S,%C -> wchars in ansi functions & chars in unicode */
410 if( capital_letter == out->unicode) /* either both TRUE or both FALSE */
411 return pf_output_format_A( out, str, len, flags);
412 else
413 return pf_output_format_W( out, str, len, flags);
414 }
415
416 static inline BOOL pf_is_integer_format( char fmt )
417 {
418 static const char float_fmts[] = "diouxX";
419 if (!fmt)
420 return FALSE;
421 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
422 }
423
424 static inline BOOL pf_is_double_format( char fmt )
425 {
426 static const char float_fmts[] = "aeEfgG";
427 if (!fmt)
428 return FALSE;
429 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
430 }
431
432 static inline BOOL pf_is_valid_format( char fmt )
433 {
434 static const char float_fmts[] = "acCdeEfgGinouxX";
435 if (!fmt)
436 return FALSE;
437 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
438 }
439
440 static void pf_rebuild_format_string( char *p, pf_flags *flags )
441 {
442 *p++ = '%';
443 if( flags->Sign )
444 *p++ = flags->Sign;
445 if( flags->LeftAlign )
446 *p++ = flags->LeftAlign;
447 if( flags->Alternate )
448 *p++ = flags->Alternate;
449 if( flags->PadZero )
450 *p++ = flags->PadZero;
451 if( flags->FieldLength )
452 {
453 lnx_sprintf(p, "%d", flags->FieldLength);
454 p += strlen(p);
455 }
456 if( flags->Precision >= 0 )
457 {
458 lnx_sprintf(p, ".%d", flags->Precision);
459 p += strlen(p);
460 }
461 *p++ = flags->Format;
462 *p++ = 0;
463 }
464
465 /* pf_integer_conv: prints x to buf, including alternate formats and
466 additional precision digits, but not field characters or the sign */
467 static void pf_integer_conv( char *buf, unsigned int buf_len, pf_flags *flags,
468 LONGLONG x )
469 {
470 unsigned int base;
471 const char *digits;
472
473 int i, j, k;
474 char number[40], *tmp = number;
475
476 if( buf_len > sizeof number )
477 tmp = HeapAlloc( GetProcessHeap(), 0, buf_len );
478
479 base = 10;
480 if( flags->Format == 'o' )
481 base = 8;
482 else if( flags->Format == 'x' || flags->Format == 'X' )
483 base = 16;
484
485 if( flags->Format == 'X' )
486 digits = "0123456789ABCDEFX";
487 else
488 digits = "0123456789abcdefx";
489
490 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
491 {
492 x = -x;
493 flags->Sign = '-';
494 }
495
496 /* Do conversion (backwards) */
497 i = 0;
498 if( x == 0 && flags->Precision )
499 tmp[i++] = '0';
500 else
501 while( x != 0 )
502 {
503 j = (ULONGLONG) x % base;
504 x = (ULONGLONG) x / base;
505 tmp[i++] = digits[j];
506 }
507 k = flags->Precision - i;
508 while( k-- > 0 )
509 tmp[i++] = '0';
510 if( flags->Alternate )
511 {
512 if( base == 16 )
513 {
514 tmp[i++] = digits[16];
515 tmp[i++] = '0';
516 }
517 else if( base == 8 && tmp[i-1] != '0' )
518 tmp[i++] = '0';
519 }
520
521 /* Reverse for buf */
522 j = 0;
523 while( i-- > 0 )
524 buf[j++] = tmp[i];
525 buf[j] = '\0';
526
527 /* Adjust precision so pf_fill won't truncate the number later */
528 flags->Precision = strlen( buf );
529
530 if( tmp != number )
531 HeapFree( GetProcessHeap(), 0, tmp );
532
533 return;
534 }
535
536 /*********************************************************************
537 * pf_vsnprintf (INTERNAL)
538 *
539 * implements both A and W vsnprintf functions
540 */
541 static int pf_vsnprintf( pf_output *out, const WCHAR *format, va_list valist )
542 {
543 int r;
544 LPCWSTR q, p = format;
545 pf_flags flags;
546
547 TRACE("format is %s\n",debugstr_w(format));
548 while (*p)
549 {
550 q = strchrW( p, '%' );
551
552 /* there's no % characters left, output the rest of the string */
553 if( !q )
554 {
555 r = pf_output_stringW(out, p, -1);
556 if( r<0 )
557 return r;
558 p += r;
559 continue;
560 }
561
562 /* there's characters before the %, output them */
563 if( q != p )
564 {
565 r = pf_output_stringW(out, p, q - p);
566 if( r<0 )
567 return r;
568 p = q;
569 }
570
571 /* we must be at a % now, skip over it */
572 assert( *p == '%' );
573 p++;
574
575 /* output a single % character */
576 if( *p == '%' )
577 {
578 r = pf_output_stringW(out, p++, 1);
579 if( r<0 )
580 return r;
581 continue;
582 }
583
584 /* parse the flags */
585 memset( &flags, 0, sizeof flags );
586 while (*p)
587 {
588 if( *p == '+' || *p == ' ' )
589 {
590 if ( flags.Sign != '+' )
591 flags.Sign = *p;
592 }
593 else if( *p == '-' )
594 flags.LeftAlign = *p;
595 else if( *p == '0' )
596 flags.PadZero = *p;
597 else if( *p == '#' )
598 flags.Alternate = *p;
599 else
600 break;
601 p++;
602 }
603
604 /* deal with the field width specifier */
605 flags.FieldLength = 0;
606 if( *p == '*' )
607 {
608 flags.FieldLength = va_arg( valist, int );
609 if (flags.FieldLength < 0)
610 {
611 flags.LeftAlign = '-';
612 flags.FieldLength = -flags.FieldLength;
613 }
614 p++;
615 }
616 else while( isdigit(*p) )
617 {
618 flags.FieldLength *= 10;
619 flags.FieldLength += *p++ - '0';
620 }
621
622 /* deal with precision */
623 flags.Precision = -1;
624 if( *p == '.' )
625 {
626 flags.Precision = 0;
627 p++;
628 if( *p == '*' )
629 {
630 flags.Precision = va_arg( valist, int );
631 p++;
632 }
633 else while( isdigit(*p) )
634 {
635 flags.Precision *= 10;
636 flags.Precision += *p++ - '0';
637 }
638 }
639
640 /* deal with integer width modifier */
641 while( *p )
642 {
643 if( *p == 'h' || *p == 'l' || *p == 'L' )
644 {
645 flags.IntegerLength = *p;
646 p++;
647 }
648 else if( *p == 'I' )
649 {
650 if( *(p+1) == '6' && *(p+2) == '4' )
651 {
652 flags.IntegerDouble++;
653 p += 3;
654 }
655 else if( *(p+1) == '3' && *(p+2) == '2' )
656 p += 3;
657 else if( isdigit(*(p+1)) || *(p+1) == 0 )
658 break;
659 else
660 p++;
661 }
662 else if( *p == 'w' )
663 flags.WideString = *p++;
664 else if( *p == 'F' )
665 p++; /* ignore */
666 else
667 break;
668 }
669
670 flags.Format = *p;
671 r = 0;
672
673 /* output a string */
674 if( flags.Format == 's' || flags.Format == 'S' )
675 r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
676 &flags, (flags.Format == 'S') );
677
678 /* output a single character */
679 else if( flags.Format == 'c' || flags.Format == 'C' )
680 {
681 INT ch = va_arg( valist, int );
682
683 r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
684 }
685
686 /* output a pointer */
687 else if( flags.Format == 'p' )
688 {
689 char pointer[11];
690
691 flags.PadZero = 0;
692 if( flags.Alternate )
693 lnx_sprintf(pointer, "0X%08lX", va_arg(valist, long));
694 else
695 lnx_sprintf(pointer, "%08lX", va_arg(valist, long));
696 r = pf_output_format_A( out, pointer, -1, &flags );
697 }
698
699 /* deal with %n */
700 else if( flags.Format == 'n' )
701 {
702 int *x = va_arg(valist, int *);
703 *x = out->used;
704 }
705
706 /* deal with 64-bit integers */
707 else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
708 {
709 char number[40], *x = number;
710
711 /* Estimate largest possible required buffer size:
712 * Chooses the larger of the field or precision
713 * Includes extra bytes: 1 byte for null, 1 byte for sign,
714 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
715 for a decimal, and 1 byte for an additional float digit. */
716 unsigned x_len = ((flags.FieldLength > flags.Precision) ?
717 flags.FieldLength : flags.Precision) + 10;
718
719 if( x_len >= sizeof number)
720 x = HeapAlloc( GetProcessHeap(), 0, x_len );
721
722 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
723
724 r = pf_output_format_A( out, x, -1, &flags );
725 if( x != number )
726 HeapFree( GetProcessHeap(), 0, x );
727 }
728
729 /* deal with integers and floats using libc's printf */
730 else if( pf_is_valid_format( flags.Format ) )
731 {
732 char fmt[20], number[40], *x = number;
733
734 /* Estimate largest possible required buffer size:
735 * Chooses the larger of the field or precision
736 * Includes extra bytes: 1 byte for null, 1 byte for sign,
737 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
738 for a decimal, and 1 byte for an additional float digit. */
739 unsigned x_len = ((flags.FieldLength > flags.Precision) ?
740 flags.FieldLength : flags.Precision) + 10;
741
742 if( x_len >= sizeof number)
743 x = HeapAlloc( GetProcessHeap(), 0, x_len );
744
745 pf_rebuild_format_string( fmt, &flags );
746
747 if( pf_is_double_format( flags.Format ) )
748 lnx_sprintf( x, fmt, va_arg(valist, double) );
749 else
750 lnx_sprintf( x, fmt, va_arg(valist, int) );
751
752 r = pf_output_stringA( out, x, -1 );
753 if( x != number )
754 HeapFree( GetProcessHeap(), 0, x );
755 }
756 else
757 continue;
758
759 if( r<0 )
760 return r;
761 p++;
762 }
763
764 /* check we reached the end, and null terminate the string */
765 assert( *p == 0 );
766 pf_output_stringW( out, p, 1 );
767
768 return out->used - 1;
769 }
770
771 /*********************************************************************
772 * _vsnprintf (MSVCRT.@)
773 */
774 int CDECL _vsnprintf( char *str, size_t len,
775 const char *format, va_list valist )
776 {
777 DWORD sz;
778 LPWSTR formatW = NULL;
779 pf_output out;
780 int r;
781
782 out.unicode = FALSE;
783 out.buf.A = str;
784 out.used = 0;
785 out.len = len;
786
787 if( format )
788 {
789 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
790 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
791 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
792 }
793
794 r = pf_vsnprintf( &out, formatW, valist );
795
796 HeapFree( GetProcessHeap(), 0, formatW );
797
798 return r;
799 }
800
801 /*********************************************************************
802 * vsprintf (MSVCRT.@)
803 */
804 int CDECL vsprintf( char *str, const char *format, va_list valist)
805 {
806 return _vsnprintf(str, INT_MAX, format, valist);
807 }
808
809 /*********************************************************************
810 * _snprintf (MSVCRT.@)
811 */
812 int CDECL _snprintf(char *str, size_t len, const char *format, ...)
813 {
814 int retval;
815 va_list valist;
816 va_start(valist, format);
817 retval = _vsnprintf(str, len, format, valist);
818 va_end(valist);
819 return retval;
820 }
821
822 /*********************************************************************
823 * _vsnwsprintf (MSVCRT.@)
824 */
825 int CDECL _vsnwprintf( wchar_t *str, size_t len,
826 const wchar_t *format, va_list valist )
827 {
828 pf_output out;
829
830 out.unicode = TRUE;
831 out.buf.W = str;
832 out.used = 0;
833 out.len = len;
834
835 return pf_vsnprintf( &out, format, valist );
836 }
837
838 /*********************************************************************
839 * _snwprintf (MSVCRT.@)
840 */
841 int CDECL _snwprintf( wchar_t *str, size_t len, const wchar_t *format, ...)
842 {
843 int retval;
844 va_list valist;
845 va_start(valist, format);
846 retval = _vsnwprintf(str, len, format, valist);
847 va_end(valist);
848 return retval;
849 }
850
851 /*********************************************************************
852 * sprintf (MSVCRT.@)
853 */
854 int CDECL sprintf( char *str, const char *format, ... )
855 {
856 va_list ap;
857 int r;
858
859 va_start( ap, format );
860 r = _vsnprintf( str, INT_MAX, format, ap );
861 va_end( ap );
862 return r;
863 }
864
865 /*********************************************************************
866 * swprintf (MSVCRT.@)
867 */
868 int CDECL swprintf( wchar_t *str, const wchar_t *format, ... )
869 {
870 va_list ap;
871 int r;
872
873 va_start( ap, format );
874 r = _vsnwprintf( str, INT_MAX, format, ap );
875 va_end( ap );
876 return r;
877 }
878
879 /*********************************************************************
880 * vswprintf (MSVCRT.@)
881 */
882 int CDECL vswprintf( wchar_t* str, const wchar_t* format, va_list args )
883 {
884 return _vsnwprintf( str, INT_MAX, format, args );
885 }
886 #endif
887 #endif
888
889 /*********************************************************************
890 * wcscoll (MSVCRT.@)
891 */
892 int CDECL wcscoll( const wchar_t* str1, const wchar_t* str2 )
893 {
894 /* FIXME: handle collates */
895 return strcmpW( str1, str2 );
896 }
897
898 /*********************************************************************
899 * wcspbrk (MSVCRT.@)
900 */
901 wchar_t* CDECL wcspbrk( const wchar_t* str, const wchar_t* accept )
902 {
903 const wchar_t* p;
904 while (*str)
905 {
906 for (p = accept; *p; p++) if (*p == *str) return (wchar_t*)str;
907 str++;
908 }
909 return NULL;
910 }
911
912 #ifndef _LIBCNT_
913 /*********************************************************************
914 * wcstok (MSVCRT.@)
915 */
916 wchar_t * CDECL wcstok( wchar_t *str, const wchar_t *delim )
917 {
918 MSVCRT_thread_data *data = msvcrt_get_thread_data();
919 wchar_t *ret;
920
921 if (!str)
922 if (!(str = data->wcstok_next)) return NULL;
923
924 while (*str && strchrW( delim, *str )) str++;
925 if (!*str) return NULL;
926 ret = str++;
927 while (*str && !strchrW( delim, *str )) str++;
928 if (*str) *str++ = 0;
929 data->wcstok_next = str;
930 return ret;
931 }
932
933 /*********************************************************************
934 * wctomb (MSVCRT.@)
935 */
936 INT CDECL wctomb(char *mbchar, wchar_t wchar)
937 {
938 BOOL bUsedDefaultChar;
939 char chMultiByte[MB_LEN_MAX];
940 int nBytes;
941
942 /* At least one parameter needs to be given, the length of a null character cannot be queried (verified by tests under WinXP SP2) */
943 if(!mbchar && !wchar)
944 return 0;
945
946 /* Use WideCharToMultiByte for doing the conversion using the codepage currently set with setlocale() */
947 nBytes = WideCharToMultiByte(MSVCRT___lc_codepage, 0, &wchar, 1, chMultiByte, MB_LEN_MAX, NULL, &bUsedDefaultChar);
948
949 /* Only copy the character if an 'mbchar' pointer was given.
950
951 The "C" locale is emulated with codepage 1252 here. This codepage has a default character "?", but the "C" locale doesn't have one.
952 Therefore don't copy the character in this case. */
953 if(mbchar && !(MSVCRT_current_lc_all[0] == 'C' && !MSVCRT_current_lc_all[1] && bUsedDefaultChar))
954 memcpy(mbchar, chMultiByte, nBytes);
955
956 /* If the default character was used, set errno to EILSEQ and return -1. */
957 if(bUsedDefaultChar)
958 {
959 __set_errno(EILSEQ);
960 return -1;
961 }
962
963 /* Otherwise return the number of bytes this character occupies. */
964 return nBytes;
965 }
966
967 size_t CDECL wcstombs(char *mbstr, const wchar_t *wcstr, size_t count)
968 {
969 BOOL bUsedDefaultChar;
970 char* p = mbstr;
971 int nResult;
972
973 /* Does the caller query for output buffer size? */
974 if(!mbstr)
975 {
976 int nLength;
977
978 /* If we currently use the "C" locale, the length of the input string is returned (verified by tests under WinXP SP2) */
979 if(MSVCRT_current_lc_all[0] == 'C' && !MSVCRT_current_lc_all[1])
980 return wcslen(wcstr);
981
982 /* Otherwise check the length each character needs and build a final return value out of this */
983 count = wcslen(wcstr);
984 nLength = 0;
985
986 while((int)(--count) >= 0 && *wcstr)
987 {
988 /* Get the length of this character */
989 nResult = wctomb(NULL, *wcstr++);
990
991 /* If this character is not convertible in the current locale, the end result will be -1 */
992 if(nResult == -1)
993 return -1;
994
995 nLength += nResult;
996 }
997
998 /* Return the final length */
999 return nLength;
1000 }
1001
1002 /* Convert the string then */
1003 bUsedDefaultChar = FALSE;
1004
1005 for(;;)
1006 {
1007 char chMultiByte[MB_LEN_MAX];
1008 UINT uLength;
1009
1010 /* Are we at the terminating null character? */
1011 if(!*wcstr)
1012 {
1013 /* Set the null character, but don't increment the pointer as the returned length never includes the terminating null character */
1014 *p = 0;
1015 break;
1016 }
1017
1018 /* Convert this character into the temporary chMultiByte variable */
1019 ZeroMemory(chMultiByte, MB_LEN_MAX);
1020 nResult = wctomb(chMultiByte, *wcstr++);
1021
1022 /* Check if this was an invalid character */
1023 if(nResult == -1)
1024 bUsedDefaultChar = TRUE;
1025
1026 /* If we got no character, stop the conversion process here */
1027 if(!chMultiByte[0])
1028 break;
1029
1030 /* Determine whether this is a double-byte or a single-byte character */
1031 if(chMultiByte[1])
1032 uLength = 2;
1033 else
1034 uLength = 1;
1035
1036 /* Decrease 'count' by the character length and check if the buffer can still hold the full character */
1037 count -= uLength;
1038
1039 if((int)count < 0)
1040 break;
1041
1042 /* It can, so copy it and move the pointer forward */
1043 memcpy(p, chMultiByte, uLength);
1044 p += uLength;
1045 }
1046
1047 if(bUsedDefaultChar)
1048 return -1;
1049
1050 /* Return the length in bytes of the copied characters (without the terminating null character) */
1051 return p - mbstr;
1052 }
1053 #endif
1054
1055 #ifndef __REACTOS__
1056 /*********************************************************************
1057 * iswalnum (MSVCRT.@)
1058 */
1059 INT CDECL iswalnum( wchar_t wc )
1060 {
1061 return isalnumW( wc );
1062 }
1063
1064 /*********************************************************************
1065 * iswalpha (MSVCRT.@)
1066 */
1067 INT CDECL iswalpha( wchar_t wc )
1068 {
1069 return isalphaW( wc );
1070 }
1071
1072 /*********************************************************************
1073 * iswcntrl (MSVCRT.@)
1074 */
1075 INT CDECL iswcntrl( wchar_t wc )
1076 {
1077 return iscntrlW( wc );
1078 }
1079
1080 /*********************************************************************
1081 * iswdigit (MSVCRT.@)
1082 */
1083 INT CDECL iswdigit( wchar_t wc )
1084 {
1085 return isdigitW( wc );
1086 }
1087
1088 /*********************************************************************
1089 * iswgraph (MSVCRT.@)
1090 */
1091 INT CDECL iswgraph( wchar_t wc )
1092 {
1093 return isgraphW( wc );
1094 }
1095
1096 /*********************************************************************
1097 * iswlower (MSVCRT.@)
1098 */
1099 INT CDECL iswlower( wchar_t wc )
1100 {
1101 return islowerW( wc );
1102 }
1103
1104 /*********************************************************************
1105 * iswprint (MSVCRT.@)
1106 */
1107 INT CDECL iswprint( wchar_t wc )
1108 {
1109 return isprintW( wc );
1110 }
1111
1112 /*********************************************************************
1113 * iswpunct (MSVCRT.@)
1114 */
1115 INT CDECL iswpunct( wchar_t wc )
1116 {
1117 return ispunctW( wc );
1118 }
1119
1120 /*********************************************************************
1121 * iswspace (MSVCRT.@)
1122 */
1123 INT CDECL iswspace( wchar_t wc )
1124 {
1125 return isspaceW( wc );
1126 }
1127
1128 /*********************************************************************
1129 * iswupper (MSVCRT.@)
1130 */
1131 INT CDECL iswupper( wchar_t wc )
1132 {
1133 return isupperW( wc );
1134 }
1135
1136 /*********************************************************************
1137 * iswxdigit (MSVCRT.@)
1138 */
1139 INT CDECL iswxdigit( wchar_t wc )
1140 {
1141 return isxdigitW( wc );
1142 }
1143
1144 #endif
1145
1146 /*********************************************************************
1147 * wcscpy_s (MSVCRT.@)
1148 */
1149 INT CDECL wcscpy_s( wchar_t* wcDest, size_t numElement, const wchar_t *wcSrc)
1150 {
1151 INT size = 0;
1152
1153 if(!wcDest || !numElement)
1154 return EINVAL;
1155
1156 wcDest[0] = 0;
1157
1158 if(!wcSrc)
1159 {
1160 return EINVAL;
1161 }
1162
1163 size = strlenW(wcSrc) + 1;
1164
1165 if(size > numElement)
1166 {
1167 return ERANGE;
1168 }
1169
1170 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1171
1172 return 0;
1173 }
1174
1175 /******************************************************************
1176 * wcsncpy_s (MSVCRT.@)
1177 */
1178 INT CDECL wcsncpy_s( wchar_t* wcDest, size_t numElement, const wchar_t *wcSrc,
1179 size_t count )
1180 {
1181 size_t size = 0;
1182
1183 if (!wcDest || !numElement)
1184 return EINVAL;
1185
1186 wcDest[0] = 0;
1187
1188 if (!wcSrc)
1189 {
1190 return EINVAL;
1191 }
1192
1193 size = min(strlenW(wcSrc), count);
1194
1195 if (size >= numElement)
1196 {
1197 return ERANGE;
1198 }
1199
1200 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1201 wcDest[size] = '\0';
1202
1203 return 0;
1204 }
1205
1206