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( ( !left && flags->LeftAlign ) ||
289 ( left && !flags->LeftAlign ))
291 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
293 if( left && flags->PadZero )
294 r = pf_output_stringA( out, "0", 1 );
296 r = pf_output_stringA( out, " ", 1 );
303 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
304 int len, pf_flags *flags )
309 len = strlenW( str );
311 if (flags->Precision >= 0 && flags->Precision < len)
312 len = flags->Precision;
314 r = pf_fill( out, len, flags, 1 );
317 r = pf_output_stringW( out, str, len );
320 r = pf_fill( out, len, flags, 0 );
325 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
326 int len, pf_flags *flags )
333 if (flags->Precision >= 0 && flags->Precision < len)
334 len = flags->Precision;
336 r = pf_fill( out, len, flags, 1 );
339 r = pf_output_stringA( out, str, len );
342 r = pf_fill( out, len, flags, 0 );
347 static inline BOOL pf_is_double_format( char fmt )
349 static const char float_fmts[] = "aeEfgG";
352 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
355 static inline BOOL pf_is_valid_format( char fmt )
357 static const char float_fmts[] = "acCdeEfgGinouxX";
360 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
363 static void pf_rebuild_format_string( char *p, pf_flags *flags )
368 if( flags->LeftAlign )
369 *p++ = flags->LeftAlign;
370 if( flags->Alternate )
371 *p++ = flags->Alternate;
373 *p++ = flags->PadZero;
374 if( flags->FieldLength )
376 sprintf(p, "%d", flags->FieldLength);
379 if( flags->Precision >= 0 )
381 sprintf(p, ".%d", flags->Precision);
384 *p++ = flags->Format;
388 /*********************************************************************
389 * pf_vsnprintf (INTERNAL)
391 * implements both A and W vsnprintf functions
393 static int pf_vsnprintf( pf_output *out, const WCHAR *format, va_list valist )
396 LPCWSTR q, p = format;
399 TRACE("format is %s\n",debugstr_w(format));
402 q = strchrW( p, '%' );
404 /* there's no % characters left, output the rest of the string */
407 r = pf_output_stringW(out, p, -1);
414 /* there's characters before the %, output them */
417 r = pf_output_stringW(out, p, q - p);
423 /* we must be at a % now, skip over it */
427 /* output a single % character */
430 r = pf_output_stringW(out, p++, 1);
436 /* parse the flags */
437 memset( &flags, 0, sizeof flags );
440 if( *p == '+' || *p == ' ' )
442 if ( flags.Sign != '+' )
446 flags.LeftAlign = *p;
450 flags.Alternate = *p;
456 /* deal with the field width specifier */
457 flags.FieldLength = 0;
460 flags.FieldLength = va_arg( valist, int );
463 else while( isdigit(*p) )
465 flags.FieldLength *= 10;
466 flags.FieldLength += *p++ - '0';
469 /* deal with precision */
470 flags.Precision = -1;
477 flags.Precision = va_arg( valist, int );
480 else while( isdigit(*p) )
482 flags.Precision *= 10;
483 flags.Precision += *p++ - '0';
487 /* deal with integer width modifier */
490 if( *p == 'h' || *p == 'l' || *p == 'L' )
492 flags.IntegerLength = *p;
497 if( *(p+1) == '6' && *(p+2) == '4' )
499 flags.IntegerDouble++;
502 else if( *(p+1) == '3' && *(p+2) == '2' )
504 else if( isdigit(*(p+1)) || *(p+1) == 0 )
510 flags.WideString = *p++;
520 /* output a unicode string */
521 if( ( flags.Format == 's' && (flags.WideString || flags.IntegerLength == 'l' )) ||
522 ( !out->unicode && flags.Format == 'S' ) ||
523 ( out->unicode && flags.Format == 's' ) )
525 LPCWSTR str = va_arg( valist, const WCHAR * );
528 r = pf_output_format_W( out, str, -1, &flags );
530 r = pf_output_format_A( out, "(null)", -1, &flags );
533 /* output a ASCII string */
534 else if( ( flags.Format == 's' && flags.IntegerLength == 'h' ) ||
535 ( out->unicode && flags.Format == 'S' ) ||
536 ( !out->unicode && flags.Format == 's' ) )
538 LPCSTR str = va_arg( valist, const CHAR * );
541 r = pf_output_format_A( out, str, -1, &flags );
543 r = pf_output_format_A( out, "(null)", -1, &flags );
546 /* output a single wide character */
547 else if( ( flags.Format == 'c' && flags.IntegerLength == 'w' ) ||
548 ( out->unicode && flags.Format == 'c' ) ||
549 ( !out->unicode && flags.Format == 'C' ) )
551 WCHAR ch = va_arg( valist, int );
553 r = pf_output_format_W( out, &ch, 1, &flags );
556 /* output a single ascii character */
557 else if( ( flags.Format == 'c' && flags.IntegerLength == 'h' ) ||
558 ( out->unicode && flags.Format == 'C' ) ||
559 ( !out->unicode && flags.Format == 'c' ) )
561 CHAR ch = va_arg( valist, int );
563 r = pf_output_format_A( out, &ch, 1, &flags );
566 /* output a pointer */
567 else if( flags.Format == 'p' )
572 if( flags.Alternate )
573 sprintf(pointer, "0X%08lX", va_arg(valist, long));
575 sprintf(pointer, "%08lX", va_arg(valist, long));
576 r = pf_output_format_A( out, pointer, -1, &flags );
580 else if( flags.Format == 'n' )
582 int *x = va_arg(valist, int *);
586 /* deal with integers and floats using libc's printf */
587 else if( pf_is_valid_format( flags.Format ) )
589 char fmt[20], number[40], *x = number;
591 if( flags.FieldLength >= sizeof number )
592 x = HeapAlloc( GetProcessHeap(), 0, flags.FieldLength+1 );
594 pf_rebuild_format_string( fmt, &flags );
596 if( pf_is_double_format( flags.Format ) )
597 sprintf( x, fmt, va_arg(valist, double) );
599 sprintf( x, fmt, va_arg(valist, int) );
601 r = pf_output_stringA( out, x, -1 );
603 HeapFree( GetProcessHeap(), 0, number );
613 /* check we reached the end, and null terminate the string */
615 r = pf_output_stringW( out, p, 1 );
619 return out->used - 1;
622 /*********************************************************************
623 * _vsnprintf (MSVCRT.@)
625 int MSVCRT_vsnprintf( char *str, unsigned int len,
626 const char *format, va_list valist )
629 LPWSTR formatW = NULL;
640 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
641 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
642 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
645 r = pf_vsnprintf( &out, formatW, valist );
647 HeapFree( GetProcessHeap(), 0, formatW );
652 /*********************************************************************
653 * _vsnwsprintf (MSVCRT.@)
655 int MSVCRT_vsnwprintf( MSVCRT_wchar_t *str, unsigned int len,
656 const WCHAR *format, va_list valist )
665 return pf_vsnprintf( &out, format, valist );
668 /*********************************************************************
671 int MSVCRT_sprintf( char *str, const char *format, ... )
676 va_start( ap, format );
677 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
682 /*********************************************************************
683 * swprintf (MSVCRT.@)
685 int MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
690 va_start( ap, format );
691 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
696 /*********************************************************************
697 * vswprintf (MSVCRT.@)
699 int MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, va_list args )
701 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
704 /*********************************************************************
707 int MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
709 /* FIXME: handle collates */
710 return strcmpW( str1, str2 );
713 /*********************************************************************
716 MSVCRT_wchar_t* MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
718 const MSVCRT_wchar_t* p;
721 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
727 /*********************************************************************
730 INT MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
732 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
735 /*********************************************************************
736 * iswalnum (MSVCRT.@)
738 INT MSVCRT_iswalnum( MSVCRT_wchar_t wc )
740 return isalnumW( wc );
743 /*********************************************************************
744 * iswalpha (MSVCRT.@)
746 INT MSVCRT_iswalpha( MSVCRT_wchar_t wc )
748 return isalphaW( wc );
751 /*********************************************************************
752 * iswcntrl (MSVCRT.@)
754 INT MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
756 return iscntrlW( wc );
759 /*********************************************************************
760 * iswdigit (MSVCRT.@)
762 INT MSVCRT_iswdigit( MSVCRT_wchar_t wc )
764 return isdigitW( wc );
767 /*********************************************************************
768 * iswgraph (MSVCRT.@)
770 INT MSVCRT_iswgraph( MSVCRT_wchar_t wc )
772 return isgraphW( wc );
775 /*********************************************************************
776 * iswlower (MSVCRT.@)
778 INT MSVCRT_iswlower( MSVCRT_wchar_t wc )
780 return islowerW( wc );
783 /*********************************************************************
784 * iswprint (MSVCRT.@)
786 INT MSVCRT_iswprint( MSVCRT_wchar_t wc )
788 return isprintW( wc );
791 /*********************************************************************
792 * iswpunct (MSVCRT.@)
794 INT MSVCRT_iswpunct( MSVCRT_wchar_t wc )
796 return ispunctW( wc );
799 /*********************************************************************
800 * iswspace (MSVCRT.@)
802 INT MSVCRT_iswspace( MSVCRT_wchar_t wc )
804 return isspaceW( wc );
807 /*********************************************************************
808 * iswupper (MSVCRT.@)
810 INT MSVCRT_iswupper( MSVCRT_wchar_t wc )
812 return isupperW( wc );
815 /*********************************************************************
816 * iswxdigit (MSVCRT.@)
818 INT MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
820 return isxdigitW( wc );