4 * Copyright 1996 Alexandre Julliard
10 #include "wine/winbase16.h"
14 #include "stackframe.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(string);
20 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
21 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
22 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
23 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
24 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
25 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
26 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
56 static const CHAR null_stringA[] = "(null)";
57 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
59 /***********************************************************************
60 * WPRINTF_ParseFormatA
62 * Parse a format specification. A format specification has the form:
64 * [-][#][0][width][.precision]type
66 * Return value is the length of the format specification in characters.
68 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
75 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
76 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
77 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
78 while ((*p >= '0') && (*p <= '9')) /* width field */
80 res->width = res->width * 10 + *p - '0';
83 if (*p == '.') /* precision field */
86 while ((*p >= '0') && (*p <= '9'))
88 res->precision = res->precision * 10 + *p - '0';
92 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
93 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
94 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
98 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
101 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
105 res->type = WPR_SIGNED;
108 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
111 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
114 res->type = WPR_UNSIGNED;
117 res->flags |= WPRINTF_UPPER_HEX;
120 res->type = WPR_HEXA;
122 default: /* unknown format char */
123 res->type = WPR_UNKNOWN;
124 p--; /* print format as normal char */
127 return (INT)(p - format) + 1;
131 /***********************************************************************
132 * WPRINTF_ParseFormatW
134 * Parse a format specification. A format specification has the form:
136 * [-][#][0][width][.precision]type
138 * Return value is the length of the format specification in characters.
140 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
147 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
148 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
149 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
150 while ((*p >= '0') && (*p <= '9')) /* width field */
152 res->width = res->width * 10 + *p - '0';
155 if (*p == '.') /* precision field */
158 while ((*p >= '0') && (*p <= '9'))
160 res->precision = res->precision * 10 + *p - '0';
164 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
165 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
166 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
170 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
173 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
177 res->type = WPR_SIGNED;
180 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
183 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
186 res->type = WPR_UNSIGNED;
189 res->flags |= WPRINTF_UPPER_HEX;
192 res->type = WPR_HEXA;
195 res->type = WPR_UNKNOWN;
196 p--; /* print format as normal char */
199 return (INT)(p - format) + 1;
203 /***********************************************************************
206 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
207 LPSTR number, UINT maxlen )
211 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
212 if (format->width > maxlen) format->width = maxlen;
217 return (format->precision = 1);
219 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
220 for (len = 0; !format->precision || (len < format->precision); len++)
221 if (!*(arg->lpcstr_view + len)) break;
222 if (len > maxlen) len = maxlen;
223 return (format->precision = len);
225 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
226 for (len = 0; !format->precision || (len < format->precision); len++)
227 if (!*(arg->lpcwstr_view + len)) break;
228 if (len > maxlen) len = maxlen;
229 return (format->precision = len);
231 len = sprintf( number, "%d", arg->int_view );
234 len = sprintf( number, "%u", (UINT)arg->int_view );
237 len = sprintf( number,
238 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
239 (UINT)arg->int_view);
244 if (len > maxlen) len = maxlen;
245 if (format->precision < len) format->precision = len;
246 if (format->precision > maxlen) format->precision = maxlen;
247 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
248 format->precision = format->width;
249 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
253 /***********************************************************************
254 * WPRINTF_ExtractVAPtr
256 static WPRINTF_DATA WPRINTF_ExtractVAPtr( WPRINTF_FORMAT *format, va_list* args )
262 result.wchar_view = (WCHAR)va_arg( *args, int );break;
264 result.char_view = (CHAR)va_arg( *args, int ); break;
266 result.lpcstr_view = va_arg( *args, LPCSTR); break;
268 result.lpcwstr_view = va_arg( *args, LPCWSTR); break;
272 result.int_view = va_arg( *args, INT ); break;
274 result.wchar_view = 0; break;
279 /***********************************************************************
280 * wvsnprintf16 (Not a Windows API)
282 static INT16 wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec,
285 WPRINTF_FORMAT format;
289 WPRINTF_DATA cur_arg;
292 while (*spec && (maxlen > 1))
294 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
296 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
297 spec += WPRINTF_ParseFormatA( spec, &format );
300 case WPR_WCHAR: /* No Unicode in Win16 */
302 cur_arg.char_view = VA_ARG16( args, CHAR );
304 case WPR_WSTRING: /* No Unicode in Win16 */
306 seg_str = VA_ARG16( args, SEGPTR );
307 if (IsBadReadPtr16(seg_str, 1 )) cur_arg.lpcstr_view = "";
308 else cur_arg.lpcstr_view = MapSL( seg_str );
311 if (!(format.flags & WPRINTF_LONG))
313 cur_arg.int_view = VA_ARG16( args, INT16 );
319 if (format.flags & WPRINTF_LONG)
320 cur_arg.int_view = VA_ARG16( args, UINT );
322 cur_arg.int_view = VA_ARG16( args, UINT16 );
327 len = WPRINTF_GetLen( &format, &cur_arg, number, maxlen - 1 );
328 if (!(format.flags & WPRINTF_LEFTALIGN))
329 for (i = format.precision; i < format.width; i++, maxlen--)
333 case WPR_WCHAR: /* No Unicode in Win16 */
335 *p= cur_arg.char_view;
337 else if (format.width > 1) *p++ = ' ';
340 case WPR_WSTRING: /* No Unicode in Win16 */
342 if (len) memcpy( p, cur_arg.lpcstr_view, len );
346 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
349 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
356 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
357 if (len) memcpy( p, number, len );
363 if (format.flags & WPRINTF_LEFTALIGN)
364 for (i = format.precision; i < format.width; i++, maxlen--)
369 return (maxlen > 1) ? (INT)(p - buffer) : -1;
373 /***********************************************************************
374 * wvsnprintfA (Not a Windows API, but we export it from USER32 anyway)
376 INT WINAPI wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, va_list args )
378 WPRINTF_FORMAT format;
382 WPRINTF_DATA argData;
384 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
386 while (*spec && (maxlen > 1))
388 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
390 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
391 spec += WPRINTF_ParseFormatA( spec, &format );
392 argData = WPRINTF_ExtractVAPtr( &format, &args );
393 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
394 if (!(format.flags & WPRINTF_LEFTALIGN))
395 for (i = format.precision; i < format.width; i++, maxlen--)
400 *p = argData.wchar_view;
402 else if (format.width > 1) *p++ = ' ';
406 *p = argData.char_view;
408 else if (format.width > 1) *p++ = ' ';
412 memcpy( p, argData.lpcstr_view, len );
417 LPCWSTR ptr = argData.lpcwstr_view;
418 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
422 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
425 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
432 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
433 memcpy( p, number, len );
439 if (format.flags & WPRINTF_LEFTALIGN)
440 for (i = format.precision; i < format.width; i++, maxlen--)
445 TRACE("%s\n",debugstr_a(buffer));
446 return (maxlen > 1) ? (INT)(p - buffer) : -1;
450 /***********************************************************************
451 * wvsnprintfW (Not a Windows API, but we export it from USER32 anyway)
453 INT WINAPI wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, va_list args )
455 WPRINTF_FORMAT format;
459 WPRINTF_DATA argData;
461 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
463 while (*spec && (maxlen > 1))
465 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
467 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
468 spec += WPRINTF_ParseFormatW( spec, &format );
469 argData = WPRINTF_ExtractVAPtr( &format, &args );
470 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
471 if (!(format.flags & WPRINTF_LEFTALIGN))
472 for (i = format.precision; i < format.width; i++, maxlen--)
477 *p = argData.wchar_view;
479 else if (format.width > 1) *p++ = ' ';
483 *p = argData.char_view;
485 else if (format.width > 1) *p++ = ' ';
490 LPCSTR ptr = argData.lpcstr_view;
491 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
495 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
499 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
502 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
509 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
510 for (i = 0; i < len; i++) *p++ = (WCHAR)number[i];
515 if (format.flags & WPRINTF_LEFTALIGN)
516 for (i = format.precision; i < format.width; i++, maxlen--)
521 TRACE("%s\n",debugstr_w(buffer));
522 return (maxlen > 1) ? (INT)(p - buffer) : -1;
526 /***********************************************************************
527 * wvsprintf16 (USER.421)
529 INT16 WINAPI wvsprintf16( LPSTR buffer, LPCSTR spec, LPCVOID args )
533 TRACE("for %p got:\n",buffer);
534 res = wvsnprintf16( buffer, 1024, spec, args );
535 return ( res == -1 ) ? 1024 : res;
539 /***********************************************************************
540 * wvsprintfA (USER32.587)
542 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, va_list args )
544 INT res = wvsnprintfA( buffer, 1024, spec, args );
545 return ( res == -1 ) ? 1024 : res;
549 /***********************************************************************
550 * wvsprintfW (USER32.588)
552 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, va_list args )
554 INT res = wvsnprintfW( buffer, 1024, spec, args );
555 return ( res == -1 ) ? 1024 : res;
559 /***********************************************************************
560 * wsprintf16 (USER.420)
562 INT16 WINAPIV wsprintf16(void)
568 VA_START16( valist );
569 buffer = VA_ARG16( valist, SEGPTR );
570 spec = VA_ARG16( valist, SEGPTR );
571 res = wvsnprintf16( MapSL(buffer), 1024, MapSL(spec), valist );
573 return ( res == -1 ) ? 1024 : res;
577 /***********************************************************************
578 * wsprintfA (USER32.585)
580 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
585 va_start( valist, spec );
586 res = wvsnprintfA( buffer, 1024, spec, valist );
588 return ( res == -1 ) ? 1024 : res;
592 /***********************************************************************
593 * wsprintfW (USER32.586)
595 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
600 va_start( valist, spec );
601 res = wvsnprintfW( buffer, 1024, spec, valist );
603 return ( res == -1 ) ? 1024 : res;