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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 #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) */
77 static const CHAR null_stringA
[] = "(null)";
78 static const WCHAR null_stringW
[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
80 /***********************************************************************
81 * WPRINTF_ParseFormatA
83 * Parse a format specification. A format specification has the form:
85 * [-][#][0][width][.precision]type
87 * Return value is the length of the format specification in characters.
89 static INT
WPRINTF_ParseFormatA( LPCSTR format
, WPRINTF_FORMAT
*res
)
96 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
97 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
98 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
99 while ((*p
>= '0') && (*p
<= '9')) /* width field */
101 res
->width
= res
->width
* 10 + *p
- '0';
104 if (*p
== '.') /* precision field */
107 while ((*p
>= '0') && (*p
<= '9'))
109 res
->precision
= res
->precision
* 10 + *p
- '0';
113 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
114 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
115 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
119 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
122 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
126 res
->type
= WPR_SIGNED
;
129 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
132 res
->type
= (res
->flags
& (WPRINTF_SHORT
|WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
135 res
->type
= WPR_UNSIGNED
;
139 res
->flags
|= WPRINTF_ZEROPAD
;
142 res
->flags
|= WPRINTF_UPPER_HEX
;
145 res
->type
= WPR_HEXA
;
147 default: /* unknown format char */
148 res
->type
= WPR_UNKNOWN
;
149 p
--; /* print format as normal char */
152 return (INT
)(p
- format
) + 1;
156 /***********************************************************************
157 * WPRINTF_ParseFormatW
159 * Parse a format specification. A format specification has the form:
161 * [-][#][0][width][.precision]type
163 * Return value is the length of the format specification in characters.
165 static INT
WPRINTF_ParseFormatW( LPCWSTR format
, WPRINTF_FORMAT
*res
)
172 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
173 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
174 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
175 while ((*p
>= '0') && (*p
<= '9')) /* width field */
177 res
->width
= res
->width
* 10 + *p
- '0';
180 if (*p
== '.') /* precision field */
183 while ((*p
>= '0') && (*p
<= '9'))
185 res
->precision
= res
->precision
* 10 + *p
- '0';
189 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
190 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
191 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
195 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
198 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
202 res
->type
= WPR_SIGNED
;
205 res
->type
= ((res
->flags
& WPRINTF_SHORT
) && !(res
->flags
& WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
208 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
211 res
->type
= WPR_UNSIGNED
;
215 res
->flags
|= WPRINTF_ZEROPAD
;
218 res
->flags
|= WPRINTF_UPPER_HEX
;
221 res
->type
= WPR_HEXA
;
224 res
->type
= WPR_UNKNOWN
;
225 p
--; /* print format as normal char */
228 return (INT
)(p
- format
) + 1;
232 /***********************************************************************
235 static UINT
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, WPRINTF_DATA
*arg
,
236 LPSTR number
, UINT maxlen
)
240 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
241 if (format
->width
> maxlen
) format
->width
= maxlen
;
246 return (format
->precision
= 1);
248 if (!arg
->lpcstr_view
) arg
->lpcstr_view
= null_stringA
;
249 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
250 if (!*(arg
->lpcstr_view
+ len
)) break;
251 if (len
> maxlen
) len
= maxlen
;
252 return (format
->precision
= len
);
254 if (!arg
->lpcwstr_view
) arg
->lpcwstr_view
= null_stringW
;
255 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
256 if (!*(arg
->lpcwstr_view
+ len
)) break;
257 if (len
> maxlen
) len
= maxlen
;
258 return (format
->precision
= len
);
260 len
= sprintf( number
, "%d", arg
->int_view
);
263 len
= sprintf( number
, "%u", (UINT
)arg
->int_view
);
266 len
= sprintf( number
,
267 (format
->flags
& WPRINTF_UPPER_HEX
) ? "%X" : "%x",
268 (UINT
)arg
->int_view
);
273 if (len
> maxlen
) len
= maxlen
;
274 if (format
->precision
< len
) format
->precision
= len
;
275 if (format
->precision
> maxlen
) format
->precision
= maxlen
;
276 if ((format
->flags
& WPRINTF_ZEROPAD
) && (format
->width
> format
->precision
))
277 format
->precision
= format
->width
;
278 if (format
->flags
& WPRINTF_PREFIX_HEX
) len
+= 2;
282 /***********************************************************************
283 * wvsnprintfA (internal)
285 static INT
wvsnprintfA( LPSTR buffer
, UINT maxlen
, LPCSTR spec
, va_list args
)
287 WPRINTF_FORMAT format
;
291 WPRINTF_DATA argData
;
293 //TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
295 while (*spec
&& (maxlen
> 1))
297 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
299 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
300 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
305 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
308 argData
.char_view
= (CHAR
)va_arg( args
, int );
311 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
314 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
319 argData
.int_view
= va_arg( args
, INT
);
322 argData
.wchar_view
= 0;
326 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
328 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
329 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
334 *p
++ = argData
.wchar_view
;
337 *p
++ = argData
.char_view
;
340 memcpy( p
, argData
.lpcstr_view
, len
);
345 LPCWSTR ptr
= argData
.lpcwstr_view
;
346 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
350 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
353 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
359 /* Transfer the sign now, just in case it will be zero-padded*/
360 if (number
[0] == '-')
367 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
368 memcpy( p
, number
+ sign
, len
- sign
);
374 if (format
.flags
& WPRINTF_LEFTALIGN
)
375 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
380 //TRACE("%s\n",debugstr_a(buffer));
381 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
385 /***********************************************************************
386 * wvsnprintfW (internal)
388 static INT
wvsnprintfW( LPWSTR buffer
, UINT maxlen
, LPCWSTR spec
, va_list args
)
390 WPRINTF_FORMAT format
;
394 WPRINTF_DATA argData
;
396 //TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
398 while (*spec
&& (maxlen
> 1))
400 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
402 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
403 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
408 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
411 argData
.char_view
= (CHAR
)va_arg( args
, int );
414 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
417 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
422 argData
.int_view
= va_arg( args
, INT
);
425 argData
.wchar_view
= 0;
429 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
431 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
432 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
437 *p
++ = argData
.wchar_view
;
440 *p
++ = argData
.char_view
;
444 LPCSTR ptr
= argData
.lpcstr_view
;
445 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)*ptr
++;
449 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
453 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
456 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
462 /* Transfer the sign now, just in case it will be zero-padded*/
463 if (number
[0] == '-')
470 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
471 for (i
= sign
; i
< len
; i
++) *p
++ = (WCHAR
)number
[i
];
476 if (format
.flags
& WPRINTF_LEFTALIGN
)
477 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
482 //TRACE("%s\n",debugstr_w(buffer));
483 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
486 /***********************************************************************
487 * wvsprintfA (USER32.@)
490 INT WINAPI
wvsprintfA( LPSTR buffer
, LPCSTR spec
, va_list args
)
492 INT res
= wvsnprintfA( buffer
, 1024, spec
, args
);
493 return ( res
== -1 ) ? 1024 : res
;
497 /***********************************************************************
498 * wvsprintfW (USER32.@)
501 INT WINAPI
wvsprintfW( LPWSTR buffer
, LPCWSTR spec
, va_list args
)
503 INT res
= wvsnprintfW( buffer
, 1024, spec
, args
);
504 return ( res
== -1 ) ? 1024 : res
;
507 /***********************************************************************
508 * wsprintfA (USER32.@)
511 INT WINAPIV
wsprintfA( LPSTR buffer
, LPCSTR spec
, ... )
516 va_start( valist
, spec
);
517 res
= wvsnprintfA( buffer
, 1024, spec
, valist
);
519 return ( res
== -1 ) ? 1024 : res
;
522 /***********************************************************************
523 * wsprintfW (USER32.@)
526 INT WINAPIV
wsprintfW( LPWSTR buffer
, LPCWSTR spec
, ... )
531 va_start( valist
, spec
);
532 res
= wvsnprintfW( buffer
, 1024, spec
, valist
);
534 return ( res
== -1 ) ? 1024 : res
;
539 DWORD STDCALL
WCSToMBEx(WORD CodePage
,LPWSTR UnicodeString
,LONG UnicodeSize
,LPSTR
*MBString
,LONG MBSize
,BOOL Allocate
)
542 if (UnicodeSize
== -1)
544 UnicodeSize
= wcslen(UnicodeString
)+1;
552 MBSize
= UnicodeSize
* 2;
556 *MBString
= RtlAllocateHeap(GetProcessHeap(), 0, MBSize
);
560 RtlUnicodeToMultiByteN(*MBString
,MBSize
,&Size
,UnicodeString
,UnicodeSize
);
564 WideCharToMultiByte(CodePage
,0,UnicodeString
,UnicodeSize
,*MBString
,MBSize
,0,0);
571 DWORD STDCALL
MBToWCSEx(WORD CodePage
,LPSTR MBString
,LONG MBSize
,LPWSTR
*UnicodeString
,LONG UnicodeSize
,BOOL Allocate
)
576 MBSize
= strlen(MBString
)+1;
578 if (UnicodeSize
== -1)
584 UnicodeSize
= MBSize
;
588 *UnicodeString
= RtlAllocateHeap(GetProcessHeap(), 0, UnicodeSize
);
590 UnicodeSize
*= sizeof(WCHAR
);
593 RtlMultiByteToUnicodeN(*UnicodeString
,UnicodeSize
,&Size
,MBString
,MBSize
);
597 Size
= MultiByteToWideChar(CodePage
,0,MBString
,MBSize
,*UnicodeString
,UnicodeSize
);