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
32 //#include "winbase.h"
33 #define NO_SHLWAPI_REG
34 //#include "shlwapi.h"
36 #include <wine/debug.h>
38 WINE_DEFAULT_DEBUG_CHANNEL(string
);
41 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
42 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
43 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
44 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
45 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
46 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
47 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
48 #define WPRINTF_INTPTR 0x0080 /* Pointer-size arg ('I' prefix) */
49 #define WPRINTF_I64 0x0100 /* 64-bit arg ('I64' prefix) */
79 static const CHAR null_stringA
[] = "(null)";
80 static const WCHAR null_stringW
[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
82 /***********************************************************************
83 * WPRINTF_ParseFormatA
85 * Parse a format specification. A format specification has the form:
87 * [-][#][0][width][.precision]type
89 * Return value is the length of the format specification in characters.
91 static INT
WPRINTF_ParseFormatA( LPCSTR format
, WPRINTF_FORMAT
*res
)
98 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
99 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
100 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
101 while ((*p
>= '0') && (*p
<= '9')) /* width field */
103 res
->width
= res
->width
* 10 + *p
- '0';
106 if (*p
== '.') /* precision field */
109 while ((*p
>= '0') && (*p
<= '9'))
111 res
->precision
= res
->precision
* 10 + *p
- '0';
115 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
116 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
117 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
120 if (p
[1] == '6' && p
[2] == '4') { res
->flags
|= WPRINTF_I64
; p
+= 3; }
121 else if (p
[1] == '3' && p
[2] == '2') p
+= 3;
122 else { res
->flags
|= WPRINTF_INTPTR
; p
++; }
127 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
130 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
134 res
->type
= WPR_SIGNED
;
137 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
140 res
->type
= (res
->flags
& (WPRINTF_SHORT
|WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
143 res
->type
= WPR_UNSIGNED
;
146 res
->width
= 2 * sizeof(void *);
147 res
->flags
|= WPRINTF_ZEROPAD
| WPRINTF_INTPTR
;
150 res
->flags
|= WPRINTF_UPPER_HEX
;
153 res
->type
= WPR_HEXA
;
155 default: /* unknown format char */
156 res
->type
= WPR_UNKNOWN
;
157 p
--; /* print format as normal char */
160 return (INT
)(p
- format
) + 1;
164 /***********************************************************************
165 * WPRINTF_ParseFormatW
167 * Parse a format specification. A format specification has the form:
169 * [-][#][0][width][.precision]type
171 * Return value is the length of the format specification in characters.
173 static INT
WPRINTF_ParseFormatW( LPCWSTR format
, WPRINTF_FORMAT
*res
)
180 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
181 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
182 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
183 while ((*p
>= '0') && (*p
<= '9')) /* width field */
185 res
->width
= res
->width
* 10 + *p
- '0';
188 if (*p
== '.') /* precision field */
191 while ((*p
>= '0') && (*p
<= '9'))
193 res
->precision
= res
->precision
* 10 + *p
- '0';
197 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
198 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
199 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
202 if (p
[1] == '6' && p
[2] == '4') { res
->flags
|= WPRINTF_I64
; p
+= 3; }
203 else if (p
[1] == '3' && p
[2] == '2') p
+= 3;
204 else { res
->flags
|= WPRINTF_INTPTR
; p
++; }
209 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
212 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
216 res
->type
= WPR_SIGNED
;
219 res
->type
= ((res
->flags
& WPRINTF_SHORT
) && !(res
->flags
& WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
222 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
225 res
->type
= WPR_UNSIGNED
;
228 res
->width
= 2 * sizeof(void *);
229 res
->flags
|= WPRINTF_ZEROPAD
| WPRINTF_INTPTR
;
232 res
->flags
|= WPRINTF_UPPER_HEX
;
235 res
->type
= WPR_HEXA
;
238 res
->type
= WPR_UNKNOWN
;
239 p
--; /* print format as normal char */
242 return (INT
)(p
- format
) + 1;
246 /***********************************************************************
249 static UINT
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, WPRINTF_DATA
*arg
,
250 LPSTR number
, UINT maxlen
)
254 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
255 if (format
->width
> maxlen
) format
->width
= maxlen
;
260 return (format
->precision
= 1);
262 if (!arg
->lpcstr_view
) arg
->lpcstr_view
= null_stringA
;
263 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
264 if (!*(arg
->lpcstr_view
+ len
)) break;
265 if (len
> maxlen
) len
= maxlen
;
266 return (format
->precision
= len
);
268 if (!arg
->lpcwstr_view
) arg
->lpcwstr_view
= null_stringW
;
269 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
270 if (!*(arg
->lpcwstr_view
+ len
)) break;
271 if (len
> maxlen
) len
= maxlen
;
272 return (format
->precision
= len
);
277 const char *digits
= (format
->flags
& WPRINTF_UPPER_HEX
) ? "0123456789ABCDEF" : "0123456789abcdef";
278 ULONGLONG num
= arg
->int_view
;
279 int base
= format
->type
== WPR_HEXA
? 16 : 10;
280 char buffer
[20], *p
= buffer
, *dst
= number
;
282 if (format
->type
== WPR_SIGNED
&& arg
->int_view
< 0)
285 num
= -arg
->int_view
;
287 if (format
->flags
& WPRINTF_INTPTR
) num
= (UINT_PTR
)num
;
288 else if (!(format
->flags
& WPRINTF_I64
)) num
= (UINT
)num
;
292 *p
++ = digits
[num
% base
];
295 while (p
> buffer
) *dst
++ = *(--p
);
303 if (len
> maxlen
) len
= maxlen
;
304 if (format
->precision
< len
) format
->precision
= len
;
305 if (format
->precision
> maxlen
) format
->precision
= maxlen
;
306 if ((format
->flags
& WPRINTF_ZEROPAD
) && (format
->width
> format
->precision
))
307 format
->precision
= format
->width
;
308 if (format
->flags
& WPRINTF_PREFIX_HEX
) len
+= 2;
313 /***********************************************************************
314 * wvnsprintfA (SHLWAPI.@)
316 * Print formatted output to a string, up to a maximum number of chars.
319 * buffer [O] Destination for output string
320 * maxlen [I] Maximum number of characters to write
321 * spec [I] Format string
324 * Success: The number of characters written.
327 INT WINAPI
wvnsprintfA( LPSTR buffer
, INT maxlen
, LPCSTR spec
, __ms_va_list args
)
329 WPRINTF_FORMAT format
;
333 WPRINTF_DATA argData
;
335 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_a(spec
));
337 while (*spec
&& (maxlen
> 1))
339 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
341 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
342 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
347 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
350 argData
.char_view
= (CHAR
)va_arg( args
, int );
353 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
356 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
361 if (format
.flags
& WPRINTF_INTPTR
) argData
.int_view
= va_arg(args
, INT_PTR
);
362 else if (format
.flags
& WPRINTF_I64
) argData
.int_view
= va_arg(args
, LONGLONG
);
363 else argData
.int_view
= va_arg(args
, INT
);
366 argData
.wchar_view
= 0;
370 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
372 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
373 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
378 *p
++ = argData
.wchar_view
;
381 *p
++ = argData
.char_view
;
384 memcpy( p
, argData
.lpcstr_view
, len
);
389 LPCWSTR ptr
= argData
.lpcwstr_view
;
390 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
394 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
397 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
403 /* Transfer the sign now, just in case it will be zero-padded*/
404 if (number
[0] == '-')
411 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
412 memcpy( p
, number
+ sign
, len
- sign
);
418 if (format
.flags
& WPRINTF_LEFTALIGN
)
419 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
424 TRACE("%s\n",debugstr_a(buffer
));
425 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
429 /***********************************************************************
430 * wvnsprintfW (SHLWAPI.@)
434 INT WINAPI
wvnsprintfW( LPWSTR buffer
, INT maxlen
, LPCWSTR spec
, __ms_va_list args
)
436 WPRINTF_FORMAT format
;
440 WPRINTF_DATA argData
;
442 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_w(spec
));
444 while (*spec
&& (maxlen
> 1))
446 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
448 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
449 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
454 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
457 argData
.char_view
= (CHAR
)va_arg( args
, int );
460 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
463 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
468 if (format
.flags
& WPRINTF_INTPTR
) argData
.int_view
= va_arg(args
, INT_PTR
);
469 else if (format
.flags
& WPRINTF_I64
) argData
.int_view
= va_arg(args
, LONGLONG
);
470 else argData
.int_view
= va_arg(args
, INT
);
473 argData
.wchar_view
= 0;
477 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
479 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
480 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
485 *p
++ = argData
.wchar_view
;
488 *p
++ = argData
.char_view
;
492 LPCSTR ptr
= argData
.lpcstr_view
;
493 for (i
= 0; i
< len
; i
++) *p
++ = (BYTE
)*ptr
++;
497 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
501 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
504 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
510 /* Transfer the sign now, just in case it will be zero-padded*/
511 if (number
[0] == '-')
518 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
519 for (i
= sign
; i
< len
; i
++) *p
++ = (BYTE
)number
[i
];
524 if (format
.flags
& WPRINTF_LEFTALIGN
)
525 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
530 TRACE("%s\n",debugstr_w(buffer
));
531 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
535 /*************************************************************************
536 * wnsprintfA (SHLWAPI.@)
538 * Print formatted output to a string, up to a maximum number of chars.
541 * lpOut [O] Destination for output string
542 * cchLimitIn [I] Maximum number of characters to write
543 * lpFmt [I] Format string
546 * Success: The number of characters written.
549 int WINAPIV
wnsprintfA(LPSTR lpOut
, int cchLimitIn
, LPCSTR lpFmt
, ...)
554 __ms_va_start( valist
, lpFmt
);
555 res
= wvnsprintfA( lpOut
, cchLimitIn
, lpFmt
, valist
);
556 __ms_va_end( valist
);
561 /*************************************************************************
562 * wnsprintfW (SHLWAPI.@)
566 int WINAPIV
wnsprintfW(LPWSTR lpOut
, int cchLimitIn
, LPCWSTR lpFmt
, ...)
571 __ms_va_start( valist
, lpFmt
);
572 res
= wvnsprintfW( lpOut
, cchLimitIn
, lpFmt
, valist
);
573 __ms_va_end( valist
);