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 // ---------------------------------------------------------------------------
25 #if defined(__MWERKS__) && __MSL__ >= 0x6000
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)
45 #define SYSTEM_SPRINTF_IS_UNSAFE
48 // ---------------------------------------------------------------------------
49 // printf format string parsing
50 // ---------------------------------------------------------------------------
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
58 // the conversion specifiers accepted by wxCRT_VsnprintfW
63 wxPAT_INT
, // %d, %i, %o, %u, %x, %X
64 wxPAT_LONGINT
, // %ld, etc
66 wxPAT_LONGLONGINT
, // %Ld, etc
68 wxPAT_SIZET
, // %Zd, etc
70 wxPAT_DOUBLE
, // %e, %E, %f, %g, %G
71 wxPAT_LONGDOUBLE
, // %le, etc
75 wxPAT_CHAR
, // %hc (in ANSI mode: %c, too)
76 wxPAT_WCHAR
, // %lc (in Unicode mode: %c, too)
78 wxPAT_PCHAR
, // %s (related to a char *)
79 wxPAT_PWCHAR
, // %s (related to a wchar_t *)
82 wxPAT_NSHORTINT
, // %hn
83 wxPAT_NLONGINT
, // %ln
85 wxPAT_STAR
// '*' used for width or precision
88 // an argument passed to wxCRT_VsnprintfW
91 int pad_int
; // %d, %i, %o, %u, %x, %X
92 long int pad_longint
; // %ld, etc
94 wxLongLong_t pad_longlongint
; // %Ld, etc
96 size_t pad_sizet
; // %Zd, etc
98 double pad_double
; // %e, %E, %f, %g, %G
99 long double pad_longdouble
; // %le, etc
101 void *pad_pointer
; // %p
103 char pad_char
; // %hc (in ANSI mode: %c, too)
104 wchar_t pad_wchar
; // %lc (in Unicode mode: %c, too)
109 short int *pad_nshortint
; // %hn
110 long int *pad_nlongint
; // %ln
113 // helper for converting string into either char* or wchar_t* depending
114 // on the type of wxPrintfConvSpec<T> instantiation:
115 template<typename CharType
> struct wxPrintfStringHelper
{};
117 template<> struct wxPrintfStringHelper
<char>
119 typedef const wxWX2MBbuf ConvertedType
;
120 static ConvertedType
Convert(const wxString
& s
) { return s
.mb_str(); }
123 template<> struct wxPrintfStringHelper
<wchar_t>
125 typedef const wxWX2WCbuf ConvertedType
;
126 static ConvertedType
Convert(const wxString
& s
) { return s
.wc_str(); }
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...
134 template<typename CharType
>
135 class wxPrintfConvSpec
139 // the position of the argument relative to this conversion specifier
142 // the type of this conversion specifier
143 wxPrintfArgType m_type
;
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
;
150 // does the argument need to the be aligned to left ?
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
;
158 // pointer to the last character of this conversion specifier in the
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
;
164 // a little buffer where formatting flags like #+\.hlqLZ are stored by Parse()
165 // for use in Process()
166 char m_szFlags
[wxMAX_SVNPRINTF_FLAGBUFFER_LEN
];
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.
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
);
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
);
186 // Loads the argument of this conversion specifier from given va_list.
187 bool LoadArg(wxPrintfArg
*p
, va_list &argptr
);
190 // An helper function of LoadArg() which is used to handle the '*' flag
191 void ReplaceAsteriskWith(int w
);
194 template<typename CharType
>
195 void wxPrintfConvSpec
<CharType
>::Init()
198 m_nMaxWidth
= 0xFFFF;
200 m_bAlignLeft
= false;
201 m_pArgPos
= m_pArgEnd
= NULL
;
202 m_type
= wxPAT_INVALID
;
204 // this character will never be removed from m_szFlags array and
205 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
209 template<typename CharType
>
210 bool wxPrintfConvSpec
<CharType
>::Parse(const CharType
*format
)
214 // temporary parse data
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
220 m_bAlignLeft
= in_prec
= prec_dot
= false;
221 m_pArgPos
= m_pArgEnd
= format
;
225 if (in_prec && !prec_dot) \
227 m_szFlags[flagofs++] = '.'; \
232 const CharType ch
= *(++m_pArgEnd
);
236 return false; // not really an argument
239 return false; // not really an argument
247 m_szFlags
[flagofs
++] = char(ch
);
253 m_szFlags
[flagofs
++] = char(ch
);
257 // don't use CHECK_PREC here to avoid warning about the value
258 // assigned to prec_dot inside it being never used (because
259 // overwritten just below) from Borland in release build
260 if (in_prec
&& !prec_dot
)
261 m_szFlags
[flagofs
++] = '.';
265 // dot will be auto-added to m_szFlags if non-negative
272 m_szFlags
[flagofs
++] = char(ch
);
276 // NB: it's safe to use flagofs-1 as flagofs always start from 1
277 if (m_szFlags
[flagofs
-1] == 'l') // 'll' modifier is the same as 'L' or 'q'
282 m_szFlags
[flagofs
++] = char(ch
);
289 m_szFlags
[flagofs
++] = char(ch
);
292 // under Windows we support the special '%I64' notation as longlong
293 // integer conversion specifier for MSVC compatibility
294 // (it behaves exactly as '%lli' or '%Li' or '%qi')
296 if (*(m_pArgEnd
+1) != wxT('6') ||
297 *(m_pArgEnd
+2) != wxT('4'))
298 return false; // bad format
305 m_szFlags
[flagofs
++] = char(ch
);
306 m_szFlags
[flagofs
++] = '6';
307 m_szFlags
[flagofs
++] = '4';
314 m_szFlags
[flagofs
++] = char(ch
);
322 // tell Process() to use the next argument
323 // in the stack as maxwidth...
328 // tell Process() to use the next argument
329 // in the stack as minwidth...
333 // save the * in our formatting buffer...
334 // will be replaced later by Process()
335 m_szFlags
[flagofs
++] = char(ch
);
338 case wxT('1'): case wxT('2'): case wxT('3'):
339 case wxT('4'): case wxT('5'): case wxT('6'):
340 case wxT('7'): case wxT('8'): case wxT('9'):
344 while ( (*m_pArgEnd
>= CharType('0')) &&
345 (*m_pArgEnd
<= CharType('9')) )
347 m_szFlags
[flagofs
++] = char(*m_pArgEnd
);
348 len
= len
*10 + (*m_pArgEnd
- wxT('0'));
357 m_pArgEnd
--; // the main loop pre-increments n again
361 case wxT('$'): // a positional parameter (e.g. %2$s) ?
363 if (m_nMinWidth
<= 0)
364 break; // ignore this formatting flag as no
365 // numbers are preceding it
367 // remove from m_szFlags all digits previously added
370 } while (m_szFlags
[flagofs
] >= '1' &&
371 m_szFlags
[flagofs
] <= '9');
373 // re-adjust the offset making it point to the
374 // next free char of m_szFlags
389 m_szFlags
[flagofs
++] = char(ch
);
390 m_szFlags
[flagofs
] = '\0';
394 // NB: 'short int' value passed through '...'
395 // is promoted to 'int', so we have to get
396 // an int from stack even if we need a short
399 m_type
= wxPAT_LONGINT
;
402 m_type
= wxPAT_LONGLONGINT
;
403 #else // !wxLongLong_t
404 m_type
= wxPAT_LONGINT
;
405 #endif // wxLongLong_t/!wxLongLong_t
407 m_type
= wxPAT_SIZET
;
417 m_szFlags
[flagofs
++] = char(ch
);
418 m_szFlags
[flagofs
] = '\0';
420 m_type
= wxPAT_LONGDOUBLE
;
422 m_type
= wxPAT_DOUBLE
;
427 m_type
= wxPAT_POINTER
;
428 m_szFlags
[flagofs
++] = char(ch
);
429 m_szFlags
[flagofs
] = '\0';
436 // in Unicode mode %hc == ANSI character
437 // and in ANSI mode, %hc == %c == ANSI...
442 // in ANSI mode %lc == Unicode character
443 // and in Unicode mode, %lc == %c == Unicode...
444 m_type
= wxPAT_WCHAR
;
449 // in Unicode mode, %c == Unicode character
450 m_type
= wxPAT_WCHAR
;
452 // in ANSI mode, %c == ANSI character
462 // Unicode mode wx extension: we'll let %hs mean non-Unicode
463 // strings (when in ANSI mode, %s == %hs == ANSI string)
464 m_type
= wxPAT_PCHAR
;
468 // in Unicode mode, %ls == %s == Unicode string
469 // in ANSI mode, %ls == Unicode string
470 m_type
= wxPAT_PWCHAR
;
475 m_type
= wxPAT_PWCHAR
;
477 m_type
= wxPAT_PCHAR
;
487 m_type
= wxPAT_NSHORTINT
;
489 m_type
= wxPAT_NLONGINT
;
494 // bad format, don't consider this an argument;
495 // leave it unchanged
499 if (flagofs
== wxMAX_SVNPRINTF_FLAGBUFFER_LEN
)
501 wxLogDebug(wxT("Too many flags specified for a single conversion specifier!"));
507 return true; // parsing was successful
510 template<typename CharType
>
511 void wxPrintfConvSpec
<CharType
>::ReplaceAsteriskWith(int width
)
513 char temp
[wxMAX_SVNPRINTF_FLAGBUFFER_LEN
];
515 // find the first * in our flag buffer
516 char *pwidth
= strchr(m_szFlags
, '*');
517 wxCHECK_RET(pwidth
, wxT("field width must be specified"));
519 // save what follows the * (the +1 is to skip the asterisk itself!)
520 strcpy(temp
, pwidth
+1);
523 pwidth
[0] = wxT('-');
527 // replace * with the actual integer given as width
528 #ifndef SYSTEM_SPRINTF_IS_UNSAFE
529 int maxlen
= (m_szFlags
+ wxMAX_SVNPRINTF_FLAGBUFFER_LEN
- pwidth
) /
532 int offset
= system_sprintf(pwidth
, maxlen
, "%d", abs(width
));
534 // restore after the expanded * what was following it
535 strcpy(pwidth
+offset
, temp
);
538 template<typename CharType
>
539 bool wxPrintfConvSpec
<CharType
>::LoadArg(wxPrintfArg
*p
, va_list &argptr
)
541 // did the '*' width/precision specifier was used ?
542 if (m_nMaxWidth
== -1)
544 // take the maxwidth specifier from the stack
545 m_nMaxWidth
= va_arg(argptr
, int);
549 ReplaceAsteriskWith(m_nMaxWidth
);
552 if (m_nMinWidth
== -1)
554 // take the minwidth specifier from the stack
555 m_nMinWidth
= va_arg(argptr
, int);
557 ReplaceAsteriskWith(m_nMinWidth
);
560 m_bAlignLeft
= !m_bAlignLeft
;
561 m_nMinWidth
= -m_nMinWidth
;
567 p
->pad_int
= va_arg(argptr
, int);
570 p
->pad_longint
= va_arg(argptr
, long int);
573 case wxPAT_LONGLONGINT
:
574 p
->pad_longlongint
= va_arg(argptr
, wxLongLong_t
);
576 #endif // wxLongLong_t
578 p
->pad_sizet
= va_arg(argptr
, size_t);
581 p
->pad_double
= va_arg(argptr
, double);
583 case wxPAT_LONGDOUBLE
:
584 p
->pad_longdouble
= va_arg(argptr
, long double);
587 p
->pad_pointer
= va_arg(argptr
, void *);
591 p
->pad_char
= (char)va_arg(argptr
, int); // char is promoted to int when passed through '...'
594 p
->pad_wchar
= (wchar_t)va_arg(argptr
, int); // char is promoted to int when passed through '...'
599 p
->pad_str
= va_arg(argptr
, void *);
603 p
->pad_nint
= va_arg(argptr
, int *);
605 case wxPAT_NSHORTINT
:
606 p
->pad_nshortint
= va_arg(argptr
, short int *);
609 p
->pad_nlongint
= va_arg(argptr
, long int *);
613 // this will be handled as part of the next argument
621 return true; // loading was successful
624 template<typename CharType
>
625 int wxPrintfConvSpec
<CharType
>::Process(CharType
*buf
, size_t lenMax
, wxPrintfArg
*p
, size_t written
)
627 // buffer to avoid dynamic memory allocation each time for small strings;
628 // note that this buffer is used only to hold results of number formatting,
629 // %s directly writes user's string in buf, without using szScratch
630 char szScratch
[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
];
631 size_t lenScratch
= 0, lenCur
= 0;
633 #define APPEND_CH(ch) \
635 if ( lenCur == lenMax ) \
638 buf[lenCur++] = ch; \
644 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_int
);
648 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longint
);
652 case wxPAT_LONGLONGINT
:
653 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longlongint
);
655 #endif // SIZEOF_LONG_LONG
658 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_sizet
);
661 case wxPAT_LONGDOUBLE
:
662 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longdouble
);
666 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_double
);
670 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_pointer
);
677 if (m_type
== wxPAT_CHAR
)
679 else // m_type == wxPAT_WCHAR
687 for (i
= 1; i
< (size_t)m_nMinWidth
; i
++)
693 for (i
= 1; i
< (size_t)m_nMinWidth
; i
++)
701 wxArgNormalizedString
arg(p
->pad_str
);
704 if ( !arg
.IsValid() && m_nMaxWidth
>= 6 )
707 typename wxPrintfStringHelper
<CharType
>::ConvertedType
strbuf(
708 wxPrintfStringHelper
<CharType
>::Convert(s
));
710 // at this point we are sure that m_nMaxWidth is positive or
711 // null (see top of wxPrintfConvSpec::LoadArg)
712 int len
= wxMin((unsigned int)m_nMaxWidth
, wxStrlen(strbuf
));
718 for (i
= len
; i
< m_nMinWidth
; i
++)
722 len
= wxMin((unsigned int)len
, lenMax
-lenCur
);
723 wxStrncpy(buf
+lenCur
, strbuf
, len
);
728 for (i
= len
; i
< m_nMinWidth
; i
++)
735 *p
->pad_nint
= written
;
738 case wxPAT_NSHORTINT
:
739 *p
->pad_nshortint
= (short int)written
;
743 *p
->pad_nlongint
= written
;
751 // if we used system's sprintf() then we now need to append the s_szScratch
752 // buffer to the given one...
758 case wxPAT_LONGLONGINT
:
761 case wxPAT_LONGDOUBLE
:
764 wxASSERT(lenScratch
< wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
);
765 // NB: 1) we can compare lenMax (for CharType*, i.e. possibly
766 // wchar_t*) with lenScratch (char*) because this code is
767 // formatting integers and that will have the same length
768 // even in UTF-8 (the only case when char* length may be
769 // more than wchar_t* length of the same string)
770 // 2) wxStrncpy converts the 2nd argument to 1st argument's
771 // type transparently if their types differ, so this code
772 // works for both instantiations
773 if (lenMax
< lenScratch
)
775 // fill output buffer and then return -1
776 wxStrncpy(buf
, szScratch
, lenMax
);
779 wxStrncpy(buf
, szScratch
, lenScratch
);
780 lenCur
+= lenScratch
;
784 break; // all other cases were completed previously
791 // helper that parses format string
792 template<typename CharType
>
793 struct wxPrintfConvSpecParser
795 typedef wxPrintfConvSpec
<CharType
> ConvSpec
;
797 wxPrintfConvSpecParser(const CharType
*fmt
)
801 nonposarg_present
= false;
803 memset(pspec
, 0, sizeof(pspec
));
805 // parse the format string
806 for ( const CharType
*toparse
= fmt
; *toparse
!= wxT('\0'); toparse
++ )
808 // skip everything except format specifications
809 if ( *toparse
!= '%' )
812 // also skip escaped percent signs
813 if ( toparse
[1] == '%' )
819 ConvSpec
*spec
= &specs
[nargs
];
822 // attempt to parse this format specification
823 if ( !spec
->Parse(toparse
) )
826 // advance to the end of this specifier
827 toparse
= spec
->m_pArgEnd
;
829 // special handling for specifications including asterisks: we need
830 // to reserve an extra slot (or two if asterisks were used for both
831 // width and precision) in specs array in this case
832 for ( const char *f
= strchr(spec
->m_szFlags
, '*');
834 f
= strchr(f
+ 1, '*') )
836 if ( nargs
++ == wxMAX_SVNPRINTF_ARGUMENTS
)
839 // TODO: we need to support specifiers of the form "%2$*1$s"
840 // (this is the same as "%*s") as if any positional arguments
841 // are used all asterisks must be positional as well but this
842 // requires a lot of changes in this code (basically we'd need
843 // to rewrite Parse() to return "*" and conversion itself as
845 if ( posarg_present
)
851 "Format string \"%s\" uses both positional "
852 "parameters and '*' but this is not currently "
853 "supported by this implementation, sorry.",
859 specs
[nargs
] = *spec
;
861 // make an entry for '*' and point to it from pspec
863 spec
->m_type
= wxPAT_STAR
;
864 pspec
[nargs
- 1] = spec
;
866 spec
= &specs
[nargs
];
869 // check if this is a positional or normal argument
870 if ( spec
->m_pos
> 0 )
872 // the positional arguments start from number 1 so we need
873 // to adjust the index
875 posarg_present
= true;
877 else // not a positional argument...
880 nonposarg_present
= true;
883 // this conversion specifier is tied to the pos-th argument...
884 pspec
[spec
->m_pos
] = spec
;
886 if ( nargs
++ == wxMAX_SVNPRINTF_ARGUMENTS
)
891 // warn if we lost any arguments (the program probably will crash
892 // anyhow because of stack corruption...)
893 if ( nargs
== wxMAX_SVNPRINTF_ARGUMENTS
)
899 "wxVsnprintf() currently supports only %d arguments, "
900 "but format string \"%s\" defines more of them.\n"
901 "You need to change wxMAX_SVNPRINTF_ARGUMENTS and "
902 "recompile if more are really needed.",
903 fmt
, wxMAX_SVNPRINTF_ARGUMENTS
909 // total number of valid elements in specs
912 // all format specifications in this format string in order of their
913 // appearance (which may be different from arguments order)
914 ConvSpec specs
[wxMAX_SVNPRINTF_ARGUMENTS
];
916 // pointer to specs array element for the N-th argument
917 ConvSpec
*pspec
[wxMAX_SVNPRINTF_ARGUMENTS
];
919 // true if any positional/non-positional parameters are used
927 #endif // _WX_PRIVATE_WXPRINTF_H_