kernelbase: Use nameless unions/structs for loader data.
[wine.git] / dlls / shlwapi / wsprintf.c
blobfbd2a7e75bf2c804daa26884420b86f39be1bd65
1 /*
2 * wsprintf functions
4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * NOTE:
21 * This code is duplicated in user32. If you change something here make sure
22 * to change it in user32 too.
25 #include <stdarg.h>
26 #include <string.h>
27 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #define NO_SHLWAPI_REG
32 #include "shlwapi.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(string);
39 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
40 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
41 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
42 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
43 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
44 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
45 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
46 #define WPRINTF_INTPTR 0x0080 /* Pointer-size arg ('I' prefix) */
47 #define WPRINTF_I64 0x0100 /* 64-bit arg ('I64' prefix) */
49 typedef enum
51 WPR_UNKNOWN,
52 WPR_CHAR,
53 WPR_WCHAR,
54 WPR_STRING,
55 WPR_WSTRING,
56 WPR_SIGNED,
57 WPR_UNSIGNED,
58 WPR_HEXA
59 } WPRINTF_TYPE;
61 typedef struct
63 UINT flags;
64 UINT width;
65 UINT precision;
66 WPRINTF_TYPE type;
67 } WPRINTF_FORMAT;
69 typedef union {
70 WCHAR wchar_view;
71 CHAR char_view;
72 LPCSTR lpcstr_view;
73 LPCWSTR lpcwstr_view;
74 LONGLONG int_view;
75 } WPRINTF_DATA;
77 static const CHAR null_stringA[] = "(null)";
78 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
80 /***********************************************************************
81 * WPRINTF_ParseFormatA
83 * Parse a format specification. A format specification has the form:
85 * [-][#][0][width][.precision]type
87 * Return value is the length of the format specification in characters.
89 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
91 LPCSTR p = format;
93 res->flags = 0;
94 res->width = 0;
95 res->precision = 0;
96 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
97 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
98 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
99 while ((*p >= '0') && (*p <= '9')) /* width field */
101 res->width = res->width * 10 + *p - '0';
102 p++;
104 if (*p == '.') /* precision field */
106 p++;
107 while ((*p >= '0') && (*p <= '9'))
109 res->precision = res->precision * 10 + *p - '0';
110 p++;
113 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
114 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
115 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
116 else if (*p == 'I')
118 if (p[1] == '6' && p[2] == '4') { res->flags |= WPRINTF_I64; p += 3; }
119 else if (p[1] == '3' && p[2] == '2') p += 3;
120 else { res->flags |= WPRINTF_INTPTR; p++; }
122 switch(*p)
124 case 'c':
125 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
126 break;
127 case 'C':
128 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
129 break;
130 case 'd':
131 case 'i':
132 res->type = WPR_SIGNED;
133 break;
134 case 's':
135 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
136 break;
137 case 'S':
138 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
139 break;
140 case 'u':
141 res->type = WPR_UNSIGNED;
142 break;
143 case 'p':
144 res->width = 2 * sizeof(void *);
145 res->flags |= WPRINTF_ZEROPAD | WPRINTF_INTPTR;
146 /* fall through */
147 case 'X':
148 res->flags |= WPRINTF_UPPER_HEX;
149 /* fall through */
150 case 'x':
151 res->type = WPR_HEXA;
152 break;
153 default: /* unknown format char */
154 res->type = WPR_UNKNOWN;
155 p--; /* print format as normal char */
156 break;
158 return (INT)(p - format) + 1;
162 /***********************************************************************
163 * WPRINTF_ParseFormatW
165 * Parse a format specification. A format specification has the form:
167 * [-][#][0][width][.precision]type
169 * Return value is the length of the format specification in characters.
171 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
173 LPCWSTR p = format;
175 res->flags = 0;
176 res->width = 0;
177 res->precision = 0;
178 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
179 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
180 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
181 while ((*p >= '0') && (*p <= '9')) /* width field */
183 res->width = res->width * 10 + *p - '0';
184 p++;
186 if (*p == '.') /* precision field */
188 p++;
189 while ((*p >= '0') && (*p <= '9'))
191 res->precision = res->precision * 10 + *p - '0';
192 p++;
195 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
196 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
197 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
198 else if (*p == 'I')
200 if (p[1] == '6' && p[2] == '4') { res->flags |= WPRINTF_I64; p += 3; }
201 else if (p[1] == '3' && p[2] == '2') p += 3;
202 else { res->flags |= WPRINTF_INTPTR; p++; }
204 switch(*p)
206 case 'c':
207 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
208 break;
209 case 'C':
210 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
211 break;
212 case 'd':
213 case 'i':
214 res->type = WPR_SIGNED;
215 break;
216 case 's':
217 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
218 break;
219 case 'S':
220 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
221 break;
222 case 'u':
223 res->type = WPR_UNSIGNED;
224 break;
225 case 'p':
226 res->width = 2 * sizeof(void *);
227 res->flags |= WPRINTF_ZEROPAD | WPRINTF_INTPTR;
228 /* fall through */
229 case 'X':
230 res->flags |= WPRINTF_UPPER_HEX;
231 /* fall through */
232 case 'x':
233 res->type = WPR_HEXA;
234 break;
235 default:
236 res->type = WPR_UNKNOWN;
237 p--; /* print format as normal char */
238 break;
240 return (INT)(p - format) + 1;
244 /***********************************************************************
245 * WPRINTF_GetLen
247 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
248 LPSTR number, UINT maxlen, BOOL dst_is_wide )
250 UINT len;
252 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
253 if (format->width > maxlen) format->width = maxlen;
254 switch(format->type)
256 case WPR_CHAR:
257 return (format->precision = 1);
258 case WPR_WCHAR:
259 if (dst_is_wide) len = 1;
260 else len = WideCharToMultiByte( CP_ACP, 0, &arg->wchar_view, 1, NULL, 0, NULL, NULL );
261 return (format->precision = len);
262 case WPR_STRING:
263 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
264 if (dst_is_wide)
266 LPCSTR p = arg->lpcstr_view;
267 for (len = 0; (!format->precision || len < format->precision) && *p; p++)
269 /* This isn't applicable for UTF-8 and UTF-7 */
270 if (IsDBCSLeadByte( *p )) p++;
271 len++;
272 if (!*p) break;
275 else
277 for (len = 0; !format->precision || (len < format->precision); len++)
278 if (!*(arg->lpcstr_view + len)) break;
280 if (len > maxlen) len = maxlen;
281 return (format->precision = len);
282 case WPR_WSTRING:
283 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
284 if (dst_is_wide)
286 for (len = 0; !format->precision || (len < format->precision); len++)
287 if (!*(arg->lpcwstr_view + len)) break;
289 else
291 LPCWSTR p = arg->lpcwstr_view;
292 for (len = 0; (!format->precision || len < format->precision) && *p; p++)
293 len += WideCharToMultiByte( CP_ACP, 0, p, 1, NULL, 0, NULL, NULL );
294 if (format->precision && len > format->precision) len = format->precision;
296 if (len > maxlen) len = maxlen;
297 return (format->precision = len);
298 case WPR_SIGNED:
299 case WPR_UNSIGNED:
300 case WPR_HEXA:
302 const char *digits = (format->flags & WPRINTF_UPPER_HEX) ? "0123456789ABCDEF" : "0123456789abcdef";
303 ULONGLONG num = arg->int_view;
304 int base = format->type == WPR_HEXA ? 16 : 10;
305 char buffer[20], *p = buffer, *dst = number;
307 if (format->type == WPR_SIGNED && arg->int_view < 0)
309 *dst++ = '-';
310 num = -arg->int_view;
312 if (format->flags & WPRINTF_INTPTR) num = (UINT_PTR)num;
313 else if (!(format->flags & WPRINTF_I64)) num = (UINT)num;
317 *p++ = digits[num % base];
318 num /= base;
319 } while (num);
320 while (p > buffer) *dst++ = *(--p);
321 *dst = 0;
322 len = dst - number;
323 break;
325 default:
326 return 0;
328 if (len > maxlen) len = maxlen;
329 if (format->precision < len) format->precision = len;
330 if (format->precision > maxlen) format->precision = maxlen;
331 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
332 format->precision = format->width;
333 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
334 return len;
338 /***********************************************************************
339 * wvnsprintfA (SHLWAPI.@)
341 * Print formatted output to a string, up to a maximum number of chars.
343 * PARAMS
344 * buffer [O] Destination for output string
345 * maxlen [I] Maximum number of characters to write
346 * spec [I] Format string
348 * RETURNS
349 * Success: The number of characters written.
350 * Failure: -1.
352 INT WINAPI wvnsprintfA( LPSTR buffer, INT maxlen, LPCSTR spec, va_list args )
354 WPRINTF_FORMAT format;
355 LPSTR p = buffer;
356 UINT i, len, sign;
357 CHAR number[21]; /* 64bit number can be 18446744073709551616 which is 20 chars. and a \0 */
358 WPRINTF_DATA argData;
360 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
362 while (*spec && (maxlen > 1))
364 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
365 spec++;
366 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
367 spec += WPRINTF_ParseFormatA( spec, &format );
369 switch(format.type)
371 case WPR_WCHAR:
372 argData.wchar_view = (WCHAR)va_arg( args, int );
373 break;
374 case WPR_CHAR:
375 argData.char_view = (CHAR)va_arg( args, int );
376 break;
377 case WPR_STRING:
378 argData.lpcstr_view = va_arg( args, LPCSTR );
379 break;
380 case WPR_WSTRING:
381 argData.lpcwstr_view = va_arg( args, LPCWSTR );
382 break;
383 case WPR_HEXA:
384 case WPR_SIGNED:
385 case WPR_UNSIGNED:
386 if (format.flags & WPRINTF_INTPTR) argData.int_view = va_arg(args, INT_PTR);
387 else if (format.flags & WPRINTF_I64) argData.int_view = va_arg(args, LONGLONG);
388 else argData.int_view = va_arg(args, INT);
389 break;
390 default:
391 argData.wchar_view = 0;
392 break;
395 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1, FALSE );
396 sign = 0;
397 if (!(format.flags & WPRINTF_LEFTALIGN))
398 for (i = format.precision; i < format.width; i++, maxlen--)
399 *p++ = ' ';
400 switch(format.type)
402 case WPR_WCHAR:
404 CHAR mb[5];
405 if (WideCharToMultiByte( CP_ACP, 0, &argData.wchar_view, 1, mb, sizeof(mb), NULL, NULL ))
407 memcpy( p, mb, len );
408 p += len;
411 break;
412 case WPR_CHAR:
413 *p++ = argData.char_view;
414 break;
415 case WPR_STRING:
416 memcpy( p, argData.lpcstr_view, len );
417 p += len;
418 break;
419 case WPR_WSTRING:
421 LPCWSTR ptr = argData.lpcwstr_view;
422 for (i = 0; i < len; ptr++)
424 CHAR mb[5]; /* 5 is MB_LEN_MAX */
425 int ret = WideCharToMultiByte( CP_ACP, 0, ptr, 1, mb, sizeof(mb), NULL, NULL );
426 i += ret;
427 if (i > len) ret = len - (p - buffer);
428 memcpy( p, mb, ret );
429 p += ret;
432 break;
433 case WPR_HEXA:
434 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
436 *p++ = '0';
437 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
438 maxlen -= 2;
439 len -= 2;
441 /* fall through */
442 case WPR_SIGNED:
443 /* Transfer the sign now, just in case it will be zero-padded*/
444 if (number[0] == '-')
446 *p++ = '-';
447 sign = 1;
449 /* fall through */
450 case WPR_UNSIGNED:
451 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
452 memcpy( p, number + sign, len - sign );
453 p += len - sign;
454 break;
455 case WPR_UNKNOWN:
456 continue;
458 if (format.flags & WPRINTF_LEFTALIGN)
459 for (i = format.precision; i < format.width; i++, maxlen--)
460 *p++ = ' ';
461 maxlen -= len;
463 *p = 0;
464 TRACE("%s\n",debugstr_a(buffer));
465 return (maxlen > 1) ? (INT)(p - buffer) : -1;
469 /***********************************************************************
470 * wvnsprintfW (SHLWAPI.@)
472 * See wvnsprintfA.
474 INT WINAPI wvnsprintfW( LPWSTR buffer, INT maxlen, LPCWSTR spec, va_list args )
476 WPRINTF_FORMAT format;
477 LPWSTR p = buffer;
478 UINT i, len, sign;
479 CHAR number[21]; /* 64bit number can be 18446744073709551616 which is 20 chars. and a \0 */
480 WPRINTF_DATA argData;
482 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
484 while (*spec && (maxlen > 1))
486 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
487 spec++;
488 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
489 spec += WPRINTF_ParseFormatW( spec, &format );
491 switch(format.type)
493 case WPR_WCHAR:
494 argData.wchar_view = (WCHAR)va_arg( args, int );
495 break;
496 case WPR_CHAR:
497 argData.char_view = (CHAR)va_arg( args, int );
498 break;
499 case WPR_STRING:
500 argData.lpcstr_view = va_arg( args, LPCSTR );
501 break;
502 case WPR_WSTRING:
503 argData.lpcwstr_view = va_arg( args, LPCWSTR );
504 break;
505 case WPR_HEXA:
506 case WPR_SIGNED:
507 case WPR_UNSIGNED:
508 if (format.flags & WPRINTF_INTPTR) argData.int_view = va_arg(args, INT_PTR);
509 else if (format.flags & WPRINTF_I64) argData.int_view = va_arg(args, LONGLONG);
510 else argData.int_view = va_arg(args, INT);
511 break;
512 default:
513 argData.wchar_view = 0;
514 break;
517 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1, TRUE );
518 sign = 0;
519 if (!(format.flags & WPRINTF_LEFTALIGN))
520 for (i = format.precision; i < format.width; i++, maxlen--)
521 *p++ = ' ';
522 switch(format.type)
524 case WPR_WCHAR:
525 *p++ = argData.wchar_view;
526 break;
527 case WPR_CHAR:
529 WCHAR wc;
530 if (!IsDBCSLeadByte( (BYTE)argData.char_view )
531 && MultiByteToWideChar( CP_ACP, 0, &argData.char_view, 1, &wc, 1 ) > 0)
532 *p++ = wc;
533 else
534 *p++ = 0;
535 break;
537 case WPR_STRING:
539 LPCSTR ptr = argData.lpcstr_view;
540 for (i = 0; i < len; i++)
542 WCHAR buf[2]; /* for LeadByte + NUL case, we need 2 WCHARs. */
543 int ret, mb_len = IsDBCSLeadByte( *ptr ) ? 2 : 1;
544 ret = MultiByteToWideChar( CP_ACP, 0, ptr, mb_len, buf, ARRAY_SIZE( buf ));
545 *p++ = buf[ret - 1];
546 ptr += mb_len;
549 break;
550 case WPR_WSTRING:
551 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
552 p += len;
553 break;
554 case WPR_HEXA:
555 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
557 *p++ = '0';
558 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
559 maxlen -= 2;
560 len -= 2;
562 /* fall through */
563 case WPR_SIGNED:
564 /* Transfer the sign now, just in case it will be zero-padded*/
565 if (number[0] == '-')
567 *p++ = '-';
568 sign = 1;
570 /* fall through */
571 case WPR_UNSIGNED:
572 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
573 for (i = sign; i < len; i++) *p++ = (BYTE)number[i];
574 break;
575 case WPR_UNKNOWN:
576 continue;
578 if (format.flags & WPRINTF_LEFTALIGN)
579 for (i = format.precision; i < format.width; i++, maxlen--)
580 *p++ = ' ';
581 maxlen -= len;
583 *p = 0;
584 TRACE("%s\n",debugstr_w(buffer));
585 return (maxlen > 1) ? (INT)(p - buffer) : -1;
589 /*************************************************************************
590 * wnsprintfA (SHLWAPI.@)
592 * Print formatted output to a string, up to a maximum number of chars.
594 * PARAMS
595 * lpOut [O] Destination for output string
596 * cchLimitIn [I] Maximum number of characters to write
597 * lpFmt [I] Format string
599 * RETURNS
600 * Success: The number of characters written.
601 * Failure: -1.
603 int WINAPIV wnsprintfA(LPSTR lpOut, int cchLimitIn, LPCSTR lpFmt, ...)
605 va_list valist;
606 INT res;
608 va_start( valist, lpFmt );
609 res = wvnsprintfA( lpOut, cchLimitIn, lpFmt, valist );
610 va_end( valist );
611 return res;
615 /*************************************************************************
616 * wnsprintfW (SHLWAPI.@)
618 * See wnsprintfA.
620 int WINAPIV wnsprintfW(LPWSTR lpOut, int cchLimitIn, LPCWSTR lpFmt, ...)
622 va_list valist;
623 INT res;
625 va_start( valist, lpFmt );
626 res = wvnsprintfW( lpOut, cchLimitIn, lpFmt, valist );
627 va_end( valist );
628 return res;