Add handling of %ws, %S and %C to _vns(w)printf, improve sprintf
[wine/dcerpc.git] / dlls / msvcrt / wcs.c
blob9eb1f9a75c5cbc0fdf56d5ab815801903b0f9899
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 r = pf_fill( out, len, flags, 1 );
314 if( r>=0 )
315 r = pf_output_stringW( out, str, len );
317 if( r>=0 )
318 r = pf_fill( out, len, flags, 0 );
320 return r;
323 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
324 int len, pf_flags *flags )
326 int r = 0;
328 if( len < 0 )
329 len = strlen( str );
331 r = pf_fill( out, len, flags, 1 );
333 if( r>=0 )
334 r = pf_output_stringA( out, str, len );
336 if( r>=0 )
337 r = pf_fill( out, len, flags, 0 );
339 return r;
342 static inline BOOL pf_is_double_format( char fmt )
344 char float_fmts[] = "aefg";
345 if (!fmt)
346 return FALSE;
347 fmt = tolower( fmt );
348 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
351 static inline BOOL pf_is_valid_format( char fmt )
353 char float_fmts[] = "acdefginoux";
354 if (!fmt)
355 return FALSE;
356 fmt = tolower( fmt );
357 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
360 static void pf_rebuild_format_string( char *p, pf_flags *flags )
362 *p++ = '%';
363 if( flags->Sign )
364 *p++ = flags->Sign;
365 if( flags->LeftAlign )
366 *p++ = flags->LeftAlign;
367 if( flags->Alternate )
368 *p++ = flags->Alternate;
369 if( flags->PadZero )
370 *p++ = flags->PadZero;
371 if( flags->FieldLength )
373 sprintf(p, "%d", flags->FieldLength);
374 p += strlen(p);
376 if( flags->Precision )
378 sprintf(p, ".%d", flags->Precision);
379 p += strlen(p);
381 *p++ = flags->Format;
382 *p++ = 0;
385 /*********************************************************************
386 * pf_vsnprintf (INTERNAL)
388 * implements both A and W vsnprintf functions
390 static int pf_vsnprintf( pf_output *out, const WCHAR *format, va_list valist )
392 int r;
393 LPCWSTR q, p = format;
394 pf_flags flags;
396 while (*p)
398 q = strchrW( p, '%' );
400 /* there's no % characters left, output the rest of the string */
401 if( !q )
403 r = pf_output_stringW(out, p, -1);
404 if( r<0 )
405 return r;
406 p += r;
407 continue;
410 /* there's characters before the %, output them */
411 if( q != p )
413 r = pf_output_stringW(out, p, q - p);
414 if( r<0 )
415 return r;
416 p = q;
419 /* we must be at a % now, skip over it */
420 assert( *p == '%' );
421 p++;
423 /* output a single % character */
424 if( *p == '%' )
426 r = pf_output_stringW(out, p, 1);
427 if( r<0 )
428 return r;
429 continue;
432 /* parse the flags */
433 memset( &flags, 0, sizeof flags );
434 while (*p)
436 if( *p == '+' || *p == ' ' )
437 flags.Sign = '+';
438 else if( *p == '-' )
439 flags.LeftAlign = *p;
440 else if( *p == '0' )
441 flags.PadZero = *p;
442 else if( *p == '#' )
443 flags.Alternate = *p;
444 else
445 break;
446 p++;
449 /* deal with the field width specifier */
450 flags.FieldLength = 0;
451 if( *p == '*' )
452 flags.FieldLength = va_arg( valist, int );
453 else while( isdigit(*p) )
455 flags.FieldLength *= 10;
456 flags.FieldLength += *p++ - '0';
459 /* deal with precision */
460 if( *p == '.' )
462 p++;
463 if( *p == '*' )
464 flags.Precision = va_arg( valist, int );
465 else while( isdigit(*p) )
467 flags.Precision *= 10;
468 flags.Precision += *p++ - '0';
472 /* deal with integer width modifier */
473 while( *p )
475 if( *p == 'h' || *p == 'l' || *p == 'L' )
477 if( flags.IntegerLength == *p ) /* FIXME: this is wrong */
478 flags.IntegerDouble++;
479 else
480 flags.IntegerLength = *p;
481 p++;
483 else if( *p == 'w' )
484 flags.WideString = *p++;
485 else
486 break;
489 flags.Format = *p;
490 r = 0;
492 /* output a unicode string */
493 if( ( flags.Format == 's' && flags.WideString ) ||
494 ( !out->unicode && flags.Format == 'S' ) ||
495 ( out->unicode && flags.Format == 's' ) )
497 LPCWSTR str = va_arg( valist, const WCHAR * );
499 if( str )
500 r = pf_output_format_W( out, str, -1, &flags );
501 else
502 r = pf_output_format_A( out, "(null)", -1, &flags );
505 /* output a ASCII string */
506 else if( ( flags.Format == 's' && flags.IntegerLength == 'h' ) ||
507 ( out->unicode && flags.Format == 'S' ) ||
508 ( !out->unicode && flags.Format == 's' ) )
510 LPCSTR str = va_arg( valist, const CHAR * );
512 if( str )
513 r = pf_output_format_A( out, str, -1, &flags );
514 else
515 r = pf_output_format_A( out, "(null)", -1, &flags );
518 /* output a single wide character */
519 else if( ( flags.Format == 'c' && flags.IntegerLength == 'w' ) ||
520 ( out->unicode && flags.Format == 'c' ) ||
521 ( !out->unicode && flags.Format == 'C' ) )
523 WCHAR ch = va_arg( valist, int );
525 r = pf_output_format_W( out, &ch, 1, &flags );
528 /* output a single ascii character */
529 else if( ( flags.Format == 'c' && flags.IntegerLength == 'h' ) ||
530 ( out->unicode && flags.Format == 'C' ) ||
531 ( !out->unicode && flags.Format == 'c' ) )
533 CHAR ch = va_arg( valist, int );
535 r = pf_output_format_A( out, &ch, 1, &flags );
538 /* output a pointer */
539 else if( flags.Format == 'p' )
541 char pointer[10];
543 flags.PadZero = 0;
544 if( flags.Alternate )
545 sprintf(pointer, "0X%08lX", va_arg(valist, long));
546 else
547 sprintf(pointer, "%08lX", va_arg(valist, long));
548 r = pf_output_format_A( out, pointer, -1, &flags );
551 /* deal with %n */
552 else if( flags.Format == 'n' )
554 int *x = va_arg(valist, int *);
555 *x = out->used;
558 /* deal with integers and floats using libc's printf */
559 else if( pf_is_valid_format( flags.Format ) )
561 char fmt[20], number[40], *x = number;
563 if( flags.FieldLength >= sizeof number )
564 x = HeapAlloc( GetProcessHeap(), 0, flags.FieldLength+1 );
566 pf_rebuild_format_string( fmt, &flags );
568 if( pf_is_double_format( flags.Format ) )
569 sprintf( number, fmt, va_arg(valist, double) );
570 else
571 sprintf( number, fmt, va_arg(valist, int) );
573 r = pf_output_stringA( out, number, -1 );
574 if( x != number )
575 HeapFree( GetProcessHeap(), 0, number );
577 else
578 continue;
580 if( r<0 )
581 return r;
582 p++;
585 /* check we reached the end, and null terminate the string */
586 assert( *p == 0 );
587 r = pf_output_stringW( out, p, 1 );
588 if( r<0 )
589 return r;
591 return out->used - 1;
594 /*********************************************************************
595 * _vsnprintf (MSVCRT.@)
597 int MSVCRT_vsnprintf( char *str, unsigned int len,
598 const char *format, va_list valist )
600 DWORD sz;
601 LPWSTR formatW = NULL;
602 pf_output out;
603 int r;
605 out.unicode = FALSE;
606 out.buf.A = str;
607 out.used = 0;
608 out.len = len;
610 if( format )
612 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
613 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
614 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
617 r = pf_vsnprintf( &out, formatW, valist );
619 HeapFree( GetProcessHeap(), 0, formatW );
621 return r;
624 /*********************************************************************
625 * _vsnwsprintf (MSVCRT.@)
627 int MSVCRT_vsnwprintf( MSVCRT_wchar_t *str, unsigned int len,
628 const WCHAR *format, va_list valist )
630 pf_output out;
632 out.unicode = TRUE;
633 out.buf.W = str;
634 out.used = 0;
635 out.len = len;
637 return pf_vsnprintf( &out, format, valist );
640 /*********************************************************************
641 * sprintf (MSVCRT.@)
643 int MSVCRT_sprintf( char *str, const char *format, ... )
645 va_list ap;
646 int r;
648 va_start( ap, format );
649 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
650 va_end( ap );
651 return r;
654 /*********************************************************************
655 * swprintf (MSVCRT.@)
657 int MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
659 va_list ap;
660 int r;
662 va_start( ap, format );
663 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
664 va_end( ap );
665 return r;
668 /*********************************************************************
669 * vswprintf (MSVCRT.@)
671 int MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, va_list args )
673 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
676 /*********************************************************************
677 * wcscoll (MSVCRT.@)
679 int MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
681 /* FIXME: handle collates */
682 return strcmpW( str1, str2 );
685 /*********************************************************************
686 * wcspbrk (MSVCRT.@)
688 MSVCRT_wchar_t* MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
690 const MSVCRT_wchar_t* p;
691 while (*str)
693 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
694 str++;
696 return NULL;
699 /*********************************************************************
700 * wctomb (MSVCRT.@)
702 INT MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
704 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
707 /*********************************************************************
708 * iswalnum (MSVCRT.@)
710 INT MSVCRT_iswalnum( MSVCRT_wchar_t wc )
712 return isalnumW( wc );
715 /*********************************************************************
716 * iswalpha (MSVCRT.@)
718 INT MSVCRT_iswalpha( MSVCRT_wchar_t wc )
720 return isalphaW( wc );
723 /*********************************************************************
724 * iswcntrl (MSVCRT.@)
726 INT MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
728 return iscntrlW( wc );
731 /*********************************************************************
732 * iswdigit (MSVCRT.@)
734 INT MSVCRT_iswdigit( MSVCRT_wchar_t wc )
736 return isdigitW( wc );
739 /*********************************************************************
740 * iswgraph (MSVCRT.@)
742 INT MSVCRT_iswgraph( MSVCRT_wchar_t wc )
744 return isgraphW( wc );
747 /*********************************************************************
748 * iswlower (MSVCRT.@)
750 INT MSVCRT_iswlower( MSVCRT_wchar_t wc )
752 return islowerW( wc );
755 /*********************************************************************
756 * iswprint (MSVCRT.@)
758 INT MSVCRT_iswprint( MSVCRT_wchar_t wc )
760 return isprintW( wc );
763 /*********************************************************************
764 * iswpunct (MSVCRT.@)
766 INT MSVCRT_iswpunct( MSVCRT_wchar_t wc )
768 return ispunctW( wc );
771 /*********************************************************************
772 * iswspace (MSVCRT.@)
774 INT MSVCRT_iswspace( MSVCRT_wchar_t wc )
776 return isspaceW( wc );
779 /*********************************************************************
780 * iswupper (MSVCRT.@)
782 INT MSVCRT_iswupper( MSVCRT_wchar_t wc )
784 return isupperW( wc );
787 /*********************************************************************
788 * iswxdigit (MSVCRT.@)
790 INT MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
792 return isxdigitW( wc );