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.
34 #include "wine/winbase16.h"
35 #include "wine/winuser16.h"
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 * wvsnprintf16 (Not a Windows API)
287 static INT16
wvsnprintf16( LPSTR buffer
, UINT16 maxlen
, LPCSTR spec
, VA_LIST16 args
)
289 WPRINTF_FORMAT format
;
293 WPRINTF_DATA cur_arg
;
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
);
304 case WPR_WCHAR
: /* No Unicode in Win16 */
306 cur_arg
.char_view
= VA_ARG16( args
, CHAR
);
308 case WPR_WSTRING
: /* No Unicode in Win16 */
310 seg_str
= VA_ARG16( args
, SEGPTR
);
311 if (IsBadReadPtr16(seg_str
, 1 )) cur_arg
.lpcstr_view
= "";
312 else cur_arg
.lpcstr_view
= MapSL( seg_str
);
315 if (!(format
.flags
& WPRINTF_LONG
))
317 cur_arg
.int_view
= VA_ARG16( args
, INT16
);
323 if (format
.flags
& WPRINTF_LONG
)
324 cur_arg
.int_view
= VA_ARG16( args
, UINT
);
326 cur_arg
.int_view
= VA_ARG16( args
, UINT16
);
331 len
= WPRINTF_GetLen( &format
, &cur_arg
, number
, maxlen
- 1 );
333 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
334 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
338 case WPR_WCHAR
: /* No Unicode in Win16 */
340 *p
= cur_arg
.char_view
;
341 /* wsprintf16 (unlike wsprintf) ignores null characters */
343 else if (format
.width
> 1) *p
++ = ' ';
346 case WPR_WSTRING
: /* No Unicode in Win16 */
348 if (len
) memcpy( p
, cur_arg
.lpcstr_view
, len
);
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 if (len
> sign
) memcpy( p
, number
+ sign
, len
- sign
);
376 if (format
.flags
& WPRINTF_LEFTALIGN
)
377 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
382 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
386 /***********************************************************************
387 * wvsnprintfA (internal)
389 static INT
wvsnprintfA( LPSTR buffer
, UINT maxlen
, LPCSTR spec
, va_list args
)
391 WPRINTF_FORMAT format
;
395 WPRINTF_DATA argData
;
397 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_a(spec
));
399 while (*spec
&& (maxlen
> 1))
401 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
403 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
404 spec
+= WPRINTF_ParseFormatA( 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
;
444 memcpy( p
, argData
.lpcstr_view
, len
);
449 LPCWSTR ptr
= argData
.lpcwstr_view
;
450 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
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 memcpy( p
, number
+ sign
, len
- sign
);
478 if (format
.flags
& WPRINTF_LEFTALIGN
)
479 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
484 TRACE("%s\n",debugstr_a(buffer
));
485 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
489 /***********************************************************************
490 * wvsnprintfW (internal)
492 static INT
wvsnprintfW( LPWSTR buffer
, UINT maxlen
, LPCWSTR spec
, va_list args
)
494 WPRINTF_FORMAT format
;
498 WPRINTF_DATA argData
;
500 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_w(spec
));
502 while (*spec
&& (maxlen
> 1))
504 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
506 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
507 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
512 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
515 argData
.char_view
= (CHAR
)va_arg( args
, int );
518 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
521 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
526 argData
.int_view
= va_arg( args
, INT
);
529 argData
.wchar_view
= 0;
533 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
535 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
536 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
541 *p
++ = argData
.wchar_view
;
544 *p
++ = argData
.char_view
;
548 LPCSTR ptr
= argData
.lpcstr_view
;
549 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)*ptr
++;
553 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
557 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
560 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
566 /* Transfer the sign now, just in case it will be zero-padded*/
567 if (number
[0] == '-')
574 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
575 for (i
= sign
; i
< len
; i
++) *p
++ = (WCHAR
)number
[i
];
580 if (format
.flags
& WPRINTF_LEFTALIGN
)
581 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
586 TRACE("%s\n",debugstr_w(buffer
));
587 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
591 /***********************************************************************
592 * wvsprintf (USER.421)
594 INT16 WINAPI
wvsprintf16( LPSTR buffer
, LPCSTR spec
, VA_LIST16 args
)
598 TRACE("for %p got:\n",buffer
);
599 res
= wvsnprintf16( buffer
, 1024, spec
, args
);
600 return ( res
== -1 ) ? 1024 : res
;
604 /***********************************************************************
605 * wvsprintfA (USER32.@)
607 INT WINAPI
wvsprintfA( LPSTR buffer
, LPCSTR spec
, va_list args
)
609 INT res
= wvsnprintfA( buffer
, 1024, spec
, args
);
610 return ( res
== -1 ) ? 1024 : res
;
614 /***********************************************************************
615 * wvsprintfW (USER32.@)
617 INT WINAPI
wvsprintfW( LPWSTR buffer
, LPCWSTR spec
, va_list args
)
619 INT res
= wvsnprintfW( buffer
, 1024, spec
, args
);
620 return ( res
== -1 ) ? 1024 : res
;
624 /***********************************************************************
625 * _wsprintf (USER.420)
627 INT16 WINAPIV
wsprintf16( LPSTR buffer
, LPCSTR spec
, VA_LIST16 valist
)
631 res
= wvsnprintf16( buffer
, 1024, spec
, valist
);
632 return ( res
== -1 ) ? 1024 : res
;
636 /***********************************************************************
637 * wsprintfA (USER32.@)
639 INT WINAPIV
wsprintfA( LPSTR buffer
, LPCSTR spec
, ... )
644 va_start( valist
, spec
);
645 res
= wvsnprintfA( buffer
, 1024, spec
, valist
);
647 return ( res
== -1 ) ? 1024 : res
;
651 /***********************************************************************
652 * wsprintfW (USER32.@)
654 INT WINAPIV
wsprintfW( LPWSTR buffer
, LPCWSTR spec
, ... )
659 va_start( valist
, spec
);
660 res
= wvsnprintfW( buffer
, 1024, spec
, valist
);
662 return ( res
== -1 ) ? 1024 : res
;