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