1 | /*
|
---|
2 | * msvcrt.dll wide-char functions
|
---|
3 | *
|
---|
4 | * Copyright 1999 Alexandre Julliard
|
---|
5 | * Copyright 2000 Jon Griffiths
|
---|
6 | *
|
---|
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.
|
---|
11 | *
|
---|
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.
|
---|
16 | *
|
---|
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
|
---|
20 | */
|
---|
21 | #include <limits.h>
|
---|
22 | #include <stdio.h>
|
---|
23 | #include "msvcrt.h"
|
---|
24 | #include "winnls.h"
|
---|
25 | #include "wine/unicode.h"
|
---|
26 |
|
---|
27 | #include "msvcrt/stdio.h"
|
---|
28 | #include "msvcrt/stdlib.h"
|
---|
29 | #include "msvcrt/string.h"
|
---|
30 | #include "msvcrt/wctype.h"
|
---|
31 |
|
---|
32 | #include "wine/debug.h"
|
---|
33 |
|
---|
34 | WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
---|
35 |
|
---|
36 |
|
---|
37 | /* INTERNAL: MSVCRT_malloc() based wstrndup */
|
---|
38 | MSVCRT_wchar_t* msvcrt_wstrndup(const MSVCRT_wchar_t *buf, unsigned int size)
|
---|
39 | {
|
---|
40 | MSVCRT_wchar_t* ret;
|
---|
41 | unsigned int len = strlenW(buf), max_len;
|
---|
42 |
|
---|
43 | max_len = size <= len? size : len + 1;
|
---|
44 |
|
---|
45 | ret = MSVCRT_malloc(max_len * sizeof (MSVCRT_wchar_t));
|
---|
46 | if (ret)
|
---|
47 | {
|
---|
48 | memcpy(ret,buf,max_len * sizeof (MSVCRT_wchar_t));
|
---|
49 | ret[max_len] = 0;
|
---|
50 | }
|
---|
51 | return ret;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /*********************************************************************
|
---|
55 | * _wcsdup (MSVCRT.@)
|
---|
56 | */
|
---|
57 | MSVCRT_wchar_t* _wcsdup( const MSVCRT_wchar_t* str )
|
---|
58 | {
|
---|
59 | MSVCRT_wchar_t* ret = NULL;
|
---|
60 | if (str)
|
---|
61 | {
|
---|
62 | int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
|
---|
63 | ret = MSVCRT_malloc( size );
|
---|
64 | if (ret) memcpy( ret, str, size );
|
---|
65 | }
|
---|
66 | return ret;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /*********************************************************************
|
---|
70 | * _wcsicoll (MSVCRT.@)
|
---|
71 | */
|
---|
72 | INT _wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
|
---|
73 | {
|
---|
74 | /* FIXME: handle collates */
|
---|
75 | return strcmpiW( str1, str2 );
|
---|
76 | }
|
---|
77 |
|
---|
78 | /*********************************************************************
|
---|
79 | * _wcsnset (MSVCRT.@)
|
---|
80 | */
|
---|
81 | MSVCRT_wchar_t* _wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
|
---|
82 | {
|
---|
83 | MSVCRT_wchar_t* ret = str;
|
---|
84 | while ((n-- > 0) && *str) *str++ = c;
|
---|
85 | return ret;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /*********************************************************************
|
---|
89 | * _wcsrev (MSVCRT.@)
|
---|
90 | */
|
---|
91 | MSVCRT_wchar_t* _wcsrev( MSVCRT_wchar_t* str )
|
---|
92 | {
|
---|
93 | MSVCRT_wchar_t* ret = str;
|
---|
94 | MSVCRT_wchar_t* end = str + strlenW(str) - 1;
|
---|
95 | while (end > str)
|
---|
96 | {
|
---|
97 | MSVCRT_wchar_t t = *end;
|
---|
98 | *end-- = *str;
|
---|
99 | *str++ = t;
|
---|
100 | }
|
---|
101 | return ret;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*********************************************************************
|
---|
105 | * _wcsset (MSVCRT.@)
|
---|
106 | */
|
---|
107 | MSVCRT_wchar_t* _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
|
---|
108 | {
|
---|
109 | MSVCRT_wchar_t* ret = str;
|
---|
110 | while (*str) *str++ = c;
|
---|
111 | return ret;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /*********************************************************************
|
---|
115 | * _vsnwprintf (MSVCRT.@)
|
---|
116 | */
|
---|
117 | int _vsnwprintf(MSVCRT_wchar_t *str, unsigned int len,
|
---|
118 | const MSVCRT_wchar_t *format, va_list valist)
|
---|
119 | {
|
---|
120 | /* If you fix a bug in this function, fix it in ntdll/wcstring.c also! */
|
---|
121 | unsigned int written = 0;
|
---|
122 | const MSVCRT_wchar_t *iter = format;
|
---|
123 | char bufa[256], fmtbufa[64], *fmta;
|
---|
124 |
|
---|
125 | TRACE("(%d,%s)\n",len,debugstr_w(format));
|
---|
126 |
|
---|
127 | while (*iter)
|
---|
128 | {
|
---|
129 | while (*iter && *iter != '%')
|
---|
130 | {
|
---|
131 | if (written++ >= len)
|
---|
132 | return -1;
|
---|
133 | *str++ = *iter++;
|
---|
134 | }
|
---|
135 | if (*iter == '%')
|
---|
136 | {
|
---|
137 | fmta = fmtbufa;
|
---|
138 | *fmta++ = *iter++;
|
---|
139 | while (*iter == '0' ||
|
---|
140 | *iter == '+' ||
|
---|
141 | *iter == '-' ||
|
---|
142 | *iter == ' ' ||
|
---|
143 | *iter == '0' ||
|
---|
144 | *iter == '*' ||
|
---|
145 | *iter == '#')
|
---|
146 | {
|
---|
147 | if (*iter == '*')
|
---|
148 | {
|
---|
149 | char *buffiter = bufa;
|
---|
150 | int fieldlen = va_arg(valist, int);
|
---|
151 | sprintf(buffiter, "%d", fieldlen);
|
---|
152 | while (*buffiter)
|
---|
153 | *fmta++ = *buffiter++;
|
---|
154 | }
|
---|
155 | else
|
---|
156 | *fmta++ = *iter;
|
---|
157 | iter++;
|
---|
158 | }
|
---|
159 |
|
---|
160 | while (isdigit(*iter))
|
---|
161 | *fmta++ = *iter++;
|
---|
162 |
|
---|
163 | if (*iter == '.')
|
---|
164 | {
|
---|
165 | *fmta++ = *iter++;
|
---|
166 | if (*iter == '*')
|
---|
167 | {
|
---|
168 | char *buffiter = bufa;
|
---|
169 | int fieldlen = va_arg(valist, int);
|
---|
170 | sprintf(buffiter, "%d", fieldlen);
|
---|
171 | while (*buffiter)
|
---|
172 | *fmta++ = *buffiter++;
|
---|
173 | }
|
---|
174 | else
|
---|
175 | while (isdigit(*iter))
|
---|
176 | *fmta++ = *iter++;
|
---|
177 | }
|
---|
178 | if (*iter == 'h' ||
|
---|
179 | *iter == 'l')
|
---|
180 | *fmta++ = *iter++;
|
---|
181 |
|
---|
182 | switch (*iter)
|
---|
183 | {
|
---|
184 | case 's':
|
---|
185 | {
|
---|
186 | static const MSVCRT_wchar_t none[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
|
---|
187 | const MSVCRT_wchar_t *wstr = va_arg(valist, const MSVCRT_wchar_t *);
|
---|
188 | const MSVCRT_wchar_t *striter = wstr ? wstr : none;
|
---|
189 | while (*striter)
|
---|
190 | {
|
---|
191 | if (written++ >= len)
|
---|
192 | return -1;
|
---|
193 | *str++ = *striter++;
|
---|
194 | }
|
---|
195 | iter++;
|
---|
196 | break;
|
---|
197 | }
|
---|
198 |
|
---|
199 | case 'c':
|
---|
200 | if (written++ >= len)
|
---|
201 | return -1;
|
---|
202 | *str++ = (MSVCRT_wchar_t)va_arg(valist, int);
|
---|
203 | iter++;
|
---|
204 | break;
|
---|
205 |
|
---|
206 | default:
|
---|
207 | {
|
---|
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 == 'p')
|
---|
212 | sprintf(bufaiter, "%08lX", va_arg(valist, long));
|
---|
213 | else
|
---|
214 | {
|
---|
215 | *fmta++ = *iter;
|
---|
216 | *fmta = '\0';
|
---|
217 | if (*iter == 'f')
|
---|
218 | sprintf(bufaiter, fmtbufa, va_arg(valist, double));
|
---|
219 | else
|
---|
220 | sprintf(bufaiter, fmtbufa, va_arg(valist, void *));
|
---|
221 | }
|
---|
222 | while (*bufaiter)
|
---|
223 | {
|
---|
224 | if (written++ >= len)
|
---|
225 | return -1;
|
---|
226 | *str++ = *bufaiter++;
|
---|
227 | }
|
---|
228 | iter++;
|
---|
229 | break;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 | if (written >= len)
|
---|
235 | return -1;
|
---|
236 | *str++ = 0;
|
---|
237 | return (int)written;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /*********************************************************************
|
---|
241 | * vswprintf (MSVCRT.@)
|
---|
242 | */
|
---|
243 | int MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, va_list args )
|
---|
244 | {
|
---|
245 | return _vsnwprintf( str, INT_MAX, format, args );
|
---|
246 | }
|
---|
247 |
|
---|
248 | /*********************************************************************
|
---|
249 | * wcscoll (MSVCRT.@)
|
---|
250 | */
|
---|
251 | int MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
|
---|
252 | {
|
---|
253 | /* FIXME: handle collates */
|
---|
254 | return strcmpW( str1, str2 );
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*********************************************************************
|
---|
258 | * wcspbrk (MSVCRT.@)
|
---|
259 | */
|
---|
260 | MSVCRT_wchar_t* MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
|
---|
261 | {
|
---|
262 | const MSVCRT_wchar_t* p;
|
---|
263 | while (*str)
|
---|
264 | {
|
---|
265 | for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
|
---|
266 | str++;
|
---|
267 | }
|
---|
268 | return NULL;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /*********************************************************************
|
---|
272 | * wctomb (MSVCRT.@)
|
---|
273 | */
|
---|
274 | INT MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
|
---|
275 | {
|
---|
276 | return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
|
---|
277 | }
|
---|
278 |
|
---|
279 | /*********************************************************************
|
---|
280 | * iswalnum (MSVCRT.@)
|
---|
281 | */
|
---|
282 | INT MSVCRT_iswalnum( MSVCRT_wchar_t wc )
|
---|
283 | {
|
---|
284 | return isalnumW( wc );
|
---|
285 | }
|
---|
286 |
|
---|
287 | /*********************************************************************
|
---|
288 | * iswalpha (MSVCRT.@)
|
---|
289 | */
|
---|
290 | INT MSVCRT_iswalpha( MSVCRT_wchar_t wc )
|
---|
291 | {
|
---|
292 | return isalphaW( wc );
|
---|
293 | }
|
---|
294 |
|
---|
295 | /*********************************************************************
|
---|
296 | * iswcntrl (MSVCRT.@)
|
---|
297 | */
|
---|
298 | INT MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
|
---|
299 | {
|
---|
300 | return iscntrlW( wc );
|
---|
301 | }
|
---|
302 |
|
---|
303 | /*********************************************************************
|
---|
304 | * iswdigit (MSVCRT.@)
|
---|
305 | */
|
---|
306 | INT MSVCRT_iswdigit( MSVCRT_wchar_t wc )
|
---|
307 | {
|
---|
308 | return isdigitW( wc );
|
---|
309 | }
|
---|
310 |
|
---|
311 | /*********************************************************************
|
---|
312 | * iswgraph (MSVCRT.@)
|
---|
313 | */
|
---|
314 | INT MSVCRT_iswgraph( MSVCRT_wchar_t wc )
|
---|
315 | {
|
---|
316 | return isgraphW( wc );
|
---|
317 | }
|
---|
318 |
|
---|
319 | /*********************************************************************
|
---|
320 | * iswlower (MSVCRT.@)
|
---|
321 | */
|
---|
322 | INT MSVCRT_iswlower( MSVCRT_wchar_t wc )
|
---|
323 | {
|
---|
324 | return islowerW( wc );
|
---|
325 | }
|
---|
326 |
|
---|
327 | /*********************************************************************
|
---|
328 | * iswprint (MSVCRT.@)
|
---|
329 | */
|
---|
330 | INT MSVCRT_iswprint( MSVCRT_wchar_t wc )
|
---|
331 | {
|
---|
332 | return isprintW( wc );
|
---|
333 | }
|
---|
334 |
|
---|
335 | /*********************************************************************
|
---|
336 | * iswpunct (MSVCRT.@)
|
---|
337 | */
|
---|
338 | INT MSVCRT_iswpunct( MSVCRT_wchar_t wc )
|
---|
339 | {
|
---|
340 | return ispunctW( wc );
|
---|
341 | }
|
---|
342 |
|
---|
343 | /*********************************************************************
|
---|
344 | * iswspace (MSVCRT.@)
|
---|
345 | */
|
---|
346 | INT MSVCRT_iswspace( MSVCRT_wchar_t wc )
|
---|
347 | {
|
---|
348 | return isspaceW( wc );
|
---|
349 | }
|
---|
350 |
|
---|
351 | /*********************************************************************
|
---|
352 | * iswupper (MSVCRT.@)
|
---|
353 | */
|
---|
354 | INT MSVCRT_iswupper( MSVCRT_wchar_t wc )
|
---|
355 | {
|
---|
356 | return isupperW( wc );
|
---|
357 | }
|
---|
358 |
|
---|
359 | /*********************************************************************
|
---|
360 | * iswxdigit (MSVCRT.@)
|
---|
361 | */
|
---|
362 | INT MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
|
---|
363 | {
|
---|
364 | return isxdigitW( wc );
|
---|
365 | }
|
---|
366 |
|
---|
367 | /*********************************************************************
|
---|
368 | * _itow (MSVCRT.@)
|
---|
369 | */
|
---|
370 | MSVCRT_wchar_t* _itow(int value,MSVCRT_wchar_t* out,int base)
|
---|
371 | {
|
---|
372 | char buf[64];
|
---|
373 | _itoa(value, buf, base);
|
---|
374 | MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buf, -1, out, 128);
|
---|
375 | return out;
|
---|
376 | }
|
---|
377 |
|
---|
378 | /*********************************************************************
|
---|
379 | * _ltow (MSVCRT.@)
|
---|
380 | */
|
---|
381 | MSVCRT_wchar_t* _ltow(long value,MSVCRT_wchar_t* out,int base)
|
---|
382 | {
|
---|
383 | char buf[128];
|
---|
384 | _ltoa(value, buf, base);
|
---|
385 | MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, buf, -1, out, 128);
|
---|
386 | return out;
|
---|
387 | }
|
---|