4 * Copyright 1996 Alexandre Julliard
10 #include "wine/winbase16.h"
15 #include "stackframe.h"
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(string
);
22 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
23 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
24 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
25 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
26 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
27 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
28 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
58 static const CHAR null_stringA
[] = "(null)";
59 static const WCHAR null_stringW
[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
61 /***********************************************************************
62 * WPRINTF_ParseFormatA
64 * Parse a format specification. A format specification has the form:
66 * [-][#][0][width][.precision]type
68 * Return value is the length of the format specification in characters.
70 static INT
WPRINTF_ParseFormatA( LPCSTR format
, WPRINTF_FORMAT
*res
)
77 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
78 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
79 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
80 while ((*p
>= '0') && (*p
<= '9')) /* width field */
82 res
->width
= res
->width
* 10 + *p
- '0';
85 if (*p
== '.') /* precision field */
88 while ((*p
>= '0') && (*p
<= '9'))
90 res
->precision
= res
->precision
* 10 + *p
- '0';
94 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
95 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
96 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
100 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
103 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
107 res
->type
= WPR_SIGNED
;
110 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
113 res
->type
= (res
->flags
& (WPRINTF_SHORT
|WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
116 res
->type
= WPR_UNSIGNED
;
119 res
->flags
|= WPRINTF_UPPER_HEX
;
122 res
->type
= WPR_HEXA
;
124 default: /* unknown format char */
125 res
->type
= WPR_UNKNOWN
;
126 p
--; /* print format as normal char */
129 return (INT
)(p
- format
) + 1;
133 /***********************************************************************
134 * WPRINTF_ParseFormatW
136 * Parse a format specification. A format specification has the form:
138 * [-][#][0][width][.precision]type
140 * Return value is the length of the format specification in characters.
142 static INT
WPRINTF_ParseFormatW( LPCWSTR format
, WPRINTF_FORMAT
*res
)
149 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
150 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
151 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
152 while ((*p
>= '0') && (*p
<= '9')) /* width field */
154 res
->width
= res
->width
* 10 + *p
- '0';
157 if (*p
== '.') /* precision field */
160 while ((*p
>= '0') && (*p
<= '9'))
162 res
->precision
= res
->precision
* 10 + *p
- '0';
166 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
167 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
168 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
172 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
175 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
179 res
->type
= WPR_SIGNED
;
182 res
->type
= ((res
->flags
& WPRINTF_SHORT
) && !(res
->flags
& WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
185 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
188 res
->type
= WPR_UNSIGNED
;
191 res
->flags
|= WPRINTF_UPPER_HEX
;
194 res
->type
= WPR_HEXA
;
197 res
->type
= WPR_UNKNOWN
;
198 p
--; /* print format as normal char */
201 return (INT
)(p
- format
) + 1;
205 /***********************************************************************
208 static UINT
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, WPRINTF_DATA
*arg
,
209 LPSTR number
, UINT maxlen
)
213 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
214 if (format
->width
> maxlen
) format
->width
= maxlen
;
219 return (format
->precision
= 1);
221 if (!arg
->lpcstr_view
) arg
->lpcstr_view
= null_stringA
;
222 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
223 if (!*(arg
->lpcstr_view
+ len
)) break;
224 if (len
> maxlen
) len
= maxlen
;
225 return (format
->precision
= len
);
227 if (!arg
->lpcwstr_view
) arg
->lpcwstr_view
= null_stringW
;
228 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
229 if (!*(arg
->lpcwstr_view
+ len
)) break;
230 if (len
> maxlen
) len
= maxlen
;
231 return (format
->precision
= len
);
233 len
= sprintf( number
, "%d", arg
->int_view
);
236 len
= sprintf( number
, "%u", (UINT
)arg
->int_view
);
239 len
= sprintf( number
,
240 (format
->flags
& WPRINTF_UPPER_HEX
) ? "%X" : "%x",
241 (UINT
)arg
->int_view
);
246 if (len
> maxlen
) len
= maxlen
;
247 if (format
->precision
< len
) format
->precision
= len
;
248 if (format
->precision
> maxlen
) format
->precision
= maxlen
;
249 if ((format
->flags
& WPRINTF_ZEROPAD
) && (format
->width
> format
->precision
))
250 format
->precision
= format
->width
;
251 if (format
->flags
& WPRINTF_PREFIX_HEX
) len
+= 2;
255 /***********************************************************************
256 * WPRINTF_ExtractVAPtr
258 static WPRINTF_DATA
WPRINTF_ExtractVAPtr( WPRINTF_FORMAT
*format
, va_list* args
)
264 result
.wchar_view
= (WCHAR
)va_arg( *args
, int );break;
266 result
.char_view
= (CHAR
)va_arg( *args
, int ); break;
268 result
.lpcstr_view
= va_arg( *args
, LPCSTR
); break;
270 result
.lpcwstr_view
= va_arg( *args
, LPCWSTR
); break;
274 result
.int_view
= va_arg( *args
, INT
); break;
276 result
.wchar_view
= 0; break;
281 /***********************************************************************
282 * wvsnprintf16 (Not a Windows API)
284 static INT16
wvsnprintf16( LPSTR buffer
, UINT16 maxlen
, LPCSTR spec
,
287 WPRINTF_FORMAT format
;
291 WPRINTF_DATA cur_arg
;
294 while (*spec
&& (maxlen
> 1))
296 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
298 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
299 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
302 case WPR_WCHAR
: /* No Unicode in Win16 */
304 cur_arg
.char_view
= VA_ARG16( args
, CHAR
);
306 case WPR_WSTRING
: /* No Unicode in Win16 */
308 seg_str
= VA_ARG16( args
, SEGPTR
);
309 if (IsBadReadPtr16(seg_str
, 1 )) cur_arg
.lpcstr_view
= "";
310 else cur_arg
.lpcstr_view
= PTR_SEG_TO_LIN( seg_str
);
313 if (!(format
.flags
& WPRINTF_LONG
))
315 cur_arg
.int_view
= VA_ARG16( args
, INT16
);
321 if (format
.flags
& WPRINTF_LONG
)
322 cur_arg
.int_view
= VA_ARG16( args
, UINT
);
324 cur_arg
.int_view
= VA_ARG16( args
, UINT16
);
329 len
= WPRINTF_GetLen( &format
, &cur_arg
, number
, maxlen
- 1 );
330 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
331 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
335 case WPR_WCHAR
: /* No Unicode in Win16 */
337 *p
= cur_arg
.char_view
;
339 else if (format
.width
> 1) *p
++ = ' ';
342 case WPR_WSTRING
: /* No Unicode in Win16 */
344 if (len
) memcpy( p
, cur_arg
.lpcstr_view
, len
);
348 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
351 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
358 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
359 if (len
) memcpy( p
, number
, len
);
365 if (format
.flags
& WPRINTF_LEFTALIGN
)
366 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
371 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
375 /***********************************************************************
376 * wvsnprintfA (Not a Windows API, but we export it from USER32 anyway)
378 INT WINAPI
wvsnprintfA( LPSTR buffer
, UINT maxlen
, LPCSTR spec
, va_list args
)
380 WPRINTF_FORMAT format
;
384 WPRINTF_DATA argData
;
386 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_a(spec
));
388 while (*spec
&& (maxlen
> 1))
390 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
392 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
393 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
394 argData
= WPRINTF_ExtractVAPtr( &format
, &args
);
395 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
396 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
397 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
402 *p
= argData
.wchar_view
;
404 else if (format
.width
> 1) *p
++ = ' ';
408 *p
= argData
.char_view
;
410 else if (format
.width
> 1) *p
++ = ' ';
414 memcpy( p
, argData
.lpcstr_view
, len
);
419 LPCWSTR ptr
= argData
.lpcwstr_view
;
420 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
424 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
427 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
434 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
435 memcpy( p
, number
, len
);
441 if (format
.flags
& WPRINTF_LEFTALIGN
)
442 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
447 TRACE("%s\n",debugstr_a(buffer
));
448 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
452 /***********************************************************************
453 * wvsnprintfW (Not a Windows API, but we export it from USER32 anyway)
455 INT WINAPI
wvsnprintfW( LPWSTR buffer
, UINT maxlen
, LPCWSTR spec
, va_list args
)
457 WPRINTF_FORMAT format
;
461 WPRINTF_DATA argData
;
463 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_w(spec
));
465 while (*spec
&& (maxlen
> 1))
467 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
469 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
470 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
471 argData
= WPRINTF_ExtractVAPtr( &format
, &args
);
472 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
473 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
474 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
479 *p
= argData
.wchar_view
;
481 else if (format
.width
> 1) *p
++ = ' ';
485 *p
= argData
.char_view
;
487 else if (format
.width
> 1) *p
++ = ' ';
492 LPCSTR ptr
= argData
.lpcstr_view
;
493 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)*ptr
++;
497 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
501 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
504 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
511 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
512 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)number
[i
];
517 if (format
.flags
& WPRINTF_LEFTALIGN
)
518 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
523 TRACE("%s\n",debugstr_w(buffer
));
524 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
528 /***********************************************************************
529 * wvsprintf16 (USER.421)
531 INT16 WINAPI
wvsprintf16( LPSTR buffer
, LPCSTR spec
, LPCVOID args
)
535 TRACE("for %p got:\n",buffer
);
536 res
= wvsnprintf16( buffer
, 1024, spec
, args
);
537 return ( res
== -1 ) ? 1024 : res
;
541 /***********************************************************************
542 * wvsprintfA (USER32.587)
544 INT WINAPI
wvsprintfA( LPSTR buffer
, LPCSTR spec
, va_list args
)
546 INT res
= wvsnprintfA( buffer
, 1024, spec
, args
);
547 return ( res
== -1 ) ? 1024 : res
;
551 /***********************************************************************
552 * wvsprintfW (USER32.588)
554 INT WINAPI
wvsprintfW( LPWSTR buffer
, LPCWSTR spec
, va_list args
)
556 INT res
= wvsnprintfW( buffer
, 1024, spec
, args
);
557 return ( res
== -1 ) ? 1024 : res
;
561 /***********************************************************************
562 * wsprintf16 (USER.420)
564 INT16 WINAPIV
wsprintf16(void)
570 VA_START16( valist
);
571 buffer
= VA_ARG16( valist
, SEGPTR
);
572 spec
= VA_ARG16( valist
, SEGPTR
);
573 res
= wvsnprintf16( (LPSTR
)PTR_SEG_TO_LIN(buffer
), 1024,
574 (LPCSTR
)PTR_SEG_TO_LIN(spec
), valist
);
576 return ( res
== -1 ) ? 1024 : res
;
580 /***********************************************************************
581 * wsprintfA (USER32.585)
583 INT WINAPIV
wsprintfA( LPSTR buffer
, LPCSTR spec
, ... )
588 va_start( valist
, spec
);
589 res
= wvsnprintfA( buffer
, 1024, spec
, valist
);
591 return ( res
== -1 ) ? 1024 : res
;
595 /***********************************************************************
596 * wsprintfW (USER32.586)
598 INT WINAPIV
wsprintfW( LPWSTR buffer
, LPCWSTR spec
, ... )
603 va_start( valist
, spec
);
604 res
= wvsnprintfW( buffer
, 1024, spec
, valist
);
606 return ( res
== -1 ) ? 1024 : res
;