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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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* _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 _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* _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* _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* _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
105 MSVCRT_wchar_t* ret = str;
106 while (*str) *str++ = c;
110 /*********************************************************************
113 double 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 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
288 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
291 if( left && flags->Sign )
293 flags->FieldLength--;
295 r = pf_output_stringA( out, &flags->Sign, 1 );
298 if( ( !left && flags->LeftAlign ) ||
299 ( left && !flags->LeftAlign ))
301 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
303 if( left && flags->PadZero )
304 r = pf_output_stringA( out, "0", 1 );
306 r = pf_output_stringA( out, " ", 1 );
310 if( left && flags->Sign && !flags->PadZero )
311 r = pf_output_stringA( out, &flags->Sign, 1 );
316 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
317 int len, pf_flags *flags )
322 len = strlenW( str );
324 if (flags->Precision >= 0 && flags->Precision < len)
325 len = flags->Precision;
327 r = pf_fill( out, len, flags, 1 );
330 r = pf_output_stringW( out, str, len );
333 r = pf_fill( out, len, flags, 0 );
338 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
339 int len, pf_flags *flags )
346 if (flags->Precision >= 0 && flags->Precision < len)
347 len = flags->Precision;
349 r = pf_fill( out, len, flags, 1 );
352 r = pf_output_stringA( out, str, len );
355 r = pf_fill( out, len, flags, 0 );
360 static inline BOOL pf_is_integer_format( char fmt )
362 static const char float_fmts[] = "diouxX";
365 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
368 static inline BOOL pf_is_double_format( char fmt )
370 static const char float_fmts[] = "aeEfgG";
373 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
376 static inline BOOL pf_is_valid_format( char fmt )
378 static const char float_fmts[] = "acCdeEfgGinouxX";
381 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
384 static void pf_rebuild_format_string( char *p, pf_flags *flags )
389 if( flags->LeftAlign )
390 *p++ = flags->LeftAlign;
391 if( flags->Alternate )
392 *p++ = flags->Alternate;
394 *p++ = flags->PadZero;
395 if( flags->FieldLength )
397 sprintf(p, "%d", flags->FieldLength);
400 if( flags->Precision >= 0 )
402 sprintf(p, ".%d", flags->Precision);
405 *p++ = flags->Format;
409 /* pf_integer_conv: prints x to buf, including alternate formats,
411 static void pf_integer_conv( char *buf, pf_flags *flags, LONGLONG x )
420 if( flags->Format == 'o' )
422 else if( flags->Format == 'x' || flags->Format == 'X' )
425 if( flags->Format == 'X' )
426 digits = "0123456789ABCDEFX";
428 digits = "0123456789abcdefx";
430 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
436 /* Do conversion (backwards) */
438 if( x == 0 && flags->Precision )
443 j = (ULONGLONG) x % base;
444 x = (ULONGLONG) x / base;
445 tmp[i++] = digits[j];
447 k = flags->Precision - i;
450 if( flags->Alternate )
454 tmp[i++] = digits[16];
457 else if( base == 8 && tmp[i-1] != '0' )
461 /* Reverse for buf */
467 /* Adjust precision so pf_fill won't truncate the number later */
468 flags->Precision = strlen( buf );
473 /*********************************************************************
474 * pf_vsnprintf (INTERNAL)
476 * implements both A and W vsnprintf functions
478 static int pf_vsnprintf( pf_output *out, const WCHAR *format, va_list valist )
481 LPCWSTR q, p = format;
484 TRACE("format is %s\n",debugstr_w(format));
487 q = strchrW( p, '%' );
489 /* there's no % characters left, output the rest of the string */
492 r = pf_output_stringW(out, p, -1);
499 /* there's characters before the %, output them */
502 r = pf_output_stringW(out, p, q - p);
508 /* we must be at a % now, skip over it */
512 /* output a single % character */
515 r = pf_output_stringW(out, p++, 1);
521 /* parse the flags */
522 memset( &flags, 0, sizeof flags );
525 if( *p == '+' || *p == ' ' )
527 if ( flags.Sign != '+' )
531 flags.LeftAlign = *p;
535 flags.Alternate = *p;
541 /* deal with the field width specifier */
542 flags.FieldLength = 0;
545 flags.FieldLength = va_arg( valist, int );
548 else while( isdigit(*p) )
550 flags.FieldLength *= 10;
551 flags.FieldLength += *p++ - '0';
554 /* deal with precision */
555 flags.Precision = -1;
562 flags.Precision = va_arg( valist, int );
565 else while( isdigit(*p) )
567 flags.Precision *= 10;
568 flags.Precision += *p++ - '0';
572 /* deal with integer width modifier */
575 if( *p == 'h' || *p == 'l' || *p == 'L' )
577 flags.IntegerLength = *p;
582 if( *(p+1) == '6' && *(p+2) == '4' )
584 flags.IntegerDouble++;
587 else if( *(p+1) == '3' && *(p+2) == '2' )
589 else if( isdigit(*(p+1)) || *(p+1) == 0 )
595 flags.WideString = *p++;
605 /* output a unicode string */
606 if( ( flags.Format == 's' && (flags.WideString || flags.IntegerLength == 'l' )) ||
607 ( !out->unicode && flags.Format == 'S' ) ||
608 ( out->unicode && flags.Format == 's' ) )
610 LPCWSTR str = va_arg( valist, const WCHAR * );
613 r = pf_output_format_W( out, str, -1, &flags );
615 r = pf_output_format_A( out, "(null)", -1, &flags );
618 /* output a ASCII string */
619 else if( ( flags.Format == 's' && flags.IntegerLength == 'h' ) ||
620 ( out->unicode && flags.Format == 'S' ) ||
621 ( !out->unicode && flags.Format == 's' ) )
623 LPCSTR str = va_arg( valist, const CHAR * );
626 r = pf_output_format_A( out, str, -1, &flags );
628 r = pf_output_format_A( out, "(null)", -1, &flags );
631 /* output a single wide character */
632 else if( ( flags.Format == 'c' && flags.IntegerLength == 'w' ) ||
633 ( out->unicode && flags.Format == 'c' ) ||
634 ( !out->unicode && flags.Format == 'C' ) )
636 WCHAR ch = va_arg( valist, int );
638 r = pf_output_format_W( out, &ch, 1, &flags );
641 /* output a single ascii character */
642 else if( ( flags.Format == 'c' && flags.IntegerLength == 'h' ) ||
643 ( out->unicode && flags.Format == 'C' ) ||
644 ( !out->unicode && flags.Format == 'c' ) )
646 CHAR ch = va_arg( valist, int );
648 r = pf_output_format_A( out, &ch, 1, &flags );
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 if( flags.FieldLength >= sizeof number )
677 x = HeapAlloc( GetProcessHeap(), 0, flags.FieldLength+1 );
679 pf_integer_conv( x, &flags, va_arg(valist, LONGLONG) );
681 r = pf_output_format_A( out, x, -1, &flags );
683 HeapFree( GetProcessHeap(), 0, number );
686 /* deal with integers and floats using libc's printf */
687 else if( pf_is_valid_format( flags.Format ) )
689 char fmt[20], number[40], *x = number;
691 if( flags.FieldLength >= sizeof number )
692 x = HeapAlloc( GetProcessHeap(), 0, flags.FieldLength+1 );
694 pf_rebuild_format_string( fmt, &flags );
696 if( pf_is_double_format( flags.Format ) )
697 sprintf( x, fmt, va_arg(valist, double) );
699 sprintf( x, fmt, va_arg(valist, int) );
701 r = pf_output_stringA( out, x, -1 );
703 HeapFree( GetProcessHeap(), 0, number );
713 /* check we reached the end, and null terminate the string */
715 pf_output_stringW( out, p, 1 );
717 return out->used - 1;
720 /*********************************************************************
721 * _vsnprintf (MSVCRT.@)
723 int MSVCRT_vsnprintf( char *str, unsigned int len,
724 const char *format, va_list valist )
727 LPWSTR formatW = NULL;
738 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
739 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
740 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
743 r = pf_vsnprintf( &out, formatW, valist );
745 HeapFree( GetProcessHeap(), 0, formatW );
750 /*********************************************************************
751 * _snprintf (MSVCRT.@)
753 int MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
757 va_start(valist, format);
758 retval = MSVCRT_vsnprintf(str, len, format, valist);
763 /*********************************************************************
764 * _vsnwsprintf (MSVCRT.@)
766 int MSVCRT_vsnwprintf( MSVCRT_wchar_t *str, unsigned int len,
767 const MSVCRT_wchar_t *format, va_list valist )
776 return pf_vsnprintf( &out, format, valist );
779 /*********************************************************************
780 * _snwprintf (MSVCRT.@)
782 int MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
786 va_start(valist, format);
787 retval = MSVCRT_vsnwprintf(str, len, format, valist);
792 /*********************************************************************
795 int MSVCRT_sprintf( char *str, const char *format, ... )
800 va_start( ap, format );
801 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
806 /*********************************************************************
807 * swprintf (MSVCRT.@)
809 int MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
814 va_start( ap, format );
815 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
820 /*********************************************************************
821 * vswprintf (MSVCRT.@)
823 int MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, va_list args )
825 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
828 /*********************************************************************
831 int MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
833 /* FIXME: handle collates */
834 return strcmpW( str1, str2 );
837 /*********************************************************************
840 MSVCRT_wchar_t* MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
842 const MSVCRT_wchar_t* p;
845 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
851 /*********************************************************************
854 MSVCRT_wchar_t *MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
856 thread_data_t *data = msvcrt_get_thread_data();
860 if (!(str = data->wcstok_next)) return NULL;
862 while (*str && strchrW( delim, *str )) str++;
863 if (!*str) return NULL;
865 while (*str && !strchrW( delim, *str )) str++;
866 if (*str) *str++ = 0;
867 data->wcstok_next = str;
872 /*********************************************************************
875 INT MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
877 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
880 /*********************************************************************
881 * iswalnum (MSVCRT.@)
883 INT MSVCRT_iswalnum( MSVCRT_wchar_t wc )
885 return isalnumW( wc );
888 /*********************************************************************
889 * iswalpha (MSVCRT.@)
891 INT MSVCRT_iswalpha( MSVCRT_wchar_t wc )
893 return isalphaW( wc );
896 /*********************************************************************
897 * iswcntrl (MSVCRT.@)
899 INT MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
901 return iscntrlW( wc );
904 /*********************************************************************
905 * iswdigit (MSVCRT.@)
907 INT MSVCRT_iswdigit( MSVCRT_wchar_t wc )
909 return isdigitW( wc );
912 /*********************************************************************
913 * iswgraph (MSVCRT.@)
915 INT MSVCRT_iswgraph( MSVCRT_wchar_t wc )
917 return isgraphW( wc );
920 /*********************************************************************
921 * iswlower (MSVCRT.@)
923 INT MSVCRT_iswlower( MSVCRT_wchar_t wc )
925 return islowerW( wc );
928 /*********************************************************************
929 * iswprint (MSVCRT.@)
931 INT MSVCRT_iswprint( MSVCRT_wchar_t wc )
933 return isprintW( wc );
936 /*********************************************************************
937 * iswpunct (MSVCRT.@)
939 INT MSVCRT_iswpunct( MSVCRT_wchar_t wc )
941 return ispunctW( wc );
944 /*********************************************************************
945 * iswspace (MSVCRT.@)
947 INT MSVCRT_iswspace( MSVCRT_wchar_t wc )
949 return isspaceW( wc );
952 /*********************************************************************
953 * iswupper (MSVCRT.@)
955 INT MSVCRT_iswupper( MSVCRT_wchar_t wc )
957 return isupperW( wc );
960 /*********************************************************************
961 * iswxdigit (MSVCRT.@)
963 INT MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
965 return isxdigitW( wc );