]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/private/wxprintf.h
Update vc10 build file versions to 3.0.0.
[wxWidgets.git] / include / wx / private / wxprintf.h
... / ...
CommitLineData
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// Copyright: (c) wxWidgets copyright
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifndef _WX_PRIVATE_WXPRINTF_H_
12#define _WX_PRIVATE_WXPRINTF_H_
13
14// ---------------------------------------------------------------------------
15// headers and macros
16// ---------------------------------------------------------------------------
17
18#include "wx/crt.h"
19#include "wx/log.h"
20#include "wx/utils.h"
21
22#include <string.h>
23
24// prefer snprintf over sprintf
25#if defined(__VISUALC__) || \
26 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
27 #define system_sprintf(buff, max, flags, data) \
28 ::_snprintf(buff, max, flags, data)
29#elif defined(HAVE_SNPRINTF)
30 #define system_sprintf(buff, max, flags, data) \
31 ::snprintf(buff, max, flags, data)
32#else // NB: at least sprintf() should always be available
33 // since 'max' is not used in this case, wxVsnprintf() should always
34 // ensure that 'buff' is big enough for all common needs
35 // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN)
36 #define system_sprintf(buff, max, flags, data) \
37 ::sprintf(buff, flags, data)
38
39 #define SYSTEM_SPRINTF_IS_UNSAFE
40#endif
41
42// ---------------------------------------------------------------------------
43// printf format string parsing
44// ---------------------------------------------------------------------------
45
46// some limits of our implementation
47#define wxMAX_SVNPRINTF_ARGUMENTS 64
48#define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32
49#define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512
50
51
52// the conversion specifiers accepted by wxCRT_VsnprintfW
53enum wxPrintfArgType
54{
55 wxPAT_INVALID = -1,
56
57 wxPAT_INT, // %d, %i, %o, %u, %x, %X
58 wxPAT_LONGINT, // %ld, etc
59#ifdef wxLongLong_t
60 wxPAT_LONGLONGINT, // %Ld, etc
61#endif
62 wxPAT_SIZET, // %zd, etc
63
64 wxPAT_DOUBLE, // %e, %E, %f, %g, %G
65 wxPAT_LONGDOUBLE, // %le, etc
66
67 wxPAT_POINTER, // %p
68
69 wxPAT_CHAR, // %hc (in ANSI mode: %c, too)
70 wxPAT_WCHAR, // %lc (in Unicode mode: %c, too)
71
72 wxPAT_PCHAR, // %s (related to a char *)
73 wxPAT_PWCHAR, // %s (related to a wchar_t *)
74
75 wxPAT_NINT, // %n
76 wxPAT_NSHORTINT, // %hn
77 wxPAT_NLONGINT, // %ln
78
79 wxPAT_STAR // '*' used for width or precision
80};
81
82// an argument passed to wxCRT_VsnprintfW
83union wxPrintfArg
84{
85 int pad_int; // %d, %i, %o, %u, %x, %X
86 long int pad_longint; // %ld, etc
87#ifdef wxLongLong_t
88 wxLongLong_t pad_longlongint; // %Ld, etc
89#endif
90 size_t pad_sizet; // %zd, etc
91
92 double pad_double; // %e, %E, %f, %g, %G
93 long double pad_longdouble; // %le, etc
94
95 void *pad_pointer; // %p
96
97 char pad_char; // %hc (in ANSI mode: %c, too)
98 wchar_t pad_wchar; // %lc (in Unicode mode: %c, too)
99
100 void *pad_str; // %s
101
102 int *pad_nint; // %n
103 short int *pad_nshortint; // %hn
104 long int *pad_nlongint; // %ln
105};
106
107// helper for converting string into either char* or wchar_t* depending
108// on the type of wxPrintfConvSpec<T> instantiation:
109template<typename CharType> struct wxPrintfStringHelper {};
110
111template<> struct wxPrintfStringHelper<char>
112{
113 typedef const wxWX2MBbuf ConvertedType;
114 static ConvertedType Convert(const wxString& s) { return s.mb_str(); }
115};
116
117template<> struct wxPrintfStringHelper<wchar_t>
118{
119 typedef const wxWX2WCbuf ConvertedType;
120 static ConvertedType Convert(const wxString& s) { return s.wc_str(); }
121};
122
123
124// Contains parsed data relative to a conversion specifier given to
125// wxCRT_VsnprintfW and parsed from the format string
126// NOTE: in C++ there is almost no difference between struct & classes thus
127// there is no performance gain by using a struct here...
128template<typename CharType>
129class wxPrintfConvSpec
130{
131public:
132
133 // the position of the argument relative to this conversion specifier
134 size_t m_pos;
135
136 // the type of this conversion specifier
137 wxPrintfArgType m_type;
138
139 // the minimum and maximum width
140 // when one of this var is set to -1 it means: use the following argument
141 // in the stack as minimum/maximum width for this conversion specifier
142 int m_nMinWidth, m_nMaxWidth;
143
144 // does the argument need to the be aligned to left ?
145 bool m_bAlignLeft;
146
147 // pointer to the '%' of this conversion specifier in the format string
148 // NOTE: this points somewhere in the string given to the Parse() function -
149 // it's task of the caller ensure that memory is still valid !
150 const CharType *m_pArgPos;
151
152 // pointer to the last character of this conversion specifier in the
153 // format string
154 // NOTE: this points somewhere in the string given to the Parse() function -
155 // it's task of the caller ensure that memory is still valid !
156 const CharType *m_pArgEnd;
157
158 // a little buffer where formatting flags like #+\.hlqLz are stored by Parse()
159 // for use in Process()
160 char m_szFlags[wxMAX_SVNPRINTF_FLAGBUFFER_LEN];
161
162
163public:
164
165 // we don't declare this as a constructor otherwise it would be called
166 // automatically and we don't want this: to be optimized, wxCRT_VsnprintfW
167 // calls this function only on really-used instances of this class.
168 void Init();
169
170 // Parses the first conversion specifier in the given string, which must
171 // begin with a '%'. Returns false if the first '%' does not introduce a
172 // (valid) conversion specifier and thus should be ignored.
173 bool Parse(const CharType *format);
174
175 // Process this conversion specifier and puts the result in the given
176 // buffer. Returns the number of characters written in 'buf' or -1 if
177 // there's not enough space.
178 int Process(CharType *buf, size_t lenMax, wxPrintfArg *p, size_t written);
179
180 // Loads the argument of this conversion specifier from given va_list.
181 bool LoadArg(wxPrintfArg *p, va_list &argptr);
182
183private:
184 // An helper function of LoadArg() which is used to handle the '*' flag
185 void ReplaceAsteriskWith(int w);
186};
187
188template<typename CharType>
189void wxPrintfConvSpec<CharType>::Init()
190{
191 m_nMinWidth = 0;
192 m_nMaxWidth = 0xFFFF;
193 m_pos = 0;
194 m_bAlignLeft = false;
195 m_pArgPos = m_pArgEnd = NULL;
196 m_type = wxPAT_INVALID;
197
198 memset(m_szFlags, 0, sizeof(m_szFlags));
199 // this character will never be removed from m_szFlags array and
200 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
201 m_szFlags[0] = '%';
202}
203
204template<typename CharType>
205bool wxPrintfConvSpec<CharType>::Parse(const CharType *format)
206{
207 bool done = false;
208
209 // temporary parse data
210 size_t flagofs = 1;
211 bool in_prec, // true if we found the dot in some previous iteration
212 prec_dot; // true if the dot has been already added to m_szFlags
213 int ilen = 0;
214
215 m_bAlignLeft = in_prec = prec_dot = false;
216 m_pArgPos = m_pArgEnd = format;
217 do
218 {
219#define CHECK_PREC \
220 if (in_prec && !prec_dot) \
221 { \
222 m_szFlags[flagofs++] = '.'; \
223 prec_dot = true; \
224 }
225
226 // what follows '%'?
227 const CharType ch = *(++m_pArgEnd);
228 switch ( ch )
229 {
230 case wxT('\0'):
231 return false; // not really an argument
232
233 case wxT('%'):
234 return false; // not really an argument
235
236 case wxT('#'):
237 case wxT('0'):
238 case wxT(' '):
239 case wxT('+'):
240 case wxT('\''):
241 CHECK_PREC
242 m_szFlags[flagofs++] = char(ch);
243 break;
244
245 case wxT('-'):
246 CHECK_PREC
247 m_bAlignLeft = true;
248 m_szFlags[flagofs++] = char(ch);
249 break;
250
251 case wxT('.'):
252 // don't use CHECK_PREC here to avoid warning about the value
253 // assigned to prec_dot inside it being never used (because
254 // overwritten just below) from Borland in release build
255 if (in_prec && !prec_dot)
256 m_szFlags[flagofs++] = '.';
257 in_prec = true;
258 prec_dot = false;
259 m_nMaxWidth = 0;
260 // dot will be auto-added to m_szFlags if non-negative
261 // number follows
262 break;
263
264 case wxT('h'):
265 ilen = -1;
266 CHECK_PREC
267 m_szFlags[flagofs++] = char(ch);
268 break;
269
270 case wxT('l'):
271 // NB: it's safe to use flagofs-1 as flagofs always start from 1
272 if (m_szFlags[flagofs-1] == 'l') // 'll' modifier is the same as 'L' or 'q'
273 ilen = 2;
274 else
275 ilen = 1;
276 CHECK_PREC
277 m_szFlags[flagofs++] = char(ch);
278 break;
279
280 case wxT('q'):
281 case wxT('L'):
282 ilen = 2;
283 CHECK_PREC
284 m_szFlags[flagofs++] = char(ch);
285 break;
286#ifdef __WINDOWS__
287 // under Windows we support the special '%I64' notation as longlong
288 // integer conversion specifier for MSVC compatibility
289 // (it behaves exactly as '%lli' or '%Li' or '%qi')
290 case wxT('I'):
291 if (*(m_pArgEnd+1) == wxT('6') &&
292 *(m_pArgEnd+2) == wxT('4'))
293 {
294 m_pArgEnd++;
295 m_pArgEnd++;
296
297 ilen = 2;
298 CHECK_PREC
299 m_szFlags[flagofs++] = char(ch);
300 m_szFlags[flagofs++] = '6';
301 m_szFlags[flagofs++] = '4';
302 break;
303 }
304 // else: fall-through, 'I' is MSVC equivalent of C99 'z'
305#endif // __WINDOWS__
306
307 case wxT('z'):
308 case wxT('Z'):
309 // 'z' is C99 standard for size_t and ptrdiff_t, 'Z' was used
310 // for this purpose in libc5 and by wx <= 2.8
311 ilen = 3;
312 CHECK_PREC
313 m_szFlags[flagofs++] = char(ch);
314 break;
315
316 case wxT('*'):
317 if (in_prec)
318 {
319 CHECK_PREC
320
321 // tell Process() to use the next argument
322 // in the stack as maxwidth...
323 m_nMaxWidth = -1;
324 }
325 else
326 {
327 // tell Process() to use the next argument
328 // in the stack as minwidth...
329 m_nMinWidth = -1;
330 }
331
332 // save the * in our formatting buffer...
333 // will be replaced later by Process()
334 m_szFlags[flagofs++] = char(ch);
335 break;
336
337 case wxT('1'): case wxT('2'): case wxT('3'):
338 case wxT('4'): case wxT('5'): case wxT('6'):
339 case wxT('7'): case wxT('8'): case wxT('9'):
340 {
341 int len = 0;
342 CHECK_PREC
343 while ( (*m_pArgEnd >= CharType('0')) &&
344 (*m_pArgEnd <= CharType('9')) )
345 {
346 m_szFlags[flagofs++] = char(*m_pArgEnd);
347 len = len*10 + (*m_pArgEnd - wxT('0'));
348 m_pArgEnd++;
349 }
350
351 if (in_prec)
352 m_nMaxWidth = len;
353 else
354 m_nMinWidth = len;
355
356 m_pArgEnd--; // the main loop pre-increments n again
357 }
358 break;
359
360 case wxT('$'): // a positional parameter (e.g. %2$s) ?
361 {
362 if (m_nMinWidth <= 0)
363 break; // ignore this formatting flag as no
364 // numbers are preceding it
365
366 // remove from m_szFlags all digits previously added
367 do {
368 flagofs--;
369 } while (m_szFlags[flagofs] >= '1' &&
370 m_szFlags[flagofs] <= '9');
371
372 // re-adjust the offset making it point to the
373 // next free char of m_szFlags
374 flagofs++;
375
376 m_pos = m_nMinWidth;
377 m_nMinWidth = 0;
378 }
379 break;
380
381 case wxT('d'):
382 case wxT('i'):
383 case wxT('o'):
384 case wxT('u'):
385 case wxT('x'):
386 case wxT('X'):
387 CHECK_PREC
388 m_szFlags[flagofs++] = char(ch);
389 if (ilen == 0)
390 m_type = wxPAT_INT;
391 else if (ilen == -1)
392 // NB: 'short int' value passed through '...'
393 // is promoted to 'int', so we have to get
394 // an int from stack even if we need a short
395 m_type = wxPAT_INT;
396 else if (ilen == 1)
397 m_type = wxPAT_LONGINT;
398 else if (ilen == 2)
399#ifdef wxLongLong_t
400 m_type = wxPAT_LONGLONGINT;
401#else // !wxLongLong_t
402 m_type = wxPAT_LONGINT;
403#endif // wxLongLong_t/!wxLongLong_t
404 else if (ilen == 3)
405 m_type = wxPAT_SIZET;
406 done = true;
407 break;
408
409 case wxT('e'):
410 case wxT('E'):
411 case wxT('f'):
412 case wxT('g'):
413 case wxT('G'):
414 CHECK_PREC
415 m_szFlags[flagofs++] = char(ch);
416 if (ilen == 2)
417 m_type = wxPAT_LONGDOUBLE;
418 else
419 m_type = wxPAT_DOUBLE;
420 done = true;
421 break;
422
423 case wxT('p'):
424 m_type = wxPAT_POINTER;
425 m_szFlags[flagofs++] = char(ch);
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, wxT("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_STAR:
609 // this will be handled as part of the next argument
610 return true;
611
612 case wxPAT_INVALID:
613 default:
614 return false;
615 }
616
617 return true; // loading was successful
618}
619
620template<typename CharType>
621int wxPrintfConvSpec<CharType>::Process(CharType *buf, size_t lenMax, wxPrintfArg *p, size_t written)
622{
623 // buffer to avoid dynamic memory allocation each time for small strings;
624 // note that this buffer is used only to hold results of number formatting,
625 // %s directly writes user's string in buf, without using szScratch
626 char szScratch[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN];
627 size_t lenScratch = 0, lenCur = 0;
628
629#define APPEND_CH(ch) \
630 { \
631 if ( lenCur == lenMax ) \
632 return -1; \
633 \
634 buf[lenCur++] = ch; \
635 }
636
637 switch ( m_type )
638 {
639 case wxPAT_INT:
640 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_int);
641 break;
642
643 case wxPAT_LONGINT:
644 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longint);
645 break;
646
647#ifdef wxLongLong_t
648 case wxPAT_LONGLONGINT:
649 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longlongint);
650 break;
651#endif // SIZEOF_LONG_LONG
652
653 case wxPAT_SIZET:
654 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_sizet);
655 break;
656
657 case wxPAT_LONGDOUBLE:
658 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longdouble);
659 break;
660
661 case wxPAT_DOUBLE:
662 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_double);
663 break;
664
665 case wxPAT_POINTER:
666 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_pointer);
667 break;
668
669 case wxPAT_CHAR:
670 case wxPAT_WCHAR:
671 {
672 wxUniChar ch;
673 if (m_type == wxPAT_CHAR)
674 ch = p->pad_char;
675 else // m_type == wxPAT_WCHAR
676 ch = p->pad_wchar;
677
678 CharType val = ch;
679
680 size_t i;
681
682 if (!m_bAlignLeft)
683 for (i = 1; i < (size_t)m_nMinWidth; i++)
684 APPEND_CH(wxT(' '));
685
686 APPEND_CH(val);
687
688 if (m_bAlignLeft)
689 for (i = 1; i < (size_t)m_nMinWidth; i++)
690 APPEND_CH(wxT(' '));
691 }
692 break;
693
694 case wxPAT_PCHAR:
695 case wxPAT_PWCHAR:
696 {
697 wxString s;
698 if ( !p->pad_str )
699 {
700 if ( m_nMaxWidth >= 6 )
701 s = wxT("(null)");
702 }
703 else if (m_type == wxPAT_PCHAR)
704 s.assign(static_cast<const char *>(p->pad_str));
705 else // m_type == wxPAT_PWCHAR
706 s.assign(static_cast<const wchar_t *>(p->pad_str));
707
708 typename wxPrintfStringHelper<CharType>::ConvertedType strbuf(
709 wxPrintfStringHelper<CharType>::Convert(s));
710
711 // at this point we are sure that m_nMaxWidth is positive or
712 // null (see top of wxPrintfConvSpec::LoadArg)
713 int len = wxMin((unsigned int)m_nMaxWidth, wxStrlen(strbuf));
714
715 int i;
716
717 if (!m_bAlignLeft)
718 {
719 for (i = len; i < m_nMinWidth; i++)
720 APPEND_CH(wxT(' '));
721 }
722
723 len = wxMin((unsigned int)len, lenMax-lenCur);
724 wxStrncpy(buf+lenCur, strbuf, len);
725 lenCur += len;
726
727 if (m_bAlignLeft)
728 {
729 for (i = len; i < m_nMinWidth; i++)
730 APPEND_CH(wxT(' '));
731 }
732 }
733 break;
734
735 case wxPAT_NINT:
736 *p->pad_nint = written;
737 break;
738
739 case wxPAT_NSHORTINT:
740 *p->pad_nshortint = (short int)written;
741 break;
742
743 case wxPAT_NLONGINT:
744 *p->pad_nlongint = written;
745 break;
746
747 case wxPAT_INVALID:
748 default:
749 return -1;
750 }
751
752 // if we used system's sprintf() then we now need to append the s_szScratch
753 // buffer to the given one...
754 switch (m_type)
755 {
756 case wxPAT_INT:
757 case wxPAT_LONGINT:
758#ifdef wxLongLong_t
759 case wxPAT_LONGLONGINT:
760#endif
761 case wxPAT_SIZET:
762 case wxPAT_LONGDOUBLE:
763 case wxPAT_DOUBLE:
764 case wxPAT_POINTER:
765 wxASSERT(lenScratch < wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN);
766 // NB: 1) we can compare lenMax (for CharType*, i.e. possibly
767 // wchar_t*) with lenScratch (char*) because this code is
768 // formatting integers and that will have the same length
769 // even in UTF-8 (the only case when char* length may be
770 // more than wchar_t* length of the same string)
771 // 2) wxStrncpy converts the 2nd argument to 1st argument's
772 // type transparently if their types differ, so this code
773 // works for both instantiations
774 if (lenMax < lenScratch)
775 {
776 // fill output buffer and then return -1
777 wxStrncpy(buf, szScratch, lenMax);
778 return -1;
779 }
780 wxStrncpy(buf, szScratch, lenScratch);
781 lenCur += lenScratch;
782 break;
783
784 default:
785 break; // all other cases were completed previously
786 }
787
788 return lenCur;
789}
790
791
792// helper that parses format string
793template<typename CharType>
794struct wxPrintfConvSpecParser
795{
796 typedef wxPrintfConvSpec<CharType> ConvSpec;
797
798 wxPrintfConvSpecParser(const CharType *fmt)
799 {
800 nargs = 0;
801 posarg_present =
802 nonposarg_present = false;
803
804 memset(pspec, 0, sizeof(pspec));
805
806 // parse the format string
807 for ( const CharType *toparse = fmt; *toparse != wxT('\0'); toparse++ )
808 {
809 // skip everything except format specifications
810 if ( *toparse != '%' )
811 continue;
812
813 // also skip escaped percent signs
814 if ( toparse[1] == '%' )
815 {
816 toparse++;
817 continue;
818 }
819
820 ConvSpec *spec = &specs[nargs];
821 spec->Init();
822
823 // attempt to parse this format specification
824 if ( !spec->Parse(toparse) )
825 continue;
826
827 // advance to the end of this specifier
828 toparse = spec->m_pArgEnd;
829
830 // special handling for specifications including asterisks: we need
831 // to reserve an extra slot (or two if asterisks were used for both
832 // width and precision) in specs array in this case
833 if ( const char *f = strchr(spec->m_szFlags, '*') )
834 {
835 unsigned numAsterisks = 1;
836 if ( strchr(++f, '*') )
837 numAsterisks++;
838
839 for ( unsigned n = 0; n < numAsterisks; n++ )
840 {
841 if ( nargs++ == wxMAX_SVNPRINTF_ARGUMENTS )
842 break;
843
844 // TODO: we need to support specifiers of the form "%2$*1$s"
845 // (this is the same as "%*s") as if any positional arguments
846 // are used all asterisks must be positional as well but this
847 // requires a lot of changes in this code (basically we'd need
848 // to rewrite Parse() to return "*" and conversion itself as
849 // separate entries)
850 if ( posarg_present )
851 {
852 wxFAIL_MSG
853 (
854 wxString::Format
855 (
856 "Format string \"%s\" uses both positional "
857 "parameters and '*' but this is not currently "
858 "supported by this implementation, sorry.",
859 fmt
860 )
861 );
862 }
863
864 specs[nargs] = *spec;
865
866 // make an entry for '*' and point to it from pspec
867 spec->Init();
868 spec->m_type = wxPAT_STAR;
869 pspec[nargs - 1] = spec;
870
871 spec = &specs[nargs];
872 }
873 }
874
875
876 // check if this is a positional or normal argument
877 if ( spec->m_pos > 0 )
878 {
879 // the positional arguments start from number 1 so we need
880 // to adjust the index
881 spec->m_pos--;
882 posarg_present = true;
883 }
884 else // not a positional argument...
885 {
886 spec->m_pos = nargs;
887 nonposarg_present = true;
888 }
889
890 // this conversion specifier is tied to the pos-th argument...
891 pspec[spec->m_pos] = spec;
892
893 if ( nargs++ == wxMAX_SVNPRINTF_ARGUMENTS )
894 break;
895 }
896
897
898 // warn if we lost any arguments (the program probably will crash
899 // anyhow because of stack corruption...)
900 if ( nargs == wxMAX_SVNPRINTF_ARGUMENTS )
901 {
902 wxFAIL_MSG
903 (
904 wxString::Format
905 (
906 "wxVsnprintf() currently supports only %d arguments, "
907 "but format string \"%s\" defines more of them.\n"
908 "You need to change wxMAX_SVNPRINTF_ARGUMENTS and "
909 "recompile if more are really needed.",
910 fmt, wxMAX_SVNPRINTF_ARGUMENTS
911 )
912 );
913 }
914 }
915
916 // total number of valid elements in specs
917 unsigned nargs;
918
919 // all format specifications in this format string in order of their
920 // appearance (which may be different from arguments order)
921 ConvSpec specs[wxMAX_SVNPRINTF_ARGUMENTS];
922
923 // pointer to specs array element for the N-th argument
924 ConvSpec *pspec[wxMAX_SVNPRINTF_ARGUMENTS];
925
926 // true if any positional/non-positional parameters are used
927 bool posarg_present,
928 nonposarg_present;
929};
930
931#undef APPEND_CH
932#undef CHECK_PREC
933
934#endif // _WX_PRIVATE_WXPRINTF_H_