]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxprintf.cpp
fix warnings about parameters shadowing member variables
[wxWidgets.git] / src / common / wxprintf.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/wxprintf.cpp
3 // Purpose: wxWidgets wxPrintf() implementation
4 // Author: Ove Kaven
5 // Modified by: Ron Lee, Francesco Montorsi
6 // Created: 09/04/99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // headers, declarations, constants
14 // ===========================================================================
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/crt.h"
24
25 #include <string.h>
26
27 #ifndef WX_PRECOMP
28 #include "wx/string.h"
29 #include "wx/hash.h"
30 #include "wx/utils.h" // for wxMin and wxMax
31 #include "wx/log.h"
32 #endif
33
34 #if defined(__MWERKS__) && __MSL__ >= 0x6000
35 namespace std {}
36 using namespace std ;
37 #endif
38
39
40 // ============================================================================
41 // printf() implementation
42 // ============================================================================
43
44 // special test mode: define all functions below even if we don't really need
45 // them to be able to test them
46 #ifdef wxTEST_PRINTF
47 #undef wxCRT_VsnprintfW
48 #undef wxCRT_VsnprintfA
49 #endif
50
51 // ----------------------------------------------------------------------------
52 // implement [v]snprintf() if the system doesn't provide a safe one
53 // or if the system's one does not support positional parameters
54 // (very useful for i18n purposes)
55 // ----------------------------------------------------------------------------
56
57 // ----------------------------------------------------------------------------
58 // common code for both ANSI and Unicode versions
59 // ----------------------------------------------------------------------------
60
61 #if !defined(wxCRT_VsnprintfW) || !defined(wxCRT_VsnprintfA)
62
63 // wxUSE_STRUTILS says our wxCRT_VsnprintfW implementation to use or not to
64 // use wxStrlen and wxStrncpy functions over one-char processing loops.
65 //
66 // Some benchmarking revealed that wxUSE_STRUTILS == 1 has the following
67 // effects:
68 // -> on Windows:
69 // when in ANSI mode, this setting does not change almost anything
70 // when in Unicode mode, it gives ~ 50% of slowdown !
71 // -> on Linux:
72 // both in ANSI and Unicode mode it gives ~ 60% of speedup !
73 //
74 #if defined(WIN32) && wxUSE_UNICODE
75 #define wxUSE_STRUTILS 0
76 #else
77 #define wxUSE_STRUTILS 1
78 #endif
79
80 // some limits of our implementation
81 #define wxMAX_SVNPRINTF_ARGUMENTS 64
82 #define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32
83 #define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512
84
85 // prefer snprintf over sprintf
86 #if defined(__VISUALC__) || \
87 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
88 #define system_sprintf(buff, max, flags, data) \
89 ::_snprintf(buff, max, flags, data)
90 #elif defined(HAVE_SNPRINTF)
91 #define system_sprintf(buff, max, flags, data) \
92 ::snprintf(buff, max, flags, data)
93 #else // NB: at least sprintf() should always be available
94 // since 'max' is not used in this case, wxVsnprintf() should always
95 // ensure that 'buff' is big enough for all common needs
96 // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN)
97 #define system_sprintf(buff, max, flags, data) \
98 ::sprintf(buff, flags, data)
99
100 #define SYSTEM_SPRINTF_IS_UNSAFE
101 #endif
102
103 namespace
104 {
105
106 // the conversion specifiers accepted by wxCRT_VsnprintfW
107 enum wxPrintfArgType {
108 wxPAT_INVALID = -1,
109
110 wxPAT_INT, // %d, %i, %o, %u, %x, %X
111 wxPAT_LONGINT, // %ld, etc
112 #ifdef wxLongLong_t
113 wxPAT_LONGLONGINT, // %Ld, etc
114 #endif
115 wxPAT_SIZET, // %Zd, etc
116
117 wxPAT_DOUBLE, // %e, %E, %f, %g, %G
118 wxPAT_LONGDOUBLE, // %le, etc
119
120 wxPAT_POINTER, // %p
121
122 wxPAT_CHAR, // %hc (in ANSI mode: %c, too)
123 wxPAT_WCHAR, // %lc (in Unicode mode: %c, too)
124
125 wxPAT_PCHAR, // %s (related to a char *)
126 wxPAT_PWCHAR, // %s (related to a wchar_t *)
127
128 wxPAT_NINT, // %n
129 wxPAT_NSHORTINT, // %hn
130 wxPAT_NLONGINT // %ln
131 };
132
133 // an argument passed to wxCRT_VsnprintfW
134 typedef union {
135 int pad_int; // %d, %i, %o, %u, %x, %X
136 long int pad_longint; // %ld, etc
137 #ifdef wxLongLong_t
138 wxLongLong_t pad_longlongint; // %Ld, etc
139 #endif
140 size_t pad_sizet; // %Zd, etc
141
142 double pad_double; // %e, %E, %f, %g, %G
143 long double pad_longdouble; // %le, etc
144
145 void *pad_pointer; // %p
146
147 char pad_char; // %hc (in ANSI mode: %c, too)
148 wchar_t pad_wchar; // %lc (in Unicode mode: %c, too)
149
150 void *pad_str; // %s
151
152 int *pad_nint; // %n
153 short int *pad_nshortint; // %hn
154 long int *pad_nlongint; // %ln
155 } wxPrintfArg;
156
157 // helper for converting string into either char* or wchar_t* dependening
158 // on the type of wxPrintfConvSpec<T> instantiation:
159 template<typename CharType> struct wxPrintfStringHelper {};
160
161 template<> struct wxPrintfStringHelper<char>
162 {
163 typedef const wxWX2MBbuf ConvertedType;
164 static ConvertedType Convert(const wxString& s) { return s.mb_str(); }
165 };
166
167 template<> struct wxPrintfStringHelper<wchar_t>
168 {
169 typedef const wxWX2WCbuf ConvertedType;
170 static ConvertedType Convert(const wxString& s) { return s.wc_str(); }
171 };
172
173
174 // Contains parsed data relative to a conversion specifier given to
175 // wxCRT_VsnprintfW and parsed from the format string
176 // NOTE: in C++ there is almost no difference between struct & classes thus
177 // there is no performance gain by using a struct here...
178 template<typename CharType>
179 class wxPrintfConvSpec
180 {
181 public:
182
183 // the position of the argument relative to this conversion specifier
184 size_t m_pos;
185
186 // the type of this conversion specifier
187 wxPrintfArgType m_type;
188
189 // the minimum and maximum width
190 // when one of this var is set to -1 it means: use the following argument
191 // in the stack as minimum/maximum width for this conversion specifier
192 int m_nMinWidth, m_nMaxWidth;
193
194 // does the argument need to the be aligned to left ?
195 bool m_bAlignLeft;
196
197 // pointer to the '%' of this conversion specifier in the format string
198 // NOTE: this points somewhere in the string given to the Parse() function -
199 // it's task of the caller ensure that memory is still valid !
200 const CharType *m_pArgPos;
201
202 // pointer to the last character of this conversion specifier in the
203 // format string
204 // NOTE: this points somewhere in the string given to the Parse() function -
205 // it's task of the caller ensure that memory is still valid !
206 const CharType *m_pArgEnd;
207
208 // a little buffer where formatting flags like #+\.hlqLZ are stored by Parse()
209 // for use in Process()
210 // NB: even if this buffer is used only for numeric conversion specifiers and
211 // thus could be safely declared as a char[] buffer, we want it to be wchar_t
212 // so that in Unicode builds we can avoid to convert its contents to Unicode
213 // chars when copying it in user's buffer.
214 char m_szFlags[wxMAX_SVNPRINTF_FLAGBUFFER_LEN];
215
216
217 public:
218
219 // we don't declare this as a constructor otherwise it would be called
220 // automatically and we don't want this: to be optimized, wxCRT_VsnprintfW
221 // calls this function only on really-used instances of this class.
222 void Init();
223
224 // Parses the first conversion specifier in the given string, which must
225 // begin with a '%'. Returns false if the first '%' does not introduce a
226 // (valid) conversion specifier and thus should be ignored.
227 bool Parse(const CharType *format);
228
229 // Process this conversion specifier and puts the result in the given
230 // buffer. Returns the number of characters written in 'buf' or -1 if
231 // there's not enough space.
232 int Process(CharType *buf, size_t lenMax, wxPrintfArg *p, size_t written);
233
234 // Loads the argument of this conversion specifier from given va_list.
235 bool LoadArg(wxPrintfArg *p, va_list &argptr);
236
237 private:
238 // An helper function of LoadArg() which is used to handle the '*' flag
239 void ReplaceAsteriskWith(int w);
240 };
241
242 template<typename CharType>
243 void wxPrintfConvSpec<CharType>::Init()
244 {
245 m_nMinWidth = 0;
246 m_nMaxWidth = 0xFFFF;
247 m_pos = 0;
248 m_bAlignLeft = false;
249 m_pArgPos = m_pArgEnd = NULL;
250 m_type = wxPAT_INVALID;
251
252 // this character will never be removed from m_szFlags array and
253 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
254 m_szFlags[0] = '%';
255 }
256
257 template<typename CharType>
258 bool wxPrintfConvSpec<CharType>::Parse(const CharType *format)
259 {
260 bool done = false;
261
262 // temporary parse data
263 size_t flagofs = 1;
264 bool in_prec, // true if we found the dot in some previous iteration
265 prec_dot; // true if the dot has been already added to m_szFlags
266 int ilen = 0;
267
268 m_bAlignLeft = in_prec = prec_dot = false;
269 m_pArgPos = m_pArgEnd = format;
270 do
271 {
272 #define CHECK_PREC \
273 if (in_prec && !prec_dot) \
274 { \
275 m_szFlags[flagofs++] = '.'; \
276 prec_dot = true; \
277 }
278
279 // what follows '%'?
280 const CharType ch = *(++m_pArgEnd);
281 switch ( ch )
282 {
283 case wxT('\0'):
284 return false; // not really an argument
285
286 case wxT('%'):
287 return false; // not really an argument
288
289 case wxT('#'):
290 case wxT('0'):
291 case wxT(' '):
292 case wxT('+'):
293 case wxT('\''):
294 CHECK_PREC
295 m_szFlags[flagofs++] = char(ch);
296 break;
297
298 case wxT('-'):
299 CHECK_PREC
300 m_bAlignLeft = true;
301 m_szFlags[flagofs++] = char(ch);
302 break;
303
304 case wxT('.'):
305 CHECK_PREC
306 in_prec = true;
307 prec_dot = false;
308 m_nMaxWidth = 0;
309 // dot will be auto-added to m_szFlags if non-negative
310 // number follows
311 break;
312
313 case wxT('h'):
314 ilen = -1;
315 CHECK_PREC
316 m_szFlags[flagofs++] = char(ch);
317 break;
318
319 case wxT('l'):
320 // NB: it's safe to use flagofs-1 as flagofs always start from 1
321 if (m_szFlags[flagofs-1] == 'l') // 'll' modifier is the same as 'L' or 'q'
322 ilen = 2;
323 else
324 ilen = 1;
325 CHECK_PREC
326 m_szFlags[flagofs++] = char(ch);
327 break;
328
329 case wxT('q'):
330 case wxT('L'):
331 ilen = 2;
332 CHECK_PREC
333 m_szFlags[flagofs++] = char(ch);
334 break;
335 #ifdef __WXMSW__
336 // under Windows we support the special '%I64' notation as longlong
337 // integer conversion specifier for MSVC compatibility
338 // (it behaves exactly as '%lli' or '%Li' or '%qi')
339 case wxT('I'):
340 if (*(m_pArgEnd+1) != wxT('6') ||
341 *(m_pArgEnd+2) != wxT('4'))
342 return false; // bad format
343
344 m_pArgEnd++;
345 m_pArgEnd++;
346
347 ilen = 2;
348 CHECK_PREC
349 m_szFlags[flagofs++] = char(ch);
350 m_szFlags[flagofs++] = '6';
351 m_szFlags[flagofs++] = '4';
352 break;
353 #endif // __WXMSW__
354
355 case wxT('Z'):
356 ilen = 3;
357 CHECK_PREC
358 m_szFlags[flagofs++] = char(ch);
359 break;
360
361 case wxT('*'):
362 if (in_prec)
363 {
364 CHECK_PREC
365
366 // tell Process() to use the next argument
367 // in the stack as maxwidth...
368 m_nMaxWidth = -1;
369 }
370 else
371 {
372 // tell Process() to use the next argument
373 // in the stack as minwidth...
374 m_nMinWidth = -1;
375 }
376
377 // save the * in our formatting buffer...
378 // will be replaced later by Process()
379 m_szFlags[flagofs++] = char(ch);
380 break;
381
382 case wxT('1'): case wxT('2'): case wxT('3'):
383 case wxT('4'): case wxT('5'): case wxT('6'):
384 case wxT('7'): case wxT('8'): case wxT('9'):
385 {
386 int len = 0;
387 CHECK_PREC
388 while ( (*m_pArgEnd >= CharType('0')) &&
389 (*m_pArgEnd <= CharType('9')) )
390 {
391 m_szFlags[flagofs++] = char(*m_pArgEnd);
392 len = len*10 + (*m_pArgEnd - wxT('0'));
393 m_pArgEnd++;
394 }
395
396 if (in_prec)
397 m_nMaxWidth = len;
398 else
399 m_nMinWidth = len;
400
401 m_pArgEnd--; // the main loop pre-increments n again
402 }
403 break;
404
405 case wxT('$'): // a positional parameter (e.g. %2$s) ?
406 {
407 if (m_nMinWidth <= 0)
408 break; // ignore this formatting flag as no
409 // numbers are preceding it
410
411 // remove from m_szFlags all digits previously added
412 do {
413 flagofs--;
414 } while (m_szFlags[flagofs] >= '1' &&
415 m_szFlags[flagofs] <= '9');
416
417 // re-adjust the offset making it point to the
418 // next free char of m_szFlags
419 flagofs++;
420
421 m_pos = m_nMinWidth;
422 m_nMinWidth = 0;
423 }
424 break;
425
426 case wxT('d'):
427 case wxT('i'):
428 case wxT('o'):
429 case wxT('u'):
430 case wxT('x'):
431 case wxT('X'):
432 CHECK_PREC
433 m_szFlags[flagofs++] = char(ch);
434 m_szFlags[flagofs] = '\0';
435 if (ilen == 0)
436 m_type = wxPAT_INT;
437 else if (ilen == -1)
438 // NB: 'short int' value passed through '...'
439 // is promoted to 'int', so we have to get
440 // an int from stack even if we need a short
441 m_type = wxPAT_INT;
442 else if (ilen == 1)
443 m_type = wxPAT_LONGINT;
444 else if (ilen == 2)
445 #ifdef wxLongLong_t
446 m_type = wxPAT_LONGLONGINT;
447 #else // !wxLongLong_t
448 m_type = wxPAT_LONGINT;
449 #endif // wxLongLong_t/!wxLongLong_t
450 else if (ilen == 3)
451 m_type = wxPAT_SIZET;
452 done = true;
453 break;
454
455 case wxT('e'):
456 case wxT('E'):
457 case wxT('f'):
458 case wxT('g'):
459 case wxT('G'):
460 CHECK_PREC
461 m_szFlags[flagofs++] = char(ch);
462 m_szFlags[flagofs] = '\0';
463 if (ilen == 2)
464 m_type = wxPAT_LONGDOUBLE;
465 else
466 m_type = wxPAT_DOUBLE;
467 done = true;
468 break;
469
470 case wxT('p'):
471 m_type = wxPAT_POINTER;
472 m_szFlags[flagofs++] = char(ch);
473 m_szFlags[flagofs] = '\0';
474 done = true;
475 break;
476
477 case wxT('c'):
478 if (ilen == -1)
479 {
480 // in Unicode mode %hc == ANSI character
481 // and in ANSI mode, %hc == %c == ANSI...
482 m_type = wxPAT_CHAR;
483 }
484 else if (ilen == 1)
485 {
486 // in ANSI mode %lc == Unicode character
487 // and in Unicode mode, %lc == %c == Unicode...
488 m_type = wxPAT_WCHAR;
489 }
490 else
491 {
492 #if wxUSE_UNICODE
493 // in Unicode mode, %c == Unicode character
494 m_type = wxPAT_WCHAR;
495 #else
496 // in ANSI mode, %c == ANSI character
497 m_type = wxPAT_CHAR;
498 #endif
499 }
500 done = true;
501 break;
502
503 case wxT('s'):
504 if (ilen == -1)
505 {
506 // Unicode mode wx extension: we'll let %hs mean non-Unicode
507 // strings (when in ANSI mode, %s == %hs == ANSI string)
508 m_type = wxPAT_PCHAR;
509 }
510 else if (ilen == 1)
511 {
512 // in Unicode mode, %ls == %s == Unicode string
513 // in ANSI mode, %ls == Unicode string
514 m_type = wxPAT_PWCHAR;
515 }
516 else
517 {
518 #if wxUSE_UNICODE
519 m_type = wxPAT_PWCHAR;
520 #else
521 m_type = wxPAT_PCHAR;
522 #endif
523 }
524 done = true;
525 break;
526
527 case wxT('n'):
528 if (ilen == 0)
529 m_type = wxPAT_NINT;
530 else if (ilen == -1)
531 m_type = wxPAT_NSHORTINT;
532 else if (ilen >= 1)
533 m_type = wxPAT_NLONGINT;
534 done = true;
535 break;
536
537 default:
538 // bad format, don't consider this an argument;
539 // leave it unchanged
540 return false;
541 }
542
543 if (flagofs == wxMAX_SVNPRINTF_FLAGBUFFER_LEN)
544 {
545 wxLogDebug(wxT("Too many flags specified for a single conversion specifier!"));
546 return false;
547 }
548 }
549 while (!done);
550
551 return true; // parsing was successful
552 }
553
554 template<typename CharType>
555 void wxPrintfConvSpec<CharType>::ReplaceAsteriskWith(int width)
556 {
557 char temp[wxMAX_SVNPRINTF_FLAGBUFFER_LEN];
558
559 // find the first * in our flag buffer
560 char *pwidth = strchr(m_szFlags, '*');
561 wxCHECK_RET(pwidth, _T("field width must be specified"));
562
563 // save what follows the * (the +1 is to skip the asterisk itself!)
564 strcpy(temp, pwidth+1);
565 if (width < 0)
566 {
567 pwidth[0] = wxT('-');
568 pwidth++;
569 }
570
571 // replace * with the actual integer given as width
572 #ifndef SYSTEM_SPRINTF_IS_UNSAFE
573 int maxlen = (m_szFlags + wxMAX_SVNPRINTF_FLAGBUFFER_LEN - pwidth) /
574 sizeof(*m_szFlags);
575 #endif
576 int offset = system_sprintf(pwidth, maxlen, "%d", abs(width));
577
578 // restore after the expanded * what was following it
579 strcpy(pwidth+offset, temp);
580 }
581
582 template<typename CharType>
583 bool wxPrintfConvSpec<CharType>::LoadArg(wxPrintfArg *p, va_list &argptr)
584 {
585 // did the '*' width/precision specifier was used ?
586 if (m_nMaxWidth == -1)
587 {
588 // take the maxwidth specifier from the stack
589 m_nMaxWidth = va_arg(argptr, int);
590 if (m_nMaxWidth < 0)
591 m_nMaxWidth = 0;
592 else
593 ReplaceAsteriskWith(m_nMaxWidth);
594 }
595
596 if (m_nMinWidth == -1)
597 {
598 // take the minwidth specifier from the stack
599 m_nMinWidth = va_arg(argptr, int);
600
601 ReplaceAsteriskWith(m_nMinWidth);
602 if (m_nMinWidth < 0)
603 {
604 m_bAlignLeft = !m_bAlignLeft;
605 m_nMinWidth = -m_nMinWidth;
606 }
607 }
608
609 switch (m_type) {
610 case wxPAT_INT:
611 p->pad_int = va_arg(argptr, int);
612 break;
613 case wxPAT_LONGINT:
614 p->pad_longint = va_arg(argptr, long int);
615 break;
616 #ifdef wxLongLong_t
617 case wxPAT_LONGLONGINT:
618 p->pad_longlongint = va_arg(argptr, wxLongLong_t);
619 break;
620 #endif // wxLongLong_t
621 case wxPAT_SIZET:
622 p->pad_sizet = va_arg(argptr, size_t);
623 break;
624 case wxPAT_DOUBLE:
625 p->pad_double = va_arg(argptr, double);
626 break;
627 case wxPAT_LONGDOUBLE:
628 p->pad_longdouble = va_arg(argptr, long double);
629 break;
630 case wxPAT_POINTER:
631 p->pad_pointer = va_arg(argptr, void *);
632 break;
633
634 case wxPAT_CHAR:
635 p->pad_char = (char)va_arg(argptr, int); // char is promoted to int when passed through '...'
636 break;
637 case wxPAT_WCHAR:
638 p->pad_wchar = (wchar_t)va_arg(argptr, int); // char is promoted to int when passed through '...'
639 break;
640
641 case wxPAT_PCHAR:
642 case wxPAT_PWCHAR:
643 p->pad_str = va_arg(argptr, void *);
644 break;
645
646 case wxPAT_NINT:
647 p->pad_nint = va_arg(argptr, int *);
648 break;
649 case wxPAT_NSHORTINT:
650 p->pad_nshortint = va_arg(argptr, short int *);
651 break;
652 case wxPAT_NLONGINT:
653 p->pad_nlongint = va_arg(argptr, long int *);
654 break;
655
656 case wxPAT_INVALID:
657 default:
658 return false;
659 }
660
661 return true; // loading was successful
662 }
663
664 template<typename CharType>
665 int wxPrintfConvSpec<CharType>::Process(CharType *buf, size_t lenMax, wxPrintfArg *p, size_t written)
666 {
667 // buffer to avoid dynamic memory allocation each time for small strings;
668 // note that this buffer is used only to hold results of number formatting,
669 // %s directly writes user's string in buf, without using szScratch
670 char szScratch[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN];
671 size_t lenScratch = 0, lenCur = 0;
672
673 #define APPEND_CH(ch) \
674 { \
675 if ( lenCur == lenMax ) \
676 return -1; \
677 \
678 buf[lenCur++] = ch; \
679 }
680
681 switch ( m_type )
682 {
683 case wxPAT_INT:
684 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_int);
685 break;
686
687 case wxPAT_LONGINT:
688 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longint);
689 break;
690
691 #ifdef wxLongLong_t
692 case wxPAT_LONGLONGINT:
693 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longlongint);
694 break;
695 #endif // SIZEOF_LONG_LONG
696
697 case wxPAT_SIZET:
698 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_sizet);
699 break;
700
701 case wxPAT_LONGDOUBLE:
702 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longdouble);
703 break;
704
705 case wxPAT_DOUBLE:
706 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_double);
707 break;
708
709 case wxPAT_POINTER:
710 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_pointer);
711 break;
712
713 case wxPAT_CHAR:
714 case wxPAT_WCHAR:
715 {
716 wxUniChar ch;
717 if (m_type == wxPAT_CHAR)
718 ch = p->pad_char;
719 else // m_type == wxPAT_WCHAR
720 ch = p->pad_wchar;
721
722 CharType val = ch;
723
724 size_t i;
725
726 if (!m_bAlignLeft)
727 for (i = 1; i < (size_t)m_nMinWidth; i++)
728 APPEND_CH(_T(' '));
729
730 APPEND_CH(val);
731
732 if (m_bAlignLeft)
733 for (i = 1; i < (size_t)m_nMinWidth; i++)
734 APPEND_CH(_T(' '));
735 }
736 break;
737
738 case wxPAT_PCHAR:
739 case wxPAT_PWCHAR:
740 {
741 wxArgNormalizedString arg(p->pad_str);
742 wxString s = arg;
743
744 if ( !arg.IsValid() && m_nMaxWidth >= 6 )
745 s = wxT("(null)");
746
747 typename wxPrintfStringHelper<CharType>::ConvertedType strbuf(
748 wxPrintfStringHelper<CharType>::Convert(s));
749
750 // at this point we are sure that m_nMaxWidth is positive or
751 // null (see top of wxPrintfConvSpec::LoadArg)
752 int len = wxMin((unsigned int)m_nMaxWidth, wxStrlen(strbuf));
753
754 int i;
755
756 if (!m_bAlignLeft)
757 {
758 for (i = len; i < m_nMinWidth; i++)
759 APPEND_CH(_T(' '));
760 }
761
762 len = wxMin((unsigned int)len, lenMax-lenCur);
763 wxStrncpy(buf+lenCur, strbuf, len);
764 lenCur += len;
765
766 if (m_bAlignLeft)
767 {
768 for (i = len; i < m_nMinWidth; i++)
769 APPEND_CH(_T(' '));
770 }
771 }
772 break;
773
774 case wxPAT_NINT:
775 *p->pad_nint = written;
776 break;
777
778 case wxPAT_NSHORTINT:
779 *p->pad_nshortint = (short int)written;
780 break;
781
782 case wxPAT_NLONGINT:
783 *p->pad_nlongint = written;
784 break;
785
786 case wxPAT_INVALID:
787 default:
788 return -1;
789 }
790
791 // if we used system's sprintf() then we now need to append the s_szScratch
792 // buffer to the given one...
793 switch (m_type)
794 {
795 case wxPAT_INT:
796 case wxPAT_LONGINT:
797 #ifdef wxLongLong_t
798 case wxPAT_LONGLONGINT:
799 #endif
800 case wxPAT_SIZET:
801 case wxPAT_LONGDOUBLE:
802 case wxPAT_DOUBLE:
803 case wxPAT_POINTER:
804 wxASSERT(lenScratch < wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN);
805 // NB: 1) we can compare lenMax (for CharType*, i.e. possibly
806 // wchar_t*) with lenScratch (char*) because this code is
807 // formatting integers and that will have the same length
808 // even in UTF-8 (the only case when char* length may be
809 // more than wchar_t* length of the same string)
810 // 2) wxStrncpy converts the 2nd argument to 1st argument's
811 // type transparently if their types differ, so this code
812 // works for both instantiations
813 if (lenMax < lenScratch)
814 {
815 // fill output buffer and then return -1
816 wxStrncpy(buf, szScratch, lenMax);
817 return -1;
818 }
819 wxStrncpy(buf, szScratch, lenScratch);
820 lenCur += lenScratch;
821 break;
822
823 default:
824 break; // all other cases were completed previously
825 }
826
827 return lenCur;
828 }
829
830 // Copy chars from source to dest converting '%%' to '%'. Takes at most maxIn
831 // chars from source and write at most outMax chars to dest, returns the
832 // number of chars actually written. Does not treat null specially.
833 template<typename CharType>
834 static int wxCopyStrWithPercents(
835 size_t maxOut,
836 CharType *dest,
837 size_t maxIn,
838 const CharType *source)
839 {
840 size_t written = 0;
841
842 if (maxIn == 0)
843 return 0;
844
845 size_t i;
846 for ( i = 0; i < maxIn-1 && written < maxOut; source++, i++)
847 {
848 dest[written++] = *source;
849 if (*(source+1) == wxT('%'))
850 {
851 // skip this additional '%' character
852 source++;
853 i++;
854 }
855 }
856
857 if (i < maxIn && written < maxOut)
858 // copy last character inconditionally
859 dest[written++] = *source;
860
861 return written;
862 }
863
864 template<typename CharType>
865 static int wxDoVsnprintf(CharType *buf, size_t lenMax,
866 const CharType *format, va_list argptr)
867 {
868 // useful for debugging, to understand if we are really using this function
869 // rather than the system implementation
870 #if 0
871 wprintf(L"Using wxCRT_VsnprintfW\n");
872 #endif
873
874 // required memory:
875 wxPrintfConvSpec<CharType> arg[wxMAX_SVNPRINTF_ARGUMENTS];
876 wxPrintfArg argdata[wxMAX_SVNPRINTF_ARGUMENTS];
877 wxPrintfConvSpec<CharType> *pspec[wxMAX_SVNPRINTF_ARGUMENTS] = { NULL };
878
879 size_t i;
880
881 // number of characters in the buffer so far, must be less than lenMax
882 size_t lenCur = 0;
883
884 size_t nargs = 0;
885 const CharType *toparse = format;
886
887 // parse the format string
888 bool posarg_present = false, nonposarg_present = false;
889 for (; *toparse != wxT('\0'); toparse++)
890 {
891 if (*toparse == wxT('%') )
892 {
893 arg[nargs].Init();
894
895 // let's see if this is a (valid) conversion specifier...
896 if (arg[nargs].Parse(toparse))
897 {
898 // ...yes it is
899 wxPrintfConvSpec<CharType> *current = &arg[nargs];
900
901 // make toparse point to the end of this specifier
902 toparse = current->m_pArgEnd;
903
904 if (current->m_pos > 0)
905 {
906 // the positionals start from number 1... adjust the index
907 current->m_pos--;
908 posarg_present = true;
909 }
910 else
911 {
912 // not a positional argument...
913 current->m_pos = nargs;
914 nonposarg_present = true;
915 }
916
917 // this conversion specifier is tied to the pos-th argument...
918 pspec[current->m_pos] = current;
919 nargs++;
920
921 if (nargs == wxMAX_SVNPRINTF_ARGUMENTS)
922 {
923 wxLogDebug(wxT("A single call to wxVsnprintf() has more than %d arguments; ")
924 wxT("ignoring all remaining arguments."), wxMAX_SVNPRINTF_ARGUMENTS);
925 break; // cannot handle any additional conv spec
926 }
927 }
928 else
929 {
930 // it's safe to look in the next character of toparse as at worst
931 // we'll hit its \0
932 if (*(toparse+1) == wxT('%'))
933 toparse++; // the Parse() returned false because we've found a %%
934 }
935 }
936 }
937
938 if (posarg_present && nonposarg_present)
939 {
940 buf[0] = 0;
941 return -1; // format strings with both positional and
942 } // non-positional conversion specifier are unsupported !!
943
944 // on platforms where va_list is an array type, it is necessary to make a
945 // copy to be able to pass it to LoadArg as a reference.
946 bool ok = true;
947 va_list ap;
948 wxVaCopy(ap, argptr);
949
950 // now load arguments from stack
951 for (i=0; i < nargs && ok; i++)
952 {
953 // !pspec[i] means that the user forgot a positional parameter (e.g. %$1s %$3s);
954 // LoadArg == false means that wxPrintfConvSpec::Parse failed to set the
955 // conversion specifier 'type' to a valid value...
956 ok = pspec[i] && pspec[i]->LoadArg(&argdata[i], ap);
957 }
958
959 va_end(ap);
960
961 // something failed while loading arguments from the variable list...
962 // (e.g. the user repeated twice the same positional argument)
963 if (!ok)
964 {
965 buf[0] = 0;
966 return -1;
967 }
968
969 // finally, process each conversion specifier with its own argument
970 toparse = format;
971 for (i=0; i < nargs; i++)
972 {
973 // copy in the output buffer the portion of the format string between
974 // last specifier and the current one
975 size_t tocopy = ( arg[i].m_pArgPos - toparse );
976
977 lenCur += wxCopyStrWithPercents(lenMax - lenCur, buf + lenCur,
978 tocopy, toparse);
979 if (lenCur == lenMax)
980 {
981 buf[lenMax - 1] = 0;
982 return lenMax+1; // not enough space in the output buffer !
983 }
984
985 // process this specifier directly in the output buffer
986 int n = arg[i].Process(buf+lenCur, lenMax - lenCur, &argdata[arg[i].m_pos], lenCur);
987 if (n == -1)
988 {
989 buf[lenMax-1] = wxT('\0'); // be sure to always NUL-terminate the string
990 return lenMax+1; // not enough space in the output buffer !
991 }
992 lenCur += n;
993
994 // the +1 is because wxPrintfConvSpec::m_pArgEnd points to the last character
995 // of the format specifier, but we are not interested to it...
996 toparse = arg[i].m_pArgEnd + 1;
997 }
998
999 // copy portion of the format string after last specifier
1000 // NOTE: toparse is pointing to the character just after the last processed
1001 // conversion specifier
1002 // NOTE2: the +1 is because we want to copy also the '\0'
1003 size_t tocopy = wxStrlen(format) + 1 - ( toparse - format ) ;
1004
1005 lenCur += wxCopyStrWithPercents(lenMax - lenCur, buf + lenCur,
1006 tocopy, toparse) - 1;
1007 if (buf[lenCur])
1008 {
1009 buf[lenCur] = 0;
1010 return lenMax+1; // not enough space in the output buffer !
1011 }
1012
1013 // Don't do:
1014 // wxASSERT(lenCur == wxStrlen(buf));
1015 // in fact if we embedded NULLs in the output buffer (using %c with a '\0')
1016 // such check would fail
1017
1018 return lenCur;
1019 }
1020
1021 #undef APPEND_CH
1022 #undef CHECK_PREC
1023
1024 } // anonymous namespace
1025
1026 #endif // !defined(wxCRT_VsnprintfW) || !defined(wxCRT_VsnprintfA)
1027
1028 // ----------------------------------------------------------------------------
1029 // wxCRT_VsnprintfW
1030 // ----------------------------------------------------------------------------
1031
1032 #if !defined(wxCRT_VsnprintfW)
1033
1034 #if !wxUSE_WXVSNPRINTFW
1035 #error "wxUSE_WXVSNPRINTFW must be 1 if our wxCRT_VsnprintfW is used"
1036 #endif
1037
1038 int wxCRT_VsnprintfW(wchar_t *buf, size_t len,
1039 const wchar_t *format, va_list argptr)
1040 {
1041 return wxDoVsnprintf(buf, len, format, argptr);
1042 }
1043
1044 #else // wxCRT_VsnprintfW is defined
1045
1046 #if wxUSE_WXVSNPRINTFW
1047 #error "wxUSE_WXVSNPRINTFW must be 0 if our wxCRT_VsnprintfW is not used"
1048 #endif
1049
1050 #endif // !wxCRT_VsnprintfW
1051
1052 // ----------------------------------------------------------------------------
1053 // wxCRT_VsnprintfA
1054 // ----------------------------------------------------------------------------
1055
1056 #ifndef wxCRT_VsnprintfA
1057
1058 #if !wxUSE_WXVSNPRINTFA
1059 #error "wxUSE_WXVSNPRINTFA must be 1 if our wxCRT_VsnprintfA is used"
1060 #endif
1061
1062 int wxCRT_VsnprintfA(char *buf, size_t len,
1063 const char *format, va_list argptr)
1064 {
1065 return wxDoVsnprintf(buf, len, format, argptr);
1066 }
1067
1068 #else // wxCRT_VsnprintfA is defined
1069
1070 #if wxUSE_WXVSNPRINTFA
1071 #error "wxUSE_WXVSNPRINTFA must be 0 if our wxCRT_VsnprintfA is not used"
1072 #endif
1073
1074 #endif // !wxCRT_VsnprintfA