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 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
37 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
38 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
39 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
40 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
41 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
42 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
72 static const CHAR null_stringA
[] = "(null)";
73 static const WCHAR null_stringW
[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
75 /***********************************************************************
76 * WPRINTF_ParseFormatA
78 * Parse a format specification. A format specification has the form:
80 * [-][#][0][width][.precision]type
82 * Return value is the length of the format specification in characters.
84 static INT
WPRINTF_ParseFormatA( LPCSTR format
, WPRINTF_FORMAT
*res
)
91 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
92 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
93 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
94 while ((*p
>= '0') && (*p
<= '9')) /* width field */
96 res
->width
= res
->width
* 10 + *p
- '0';
99 if (*p
== '.') /* precision field */
102 while ((*p
>= '0') && (*p
<= '9'))
104 res
->precision
= res
->precision
* 10 + *p
- '0';
108 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
109 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
110 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
114 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
117 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
121 res
->type
= WPR_SIGNED
;
124 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
127 res
->type
= (res
->flags
& (WPRINTF_SHORT
|WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
130 res
->type
= WPR_UNSIGNED
;
134 res
->flags
|= WPRINTF_ZEROPAD
;
137 res
->flags
|= WPRINTF_UPPER_HEX
;
140 res
->type
= WPR_HEXA
;
142 default: /* unknown format char */
143 res
->type
= WPR_UNKNOWN
;
144 p
--; /* print format as normal char */
147 return (INT
)(p
- format
) + 1;
151 /***********************************************************************
152 * WPRINTF_ParseFormatW
154 * Parse a format specification. A format specification has the form:
156 * [-][#][0][width][.precision]type
158 * Return value is the length of the format specification in characters.
160 static INT
WPRINTF_ParseFormatW( LPCWSTR format
, WPRINTF_FORMAT
*res
)
167 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
168 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
169 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
170 while ((*p
>= '0') && (*p
<= '9')) /* width field */
172 res
->width
= res
->width
* 10 + *p
- '0';
175 if (*p
== '.') /* precision field */
178 while ((*p
>= '0') && (*p
<= '9'))
180 res
->precision
= res
->precision
* 10 + *p
- '0';
184 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
185 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
186 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
190 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
193 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
197 res
->type
= WPR_SIGNED
;
200 res
->type
= ((res
->flags
& WPRINTF_SHORT
) && !(res
->flags
& WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
203 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
206 res
->type
= WPR_UNSIGNED
;
210 res
->flags
|= WPRINTF_ZEROPAD
;
213 res
->flags
|= WPRINTF_UPPER_HEX
;
216 res
->type
= WPR_HEXA
;
219 res
->type
= WPR_UNKNOWN
;
220 p
--; /* print format as normal char */
223 return (INT
)(p
- format
) + 1;
227 /***********************************************************************
230 static UINT
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, WPRINTF_DATA
*arg
,
231 LPSTR number
, UINT maxlen
)
235 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
236 if (format
->width
> maxlen
) format
->width
= maxlen
;
241 return (format
->precision
= 1);
243 if (!arg
->lpcstr_view
) arg
->lpcstr_view
= null_stringA
;
244 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
245 if (!*(arg
->lpcstr_view
+ len
)) break;
246 if (len
> maxlen
) len
= maxlen
;
247 return (format
->precision
= len
);
249 if (!arg
->lpcwstr_view
) arg
->lpcwstr_view
= null_stringW
;
250 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
251 if (!*(arg
->lpcwstr_view
+ len
)) break;
252 if (len
> maxlen
) len
= maxlen
;
253 return (format
->precision
= len
);
255 len
= sprintf( number
, "%d", arg
->int_view
);
258 len
= sprintf( number
, "%u", (UINT
)arg
->int_view
);
261 len
= sprintf( number
,
262 (format
->flags
& WPRINTF_UPPER_HEX
) ? "%X" : "%x",
263 (UINT
)arg
->int_view
);
268 if (len
> maxlen
) len
= maxlen
;
269 if (format
->precision
< len
) format
->precision
= len
;
270 if (format
->precision
> maxlen
) format
->precision
= maxlen
;
271 if ((format
->flags
& WPRINTF_ZEROPAD
) && (format
->width
> format
->precision
))
272 format
->precision
= format
->width
;
273 if (format
->flags
& WPRINTF_PREFIX_HEX
) len
+= 2;
277 /***********************************************************************
278 * wvsnprintfA (internal)
280 static INT
wvsnprintfA( LPSTR buffer
, UINT maxlen
, LPCSTR spec
, va_list args
)
282 WPRINTF_FORMAT format
;
286 WPRINTF_DATA argData
;
288 //TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
290 while (*spec
&& (maxlen
> 1))
292 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
294 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
295 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
300 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
303 argData
.char_view
= (CHAR
)va_arg( args
, int );
306 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
309 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
314 argData
.int_view
= va_arg( args
, INT
);
317 argData
.wchar_view
= 0;
321 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
323 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
324 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
329 *p
++ = argData
.wchar_view
;
332 *p
++ = argData
.char_view
;
335 memcpy( p
, argData
.lpcstr_view
, len
);
340 LPCWSTR ptr
= argData
.lpcwstr_view
;
341 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
345 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
348 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
354 /* Transfer the sign now, just in case it will be zero-padded*/
355 if (number
[0] == '-')
362 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
363 memcpy( p
, number
+ sign
, len
- sign
);
369 if (format
.flags
& WPRINTF_LEFTALIGN
)
370 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
375 //TRACE("%s\n",debugstr_a(buffer));
376 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
380 /***********************************************************************
381 * wvsnprintfW (internal)
383 static INT
wvsnprintfW( LPWSTR buffer
, UINT maxlen
, LPCWSTR spec
, va_list args
)
385 WPRINTF_FORMAT format
;
389 WPRINTF_DATA argData
;
391 //TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
393 while (*spec
&& (maxlen
> 1))
395 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
397 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
398 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
403 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
406 argData
.char_view
= (CHAR
)va_arg( args
, int );
409 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
412 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
417 argData
.int_view
= va_arg( args
, INT
);
420 argData
.wchar_view
= 0;
424 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
426 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
427 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
432 *p
++ = argData
.wchar_view
;
435 *p
++ = argData
.char_view
;
439 LPCSTR ptr
= argData
.lpcstr_view
;
440 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)*ptr
++;
444 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
448 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
451 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
457 /* Transfer the sign now, just in case it will be zero-padded*/
458 if (number
[0] == '-')
465 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
466 for (i
= sign
; i
< len
; i
++) *p
++ = (WCHAR
)number
[i
];
471 if (format
.flags
& WPRINTF_LEFTALIGN
)
472 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
477 //TRACE("%s\n",debugstr_w(buffer));
478 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
481 /***********************************************************************
482 * wvsprintfA (USER32.@)
485 INT STDCALL
wvsprintfA( LPSTR buffer
, LPCSTR spec
, va_list args
)
487 INT res
= wvsnprintfA( buffer
, 1024, spec
, args
);
488 return ( res
== -1 ) ? 1024 : res
;
492 /***********************************************************************
493 * wvsprintfW (USER32.@)
496 INT STDCALL
wvsprintfW( LPWSTR buffer
, LPCWSTR spec
, va_list args
)
498 INT res
= wvsnprintfW( buffer
, 1024, spec
, args
);
499 return ( res
== -1 ) ? 1024 : res
;
502 /***********************************************************************
503 * wsprintfA (USER32.@)
506 INT CDECL
wsprintfA( LPSTR buffer
, LPCSTR spec
, ... )
511 va_start( valist
, spec
);
512 res
= wvsnprintfA( buffer
, 1024, spec
, valist
);
514 return ( res
== -1 ) ? 1024 : res
;
517 /***********************************************************************
518 * wsprintfW (USER32.@)
521 INT CDECL
wsprintfW( LPWSTR buffer
, LPCWSTR spec
, ... )
526 va_start( valist
, spec
);
527 res
= wvsnprintfW( buffer
, 1024, spec
, valist
);
529 return ( res
== -1 ) ? 1024 : res
;
534 DWORD STDCALL
WCSToMBEx(WORD CodePage
,LPWSTR UnicodeString
,LONG UnicodeSize
,LPSTR
*MBString
,LONG MBSize
,BOOL Allocate
)
537 if (UnicodeSize
== -1)
539 UnicodeSize
= wcslen(UnicodeString
)+1;
547 MBSize
= UnicodeSize
* 2;
551 *MBString
= RtlAllocateHeap(GetProcessHeap(), 0, MBSize
);
555 RtlUnicodeToMultiByteN(*MBString
,MBSize
,&Size
,UnicodeString
,UnicodeSize
);
559 WideCharToMultiByte(CodePage
,0,UnicodeString
,UnicodeSize
,*MBString
,MBSize
,0,0);
566 DWORD STDCALL
MBToWCSEx(WORD CodePage
,LPSTR MBString
,LONG MBSize
,LPWSTR
*UnicodeString
,LONG UnicodeSize
,BOOL Allocate
)
571 MBSize
= strlen(MBString
)+1;
573 if (UnicodeSize
== -1)
579 UnicodeSize
= MBSize
;
583 *UnicodeString
= RtlAllocateHeap(GetProcessHeap(), 0, UnicodeSize
);
585 UnicodeSize
*= sizeof(WCHAR
);
588 RtlMultiByteToUnicodeN(*UnicodeString
,UnicodeSize
,&Size
,MBString
,MBSize
);
592 Size
= MultiByteToWideChar(CodePage
,0,MBString
,MBSize
,*UnicodeString
,UnicodeSize
);
596 const LPWSTR strings
[14] = {L
"OK",L
"Cancel",L
"&Abort",L
"&Retry",L
"&Ignore",L
"&Yes",L
"&No",L
"&Close",L
"Help",L
"&Try Again",L
"&Continue"};
600 LPWSTR STDCALL
MB_GetString(DWORD string
)
602 return heap_string_poolW(strings
[string
],wcslen(strings
[string
]));