Fix msvcrt symbol demangling for non MS symbols.
[wine/multimedia.git] / dlls / msvcrt / wcs.c
blobc88cf02b17a0b2e41d8eb42dc769b26c69fbd66a
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 <math.h>
24 #include <assert.h>
25 #include "msvcrt.h"
26 #include "winnls.h"
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33 /* INTERNAL: MSVCRT_malloc() based wstrndup */
34 MSVCRT_wchar_t* msvcrt_wstrndup(const MSVCRT_wchar_t *buf, unsigned int size)
36 MSVCRT_wchar_t* ret;
37 unsigned int len = strlenW(buf), max_len;
39 max_len = size <= len? size : len + 1;
41 ret = MSVCRT_malloc(max_len * sizeof (MSVCRT_wchar_t));
42 if (ret)
44 memcpy(ret,buf,max_len * sizeof (MSVCRT_wchar_t));
45 ret[max_len] = 0;
47 return ret;
50 /*********************************************************************
51 * _wcsdup (MSVCRT.@)
53 MSVCRT_wchar_t* _wcsdup( const MSVCRT_wchar_t* str )
55 MSVCRT_wchar_t* ret = NULL;
56 if (str)
58 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
59 ret = MSVCRT_malloc( size );
60 if (ret) memcpy( ret, str, size );
62 return ret;
65 /*********************************************************************
66 * _wcsicoll (MSVCRT.@)
68 INT _wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
70 /* FIXME: handle collates */
71 return strcmpiW( str1, str2 );
74 /*********************************************************************
75 * _wcsnset (MSVCRT.@)
77 MSVCRT_wchar_t* _wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
79 MSVCRT_wchar_t* ret = str;
80 while ((n-- > 0) && *str) *str++ = c;
81 return ret;
84 /*********************************************************************
85 * _wcsrev (MSVCRT.@)
87 MSVCRT_wchar_t* _wcsrev( MSVCRT_wchar_t* str )
89 MSVCRT_wchar_t* ret = str;
90 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
91 while (end > str)
93 MSVCRT_wchar_t t = *end;
94 *end-- = *str;
95 *str++ = t;
97 return ret;
100 /*********************************************************************
101 * _wcsset (MSVCRT.@)
103 MSVCRT_wchar_t* _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
105 MSVCRT_wchar_t* ret = str;
106 while (*str) *str++ = c;
107 return ret;
110 /*********************************************************************
111 * wcstod (MSVCRT.@)
113 double MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
115 const MSVCRT_wchar_t* str = lpszStr;
116 int negative = 0;
117 double ret = 0, divisor = 10.0;
119 TRACE("(%s,%p) semi-stub\n", debugstr_w(lpszStr), end);
121 /* FIXME:
122 * - Should set errno on failure
123 * - Should fail on overflow
124 * - Need to check which input formats are allowed
126 while (isspaceW(*str))
127 str++;
129 if (*str == '-')
131 negative = 1;
132 str++;
135 while (isdigitW(*str))
137 ret = ret * 10.0 + (*str - '0');
138 str++;
140 if (*str == '.')
141 str++;
142 while (isdigitW(*str))
144 ret = ret + (*str - '0') / divisor;
145 divisor *= 10;
146 str++;
149 if (*str == 'E' || *str == 'e' || *str == 'D' || *str == 'd')
151 int negativeExponent = 0;
152 int exponent = 0;
153 if (*(++str) == '-')
155 negativeExponent = 1;
156 str++;
158 while (isdigitW(*str))
160 exponent = exponent * 10 + (*str - '0');
161 str++;
163 if (exponent != 0)
165 if (negativeExponent)
166 ret = ret / pow(10.0, exponent);
167 else
168 ret = ret * pow(10.0, exponent);
172 if (negative)
173 ret = -ret;
175 if (end)
176 *end = (MSVCRT_wchar_t*)str;
178 TRACE("returning %g\n", ret);
179 return ret;
183 typedef struct pf_output_t
185 int used;
186 int len;
187 BOOL unicode;
188 union {
189 LPWSTR W;
190 LPSTR A;
191 } buf;
192 } pf_output;
194 typedef struct pf_flags_t
196 char Sign, LeftAlign, Alternate, PadZero;
197 char FieldLength, Precision;
198 char IntegerLength, IntegerDouble;
199 char WideString;
200 char Format;
201 } pf_flags;
204 * writes a string of characters to the output
205 * returns -1 if the string doesn't fit in the output buffer
206 * return the length of the string if all characters were written
208 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
210 int space = out->len - out->used;
212 if( len < 0 )
213 len = strlenW( str );
214 if( out->unicode )
216 LPWSTR p = out->buf.W + out->used;
218 if( space >= len )
220 memcpy( p, str, len*sizeof(WCHAR) );
221 out->used += len;
222 return len;
224 if( space > 0 )
225 memcpy( p, str, space*sizeof(WCHAR) );
226 out->used += len;
228 else
230 int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
231 LPSTR p = out->buf.A + out->used;
233 if( space >= n )
235 WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
236 out->used += n;
237 return len;
239 if( space > 0 )
240 WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
241 out->used += n;
243 return -1;
246 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
248 int space = out->len - out->used;
250 if( len < 0 )
251 len = strlen( str );
252 if( !out->unicode )
254 LPSTR p = out->buf.A + out->used;
256 if( space >= len )
258 memcpy( p, str, len );
259 out->used += len;
260 return len;
262 if( space > 0 )
263 memcpy( p, str, space );
264 out->used += len;
266 else
268 int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
269 LPWSTR p = out->buf.W + out->used;
271 if( space >= n )
273 MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
274 out->used += n;
275 return len;
277 if( space > 0 )
278 MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
279 out->used += n;
281 return -1;
284 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
286 int i, r = 0;
288 if( ( !left && flags->LeftAlign ) ||
289 ( left && !flags->LeftAlign ) ||
290 ( left && flags->PadZero ) )
292 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
294 if( flags->PadZero )
295 r = pf_output_stringA( out, "0", 1 );
296 else
297 r = pf_output_stringA( out, " ", 1 );
301 return r;
304 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
305 int len, pf_flags *flags )
307 int r = 0;
309 if( len < 0 )
310 len = strlenW( str );
312 if (flags->Precision && flags->Precision < len)
313 len = flags->Precision;
315 r = pf_fill( out, len, flags, 1 );
317 if( r>=0 )
318 r = pf_output_stringW( out, str, len );
320 if( r>=0 )
321 r = pf_fill( out, len, flags, 0 );
323 return r;
326 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
327 int len, pf_flags *flags )
329 int r = 0;
331 if( len < 0 )
332 len = strlen( str );
334 if (flags->Precision && flags->Precision < len)
335 len = flags->Precision;
337 r = pf_fill( out, len, flags, 1 );
339 if( r>=0 )
340 r = pf_output_stringA( out, str, len );
342 if( r>=0 )
343 r = pf_fill( out, len, flags, 0 );
345 return r;
348 static inline BOOL pf_is_double_format( char fmt )
350 char float_fmts[] = "aefg";
351 if (!fmt)
352 return FALSE;
353 fmt = tolower( fmt );
354 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
357 static inline BOOL pf_is_valid_format( char fmt )
359 char float_fmts[] = "acdefginoux";
360 if (!fmt)
361 return FALSE;
362 fmt = tolower( fmt );
363 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
366 static void pf_rebuild_format_string( char *p, pf_flags *flags )
368 *p++ = '%';
369 if( flags->Sign )
370 *p++ = flags->Sign;
371 if( flags->LeftAlign )
372 *p++ = flags->LeftAlign;
373 if( flags->Alternate )
374 *p++ = flags->Alternate;
375 if( flags->PadZero )
376 *p++ = flags->PadZero;
377 if( flags->FieldLength )
379 sprintf(p, "%d", flags->FieldLength);
380 p += strlen(p);
382 if( flags->Precision )
384 sprintf(p, ".%d", flags->Precision);
385 p += strlen(p);
387 *p++ = flags->Format;
388 *p++ = 0;
391 /*********************************************************************
392 * pf_vsnprintf (INTERNAL)
394 * implements both A and W vsnprintf functions
396 static int pf_vsnprintf( pf_output *out, const WCHAR *format, va_list valist )
398 int r;
399 LPCWSTR q, p = format;
400 pf_flags flags;
402 TRACE("format is %s\n",debugstr_w(format));
403 while (*p)
405 q = strchrW( p, '%' );
407 /* there's no % characters left, output the rest of the string */
408 if( !q )
410 r = pf_output_stringW(out, p, -1);
411 if( r<0 )
412 return r;
413 p += r;
414 continue;
417 /* there's characters before the %, output them */
418 if( q != p )
420 r = pf_output_stringW(out, p, q - p);
421 if( r<0 )
422 return r;
423 p = q;
426 /* we must be at a % now, skip over it */
427 assert( *p == '%' );
428 p++;
430 /* output a single % character */
431 if( *p == '%' )
433 r = pf_output_stringW(out, p++, 1);
434 if( r<0 )
435 return r;
436 continue;
439 /* parse the flags */
440 memset( &flags, 0, sizeof flags );
441 while (*p)
443 if( *p == '+' || *p == ' ' )
444 flags.Sign = '+';
445 else if( *p == '-' )
446 flags.LeftAlign = *p;
447 else if( *p == '0' )
448 flags.PadZero = *p;
449 else if( *p == '#' )
450 flags.Alternate = *p;
451 else
452 break;
453 p++;
456 /* deal with the field width specifier */
457 flags.FieldLength = 0;
458 if( *p == '*' )
460 flags.FieldLength = va_arg( valist, int );
461 p++;
463 else while( isdigit(*p) )
465 flags.FieldLength *= 10;
466 flags.FieldLength += *p++ - '0';
469 /* deal with precision */
470 if( *p == '.' )
472 p++;
473 if( *p == '*' )
475 flags.Precision = va_arg( valist, int );
476 p++;
478 else while( isdigit(*p) )
480 flags.Precision *= 10;
481 flags.Precision += *p++ - '0';
485 /* deal with integer width modifier */
486 while( *p )
488 if( *p == 'h' || *p == 'l' || *p == 'L' )
490 if( flags.IntegerLength == *p ) /* FIXME: this is wrong */
491 flags.IntegerDouble++;
492 else
493 flags.IntegerLength = *p;
494 p++;
496 else if( *p == 'w' )
497 flags.WideString = *p++;
498 else if( *p == 'F' )
499 p++; /* ignore */
500 else
501 break;
504 flags.Format = *p;
505 r = 0;
507 /* output a unicode string */
508 if( ( flags.Format == 's' && (flags.WideString || flags.IntegerLength == 'l' )) ||
509 ( !out->unicode && flags.Format == 'S' ) ||
510 ( out->unicode && flags.Format == 's' ) )
512 LPCWSTR str = va_arg( valist, const WCHAR * );
514 if( str )
515 r = pf_output_format_W( out, str, -1, &flags );
516 else
517 r = pf_output_format_A( out, "(null)", -1, &flags );
520 /* output a ASCII string */
521 else if( ( flags.Format == 's' && flags.IntegerLength == 'h' ) ||
522 ( out->unicode && flags.Format == 'S' ) ||
523 ( !out->unicode && flags.Format == 's' ) )
525 LPCSTR str = va_arg( valist, const CHAR * );
527 if( str )
528 r = pf_output_format_A( out, str, -1, &flags );
529 else
530 r = pf_output_format_A( out, "(null)", -1, &flags );
533 /* output a single wide character */
534 else if( ( flags.Format == 'c' && flags.IntegerLength == 'w' ) ||
535 ( out->unicode && flags.Format == 'c' ) ||
536 ( !out->unicode && flags.Format == 'C' ) )
538 WCHAR ch = va_arg( valist, int );
540 r = pf_output_format_W( out, &ch, 1, &flags );
543 /* output a single ascii character */
544 else if( ( flags.Format == 'c' && flags.IntegerLength == 'h' ) ||
545 ( out->unicode && flags.Format == 'C' ) ||
546 ( !out->unicode && flags.Format == 'c' ) )
548 CHAR ch = va_arg( valist, int );
550 r = pf_output_format_A( out, &ch, 1, &flags );
553 /* output a pointer */
554 else if( flags.Format == 'p' )
556 char pointer[11];
558 flags.PadZero = 0;
559 if( flags.Alternate )
560 sprintf(pointer, "0X%08lX", va_arg(valist, long));
561 else
562 sprintf(pointer, "%08lX", va_arg(valist, long));
563 r = pf_output_format_A( out, pointer, -1, &flags );
566 /* deal with %n */
567 else if( flags.Format == 'n' )
569 int *x = va_arg(valist, int *);
570 *x = out->used;
573 /* deal with integers and floats using libc's printf */
574 else if( pf_is_valid_format( flags.Format ) )
576 char fmt[20], number[40], *x = number;
578 if( flags.FieldLength >= sizeof number )
579 x = HeapAlloc( GetProcessHeap(), 0, flags.FieldLength+1 );
581 pf_rebuild_format_string( fmt, &flags );
583 if( pf_is_double_format( flags.Format ) )
584 sprintf( number, fmt, va_arg(valist, double) );
585 else
586 sprintf( number, fmt, va_arg(valist, int) );
588 r = pf_output_stringA( out, number, -1 );
589 if( x != number )
590 HeapFree( GetProcessHeap(), 0, number );
592 else
593 continue;
595 if( r<0 )
596 return r;
597 p++;
600 /* check we reached the end, and null terminate the string */
601 assert( *p == 0 );
602 r = pf_output_stringW( out, p, 1 );
603 if( r<0 )
604 return r;
606 return out->used - 1;
609 /*********************************************************************
610 * _vsnprintf (MSVCRT.@)
612 int MSVCRT_vsnprintf( char *str, unsigned int len,
613 const char *format, va_list valist )
615 DWORD sz;
616 LPWSTR formatW = NULL;
617 pf_output out;
618 int r;
620 out.unicode = FALSE;
621 out.buf.A = str;
622 out.used = 0;
623 out.len = len;
625 if( format )
627 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
628 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
629 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
632 r = pf_vsnprintf( &out, formatW, valist );
634 HeapFree( GetProcessHeap(), 0, formatW );
636 return r;
639 /*********************************************************************
640 * _vsnwsprintf (MSVCRT.@)
642 int MSVCRT_vsnwprintf( MSVCRT_wchar_t *str, unsigned int len,
643 const WCHAR *format, va_list valist )
645 pf_output out;
647 out.unicode = TRUE;
648 out.buf.W = str;
649 out.used = 0;
650 out.len = len;
652 return pf_vsnprintf( &out, format, valist );
655 /*********************************************************************
656 * sprintf (MSVCRT.@)
658 int MSVCRT_sprintf( char *str, const char *format, ... )
660 va_list ap;
661 int r;
663 va_start( ap, format );
664 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
665 va_end( ap );
666 return r;
669 /*********************************************************************
670 * swprintf (MSVCRT.@)
672 int MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
674 va_list ap;
675 int r;
677 va_start( ap, format );
678 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
679 va_end( ap );
680 return r;
683 /*********************************************************************
684 * vswprintf (MSVCRT.@)
686 int MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, va_list args )
688 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
691 /*********************************************************************
692 * wcscoll (MSVCRT.@)
694 int MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
696 /* FIXME: handle collates */
697 return strcmpW( str1, str2 );
700 /*********************************************************************
701 * wcspbrk (MSVCRT.@)
703 MSVCRT_wchar_t* MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
705 const MSVCRT_wchar_t* p;
706 while (*str)
708 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
709 str++;
711 return NULL;
714 /*********************************************************************
715 * wctomb (MSVCRT.@)
717 INT MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
719 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
722 /*********************************************************************
723 * iswalnum (MSVCRT.@)
725 INT MSVCRT_iswalnum( MSVCRT_wchar_t wc )
727 return isalnumW( wc );
730 /*********************************************************************
731 * iswalpha (MSVCRT.@)
733 INT MSVCRT_iswalpha( MSVCRT_wchar_t wc )
735 return isalphaW( wc );
738 /*********************************************************************
739 * iswcntrl (MSVCRT.@)
741 INT MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
743 return iscntrlW( wc );
746 /*********************************************************************
747 * iswdigit (MSVCRT.@)
749 INT MSVCRT_iswdigit( MSVCRT_wchar_t wc )
751 return isdigitW( wc );
754 /*********************************************************************
755 * iswgraph (MSVCRT.@)
757 INT MSVCRT_iswgraph( MSVCRT_wchar_t wc )
759 return isgraphW( wc );
762 /*********************************************************************
763 * iswlower (MSVCRT.@)
765 INT MSVCRT_iswlower( MSVCRT_wchar_t wc )
767 return islowerW( wc );
770 /*********************************************************************
771 * iswprint (MSVCRT.@)
773 INT MSVCRT_iswprint( MSVCRT_wchar_t wc )
775 return isprintW( wc );
778 /*********************************************************************
779 * iswpunct (MSVCRT.@)
781 INT MSVCRT_iswpunct( MSVCRT_wchar_t wc )
783 return ispunctW( wc );
786 /*********************************************************************
787 * iswspace (MSVCRT.@)
789 INT MSVCRT_iswspace( MSVCRT_wchar_t wc )
791 return isspaceW( wc );
794 /*********************************************************************
795 * iswupper (MSVCRT.@)
797 INT MSVCRT_iswupper( MSVCRT_wchar_t wc )
799 return isupperW( wc );
802 /*********************************************************************
803 * iswxdigit (MSVCRT.@)
805 INT MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
807 return isxdigitW( wc );