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.
25 #define WIN32_NO_STATUS
26 #include <wine/debug.h>
28 WINE_DEFAULT_DEBUG_CHANNEL(string
);
30 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
31 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
32 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
33 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
34 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
35 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
36 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
37 #define WPRINTF_INTPTR 0x0080 /* Pointer-size arg ('I' prefix) */
38 #define WPRINTF_I64 0x0100 /* 64-bit arg ('I64' prefix) */
68 static const CHAR null_stringA
[] = "(null)";
69 static const WCHAR null_stringW
[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
71 /***********************************************************************
72 * WPRINTF_ParseFormatA
74 * Parse a format specification. A format specification has the form:
76 * [-][#][0][width][.precision]type
78 * Return value is the length of the format specification in characters.
80 static INT
WPRINTF_ParseFormatA( LPCSTR format
, WPRINTF_FORMAT
*res
)
87 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
88 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
89 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
90 while ((*p
>= '0') && (*p
<= '9')) /* width field */
92 res
->width
= res
->width
* 10 + *p
- '0';
95 if (*p
== '.') /* precision field */
98 while ((*p
>= '0') && (*p
<= '9'))
100 res
->precision
= res
->precision
* 10 + *p
- '0';
104 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
105 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
106 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
109 if (p
[1] == '6' && p
[2] == '4') { res
->flags
|= WPRINTF_I64
; p
+= 3; }
110 else if (p
[1] == '3' && p
[2] == '2') p
+= 3;
111 else { res
->flags
|= WPRINTF_INTPTR
; p
++; }
116 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
119 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
123 res
->type
= WPR_SIGNED
;
126 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
129 res
->type
= (res
->flags
& (WPRINTF_SHORT
|WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
132 res
->type
= WPR_UNSIGNED
;
135 res
->width
= 2 * sizeof(void *);
136 res
->flags
|= WPRINTF_ZEROPAD
| WPRINTF_INTPTR
;
139 res
->flags
|= WPRINTF_UPPER_HEX
;
142 res
->type
= WPR_HEXA
;
144 default: /* unknown format char */
145 res
->type
= WPR_UNKNOWN
;
146 p
--; /* print format as normal char */
149 return (INT
)(p
- format
) + 1;
153 /***********************************************************************
154 * WPRINTF_ParseFormatW
156 * Parse a format specification. A format specification has the form:
158 * [-][#][0][width][.precision]type
160 * Return value is the length of the format specification in characters.
162 static INT
WPRINTF_ParseFormatW( LPCWSTR format
, WPRINTF_FORMAT
*res
)
169 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
170 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
171 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
172 while ((*p
>= '0') && (*p
<= '9')) /* width field */
174 res
->width
= res
->width
* 10 + *p
- '0';
177 if (*p
== '.') /* precision field */
180 while ((*p
>= '0') && (*p
<= '9'))
182 res
->precision
= res
->precision
* 10 + *p
- '0';
186 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
187 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
188 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
191 if (p
[1] == '6' && p
[2] == '4') { res
->flags
|= WPRINTF_I64
; p
+= 3; }
192 else if (p
[1] == '3' && p
[2] == '2') p
+= 3;
193 else { res
->flags
|= WPRINTF_INTPTR
; p
++; }
198 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
201 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
205 res
->type
= WPR_SIGNED
;
208 res
->type
= ((res
->flags
& WPRINTF_SHORT
) && !(res
->flags
& WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
211 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
214 res
->type
= WPR_UNSIGNED
;
217 res
->width
= 2 * sizeof(void *);
218 res
->flags
|= WPRINTF_ZEROPAD
| WPRINTF_INTPTR
;
221 res
->flags
|= WPRINTF_UPPER_HEX
;
224 res
->type
= WPR_HEXA
;
227 res
->type
= WPR_UNKNOWN
;
228 p
--; /* print format as normal char */
231 return (INT
)(p
- format
) + 1;
235 /***********************************************************************
238 static UINT
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, WPRINTF_DATA
*arg
,
239 LPSTR number
, UINT maxlen
)
243 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
244 if (format
->width
> maxlen
) format
->width
= maxlen
;
249 return (format
->precision
= 1);
251 if (!arg
->lpcstr_view
) arg
->lpcstr_view
= null_stringA
;
252 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
253 if (!*(arg
->lpcstr_view
+ len
)) break;
254 if (len
> maxlen
) len
= maxlen
;
255 return (format
->precision
= len
);
257 if (!arg
->lpcwstr_view
) arg
->lpcwstr_view
= null_stringW
;
258 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
259 if (!*(arg
->lpcwstr_view
+ len
)) break;
260 if (len
> maxlen
) len
= maxlen
;
261 return (format
->precision
= len
);
266 const char *digits
= (format
->flags
& WPRINTF_UPPER_HEX
) ? "0123456789ABCDEF" : "0123456789abcdef";
267 ULONGLONG num
= arg
->int_view
;
268 int base
= format
->type
== WPR_HEXA
? 16 : 10;
269 char buffer
[20], *p
= buffer
, *dst
= number
;
271 if (format
->type
== WPR_SIGNED
&& arg
->int_view
< 0)
274 num
= -arg
->int_view
;
276 if (format
->flags
& WPRINTF_INTPTR
) num
= (UINT_PTR
)num
;
277 else if (!(format
->flags
& WPRINTF_I64
)) num
= (UINT
)num
;
281 *p
++ = digits
[num
% base
];
284 while (p
> buffer
) *dst
++ = *(--p
);
292 if (len
> maxlen
) len
= maxlen
;
293 if (format
->precision
< len
) format
->precision
= len
;
294 if (format
->precision
> maxlen
) format
->precision
= maxlen
;
295 if ((format
->flags
& WPRINTF_ZEROPAD
) && (format
->width
> format
->precision
))
296 format
->precision
= format
->width
;
297 if (format
->flags
& WPRINTF_PREFIX_HEX
) len
+= 2;
302 /***********************************************************************
303 * wvnsprintfA (SHLWAPI.@)
305 * Print formatted output to a string, up to a maximum number of chars.
308 * buffer [O] Destination for output string
309 * maxlen [I] Maximum number of characters to write
310 * spec [I] Format string
313 * Success: The number of characters written.
316 INT WINAPI
wvnsprintfA( LPSTR buffer
, INT maxlen
, LPCSTR spec
, __ms_va_list args
)
318 WPRINTF_FORMAT format
;
322 WPRINTF_DATA argData
;
324 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_a(spec
));
326 while (*spec
&& (maxlen
> 1))
328 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
330 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
331 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
336 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
339 argData
.char_view
= (CHAR
)va_arg( args
, int );
342 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
345 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
350 if (format
.flags
& WPRINTF_INTPTR
) argData
.int_view
= va_arg(args
, INT_PTR
);
351 else if (format
.flags
& WPRINTF_I64
) argData
.int_view
= va_arg(args
, LONGLONG
);
352 else argData
.int_view
= va_arg(args
, INT
);
355 argData
.wchar_view
= 0;
359 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
361 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
362 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
367 *p
++ = argData
.wchar_view
;
370 *p
++ = argData
.char_view
;
373 memcpy( p
, argData
.lpcstr_view
, len
);
378 LPCWSTR ptr
= argData
.lpcwstr_view
;
379 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
383 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
386 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
392 /* Transfer the sign now, just in case it will be zero-padded*/
393 if (number
[0] == '-')
400 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
401 memcpy( p
, number
+ sign
, len
- sign
);
407 if (format
.flags
& WPRINTF_LEFTALIGN
)
408 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
413 TRACE("%s\n",debugstr_a(buffer
));
414 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
418 /***********************************************************************
419 * wvnsprintfW (SHLWAPI.@)
423 INT WINAPI
wvnsprintfW( LPWSTR buffer
, INT maxlen
, LPCWSTR spec
, __ms_va_list args
)
425 WPRINTF_FORMAT format
;
429 WPRINTF_DATA argData
;
431 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_w(spec
));
433 while (*spec
&& (maxlen
> 1))
435 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
437 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
438 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
443 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
446 argData
.char_view
= (CHAR
)va_arg( args
, int );
449 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
452 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
457 if (format
.flags
& WPRINTF_INTPTR
) argData
.int_view
= va_arg(args
, INT_PTR
);
458 else if (format
.flags
& WPRINTF_I64
) argData
.int_view
= va_arg(args
, LONGLONG
);
459 else argData
.int_view
= va_arg(args
, INT
);
462 argData
.wchar_view
= 0;
466 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
468 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
469 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
474 *p
++ = argData
.wchar_view
;
477 *p
++ = argData
.char_view
;
481 LPCSTR ptr
= argData
.lpcstr_view
;
482 for (i
= 0; i
< len
; i
++) *p
++ = (BYTE
)*ptr
++;
486 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
490 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
493 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
499 /* Transfer the sign now, just in case it will be zero-padded*/
500 if (number
[0] == '-')
507 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
508 for (i
= sign
; i
< len
; i
++) *p
++ = (BYTE
)number
[i
];
513 if (format
.flags
& WPRINTF_LEFTALIGN
)
514 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
519 TRACE("%s\n",debugstr_w(buffer
));
520 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
524 /*************************************************************************
525 * wnsprintfA (SHLWAPI.@)
527 * Print formatted output to a string, up to a maximum number of chars.
530 * lpOut [O] Destination for output string
531 * cchLimitIn [I] Maximum number of characters to write
532 * lpFmt [I] Format string
535 * Success: The number of characters written.
538 int WINAPIV
wnsprintfA(LPSTR lpOut
, int cchLimitIn
, LPCSTR lpFmt
, ...)
543 __ms_va_start( valist
, lpFmt
);
544 res
= wvnsprintfA( lpOut
, cchLimitIn
, lpFmt
, valist
);
545 __ms_va_end( valist
);
550 /*************************************************************************
551 * wnsprintfW (SHLWAPI.@)
555 int WINAPIV
wnsprintfW(LPWSTR lpOut
, int cchLimitIn
, LPCWSTR lpFmt
, ...)
560 __ms_va_start( valist
, lpFmt
);
561 res
= wvnsprintfW( lpOut
, cchLimitIn
, lpFmt
, valist
);
562 __ms_va_end( valist
);