]> git.saurik.com Git - wxWidgets.git/blame - include/wx/private/wxprintf.h
Blind fix for bug #1936227 (unable to set initial value > 100).
[wxWidgets.git] / include / wx / private / wxprintf.h
CommitLineData
47346406
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/private/wxprintf.h
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#ifndef _WX_PRIVATE_WXPRINTF_H_
13#define _WX_PRIVATE_WXPRINTF_H_
14
15// ---------------------------------------------------------------------------
16// headers and macros
17// ---------------------------------------------------------------------------
18
19#include "wx/crt.h"
05962bdd
JS
20#include "wx/log.h"
21#include "wx/utils.h"
47346406
VS
22
23#include <string.h>
24
25#if defined(__MWERKS__) && __MSL__ >= 0x6000
26namespace std {}
27using namespace std ;
28#endif
29
30// prefer snprintf over sprintf
31#if defined(__VISUALC__) || \
32 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
33 #define system_sprintf(buff, max, flags, data) \
34 ::_snprintf(buff, max, flags, data)
35#elif defined(HAVE_SNPRINTF)
36 #define system_sprintf(buff, max, flags, data) \
37 ::snprintf(buff, max, flags, data)
38#else // NB: at least sprintf() should always be available
39 // since 'max' is not used in this case, wxVsnprintf() should always
40 // ensure that 'buff' is big enough for all common needs
41 // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN)
42 #define system_sprintf(buff, max, flags, data) \
43 ::sprintf(buff, flags, data)
44
45 #define SYSTEM_SPRINTF_IS_UNSAFE
46#endif
47
48// ---------------------------------------------------------------------------
49// printf format string parsing
50// ---------------------------------------------------------------------------
51
52// some limits of our implementation
53#define wxMAX_SVNPRINTF_ARGUMENTS 64
54#define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32
55#define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512
56
57
58// the conversion specifiers accepted by wxCRT_VsnprintfW
59enum wxPrintfArgType {
60 wxPAT_INVALID = -1,
61
62 wxPAT_INT, // %d, %i, %o, %u, %x, %X
63 wxPAT_LONGINT, // %ld, etc
64#ifdef wxLongLong_t
65 wxPAT_LONGLONGINT, // %Ld, etc
66#endif
67 wxPAT_SIZET, // %Zd, etc
68
69 wxPAT_DOUBLE, // %e, %E, %f, %g, %G
70 wxPAT_LONGDOUBLE, // %le, etc
71
72 wxPAT_POINTER, // %p
73
74 wxPAT_CHAR, // %hc (in ANSI mode: %c, too)
75 wxPAT_WCHAR, // %lc (in Unicode mode: %c, too)
76
77 wxPAT_PCHAR, // %s (related to a char *)
78 wxPAT_PWCHAR, // %s (related to a wchar_t *)
79
80 wxPAT_NINT, // %n
81 wxPAT_NSHORTINT, // %hn
82 wxPAT_NLONGINT // %ln
83};
84
85// an argument passed to wxCRT_VsnprintfW
86typedef union {
87 int pad_int; // %d, %i, %o, %u, %x, %X
88 long int pad_longint; // %ld, etc
89#ifdef wxLongLong_t
90 wxLongLong_t pad_longlongint; // %Ld, etc
91#endif
92 size_t pad_sizet; // %Zd, etc
93
94 double pad_double; // %e, %E, %f, %g, %G
95 long double pad_longdouble; // %le, etc
96
97 void *pad_pointer; // %p
98
99 char pad_char; // %hc (in ANSI mode: %c, too)
100 wchar_t pad_wchar; // %lc (in Unicode mode: %c, too)
101
102 void *pad_str; // %s
103
104 int *pad_nint; // %n
105 short int *pad_nshortint; // %hn
106 long int *pad_nlongint; // %ln
107} wxPrintfArg;
108
109// helper for converting string into either char* or wchar_t* dependening
110// on the type of wxPrintfConvSpec<T> instantiation:
111template<typename CharType> struct wxPrintfStringHelper {};
112
113template<> struct wxPrintfStringHelper<char>
114{
115 typedef const wxWX2MBbuf ConvertedType;
116 static ConvertedType Convert(const wxString& s) { return s.mb_str(); }
117};
118
119template<> struct wxPrintfStringHelper<wchar_t>
120{
121 typedef const wxWX2WCbuf ConvertedType;
122 static ConvertedType Convert(const wxString& s) { return s.wc_str(); }
123};
124
125
126// Contains parsed data relative to a conversion specifier given to
127// wxCRT_VsnprintfW and parsed from the format string
128// NOTE: in C++ there is almost no difference between struct & classes thus
129// there is no performance gain by using a struct here...
130template<typename CharType>
131class wxPrintfConvSpec
132{
133public:
134
135 // the position of the argument relative to this conversion specifier
136 size_t m_pos;
137
138 // the type of this conversion specifier
139 wxPrintfArgType m_type;
140
141 // the minimum and maximum width
142 // when one of this var is set to -1 it means: use the following argument
143 // in the stack as minimum/maximum width for this conversion specifier
144 int m_nMinWidth, m_nMaxWidth;
145
146 // does the argument need to the be aligned to left ?
147 bool m_bAlignLeft;
148
149 // pointer to the '%' of this conversion specifier in the format string
150 // NOTE: this points somewhere in the string given to the Parse() function -
151 // it's task of the caller ensure that memory is still valid !
152 const CharType *m_pArgPos;
153
154 // pointer to the last character of this conversion specifier in the
155 // format string
156 // NOTE: this points somewhere in the string given to the Parse() function -
157 // it's task of the caller ensure that memory is still valid !
158 const CharType *m_pArgEnd;
159
160 // a little buffer where formatting flags like #+\.hlqLZ are stored by Parse()
161 // for use in Process()
162 // NB: even if this buffer is used only for numeric conversion specifiers
163 // and thus could be safely declared as a char[] buffer, we want it to
164 // be wchar_t so that in Unicode builds we can avoid to convert its
165 // contents to Unicode chars when copying it in user's buffer.
166 char m_szFlags[wxMAX_SVNPRINTF_FLAGBUFFER_LEN];
167
168
169public:
170
171 // we don't declare this as a constructor otherwise it would be called
172 // automatically and we don't want this: to be optimized, wxCRT_VsnprintfW
173 // calls this function only on really-used instances of this class.
174 void Init();
175
176 // Parses the first conversion specifier in the given string, which must
177 // begin with a '%'. Returns false if the first '%' does not introduce a
178 // (valid) conversion specifier and thus should be ignored.
179 bool Parse(const CharType *format);
180
181 // Process this conversion specifier and puts the result in the given
182 // buffer. Returns the number of characters written in 'buf' or -1 if
183 // there's not enough space.
184 int Process(CharType *buf, size_t lenMax, wxPrintfArg *p, size_t written);
185
186 // Loads the argument of this conversion specifier from given va_list.
187 bool LoadArg(wxPrintfArg *p, va_list &argptr);
188
189private:
190 // An helper function of LoadArg() which is used to handle the '*' flag
191 void ReplaceAsteriskWith(int w);
192};
193
194template<typename CharType>
195void wxPrintfConvSpec<CharType>::Init()
196{
197 m_nMinWidth = 0;
198 m_nMaxWidth = 0xFFFF;
199 m_pos = 0;
200 m_bAlignLeft = false;
201 m_pArgPos = m_pArgEnd = NULL;
202 m_type = wxPAT_INVALID;
203
204 // this character will never be removed from m_szFlags array and
205 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
206 m_szFlags[0] = '%';
207}
208
209template<typename CharType>
210bool wxPrintfConvSpec<CharType>::Parse(const CharType *format)
211{
212 bool done = false;
213
214 // temporary parse data
215 size_t flagofs = 1;
216 bool in_prec, // true if we found the dot in some previous iteration
217 prec_dot; // true if the dot has been already added to m_szFlags
218 int ilen = 0;
219
220 m_bAlignLeft = in_prec = prec_dot = false;
221 m_pArgPos = m_pArgEnd = format;
222 do
223 {
224#define CHECK_PREC \
225 if (in_prec && !prec_dot) \
226 { \
227 m_szFlags[flagofs++] = '.'; \
228 prec_dot = true; \
229 }
230
231 // what follows '%'?
232 const CharType ch = *(++m_pArgEnd);
233 switch ( ch )
234 {
235 case wxT('\0'):
236 return false; // not really an argument
237
238 case wxT('%'):
239 return false; // not really an argument
240
241 case wxT('#'):
242 case wxT('0'):
243 case wxT(' '):
244 case wxT('+'):
245 case wxT('\''):
246 CHECK_PREC
247 m_szFlags[flagofs++] = char(ch);
248 break;
249
250 case wxT('-'):
251 CHECK_PREC
252 m_bAlignLeft = true;
253 m_szFlags[flagofs++] = char(ch);
254 break;
255
256 case wxT('.'):
257 CHECK_PREC
258 in_prec = true;
259 prec_dot = false;
260 m_nMaxWidth = 0;
261 // dot will be auto-added to m_szFlags if non-negative
262 // number follows
263 break;
264
265 case wxT('h'):
266 ilen = -1;
267 CHECK_PREC
268 m_szFlags[flagofs++] = char(ch);
269 break;
270
271 case wxT('l'):
272 // NB: it's safe to use flagofs-1 as flagofs always start from 1
273 if (m_szFlags[flagofs-1] == 'l') // 'll' modifier is the same as 'L' or 'q'
274 ilen = 2;
275 else
276 ilen = 1;
277 CHECK_PREC
278 m_szFlags[flagofs++] = char(ch);
279 break;
280
281 case wxT('q'):
282 case wxT('L'):
283 ilen = 2;
284 CHECK_PREC
285 m_szFlags[flagofs++] = char(ch);
286 break;
287#ifdef __WXMSW__
288 // under Windows we support the special '%I64' notation as longlong
289 // integer conversion specifier for MSVC compatibility
290 // (it behaves exactly as '%lli' or '%Li' or '%qi')
291 case wxT('I'):
292 if (*(m_pArgEnd+1) != wxT('6') ||
293 *(m_pArgEnd+2) != wxT('4'))
294 return false; // bad format
295
296 m_pArgEnd++;
297 m_pArgEnd++;
298
299 ilen = 2;
300 CHECK_PREC
301 m_szFlags[flagofs++] = char(ch);
302 m_szFlags[flagofs++] = '6';
303 m_szFlags[flagofs++] = '4';
304 break;
305#endif // __WXMSW__
306
307 case wxT('Z'):
308 ilen = 3;
309 CHECK_PREC
310 m_szFlags[flagofs++] = char(ch);
311 break;
312
313 case wxT('*'):
314 if (in_prec)
315 {
316 CHECK_PREC
317
318 // tell Process() to use the next argument
319 // in the stack as maxwidth...
320 m_nMaxWidth = -1;
321 }
322 else
323 {
324 // tell Process() to use the next argument
325 // in the stack as minwidth...
326 m_nMinWidth = -1;
327 }
328
329 // save the * in our formatting buffer...
330 // will be replaced later by Process()
331 m_szFlags[flagofs++] = char(ch);
332 break;
333
334 case wxT('1'): case wxT('2'): case wxT('3'):
335 case wxT('4'): case wxT('5'): case wxT('6'):
336 case wxT('7'): case wxT('8'): case wxT('9'):
337 {
338 int len = 0;
339 CHECK_PREC
340 while ( (*m_pArgEnd >= CharType('0')) &&
341 (*m_pArgEnd <= CharType('9')) )
342 {
343 m_szFlags[flagofs++] = char(*m_pArgEnd);
344 len = len*10 + (*m_pArgEnd - wxT('0'));
345 m_pArgEnd++;
346 }
347
348 if (in_prec)
349 m_nMaxWidth = len;
350 else
351 m_nMinWidth = len;
352
353 m_pArgEnd--; // the main loop pre-increments n again
354 }
355 break;
356
357 case wxT('$'): // a positional parameter (e.g. %2$s) ?
358 {
359 if (m_nMinWidth <= 0)
360 break; // ignore this formatting flag as no
361 // numbers are preceding it
362
363 // remove from m_szFlags all digits previously added
364 do {
365 flagofs--;
366 } while (m_szFlags[flagofs] >= '1' &&
367 m_szFlags[flagofs] <= '9');
368
369 // re-adjust the offset making it point to the
370 // next free char of m_szFlags
371 flagofs++;
372
373 m_pos = m_nMinWidth;
374 m_nMinWidth = 0;
375 }
376 break;
377
378 case wxT('d'):
379 case wxT('i'):
380 case wxT('o'):
381 case wxT('u'):
382 case wxT('x'):
383 case wxT('X'):
384 CHECK_PREC
385 m_szFlags[flagofs++] = char(ch);
386 m_szFlags[flagofs] = '\0';
387 if (ilen == 0)
388 m_type = wxPAT_INT;
389 else if (ilen == -1)
390 // NB: 'short int' value passed through '...'
391 // is promoted to 'int', so we have to get
392 // an int from stack even if we need a short
393 m_type = wxPAT_INT;
394 else if (ilen == 1)
395 m_type = wxPAT_LONGINT;
396 else if (ilen == 2)
397#ifdef wxLongLong_t
398 m_type = wxPAT_LONGLONGINT;
399#else // !wxLongLong_t
400 m_type = wxPAT_LONGINT;
401#endif // wxLongLong_t/!wxLongLong_t
402 else if (ilen == 3)
403 m_type = wxPAT_SIZET;
404 done = true;
405 break;
406
407 case wxT('e'):
408 case wxT('E'):
409 case wxT('f'):
410 case wxT('g'):
411 case wxT('G'):
412 CHECK_PREC
413 m_szFlags[flagofs++] = char(ch);
414 m_szFlags[flagofs] = '\0';
415 if (ilen == 2)
416 m_type = wxPAT_LONGDOUBLE;
417 else
418 m_type = wxPAT_DOUBLE;
419 done = true;
420 break;
421
422 case wxT('p'):
423 m_type = wxPAT_POINTER;
424 m_szFlags[flagofs++] = char(ch);
425 m_szFlags[flagofs] = '\0';
426 done = true;
427 break;
428
429 case wxT('c'):
430 if (ilen == -1)
431 {
432 // in Unicode mode %hc == ANSI character
433 // and in ANSI mode, %hc == %c == ANSI...
434 m_type = wxPAT_CHAR;
435 }
436 else if (ilen == 1)
437 {
438 // in ANSI mode %lc == Unicode character
439 // and in Unicode mode, %lc == %c == Unicode...
440 m_type = wxPAT_WCHAR;
441 }
442 else
443 {
444#if wxUSE_UNICODE
445 // in Unicode mode, %c == Unicode character
446 m_type = wxPAT_WCHAR;
447#else
448 // in ANSI mode, %c == ANSI character
449 m_type = wxPAT_CHAR;
450#endif
451 }
452 done = true;
453 break;
454
455 case wxT('s'):
456 if (ilen == -1)
457 {
458 // Unicode mode wx extension: we'll let %hs mean non-Unicode
459 // strings (when in ANSI mode, %s == %hs == ANSI string)
460 m_type = wxPAT_PCHAR;
461 }
462 else if (ilen == 1)
463 {
464 // in Unicode mode, %ls == %s == Unicode string
465 // in ANSI mode, %ls == Unicode string
466 m_type = wxPAT_PWCHAR;
467 }
468 else
469 {
470#if wxUSE_UNICODE
471 m_type = wxPAT_PWCHAR;
472#else
473 m_type = wxPAT_PCHAR;
474#endif
475 }
476 done = true;
477 break;
478
479 case wxT('n'):
480 if (ilen == 0)
481 m_type = wxPAT_NINT;
482 else if (ilen == -1)
483 m_type = wxPAT_NSHORTINT;
484 else if (ilen >= 1)
485 m_type = wxPAT_NLONGINT;
486 done = true;
487 break;
488
489 default:
490 // bad format, don't consider this an argument;
491 // leave it unchanged
492 return false;
493 }
494
495 if (flagofs == wxMAX_SVNPRINTF_FLAGBUFFER_LEN)
496 {
497 wxLogDebug(wxT("Too many flags specified for a single conversion specifier!"));
498 return false;
499 }
500 }
501 while (!done);
502
503 return true; // parsing was successful
504}
505
506template<typename CharType>
507void wxPrintfConvSpec<CharType>::ReplaceAsteriskWith(int width)
508{
509 char temp[wxMAX_SVNPRINTF_FLAGBUFFER_LEN];
510
511 // find the first * in our flag buffer
512 char *pwidth = strchr(m_szFlags, '*');
513 wxCHECK_RET(pwidth, _T("field width must be specified"));
514
515 // save what follows the * (the +1 is to skip the asterisk itself!)
516 strcpy(temp, pwidth+1);
517 if (width < 0)
518 {
519 pwidth[0] = wxT('-');
520 pwidth++;
521 }
522
523 // replace * with the actual integer given as width
524#ifndef SYSTEM_SPRINTF_IS_UNSAFE
525 int maxlen = (m_szFlags + wxMAX_SVNPRINTF_FLAGBUFFER_LEN - pwidth) /
526 sizeof(*m_szFlags);
527#endif
528 int offset = system_sprintf(pwidth, maxlen, "%d", abs(width));
529
530 // restore after the expanded * what was following it
531 strcpy(pwidth+offset, temp);
532}
533
534template<typename CharType>
535bool wxPrintfConvSpec<CharType>::LoadArg(wxPrintfArg *p, va_list &argptr)
536{
537 // did the '*' width/precision specifier was used ?
538 if (m_nMaxWidth == -1)
539 {
540 // take the maxwidth specifier from the stack
541 m_nMaxWidth = va_arg(argptr, int);
542 if (m_nMaxWidth < 0)
543 m_nMaxWidth = 0;
544 else
545 ReplaceAsteriskWith(m_nMaxWidth);
546 }
547
548 if (m_nMinWidth == -1)
549 {
550 // take the minwidth specifier from the stack
551 m_nMinWidth = va_arg(argptr, int);
552
553 ReplaceAsteriskWith(m_nMinWidth);
554 if (m_nMinWidth < 0)
555 {
556 m_bAlignLeft = !m_bAlignLeft;
557 m_nMinWidth = -m_nMinWidth;
558 }
559 }
560
561 switch (m_type) {
562 case wxPAT_INT:
563 p->pad_int = va_arg(argptr, int);
564 break;
565 case wxPAT_LONGINT:
566 p->pad_longint = va_arg(argptr, long int);
567 break;
568#ifdef wxLongLong_t
569 case wxPAT_LONGLONGINT:
570 p->pad_longlongint = va_arg(argptr, wxLongLong_t);
571 break;
572#endif // wxLongLong_t
573 case wxPAT_SIZET:
574 p->pad_sizet = va_arg(argptr, size_t);
575 break;
576 case wxPAT_DOUBLE:
577 p->pad_double = va_arg(argptr, double);
578 break;
579 case wxPAT_LONGDOUBLE:
580 p->pad_longdouble = va_arg(argptr, long double);
581 break;
582 case wxPAT_POINTER:
583 p->pad_pointer = va_arg(argptr, void *);
584 break;
585
586 case wxPAT_CHAR:
587 p->pad_char = (char)va_arg(argptr, int); // char is promoted to int when passed through '...'
588 break;
589 case wxPAT_WCHAR:
590 p->pad_wchar = (wchar_t)va_arg(argptr, int); // char is promoted to int when passed through '...'
591 break;
592
593 case wxPAT_PCHAR:
594 case wxPAT_PWCHAR:
595 p->pad_str = va_arg(argptr, void *);
596 break;
597
598 case wxPAT_NINT:
599 p->pad_nint = va_arg(argptr, int *);
600 break;
601 case wxPAT_NSHORTINT:
602 p->pad_nshortint = va_arg(argptr, short int *);
603 break;
604 case wxPAT_NLONGINT:
605 p->pad_nlongint = va_arg(argptr, long int *);
606 break;
607
608 case wxPAT_INVALID:
609 default:
610 return false;
611 }
612
613 return true; // loading was successful
614}
615
616template<typename CharType>
617int wxPrintfConvSpec<CharType>::Process(CharType *buf, size_t lenMax, wxPrintfArg *p, size_t written)
618{
619 // buffer to avoid dynamic memory allocation each time for small strings;
620 // note that this buffer is used only to hold results of number formatting,
621 // %s directly writes user's string in buf, without using szScratch
622 char szScratch[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN];
623 size_t lenScratch = 0, lenCur = 0;
624
625#define APPEND_CH(ch) \
626 { \
627 if ( lenCur == lenMax ) \
628 return -1; \
629 \
630 buf[lenCur++] = ch; \
631 }
632
633 switch ( m_type )
634 {
635 case wxPAT_INT:
636 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_int);
637 break;
638
639 case wxPAT_LONGINT:
640 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longint);
641 break;
642
643#ifdef wxLongLong_t
644 case wxPAT_LONGLONGINT:
645 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longlongint);
646 break;
647#endif // SIZEOF_LONG_LONG
648
649 case wxPAT_SIZET:
650 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_sizet);
651 break;
652
653 case wxPAT_LONGDOUBLE:
654 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longdouble);
655 break;
656
657 case wxPAT_DOUBLE:
658 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_double);
659 break;
660
661 case wxPAT_POINTER:
662 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_pointer);
663 break;
664
665 case wxPAT_CHAR:
666 case wxPAT_WCHAR:
667 {
668 wxUniChar ch;
669 if (m_type == wxPAT_CHAR)
670 ch = p->pad_char;
671 else // m_type == wxPAT_WCHAR
672 ch = p->pad_wchar;
673
674 CharType val = ch;
675
676 size_t i;
677
678 if (!m_bAlignLeft)
679 for (i = 1; i < (size_t)m_nMinWidth; i++)
680 APPEND_CH(_T(' '));
681
682 APPEND_CH(val);
683
684 if (m_bAlignLeft)
685 for (i = 1; i < (size_t)m_nMinWidth; i++)
686 APPEND_CH(_T(' '));
687 }
688 break;
689
690 case wxPAT_PCHAR:
691 case wxPAT_PWCHAR:
692 {
693 wxArgNormalizedString arg(p->pad_str);
694 wxString s = arg;
695
696 if ( !arg.IsValid() && m_nMaxWidth >= 6 )
697 s = wxT("(null)");
698
699 typename wxPrintfStringHelper<CharType>::ConvertedType strbuf(
700 wxPrintfStringHelper<CharType>::Convert(s));
701
702 // at this point we are sure that m_nMaxWidth is positive or
703 // null (see top of wxPrintfConvSpec::LoadArg)
704 int len = wxMin((unsigned int)m_nMaxWidth, wxStrlen(strbuf));
705
706 int i;
707
708 if (!m_bAlignLeft)
709 {
710 for (i = len; i < m_nMinWidth; i++)
711 APPEND_CH(_T(' '));
712 }
713
714 len = wxMin((unsigned int)len, lenMax-lenCur);
715 wxStrncpy(buf+lenCur, strbuf, len);
716 lenCur += len;
717
718 if (m_bAlignLeft)
719 {
720 for (i = len; i < m_nMinWidth; i++)
721 APPEND_CH(_T(' '));
722 }
723 }
724 break;
725
726 case wxPAT_NINT:
727 *p->pad_nint = written;
728 break;
729
730 case wxPAT_NSHORTINT:
731 *p->pad_nshortint = (short int)written;
732 break;
733
734 case wxPAT_NLONGINT:
735 *p->pad_nlongint = written;
736 break;
737
738 case wxPAT_INVALID:
739 default:
740 return -1;
741 }
742
743 // if we used system's sprintf() then we now need to append the s_szScratch
744 // buffer to the given one...
745 switch (m_type)
746 {
747 case wxPAT_INT:
748 case wxPAT_LONGINT:
749#ifdef wxLongLong_t
750 case wxPAT_LONGLONGINT:
751#endif
752 case wxPAT_SIZET:
753 case wxPAT_LONGDOUBLE:
754 case wxPAT_DOUBLE:
755 case wxPAT_POINTER:
756 wxASSERT(lenScratch < wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN);
757 // NB: 1) we can compare lenMax (for CharType*, i.e. possibly
758 // wchar_t*) with lenScratch (char*) because this code is
759 // formatting integers and that will have the same length
760 // even in UTF-8 (the only case when char* length may be
761 // more than wchar_t* length of the same string)
762 // 2) wxStrncpy converts the 2nd argument to 1st argument's
763 // type transparently if their types differ, so this code
764 // works for both instantiations
765 if (lenMax < lenScratch)
766 {
767 // fill output buffer and then return -1
768 wxStrncpy(buf, szScratch, lenMax);
769 return -1;
770 }
771 wxStrncpy(buf, szScratch, lenScratch);
772 lenCur += lenScratch;
773 break;
774
775 default:
776 break; // all other cases were completed previously
777 }
778
779 return lenCur;
780}
781
782
783// helper that parses format string
784template<typename CharType>
785struct wxPrintfConvSpecParser
786{
787 wxPrintfConvSpecParser(const CharType *format)
788 : posarg_present(false), nonposarg_present(false),
789 nargs(0)
790 {
791 memset(pspec, 0, sizeof(pspec));
792
793 const CharType *toparse = format;
794
795 // parse the format string
796 for (; *toparse != wxT('\0'); toparse++)
797 {
798 if (*toparse == wxT('%') )
799 {
800 arg[nargs].Init();
801
802 // let's see if this is a (valid) conversion specifier...
803 if (arg[nargs].Parse(toparse))
804 {
805 // ...yes it is
806 wxPrintfConvSpec<CharType> *current = &arg[nargs];
807
808 // make toparse point to the end of this specifier
809 toparse = current->m_pArgEnd;
810
811 if (current->m_pos > 0)
812 {
813 // the positionals start from number 1... adjust the index
814 current->m_pos--;
815 posarg_present = true;
816 }
817 else
818 {
819 // not a positional argument...
820 current->m_pos = nargs;
821 nonposarg_present = true;
822 }
823
824 // this conversion specifier is tied to the pos-th argument...
825 pspec[current->m_pos] = current;
826 nargs++;
827
828 if (nargs == wxMAX_SVNPRINTF_ARGUMENTS)
829 {
830 wxLogDebug(wxT("A single call to wxVsnprintf() has more than %d arguments; ")
831 wxT("ignoring all remaining arguments."), wxMAX_SVNPRINTF_ARGUMENTS);
832 break; // cannot handle any additional conv spec
833 }
834 }
835 else
836 {
837 // it's safe to look in the next character of toparse as at
838 // worst we'll hit its \0
839 if (*(toparse+1) == wxT('%'))
840 {
841 // the Parse() returned false because we've found a %%
842 toparse++;
843 }
844 }
845 }
846 }
847 }
848
849 wxPrintfConvSpec<CharType> arg[wxMAX_SVNPRINTF_ARGUMENTS];
850 wxPrintfConvSpec<CharType> *pspec[wxMAX_SVNPRINTF_ARGUMENTS];
851 bool posarg_present, nonposarg_present;
852 unsigned nargs;
853};
854
855#undef APPEND_CH
856#undef CHECK_PREC
857
858#endif // _WX_PRIVATE_WXPRINTF_H_