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
21 * This code is duplicated in user32. If you change something here make sure
22 * to change it in user32 too.
31 #define NO_SHLWAPI_REG
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) */
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
)
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';
104 if (*p
== '.') /* precision field */
107 while ((*p
>= '0') && (*p
<= '9'))
109 res
->precision
= res
->precision
* 10 + *p
- '0';
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
++; }
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
++; }
125 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
128 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
132 res
->type
= WPR_SIGNED
;
135 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
138 res
->type
= (res
->flags
& (WPRINTF_SHORT
|WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
141 res
->type
= WPR_UNSIGNED
;
144 res
->width
= 2 * sizeof(void *);
145 res
->flags
|= WPRINTF_ZEROPAD
| WPRINTF_INTPTR
;
148 res
->flags
|= WPRINTF_UPPER_HEX
;
151 res
->type
= WPR_HEXA
;
153 default: /* unknown format char */
154 res
->type
= WPR_UNKNOWN
;
155 p
--; /* print format as normal char */
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
)
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';
186 if (*p
== '.') /* precision field */
189 while ((*p
>= '0') && (*p
<= '9'))
191 res
->precision
= res
->precision
* 10 + *p
- '0';
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
++; }
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
++; }
207 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
210 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
214 res
->type
= WPR_SIGNED
;
217 res
->type
= ((res
->flags
& WPRINTF_SHORT
) && !(res
->flags
& WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
220 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
223 res
->type
= WPR_UNSIGNED
;
226 res
->width
= 2 * sizeof(void *);
227 res
->flags
|= WPRINTF_ZEROPAD
| WPRINTF_INTPTR
;
230 res
->flags
|= WPRINTF_UPPER_HEX
;
233 res
->type
= WPR_HEXA
;
236 res
->type
= WPR_UNKNOWN
;
237 p
--; /* print format as normal char */
240 return (INT
)(p
- format
) + 1;
244 /***********************************************************************
247 static UINT
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, WPRINTF_DATA
*arg
,
248 LPSTR number
, UINT maxlen
, BOOL dst_is_wide
)
252 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
253 if (format
->width
> maxlen
) format
->width
= maxlen
;
257 return (format
->precision
= 1);
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
);
263 if (!arg
->lpcstr_view
) arg
->lpcstr_view
= null_stringA
;
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
++;
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
);
283 if (!arg
->lpcwstr_view
) arg
->lpcwstr_view
= null_stringW
;
286 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
287 if (!*(arg
->lpcwstr_view
+ len
)) break;
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
);
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)
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
];
320 while (p
> buffer
) *dst
++ = *(--p
);
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;
338 /***********************************************************************
339 * wvnsprintfA (SHLWAPI.@)
341 * Print formatted output to a string, up to a maximum number of chars.
344 * buffer [O] Destination for output string
345 * maxlen [I] Maximum number of characters to write
346 * spec [I] Format string
349 * Success: The number of characters written.
352 INT WINAPI
wvnsprintfA( LPSTR buffer
, INT maxlen
, LPCSTR spec
, va_list args
)
354 WPRINTF_FORMAT format
;
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; }
366 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
367 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
372 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
375 argData
.char_view
= (CHAR
)va_arg( args
, int );
378 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
381 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
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
);
391 argData
.wchar_view
= 0;
395 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1, FALSE
);
397 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
398 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
405 if (WideCharToMultiByte( CP_ACP
, 0, &argData
.wchar_view
, 1, mb
, sizeof(mb
), NULL
, NULL
))
407 memcpy( p
, mb
, len
);
413 *p
++ = argData
.char_view
;
416 memcpy( p
, argData
.lpcstr_view
, len
);
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
);
427 if (i
> len
) ret
= len
- (p
- buffer
);
428 memcpy( p
, mb
, ret
);
434 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
437 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
443 /* Transfer the sign now, just in case it will be zero-padded*/
444 if (number
[0] == '-')
451 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
452 memcpy( p
, number
+ sign
, len
- sign
);
458 if (format
.flags
& WPRINTF_LEFTALIGN
)
459 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
464 TRACE("%s\n",debugstr_a(buffer
));
465 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
469 /***********************************************************************
470 * wvnsprintfW (SHLWAPI.@)
474 INT WINAPI
wvnsprintfW( LPWSTR buffer
, INT maxlen
, LPCWSTR spec
, va_list args
)
476 WPRINTF_FORMAT format
;
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; }
488 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
489 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
494 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
497 argData
.char_view
= (CHAR
)va_arg( args
, int );
500 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
503 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
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
);
513 argData
.wchar_view
= 0;
517 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1, TRUE
);
519 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
520 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
525 *p
++ = argData
.wchar_view
;
530 if (!IsDBCSLeadByte( (BYTE
)argData
.char_view
)
531 && MultiByteToWideChar( CP_ACP
, 0, &argData
.char_view
, 1, &wc
, 1 ) > 0)
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
));
551 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
555 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
558 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
564 /* Transfer the sign now, just in case it will be zero-padded*/
565 if (number
[0] == '-')
572 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
573 for (i
= sign
; i
< len
; i
++) *p
++ = (BYTE
)number
[i
];
578 if (format
.flags
& WPRINTF_LEFTALIGN
)
579 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
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.
595 * lpOut [O] Destination for output string
596 * cchLimitIn [I] Maximum number of characters to write
597 * lpFmt [I] Format string
600 * Success: The number of characters written.
603 int WINAPIV
wnsprintfA(LPSTR lpOut
, int cchLimitIn
, LPCSTR lpFmt
, ...)
608 va_start( valist
, lpFmt
);
609 res
= wvnsprintfA( lpOut
, cchLimitIn
, lpFmt
, valist
);
615 /*************************************************************************
616 * wnsprintfW (SHLWAPI.@)
620 int WINAPIV
wnsprintfW(LPWSTR lpOut
, int cchLimitIn
, LPCWSTR lpFmt
, ...)
625 va_start( valist
, lpFmt
);
626 res
= wvnsprintfW( lpOut
, cchLimitIn
, lpFmt
, valist
);