Moved a few remaining 16-bit window functions to wnd16.c and moved it
[wine.git] / dlls / msvcrt / wcs.c
blob4a41cc7e2d307351b5eb065c0ddde1e4a703d21e
1 /*
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
21 #include <limits.h>
22 #include <stdio.h>
23 #include "msvcrt.h"
24 #include "winnls.h"
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)
40 WCHAR* ret;
41 unsigned int len = strlenW(buf), max_len;
43 max_len = size <= len? size : len + 1;
45 ret = MSVCRT_malloc(max_len * sizeof (WCHAR));
46 if (ret)
48 memcpy(ret,buf,max_len * sizeof (WCHAR));
49 ret[max_len] = 0;
51 return ret;
54 /*********************************************************************
55 * _wcsdup (MSVCRT.@)
57 WCHAR* _wcsdup( const WCHAR* str )
59 WCHAR* ret = NULL;
60 if (str)
62 int size = (strlenW(str) + 1) * sizeof(WCHAR);
63 ret = MSVCRT_malloc( size );
64 if (ret) memcpy( ret, str, size );
66 return ret;
69 /*********************************************************************
70 * _wcsicoll (MSVCRT.@)
72 INT _wcsicoll( const WCHAR* str1, const WCHAR* str2 )
74 /* FIXME: handle collates */
75 return strcmpiW( str1, str2 );
78 /*********************************************************************
79 * _wcsnset (MSVCRT.@)
81 WCHAR* _wcsnset( WCHAR* str, WCHAR c, MSVCRT_size_t n )
83 WCHAR* ret = str;
84 while ((n-- > 0) && *str) *str++ = c;
85 return ret;
88 /*********************************************************************
89 * _wcsrev (MSVCRT.@)
91 WCHAR* _wcsrev( WCHAR* str )
93 WCHAR* ret = str;
94 WCHAR* end = str + strlenW(str) - 1;
95 while (end > str)
97 WCHAR t = *end;
98 *end-- = *str;
99 *str++ = t;
101 return ret;
104 /*********************************************************************
105 * _wcsset (MSVCRT.@)
107 WCHAR* _wcsset( WCHAR* str, WCHAR c )
109 WCHAR* ret = str;
110 while (*str) *str++ = c;
111 return ret;
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));
127 while (*iter)
129 while (*iter && *iter != (WCHAR)L'%')
131 if (written++ >= len)
132 return -1;
133 *str++ = *iter++;
135 if (*iter == (WCHAR)L'%')
137 fmta = fmtbufa;
138 *fmta++ = *iter++;
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);
152 while (*buffiter)
153 *fmta++ = *buffiter++;
155 else
156 *fmta++ = *iter;
157 iter++;
160 while (isdigit(*iter))
161 *fmta++ = *iter++;
163 if (*iter == (WCHAR)L'.')
165 *fmta++ = *iter++;
166 if (*iter == (WCHAR)L'*')
168 char *buffiter = bufa;
169 int fieldlen = va_arg(valist, int);
170 sprintf(buffiter, "%d", fieldlen);
171 while (*buffiter)
172 *fmta++ = *buffiter++;
174 else
175 while (isdigit(*iter))
176 *fmta++ = *iter++;
178 if (*iter == (WCHAR)L'h' ||
179 *iter == (WCHAR)L'l')
180 *fmta++ = *iter++;
182 switch (*iter)
184 case (WCHAR)L's':
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;
189 while (*striter)
191 if (written++ >= len)
192 return -1;
193 *str++ = *striter++;
195 iter++;
196 break;
199 case (WCHAR)L'c':
200 if (written++ >= len)
201 return -1;
202 *str++ = (WCHAR)va_arg(valist, int);
203 iter++;
204 break;
206 default:
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));
213 else
215 *fmta++ = *iter;
216 *fmta = '\0';
217 if (*iter == (WCHAR)L'f')
218 sprintf(bufaiter, fmtbufa, va_arg(valist, double));
219 else
220 sprintf(bufaiter, fmtbufa, va_arg(valist, void *));
222 while (*bufaiter)
224 if (written++ >= len)
225 return -1;
226 *str++ = *bufaiter++;
228 iter++;
229 break;
234 if (written >= len)
235 return -1;
236 *str++ = (WCHAR)L'\0';
237 return (int)written;
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 /*********************************************************************
249 * wcscoll (MSVCRT.@)
251 int MSVCRT_wcscoll( const WCHAR* str1, const WCHAR* str2 )
253 /* FIXME: handle collates */
254 return strcmpW( str1, str2 );
257 /*********************************************************************
258 * wcspbrk (MSVCRT.@)
260 WCHAR* MSVCRT_wcspbrk( const WCHAR* str, const WCHAR* accept )
262 const WCHAR* p;
263 while (*str)
265 for (p = accept; *p; p++) if (*p == *str) return (WCHAR*)str;
266 str++;
268 return NULL;
271 /*********************************************************************
272 * wctomb (MSVCRT.@)
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 /*********************************************************************
368 * _itow (MSVCRT.@)
370 WCHAR* _itow(int value,WCHAR* out,int base)
372 char buf[64];
373 _itoa(value, buf, base);
374 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buf, -1, out, 128);
375 return out;
378 /*********************************************************************
379 * _ltow (MSVCRT.@)
381 WCHAR* _ltow(long value,WCHAR* out,int base)
383 char buf[128];
384 _ltoa(value, buf, base);
385 MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, buf, -1, out, 128);
386 return out;