source: trunk/src/user32/wsprintf.cpp@ 6666

Last change on this file since 6666 was 6650, checked in by bird, 24 years ago

Added $Id:$ keyword.

File size: 19.5 KB
Line 
1/* $Id: wsprintf.cpp,v 1.11 2001-09-05 13:54:53 bird Exp $ */
2/*
3 * wsprintf functions
4 *
5 * Copyright 1996 Alexandre Julliard
6 */
7
8#ifdef __WIN32OS2__
9#include <odin.h>
10#include <odinwrap.h>
11#include <os2sel.h>
12#include <os2win.h>
13
14#include <misc.h>
15
16#define DBG_LOCALLOG DBG_wsprintf
17#include "dbglocal.h"
18
19ODINDEBUGCHANNEL(USER32-WSPRINTF)
20#endif
21
22#include <stdarg.h>
23#include <string.h>
24#include <stdio.h>
25#include "wine/winbase16.h"
26#include "windef.h"
27#include "wingdi.h"
28#include "winuser.h"
29#include "stackframe.h"
30#include "debugtools.h"
31
32DEFAULT_DEBUG_CHANNEL(string);
33
34
35#define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
36#define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
37#define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
38#define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
39#define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
40#define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
41#define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
42
43typedef enum
44{
45 WPR_UNKNOWN,
46 WPR_CHAR,
47 WPR_WCHAR,
48 WPR_STRING,
49 WPR_WSTRING,
50 WPR_SIGNED,
51 WPR_UNSIGNED,
52 WPR_HEXA
53} WPRINTF_TYPE;
54
55typedef struct
56{
57 UINT flags;
58 UINT width;
59 UINT precision;
60 WPRINTF_TYPE type;
61} WPRINTF_FORMAT;
62
63typedef union {
64 WCHAR wchar_view;
65 CHAR char_view;
66 LPCSTR lpcstr_view;
67 LPCWSTR lpcwstr_view;
68 INT int_view;
69} WPRINTF_DATA;
70
71static const CHAR null_stringA[] = "(null)";
72static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
73
74/***********************************************************************
75 * WPRINTF_ParseFormatA
76 *
77 * Parse a format specification. A format specification has the form:
78 *
79 * [-][#][0][width][.precision]type
80 *
81 * Return value is the length of the format specification in characters.
82 */
83static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
84{
85 LPCSTR p = format;
86
87 res->flags = 0;
88 res->width = 0;
89 res->precision = 0;
90 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
91 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
92 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
93 while ((*p >= '0') && (*p <= '9')) /* width field */
94 {
95 res->width = res->width * 10 + *p - '0';
96 p++;
97 }
98 if (*p == '.') /* precision field */
99 {
100 p++;
101 while ((*p >= '0') && (*p <= '9'))
102 {
103 res->precision = res->precision * 10 + *p - '0';
104 p++;
105 }
106 }
107 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
108 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
109 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
110 switch(*p)
111 {
112 case 'c':
113 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
114 break;
115 case 'C':
116 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
117 break;
118 case 'd':
119 case 'i':
120 res->type = WPR_SIGNED;
121 break;
122 case 's':
123 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
124 break;
125 case 'S':
126 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
127 break;
128 case 'u':
129 res->type = WPR_UNSIGNED;
130 break;
131 case 'X':
132 res->flags |= WPRINTF_UPPER_HEX;
133 /* fall through */
134 case 'x':
135 res->type = WPR_HEXA;
136 break;
137 default: /* unknown format char */
138 res->type = WPR_UNKNOWN;
139 p--; /* print format as normal char */
140 break;
141 }
142 return (INT)(p - format) + 1;
143}
144
145
146/***********************************************************************
147 * WPRINTF_ParseFormatW
148 *
149 * Parse a format specification. A format specification has the form:
150 *
151 * [-][#][0][width][.precision]type
152 *
153 * Return value is the length of the format specification in characters.
154 */
155static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
156{
157 LPCWSTR p = format;
158
159 res->flags = 0;
160 res->width = 0;
161 res->precision = 0;
162 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
163 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
164 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
165 while ((*p >= '0') && (*p <= '9')) /* width field */
166 {
167 res->width = res->width * 10 + *p - '0';
168 p++;
169 }
170 if (*p == '.') /* precision field */
171 {
172 p++;
173 while ((*p >= '0') && (*p <= '9'))
174 {
175 res->precision = res->precision * 10 + *p - '0';
176 p++;
177 }
178 }
179 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
180 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
181 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
182 switch((CHAR)*p)
183 {
184 case 'c':
185 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
186 break;
187 case 'C':
188 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
189 break;
190 case 'd':
191 case 'i':
192 res->type = WPR_SIGNED;
193 break;
194 case 's':
195 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
196 break;
197 case 'S':
198 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
199 break;
200 case 'u':
201 res->type = WPR_UNSIGNED;
202 break;
203 case 'X':
204 res->flags |= WPRINTF_UPPER_HEX;
205 /* fall through */
206 case 'x':
207 res->type = WPR_HEXA;
208 break;
209 default:
210 res->type = WPR_UNKNOWN;
211 p--; /* print format as normal char */
212 break;
213 }
214 return (INT)(p - format) + 1;
215}
216
217
218/***********************************************************************
219 * WPRINTF_GetLen
220 */
221static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
222 LPSTR number, UINT maxlen )
223{
224 UINT len;
225
226 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
227 if (format->width > maxlen) format->width = maxlen;
228 switch(format->type)
229 {
230 case WPR_CHAR:
231 case WPR_WCHAR:
232 return (format->precision = 1);
233 case WPR_STRING:
234 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
235 for (len = 0; !format->precision || (len < format->precision); len++)
236 if (!*(arg->lpcstr_view + len)) break;
237 if (len > maxlen) len = maxlen;
238 return (format->precision = len);
239 case WPR_WSTRING:
240 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
241 for (len = 0; !format->precision || (len < format->precision); len++)
242 if (!*(arg->lpcwstr_view + len)) break;
243 if (len > maxlen) len = maxlen;
244 return (format->precision = len);
245 case WPR_SIGNED:
246 len = sprintf( number, "%d", arg->int_view );
247 break;
248 case WPR_UNSIGNED:
249 len = sprintf( number, "%u", (UINT)arg->int_view );
250 break;
251 case WPR_HEXA:
252 len = sprintf( number,
253 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
254 (UINT)arg->int_view);
255 break;
256 default:
257 return 0;
258 }
259 if (len > maxlen) len = maxlen;
260 if (format->precision < len) format->precision = len;
261 if (format->precision > maxlen) format->precision = maxlen;
262 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
263 format->precision = format->width;
264 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
265 return len;
266}
267
268
269#ifndef __WIN32OS2__
270/***********************************************************************
271 * wvsnprintf16 (Not a Windows API)
272 */
273static INT16 wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec,
274 LPCVOID args )
275{
276 WPRINTF_FORMAT format;
277 LPSTR p = buffer;
278 UINT i, len;
279 CHAR number[20];
280 WPRINTF_DATA cur_arg;
281 SEGPTR seg_str;
282
283 while (*spec && (maxlen > 1))
284 {
285 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
286 spec++;
287 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
288 spec += WPRINTF_ParseFormatA( spec, &format );
289 switch(format.type)
290 {
291 case WPR_WCHAR: /* No Unicode in Win16 */
292 case WPR_CHAR:
293 cur_arg.char_view = VA_ARG16( args, CHAR );
294 break;
295 case WPR_WSTRING: /* No Unicode in Win16 */
296 case WPR_STRING:
297 seg_str = VA_ARG16( args, SEGPTR );
298 if (IsBadReadPtr16(seg_str, 1 )) cur_arg.lpcstr_view = "";
299 else cur_arg.lpcstr_view = MapSL( seg_str );
300 break;
301 case WPR_SIGNED:
302 if (!(format.flags & WPRINTF_LONG))
303 {
304 cur_arg.int_view = VA_ARG16( args, INT16 );
305 break;
306 }
307 /* fall through */
308 case WPR_HEXA:
309 case WPR_UNSIGNED:
310 if (format.flags & WPRINTF_LONG)
311 cur_arg.int_view = VA_ARG16( args, UINT );
312 else
313 cur_arg.int_view = VA_ARG16( args, UINT16 );
314 break;
315 case WPR_UNKNOWN:
316 continue;
317 }
318 len = WPRINTF_GetLen( &format, &cur_arg, number, maxlen - 1 );
319 if (!(format.flags & WPRINTF_LEFTALIGN))
320 for (i = format.precision; i < format.width; i++, maxlen--)
321 *p++ = ' ';
322 switch(format.type)
323 {
324 case WPR_WCHAR: /* No Unicode in Win16 */
325 case WPR_CHAR:
326 *p= cur_arg.char_view;
327 if (*p != '\0') p++;
328 else if (format.width > 1) *p++ = ' ';
329 else len = 0;
330 break;
331 case WPR_WSTRING: /* No Unicode in Win16 */
332 case WPR_STRING:
333 if (len) memcpy( p, cur_arg.lpcstr_view, len );
334 p += len;
335 break;
336 case WPR_HEXA:
337 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
338 {
339 *p++ = '0';
340 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
341 maxlen -= 2;
342 len -= 2;
343 }
344 /* fall through */
345 case WPR_SIGNED:
346 case WPR_UNSIGNED:
347 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
348 if (len) memcpy( p, number, len );
349 p += len;
350 break;
351 case WPR_UNKNOWN:
352 continue;
353 }
354 if (format.flags & WPRINTF_LEFTALIGN)
355 for (i = format.precision; i < format.width; i++, maxlen--)
356 *p++ = ' ';
357 maxlen -= len;
358 }
359 *p = 0;
360 return (maxlen > 1) ? (INT)(p - buffer) : -1;
361}
362#endif //!__WIN32OS2__
363
364
365/***********************************************************************
366 * wvsnprintfA (USER32.@) (Not a Windows API, but we export it from USER32 anyway)
367 */
368INT WINAPI wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, va_list args )
369{
370 WPRINTF_FORMAT format;
371 LPSTR p = buffer;
372 UINT i, len;
373 CHAR number[20];
374 WPRINTF_DATA argData;
375
376 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
377
378 while (*spec && (maxlen > 1))
379 {
380 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
381 spec++;
382 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
383 spec += WPRINTF_ParseFormatA( spec, &format );
384
385 switch(format.type)
386 {
387 case WPR_WCHAR:
388 argData.wchar_view = (WCHAR)va_arg( args, int );
389 break;
390 case WPR_CHAR:
391 argData.char_view = (CHAR)va_arg( args, int );
392 break;
393 case WPR_STRING:
394 argData.lpcstr_view = va_arg( args, LPCSTR );
395 break;
396 case WPR_WSTRING:
397 argData.lpcwstr_view = va_arg( args, LPCWSTR );
398 break;
399 case WPR_HEXA:
400 case WPR_SIGNED:
401 case WPR_UNSIGNED:
402 argData.int_view = va_arg( args, INT );
403 break;
404 default:
405 argData.wchar_view = 0;
406 break;
407 }
408
409 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
410 if (!(format.flags & WPRINTF_LEFTALIGN))
411 for (i = format.precision; i < format.width; i++, maxlen--)
412 *p++ = ' ';
413 switch(format.type)
414 {
415 case WPR_WCHAR:
416 *p = argData.wchar_view;
417 if (*p != '\0') p++;
418 else if (format.width > 1) *p++ = ' ';
419 else len = 0;
420 break;
421 case WPR_CHAR:
422 *p = argData.char_view;
423 if (*p != '\0') p++;
424 else if (format.width > 1) *p++ = ' ';
425 else len = 0;
426 break;
427 case WPR_STRING:
428 memcpy( p, argData.lpcstr_view, len );
429 p += len;
430 break;
431 case WPR_WSTRING:
432 {
433 LPCWSTR ptr = argData.lpcwstr_view;
434 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
435 }
436 break;
437 case WPR_HEXA:
438 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
439 {
440 *p++ = '0';
441 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
442 maxlen -= 2;
443 len -= 2;
444 }
445 /* fall through */
446 case WPR_SIGNED:
447 case WPR_UNSIGNED:
448 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
449 memcpy( p, number, len );
450 p += len;
451 break;
452 case WPR_UNKNOWN:
453 continue;
454 }
455 if (format.flags & WPRINTF_LEFTALIGN)
456 for (i = format.precision; i < format.width; i++, maxlen--)
457 *p++ = ' ';
458 maxlen -= len;
459 }
460 *p = 0;
461 TRACE("%s\n",debugstr_a(buffer));
462 return (maxlen > 1) ? (INT)(p - buffer) : -1;
463}
464
465
466/***********************************************************************
467 * wvsnprintfW (USER32.@) (Not a Windows API, but we export it from USER32 anyway)
468 */
469INT WINAPI wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, va_list args )
470{
471 WPRINTF_FORMAT format;
472 LPWSTR p = buffer;
473 UINT i, len;
474 CHAR number[20];
475 WPRINTF_DATA argData;
476
477 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
478
479 while (*spec && (maxlen > 1))
480 {
481 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
482 spec++;
483 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
484 spec += WPRINTF_ParseFormatW( spec, &format );
485
486 switch(format.type)
487 {
488 case WPR_WCHAR:
489 argData.wchar_view = (WCHAR)va_arg( args, int );
490 break;
491 case WPR_CHAR:
492 argData.char_view = (CHAR)va_arg( args, int );
493 break;
494 case WPR_STRING:
495 argData.lpcstr_view = va_arg( args, LPCSTR );
496 break;
497 case WPR_WSTRING:
498 argData.lpcwstr_view = va_arg( args, LPCWSTR );
499 break;
500 case WPR_HEXA:
501 case WPR_SIGNED:
502 case WPR_UNSIGNED:
503 argData.int_view = va_arg( args, INT );
504 break;
505 default:
506 argData.wchar_view = 0;
507 break;
508 }
509
510 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
511 if (!(format.flags & WPRINTF_LEFTALIGN))
512 for (i = format.precision; i < format.width; i++, maxlen--)
513 *p++ = ' ';
514 switch(format.type)
515 {
516 case WPR_WCHAR:
517 *p = argData.wchar_view;
518 if (*p != '\0') p++;
519 else if (format.width > 1) *p++ = ' ';
520 else len = 0;
521 break;
522 case WPR_CHAR:
523 *p = argData.char_view;
524 if (*p != '\0') p++;
525 else if (format.width > 1) *p++ = ' ';
526 else len = 0;
527 break;
528 case WPR_STRING:
529 {
530 LPCSTR ptr = argData.lpcstr_view;
531 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
532 }
533 break;
534 case WPR_WSTRING:
535 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
536 p += len;
537 break;
538 case WPR_HEXA:
539 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
540 {
541 *p++ = '0';
542 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
543 maxlen -= 2;
544 len -= 2;
545 }
546 /* fall through */
547 case WPR_SIGNED:
548 case WPR_UNSIGNED:
549 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
550 for (i = 0; i < len; i++) *p++ = (WCHAR)number[i];
551 break;
552 case WPR_UNKNOWN:
553 continue;
554 }
555 if (format.flags & WPRINTF_LEFTALIGN)
556 for (i = format.precision; i < format.width; i++, maxlen--)
557 *p++ = ' ';
558 maxlen -= len;
559 }
560 *p = 0;
561 TRACE("%s\n",debugstr_w(buffer));
562 return (maxlen > 1) ? (INT)(p - buffer) : -1;
563}
564
565
566#ifndef __WIN32OS2__
567/***********************************************************************
568 * wvsprintf16 (USER.421)
569 */
570INT16 WINAPI wvsprintf16( LPSTR buffer, LPCSTR spec, LPCVOID args )
571{
572 INT16 res;
573
574 TRACE("for %p got:\n",buffer);
575 res = wvsnprintf16( buffer, 1024, spec, args );
576 return ( res == -1 ) ? 1024 : res;
577}
578#endif //!__WIN32OS2__
579
580
581/***********************************************************************
582 * wvsprintfA (USER32.@)
583 */
584INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, va_list args )
585{
586 INT res = wvsnprintfA( buffer, 1024, spec, args );
587 return ( res == -1 ) ? 1024 : res;
588}
589
590
591/***********************************************************************
592 * wvsprintfW (USER32.@)
593 */
594INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, va_list args )
595{
596 INT res = wvsnprintfW( buffer, 1024, spec, args );
597 return ( res == -1 ) ? 1024 : res;
598}
599
600#ifndef __WIN32OS2__
601/***********************************************************************
602 * wsprintf16 (USER.420)
603 */
604INT16 WINAPIV wsprintf16(void)
605{
606 VA_LIST16 valist;
607 INT16 res;
608 SEGPTR buffer, spec;
609
610 VA_START16( valist );
611 buffer = VA_ARG16( valist, SEGPTR );
612 spec = VA_ARG16( valist, SEGPTR );
613 res = wvsnprintf16( MapSL(buffer), 1024, MapSL(spec), valist );
614 VA_END16( valist );
615 return ( res == -1 ) ? 1024 : res;
616}
617#endif //!__WIN32OS2__
618
619
620/***********************************************************************
621 * wsprintfA (USER32.@)
622 */
623INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
624{
625 va_list valist;
626 INT res;
627
628 va_start( valist, spec );
629 res = wvsnprintfA( buffer, 1024, spec, valist );
630 va_end( valist );
631 return ( res == -1 ) ? 1024 : res;
632}
633
634
635/***********************************************************************
636 * wsprintfW (USER32.@)
637 */
638INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
639{
640 va_list valist;
641 INT res;
642
643 va_start( valist, spec );
644 res = wvsnprintfW( buffer, 1024, spec, valist );
645 va_end( valist );
646 return ( res == -1 ) ? 1024 : res;
647}
648
649/***********************************************************************
650 * wsnprintfA (Not a Windows API)
651 */
652INT WINAPIV wsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, ... )
653{
654 va_list valist;
655 INT res;
656
657 va_start( valist, spec );
658 res = wvsnprintfA( buffer, maxlen, spec, valist );
659 va_end( valist );
660 return res;
661}
662
663/***********************************************************************
664 * wsnprintfW (Not a Windows API)
665 */
666INT WINAPIV wsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, ... )
667{
668 va_list valist;
669 INT res;
670
671 va_start( valist, spec );
672 res = wvsnprintfW( buffer, maxlen, spec, valist );
673 va_end( valist );
674 return res;
675}
Note: See TracBrowser for help on using the repository browser.