4 * Copyright 1996 Alexandre Julliard
15 #include "wine/winbase16.h"
16 #include "wine/winuser16.h"
17 #include "stackframe.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(string
);
24 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
25 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
26 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
27 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
28 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
29 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
30 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
60 static const CHAR null_stringA
[] = "(null)";
61 static const WCHAR null_stringW
[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
63 /***********************************************************************
64 * WPRINTF_ParseFormatA
66 * Parse a format specification. A format specification has the form:
68 * [-][#][0][width][.precision]type
70 * Return value is the length of the format specification in characters.
72 static INT
WPRINTF_ParseFormatA( LPCSTR format
, WPRINTF_FORMAT
*res
)
79 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
80 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
81 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
82 while ((*p
>= '0') && (*p
<= '9')) /* width field */
84 res
->width
= res
->width
* 10 + *p
- '0';
87 if (*p
== '.') /* precision field */
90 while ((*p
>= '0') && (*p
<= '9'))
92 res
->precision
= res
->precision
* 10 + *p
- '0';
96 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
97 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
98 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
102 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
105 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
109 res
->type
= WPR_SIGNED
;
112 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
115 res
->type
= (res
->flags
& (WPRINTF_SHORT
|WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
118 res
->type
= WPR_UNSIGNED
;
121 res
->flags
|= WPRINTF_UPPER_HEX
;
124 res
->type
= WPR_HEXA
;
126 default: /* unknown format char */
127 res
->type
= WPR_UNKNOWN
;
128 p
--; /* print format as normal char */
131 return (INT
)(p
- format
) + 1;
135 /***********************************************************************
136 * WPRINTF_ParseFormatW
138 * Parse a format specification. A format specification has the form:
140 * [-][#][0][width][.precision]type
142 * Return value is the length of the format specification in characters.
144 static INT
WPRINTF_ParseFormatW( LPCWSTR format
, WPRINTF_FORMAT
*res
)
151 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
152 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
153 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
154 while ((*p
>= '0') && (*p
<= '9')) /* width field */
156 res
->width
= res
->width
* 10 + *p
- '0';
159 if (*p
== '.') /* precision field */
162 while ((*p
>= '0') && (*p
<= '9'))
164 res
->precision
= res
->precision
* 10 + *p
- '0';
168 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
169 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
170 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
174 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
177 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
181 res
->type
= WPR_SIGNED
;
184 res
->type
= ((res
->flags
& WPRINTF_SHORT
) && !(res
->flags
& WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
187 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
190 res
->type
= WPR_UNSIGNED
;
193 res
->flags
|= WPRINTF_UPPER_HEX
;
196 res
->type
= WPR_HEXA
;
199 res
->type
= WPR_UNKNOWN
;
200 p
--; /* print format as normal char */
203 return (INT
)(p
- format
) + 1;
207 /***********************************************************************
210 static UINT
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, WPRINTF_DATA
*arg
,
211 LPSTR number
, UINT maxlen
)
215 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
216 if (format
->width
> maxlen
) format
->width
= maxlen
;
221 return (format
->precision
= 1);
223 if (!arg
->lpcstr_view
) arg
->lpcstr_view
= null_stringA
;
224 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
225 if (!*(arg
->lpcstr_view
+ len
)) break;
226 if (len
> maxlen
) len
= maxlen
;
227 return (format
->precision
= len
);
229 if (!arg
->lpcwstr_view
) arg
->lpcwstr_view
= null_stringW
;
230 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
231 if (!*(arg
->lpcwstr_view
+ len
)) break;
232 if (len
> maxlen
) len
= maxlen
;
233 return (format
->precision
= len
);
235 len
= sprintf( number
, "%d", arg
->int_view
);
238 len
= sprintf( number
, "%u", (UINT
)arg
->int_view
);
241 len
= sprintf( number
,
242 (format
->flags
& WPRINTF_UPPER_HEX
) ? "%X" : "%x",
243 (UINT
)arg
->int_view
);
248 if (len
> maxlen
) len
= maxlen
;
249 if (format
->precision
< len
) format
->precision
= len
;
250 if (format
->precision
> maxlen
) format
->precision
= maxlen
;
251 if ((format
->flags
& WPRINTF_ZEROPAD
) && (format
->width
> format
->precision
))
252 format
->precision
= format
->width
;
253 if (format
->flags
& WPRINTF_PREFIX_HEX
) len
+= 2;
258 /***********************************************************************
259 * wvsnprintf16 (Not a Windows API)
261 static INT16
wvsnprintf16( LPSTR buffer
, UINT16 maxlen
, LPCSTR spec
,
264 WPRINTF_FORMAT format
;
268 WPRINTF_DATA cur_arg
;
271 while (*spec
&& (maxlen
> 1))
273 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
275 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
276 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
279 case WPR_WCHAR
: /* No Unicode in Win16 */
281 cur_arg
.char_view
= VA_ARG16( args
, CHAR
);
283 case WPR_WSTRING
: /* No Unicode in Win16 */
285 seg_str
= VA_ARG16( args
, SEGPTR
);
286 if (IsBadReadPtr16(seg_str
, 1 )) cur_arg
.lpcstr_view
= "";
287 else cur_arg
.lpcstr_view
= MapSL( seg_str
);
290 if (!(format
.flags
& WPRINTF_LONG
))
292 cur_arg
.int_view
= VA_ARG16( args
, INT16
);
298 if (format
.flags
& WPRINTF_LONG
)
299 cur_arg
.int_view
= VA_ARG16( args
, UINT
);
301 cur_arg
.int_view
= VA_ARG16( args
, UINT16
);
306 len
= WPRINTF_GetLen( &format
, &cur_arg
, number
, maxlen
- 1 );
307 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
308 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
312 case WPR_WCHAR
: /* No Unicode in Win16 */
314 *p
= cur_arg
.char_view
;
316 else if (format
.width
> 1) *p
++ = ' ';
319 case WPR_WSTRING
: /* No Unicode in Win16 */
321 if (len
) memcpy( p
, cur_arg
.lpcstr_view
, len
);
325 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
328 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
335 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
336 if (len
) memcpy( p
, number
, len
);
342 if (format
.flags
& WPRINTF_LEFTALIGN
)
343 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
348 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
352 /***********************************************************************
353 * wvsnprintfA (USER32.@) (Not a Windows API, but we export it from USER32 anyway)
355 INT WINAPI
wvsnprintfA( LPSTR buffer
, UINT maxlen
, LPCSTR spec
, va_list args
)
357 WPRINTF_FORMAT format
;
361 WPRINTF_DATA argData
;
363 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_a(spec
));
365 while (*spec
&& (maxlen
> 1))
367 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
369 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
370 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
375 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
378 argData
.char_view
= (CHAR
)va_arg( args
, int );
381 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
384 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
389 argData
.int_view
= va_arg( args
, INT
);
392 argData
.wchar_view
= 0;
396 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
397 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
398 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
403 *p
= argData
.wchar_view
;
405 else if (format
.width
> 1) *p
++ = ' ';
409 *p
= argData
.char_view
;
411 else if (format
.width
> 1) *p
++ = ' ';
415 memcpy( p
, argData
.lpcstr_view
, len
);
420 LPCWSTR ptr
= argData
.lpcwstr_view
;
421 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
425 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
428 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
435 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
436 memcpy( p
, number
, len
);
442 if (format
.flags
& WPRINTF_LEFTALIGN
)
443 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
448 TRACE("%s\n",debugstr_a(buffer
));
449 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
453 /***********************************************************************
454 * wvsnprintfW (USER32.@) (Not a Windows API, but we export it from USER32 anyway)
456 INT WINAPI
wvsnprintfW( LPWSTR buffer
, UINT maxlen
, LPCWSTR spec
, va_list args
)
458 WPRINTF_FORMAT format
;
462 WPRINTF_DATA argData
;
464 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_w(spec
));
466 while (*spec
&& (maxlen
> 1))
468 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
470 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
471 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
476 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
479 argData
.char_view
= (CHAR
)va_arg( args
, int );
482 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
485 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
490 argData
.int_view
= va_arg( args
, INT
);
493 argData
.wchar_view
= 0;
497 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
498 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
499 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
504 *p
= argData
.wchar_view
;
506 else if (format
.width
> 1) *p
++ = ' ';
510 *p
= argData
.char_view
;
512 else if (format
.width
> 1) *p
++ = ' ';
517 LPCSTR ptr
= argData
.lpcstr_view
;
518 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)*ptr
++;
522 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
526 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
529 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
536 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
537 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)number
[i
];
542 if (format
.flags
& WPRINTF_LEFTALIGN
)
543 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
548 TRACE("%s\n",debugstr_w(buffer
));
549 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
553 /***********************************************************************
554 * wvsprintf (USER.421)
556 INT16 WINAPI
wvsprintf16( LPSTR buffer
, LPCSTR spec
, LPCVOID args
)
560 TRACE("for %p got:\n",buffer
);
561 res
= wvsnprintf16( buffer
, 1024, spec
, args
);
562 return ( res
== -1 ) ? 1024 : res
;
566 /***********************************************************************
567 * wvsprintfA (USER32.@)
569 INT WINAPI
wvsprintfA( LPSTR buffer
, LPCSTR spec
, va_list args
)
571 INT res
= wvsnprintfA( buffer
, 1024, spec
, args
);
572 return ( res
== -1 ) ? 1024 : res
;
576 /***********************************************************************
577 * wvsprintfW (USER32.@)
579 INT WINAPI
wvsprintfW( LPWSTR buffer
, LPCWSTR spec
, va_list args
)
581 INT res
= wvsnprintfW( buffer
, 1024, spec
, args
);
582 return ( res
== -1 ) ? 1024 : res
;
586 /***********************************************************************
587 * _wsprintf (USER.420)
589 INT16 WINAPIV
wsprintf16(void)
595 VA_START16( valist
);
596 buffer
= VA_ARG16( valist
, SEGPTR
);
597 spec
= VA_ARG16( valist
, SEGPTR
);
598 res
= wvsnprintf16( MapSL(buffer
), 1024, MapSL(spec
), valist
);
600 return ( res
== -1 ) ? 1024 : res
;
604 /***********************************************************************
605 * wsprintfA (USER32.@)
607 INT WINAPIV
wsprintfA( LPSTR buffer
, LPCSTR spec
, ... )
612 va_start( valist
, spec
);
613 res
= wvsnprintfA( buffer
, 1024, spec
, valist
);
615 return ( res
== -1 ) ? 1024 : res
;
619 /***********************************************************************
620 * wsprintfW (USER32.@)
622 INT WINAPIV
wsprintfW( LPWSTR buffer
, LPCWSTR spec
, ... )
627 va_start( valist
, spec
);
628 res
= wvsnprintfW( buffer
, 1024, spec
, valist
);
630 return ( res
== -1 ) ? 1024 : res
;