Added a first-cut version of MapVirtualKeyExW() that has the same
[wine] / dlls / user / wsprintf.c
1 /*
2  * wsprintf functions
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #include <stdarg.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include "wine/winbase16.h"
11 #include "windef.h"
12 #include "wingdi.h"
13 #include "winuser.h"
14 #include "ldt.h"
15 #include "stackframe.h"
16 #include "global.h"
17 #include "debugtools.h"
18
19 DEFAULT_DEBUG_CHANNEL(string);
20
21
22 #define WPRINTF_LEFTALIGN   0x0001  /* Align output on the left ('-' prefix) */
23 #define WPRINTF_PREFIX_HEX  0x0002  /* Prefix hex with 0x ('#' prefix) */
24 #define WPRINTF_ZEROPAD     0x0004  /* Pad with zeros ('0' prefix) */
25 #define WPRINTF_LONG        0x0008  /* Long arg ('l' prefix) */
26 #define WPRINTF_SHORT       0x0010  /* Short arg ('h' prefix) */
27 #define WPRINTF_UPPER_HEX   0x0020  /* Upper-case hex ('X' specifier) */
28 #define WPRINTF_WIDE        0x0040  /* Wide arg ('w' prefix) */
29
30 typedef enum
31 {
32     WPR_UNKNOWN,
33     WPR_CHAR,
34     WPR_WCHAR,
35     WPR_STRING,
36     WPR_WSTRING,
37     WPR_SIGNED,
38     WPR_UNSIGNED,
39     WPR_HEXA
40 } WPRINTF_TYPE;
41
42 typedef struct
43 {
44     UINT         flags;
45     UINT         width;
46     UINT         precision;
47     WPRINTF_TYPE   type;
48 } WPRINTF_FORMAT;
49
50 typedef union {
51     WCHAR   wchar_view;
52     CHAR    char_view;
53     LPCSTR  lpcstr_view;
54     LPCWSTR lpcwstr_view;
55     INT     int_view;
56 } WPRINTF_DATA;
57
58 static const CHAR null_stringA[] = "(null)";
59 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
60
61 /***********************************************************************
62  *           WPRINTF_ParseFormatA
63  *
64  * Parse a format specification. A format specification has the form:
65  *
66  * [-][#][0][width][.precision]type
67  *
68  * Return value is the length of the format specification in characters.
69  */
70 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
71 {
72     LPCSTR p = format;
73
74     res->flags = 0;
75     res->width = 0;
76     res->precision = 0;
77     if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
78     if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
79     if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
80     while ((*p >= '0') && (*p <= '9'))  /* width field */
81     {
82         res->width = res->width * 10 + *p - '0';
83         p++;
84     }
85     if (*p == '.')  /* precision field */
86     {
87         p++;
88         while ((*p >= '0') && (*p <= '9'))
89         {
90             res->precision = res->precision * 10 + *p - '0';
91             p++;
92         }
93     }
94     if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
95     else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
96     else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
97     switch(*p)
98     {
99     case 'c':
100         res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
101         break;
102     case 'C':
103         res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
104         break;
105     case 'd':
106     case 'i':
107         res->type = WPR_SIGNED;
108         break;
109     case 's':
110         res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
111         break;
112     case 'S':
113         res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
114         break;
115     case 'u':
116         res->type = WPR_UNSIGNED;
117         break;
118     case 'X':
119         res->flags |= WPRINTF_UPPER_HEX;
120         /* fall through */
121     case 'x':
122         res->type = WPR_HEXA;
123         break;
124     default: /* unknown format char */
125         res->type = WPR_UNKNOWN;
126         p--;  /* print format as normal char */
127         break;
128     }
129     return (INT)(p - format) + 1;
130 }
131
132
133 /***********************************************************************
134  *           WPRINTF_ParseFormatW
135  *
136  * Parse a format specification. A format specification has the form:
137  *
138  * [-][#][0][width][.precision]type
139  *
140  * Return value is the length of the format specification in characters.
141  */
142 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
143 {
144     LPCWSTR p = format;
145
146     res->flags = 0;
147     res->width = 0;
148     res->precision = 0;
149     if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
150     if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
151     if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
152     while ((*p >= '0') && (*p <= '9'))  /* width field */
153     {
154         res->width = res->width * 10 + *p - '0';
155         p++;
156     }
157     if (*p == '.')  /* precision field */
158     {
159         p++;
160         while ((*p >= '0') && (*p <= '9'))
161         {
162             res->precision = res->precision * 10 + *p - '0';
163             p++;
164         }
165     }
166     if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
167     else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
168     else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
169     switch((CHAR)*p)
170     {
171     case 'c':
172         res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
173         break;
174     case 'C':
175         res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
176         break;
177     case 'd':
178     case 'i':
179         res->type = WPR_SIGNED;
180         break;
181     case 's':
182         res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
183         break;
184     case 'S':
185         res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
186         break;
187     case 'u':
188         res->type = WPR_UNSIGNED;
189         break;
190     case 'X':
191         res->flags |= WPRINTF_UPPER_HEX;
192         /* fall through */
193     case 'x':
194         res->type = WPR_HEXA;
195         break;
196     default:
197         res->type = WPR_UNKNOWN;
198         p--;  /* print format as normal char */
199         break;
200     }
201     return (INT)(p - format) + 1;
202 }
203
204
205 /***********************************************************************
206  *           WPRINTF_GetLen
207  */
208 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
209                               LPSTR number, UINT maxlen )
210 {
211     UINT len;
212
213     if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
214     if (format->width > maxlen) format->width = maxlen;
215     switch(format->type)
216     {
217     case WPR_CHAR:
218     case WPR_WCHAR:
219         return (format->precision = 1);
220     case WPR_STRING:
221         if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
222         for (len = 0; !format->precision || (len < format->precision); len++)
223             if (!*(arg->lpcstr_view + len)) break;
224         if (len > maxlen) len = maxlen;
225         return (format->precision = len);
226     case WPR_WSTRING:
227         if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
228         for (len = 0; !format->precision || (len < format->precision); len++)
229             if (!*(arg->lpcwstr_view + len)) break;
230         if (len > maxlen) len = maxlen;
231         return (format->precision = len);
232     case WPR_SIGNED:
233         len = sprintf( number, "%d", arg->int_view );
234         break;
235     case WPR_UNSIGNED:
236         len = sprintf( number, "%u", (UINT)arg->int_view );
237         break;
238     case WPR_HEXA:
239         len = sprintf( number,
240                        (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
241                        (UINT)arg->int_view);
242         break;
243     default:
244         return 0;
245     }
246     if (len > maxlen) len = maxlen;
247     if (format->precision < len) format->precision = len;
248     if (format->precision > maxlen) format->precision = maxlen;
249     if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
250         format->precision = format->width;
251     if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
252     return len;
253 }
254
255 /***********************************************************************
256  *           WPRINTF_ExtractVAPtr
257  */
258 static WPRINTF_DATA WPRINTF_ExtractVAPtr( WPRINTF_FORMAT *format, va_list* args )
259 {
260     WPRINTF_DATA result;
261     switch(format->type)
262     {
263         case WPR_WCHAR:
264             result.wchar_view = (WCHAR)va_arg( *args, int );break;
265         case WPR_CHAR:
266             result.char_view = (CHAR)va_arg( *args, int );  break;
267         case WPR_STRING:
268             result.lpcstr_view = va_arg( *args, LPCSTR);    break;
269         case WPR_WSTRING:
270             result.lpcwstr_view = va_arg( *args, LPCWSTR);  break;
271         case WPR_HEXA:
272         case WPR_SIGNED:
273         case WPR_UNSIGNED:
274             result.int_view = va_arg( *args, INT );         break;
275         default:
276             result.wchar_view = 0;                          break;
277     }
278     return result;
279 }
280
281 /***********************************************************************
282  *           wvsnprintf16   (Not a Windows API)
283  */
284 static INT16 wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec,
285                            LPCVOID args )
286 {
287     WPRINTF_FORMAT format;
288     LPSTR p = buffer;
289     UINT i, len;
290     CHAR number[20];
291     WPRINTF_DATA cur_arg;
292     SEGPTR seg_str;
293
294     while (*spec && (maxlen > 1))
295     {
296         if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
297         spec++;
298         if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
299         spec += WPRINTF_ParseFormatA( spec, &format );
300         switch(format.type)
301         {
302         case WPR_WCHAR:  /* No Unicode in Win16 */
303         case WPR_CHAR:
304             cur_arg.char_view = VA_ARG16( args, CHAR );
305             break;
306         case WPR_WSTRING:  /* No Unicode in Win16 */
307         case WPR_STRING:
308             seg_str = VA_ARG16( args, SEGPTR );
309             if (IsBadReadPtr16(seg_str, 1 )) cur_arg.lpcstr_view = "";
310             else cur_arg.lpcstr_view = PTR_SEG_TO_LIN( seg_str );
311             break;
312         case WPR_SIGNED:
313             if (!(format.flags & WPRINTF_LONG))
314             {
315                 cur_arg.int_view = VA_ARG16( args, INT16 );
316                 break;
317             }
318             /* fall through */
319         case WPR_HEXA:
320         case WPR_UNSIGNED:
321             if (format.flags & WPRINTF_LONG)
322                 cur_arg.int_view = VA_ARG16( args, UINT );
323             else
324                 cur_arg.int_view = VA_ARG16( args, UINT16 );
325             break;
326         case WPR_UNKNOWN:
327             continue;
328         }
329         len = WPRINTF_GetLen( &format, &cur_arg, number, maxlen - 1 );
330         if (!(format.flags & WPRINTF_LEFTALIGN))
331             for (i = format.precision; i < format.width; i++, maxlen--)
332                 *p++ = ' ';
333         switch(format.type)
334         {
335         case WPR_WCHAR:  /* No Unicode in Win16 */
336         case WPR_CHAR:
337             *p= cur_arg.char_view;
338             if (*p != '\0') p++;
339             else if (format.width > 1) *p++ = ' ';
340             else len = 0;
341             break;
342         case WPR_WSTRING:  /* No Unicode in Win16 */
343         case WPR_STRING:
344             if (len) memcpy( p, cur_arg.lpcstr_view, len );
345             p += len;
346             break;
347         case WPR_HEXA:
348             if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
349             {
350                 *p++ = '0';
351                 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
352                 maxlen -= 2;
353                 len -= 2;
354             }
355             /* fall through */
356         case WPR_SIGNED:
357         case WPR_UNSIGNED:
358             for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
359             if (len) memcpy( p, number, len );
360             p += len;
361             break;
362         case WPR_UNKNOWN:
363             continue;
364         }
365         if (format.flags & WPRINTF_LEFTALIGN)
366             for (i = format.precision; i < format.width; i++, maxlen--)
367                 *p++ = ' ';
368         maxlen -= len;
369     }
370     *p = 0;
371     return (maxlen > 1) ? (INT)(p - buffer) : -1;
372 }
373
374
375 /***********************************************************************
376  *           wvsnprintfA   (Not a Windows API, but we export it from USER32 anyway)
377  */
378 INT WINAPI wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, va_list args )
379 {
380     WPRINTF_FORMAT format;
381     LPSTR p = buffer;
382     UINT i, len;
383     CHAR number[20];
384     WPRINTF_DATA argData;
385
386     TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
387
388     while (*spec && (maxlen > 1))
389     {
390         if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
391         spec++;
392         if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
393         spec += WPRINTF_ParseFormatA( spec, &format );
394         argData = WPRINTF_ExtractVAPtr( &format, &args );
395         len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
396         if (!(format.flags & WPRINTF_LEFTALIGN))
397             for (i = format.precision; i < format.width; i++, maxlen--)
398                 *p++ = ' ';
399         switch(format.type)
400         {
401         case WPR_WCHAR:
402             *p = argData.wchar_view;
403             if (*p != '\0') p++;
404             else if (format.width > 1) *p++ = ' ';
405             else len = 0;
406             break;
407         case WPR_CHAR:
408             *p = argData.char_view;
409             if (*p != '\0') p++;
410             else if (format.width > 1) *p++ = ' ';
411             else len = 0;
412             break;
413         case WPR_STRING:
414             memcpy( p, argData.lpcstr_view, len );
415             p += len;
416             break;
417         case WPR_WSTRING:
418             {
419                 LPCWSTR ptr = argData.lpcwstr_view;
420                 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
421             }
422             break;
423         case WPR_HEXA:
424             if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
425             {
426                 *p++ = '0';
427                 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
428                 maxlen -= 2;
429                 len -= 2;
430             }
431             /* fall through */
432         case WPR_SIGNED:
433         case WPR_UNSIGNED:
434             for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
435             memcpy( p, number, len );
436             p += len;
437             break;
438         case WPR_UNKNOWN:
439             continue;
440         }
441         if (format.flags & WPRINTF_LEFTALIGN)
442             for (i = format.precision; i < format.width; i++, maxlen--)
443                 *p++ = ' ';
444         maxlen -= len;
445     }
446     *p = 0;
447     TRACE("%s\n",debugstr_a(buffer));
448     return (maxlen > 1) ? (INT)(p - buffer) : -1;
449 }
450
451
452 /***********************************************************************
453  *           wvsnprintfW   (Not a Windows API, but we export it from USER32 anyway)
454  */
455 INT WINAPI wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, va_list args )
456 {
457     WPRINTF_FORMAT format;
458     LPWSTR p = buffer;
459     UINT i, len;
460     CHAR number[20];
461     WPRINTF_DATA argData;
462
463     TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
464
465     while (*spec && (maxlen > 1))
466     {
467         if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
468         spec++;
469         if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
470         spec += WPRINTF_ParseFormatW( spec, &format );
471         argData = WPRINTF_ExtractVAPtr( &format, &args );
472         len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
473         if (!(format.flags & WPRINTF_LEFTALIGN))
474             for (i = format.precision; i < format.width; i++, maxlen--)
475                 *p++ = ' ';
476         switch(format.type)
477         {
478         case WPR_WCHAR:
479             *p = argData.wchar_view;
480             if (*p != '\0') p++;
481             else if (format.width > 1) *p++ = ' ';
482             else len = 0;
483             break;
484         case WPR_CHAR:
485             *p = argData.char_view;
486             if (*p != '\0') p++;
487             else if (format.width > 1) *p++ = ' ';
488             else len = 0;
489             break;
490         case WPR_STRING:
491             {
492                 LPCSTR ptr = argData.lpcstr_view;
493                 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
494             }
495             break;
496         case WPR_WSTRING:
497             if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
498             p += len;
499             break;
500         case WPR_HEXA:
501             if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
502             {
503                 *p++ = '0';
504                 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
505                 maxlen -= 2;
506                 len -= 2;
507             }
508             /* fall through */
509         case WPR_SIGNED:
510         case WPR_UNSIGNED:
511             for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
512             for (i = 0; i < len; i++) *p++ = (WCHAR)number[i];
513             break;
514         case WPR_UNKNOWN:
515             continue;
516         }
517         if (format.flags & WPRINTF_LEFTALIGN)
518             for (i = format.precision; i < format.width; i++, maxlen--)
519                 *p++ = ' ';
520         maxlen -= len;
521     }
522     *p = 0;
523     TRACE("%s\n",debugstr_w(buffer));
524     return (maxlen > 1) ? (INT)(p - buffer) : -1;
525 }
526
527
528 /***********************************************************************
529  *           wvsprintf16   (USER.421)
530  */
531 INT16 WINAPI wvsprintf16( LPSTR buffer, LPCSTR spec, LPCVOID args )
532 {
533     INT16 res;
534
535     TRACE("for %p got:\n",buffer);
536     res = wvsnprintf16( buffer, 1024, spec, args );
537     return ( res == -1 ) ? 1024 : res;
538 }
539
540
541 /***********************************************************************
542  *           wvsprintfA   (USER32.587)
543  */
544 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, va_list args )
545 {
546     INT res = wvsnprintfA( buffer, 1024, spec, args );
547     return ( res == -1 ) ? 1024 : res;
548 }
549
550
551 /***********************************************************************
552  *           wvsprintfW   (USER32.588)
553  */
554 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, va_list args )
555 {
556     INT res = wvsnprintfW( buffer, 1024, spec, args );
557     return ( res == -1 ) ? 1024 : res;
558 }
559
560
561 /***********************************************************************
562  *           wsprintf16   (USER.420)
563  */
564 INT16 WINAPIV wsprintf16(void)
565 {
566     VA_LIST16 valist;
567     INT16 res;
568     SEGPTR buffer, spec;
569
570     VA_START16( valist );
571     buffer = VA_ARG16( valist, SEGPTR );
572     spec   = VA_ARG16( valist, SEGPTR );
573     res = wvsnprintf16( (LPSTR)PTR_SEG_TO_LIN(buffer), 1024,
574                         (LPCSTR)PTR_SEG_TO_LIN(spec), valist );
575     VA_END16( valist );
576     return ( res == -1 ) ? 1024 : res;
577 }
578
579
580 /***********************************************************************
581  *           wsprintfA   (USER32.585)
582  */
583 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
584 {
585     va_list valist;
586     INT res;
587
588     va_start( valist, spec );
589     res = wvsnprintfA( buffer, 1024, spec, valist );
590     va_end( valist );
591     return ( res == -1 ) ? 1024 : res;
592 }
593
594
595 /***********************************************************************
596  *           wsprintfW   (USER32.586)
597  */
598 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
599 {
600     va_list valist;
601     INT res;
602
603     va_start( valist, spec );
604     res = wvsnprintfW( buffer, 1024, spec, valist );
605     va_end( valist );
606     return ( res == -1 ) ? 1024 : res;
607 }