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;
283 /***********************************************************************
284 * wvsnprintfA (internal)
286 static INT
wvsnprintfA( LPSTR buffer
, UINT maxlen
, LPCSTR spec
, __ms_va_list args
)
288 WPRINTF_FORMAT format
;
292 WPRINTF_DATA argData
;
294 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_a(spec
));
296 while (*spec
&& (maxlen
> 1))
298 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
300 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
301 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
306 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
309 argData
.char_view
= (CHAR
)va_arg( args
, int );
312 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
315 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
320 argData
.int_view
= va_arg( args
, INT
);
323 argData
.wchar_view
= 0;
327 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
329 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
330 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
335 *p
++ = argData
.wchar_view
;
338 *p
++ = argData
.char_view
;
341 memcpy( p
, argData
.lpcstr_view
, len
);
346 LPCWSTR ptr
= argData
.lpcwstr_view
;
347 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
351 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
354 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
360 /* Transfer the sign now, just in case it will be zero-padded*/
361 if (number
[0] == '-')
368 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
369 memcpy( p
, number
+ sign
, len
- sign
);
375 if (format
.flags
& WPRINTF_LEFTALIGN
)
376 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
381 TRACE("%s\n",debugstr_a(buffer
));
382 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
386 /***********************************************************************
387 * wvsnprintfW (internal)
389 static INT
wvsnprintfW( LPWSTR buffer
, UINT maxlen
, LPCWSTR spec
, __ms_va_list args
)
391 WPRINTF_FORMAT format
;
395 WPRINTF_DATA argData
;
397 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_w(spec
));
399 while (*spec
&& (maxlen
> 1))
401 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
403 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
404 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
409 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
412 argData
.char_view
= (CHAR
)va_arg( args
, int );
415 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
418 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
423 argData
.int_view
= va_arg( args
, INT
);
426 argData
.wchar_view
= 0;
430 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
432 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
433 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
438 *p
++ = argData
.wchar_view
;
441 *p
++ = argData
.char_view
;
445 LPCSTR ptr
= argData
.lpcstr_view
;
446 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)*ptr
++;
450 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
454 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
457 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
463 /* Transfer the sign now, just in case it will be zero-padded*/
464 if (number
[0] == '-')
471 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
472 for (i
= sign
; i
< len
; i
++) *p
++ = (WCHAR
)number
[i
];
477 if (format
.flags
& WPRINTF_LEFTALIGN
)
478 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
483 TRACE("%s\n",debugstr_w(buffer
));
484 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
487 /***********************************************************************
488 * wvsprintfA (USER32.@)
490 INT WINAPI
wvsprintfA( LPSTR buffer
, LPCSTR spec
, __ms_va_list args
)
492 INT res
= wvsnprintfA( buffer
, 1024, spec
, args
);
493 return ( res
== -1 ) ? 1024 : res
;
497 /***********************************************************************
498 * wvsprintfW (USER32.@)
500 INT WINAPI
wvsprintfW( LPWSTR buffer
, LPCWSTR spec
, __ms_va_list args
)
502 INT res
= wvsnprintfW( buffer
, 1024, spec
, args
);
503 return ( res
== -1 ) ? 1024 : res
;
506 /***********************************************************************
507 * wsprintfA (USER32.@)
509 INT WINAPIV
wsprintfA( LPSTR buffer
, LPCSTR spec
, ... )
514 __ms_va_start( valist
, spec
);
515 res
= wvsnprintfA( buffer
, 1024, spec
, valist
);
516 __ms_va_end( valist
);
517 return ( res
== -1 ) ? 1024 : res
;
520 /***********************************************************************
521 * wsprintfW (USER32.@)
523 INT WINAPIV
wsprintfW( LPWSTR buffer
, LPCWSTR spec
, ... )
528 __ms_va_start( valist
, spec
);
529 res
= wvsnprintfW( buffer
, 1024, spec
, valist
);
530 __ms_va_end( valist
);
531 return ( res
== -1 ) ? 1024 : res
;