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
25 #include "wine/unicode.h"
27 #include "msvcrt/stdio.h"
28 #include "msvcrt/stdlib.h"
29 #include "msvcrt/string.h"
30 #include "msvcrt/wctype.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37 /* INTERNAL: MSVCRT_malloc() based wstrndup */
38 WCHAR* msvcrt_wstrndup(LPCWSTR buf, unsigned int size)
41 unsigned int len = strlenW(buf), max_len;
43 max_len = size <= len? size : len + 1;
45 ret = MSVCRT_malloc(max_len * sizeof (WCHAR));
48 memcpy(ret,buf,max_len * sizeof (WCHAR));
54 /*********************************************************************
57 WCHAR* _wcsdup( const WCHAR* str )
62 int size = (strlenW(str) + 1) * sizeof(WCHAR);
63 ret = MSVCRT_malloc( size );
64 if (ret) memcpy( ret, str, size );
69 /*********************************************************************
70 * _wcsicoll (MSVCRT.@)
72 INT _wcsicoll( const WCHAR* str1, const WCHAR* str2 )
74 /* FIXME: handle collates */
75 return strcmpiW( str1, str2 );
78 /*********************************************************************
81 WCHAR* _wcsnset( WCHAR* str, WCHAR c, MSVCRT_size_t n )
84 while ((n-- > 0) && *str) *str++ = c;
88 /*********************************************************************
91 WCHAR* _wcsrev( WCHAR* str )
94 WCHAR* end = str + strlenW(str) - 1;
104 /*********************************************************************
107 WCHAR* _wcsset( WCHAR* str, WCHAR c )
110 while (*str) *str++ = c;
114 /*********************************************************************
115 * _vsnwprintf (MSVCRT.@)
117 int _vsnwprintf(WCHAR *str, unsigned int len,
118 const WCHAR *format, va_list valist)
120 /* If you fix a bug in this function, fix it in ntdll/wcstring.c also! */
121 unsigned int written = 0;
122 const WCHAR *iter = format;
123 char bufa[256], fmtbufa[64], *fmta;
125 TRACE("(%d,%s)\n",len,debugstr_w(format));
129 while (*iter && *iter != (WCHAR)L'%')
131 if (written++ >= len)
135 if (*iter == (WCHAR)L'%')
139 while (*iter == (WCHAR)L'0' ||
140 *iter == (WCHAR)L'+' ||
141 *iter == (WCHAR)L'-' ||
142 *iter == (WCHAR)L' ' ||
143 *iter == (WCHAR)L'0' ||
144 *iter == (WCHAR)L'*' ||
145 *iter == (WCHAR)L'#')
147 if (*iter == (WCHAR)L'*')
149 char *buffiter = bufa;
150 int fieldlen = va_arg(valist, int);
151 sprintf(buffiter, "%d", fieldlen);
153 *fmta++ = *buffiter++;
160 while (isdigit(*iter))
163 if (*iter == (WCHAR)L'.')
166 if (*iter == (WCHAR)L'*')
168 char *buffiter = bufa;
169 int fieldlen = va_arg(valist, int);
170 sprintf(buffiter, "%d", fieldlen);
172 *fmta++ = *buffiter++;
175 while (isdigit(*iter))
178 if (*iter == (WCHAR)L'h' ||
179 *iter == (WCHAR)L'l')
186 static const WCHAR none[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
187 const WCHAR *wstr = va_arg(valist, const WCHAR *);
188 const WCHAR *striter = wstr ? wstr : none;
191 if (written++ >= len)
200 if (written++ >= len)
202 *str++ = (WCHAR)va_arg(valist, int);
208 /* For non wc types, use system sprintf and append to wide char output */
209 /* FIXME: for unrecognised types, should ignore % when printing */
210 char *bufaiter = bufa;
211 if (*iter == (WCHAR)L'p')
212 sprintf(bufaiter, "%08lX", va_arg(valist, long));
217 if (*iter == (WCHAR)L'f')
218 sprintf(bufaiter, fmtbufa, va_arg(valist, double));
220 sprintf(bufaiter, fmtbufa, va_arg(valist, void *));
224 if (written++ >= len)
226 *str++ = *bufaiter++;
236 *str++ = (WCHAR)L'\0';
240 /*********************************************************************
241 * vswprintf (MSVCRT.@)
243 int MSVCRT_vswprintf( WCHAR* str, const WCHAR* format, va_list args )
245 return _vsnwprintf( str, INT_MAX, format, args );
248 /*********************************************************************
251 int MSVCRT_wcscoll( const WCHAR* str1, const WCHAR* str2 )
253 /* FIXME: handle collates */
254 return strcmpW( str1, str2 );
257 /*********************************************************************
260 WCHAR* MSVCRT_wcspbrk( const WCHAR* str, const WCHAR* accept )
265 for (p = accept; *p; p++) if (*p == *str) return (WCHAR*)str;
271 /*********************************************************************
274 INT MSVCRT_wctomb( char *dst, WCHAR ch )
276 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
279 /*********************************************************************
280 * iswalnum (MSVCRT.@)
282 INT MSVCRT_iswalnum( WCHAR wc )
284 return isalnumW( wc );
287 /*********************************************************************
288 * iswalpha (MSVCRT.@)
290 INT MSVCRT_iswalpha( WCHAR wc )
292 return isalphaW( wc );
295 /*********************************************************************
296 * iswcntrl (MSVCRT.@)
298 INT MSVCRT_iswcntrl( WCHAR wc )
300 return iscntrlW( wc );
303 /*********************************************************************
304 * iswdigit (MSVCRT.@)
306 INT MSVCRT_iswdigit( WCHAR wc )
308 return isdigitW( wc );
311 /*********************************************************************
312 * iswgraph (MSVCRT.@)
314 INT MSVCRT_iswgraph( WCHAR wc )
316 return isgraphW( wc );
319 /*********************************************************************
320 * iswlower (MSVCRT.@)
322 INT MSVCRT_iswlower( WCHAR wc )
324 return islowerW( wc );
327 /*********************************************************************
328 * iswprint (MSVCRT.@)
330 INT MSVCRT_iswprint( WCHAR wc )
332 return isprintW( wc );
335 /*********************************************************************
336 * iswpunct (MSVCRT.@)
338 INT MSVCRT_iswpunct( WCHAR wc )
340 return ispunctW( wc );
343 /*********************************************************************
344 * iswspace (MSVCRT.@)
346 INT MSVCRT_iswspace( WCHAR wc )
348 return isspaceW( wc );
351 /*********************************************************************
352 * iswupper (MSVCRT.@)
354 INT MSVCRT_iswupper( WCHAR wc )
356 return isupperW( wc );
359 /*********************************************************************
360 * iswxdigit (MSVCRT.@)
362 INT MSVCRT_iswxdigit( WCHAR wc )
364 return isxdigitW( wc );
367 /*********************************************************************
370 WCHAR* _itow(int value,WCHAR* out,int base)
373 _itoa(value, buf, base);
374 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buf, -1, out, 128);
378 /*********************************************************************
381 WCHAR* _ltow(long value,WCHAR* out,int base)
384 _ltoa(value, buf, base);
385 MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, buf, -1, out, 128);