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 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 int pf_handle_string_format( pf_output
*out
, const void* str
, int len
,
361 pf_flags
*flags
, BOOL capital_letter
)
363 if(str
== NULL
) /* catch NULL pointer */
364 return pf_output_format_A( out
, "(null)", -1, flags
);
366 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
367 if(flags
->WideString
|| flags
->IntegerLength
== 'l')
368 return pf_output_format_W( out
, str
, len
, flags
);
369 if(flags
->IntegerLength
== 'h')
370 return pf_output_format_A( out
, str
, len
, flags
);
372 /* %s,%c -> chars in ansi functions & wchars in unicode
373 * %S,%C -> wchars in ansi functions & chars in unicode */
374 if( capital_letter
== out
->unicode
) /* either both TRUE or both FALSE */
375 return pf_output_format_A( out
, str
, len
, flags
);
377 return pf_output_format_W( out
, str
, len
, flags
);
380 static inline BOOL
pf_is_integer_format( char fmt
)
382 static const char float_fmts
[] = "diouxX";
385 return strchr( float_fmts
, fmt
) ? TRUE
: FALSE
;
388 static inline BOOL
pf_is_double_format( char fmt
)
390 static const char float_fmts
[] = "aeEfgG";
393 return strchr( float_fmts
, fmt
) ? TRUE
: FALSE
;
396 static inline BOOL
pf_is_valid_format( char fmt
)
398 static const char float_fmts
[] = "acCdeEfgGinouxX";
401 return strchr( float_fmts
, fmt
) ? TRUE
: FALSE
;
404 static void pf_rebuild_format_string( char *p
, pf_flags
*flags
)
409 if( flags
->LeftAlign
)
410 *p
++ = flags
->LeftAlign
;
411 if( flags
->Alternate
)
412 *p
++ = flags
->Alternate
;
414 *p
++ = flags
->PadZero
;
415 if( flags
->FieldLength
)
417 sprintf(p
, "%d", flags
->FieldLength
);
420 if( flags
->Precision
>= 0 )
422 sprintf(p
, ".%d", flags
->Precision
);
425 *p
++ = flags
->Format
;
429 /* pf_integer_conv: prints x to buf, including alternate formats,
431 static void pf_integer_conv( char *buf
, pf_flags
*flags
, LONGLONG x
)
440 if( flags
->Format
== 'o' )
442 else if( flags
->Format
== 'x' || flags
->Format
== 'X' )
445 if( flags
->Format
== 'X' )
446 digits
= "0123456789ABCDEFX";
448 digits
= "0123456789abcdefx";
450 if( x
< 0 && ( flags
->Format
== 'd' || flags
->Format
== 'i' ) )
456 /* Do conversion (backwards) */
458 if( x
== 0 && flags
->Precision
)
463 j
= (ULONGLONG
) x
% base
;
464 x
= (ULONGLONG
) x
/ base
;
465 tmp
[i
++] = digits
[j
];
467 k
= flags
->Precision
- i
;
470 if( flags
->Alternate
)
474 tmp
[i
++] = digits
[16];
477 else if( base
== 8 && tmp
[i
-1] != '0' )
481 /* Reverse for buf */
487 /* Adjust precision so pf_fill won't truncate the number later */
488 flags
->Precision
= strlen( buf
);
493 /*********************************************************************
494 * pf_vsnprintf (INTERNAL)
496 * implements both A and W vsnprintf functions
498 static int pf_vsnprintf( pf_output
*out
, const WCHAR
*format
, va_list valist
)
501 LPCWSTR q
, p
= format
;
504 TRACE("format is %s\n",debugstr_w(format
));
507 q
= strchrW( p
, '%' );
509 /* there's no % characters left, output the rest of the string */
512 r
= pf_output_stringW(out
, p
, -1);
519 /* there's characters before the %, output them */
522 r
= pf_output_stringW(out
, p
, q
- p
);
528 /* we must be at a % now, skip over it */
532 /* output a single % character */
535 r
= pf_output_stringW(out
, p
++, 1);
541 /* parse the flags */
542 memset( &flags
, 0, sizeof flags
);
545 if( *p
== '+' || *p
== ' ' )
547 if ( flags
.Sign
!= '+' )
551 flags
.LeftAlign
= *p
;
555 flags
.Alternate
= *p
;
561 /* deal with the field width specifier */
562 flags
.FieldLength
= 0;
565 flags
.FieldLength
= va_arg( valist
, int );
568 else while( isdigit(*p
) )
570 flags
.FieldLength
*= 10;
571 flags
.FieldLength
+= *p
++ - '0';
574 /* deal with precision */
575 flags
.Precision
= -1;
582 flags
.Precision
= va_arg( valist
, int );
585 else while( isdigit(*p
) )
587 flags
.Precision
*= 10;
588 flags
.Precision
+= *p
++ - '0';
592 /* deal with integer width modifier */
595 if( *p
== 'h' || *p
== 'l' || *p
== 'L' )
597 flags
.IntegerLength
= *p
;
602 if( *(p
+1) == '6' && *(p
+2) == '4' )
604 flags
.IntegerDouble
++;
607 else if( *(p
+1) == '3' && *(p
+2) == '2' )
609 else if( isdigit(*(p
+1)) || *(p
+1) == 0 )
615 flags
.WideString
= *p
++;
625 /* output a string */
626 if( flags
.Format
== 's' || flags
.Format
== 'S' )
627 r
= pf_handle_string_format( out
, va_arg(valist
, const void*), -1,
628 &flags
, (flags
.Format
== 'S') );
630 /* output a single character */
631 else if( flags
.Format
== 'c' || flags
.Format
== 'C' )
633 INT ch
= va_arg( valist
, int );
635 r
= pf_handle_string_format( out
, &ch
, 1, &flags
, (flags
.Format
== 'C') );
638 /* output a pointer */
639 else if( flags
.Format
== 'p' )
644 if( flags
.Alternate
)
645 sprintf(pointer
, "0X%08lX", va_arg(valist
, long));
647 sprintf(pointer
, "%08lX", va_arg(valist
, long));
648 r
= pf_output_format_A( out
, pointer
, -1, &flags
);
652 else if( flags
.Format
== 'n' )
654 int *x
= va_arg(valist
, int *);
658 /* deal with 64-bit integers */
659 else if( pf_is_integer_format( flags
.Format
) && flags
.IntegerDouble
)
661 char number
[40], *x
= number
;
663 if( flags
.FieldLength
>= sizeof number
)
664 x
= HeapAlloc( GetProcessHeap(), 0, flags
.FieldLength
+1 );
666 pf_integer_conv( x
, &flags
, va_arg(valist
, LONGLONG
) );
668 r
= pf_output_format_A( out
, x
, -1, &flags
);
670 HeapFree( GetProcessHeap(), 0, number
);
673 /* deal with integers and floats using libc's printf */
674 else if( pf_is_valid_format( flags
.Format
) )
676 char fmt
[20], number
[40], *x
= number
;
678 if( flags
.FieldLength
>= sizeof number
)
679 x
= HeapAlloc( GetProcessHeap(), 0, flags
.FieldLength
+1 );
681 pf_rebuild_format_string( fmt
, &flags
);
683 if( pf_is_double_format( flags
.Format
) )
684 sprintf( x
, fmt
, va_arg(valist
, double) );
686 sprintf( x
, fmt
, va_arg(valist
, int) );
688 r
= pf_output_stringA( out
, x
, -1 );
690 HeapFree( GetProcessHeap(), 0, number
);
700 /* check we reached the end, and null terminate the string */
702 pf_output_stringW( out
, p
, 1 );
704 return out
->used
- 1;
707 /*********************************************************************
708 * _vsnprintf (MSVCRT.@)
710 int CDECL
MSVCRT_vsnprintf( char *str
, unsigned int len
,
711 const char *format
, va_list valist
)
714 LPWSTR formatW
= NULL
;
725 sz
= MultiByteToWideChar( CP_ACP
, 0, format
, -1, NULL
, 0 );
726 formatW
= HeapAlloc( GetProcessHeap(), 0, sz
*sizeof(WCHAR
) );
727 MultiByteToWideChar( CP_ACP
, 0, format
, -1, formatW
, sz
);
730 r
= pf_vsnprintf( &out
, formatW
, valist
);
732 HeapFree( GetProcessHeap(), 0, formatW
);
737 /*********************************************************************
738 * _snprintf (MSVCRT.@)
740 int CDECL
MSVCRT__snprintf(char *str
, unsigned int len
, const char *format
, ...)
744 va_start(valist
, format
);
745 retval
= MSVCRT_vsnprintf(str
, len
, format
, valist
);
750 /*********************************************************************
751 * _vsnwsprintf (MSVCRT.@)
753 int CDECL
MSVCRT_vsnwprintf( MSVCRT_wchar_t
*str
, unsigned int len
,
754 const MSVCRT_wchar_t
*format
, va_list valist
)
763 return pf_vsnprintf( &out
, format
, valist
);
766 /*********************************************************************
767 * _snwprintf (MSVCRT.@)
769 int CDECL
MSVCRT__snwprintf( MSVCRT_wchar_t
*str
, unsigned int len
, const MSVCRT_wchar_t
*format
, ...)
773 va_start(valist
, format
);
774 retval
= MSVCRT_vsnwprintf(str
, len
, format
, valist
);
779 /*********************************************************************
782 int CDECL
MSVCRT_sprintf( char *str
, const char *format
, ... )
787 va_start( ap
, format
);
788 r
= MSVCRT_vsnprintf( str
, INT_MAX
, format
, ap
);
793 /*********************************************************************
794 * swprintf (MSVCRT.@)
796 int CDECL
MSVCRT_swprintf( MSVCRT_wchar_t
*str
, const MSVCRT_wchar_t
*format
, ... )
801 va_start( ap
, format
);
802 r
= MSVCRT_vsnwprintf( str
, INT_MAX
, format
, ap
);
807 /*********************************************************************
808 * vswprintf (MSVCRT.@)
810 int CDECL
MSVCRT_vswprintf( MSVCRT_wchar_t
* str
, const MSVCRT_wchar_t
* format
, va_list args
)
812 return MSVCRT_vsnwprintf( str
, INT_MAX
, format
, args
);
815 /*********************************************************************
818 int CDECL
MSVCRT_wcscoll( const MSVCRT_wchar_t
* str1
, const MSVCRT_wchar_t
* str2
)
820 /* FIXME: handle collates */
821 return strcmpW( str1
, str2
);
824 /*********************************************************************
827 MSVCRT_wchar_t
* CDECL
MSVCRT_wcspbrk( const MSVCRT_wchar_t
* str
, const MSVCRT_wchar_t
* accept
)
829 const MSVCRT_wchar_t
* p
;
832 for (p
= accept
; *p
; p
++) if (*p
== *str
) return (MSVCRT_wchar_t
*)str
;
838 /*********************************************************************
841 MSVCRT_wchar_t
* CDECL
MSVCRT_wcstok( MSVCRT_wchar_t
*str
, const MSVCRT_wchar_t
*delim
)
843 thread_data_t
*data
= msvcrt_get_thread_data();
847 if (!(str
= data
->wcstok_next
)) return NULL
;
849 while (*str
&& strchrW( delim
, *str
)) str
++;
850 if (!*str
) return NULL
;
852 while (*str
&& !strchrW( delim
, *str
)) str
++;
853 if (*str
) *str
++ = 0;
854 data
->wcstok_next
= str
;
859 /*********************************************************************
862 INT CDECL
MSVCRT_wctomb( char *dst
, MSVCRT_wchar_t ch
)
864 return WideCharToMultiByte( CP_ACP
, 0, &ch
, 1, dst
, 6, NULL
, NULL
);
867 /*********************************************************************
868 * iswalnum (MSVCRT.@)
870 INT CDECL
MSVCRT_iswalnum( MSVCRT_wchar_t wc
)
872 return isalnumW( wc
);
875 /*********************************************************************
876 * iswalpha (MSVCRT.@)
878 INT CDECL
MSVCRT_iswalpha( MSVCRT_wchar_t wc
)
880 return isalphaW( wc
);
883 /*********************************************************************
884 * iswcntrl (MSVCRT.@)
886 INT CDECL
MSVCRT_iswcntrl( MSVCRT_wchar_t wc
)
888 return iscntrlW( wc
);
891 /*********************************************************************
892 * iswdigit (MSVCRT.@)
894 INT CDECL
MSVCRT_iswdigit( MSVCRT_wchar_t wc
)
896 return isdigitW( wc
);
899 /*********************************************************************
900 * iswgraph (MSVCRT.@)
902 INT CDECL
MSVCRT_iswgraph( MSVCRT_wchar_t wc
)
904 return isgraphW( wc
);
907 /*********************************************************************
908 * iswlower (MSVCRT.@)
910 INT CDECL
MSVCRT_iswlower( MSVCRT_wchar_t wc
)
912 return islowerW( wc
);
915 /*********************************************************************
916 * iswprint (MSVCRT.@)
918 INT CDECL
MSVCRT_iswprint( MSVCRT_wchar_t wc
)
920 return isprintW( wc
);
923 /*********************************************************************
924 * iswpunct (MSVCRT.@)
926 INT CDECL
MSVCRT_iswpunct( MSVCRT_wchar_t wc
)
928 return ispunctW( wc
);
931 /*********************************************************************
932 * iswspace (MSVCRT.@)
934 INT CDECL
MSVCRT_iswspace( MSVCRT_wchar_t wc
)
936 return isspaceW( wc
);
939 /*********************************************************************
940 * iswupper (MSVCRT.@)
942 INT CDECL
MSVCRT_iswupper( MSVCRT_wchar_t wc
)
944 return isupperW( wc
);
947 /*********************************************************************
948 * iswxdigit (MSVCRT.@)
950 INT CDECL
MSVCRT_iswxdigit( MSVCRT_wchar_t wc
)
952 return isxdigitW( wc
);