Handle wParam in WM_PAINT properly: if non-null, it is the hdc we are
[wine] / dlls / msvcrt / wcs.c
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 <math.h>
24 #include <assert.h>
25 #include "msvcrt.h"
26 #include "winnls.h"
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
31
32
33 /* INTERNAL: MSVCRT_malloc() based wstrndup */
34 MSVCRT_wchar_t* msvcrt_wstrndup(const MSVCRT_wchar_t *buf, unsigned int size)
35 {
36   MSVCRT_wchar_t* ret;
37   unsigned int len = strlenW(buf), max_len;
38
39   max_len = size <= len? size : len + 1;
40
41   ret = MSVCRT_malloc(max_len * sizeof (MSVCRT_wchar_t));
42   if (ret)
43   {
44     memcpy(ret,buf,max_len * sizeof (MSVCRT_wchar_t));
45     ret[max_len] = 0;
46   }
47   return ret;
48 }
49
50 /*********************************************************************
51  *              _wcsdup (MSVCRT.@)
52  */
53 MSVCRT_wchar_t* _wcsdup( const MSVCRT_wchar_t* str )
54 {
55   MSVCRT_wchar_t* ret = NULL;
56   if (str)
57   {
58     int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
59     ret = MSVCRT_malloc( size );
60     if (ret) memcpy( ret, str, size );
61   }
62   return ret;
63 }
64
65 /*********************************************************************
66  *              _wcsicoll (MSVCRT.@)
67  */
68 INT _wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
69 {
70   /* FIXME: handle collates */
71   return strcmpiW( str1, str2 );
72 }
73
74 /*********************************************************************
75  *              _wcsnset (MSVCRT.@)
76  */
77 MSVCRT_wchar_t* _wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
78 {
79   MSVCRT_wchar_t* ret = str;
80   while ((n-- > 0) && *str) *str++ = c;
81   return ret;
82 }
83
84 /*********************************************************************
85  *              _wcsrev (MSVCRT.@)
86  */
87 MSVCRT_wchar_t* _wcsrev( MSVCRT_wchar_t* str )
88 {
89   MSVCRT_wchar_t* ret = str;
90   MSVCRT_wchar_t* end = str + strlenW(str) - 1;
91   while (end > str)
92   {
93     MSVCRT_wchar_t t = *end;
94     *end--  = *str;
95     *str++  = t;
96   }
97   return ret;
98 }
99
100 /*********************************************************************
101  *              _wcsset (MSVCRT.@)
102  */
103 MSVCRT_wchar_t* _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
104 {
105   MSVCRT_wchar_t* ret = str;
106   while (*str) *str++ = c;
107   return ret;
108 }
109
110 /*********************************************************************
111  *              wcstod (MSVCRT.@)
112  */
113 double MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
114 {
115   const MSVCRT_wchar_t* str = lpszStr;
116   int negative = 0;
117   double ret = 0, divisor = 10.0;
118
119   TRACE("(%s,%p) semi-stub\n", debugstr_w(lpszStr), end);
120
121   /* FIXME:
122    * - Should set errno on failure
123    * - Should fail on overflow
124    * - Need to check which input formats are allowed
125    */
126   while (isspaceW(*str))
127     str++;
128
129   if (*str == '-')
130   {
131     negative = 1;
132     str++;
133   }
134
135   while (isdigitW(*str))
136   {
137     ret = ret * 10.0 + (*str - '0');
138     str++;
139   }
140   if (*str == '.')
141     str++;
142   while (isdigitW(*str))
143   {
144     ret = ret + (*str - '0') / divisor;
145     divisor *= 10;
146     str++;
147   }
148
149   if (*str == 'E' || *str == 'e' || *str == 'D' || *str == 'd')
150   {
151     int negativeExponent = 0;
152     int exponent = 0;
153     if (*(++str) == '-')
154     {
155       negativeExponent = 1;
156       str++;
157     }
158     while (isdigitW(*str))
159     {
160       exponent = exponent * 10 + (*str - '0');
161       str++;
162     }
163     if (exponent != 0)
164     {
165       if (negativeExponent)
166         ret = ret / pow(10.0, exponent);
167       else
168         ret = ret * pow(10.0, exponent);
169     }
170   }
171
172   if (negative)
173     ret = -ret;
174
175   if (end)
176     *end = (MSVCRT_wchar_t*)str;
177
178   TRACE("returning %g\n", ret);
179   return ret;
180 }
181
182
183 typedef struct pf_output_t
184 {
185     int used;
186     int len;
187     BOOL unicode;
188     union {
189         LPWSTR W;
190         LPSTR  A;
191     } buf;
192 } pf_output;
193
194 typedef struct pf_flags_t
195 {
196     char Sign, LeftAlign, Alternate, PadZero;
197     char FieldLength, Precision;
198     char IntegerLength, IntegerDouble;
199     char WideString;
200     char Format;
201 } pf_flags;
202
203 /*
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
207  */
208 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
209 {
210     int space = out->len - out->used;
211
212     if( len < 0 )
213         len = strlenW( str );
214     if( out->unicode )
215     {
216         LPWSTR p = out->buf.W + out->used;
217
218         if( space >= len )
219         {
220             memcpy( p, str, len*sizeof(WCHAR) );
221             out->used += len;
222             return len;
223         }
224         if( space > 0 )
225             memcpy( p, str, space*sizeof(WCHAR) );
226         out->used += len;
227     }
228     else
229     {
230         int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
231         LPSTR p = out->buf.A + out->used;
232
233         if( space >= n )
234         {
235             WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
236             out->used += n;
237             return len;
238         }
239         if( space > 0 )
240             WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
241         out->used += n;
242     }
243     return -1;
244 }
245
246 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
247 {
248     int space = out->len - out->used;
249
250     if( len < 0 )
251         len = strlen( str );
252     if( !out->unicode )
253     {
254         LPSTR p = out->buf.A + out->used;
255
256         if( space >= len )
257         {
258             memcpy( p, str, len );
259             out->used += len;
260             return len;
261         }
262         if( space > 0 )
263             memcpy( p, str, space );
264         out->used += len;
265     }
266     else
267     {
268         int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
269         LPWSTR p = out->buf.W + out->used;
270
271         if( space >= n )
272         {
273             MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
274             out->used += n;
275             return len;
276         }
277         if( space > 0 )
278             MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
279         out->used += n;
280     }
281     return -1;
282 }
283
284 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
285 {
286     int i, r = 0;
287
288     if( ( !left &&  flags->LeftAlign ) || 
289         (  left && !flags->LeftAlign ) ||
290         (  left &&  flags->PadZero   ) )
291     {
292         for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
293         {
294             if( flags->PadZero )
295                 r = pf_output_stringA( out, "0", 1 );
296             else
297                 r = pf_output_stringA( out, " ", 1 );
298         }
299     }
300
301     return r;
302 }
303
304 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
305                                       int len, pf_flags *flags )
306 {
307     int r = 0;
308
309     if( len < 0 )
310         len = strlenW( str );
311
312     if (flags->Precision && flags->Precision < len)
313       len = flags->Precision;
314
315     r = pf_fill( out, len, flags, 1 );
316
317     if( r>=0 )
318         r = pf_output_stringW( out, str, len );
319
320     if( r>=0 )
321         r = pf_fill( out, len, flags, 0 );
322
323     return r;
324 }
325
326 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
327                                       int len, pf_flags *flags )
328 {
329     int r = 0;
330
331     if( len < 0 )
332         len = strlen( str );
333
334     if (flags->Precision && flags->Precision < len)
335       len = flags->Precision;
336
337     r = pf_fill( out, len, flags, 1 );
338
339     if( r>=0 )
340         r = pf_output_stringA( out, str, len );
341
342     if( r>=0 )
343         r = pf_fill( out, len, flags, 0 );
344
345     return r;
346 }
347
348 static inline BOOL pf_is_double_format( char fmt )
349 {
350     char float_fmts[] = "aefg";
351     if (!fmt)
352         return FALSE;
353     fmt = tolower( fmt );
354     return strchr( float_fmts, fmt ) ? TRUE : FALSE;
355 }
356
357 static inline BOOL pf_is_valid_format( char fmt )
358 {
359     char float_fmts[] = "acdefginoux";
360     if (!fmt)
361         return FALSE;
362     fmt = tolower( fmt );
363     return strchr( float_fmts, fmt ) ? TRUE : FALSE;
364 }
365
366 static void pf_rebuild_format_string( char *p, pf_flags *flags )
367 {
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 )
378     {
379         sprintf(p, "%d", flags->FieldLength);
380         p += strlen(p);
381     }
382     if( flags->Precision )
383     {
384         sprintf(p, ".%d", flags->Precision);
385         p += strlen(p);
386     }
387     *p++ = flags->Format;
388     *p++ = 0;
389 }
390
391 /*********************************************************************
392  *  pf_vsnprintf  (INTERNAL)
393  *
394  *  implements both A and W vsnprintf functions
395  */
396 static int pf_vsnprintf( pf_output *out, const WCHAR *format, va_list valist )
397 {
398     int r;
399     LPCWSTR q, p = format;
400     pf_flags flags;
401
402     while (*p)
403     {
404         q = strchrW( p, '%' );
405
406         /* there's no % characters left, output the rest of the string */
407         if( !q )
408         {
409             r = pf_output_stringW(out, p, -1);
410             if( r<0 )
411                 return r;
412             p += r;
413             continue;
414         }
415
416         /* there's characters before the %, output them */
417         if( q != p )
418         {
419             r = pf_output_stringW(out, p, q - p);
420             if( r<0 )
421                 return r;
422             p = q;
423         }
424
425         /* we must be at a % now, skip over it */
426         assert( *p == '%' );
427         p++;
428
429         /* output a single % character */
430         if( *p == '%' )
431         {
432             r = pf_output_stringW(out, p, 1);
433             if( r<0 )
434                 return r;
435             continue;
436         }
437
438         /* parse the flags */
439         memset( &flags, 0, sizeof flags );
440         while (*p)
441         {
442             if( *p == '+' || *p == ' ' )
443                 flags.Sign = '+';
444             else if( *p == '-' )
445                 flags.LeftAlign = *p;
446             else if( *p == '0' )
447                 flags.PadZero = *p;
448             else if( *p == '#' )
449                 flags.Alternate = *p;
450             else
451                 break;
452             p++;
453         }
454
455         /* deal with the field width specifier */
456         flags.FieldLength = 0;
457         if( *p == '*' )
458             flags.FieldLength = va_arg( valist, int );
459         else while( isdigit(*p) )
460         {
461             flags.FieldLength *= 10;
462             flags.FieldLength += *p++ - '0';
463         }
464
465         /* deal with precision */
466         if( *p == '.' )
467         {
468             p++;
469             if( *p == '*' )
470             {
471                 flags.Precision = va_arg( valist, int );
472                 p++;
473             }
474             else while( isdigit(*p) )
475             {
476                 flags.Precision *= 10;
477                 flags.Precision += *p++ - '0';
478             }
479         }
480
481         /* deal with integer width modifier */
482         while( *p )
483         {
484             if( *p == 'h' || *p == 'l' || *p == 'L' )
485             {
486                 if( flags.IntegerLength == *p )  /* FIXME: this is wrong */
487                     flags.IntegerDouble++;
488                 else
489                     flags.IntegerLength = *p;
490                 p++;
491             }
492             else if( *p == 'w' )
493                 flags.WideString = *p++;
494             else if( *p == 'F' )
495                 p++; /* ignore */
496             else
497                 break;
498         }
499
500         flags.Format = *p;
501         r = 0;
502
503         /* output a unicode string */
504         if( ( flags.Format == 's' && (flags.WideString || flags.IntegerLength == 'l' )) ||
505             ( !out->unicode && flags.Format == 'S' ) ||
506             ( out->unicode && flags.Format == 's' ) )
507         {
508             LPCWSTR str = va_arg( valist, const WCHAR * );
509
510             if( str )
511                 r = pf_output_format_W( out, str, -1, &flags );
512             else
513                 r = pf_output_format_A( out, "(null)", -1, &flags );
514         }
515
516         /* output a ASCII string */
517         else if( ( flags.Format == 's' && flags.IntegerLength == 'h' ) ||
518             ( out->unicode && flags.Format == 'S' ) ||
519             ( !out->unicode && flags.Format == 's' ) )
520         {
521             LPCSTR str = va_arg( valist, const CHAR * );
522
523             if( str )
524                 r = pf_output_format_A( out, str, -1, &flags );
525             else
526                 r = pf_output_format_A( out, "(null)", -1, &flags );
527         }
528
529         /* output a single wide character */
530         else if( ( flags.Format == 'c' && flags.IntegerLength == 'w' ) ||
531             ( out->unicode && flags.Format == 'c' ) ||
532             ( !out->unicode && flags.Format == 'C' ) )
533         {
534             WCHAR ch = va_arg( valist, int );
535
536             r = pf_output_format_W( out, &ch, 1, &flags );
537         }
538
539         /* output a single ascii character */
540         else if( ( flags.Format == 'c' && flags.IntegerLength == 'h' ) ||
541             ( out->unicode && flags.Format == 'C' ) ||
542             ( !out->unicode && flags.Format == 'c' ) )
543         {
544             CHAR ch = va_arg( valist, int );
545
546             r = pf_output_format_A( out, &ch, 1, &flags );
547         }
548
549         /* output a pointer */
550         else if( flags.Format == 'p' )
551         {
552             char pointer[10];
553
554             flags.PadZero = 0;
555             if( flags.Alternate )
556                 sprintf(pointer, "0X%08lX", va_arg(valist, long));
557             else
558                 sprintf(pointer, "%08lX", va_arg(valist, long));
559             r = pf_output_format_A( out, pointer, -1, &flags );
560         }
561
562         /* deal with %n */
563         else if( flags.Format == 'n' )
564         {
565             int *x = va_arg(valist, int *);
566             *x = out->used;
567         }
568
569         /* deal with integers and floats using libc's printf */
570         else if( pf_is_valid_format( flags.Format ) )
571         {
572             char fmt[20], number[40], *x = number;
573
574             if( flags.FieldLength >= sizeof number )
575                 x = HeapAlloc( GetProcessHeap(), 0, flags.FieldLength+1 );
576
577             pf_rebuild_format_string( fmt, &flags );
578
579             if( pf_is_double_format( flags.Format ) )
580                 sprintf( number, fmt, va_arg(valist, double) );
581             else
582                 sprintf( number, fmt, va_arg(valist, int) );
583
584             r = pf_output_stringA( out, number, -1 );
585             if( x != number )
586                 HeapFree( GetProcessHeap(), 0, number );
587         }
588         else
589             continue;
590
591         if( r<0 )
592             return r;
593         p++;
594     }
595
596     /* check we reached the end, and null terminate the string */
597     assert( *p == 0 );
598     r = pf_output_stringW( out, p, 1 );
599     if( r<0 )
600         return r;
601
602     return out->used - 1;
603 }
604
605 /*********************************************************************
606  *              _vsnprintf (MSVCRT.@)
607  */
608 int MSVCRT_vsnprintf( char *str, unsigned int len,
609                 const char *format, va_list valist )
610 {
611     DWORD sz;
612     LPWSTR formatW = NULL;
613     pf_output out;
614     int r;
615
616     out.unicode = FALSE;
617     out.buf.A = str;
618     out.used = 0;
619     out.len = len;
620
621     if( format )
622     {
623         sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
624         formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
625         MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
626     }
627
628     r = pf_vsnprintf( &out, formatW, valist );
629
630     HeapFree( GetProcessHeap(), 0, formatW );
631
632     return r;
633 }
634
635 /*********************************************************************
636  *              _vsnwsprintf (MSVCRT.@)
637  */
638 int MSVCRT_vsnwprintf( MSVCRT_wchar_t *str, unsigned int len,
639                               const WCHAR *format, va_list valist )
640 {
641     pf_output out;
642
643     out.unicode = TRUE;
644     out.buf.W = str;
645     out.used = 0;
646     out.len = len;
647
648     return pf_vsnprintf( &out, format, valist );
649 }
650
651 /*********************************************************************
652  *              sprintf (MSVCRT.@)
653  */
654 int MSVCRT_sprintf( char *str, const char *format, ... )
655 {
656     va_list ap;
657     int r;
658
659     va_start( ap, format );
660     r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
661     va_end( ap );
662     return r;
663 }
664
665 /*********************************************************************
666  *              swprintf (MSVCRT.@)
667  */
668 int MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
669 {
670     va_list ap;
671     int r;
672
673     va_start( ap, format );
674     r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
675     va_end( ap );
676     return r;
677 }
678
679 /*********************************************************************
680  *              vswprintf (MSVCRT.@)
681  */
682 int MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, va_list args )
683 {
684     return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
685 }
686
687 /*********************************************************************
688  *              wcscoll (MSVCRT.@)
689  */
690 int MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
691 {
692   /* FIXME: handle collates */
693   return strcmpW( str1, str2 );
694 }
695
696 /*********************************************************************
697  *              wcspbrk (MSVCRT.@)
698  */
699 MSVCRT_wchar_t* MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
700 {
701   const MSVCRT_wchar_t* p;
702   while (*str)
703   {
704     for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
705       str++;
706   }
707   return NULL;
708 }
709
710 /*********************************************************************
711  *              wctomb (MSVCRT.@)
712  */
713 INT MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
714 {
715   return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
716 }
717
718 /*********************************************************************
719  *              iswalnum (MSVCRT.@)
720  */
721 INT MSVCRT_iswalnum( MSVCRT_wchar_t wc )
722 {
723     return isalnumW( wc );
724 }
725
726 /*********************************************************************
727  *              iswalpha (MSVCRT.@)
728  */
729 INT MSVCRT_iswalpha( MSVCRT_wchar_t wc )
730 {
731     return isalphaW( wc );
732 }
733
734 /*********************************************************************
735  *              iswcntrl (MSVCRT.@)
736  */
737 INT MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
738 {
739     return iscntrlW( wc );
740 }
741
742 /*********************************************************************
743  *              iswdigit (MSVCRT.@)
744  */
745 INT MSVCRT_iswdigit( MSVCRT_wchar_t wc )
746 {
747     return isdigitW( wc );
748 }
749
750 /*********************************************************************
751  *              iswgraph (MSVCRT.@)
752  */
753 INT MSVCRT_iswgraph( MSVCRT_wchar_t wc )
754 {
755     return isgraphW( wc );
756 }
757
758 /*********************************************************************
759  *              iswlower (MSVCRT.@)
760  */
761 INT MSVCRT_iswlower( MSVCRT_wchar_t wc )
762 {
763     return islowerW( wc );
764 }
765
766 /*********************************************************************
767  *              iswprint (MSVCRT.@)
768  */
769 INT MSVCRT_iswprint( MSVCRT_wchar_t wc )
770 {
771     return isprintW( wc );
772 }
773
774 /*********************************************************************
775  *              iswpunct (MSVCRT.@)
776  */
777 INT MSVCRT_iswpunct( MSVCRT_wchar_t wc )
778 {
779     return ispunctW( wc );
780 }
781
782 /*********************************************************************
783  *              iswspace (MSVCRT.@)
784  */
785 INT MSVCRT_iswspace( MSVCRT_wchar_t wc )
786 {
787     return isspaceW( wc );
788 }
789
790 /*********************************************************************
791  *              iswupper (MSVCRT.@)
792  */
793 INT MSVCRT_iswupper( MSVCRT_wchar_t wc )
794 {
795     return isupperW( wc );
796 }
797
798 /*********************************************************************
799  *              iswxdigit (MSVCRT.@)
800  */
801 INT MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
802 {
803     return isxdigitW( wc );
804 }