1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/private/wxprintf.h
3 // Purpose: wxWidgets wxPrintf() implementation
5 // Modified by: Ron Lee, Francesco Montorsi
8 // Copyright: (c) wxWidgets copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_PRIVATE_WXPRINTF_H_
13 #define _WX_PRIVATE_WXPRINTF_H_
15 // ---------------------------------------------------------------------------
17 // ---------------------------------------------------------------------------
23 #if defined(__MWERKS__) && __MSL__ >= 0x6000
28 // prefer snprintf over sprintf
29 #if defined(__VISUALC__) || \
30 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
31 #define system_sprintf(buff, max, flags, data) \
32 ::_snprintf(buff, max, flags, data)
33 #elif defined(HAVE_SNPRINTF)
34 #define system_sprintf(buff, max, flags, data) \
35 ::snprintf(buff, max, flags, data)
36 #else // NB: at least sprintf() should always be available
37 // since 'max' is not used in this case, wxVsnprintf() should always
38 // ensure that 'buff' is big enough for all common needs
39 // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN)
40 #define system_sprintf(buff, max, flags, data) \
41 ::sprintf(buff, flags, data)
43 #define SYSTEM_SPRINTF_IS_UNSAFE
46 // ---------------------------------------------------------------------------
47 // printf format string parsing
48 // ---------------------------------------------------------------------------
50 // some limits of our implementation
51 #define wxMAX_SVNPRINTF_ARGUMENTS 64
52 #define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32
53 #define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512
56 // the conversion specifiers accepted by wxCRT_VsnprintfW
57 enum wxPrintfArgType
{
60 wxPAT_INT
, // %d, %i, %o, %u, %x, %X
61 wxPAT_LONGINT
, // %ld, etc
63 wxPAT_LONGLONGINT
, // %Ld, etc
65 wxPAT_SIZET
, // %Zd, etc
67 wxPAT_DOUBLE
, // %e, %E, %f, %g, %G
68 wxPAT_LONGDOUBLE
, // %le, etc
72 wxPAT_CHAR
, // %hc (in ANSI mode: %c, too)
73 wxPAT_WCHAR
, // %lc (in Unicode mode: %c, too)
75 wxPAT_PCHAR
, // %s (related to a char *)
76 wxPAT_PWCHAR
, // %s (related to a wchar_t *)
79 wxPAT_NSHORTINT
, // %hn
83 // an argument passed to wxCRT_VsnprintfW
85 int pad_int
; // %d, %i, %o, %u, %x, %X
86 long int pad_longint
; // %ld, etc
88 wxLongLong_t pad_longlongint
; // %Ld, etc
90 size_t pad_sizet
; // %Zd, etc
92 double pad_double
; // %e, %E, %f, %g, %G
93 long double pad_longdouble
; // %le, etc
95 void *pad_pointer
; // %p
97 char pad_char
; // %hc (in ANSI mode: %c, too)
98 wchar_t pad_wchar
; // %lc (in Unicode mode: %c, too)
103 short int *pad_nshortint
; // %hn
104 long int *pad_nlongint
; // %ln
107 // helper for converting string into either char* or wchar_t* dependening
108 // on the type of wxPrintfConvSpec<T> instantiation:
109 template<typename CharType
> struct wxPrintfStringHelper
{};
111 template<> struct wxPrintfStringHelper
<char>
113 typedef const wxWX2MBbuf ConvertedType
;
114 static ConvertedType
Convert(const wxString
& s
) { return s
.mb_str(); }
117 template<> struct wxPrintfStringHelper
<wchar_t>
119 typedef const wxWX2WCbuf ConvertedType
;
120 static ConvertedType
Convert(const wxString
& s
) { return s
.wc_str(); }
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...
128 template<typename CharType
>
129 class wxPrintfConvSpec
133 // the position of the argument relative to this conversion specifier
136 // the type of this conversion specifier
137 wxPrintfArgType m_type
;
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
;
144 // does the argument need to the be aligned to left ?
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
;
152 // pointer to the last character of this conversion specifier in the
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
;
158 // a little buffer where formatting flags like #+\.hlqLZ are stored by Parse()
159 // for use in Process()
160 // NB: even if this buffer is used only for numeric conversion specifiers
161 // and thus could be safely declared as a char[] buffer, we want it to
162 // be wchar_t so that in Unicode builds we can avoid to convert its
163 // contents to Unicode chars when copying it in user's buffer.
164 char m_szFlags
[wxMAX_SVNPRINTF_FLAGBUFFER_LEN
];
169 // we don't declare this as a constructor otherwise it would be called
170 // automatically and we don't want this: to be optimized, wxCRT_VsnprintfW
171 // calls this function only on really-used instances of this class.
174 // Parses the first conversion specifier in the given string, which must
175 // begin with a '%'. Returns false if the first '%' does not introduce a
176 // (valid) conversion specifier and thus should be ignored.
177 bool Parse(const CharType
*format
);
179 // Process this conversion specifier and puts the result in the given
180 // buffer. Returns the number of characters written in 'buf' or -1 if
181 // there's not enough space.
182 int Process(CharType
*buf
, size_t lenMax
, wxPrintfArg
*p
, size_t written
);
184 // Loads the argument of this conversion specifier from given va_list.
185 bool LoadArg(wxPrintfArg
*p
, va_list &argptr
);
188 // An helper function of LoadArg() which is used to handle the '*' flag
189 void ReplaceAsteriskWith(int w
);
192 template<typename CharType
>
193 void wxPrintfConvSpec
<CharType
>::Init()
196 m_nMaxWidth
= 0xFFFF;
198 m_bAlignLeft
= false;
199 m_pArgPos
= m_pArgEnd
= NULL
;
200 m_type
= wxPAT_INVALID
;
202 // this character will never be removed from m_szFlags array and
203 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
207 template<typename CharType
>
208 bool wxPrintfConvSpec
<CharType
>::Parse(const CharType
*format
)
212 // temporary parse data
214 bool in_prec
, // true if we found the dot in some previous iteration
215 prec_dot
; // true if the dot has been already added to m_szFlags
218 m_bAlignLeft
= in_prec
= prec_dot
= false;
219 m_pArgPos
= m_pArgEnd
= format
;
223 if (in_prec && !prec_dot) \
225 m_szFlags[flagofs++] = '.'; \
230 const CharType ch
= *(++m_pArgEnd
);
234 return false; // not really an argument
237 return false; // not really an argument
245 m_szFlags
[flagofs
++] = char(ch
);
251 m_szFlags
[flagofs
++] = char(ch
);
259 // dot will be auto-added to m_szFlags if non-negative
266 m_szFlags
[flagofs
++] = char(ch
);
270 // NB: it's safe to use flagofs-1 as flagofs always start from 1
271 if (m_szFlags
[flagofs
-1] == 'l') // 'll' modifier is the same as 'L' or 'q'
276 m_szFlags
[flagofs
++] = char(ch
);
283 m_szFlags
[flagofs
++] = char(ch
);
286 // under Windows we support the special '%I64' notation as longlong
287 // integer conversion specifier for MSVC compatibility
288 // (it behaves exactly as '%lli' or '%Li' or '%qi')
290 if (*(m_pArgEnd
+1) != wxT('6') ||
291 *(m_pArgEnd
+2) != wxT('4'))
292 return false; // bad format
299 m_szFlags
[flagofs
++] = char(ch
);
300 m_szFlags
[flagofs
++] = '6';
301 m_szFlags
[flagofs
++] = '4';
308 m_szFlags
[flagofs
++] = char(ch
);
316 // tell Process() to use the next argument
317 // in the stack as maxwidth...
322 // tell Process() to use the next argument
323 // in the stack as minwidth...
327 // save the * in our formatting buffer...
328 // will be replaced later by Process()
329 m_szFlags
[flagofs
++] = char(ch
);
332 case wxT('1'): case wxT('2'): case wxT('3'):
333 case wxT('4'): case wxT('5'): case wxT('6'):
334 case wxT('7'): case wxT('8'): case wxT('9'):
338 while ( (*m_pArgEnd
>= CharType('0')) &&
339 (*m_pArgEnd
<= CharType('9')) )
341 m_szFlags
[flagofs
++] = char(*m_pArgEnd
);
342 len
= len
*10 + (*m_pArgEnd
- wxT('0'));
351 m_pArgEnd
--; // the main loop pre-increments n again
355 case wxT('$'): // a positional parameter (e.g. %2$s) ?
357 if (m_nMinWidth
<= 0)
358 break; // ignore this formatting flag as no
359 // numbers are preceding it
361 // remove from m_szFlags all digits previously added
364 } while (m_szFlags
[flagofs
] >= '1' &&
365 m_szFlags
[flagofs
] <= '9');
367 // re-adjust the offset making it point to the
368 // next free char of m_szFlags
383 m_szFlags
[flagofs
++] = char(ch
);
384 m_szFlags
[flagofs
] = '\0';
388 // NB: 'short int' value passed through '...'
389 // is promoted to 'int', so we have to get
390 // an int from stack even if we need a short
393 m_type
= wxPAT_LONGINT
;
396 m_type
= wxPAT_LONGLONGINT
;
397 #else // !wxLongLong_t
398 m_type
= wxPAT_LONGINT
;
399 #endif // wxLongLong_t/!wxLongLong_t
401 m_type
= wxPAT_SIZET
;
411 m_szFlags
[flagofs
++] = char(ch
);
412 m_szFlags
[flagofs
] = '\0';
414 m_type
= wxPAT_LONGDOUBLE
;
416 m_type
= wxPAT_DOUBLE
;
421 m_type
= wxPAT_POINTER
;
422 m_szFlags
[flagofs
++] = char(ch
);
423 m_szFlags
[flagofs
] = '\0';
430 // in Unicode mode %hc == ANSI character
431 // and in ANSI mode, %hc == %c == ANSI...
436 // in ANSI mode %lc == Unicode character
437 // and in Unicode mode, %lc == %c == Unicode...
438 m_type
= wxPAT_WCHAR
;
443 // in Unicode mode, %c == Unicode character
444 m_type
= wxPAT_WCHAR
;
446 // in ANSI mode, %c == ANSI character
456 // Unicode mode wx extension: we'll let %hs mean non-Unicode
457 // strings (when in ANSI mode, %s == %hs == ANSI string)
458 m_type
= wxPAT_PCHAR
;
462 // in Unicode mode, %ls == %s == Unicode string
463 // in ANSI mode, %ls == Unicode string
464 m_type
= wxPAT_PWCHAR
;
469 m_type
= wxPAT_PWCHAR
;
471 m_type
= wxPAT_PCHAR
;
481 m_type
= wxPAT_NSHORTINT
;
483 m_type
= wxPAT_NLONGINT
;
488 // bad format, don't consider this an argument;
489 // leave it unchanged
493 if (flagofs
== wxMAX_SVNPRINTF_FLAGBUFFER_LEN
)
495 wxLogDebug(wxT("Too many flags specified for a single conversion specifier!"));
501 return true; // parsing was successful
504 template<typename CharType
>
505 void wxPrintfConvSpec
<CharType
>::ReplaceAsteriskWith(int width
)
507 char temp
[wxMAX_SVNPRINTF_FLAGBUFFER_LEN
];
509 // find the first * in our flag buffer
510 char *pwidth
= strchr(m_szFlags
, '*');
511 wxCHECK_RET(pwidth
, _T("field width must be specified"));
513 // save what follows the * (the +1 is to skip the asterisk itself!)
514 strcpy(temp
, pwidth
+1);
517 pwidth
[0] = wxT('-');
521 // replace * with the actual integer given as width
522 #ifndef SYSTEM_SPRINTF_IS_UNSAFE
523 int maxlen
= (m_szFlags
+ wxMAX_SVNPRINTF_FLAGBUFFER_LEN
- pwidth
) /
526 int offset
= system_sprintf(pwidth
, maxlen
, "%d", abs(width
));
528 // restore after the expanded * what was following it
529 strcpy(pwidth
+offset
, temp
);
532 template<typename CharType
>
533 bool wxPrintfConvSpec
<CharType
>::LoadArg(wxPrintfArg
*p
, va_list &argptr
)
535 // did the '*' width/precision specifier was used ?
536 if (m_nMaxWidth
== -1)
538 // take the maxwidth specifier from the stack
539 m_nMaxWidth
= va_arg(argptr
, int);
543 ReplaceAsteriskWith(m_nMaxWidth
);
546 if (m_nMinWidth
== -1)
548 // take the minwidth specifier from the stack
549 m_nMinWidth
= va_arg(argptr
, int);
551 ReplaceAsteriskWith(m_nMinWidth
);
554 m_bAlignLeft
= !m_bAlignLeft
;
555 m_nMinWidth
= -m_nMinWidth
;
561 p
->pad_int
= va_arg(argptr
, int);
564 p
->pad_longint
= va_arg(argptr
, long int);
567 case wxPAT_LONGLONGINT
:
568 p
->pad_longlongint
= va_arg(argptr
, wxLongLong_t
);
570 #endif // wxLongLong_t
572 p
->pad_sizet
= va_arg(argptr
, size_t);
575 p
->pad_double
= va_arg(argptr
, double);
577 case wxPAT_LONGDOUBLE
:
578 p
->pad_longdouble
= va_arg(argptr
, long double);
581 p
->pad_pointer
= va_arg(argptr
, void *);
585 p
->pad_char
= (char)va_arg(argptr
, int); // char is promoted to int when passed through '...'
588 p
->pad_wchar
= (wchar_t)va_arg(argptr
, int); // char is promoted to int when passed through '...'
593 p
->pad_str
= va_arg(argptr
, void *);
597 p
->pad_nint
= va_arg(argptr
, int *);
599 case wxPAT_NSHORTINT
:
600 p
->pad_nshortint
= va_arg(argptr
, short int *);
603 p
->pad_nlongint
= va_arg(argptr
, long int *);
611 return true; // loading was successful
614 template<typename CharType
>
615 int wxPrintfConvSpec
<CharType
>::Process(CharType
*buf
, size_t lenMax
, wxPrintfArg
*p
, size_t written
)
617 // buffer to avoid dynamic memory allocation each time for small strings;
618 // note that this buffer is used only to hold results of number formatting,
619 // %s directly writes user's string in buf, without using szScratch
620 char szScratch
[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
];
621 size_t lenScratch
= 0, lenCur
= 0;
623 #define APPEND_CH(ch) \
625 if ( lenCur == lenMax ) \
628 buf[lenCur++] = ch; \
634 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_int
);
638 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longint
);
642 case wxPAT_LONGLONGINT
:
643 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longlongint
);
645 #endif // SIZEOF_LONG_LONG
648 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_sizet
);
651 case wxPAT_LONGDOUBLE
:
652 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longdouble
);
656 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_double
);
660 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_pointer
);
667 if (m_type
== wxPAT_CHAR
)
669 else // m_type == wxPAT_WCHAR
677 for (i
= 1; i
< (size_t)m_nMinWidth
; i
++)
683 for (i
= 1; i
< (size_t)m_nMinWidth
; i
++)
691 wxArgNormalizedString
arg(p
->pad_str
);
694 if ( !arg
.IsValid() && m_nMaxWidth
>= 6 )
697 typename wxPrintfStringHelper
<CharType
>::ConvertedType
strbuf(
698 wxPrintfStringHelper
<CharType
>::Convert(s
));
700 // at this point we are sure that m_nMaxWidth is positive or
701 // null (see top of wxPrintfConvSpec::LoadArg)
702 int len
= wxMin((unsigned int)m_nMaxWidth
, wxStrlen(strbuf
));
708 for (i
= len
; i
< m_nMinWidth
; i
++)
712 len
= wxMin((unsigned int)len
, lenMax
-lenCur
);
713 wxStrncpy(buf
+lenCur
, strbuf
, len
);
718 for (i
= len
; i
< m_nMinWidth
; i
++)
725 *p
->pad_nint
= written
;
728 case wxPAT_NSHORTINT
:
729 *p
->pad_nshortint
= (short int)written
;
733 *p
->pad_nlongint
= written
;
741 // if we used system's sprintf() then we now need to append the s_szScratch
742 // buffer to the given one...
748 case wxPAT_LONGLONGINT
:
751 case wxPAT_LONGDOUBLE
:
754 wxASSERT(lenScratch
< wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
);
755 // NB: 1) we can compare lenMax (for CharType*, i.e. possibly
756 // wchar_t*) with lenScratch (char*) because this code is
757 // formatting integers and that will have the same length
758 // even in UTF-8 (the only case when char* length may be
759 // more than wchar_t* length of the same string)
760 // 2) wxStrncpy converts the 2nd argument to 1st argument's
761 // type transparently if their types differ, so this code
762 // works for both instantiations
763 if (lenMax
< lenScratch
)
765 // fill output buffer and then return -1
766 wxStrncpy(buf
, szScratch
, lenMax
);
769 wxStrncpy(buf
, szScratch
, lenScratch
);
770 lenCur
+= lenScratch
;
774 break; // all other cases were completed previously
781 // helper that parses format string
782 template<typename CharType
>
783 struct wxPrintfConvSpecParser
785 wxPrintfConvSpecParser(const CharType
*format
)
786 : posarg_present(false), nonposarg_present(false),
789 memset(pspec
, 0, sizeof(pspec
));
791 const CharType
*toparse
= format
;
793 // parse the format string
794 for (; *toparse
!= wxT('\0'); toparse
++)
796 if (*toparse
== wxT('%') )
800 // let's see if this is a (valid) conversion specifier...
801 if (arg
[nargs
].Parse(toparse
))
804 wxPrintfConvSpec
<CharType
> *current
= &arg
[nargs
];
806 // make toparse point to the end of this specifier
807 toparse
= current
->m_pArgEnd
;
809 if (current
->m_pos
> 0)
811 // the positionals start from number 1... adjust the index
813 posarg_present
= true;
817 // not a positional argument...
818 current
->m_pos
= nargs
;
819 nonposarg_present
= true;
822 // this conversion specifier is tied to the pos-th argument...
823 pspec
[current
->m_pos
] = current
;
826 if (nargs
== wxMAX_SVNPRINTF_ARGUMENTS
)
828 wxLogDebug(wxT("A single call to wxVsnprintf() has more than %d arguments; ")
829 wxT("ignoring all remaining arguments."), wxMAX_SVNPRINTF_ARGUMENTS
);
830 break; // cannot handle any additional conv spec
835 // it's safe to look in the next character of toparse as at
836 // worst we'll hit its \0
837 if (*(toparse
+1) == wxT('%'))
839 // the Parse() returned false because we've found a %%
847 wxPrintfConvSpec
<CharType
> arg
[wxMAX_SVNPRINTF_ARGUMENTS
];
848 wxPrintfConvSpec
<CharType
> *pspec
[wxMAX_SVNPRINTF_ARGUMENTS
];
849 bool posarg_present
, nonposarg_present
;
856 #endif // _WX_PRIVATE_WXPRINTF_H_