Add real syntax doc for LABEL command.
[wine/multimedia.git] / dlls / msvcrt / wcs.c
blobc1c5b6b14fda71996a9b51bdaa4751c1bf36a922
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 MSVCRT_wchar_t* msvcrt_wstrndup(const MSVCRT_wchar_t *buf, unsigned int size)
40 MSVCRT_wchar_t* ret;
41 unsigned int len = strlenW(buf), max_len;
43 max_len = size <= len? size : len + 1;
45 ret = MSVCRT_malloc(max_len * sizeof (MSVCRT_wchar_t));
46 if (ret)
48 memcpy(ret,buf,max_len * sizeof (MSVCRT_wchar_t));
49 ret[max_len] = 0;
51 return ret;
54 /*********************************************************************
55 * _wcsdup (MSVCRT.@)
57 MSVCRT_wchar_t* _wcsdup( const MSVCRT_wchar_t* str )
59 MSVCRT_wchar_t* ret = NULL;
60 if (str)
62 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
63 ret = MSVCRT_malloc( size );
64 if (ret) memcpy( ret, str, size );
66 return ret;
69 /*********************************************************************
70 * _wcsicoll (MSVCRT.@)
72 INT _wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
74 /* FIXME: handle collates */
75 return strcmpiW( str1, str2 );
78 /*********************************************************************
79 * _wcsnset (MSVCRT.@)
81 MSVCRT_wchar_t* _wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
83 MSVCRT_wchar_t* ret = str;
84 while ((n-- > 0) && *str) *str++ = c;
85 return ret;
88 /*********************************************************************
89 * _wcsrev (MSVCRT.@)
91 MSVCRT_wchar_t* _wcsrev( MSVCRT_wchar_t* str )
93 MSVCRT_wchar_t* ret = str;
94 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
95 while (end > str)
97 MSVCRT_wchar_t t = *end;
98 *end-- = *str;
99 *str++ = t;
101 return ret;
104 /*********************************************************************
105 * _wcsset (MSVCRT.@)
107 MSVCRT_wchar_t* _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
109 MSVCRT_wchar_t* ret = str;
110 while (*str) *str++ = c;
111 return ret;
114 /*********************************************************************
115 * _vsnwprintf (MSVCRT.@)
117 int _vsnwprintf(MSVCRT_wchar_t *str, unsigned int len,
118 const MSVCRT_wchar_t *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 MSVCRT_wchar_t *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 != '%')
131 if (written++ >= len)
132 return -1;
133 *str++ = *iter++;
135 if (*iter == '%')
137 fmta = fmtbufa;
138 *fmta++ = *iter++;
139 while (*iter == '0' ||
140 *iter == '+' ||
141 *iter == '-' ||
142 *iter == ' ' ||
143 *iter == '0' ||
144 *iter == '*' ||
145 *iter == '#')
147 if (*iter == '*')
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 == '.')
165 *fmta++ = *iter++;
166 if (*iter == '*')
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 == 'h' ||
179 *iter == 'l')
180 *fmta++ = *iter++;
182 switch (*iter)
184 case 's':
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)
191 if (written++ >= len)
192 return -1;
193 *str++ = *striter++;
195 iter++;
196 break;
199 case 'c':
200 if (written++ >= len)
201 return -1;
202 *str++ = (MSVCRT_wchar_t)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 == 'p')
212 sprintf(bufaiter, "%08lX", va_arg(valist, long));
213 else
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 *));
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++ = 0;
237 return (int)written;
240 /*********************************************************************
241 * vswprintf (MSVCRT.@)
243 int MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, va_list args )
245 return _vsnwprintf( str, INT_MAX, format, args );
248 /*********************************************************************
249 * wcscoll (MSVCRT.@)
251 int MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
253 /* FIXME: handle collates */
254 return strcmpW( str1, str2 );
257 /*********************************************************************
258 * wcspbrk (MSVCRT.@)
260 MSVCRT_wchar_t* MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
262 const MSVCRT_wchar_t* p;
263 while (*str)
265 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
266 str++;
268 return NULL;
271 /*********************************************************************
272 * wctomb (MSVCRT.@)
274 INT MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
276 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
279 /*********************************************************************
280 * iswalnum (MSVCRT.@)
282 INT MSVCRT_iswalnum( MSVCRT_wchar_t wc )
284 return isalnumW( wc );
287 /*********************************************************************
288 * iswalpha (MSVCRT.@)
290 INT MSVCRT_iswalpha( MSVCRT_wchar_t wc )
292 return isalphaW( wc );
295 /*********************************************************************
296 * iswcntrl (MSVCRT.@)
298 INT MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
300 return iscntrlW( wc );
303 /*********************************************************************
304 * iswdigit (MSVCRT.@)
306 INT MSVCRT_iswdigit( MSVCRT_wchar_t wc )
308 return isdigitW( wc );
311 /*********************************************************************
312 * iswgraph (MSVCRT.@)
314 INT MSVCRT_iswgraph( MSVCRT_wchar_t wc )
316 return isgraphW( wc );
319 /*********************************************************************
320 * iswlower (MSVCRT.@)
322 INT MSVCRT_iswlower( MSVCRT_wchar_t wc )
324 return islowerW( wc );
327 /*********************************************************************
328 * iswprint (MSVCRT.@)
330 INT MSVCRT_iswprint( MSVCRT_wchar_t wc )
332 return isprintW( wc );
335 /*********************************************************************
336 * iswpunct (MSVCRT.@)
338 INT MSVCRT_iswpunct( MSVCRT_wchar_t wc )
340 return ispunctW( wc );
343 /*********************************************************************
344 * iswspace (MSVCRT.@)
346 INT MSVCRT_iswspace( MSVCRT_wchar_t wc )
348 return isspaceW( wc );
351 /*********************************************************************
352 * iswupper (MSVCRT.@)
354 INT MSVCRT_iswupper( MSVCRT_wchar_t wc )
356 return isupperW( wc );
359 /*********************************************************************
360 * iswxdigit (MSVCRT.@)
362 INT MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
364 return isxdigitW( wc );
367 /*********************************************************************
368 * _itow (MSVCRT.@)
370 MSVCRT_wchar_t* _itow(int value,MSVCRT_wchar_t* 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 MSVCRT_wchar_t* _ltow(long value,MSVCRT_wchar_t* 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;