2 * msvcrt.dll wide-char functions
4 * Copyright 1999 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
11 #include "wine/unicode.h"
13 #include "msvcrt/stdio.h"
14 #include "msvcrt/stdlib.h"
15 #include "msvcrt/string.h"
16 #include "msvcrt/wctype.h"
18 DEFAULT_DEBUG_CHANNEL(msvcrt
);
21 /* INTERNAL: MSVCRT_malloc() based wstrndup */
22 WCHAR
* msvcrt_wstrndup(LPCWSTR buf
, unsigned int size
)
25 unsigned int len
= strlenW(buf
), max_len
;
27 max_len
= size
<= len
? size
: len
+ 1;
29 ret
= MSVCRT_malloc(max_len
* sizeof (WCHAR
));
32 memcpy(ret
,buf
,max_len
* sizeof (WCHAR
));
38 /*********************************************************************
41 WCHAR
* _wcsdup( const WCHAR
* str
)
46 int size
= (strlenW(str
) + 1) * sizeof(WCHAR
);
47 ret
= MSVCRT_malloc( size
);
48 if (ret
) memcpy( ret
, str
, size
);
53 /*********************************************************************
54 * _wcsicoll (MSVCRT.@)
56 INT
_wcsicoll( const WCHAR
* str1
, const WCHAR
* str2
)
58 /* FIXME: handle collates */
59 return strcmpiW( str1
, str2
);
62 /*********************************************************************
65 WCHAR
* _wcsnset( WCHAR
* str
, WCHAR c
, MSVCRT_size_t n
)
68 while ((n
-- > 0) && *str
) *str
++ = c
;
72 /*********************************************************************
75 WCHAR
* _wcsrev( WCHAR
* str
)
78 WCHAR
* end
= str
+ strlenW(str
) - 1;
88 /*********************************************************************
91 WCHAR
* _wcsset( WCHAR
* str
, WCHAR c
)
94 while (*str
) *str
++ = c
;
98 /*********************************************************************
99 * _vsnwprintf (MSVCRT.@)
101 int _vsnwprintf(WCHAR
*str
, unsigned int len
,
102 const WCHAR
*format
, va_list valist
)
104 /* If you fix a bug in this function, fix it in ntdll/wcstring.c also! */
105 unsigned int written
= 0;
106 const WCHAR
*iter
= format
;
107 char bufa
[256], fmtbufa
[64], *fmta
;
109 TRACE("(%d,%s)\n",len
,debugstr_w(format
));
113 while (*iter
&& *iter
!= (WCHAR
)L
'%')
115 if (written
++ >= len
)
119 if (*iter
== (WCHAR
)L
'%')
123 while (*iter
== (WCHAR
)L
'0' ||
124 *iter
== (WCHAR
)L
'+' ||
125 *iter
== (WCHAR
)L
'-' ||
126 *iter
== (WCHAR
)L
' ' ||
127 *iter
== (WCHAR
)L
'0' ||
128 *iter
== (WCHAR
)L
'*' ||
129 *iter
== (WCHAR
)L
'#')
131 if (*iter
== (WCHAR
)L
'*')
133 char *buffiter
= bufa
;
134 int fieldlen
= va_arg(valist
, int);
135 sprintf(buffiter
, "%d", fieldlen
);
137 *fmta
++ = *buffiter
++;
144 while (isdigit(*iter
))
147 if (*iter
== (WCHAR
)L
'.')
150 if (*iter
== (WCHAR
)L
'*')
152 char *buffiter
= bufa
;
153 int fieldlen
= va_arg(valist
, int);
154 sprintf(buffiter
, "%d", fieldlen
);
156 *fmta
++ = *buffiter
++;
159 while (isdigit(*iter
))
162 if (*iter
== (WCHAR
)L
'h' ||
163 *iter
== (WCHAR
)L
'l')
173 static const WCHAR none
[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
174 const WCHAR
*wstr
= va_arg(valist
, const WCHAR
*);
175 const WCHAR
*striter
= wstr
? wstr
: none
;
178 if (written
++ >= len
)
187 if (written
++ >= len
)
189 *str
++ = (WCHAR
)va_arg(valist
, int);
195 /* For non wc types, use system sprintf and append to wide char output */
196 /* FIXME: for unrecognised types, should ignore % when printing */
197 char *bufaiter
= bufa
;
198 if (*iter
== (WCHAR
)L
'p')
199 sprintf(bufaiter
, "%08lX", va_arg(valist
, long));
204 if (*iter
== (WCHAR
)L
'f')
205 sprintf(bufaiter
, fmtbufa
, va_arg(valist
, double));
207 sprintf(bufaiter
, fmtbufa
, va_arg(valist
, void *));
211 if (written
++ >= len
)
213 *str
++ = *bufaiter
++;
223 *str
++ = (WCHAR
)L
'\0';
227 /*********************************************************************
228 * vswprintf (MSVCRT.@)
230 int MSVCRT_vswprintf( WCHAR
* str
, const WCHAR
* format
, va_list args
)
232 return _vsnwprintf( str
, INT_MAX
, format
, args
);
235 /*********************************************************************
238 int MSVCRT_wcscoll( const WCHAR
* str1
, const WCHAR
* str2
)
240 /* FIXME: handle collates */
241 return strcmpW( str1
, str2
);
244 /*********************************************************************
247 WCHAR
* MSVCRT_wcspbrk( const WCHAR
* str
, const WCHAR
* accept
)
252 for (p
= accept
; *p
; p
++) if (*p
== *str
) return (WCHAR
*)str
;
258 /*********************************************************************
261 INT
MSVCRT_wctomb( char *dst
, WCHAR ch
)
263 return WideCharToMultiByte( CP_ACP
, 0, &ch
, 1, dst
, 6, NULL
, NULL
);
266 /*********************************************************************
267 * iswalnum (MSVCRT.@)
269 INT
MSVCRT_iswalnum( WCHAR wc
)
271 return isalnumW( wc
);
274 /*********************************************************************
275 * iswalpha (MSVCRT.@)
277 INT
MSVCRT_iswalpha( WCHAR wc
)
279 return isalphaW( wc
);
282 /*********************************************************************
283 * iswcntrl (MSVCRT.@)
285 INT
MSVCRT_iswcntrl( WCHAR wc
)
287 return iscntrlW( wc
);
290 /*********************************************************************
291 * iswdigit (MSVCRT.@)
293 INT
MSVCRT_iswdigit( WCHAR wc
)
295 return isdigitW( wc
);
298 /*********************************************************************
299 * iswgraph (MSVCRT.@)
301 INT
MSVCRT_iswgraph( WCHAR wc
)
303 return isgraphW( wc
);
306 /*********************************************************************
307 * iswlower (MSVCRT.@)
309 INT
MSVCRT_iswlower( WCHAR wc
)
311 return islowerW( wc
);
314 /*********************************************************************
315 * iswprint (MSVCRT.@)
317 INT
MSVCRT_iswprint( WCHAR wc
)
319 return isprintW( wc
);
322 /*********************************************************************
323 * iswpunct (MSVCRT.@)
325 INT
MSVCRT_iswpunct( WCHAR wc
)
327 return ispunctW( wc
);
330 /*********************************************************************
331 * iswspace (MSVCRT.@)
333 INT
MSVCRT_iswspace( WCHAR wc
)
335 return isspaceW( wc
);
338 /*********************************************************************
339 * iswupper (MSVCRT.@)
341 INT
MSVCRT_iswupper( WCHAR wc
)
343 return isupperW( wc
);
346 /*********************************************************************
347 * iswxdigit (MSVCRT.@)
349 INT
MSVCRT_iswxdigit( WCHAR wc
)
351 return isxdigitW( wc
);
354 /*********************************************************************
357 WCHAR
* _itow(int value
,WCHAR
* out
,int base
)
360 _itoa(value
, buf
, base
);
361 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, buf
, -1, out
, 128);
365 /*********************************************************************
368 WCHAR
* _ltow(long value
,WCHAR
* out
,int base
)
371 _ltoa(value
, buf
, base
);
372 MultiByteToWideChar (CP_ACP
, MB_PRECOMPOSED
, buf
, -1, out
, 128);