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 shlwapi. If you change something here make sure
22 * to change it in shlwapi too.
25 * COPYRIGHT: See COPYING in the top level directory
26 * PROJECT: ReactOS User32
27 * PURPOSE: [w]sprintf functions
28 * FILE: lib/user32/wsprintf.c
29 * PROGRAMER: Steven Edwards
30 * REVISION HISTORY: 2003/07/13 Merged from wine user/wsprintf.c
31 * NOTES: Adapted from Wine
36 #define WINE_NO_TRACE_MSGS
37 #include <wine/debug.h>
39 WINE_DEFAULT_DEBUG_CHANNEL(string
);
42 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
43 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
44 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
45 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
46 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
47 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
48 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
78 static const CHAR null_stringA
[] = "(null)";
79 static const WCHAR null_stringW
[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
81 /***********************************************************************
82 * WPRINTF_ParseFormatA
84 * Parse a format specification. A format specification has the form:
86 * [-][#][0][width][.precision]type
88 * Return value is the length of the format specification in characters.
90 static INT
WPRINTF_ParseFormatA( LPCSTR format
, WPRINTF_FORMAT
*res
)
97 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
98 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
99 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
100 while ((*p
>= '0') && (*p
<= '9')) /* width field */
102 res
->width
= res
->width
* 10 + *p
- '0';
105 if (*p
== '.') /* precision field */
108 while ((*p
>= '0') && (*p
<= '9'))
110 res
->precision
= res
->precision
* 10 + *p
- '0';
114 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
115 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
116 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
120 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
123 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
127 res
->type
= WPR_SIGNED
;
130 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
133 res
->type
= (res
->flags
& (WPRINTF_SHORT
|WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
136 res
->type
= WPR_UNSIGNED
;
140 res
->flags
|= WPRINTF_ZEROPAD
;
143 res
->flags
|= WPRINTF_UPPER_HEX
;
146 res
->type
= WPR_HEXA
;
148 default: /* unknown format char */
149 res
->type
= WPR_UNKNOWN
;
150 p
--; /* print format as normal char */
153 return (INT
)(p
- format
) + 1;
157 /***********************************************************************
158 * WPRINTF_ParseFormatW
160 * Parse a format specification. A format specification has the form:
162 * [-][#][0][width][.precision]type
164 * Return value is the length of the format specification in characters.
166 static INT
WPRINTF_ParseFormatW( LPCWSTR format
, WPRINTF_FORMAT
*res
)
173 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
174 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
175 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
176 while ((*p
>= '0') && (*p
<= '9')) /* width field */
178 res
->width
= res
->width
* 10 + *p
- '0';
181 if (*p
== '.') /* precision field */
184 while ((*p
>= '0') && (*p
<= '9'))
186 res
->precision
= res
->precision
* 10 + *p
- '0';
190 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
191 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
192 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
196 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
199 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
203 res
->type
= WPR_SIGNED
;
206 res
->type
= ((res
->flags
& WPRINTF_SHORT
) && !(res
->flags
& WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
209 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
212 res
->type
= WPR_UNSIGNED
;
216 res
->flags
|= WPRINTF_ZEROPAD
;
219 res
->flags
|= WPRINTF_UPPER_HEX
;
222 res
->type
= WPR_HEXA
;
225 res
->type
= WPR_UNKNOWN
;
226 p
--; /* print format as normal char */
229 return (INT
)(p
- format
) + 1;
233 /***********************************************************************
236 static UINT
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, WPRINTF_DATA
*arg
,
237 LPSTR number
, UINT maxlen
)
241 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
242 if (format
->width
> maxlen
) format
->width
= maxlen
;
247 return (format
->precision
= 1);
249 if (!arg
->lpcstr_view
) arg
->lpcstr_view
= null_stringA
;
250 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
251 if (!*(arg
->lpcstr_view
+ len
)) break;
252 if (len
> maxlen
) len
= maxlen
;
253 return (format
->precision
= len
);
255 if (!arg
->lpcwstr_view
) arg
->lpcwstr_view
= null_stringW
;
256 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
257 if (!*(arg
->lpcwstr_view
+ len
)) break;
258 if (len
> maxlen
) len
= maxlen
;
259 return (format
->precision
= len
);
261 len
= sprintf( number
, "%d", arg
->int_view
);
264 len
= sprintf( number
, "%u", (UINT
)arg
->int_view
);
267 len
= sprintf( number
,
268 (format
->flags
& WPRINTF_UPPER_HEX
) ? "%X" : "%x",
269 (UINT
)arg
->int_view
);
274 if (len
> maxlen
) len
= maxlen
;
275 if (format
->precision
< len
) format
->precision
= len
;
276 if (format
->precision
> maxlen
) format
->precision
= maxlen
;
277 if ((format
->flags
& WPRINTF_ZEROPAD
) && (format
->width
> format
->precision
))
278 format
->precision
= format
->width
;
279 if (format
->flags
& WPRINTF_PREFIX_HEX
) len
+= 2;
284 /***********************************************************************
285 * wvsnprintfA (internal)
287 static INT
wvsnprintfA( LPSTR buffer
, UINT maxlen
, LPCSTR spec
, __ms_va_list args
)
289 WPRINTF_FORMAT format
;
293 WPRINTF_DATA argData
;
295 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_a(spec
));
297 while (*spec
&& (maxlen
> 1))
299 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
301 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
302 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
307 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
310 argData
.char_view
= (CHAR
)va_arg( args
, int );
313 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
316 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
321 argData
.int_view
= va_arg( args
, INT
);
324 argData
.wchar_view
= 0;
328 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
330 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
331 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
336 *p
++ = argData
.wchar_view
;
339 *p
++ = argData
.char_view
;
342 memcpy( p
, argData
.lpcstr_view
, len
);
347 LPCWSTR ptr
= argData
.lpcwstr_view
;
348 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
352 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
355 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
361 /* Transfer the sign now, just in case it will be zero-padded*/
362 if (number
[0] == '-')
369 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
370 memcpy( p
, number
+ sign
, len
- sign
);
376 if (format
.flags
& WPRINTF_LEFTALIGN
)
377 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
382 TRACE("%s\n",debugstr_a(buffer
));
383 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
387 /***********************************************************************
388 * wvsnprintfW (internal)
390 static INT
wvsnprintfW( LPWSTR buffer
, UINT maxlen
, LPCWSTR spec
, __ms_va_list args
)
392 WPRINTF_FORMAT format
;
396 WPRINTF_DATA argData
;
398 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_w(spec
));
400 while (*spec
&& (maxlen
> 1))
402 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
404 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
405 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
410 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
413 argData
.char_view
= (CHAR
)va_arg( args
, int );
416 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
419 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
424 argData
.int_view
= va_arg( args
, INT
);
427 argData
.wchar_view
= 0;
431 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
433 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
434 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
439 *p
++ = argData
.wchar_view
;
442 *p
++ = argData
.char_view
;
446 LPCSTR ptr
= argData
.lpcstr_view
;
447 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)*ptr
++;
451 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
455 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
458 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
464 /* Transfer the sign now, just in case it will be zero-padded*/
465 if (number
[0] == '-')
472 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
473 for (i
= sign
; i
< len
; i
++) *p
++ = (WCHAR
)number
[i
];
478 if (format
.flags
& WPRINTF_LEFTALIGN
)
479 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
484 TRACE("%s\n",debugstr_w(buffer
));
485 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
489 /***********************************************************************
490 * wvsprintfA (USER32.@)
492 INT WINAPI
wvsprintfA( LPSTR buffer
, LPCSTR spec
, __ms_va_list args
)
494 INT res
= wvsnprintfA( buffer
, 1024, spec
, args
);
495 return ( res
== -1 ) ? 1024 : res
;
499 /***********************************************************************
500 * wvsprintfW (USER32.@)
502 INT WINAPI
wvsprintfW( LPWSTR buffer
, LPCWSTR spec
, __ms_va_list args
)
504 INT res
= wvsnprintfW( buffer
, 1024, spec
, args
);
505 return ( res
== -1 ) ? 1024 : res
;
509 /***********************************************************************
510 * wsprintfA (USER32.@)
512 INT WINAPIV
wsprintfA( LPSTR buffer
, LPCSTR spec
, ... )
517 __ms_va_start( valist
, spec
);
518 res
= wvsnprintfA( buffer
, 1024, spec
, valist
);
519 __ms_va_end( valist
);
520 return ( res
== -1 ) ? 1024 : res
;
524 /***********************************************************************
525 * wsprintfW (USER32.@)
527 INT WINAPIV
wsprintfW( LPWSTR buffer
, LPCWSTR spec
, ... )
532 __ms_va_start( valist
, spec
);
533 res
= wvsnprintfW( buffer
, 1024, spec
, valist
);
534 __ms_va_end( valist
);
535 return ( res
== -1 ) ? 1024 : res
;