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 // prefer snprintf over sprintf
26 #if defined(__VISUALC__) || \
27 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
28 #define system_sprintf(buff, max, flags, data) \
29 ::_snprintf(buff, max, flags, data)
30 #elif defined(HAVE_SNPRINTF)
31 #define system_sprintf(buff, max, flags, data) \
32 ::snprintf(buff, max, flags, data)
33 #else // NB: at least sprintf() should always be available
34 // since 'max' is not used in this case, wxVsnprintf() should always
35 // ensure that 'buff' is big enough for all common needs
36 // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN)
37 #define system_sprintf(buff, max, flags, data) \
38 ::sprintf(buff, flags, data)
40 #define SYSTEM_SPRINTF_IS_UNSAFE
43 // ---------------------------------------------------------------------------
44 // printf format string parsing
45 // ---------------------------------------------------------------------------
47 // some limits of our implementation
48 #define wxMAX_SVNPRINTF_ARGUMENTS 64
49 #define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32
50 #define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512
53 // the conversion specifiers accepted by wxCRT_VsnprintfW
58 wxPAT_INT
, // %d, %i, %o, %u, %x, %X
59 wxPAT_LONGINT
, // %ld, etc
61 wxPAT_LONGLONGINT
, // %Ld, etc
63 wxPAT_SIZET
, // %zd, etc
65 wxPAT_DOUBLE
, // %e, %E, %f, %g, %G
66 wxPAT_LONGDOUBLE
, // %le, etc
70 wxPAT_CHAR
, // %hc (in ANSI mode: %c, too)
71 wxPAT_WCHAR
, // %lc (in Unicode mode: %c, too)
73 wxPAT_PCHAR
, // %s (related to a char *)
74 wxPAT_PWCHAR
, // %s (related to a wchar_t *)
77 wxPAT_NSHORTINT
, // %hn
78 wxPAT_NLONGINT
, // %ln
80 wxPAT_STAR
// '*' used for width or precision
83 // an argument passed to wxCRT_VsnprintfW
86 int pad_int
; // %d, %i, %o, %u, %x, %X
87 long int pad_longint
; // %ld, etc
89 wxLongLong_t pad_longlongint
; // %Ld, etc
91 size_t pad_sizet
; // %zd, etc
93 double pad_double
; // %e, %E, %f, %g, %G
94 long double pad_longdouble
; // %le, etc
96 void *pad_pointer
; // %p
98 char pad_char
; // %hc (in ANSI mode: %c, too)
99 wchar_t pad_wchar
; // %lc (in Unicode mode: %c, too)
104 short int *pad_nshortint
; // %hn
105 long int *pad_nlongint
; // %ln
108 // helper for converting string into either char* or wchar_t* depending
109 // on the type of wxPrintfConvSpec<T> instantiation:
110 template<typename CharType
> struct wxPrintfStringHelper
{};
112 template<> struct wxPrintfStringHelper
<char>
114 typedef const wxWX2MBbuf ConvertedType
;
115 static ConvertedType
Convert(const wxString
& s
) { return s
.mb_str(); }
118 template<> struct wxPrintfStringHelper
<wchar_t>
120 typedef const wxWX2WCbuf ConvertedType
;
121 static ConvertedType
Convert(const wxString
& s
) { return s
.wc_str(); }
125 // Contains parsed data relative to a conversion specifier given to
126 // wxCRT_VsnprintfW and parsed from the format string
127 // NOTE: in C++ there is almost no difference between struct & classes thus
128 // there is no performance gain by using a struct here...
129 template<typename CharType
>
130 class wxPrintfConvSpec
134 // the position of the argument relative to this conversion specifier
137 // the type of this conversion specifier
138 wxPrintfArgType m_type
;
140 // the minimum and maximum width
141 // when one of this var is set to -1 it means: use the following argument
142 // in the stack as minimum/maximum width for this conversion specifier
143 int m_nMinWidth
, m_nMaxWidth
;
145 // does the argument need to the be aligned to left ?
148 // pointer to the '%' of this conversion specifier in the format string
149 // NOTE: this points somewhere in the string given to the Parse() function -
150 // it's task of the caller ensure that memory is still valid !
151 const CharType
*m_pArgPos
;
153 // pointer to the last character of this conversion specifier in the
155 // NOTE: this points somewhere in the string given to the Parse() function -
156 // it's task of the caller ensure that memory is still valid !
157 const CharType
*m_pArgEnd
;
159 // a little buffer where formatting flags like #+\.hlqLz are stored by Parse()
160 // for use in Process()
161 char m_szFlags
[wxMAX_SVNPRINTF_FLAGBUFFER_LEN
];
166 // we don't declare this as a constructor otherwise it would be called
167 // automatically and we don't want this: to be optimized, wxCRT_VsnprintfW
168 // calls this function only on really-used instances of this class.
171 // Parses the first conversion specifier in the given string, which must
172 // begin with a '%'. Returns false if the first '%' does not introduce a
173 // (valid) conversion specifier and thus should be ignored.
174 bool Parse(const CharType
*format
);
176 // Process this conversion specifier and puts the result in the given
177 // buffer. Returns the number of characters written in 'buf' or -1 if
178 // there's not enough space.
179 int Process(CharType
*buf
, size_t lenMax
, wxPrintfArg
*p
, size_t written
);
181 // Loads the argument of this conversion specifier from given va_list.
182 bool LoadArg(wxPrintfArg
*p
, va_list &argptr
);
185 // An helper function of LoadArg() which is used to handle the '*' flag
186 void ReplaceAsteriskWith(int w
);
189 template<typename CharType
>
190 void wxPrintfConvSpec
<CharType
>::Init()
193 m_nMaxWidth
= 0xFFFF;
195 m_bAlignLeft
= false;
196 m_pArgPos
= m_pArgEnd
= NULL
;
197 m_type
= wxPAT_INVALID
;
199 memset(m_szFlags
, 0, sizeof(m_szFlags
));
200 // this character will never be removed from m_szFlags array and
201 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
205 template<typename CharType
>
206 bool wxPrintfConvSpec
<CharType
>::Parse(const CharType
*format
)
210 // temporary parse data
212 bool in_prec
, // true if we found the dot in some previous iteration
213 prec_dot
; // true if the dot has been already added to m_szFlags
216 m_bAlignLeft
= in_prec
= prec_dot
= false;
217 m_pArgPos
= m_pArgEnd
= format
;
221 if (in_prec && !prec_dot) \
223 m_szFlags[flagofs++] = '.'; \
228 const CharType ch
= *(++m_pArgEnd
);
232 return false; // not really an argument
235 return false; // not really an argument
243 m_szFlags
[flagofs
++] = char(ch
);
249 m_szFlags
[flagofs
++] = char(ch
);
253 // don't use CHECK_PREC here to avoid warning about the value
254 // assigned to prec_dot inside it being never used (because
255 // overwritten just below) from Borland in release build
256 if (in_prec
&& !prec_dot
)
257 m_szFlags
[flagofs
++] = '.';
261 // dot will be auto-added to m_szFlags if non-negative
268 m_szFlags
[flagofs
++] = char(ch
);
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'
278 m_szFlags
[flagofs
++] = char(ch
);
285 m_szFlags
[flagofs
++] = char(ch
);
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')
292 if (*(m_pArgEnd
+1) == wxT('6') &&
293 *(m_pArgEnd
+2) == wxT('4'))
300 m_szFlags
[flagofs
++] = char(ch
);
301 m_szFlags
[flagofs
++] = '6';
302 m_szFlags
[flagofs
++] = '4';
305 // else: fall-through, 'I' is MSVC equivalent of C99 'z'
306 #endif // __WINDOWS__
310 // 'z' is C99 standard for size_t and ptrdiff_t, 'Z' was used
311 // for this purpose in libc5 and by wx <= 2.8
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
);
393 // NB: 'short int' value passed through '...'
394 // is promoted to 'int', so we have to get
395 // an int from stack even if we need a short
398 m_type
= wxPAT_LONGINT
;
401 m_type
= wxPAT_LONGLONGINT
;
402 #else // !wxLongLong_t
403 m_type
= wxPAT_LONGINT
;
404 #endif // wxLongLong_t/!wxLongLong_t
406 m_type
= wxPAT_SIZET
;
416 m_szFlags
[flagofs
++] = char(ch
);
418 m_type
= wxPAT_LONGDOUBLE
;
420 m_type
= wxPAT_DOUBLE
;
425 m_type
= wxPAT_POINTER
;
426 m_szFlags
[flagofs
++] = char(ch
);
433 // in Unicode mode %hc == ANSI character
434 // and in ANSI mode, %hc == %c == ANSI...
439 // in ANSI mode %lc == Unicode character
440 // and in Unicode mode, %lc == %c == Unicode...
441 m_type
= wxPAT_WCHAR
;
446 // in Unicode mode, %c == Unicode character
447 m_type
= wxPAT_WCHAR
;
449 // in ANSI mode, %c == ANSI character
459 // Unicode mode wx extension: we'll let %hs mean non-Unicode
460 // strings (when in ANSI mode, %s == %hs == ANSI string)
461 m_type
= wxPAT_PCHAR
;
465 // in Unicode mode, %ls == %s == Unicode string
466 // in ANSI mode, %ls == Unicode string
467 m_type
= wxPAT_PWCHAR
;
472 m_type
= wxPAT_PWCHAR
;
474 m_type
= wxPAT_PCHAR
;
484 m_type
= wxPAT_NSHORTINT
;
486 m_type
= wxPAT_NLONGINT
;
491 // bad format, don't consider this an argument;
492 // leave it unchanged
496 if (flagofs
== wxMAX_SVNPRINTF_FLAGBUFFER_LEN
)
498 wxLogDebug(wxT("Too many flags specified for a single conversion specifier!"));
504 return true; // parsing was successful
507 template<typename CharType
>
508 void wxPrintfConvSpec
<CharType
>::ReplaceAsteriskWith(int width
)
510 char temp
[wxMAX_SVNPRINTF_FLAGBUFFER_LEN
];
512 // find the first * in our flag buffer
513 char *pwidth
= strchr(m_szFlags
, '*');
514 wxCHECK_RET(pwidth
, wxT("field width must be specified"));
516 // save what follows the * (the +1 is to skip the asterisk itself!)
517 strcpy(temp
, pwidth
+1);
520 pwidth
[0] = wxT('-');
524 // replace * with the actual integer given as width
525 #ifndef SYSTEM_SPRINTF_IS_UNSAFE
526 int maxlen
= (m_szFlags
+ wxMAX_SVNPRINTF_FLAGBUFFER_LEN
- pwidth
) /
529 int offset
= system_sprintf(pwidth
, maxlen
, "%d", abs(width
));
531 // restore after the expanded * what was following it
532 strcpy(pwidth
+offset
, temp
);
535 template<typename CharType
>
536 bool wxPrintfConvSpec
<CharType
>::LoadArg(wxPrintfArg
*p
, va_list &argptr
)
538 // did the '*' width/precision specifier was used ?
539 if (m_nMaxWidth
== -1)
541 // take the maxwidth specifier from the stack
542 m_nMaxWidth
= va_arg(argptr
, int);
546 ReplaceAsteriskWith(m_nMaxWidth
);
549 if (m_nMinWidth
== -1)
551 // take the minwidth specifier from the stack
552 m_nMinWidth
= va_arg(argptr
, int);
554 ReplaceAsteriskWith(m_nMinWidth
);
557 m_bAlignLeft
= !m_bAlignLeft
;
558 m_nMinWidth
= -m_nMinWidth
;
564 p
->pad_int
= va_arg(argptr
, int);
567 p
->pad_longint
= va_arg(argptr
, long int);
570 case wxPAT_LONGLONGINT
:
571 p
->pad_longlongint
= va_arg(argptr
, wxLongLong_t
);
573 #endif // wxLongLong_t
575 p
->pad_sizet
= va_arg(argptr
, size_t);
578 p
->pad_double
= va_arg(argptr
, double);
580 case wxPAT_LONGDOUBLE
:
581 p
->pad_longdouble
= va_arg(argptr
, long double);
584 p
->pad_pointer
= va_arg(argptr
, void *);
588 p
->pad_char
= (char)va_arg(argptr
, int); // char is promoted to int when passed through '...'
591 p
->pad_wchar
= (wchar_t)va_arg(argptr
, int); // char is promoted to int when passed through '...'
596 p
->pad_str
= va_arg(argptr
, void *);
600 p
->pad_nint
= va_arg(argptr
, int *);
602 case wxPAT_NSHORTINT
:
603 p
->pad_nshortint
= va_arg(argptr
, short int *);
606 p
->pad_nlongint
= va_arg(argptr
, long int *);
610 // this will be handled as part of the next argument
618 return true; // loading was successful
621 template<typename CharType
>
622 int wxPrintfConvSpec
<CharType
>::Process(CharType
*buf
, size_t lenMax
, wxPrintfArg
*p
, size_t written
)
624 // buffer to avoid dynamic memory allocation each time for small strings;
625 // note that this buffer is used only to hold results of number formatting,
626 // %s directly writes user's string in buf, without using szScratch
627 char szScratch
[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
];
628 size_t lenScratch
= 0, lenCur
= 0;
630 #define APPEND_CH(ch) \
632 if ( lenCur == lenMax ) \
635 buf[lenCur++] = ch; \
641 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_int
);
645 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longint
);
649 case wxPAT_LONGLONGINT
:
650 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longlongint
);
652 #endif // SIZEOF_LONG_LONG
655 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_sizet
);
658 case wxPAT_LONGDOUBLE
:
659 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longdouble
);
663 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_double
);
667 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_pointer
);
674 if (m_type
== wxPAT_CHAR
)
676 else // m_type == wxPAT_WCHAR
684 for (i
= 1; i
< (size_t)m_nMinWidth
; i
++)
690 for (i
= 1; i
< (size_t)m_nMinWidth
; i
++)
701 if ( m_nMaxWidth
>= 6 )
704 else if (m_type
== wxPAT_PCHAR
)
705 s
.assign(static_cast<const char *>(p
->pad_str
));
706 else // m_type == wxPAT_PWCHAR
707 s
.assign(static_cast<const wchar_t *>(p
->pad_str
));
709 typename wxPrintfStringHelper
<CharType
>::ConvertedType
strbuf(
710 wxPrintfStringHelper
<CharType
>::Convert(s
));
712 // at this point we are sure that m_nMaxWidth is positive or
713 // null (see top of wxPrintfConvSpec::LoadArg)
714 int len
= wxMin((unsigned int)m_nMaxWidth
, wxStrlen(strbuf
));
720 for (i
= len
; i
< m_nMinWidth
; i
++)
724 len
= wxMin((unsigned int)len
, lenMax
-lenCur
);
725 wxStrncpy(buf
+lenCur
, strbuf
, len
);
730 for (i
= len
; i
< m_nMinWidth
; i
++)
737 *p
->pad_nint
= written
;
740 case wxPAT_NSHORTINT
:
741 *p
->pad_nshortint
= (short int)written
;
745 *p
->pad_nlongint
= written
;
753 // if we used system's sprintf() then we now need to append the s_szScratch
754 // buffer to the given one...
760 case wxPAT_LONGLONGINT
:
763 case wxPAT_LONGDOUBLE
:
766 wxASSERT(lenScratch
< wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
);
767 // NB: 1) we can compare lenMax (for CharType*, i.e. possibly
768 // wchar_t*) with lenScratch (char*) because this code is
769 // formatting integers and that will have the same length
770 // even in UTF-8 (the only case when char* length may be
771 // more than wchar_t* length of the same string)
772 // 2) wxStrncpy converts the 2nd argument to 1st argument's
773 // type transparently if their types differ, so this code
774 // works for both instantiations
775 if (lenMax
< lenScratch
)
777 // fill output buffer and then return -1
778 wxStrncpy(buf
, szScratch
, lenMax
);
781 wxStrncpy(buf
, szScratch
, lenScratch
);
782 lenCur
+= lenScratch
;
786 break; // all other cases were completed previously
793 // helper that parses format string
794 template<typename CharType
>
795 struct wxPrintfConvSpecParser
797 typedef wxPrintfConvSpec
<CharType
> ConvSpec
;
799 wxPrintfConvSpecParser(const CharType
*fmt
)
803 nonposarg_present
= false;
805 memset(pspec
, 0, sizeof(pspec
));
807 // parse the format string
808 for ( const CharType
*toparse
= fmt
; *toparse
!= wxT('\0'); toparse
++ )
810 // skip everything except format specifications
811 if ( *toparse
!= '%' )
814 // also skip escaped percent signs
815 if ( toparse
[1] == '%' )
821 ConvSpec
*spec
= &specs
[nargs
];
824 // attempt to parse this format specification
825 if ( !spec
->Parse(toparse
) )
828 // advance to the end of this specifier
829 toparse
= spec
->m_pArgEnd
;
831 // special handling for specifications including asterisks: we need
832 // to reserve an extra slot (or two if asterisks were used for both
833 // width and precision) in specs array in this case
834 if ( const char *f
= strchr(spec
->m_szFlags
, '*') )
836 unsigned numAsterisks
= 1;
837 if ( strchr(++f
, '*') )
840 for ( unsigned n
= 0; n
< numAsterisks
; n
++ )
842 if ( nargs
++ == wxMAX_SVNPRINTF_ARGUMENTS
)
845 // TODO: we need to support specifiers of the form "%2$*1$s"
846 // (this is the same as "%*s") as if any positional arguments
847 // are used all asterisks must be positional as well but this
848 // requires a lot of changes in this code (basically we'd need
849 // to rewrite Parse() to return "*" and conversion itself as
851 if ( posarg_present
)
857 "Format string \"%s\" uses both positional "
858 "parameters and '*' but this is not currently "
859 "supported by this implementation, sorry.",
865 specs
[nargs
] = *spec
;
867 // make an entry for '*' and point to it from pspec
869 spec
->m_type
= wxPAT_STAR
;
870 pspec
[nargs
- 1] = spec
;
872 spec
= &specs
[nargs
];
877 // check if this is a positional or normal argument
878 if ( spec
->m_pos
> 0 )
880 // the positional arguments start from number 1 so we need
881 // to adjust the index
883 posarg_present
= true;
885 else // not a positional argument...
888 nonposarg_present
= true;
891 // this conversion specifier is tied to the pos-th argument...
892 pspec
[spec
->m_pos
] = spec
;
894 if ( nargs
++ == wxMAX_SVNPRINTF_ARGUMENTS
)
899 // warn if we lost any arguments (the program probably will crash
900 // anyhow because of stack corruption...)
901 if ( nargs
== wxMAX_SVNPRINTF_ARGUMENTS
)
907 "wxVsnprintf() currently supports only %d arguments, "
908 "but format string \"%s\" defines more of them.\n"
909 "You need to change wxMAX_SVNPRINTF_ARGUMENTS and "
910 "recompile if more are really needed.",
911 fmt
, wxMAX_SVNPRINTF_ARGUMENTS
917 // total number of valid elements in specs
920 // all format specifications in this format string in order of their
921 // appearance (which may be different from arguments order)
922 ConvSpec specs
[wxMAX_SVNPRINTF_ARGUMENTS
];
924 // pointer to specs array element for the N-th argument
925 ConvSpec
*pspec
[wxMAX_SVNPRINTF_ARGUMENTS
];
927 // true if any positional/non-positional parameters are used
935 #endif // _WX_PRIVATE_WXPRINTF_H_