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
26 #include "wine/unicode.h"
28 #include "msvcrt/stdio.h"
29 #include "msvcrt/stdlib.h"
30 #include "msvcrt/string.h"
31 #include "msvcrt/wctype.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
38 /* INTERNAL: MSVCRT_malloc() based wstrndup */
39 MSVCRT_wchar_t* msvcrt_wstrndup(const MSVCRT_wchar_t *buf, unsigned int size)
42 unsigned int len = strlenW(buf), max_len;
44 max_len = size <= len? size : len + 1;
46 ret = MSVCRT_malloc(max_len * sizeof (MSVCRT_wchar_t));
49 memcpy(ret,buf,max_len * sizeof (MSVCRT_wchar_t));
55 /*********************************************************************
58 MSVCRT_wchar_t* _wcsdup( const MSVCRT_wchar_t* str )
60 MSVCRT_wchar_t* ret = NULL;
63 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
64 ret = MSVCRT_malloc( size );
65 if (ret) memcpy( ret, str, size );
70 /*********************************************************************
71 * _wcsicoll (MSVCRT.@)
73 INT _wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
75 /* FIXME: handle collates */
76 return strcmpiW( str1, str2 );
79 /*********************************************************************
82 MSVCRT_wchar_t* _wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
84 MSVCRT_wchar_t* ret = str;
85 while ((n-- > 0) && *str) *str++ = c;
89 /*********************************************************************
92 MSVCRT_wchar_t* _wcsrev( MSVCRT_wchar_t* str )
94 MSVCRT_wchar_t* ret = str;
95 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
98 MSVCRT_wchar_t t = *end;
105 /*********************************************************************
108 MSVCRT_wchar_t* _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
110 MSVCRT_wchar_t* ret = str;
111 while (*str) *str++ = c;
115 /*********************************************************************
118 double MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
120 const MSVCRT_wchar_t* str = lpszStr;
122 double ret = 0, divisor = 10.0;
124 TRACE("(%s,%p) semi-stub\n", debugstr_w(lpszStr), end);
127 * - Should set errno on failure
128 * - Should fail on overflow
129 * - Need to check which input formats are allowed
131 while (isspaceW(*str))
140 while (isdigitW(*str))
142 ret = ret * 10.0 + (*str - '0');
147 while (isdigitW(*str))
149 ret = ret + (*str - '0') / divisor;
154 if (*str == 'E' || *str == 'e' || *str == 'D' || *str == 'd')
156 int negativeExponent = 0;
160 negativeExponent = 1;
163 while (isdigitW(*str))
165 exponent = exponent * 10 + (*str - '0');
170 if (negativeExponent)
171 ret = ret / pow(10.0, exponent);
173 ret = ret * pow(10.0, exponent);
181 *end = (MSVCRT_wchar_t*)str;
183 TRACE("returning %g\n", ret);
187 static int MSVCRT_vsnprintfW(WCHAR *str, size_t len, const WCHAR *format, va_list valist)
189 unsigned int written = 0;
190 const WCHAR *iter = format;
191 char bufa[256], fmtbufa[64], *fmta;
195 while (*iter && *iter != '%')
197 if (written++ >= len)
205 if (written++ >= len)
207 *str++ = '%'; /* "%%"->'%' */
214 while (*iter == '0' ||
223 char *buffiter = bufa;
224 int fieldlen = va_arg(valist, int);
225 sprintf(buffiter, "%d", fieldlen);
227 *fmta++ = *buffiter++;
234 while (isdigit(*iter))
242 char *buffiter = bufa;
243 int fieldlen = va_arg(valist, int);
244 sprintf(buffiter, "%d", fieldlen);
246 *fmta++ = *buffiter++;
249 while (isdigit(*iter))
252 if (*iter == 'h' || *iter == 'l')
259 static const char *none = "(null)";
260 const char *astr = va_arg(valist, const char *);
261 const char *striter = astr ? astr : none;
268 if( IsDBCSLeadByte( *striter ) )
270 r = MultiByteToWideChar( CP_ACP, 0,
271 striter, n, str, len - written );
282 static const WCHAR none[] = { '(','n','u','l','l',')',0 };
283 const WCHAR *wstr = va_arg(valist, const WCHAR *);
284 const WCHAR *striter = wstr ? wstr : none;
287 if (written++ >= len)
296 if (written++ >= len)
298 *str++ = (WCHAR)va_arg(valist, int);
304 /* For non wc types, use system sprintf and append to wide char output */
305 /* FIXME: for unrecognised types, should ignore % when printing */
306 char *bufaiter = bufa;
308 sprintf(bufaiter, "%08lX", va_arg(valist, long));
313 if (*iter == 'a' || *iter == 'A' ||
314 *iter == 'e' || *iter == 'E' ||
315 *iter == 'f' || *iter == 'F' ||
316 *iter == 'g' || *iter == 'G')
317 sprintf(bufaiter, fmtbufa, va_arg(valist, double));
320 /* FIXME: On 32 bit systems this doesn't handle int 64's.
321 * on 64 bit systems this doesn't work for 32 bit types
323 sprintf(bufaiter, fmtbufa, va_arg(valist, void *));
328 if (written++ >= len)
330 *str++ = *bufaiter++;
344 /*********************************************************************
345 * swprintf (MSVCRT.@)
347 int MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
352 va_start( ap, format );
353 r = MSVCRT_vsnprintfW( str, INT_MAX, format, ap );
358 /*********************************************************************
359 * _vsnwprintf (MSVCRT.@)
361 int _vsnwprintf(MSVCRT_wchar_t *str, unsigned int len,
362 const MSVCRT_wchar_t *format, va_list valist)
364 return MSVCRT_vsnprintfW(str, len, format, valist);
367 /*********************************************************************
368 * vswprintf (MSVCRT.@)
370 int MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, va_list args )
372 return MSVCRT_vsnprintfW( str, INT_MAX, format, args );
375 /*********************************************************************
378 int MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
380 /* FIXME: handle collates */
381 return strcmpW( str1, str2 );
384 /*********************************************************************
387 MSVCRT_wchar_t* MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
389 const MSVCRT_wchar_t* p;
392 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
398 /*********************************************************************
401 INT MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
403 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
406 /*********************************************************************
407 * iswalnum (MSVCRT.@)
409 INT MSVCRT_iswalnum( MSVCRT_wchar_t wc )
411 return isalnumW( wc );
414 /*********************************************************************
415 * iswalpha (MSVCRT.@)
417 INT MSVCRT_iswalpha( MSVCRT_wchar_t wc )
419 return isalphaW( wc );
422 /*********************************************************************
423 * iswcntrl (MSVCRT.@)
425 INT MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
427 return iscntrlW( wc );
430 /*********************************************************************
431 * iswdigit (MSVCRT.@)
433 INT MSVCRT_iswdigit( MSVCRT_wchar_t wc )
435 return isdigitW( wc );
438 /*********************************************************************
439 * iswgraph (MSVCRT.@)
441 INT MSVCRT_iswgraph( MSVCRT_wchar_t wc )
443 return isgraphW( wc );
446 /*********************************************************************
447 * iswlower (MSVCRT.@)
449 INT MSVCRT_iswlower( MSVCRT_wchar_t wc )
451 return islowerW( wc );
454 /*********************************************************************
455 * iswprint (MSVCRT.@)
457 INT MSVCRT_iswprint( MSVCRT_wchar_t wc )
459 return isprintW( wc );
462 /*********************************************************************
463 * iswpunct (MSVCRT.@)
465 INT MSVCRT_iswpunct( MSVCRT_wchar_t wc )
467 return ispunctW( wc );
470 /*********************************************************************
471 * iswspace (MSVCRT.@)
473 INT MSVCRT_iswspace( MSVCRT_wchar_t wc )
475 return isspaceW( wc );
478 /*********************************************************************
479 * iswupper (MSVCRT.@)
481 INT MSVCRT_iswupper( MSVCRT_wchar_t wc )
483 return isupperW( wc );
486 /*********************************************************************
487 * iswxdigit (MSVCRT.@)
489 INT MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
491 return isxdigitW( wc );