e37a8833c2b96786f875b65382b65d72b0b5ba09
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
);
31 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
32 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
33 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
34 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
35 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
36 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
37 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
38 #define WPRINTF_INTPTR 0x0080 /* Pointer-size arg ('I' prefix) */
39 #define WPRINTF_I64 0x0100 /* 64-bit arg ('I64' prefix) */
69 static const CHAR null_stringA
[] = "(null)";
70 static const WCHAR null_stringW
[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
72 /***********************************************************************
73 * WPRINTF_ParseFormatA
75 * Parse a format specification. A format specification has the form:
77 * [-][#][0][width][.precision]type
79 * Return value is the length of the format specification in characters.
81 static INT
WPRINTF_ParseFormatA( LPCSTR format
, WPRINTF_FORMAT
*res
)
88 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
89 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
90 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
91 while ((*p
>= '0') && (*p
<= '9')) /* width field */
93 res
->width
= res
->width
* 10 + *p
- '0';
96 if (*p
== '.') /* precision field */
99 while ((*p
>= '0') && (*p
<= '9'))
101 res
->precision
= res
->precision
* 10 + *p
- '0';
105 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
106 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
107 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
110 if (p
[1] == '6' && p
[2] == '4') { res
->flags
|= WPRINTF_I64
; p
+= 3; }
111 else if (p
[1] == '3' && p
[2] == '2') p
+= 3;
112 else { res
->flags
|= WPRINTF_INTPTR
; p
++; }
117 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
120 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
124 res
->type
= WPR_SIGNED
;
127 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
130 res
->type
= (res
->flags
& (WPRINTF_SHORT
|WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
133 res
->type
= WPR_UNSIGNED
;
136 res
->width
= 2 * sizeof(void *);
137 res
->flags
|= WPRINTF_ZEROPAD
| WPRINTF_INTPTR
;
140 res
->flags
|= WPRINTF_UPPER_HEX
;
143 res
->type
= WPR_HEXA
;
145 default: /* unknown format char */
146 res
->type
= WPR_UNKNOWN
;
147 p
--; /* print format as normal char */
150 return (INT
)(p
- format
) + 1;
154 /***********************************************************************
155 * WPRINTF_ParseFormatW
157 * Parse a format specification. A format specification has the form:
159 * [-][#][0][width][.precision]type
161 * Return value is the length of the format specification in characters.
163 static INT
WPRINTF_ParseFormatW( LPCWSTR format
, WPRINTF_FORMAT
*res
)
170 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
171 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
172 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
173 while ((*p
>= '0') && (*p
<= '9')) /* width field */
175 res
->width
= res
->width
* 10 + *p
- '0';
178 if (*p
== '.') /* precision field */
181 while ((*p
>= '0') && (*p
<= '9'))
183 res
->precision
= res
->precision
* 10 + *p
- '0';
187 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
188 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
189 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
192 if (p
[1] == '6' && p
[2] == '4') { res
->flags
|= WPRINTF_I64
; p
+= 3; }
193 else if (p
[1] == '3' && p
[2] == '2') p
+= 3;
194 else { res
->flags
|= WPRINTF_INTPTR
; p
++; }
199 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
202 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
206 res
->type
= WPR_SIGNED
;
209 res
->type
= ((res
->flags
& WPRINTF_SHORT
) && !(res
->flags
& WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
212 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
215 res
->type
= WPR_UNSIGNED
;
218 res
->width
= 2 * sizeof(void *);
219 res
->flags
|= WPRINTF_ZEROPAD
| WPRINTF_INTPTR
;
222 res
->flags
|= WPRINTF_UPPER_HEX
;
225 res
->type
= WPR_HEXA
;
228 res
->type
= WPR_UNKNOWN
;
229 p
--; /* print format as normal char */
232 return (INT
)(p
- format
) + 1;
236 /***********************************************************************
239 static UINT
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, WPRINTF_DATA
*arg
,
240 LPSTR number
, UINT maxlen
)
244 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
245 if (format
->width
> maxlen
) format
->width
= maxlen
;
250 return (format
->precision
= 1);
252 if (!arg
->lpcstr_view
) arg
->lpcstr_view
= null_stringA
;
253 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
254 if (!*(arg
->lpcstr_view
+ len
)) break;
255 if (len
> maxlen
) len
= maxlen
;
256 return (format
->precision
= len
);
258 if (!arg
->lpcwstr_view
) arg
->lpcwstr_view
= null_stringW
;
259 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
260 if (!*(arg
->lpcwstr_view
+ len
)) break;
261 if (len
> maxlen
) len
= maxlen
;
262 return (format
->precision
= len
);
267 const char *digits
= (format
->flags
& WPRINTF_UPPER_HEX
) ? "0123456789ABCDEF" : "0123456789abcdef";
268 ULONGLONG num
= arg
->int_view
;
269 int base
= format
->type
== WPR_HEXA
? 16 : 10;
270 char buffer
[20], *p
= buffer
, *dst
= number
;
272 if (format
->type
== WPR_SIGNED
&& arg
->int_view
< 0)
275 num
= -arg
->int_view
;
277 if (format
->flags
& WPRINTF_INTPTR
) num
= (UINT_PTR
)num
;
278 else if (!(format
->flags
& WPRINTF_I64
)) num
= (UINT
)num
;
282 *p
++ = digits
[num
% base
];
285 while (p
> buffer
) *dst
++ = *(--p
);
293 if (len
> maxlen
) len
= maxlen
;
294 if (format
->precision
< len
) format
->precision
= len
;
295 if (format
->precision
> maxlen
) format
->precision
= maxlen
;
296 if ((format
->flags
& WPRINTF_ZEROPAD
) && (format
->width
> format
->precision
))
297 format
->precision
= format
->width
;
298 if (format
->flags
& WPRINTF_PREFIX_HEX
) len
+= 2;
303 /***********************************************************************
304 * wvnsprintfA (SHLWAPI.@)
306 * Print formatted output to a string, up to a maximum number of chars.
309 * buffer [O] Destination for output string
310 * maxlen [I] Maximum number of characters to write
311 * spec [I] Format string
314 * Success: The number of characters written.
317 INT WINAPI
wvnsprintfA( LPSTR buffer
, INT maxlen
, LPCSTR spec
, __ms_va_list args
)
319 WPRINTF_FORMAT format
;
323 WPRINTF_DATA argData
;
325 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_a(spec
));
327 while (*spec
&& (maxlen
> 1))
329 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
331 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
332 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
337 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
340 argData
.char_view
= (CHAR
)va_arg( args
, int );
343 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
346 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
351 if (format
.flags
& WPRINTF_INTPTR
) argData
.int_view
= va_arg(args
, INT_PTR
);
352 else if (format
.flags
& WPRINTF_I64
) argData
.int_view
= va_arg(args
, LONGLONG
);
353 else argData
.int_view
= va_arg(args
, INT
);
356 argData
.wchar_view
= 0;
360 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
362 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
363 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
368 *p
++ = argData
.wchar_view
;
371 *p
++ = argData
.char_view
;
374 memcpy( p
, argData
.lpcstr_view
, len
);
379 LPCWSTR ptr
= argData
.lpcwstr_view
;
380 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
384 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
387 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
393 /* Transfer the sign now, just in case it will be zero-padded*/
394 if (number
[0] == '-')
401 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
402 memcpy( p
, number
+ sign
, len
- sign
);
408 if (format
.flags
& WPRINTF_LEFTALIGN
)
409 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
414 TRACE("%s\n",debugstr_a(buffer
));
415 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
419 /***********************************************************************
420 * wvnsprintfW (SHLWAPI.@)
424 INT WINAPI
wvnsprintfW( LPWSTR buffer
, INT maxlen
, LPCWSTR spec
, __ms_va_list args
)
426 WPRINTF_FORMAT format
;
430 WPRINTF_DATA argData
;
432 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_w(spec
));
434 while (*spec
&& (maxlen
> 1))
436 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
438 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
439 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
444 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
447 argData
.char_view
= (CHAR
)va_arg( args
, int );
450 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
453 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
458 if (format
.flags
& WPRINTF_INTPTR
) argData
.int_view
= va_arg(args
, INT_PTR
);
459 else if (format
.flags
& WPRINTF_I64
) argData
.int_view
= va_arg(args
, LONGLONG
);
460 else argData
.int_view
= va_arg(args
, INT
);
463 argData
.wchar_view
= 0;
467 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
469 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
470 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
475 *p
++ = argData
.wchar_view
;
478 *p
++ = argData
.char_view
;
482 LPCSTR ptr
= argData
.lpcstr_view
;
483 for (i
= 0; i
< len
; i
++) *p
++ = (BYTE
)*ptr
++;
487 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
491 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
494 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
500 /* Transfer the sign now, just in case it will be zero-padded*/
501 if (number
[0] == '-')
508 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
509 for (i
= sign
; i
< len
; i
++) *p
++ = (BYTE
)number
[i
];
514 if (format
.flags
& WPRINTF_LEFTALIGN
)
515 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
520 TRACE("%s\n",debugstr_w(buffer
));
521 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
525 /*************************************************************************
526 * wnsprintfA (SHLWAPI.@)
528 * Print formatted output to a string, up to a maximum number of chars.
531 * lpOut [O] Destination for output string
532 * cchLimitIn [I] Maximum number of characters to write
533 * lpFmt [I] Format string
536 * Success: The number of characters written.
539 int WINAPIV
wnsprintfA(LPSTR lpOut
, int cchLimitIn
, LPCSTR lpFmt
, ...)
544 __ms_va_start( valist
, lpFmt
);
545 res
= wvnsprintfA( lpOut
, cchLimitIn
, lpFmt
, valist
);
546 __ms_va_end( valist
);
551 /*************************************************************************
552 * wnsprintfW (SHLWAPI.@)
556 int WINAPIV
wnsprintfW(LPWSTR lpOut
, int cchLimitIn
, LPCWSTR lpFmt
, ...)
561 __ms_va_start( valist
, lpFmt
);
562 res
= wvnsprintfW( lpOut
, cchLimitIn
, lpFmt
, valist
);
563 __ms_va_end( valist
);