]> git.saurik.com Git - wxWidgets.git/blame - include/wx/private/wxprintf.h
No real changes, just cleanup week days handling in wxGenericCalendarCtrl.
[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
75ac34ce
VZ
59enum wxPrintfArgType
60{
47346406
VS
61 wxPAT_INVALID = -1,
62
63 wxPAT_INT, // %d, %i, %o, %u, %x, %X
64 wxPAT_LONGINT, // %ld, etc
65#ifdef wxLongLong_t
66 wxPAT_LONGLONGINT, // %Ld, etc
67#endif
b113edcd 68 wxPAT_SIZET, // %zd, etc
47346406
VS
69
70 wxPAT_DOUBLE, // %e, %E, %f, %g, %G
71 wxPAT_LONGDOUBLE, // %le, etc
72
73 wxPAT_POINTER, // %p
74
75 wxPAT_CHAR, // %hc (in ANSI mode: %c, too)
76 wxPAT_WCHAR, // %lc (in Unicode mode: %c, too)
77
78 wxPAT_PCHAR, // %s (related to a char *)
79 wxPAT_PWCHAR, // %s (related to a wchar_t *)
80
81 wxPAT_NINT, // %n
82 wxPAT_NSHORTINT, // %hn
75ac34ce
VZ
83 wxPAT_NLONGINT, // %ln
84
85 wxPAT_STAR // '*' used for width or precision
47346406
VS
86};
87
88// an argument passed to wxCRT_VsnprintfW
75ac34ce
VZ
89union wxPrintfArg
90{
47346406
VS
91 int pad_int; // %d, %i, %o, %u, %x, %X
92 long int pad_longint; // %ld, etc
93#ifdef wxLongLong_t
75ac34ce 94 wxLongLong_t pad_longlongint; // %Ld, etc
47346406 95#endif
b113edcd 96 size_t pad_sizet; // %zd, etc
47346406
VS
97
98 double pad_double; // %e, %E, %f, %g, %G
99 long double pad_longdouble; // %le, etc
100
101 void *pad_pointer; // %p
102
103 char pad_char; // %hc (in ANSI mode: %c, too)
104 wchar_t pad_wchar; // %lc (in Unicode mode: %c, too)
105
106 void *pad_str; // %s
107
108 int *pad_nint; // %n
109 short int *pad_nshortint; // %hn
110 long int *pad_nlongint; // %ln
75ac34ce 111};
47346406 112
75ac34ce 113// helper for converting string into either char* or wchar_t* depending
47346406
VS
114// on the type of wxPrintfConvSpec<T> instantiation:
115template<typename CharType> struct wxPrintfStringHelper {};
116
117template<> struct wxPrintfStringHelper<char>
118{
119 typedef const wxWX2MBbuf ConvertedType;
120 static ConvertedType Convert(const wxString& s) { return s.mb_str(); }
121};
122
123template<> struct wxPrintfStringHelper<wchar_t>
124{
125 typedef const wxWX2WCbuf ConvertedType;
126 static ConvertedType Convert(const wxString& s) { return s.wc_str(); }
127};
128
129
130// Contains parsed data relative to a conversion specifier given to
131// wxCRT_VsnprintfW and parsed from the format string
132// NOTE: in C++ there is almost no difference between struct & classes thus
133// there is no performance gain by using a struct here...
134template<typename CharType>
135class wxPrintfConvSpec
136{
137public:
138
139 // the position of the argument relative to this conversion specifier
140 size_t m_pos;
141
142 // the type of this conversion specifier
143 wxPrintfArgType m_type;
144
145 // the minimum and maximum width
146 // when one of this var is set to -1 it means: use the following argument
147 // in the stack as minimum/maximum width for this conversion specifier
148 int m_nMinWidth, m_nMaxWidth;
149
150 // does the argument need to the be aligned to left ?
151 bool m_bAlignLeft;
152
153 // pointer to the '%' of this conversion specifier in the 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_pArgPos;
157
158 // pointer to the last character of this conversion specifier in the
159 // format string
160 // NOTE: this points somewhere in the string given to the Parse() function -
161 // it's task of the caller ensure that memory is still valid !
162 const CharType *m_pArgEnd;
163
b113edcd 164 // a little buffer where formatting flags like #+\.hlqLz are stored by Parse()
47346406 165 // for use in Process()
47346406
VS
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
060ec116 204 memset(m_szFlags, 0, sizeof(m_szFlags));
47346406
VS
205 // this character will never be removed from m_szFlags array and
206 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
207 m_szFlags[0] = '%';
208}
209
210template<typename CharType>
211bool wxPrintfConvSpec<CharType>::Parse(const CharType *format)
212{
213 bool done = false;
214
215 // temporary parse data
216 size_t flagofs = 1;
217 bool in_prec, // true if we found the dot in some previous iteration
218 prec_dot; // true if the dot has been already added to m_szFlags
219 int ilen = 0;
220
221 m_bAlignLeft = in_prec = prec_dot = false;
222 m_pArgPos = m_pArgEnd = format;
223 do
224 {
225#define CHECK_PREC \
226 if (in_prec && !prec_dot) \
227 { \
228 m_szFlags[flagofs++] = '.'; \
229 prec_dot = true; \
230 }
231
232 // what follows '%'?
233 const CharType ch = *(++m_pArgEnd);
234 switch ( ch )
235 {
236 case wxT('\0'):
237 return false; // not really an argument
238
239 case wxT('%'):
240 return false; // not really an argument
241
242 case wxT('#'):
243 case wxT('0'):
244 case wxT(' '):
245 case wxT('+'):
246 case wxT('\''):
247 CHECK_PREC
248 m_szFlags[flagofs++] = char(ch);
249 break;
250
251 case wxT('-'):
252 CHECK_PREC
253 m_bAlignLeft = true;
254 m_szFlags[flagofs++] = char(ch);
255 break;
256
257 case wxT('.'):
e822d1bd
VZ
258 // don't use CHECK_PREC here to avoid warning about the value
259 // assigned to prec_dot inside it being never used (because
260 // overwritten just below) from Borland in release build
261 if (in_prec && !prec_dot)
262 m_szFlags[flagofs++] = '.';
47346406
VS
263 in_prec = true;
264 prec_dot = false;
265 m_nMaxWidth = 0;
266 // dot will be auto-added to m_szFlags if non-negative
267 // number follows
268 break;
269
270 case wxT('h'):
271 ilen = -1;
272 CHECK_PREC
273 m_szFlags[flagofs++] = char(ch);
274 break;
275
276 case wxT('l'):
277 // NB: it's safe to use flagofs-1 as flagofs always start from 1
278 if (m_szFlags[flagofs-1] == 'l') // 'll' modifier is the same as 'L' or 'q'
279 ilen = 2;
280 else
281 ilen = 1;
282 CHECK_PREC
283 m_szFlags[flagofs++] = char(ch);
284 break;
285
286 case wxT('q'):
287 case wxT('L'):
288 ilen = 2;
289 CHECK_PREC
290 m_szFlags[flagofs++] = char(ch);
291 break;
292#ifdef __WXMSW__
293 // under Windows we support the special '%I64' notation as longlong
294 // integer conversion specifier for MSVC compatibility
295 // (it behaves exactly as '%lli' or '%Li' or '%qi')
296 case wxT('I'):
b113edcd
VS
297 if (*(m_pArgEnd+1) == wxT('6') &&
298 *(m_pArgEnd+2) == wxT('4'))
299 {
300 m_pArgEnd++;
301 m_pArgEnd++;
47346406 302
b113edcd
VS
303 ilen = 2;
304 CHECK_PREC
305 m_szFlags[flagofs++] = char(ch);
306 m_szFlags[flagofs++] = '6';
307 m_szFlags[flagofs++] = '4';
308 break;
309 }
310 // else: fall-through, 'I' is MSVC equivalent of C99 'z'
47346406
VS
311#endif // __WXMSW__
312
b113edcd 313 case wxT('z'):
47346406 314 case wxT('Z'):
b113edcd
VS
315 // 'z' is C99 standard for size_t and ptrdiff_t, 'Z' was used
316 // for this purpose in libc5 and by wx <= 2.8
47346406
VS
317 ilen = 3;
318 CHECK_PREC
319 m_szFlags[flagofs++] = char(ch);
320 break;
321
322 case wxT('*'):
323 if (in_prec)
324 {
325 CHECK_PREC
326
327 // tell Process() to use the next argument
328 // in the stack as maxwidth...
329 m_nMaxWidth = -1;
330 }
331 else
332 {
333 // tell Process() to use the next argument
334 // in the stack as minwidth...
335 m_nMinWidth = -1;
336 }
337
338 // save the * in our formatting buffer...
339 // will be replaced later by Process()
340 m_szFlags[flagofs++] = char(ch);
341 break;
342
343 case wxT('1'): case wxT('2'): case wxT('3'):
344 case wxT('4'): case wxT('5'): case wxT('6'):
345 case wxT('7'): case wxT('8'): case wxT('9'):
346 {
347 int len = 0;
348 CHECK_PREC
349 while ( (*m_pArgEnd >= CharType('0')) &&
350 (*m_pArgEnd <= CharType('9')) )
351 {
352 m_szFlags[flagofs++] = char(*m_pArgEnd);
353 len = len*10 + (*m_pArgEnd - wxT('0'));
354 m_pArgEnd++;
355 }
356
357 if (in_prec)
358 m_nMaxWidth = len;
359 else
360 m_nMinWidth = len;
361
362 m_pArgEnd--; // the main loop pre-increments n again
363 }
364 break;
365
366 case wxT('$'): // a positional parameter (e.g. %2$s) ?
367 {
368 if (m_nMinWidth <= 0)
369 break; // ignore this formatting flag as no
370 // numbers are preceding it
371
372 // remove from m_szFlags all digits previously added
373 do {
374 flagofs--;
375 } while (m_szFlags[flagofs] >= '1' &&
376 m_szFlags[flagofs] <= '9');
377
378 // re-adjust the offset making it point to the
379 // next free char of m_szFlags
380 flagofs++;
381
382 m_pos = m_nMinWidth;
383 m_nMinWidth = 0;
384 }
385 break;
386
387 case wxT('d'):
388 case wxT('i'):
389 case wxT('o'):
390 case wxT('u'):
391 case wxT('x'):
392 case wxT('X'):
393 CHECK_PREC
394 m_szFlags[flagofs++] = char(ch);
47346406
VS
395 if (ilen == 0)
396 m_type = wxPAT_INT;
397 else if (ilen == -1)
398 // NB: 'short int' value passed through '...'
399 // is promoted to 'int', so we have to get
400 // an int from stack even if we need a short
401 m_type = wxPAT_INT;
402 else if (ilen == 1)
403 m_type = wxPAT_LONGINT;
404 else if (ilen == 2)
405#ifdef wxLongLong_t
406 m_type = wxPAT_LONGLONGINT;
407#else // !wxLongLong_t
408 m_type = wxPAT_LONGINT;
409#endif // wxLongLong_t/!wxLongLong_t
410 else if (ilen == 3)
411 m_type = wxPAT_SIZET;
412 done = true;
413 break;
414
415 case wxT('e'):
416 case wxT('E'):
417 case wxT('f'):
418 case wxT('g'):
419 case wxT('G'):
420 CHECK_PREC
421 m_szFlags[flagofs++] = char(ch);
47346406
VS
422 if (ilen == 2)
423 m_type = wxPAT_LONGDOUBLE;
424 else
425 m_type = wxPAT_DOUBLE;
426 done = true;
427 break;
428
429 case wxT('p'):
430 m_type = wxPAT_POINTER;
431 m_szFlags[flagofs++] = char(ch);
47346406
VS
432 done = true;
433 break;
434
435 case wxT('c'):
436 if (ilen == -1)
437 {
438 // in Unicode mode %hc == ANSI character
439 // and in ANSI mode, %hc == %c == ANSI...
440 m_type = wxPAT_CHAR;
441 }
442 else if (ilen == 1)
443 {
444 // in ANSI mode %lc == Unicode character
445 // and in Unicode mode, %lc == %c == Unicode...
446 m_type = wxPAT_WCHAR;
447 }
448 else
449 {
450#if wxUSE_UNICODE
451 // in Unicode mode, %c == Unicode character
452 m_type = wxPAT_WCHAR;
453#else
454 // in ANSI mode, %c == ANSI character
455 m_type = wxPAT_CHAR;
456#endif
457 }
458 done = true;
459 break;
460
461 case wxT('s'):
462 if (ilen == -1)
463 {
464 // Unicode mode wx extension: we'll let %hs mean non-Unicode
465 // strings (when in ANSI mode, %s == %hs == ANSI string)
466 m_type = wxPAT_PCHAR;
467 }
468 else if (ilen == 1)
469 {
470 // in Unicode mode, %ls == %s == Unicode string
471 // in ANSI mode, %ls == Unicode string
472 m_type = wxPAT_PWCHAR;
473 }
474 else
475 {
476#if wxUSE_UNICODE
477 m_type = wxPAT_PWCHAR;
478#else
479 m_type = wxPAT_PCHAR;
480#endif
481 }
482 done = true;
483 break;
484
485 case wxT('n'):
486 if (ilen == 0)
487 m_type = wxPAT_NINT;
488 else if (ilen == -1)
489 m_type = wxPAT_NSHORTINT;
490 else if (ilen >= 1)
491 m_type = wxPAT_NLONGINT;
492 done = true;
493 break;
494
495 default:
496 // bad format, don't consider this an argument;
497 // leave it unchanged
498 return false;
499 }
500
501 if (flagofs == wxMAX_SVNPRINTF_FLAGBUFFER_LEN)
502 {
503 wxLogDebug(wxT("Too many flags specified for a single conversion specifier!"));
504 return false;
505 }
506 }
507 while (!done);
508
509 return true; // parsing was successful
510}
511
512template<typename CharType>
513void wxPrintfConvSpec<CharType>::ReplaceAsteriskWith(int width)
514{
515 char temp[wxMAX_SVNPRINTF_FLAGBUFFER_LEN];
516
517 // find the first * in our flag buffer
518 char *pwidth = strchr(m_szFlags, '*');
9a83f860 519 wxCHECK_RET(pwidth, wxT("field width must be specified"));
47346406
VS
520
521 // save what follows the * (the +1 is to skip the asterisk itself!)
522 strcpy(temp, pwidth+1);
523 if (width < 0)
524 {
525 pwidth[0] = wxT('-');
526 pwidth++;
527 }
528
529 // replace * with the actual integer given as width
530#ifndef SYSTEM_SPRINTF_IS_UNSAFE
531 int maxlen = (m_szFlags + wxMAX_SVNPRINTF_FLAGBUFFER_LEN - pwidth) /
532 sizeof(*m_szFlags);
533#endif
534 int offset = system_sprintf(pwidth, maxlen, "%d", abs(width));
535
536 // restore after the expanded * what was following it
537 strcpy(pwidth+offset, temp);
538}
539
540template<typename CharType>
541bool wxPrintfConvSpec<CharType>::LoadArg(wxPrintfArg *p, va_list &argptr)
542{
543 // did the '*' width/precision specifier was used ?
544 if (m_nMaxWidth == -1)
545 {
546 // take the maxwidth specifier from the stack
547 m_nMaxWidth = va_arg(argptr, int);
548 if (m_nMaxWidth < 0)
549 m_nMaxWidth = 0;
550 else
551 ReplaceAsteriskWith(m_nMaxWidth);
552 }
553
554 if (m_nMinWidth == -1)
555 {
556 // take the minwidth specifier from the stack
557 m_nMinWidth = va_arg(argptr, int);
558
559 ReplaceAsteriskWith(m_nMinWidth);
560 if (m_nMinWidth < 0)
561 {
562 m_bAlignLeft = !m_bAlignLeft;
563 m_nMinWidth = -m_nMinWidth;
564 }
565 }
566
567 switch (m_type) {
568 case wxPAT_INT:
569 p->pad_int = va_arg(argptr, int);
570 break;
571 case wxPAT_LONGINT:
572 p->pad_longint = va_arg(argptr, long int);
573 break;
574#ifdef wxLongLong_t
575 case wxPAT_LONGLONGINT:
576 p->pad_longlongint = va_arg(argptr, wxLongLong_t);
577 break;
578#endif // wxLongLong_t
579 case wxPAT_SIZET:
580 p->pad_sizet = va_arg(argptr, size_t);
581 break;
582 case wxPAT_DOUBLE:
583 p->pad_double = va_arg(argptr, double);
584 break;
585 case wxPAT_LONGDOUBLE:
586 p->pad_longdouble = va_arg(argptr, long double);
587 break;
588 case wxPAT_POINTER:
589 p->pad_pointer = va_arg(argptr, void *);
590 break;
591
592 case wxPAT_CHAR:
593 p->pad_char = (char)va_arg(argptr, int); // char is promoted to int when passed through '...'
594 break;
595 case wxPAT_WCHAR:
596 p->pad_wchar = (wchar_t)va_arg(argptr, int); // char is promoted to int when passed through '...'
597 break;
598
599 case wxPAT_PCHAR:
600 case wxPAT_PWCHAR:
601 p->pad_str = va_arg(argptr, void *);
602 break;
603
604 case wxPAT_NINT:
605 p->pad_nint = va_arg(argptr, int *);
606 break;
607 case wxPAT_NSHORTINT:
608 p->pad_nshortint = va_arg(argptr, short int *);
609 break;
610 case wxPAT_NLONGINT:
611 p->pad_nlongint = va_arg(argptr, long int *);
612 break;
613
75ac34ce
VZ
614 case wxPAT_STAR:
615 // this will be handled as part of the next argument
616 return true;
617
47346406
VS
618 case wxPAT_INVALID:
619 default:
620 return false;
621 }
622
623 return true; // loading was successful
624}
625
626template<typename CharType>
627int wxPrintfConvSpec<CharType>::Process(CharType *buf, size_t lenMax, wxPrintfArg *p, size_t written)
628{
629 // buffer to avoid dynamic memory allocation each time for small strings;
630 // note that this buffer is used only to hold results of number formatting,
631 // %s directly writes user's string in buf, without using szScratch
632 char szScratch[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN];
633 size_t lenScratch = 0, lenCur = 0;
634
635#define APPEND_CH(ch) \
636 { \
637 if ( lenCur == lenMax ) \
638 return -1; \
639 \
640 buf[lenCur++] = ch; \
641 }
642
643 switch ( m_type )
644 {
645 case wxPAT_INT:
646 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_int);
647 break;
648
649 case wxPAT_LONGINT:
650 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longint);
651 break;
652
653#ifdef wxLongLong_t
654 case wxPAT_LONGLONGINT:
655 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longlongint);
656 break;
657#endif // SIZEOF_LONG_LONG
658
659 case wxPAT_SIZET:
660 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_sizet);
661 break;
662
663 case wxPAT_LONGDOUBLE:
664 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longdouble);
665 break;
666
667 case wxPAT_DOUBLE:
668 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_double);
669 break;
670
671 case wxPAT_POINTER:
672 lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_pointer);
673 break;
674
675 case wxPAT_CHAR:
676 case wxPAT_WCHAR:
677 {
678 wxUniChar ch;
679 if (m_type == wxPAT_CHAR)
680 ch = p->pad_char;
681 else // m_type == wxPAT_WCHAR
682 ch = p->pad_wchar;
683
684 CharType val = ch;
685
686 size_t i;
687
688 if (!m_bAlignLeft)
689 for (i = 1; i < (size_t)m_nMinWidth; i++)
9a83f860 690 APPEND_CH(wxT(' '));
47346406
VS
691
692 APPEND_CH(val);
693
694 if (m_bAlignLeft)
695 for (i = 1; i < (size_t)m_nMinWidth; i++)
9a83f860 696 APPEND_CH(wxT(' '));
47346406
VS
697 }
698 break;
699
700 case wxPAT_PCHAR:
701 case wxPAT_PWCHAR:
702 {
37513b67
VZ
703 wxString s;
704 if ( !p->pad_str )
705 {
706 if ( m_nMaxWidth >= 6 )
707 s = wxT("(null)");
708 }
709 else if (m_type == wxPAT_PCHAR)
710 s.assign(static_cast<const char *>(p->pad_str));
711 else // m_type == wxPAT_PWCHAR
712 s.assign(static_cast<const wchar_t *>(p->pad_str));
47346406
VS
713
714 typename wxPrintfStringHelper<CharType>::ConvertedType strbuf(
715 wxPrintfStringHelper<CharType>::Convert(s));
716
717 // at this point we are sure that m_nMaxWidth is positive or
718 // null (see top of wxPrintfConvSpec::LoadArg)
719 int len = wxMin((unsigned int)m_nMaxWidth, wxStrlen(strbuf));
720
721 int i;
722
723 if (!m_bAlignLeft)
724 {
725 for (i = len; i < m_nMinWidth; i++)
9a83f860 726 APPEND_CH(wxT(' '));
47346406
VS
727 }
728
729 len = wxMin((unsigned int)len, lenMax-lenCur);
730 wxStrncpy(buf+lenCur, strbuf, len);
731 lenCur += len;
732
733 if (m_bAlignLeft)
734 {
735 for (i = len; i < m_nMinWidth; i++)
9a83f860 736 APPEND_CH(wxT(' '));
47346406
VS
737 }
738 }
739 break;
740
741 case wxPAT_NINT:
742 *p->pad_nint = written;
743 break;
744
745 case wxPAT_NSHORTINT:
746 *p->pad_nshortint = (short int)written;
747 break;
748
749 case wxPAT_NLONGINT:
750 *p->pad_nlongint = written;
751 break;
752
753 case wxPAT_INVALID:
754 default:
755 return -1;
756 }
757
758 // if we used system's sprintf() then we now need to append the s_szScratch
759 // buffer to the given one...
760 switch (m_type)
761 {
762 case wxPAT_INT:
763 case wxPAT_LONGINT:
764#ifdef wxLongLong_t
765 case wxPAT_LONGLONGINT:
766#endif
767 case wxPAT_SIZET:
768 case wxPAT_LONGDOUBLE:
769 case wxPAT_DOUBLE:
770 case wxPAT_POINTER:
771 wxASSERT(lenScratch < wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN);
772 // NB: 1) we can compare lenMax (for CharType*, i.e. possibly
773 // wchar_t*) with lenScratch (char*) because this code is
774 // formatting integers and that will have the same length
775 // even in UTF-8 (the only case when char* length may be
776 // more than wchar_t* length of the same string)
777 // 2) wxStrncpy converts the 2nd argument to 1st argument's
778 // type transparently if their types differ, so this code
779 // works for both instantiations
780 if (lenMax < lenScratch)
781 {
782 // fill output buffer and then return -1
783 wxStrncpy(buf, szScratch, lenMax);
784 return -1;
785 }
786 wxStrncpy(buf, szScratch, lenScratch);
787 lenCur += lenScratch;
788 break;
789
790 default:
791 break; // all other cases were completed previously
792 }
793
794 return lenCur;
795}
796
797
798// helper that parses format string
799template<typename CharType>
800struct wxPrintfConvSpecParser
801{
75ac34ce
VZ
802 typedef wxPrintfConvSpec<CharType> ConvSpec;
803
804 wxPrintfConvSpecParser(const CharType *fmt)
47346406 805 {
f20e2689
VZ
806 nargs = 0;
807 posarg_present =
808 nonposarg_present = false;
809
47346406
VS
810 memset(pspec, 0, sizeof(pspec));
811
47346406 812 // parse the format string
75ac34ce 813 for ( const CharType *toparse = fmt; *toparse != wxT('\0'); toparse++ )
47346406 814 {
75ac34ce
VZ
815 // skip everything except format specifications
816 if ( *toparse != '%' )
817 continue;
818
819 // also skip escaped percent signs
820 if ( toparse[1] == '%' )
47346406 821 {
75ac34ce
VZ
822 toparse++;
823 continue;
824 }
47346406 825
75ac34ce
VZ
826 ConvSpec *spec = &specs[nargs];
827 spec->Init();
47346406 828
75ac34ce
VZ
829 // attempt to parse this format specification
830 if ( !spec->Parse(toparse) )
831 continue;
47346406 832
75ac34ce
VZ
833 // advance to the end of this specifier
834 toparse = spec->m_pArgEnd;
47346406 835
75ac34ce
VZ
836 // special handling for specifications including asterisks: we need
837 // to reserve an extra slot (or two if asterisks were used for both
838 // width and precision) in specs array in this case
e2dd6247 839 if ( const char *f = strchr(spec->m_szFlags, '*') )
75ac34ce 840 {
e2dd6247
VZ
841 unsigned numAsterisks = 1;
842 if ( strchr(++f, '*') )
843 numAsterisks++;
75ac34ce 844
e2dd6247 845 for ( unsigned n = 0; n < numAsterisks; n++ )
47346406 846 {
e2dd6247
VZ
847 if ( nargs++ == wxMAX_SVNPRINTF_ARGUMENTS )
848 break;
849
850 // TODO: we need to support specifiers of the form "%2$*1$s"
851 // (this is the same as "%*s") as if any positional arguments
852 // are used all asterisks must be positional as well but this
853 // requires a lot of changes in this code (basically we'd need
854 // to rewrite Parse() to return "*" and conversion itself as
855 // separate entries)
856 if ( posarg_present )
857 {
858 wxFAIL_MSG
75ac34ce 859 (
e2dd6247
VZ
860 wxString::Format
861 (
862 "Format string \"%s\" uses both positional "
863 "parameters and '*' but this is not currently "
864 "supported by this implementation, sorry.",
865 fmt
866 )
867 );
868 }
75ac34ce 869
e2dd6247 870 specs[nargs] = *spec;
75ac34ce 871
e2dd6247
VZ
872 // make an entry for '*' and point to it from pspec
873 spec->Init();
874 spec->m_type = wxPAT_STAR;
875 pspec[nargs - 1] = spec;
75ac34ce 876
e2dd6247
VZ
877 spec = &specs[nargs];
878 }
75ac34ce
VZ
879 }
880
e2dd6247 881
75ac34ce
VZ
882 // check if this is a positional or normal argument
883 if ( spec->m_pos > 0 )
884 {
885 // the positional arguments start from number 1 so we need
886 // to adjust the index
887 spec->m_pos--;
888 posarg_present = true;
889 }
890 else // not a positional argument...
891 {
892 spec->m_pos = nargs;
893 nonposarg_present = true;
47346406 894 }
75ac34ce
VZ
895
896 // this conversion specifier is tied to the pos-th argument...
897 pspec[spec->m_pos] = spec;
898
899 if ( nargs++ == wxMAX_SVNPRINTF_ARGUMENTS )
900 break;
901 }
902
903
904 // warn if we lost any arguments (the program probably will crash
905 // anyhow because of stack corruption...)
906 if ( nargs == wxMAX_SVNPRINTF_ARGUMENTS )
907 {
908 wxFAIL_MSG
909 (
910 wxString::Format
911 (
912 "wxVsnprintf() currently supports only %d arguments, "
913 "but format string \"%s\" defines more of them.\n"
914 "You need to change wxMAX_SVNPRINTF_ARGUMENTS and "
915 "recompile if more are really needed.",
916 fmt, wxMAX_SVNPRINTF_ARGUMENTS
917 )
918 );
47346406
VS
919 }
920 }
921
75ac34ce 922 // total number of valid elements in specs
47346406 923 unsigned nargs;
75ac34ce
VZ
924
925 // all format specifications in this format string in order of their
926 // appearance (which may be different from arguments order)
927 ConvSpec specs[wxMAX_SVNPRINTF_ARGUMENTS];
928
929 // pointer to specs array element for the N-th argument
930 ConvSpec *pspec[wxMAX_SVNPRINTF_ARGUMENTS];
931
932 // true if any positional/non-positional parameters are used
933 bool posarg_present,
934 nonposarg_present;
47346406
VS
935};
936
937#undef APPEND_CH
938#undef CHECK_PREC
939
940#endif // _WX_PRIVATE_WXPRINTF_H_