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 /* INTERNAL: MSVCRT_malloc() based wstrndup */
34 MSVCRT_wchar_t* msvcrt_wstrndup(const MSVCRT_wchar_t *buf, unsigned int size)
37 unsigned int len = strlenW(buf), max_len;
39 max_len = size <= len? size : len + 1;
41 ret = MSVCRT_malloc(max_len * sizeof (MSVCRT_wchar_t));
44 memcpy(ret,buf,max_len * sizeof (MSVCRT_wchar_t));
50 /*********************************************************************
53 MSVCRT_wchar_t* CDECL _wcsdup( const MSVCRT_wchar_t* str )
55 MSVCRT_wchar_t* ret = NULL;
58 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
59 ret = MSVCRT_malloc( size );
60 if (ret) memcpy( ret, str, size );
65 /*********************************************************************
66 * _wcsicoll (MSVCRT.@)
68 INT CDECL _wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
70 /* FIXME: handle collates */
71 return strcmpiW( str1, str2 );
74 /*********************************************************************
77 MSVCRT_wchar_t* CDECL _wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
79 MSVCRT_wchar_t* ret = str;
80 while ((n-- > 0) && *str) *str++ = c;
84 /*********************************************************************
87 MSVCRT_wchar_t* CDECL _wcsrev( MSVCRT_wchar_t* str )
89 MSVCRT_wchar_t* ret = str;
90 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
93 MSVCRT_wchar_t t = *end;
100 /*********************************************************************
103 MSVCRT_wchar_t* CDECL _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
105 MSVCRT_wchar_t* ret = str;
106 while (*str) *str++ = c;
110 /*********************************************************************
113 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
115 const MSVCRT_wchar_t* str = lpszStr;
117 double ret = 0, divisor = 10.0;
119 TRACE("(%s,%p) semi-stub\n", debugstr_w(lpszStr), end);
122 * - Should set errno on failure
123 * - Should fail on overflow
124 * - Need to check which input formats are allowed
126 while (isspaceW(*str))
135 while (isdigitW(*str))
137 ret = ret * 10.0 + (*str - '0');
142 while (isdigitW(*str))
144 ret = ret + (*str - '0') / divisor;
149 if (*str == 'E' || *str == 'e' || *str == 'D' || *str == 'd')
151 int negativeExponent = 0;
155 negativeExponent = 1;
158 while (isdigitW(*str))
160 exponent = exponent * 10 + (*str - '0');
165 if (negativeExponent)
166 ret = ret / pow(10.0, exponent);
168 ret = ret * pow(10.0, exponent);
176 *end = (MSVCRT_wchar_t*)str;
178 TRACE("returning %g\n", ret);
183 typedef struct pf_output_t
194 typedef struct pf_flags_t
196 char Sign, LeftAlign, Alternate, PadZero;
197 int FieldLength, Precision;
198 char IntegerLength, IntegerDouble;
204 * writes a string of characters to the output
205 * returns -1 if the string doesn't fit in the output buffer
206 * return the length of the string if all characters were written
208 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
210 int space = out->len - out->used;
213 len = strlenW( str );
216 LPWSTR p = out->buf.W + out->used;
220 memcpy( p, str, len*sizeof(WCHAR) );
225 memcpy( p, str, space*sizeof(WCHAR) );
230 int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
231 LPSTR p = out->buf.A + out->used;
235 WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
240 WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
246 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
248 int space = out->len - out->used;
254 LPSTR p = out->buf.A + out->used;
258 memcpy( p, str, len );
263 memcpy( p, str, space );
268 int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
269 LPWSTR p = out->buf.W + out->used;
273 MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
278 MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
284 /* pf_fill: takes care of signs, alignment, zero and field padding */
285 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
289 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
292 if( left && flags->Sign )
294 flags->FieldLength--;
296 r = pf_output_stringA( out, &flags->Sign, 1 );
299 if( ( !left && flags->LeftAlign ) ||
300 ( left && !flags->LeftAlign ))
302 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
304 if( left && flags->PadZero )
305 r = pf_output_stringA( out, "0", 1 );
307 r = pf_output_stringA( out, " ", 1 );
311 if( left && flags->Sign && !flags->PadZero )
312 r = pf_output_stringA( out, &flags->Sign, 1 );
317 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
318 int len, pf_flags *flags )
323 len = strlenW( str );
325 if (flags->Precision >= 0 && flags->Precision < len)
326 len = flags->Precision;
328 r = pf_fill( out, len, flags, 1 );
331 r = pf_output_stringW( out, str, len );
334 r = pf_fill( out, len, flags, 0 );
339 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
340 int len, pf_flags *flags )
347 if (flags->Precision >= 0 && flags->Precision < len)
348 len = flags->Precision;
350 r = pf_fill( out, len, flags, 1 );
353 r = pf_output_stringA( out, str, len );
356 r = pf_fill( out, len, flags, 0 );
361 static int pf_handle_string_format( pf_output *out, const void* str, int len,
362 pf_flags *flags, BOOL capital_letter)
364 if(str == NULL) /* catch NULL pointer */
365 return pf_output_format_A( out, "(null)", -1, flags);
367 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
368 if(flags->WideString || flags->IntegerLength == 'l')
369 return pf_output_format_W( out, str, len, flags);
370 if(flags->IntegerLength == 'h')
371 return pf_output_format_A( out, str, len, flags);
373 /* %s,%c -> chars in ansi functions & wchars in unicode
374 * %S,%C -> wchars in ansi functions & chars in unicode */
375 if( capital_letter == out->unicode) /* either both TRUE or both FALSE */
376 return pf_output_format_A( out, str, len, flags);
378 return pf_output_format_W( out, str, len, flags);
381 static inline BOOL pf_is_integer_format( char fmt )
383 static const char float_fmts[] = "diouxX";
386 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
389 static inline BOOL pf_is_double_format( char fmt )
391 static const char float_fmts[] = "aeEfgG";
394 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
397 static inline BOOL pf_is_valid_format( char fmt )
399 static const char float_fmts[] = "acCdeEfgGinouxX";
402 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
405 static void pf_rebuild_format_string( char *p, pf_flags *flags )
410 if( flags->LeftAlign )
411 *p++ = flags->LeftAlign;
412 if( flags->Alternate )
413 *p++ = flags->Alternate;
415 *p++ = flags->PadZero;
416 if( flags->FieldLength )
418 sprintf(p, "%d", flags->FieldLength);
421 if( flags->Precision >= 0 )
423 sprintf(p, ".%d", flags->Precision);
426 *p++ = flags->Format;
430 /* pf_integer_conv: prints x to buf, including alternate formats and
431 additional precision digits, but not field characters or the sign */
432 static void pf_integer_conv( char *buf, int buf_len, pf_flags *flags,
439 char number[40], *tmp = number;
441 if( buf_len > sizeof number )
442 tmp = HeapAlloc( GetProcessHeap(), 0, buf_len );
445 if( flags->Format == 'o' )
447 else if( flags->Format == 'x' || flags->Format == 'X' )
450 if( flags->Format == 'X' )
451 digits = "0123456789ABCDEFX";
453 digits = "0123456789abcdefx";
455 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
461 /* Do conversion (backwards) */
463 if( x == 0 && flags->Precision )
468 j = (ULONGLONG) x % base;
469 x = (ULONGLONG) x / base;
470 tmp[i++] = digits[j];
472 k = flags->Precision - i;
475 if( flags->Alternate )
479 tmp[i++] = digits[16];
482 else if( base == 8 && tmp[i-1] != '0' )
486 /* Reverse for buf */
492 /* Adjust precision so pf_fill won't truncate the number later */
493 flags->Precision = strlen( buf );
496 HeapFree( GetProcessHeap(), 0, tmp );
501 /*********************************************************************
502 * pf_vsnprintf (INTERNAL)
504 * implements both A and W vsnprintf functions
506 static int pf_vsnprintf( pf_output *out, const WCHAR *format, va_list valist )
509 LPCWSTR q, p = format;
512 TRACE("format is %s\n",debugstr_w(format));
515 q = strchrW( p, '%' );
517 /* there's no % characters left, output the rest of the string */
520 r = pf_output_stringW(out, p, -1);
527 /* there's characters before the %, output them */
530 r = pf_output_stringW(out, p, q - p);
536 /* we must be at a % now, skip over it */
540 /* output a single % character */
543 r = pf_output_stringW(out, p++, 1);
549 /* parse the flags */
550 memset( &flags, 0, sizeof flags );
553 if( *p == '+' || *p == ' ' )
555 if ( flags.Sign != '+' )
559 flags.LeftAlign = *p;
563 flags.Alternate = *p;
569 /* deal with the field width specifier */
570 flags.FieldLength = 0;
573 flags.FieldLength = va_arg( valist, int );
574 if (flags.FieldLength < 0)
576 flags.LeftAlign = '-';
577 flags.FieldLength = -flags.FieldLength;
581 else while( isdigit(*p) )
583 flags.FieldLength *= 10;
584 flags.FieldLength += *p++ - '0';
587 /* deal with precision */
588 flags.Precision = -1;
595 flags.Precision = va_arg( valist, int );
598 else while( isdigit(*p) )
600 flags.Precision *= 10;
601 flags.Precision += *p++ - '0';
605 /* deal with integer width modifier */
608 if( *p == 'h' || *p == 'l' || *p == 'L' )
610 flags.IntegerLength = *p;
615 if( *(p+1) == '6' && *(p+2) == '4' )
617 flags.IntegerDouble++;
620 else if( *(p+1) == '3' && *(p+2) == '2' )
622 else if( isdigit(*(p+1)) || *(p+1) == 0 )
628 flags.WideString = *p++;
638 /* output a string */
639 if( flags.Format == 's' || flags.Format == 'S' )
640 r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
641 &flags, (flags.Format == 'S') );
643 /* output a single character */
644 else if( flags.Format == 'c' || flags.Format == 'C' )
646 INT ch = va_arg( valist, int );
648 r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
651 /* output a pointer */
652 else if( flags.Format == 'p' )
657 if( flags.Alternate )
658 sprintf(pointer, "0X%08lX", va_arg(valist, long));
660 sprintf(pointer, "%08lX", va_arg(valist, long));
661 r = pf_output_format_A( out, pointer, -1, &flags );
665 else if( flags.Format == 'n' )
667 int *x = va_arg(valist, int *);
671 /* deal with 64-bit integers */
672 else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
674 char number[40], *x = number;
676 /* Estimate largest possible required buffer size:
677 * Chooses the larger of the field or precision
678 * Includes extra bytes: 1 byte for null, 1 byte for sign,
679 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
680 for a decimal, and 1 byte for an additional float digit. */
681 int x_len = ((flags.FieldLength > flags.Precision) ?
682 flags.FieldLength : flags.Precision) + 10;
684 if( x_len >= sizeof number)
685 x = HeapAlloc( GetProcessHeap(), 0, x_len );
687 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
689 r = pf_output_format_A( out, x, -1, &flags );
691 HeapFree( GetProcessHeap(), 0, x );
694 /* deal with integers and floats using libc's printf */
695 else if( pf_is_valid_format( flags.Format ) )
697 char fmt[20], number[40], *x = number;
699 /* Estimate largest possible required buffer size:
700 * Chooses the larger of the field or precision
701 * Includes extra bytes: 1 byte for null, 1 byte for sign,
702 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
703 for a decimal, and 1 byte for an additional float digit. */
704 int x_len = ((flags.FieldLength > flags.Precision) ?
705 flags.FieldLength : flags.Precision) + 10;
707 if( x_len >= sizeof number)
708 x = HeapAlloc( GetProcessHeap(), 0, x_len );
710 pf_rebuild_format_string( fmt, &flags );
712 if( pf_is_double_format( flags.Format ) )
713 sprintf( x, fmt, va_arg(valist, double) );
715 sprintf( x, fmt, va_arg(valist, int) );
717 r = pf_output_stringA( out, x, -1 );
719 HeapFree( GetProcessHeap(), 0, x );
729 /* check we reached the end, and null terminate the string */
731 pf_output_stringW( out, p, 1 );
733 return out->used - 1;
736 /*********************************************************************
737 * _vsnprintf (MSVCRT.@)
739 int CDECL MSVCRT_vsnprintf( char *str, unsigned int len,
740 const char *format, va_list valist )
743 LPWSTR formatW = NULL;
754 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
755 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
756 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
759 r = pf_vsnprintf( &out, formatW, valist );
761 HeapFree( GetProcessHeap(), 0, formatW );
766 /*********************************************************************
767 * vsprintf (MSVCRT.@)
769 int CDECL MSVCRT_vsprintf( char *str, const char *format, va_list valist)
771 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
774 /*********************************************************************
775 * _snprintf (MSVCRT.@)
777 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
781 va_start(valist, format);
782 retval = MSVCRT_vsnprintf(str, len, format, valist);
787 /*********************************************************************
788 * _vsnwsprintf (MSVCRT.@)
790 int CDECL MSVCRT_vsnwprintf( MSVCRT_wchar_t *str, unsigned int len,
791 const MSVCRT_wchar_t *format, va_list valist )
800 return pf_vsnprintf( &out, format, valist );
803 /*********************************************************************
804 * _snwprintf (MSVCRT.@)
806 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
810 va_start(valist, format);
811 retval = MSVCRT_vsnwprintf(str, len, format, valist);
816 /*********************************************************************
819 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
824 va_start( ap, format );
825 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
830 /*********************************************************************
831 * swprintf (MSVCRT.@)
833 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
838 va_start( ap, format );
839 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
844 /*********************************************************************
845 * vswprintf (MSVCRT.@)
847 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, va_list args )
849 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
852 /*********************************************************************
855 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
857 /* FIXME: handle collates */
858 return strcmpW( str1, str2 );
861 /*********************************************************************
864 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
866 const MSVCRT_wchar_t* p;
869 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
875 /*********************************************************************
878 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
880 thread_data_t *data = msvcrt_get_thread_data();
884 if (!(str = data->wcstok_next)) return NULL;
886 while (*str && strchrW( delim, *str )) str++;
887 if (!*str) return NULL;
889 while (*str && !strchrW( delim, *str )) str++;
890 if (*str) *str++ = 0;
891 data->wcstok_next = str;
896 /*********************************************************************
899 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
901 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
904 /*********************************************************************
905 * iswalnum (MSVCRT.@)
907 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
909 return isalnumW( wc );
912 /*********************************************************************
913 * iswalpha (MSVCRT.@)
915 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
917 return isalphaW( wc );
920 /*********************************************************************
921 * iswcntrl (MSVCRT.@)
923 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
925 return iscntrlW( wc );
928 /*********************************************************************
929 * iswdigit (MSVCRT.@)
931 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
933 return isdigitW( wc );
936 /*********************************************************************
937 * iswgraph (MSVCRT.@)
939 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
941 return isgraphW( wc );
944 /*********************************************************************
945 * iswlower (MSVCRT.@)
947 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
949 return islowerW( wc );
952 /*********************************************************************
953 * iswprint (MSVCRT.@)
955 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
957 return isprintW( wc );
960 /*********************************************************************
961 * iswpunct (MSVCRT.@)
963 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
965 return ispunctW( wc );
968 /*********************************************************************
969 * iswspace (MSVCRT.@)
971 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
973 return isspaceW( wc );
976 /*********************************************************************
977 * iswupper (MSVCRT.@)
979 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
981 return isupperW( wc );
984 /*********************************************************************
985 * iswxdigit (MSVCRT.@)
987 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
989 return isxdigitW( wc );