1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/wxprintf.cpp
3 // Purpose: wxWidgets wxPrintf() implementation
5 // Modified by: Ron Lee, Francesco Montorsi
8 // Copyright: (c) wxWidgets copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
13 // headers, declarations, constants
14 // ===========================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
28 #include "wx/string.h"
30 #include "wx/utils.h" // for wxMin and wxMax
34 #if defined(__MWERKS__) && __MSL__ >= 0x6000
40 // ============================================================================
41 // printf() implementation
42 // ============================================================================
44 // special test mode: define all functions below even if we don't really need
45 // them to be able to test them
47 #undef wxCRT_VsnprintfW_
48 #undef wxCRT_VsnprintfA
51 // ----------------------------------------------------------------------------
52 // implement [v]snprintf() if the system doesn't provide a safe one
53 // or if the system's one does not support positional parameters
54 // (very useful for i18n purposes)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
58 // common code for both ANSI and Unicode versions
59 // ----------------------------------------------------------------------------
61 #if !defined(wxCRT_VsnprintfW_) || !defined(wxCRT_VsnprintfA)
63 // wxUSE_STRUTILS says our wxCRT_VsnprintfW_ implementation to use or not to
64 // use wxStrlen and wxStrncpy functions over one-char processing loops.
66 // Some benchmarking revealed that wxUSE_STRUTILS == 1 has the following
69 // when in ANSI mode, this setting does not change almost anything
70 // when in Unicode mode, it gives ~ 50% of slowdown !
72 // both in ANSI and Unicode mode it gives ~ 60% of speedup !
74 #if defined(WIN32) && wxUSE_UNICODE
75 #define wxUSE_STRUTILS 0
77 #define wxUSE_STRUTILS 1
80 // some limits of our implementation
81 #define wxMAX_SVNPRINTF_ARGUMENTS 64
82 #define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32
83 #define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512
85 // prefer snprintf over sprintf
86 #if defined(__VISUALC__) || \
87 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
88 #define system_sprintf(buff, max, flags, data) \
89 ::_snprintf(buff, max, flags, data)
90 #elif defined(HAVE_SNPRINTF)
91 #define system_sprintf(buff, max, flags, data) \
92 ::snprintf(buff, max, flags, data)
93 #else // NB: at least sprintf() should always be available
94 // since 'max' is not used in this case, wxVsnprintf() should always
95 // ensure that 'buff' is big enough for all common needs
96 // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN)
97 #define system_sprintf(buff, max, flags, data) \
98 ::sprintf(buff, flags, data)
100 #define SYSTEM_SPRINTF_IS_UNSAFE
106 // the conversion specifiers accepted by wxCRT_VsnprintfW_
107 enum wxPrintfArgType
{
110 wxPAT_INT
, // %d, %i, %o, %u, %x, %X
111 wxPAT_LONGINT
, // %ld, etc
113 wxPAT_LONGLONGINT
, // %Ld, etc
115 wxPAT_SIZET
, // %Zd, etc
117 wxPAT_DOUBLE
, // %e, %E, %f, %g, %G
118 wxPAT_LONGDOUBLE
, // %le, etc
122 wxPAT_CHAR
, // %hc (in ANSI mode: %c, too)
123 wxPAT_WCHAR
, // %lc (in Unicode mode: %c, too)
125 wxPAT_PCHAR
, // %s (related to a char *)
126 wxPAT_PWCHAR
, // %s (related to a wchar_t *)
129 wxPAT_NSHORTINT
, // %hn
130 wxPAT_NLONGINT
// %ln
133 // an argument passed to wxCRT_VsnprintfW_
135 int pad_int
; // %d, %i, %o, %u, %x, %X
136 long int pad_longint
; // %ld, etc
138 wxLongLong_t pad_longlongint
; // %Ld, etc
140 size_t pad_sizet
; // %Zd, etc
142 double pad_double
; // %e, %E, %f, %g, %G
143 long double pad_longdouble
; // %le, etc
145 void *pad_pointer
; // %p
147 char pad_char
; // %hc (in ANSI mode: %c, too)
148 wchar_t pad_wchar
; // %lc (in Unicode mode: %c, too)
153 short int *pad_nshortint
; // %hn
154 long int *pad_nlongint
; // %ln
157 // helper for converting string into either char* or wchar_t* dependening
158 // on the type of wxPrintfConvSpec<T> instantiation:
159 template<typename CharType
> struct wxPrintfStringHelper
{};
161 template<> struct wxPrintfStringHelper
<char>
163 typedef const wxWX2MBbuf ConvertedType
;
164 static ConvertedType
Convert(const wxString
& s
) { return s
.mb_str(); }
167 template<> struct wxPrintfStringHelper
<wchar_t>
169 typedef const wxWX2WCbuf ConvertedType
;
170 static ConvertedType
Convert(const wxString
& s
) { return s
.wc_str(); }
174 // Contains parsed data relative to a conversion specifier given to
175 // wxCRT_VsnprintfW_ and parsed from the format string
176 // NOTE: in C++ there is almost no difference between struct & classes thus
177 // there is no performance gain by using a struct here...
178 template<typename CharType
>
179 class wxPrintfConvSpec
183 // the position of the argument relative to this conversion specifier
186 // the type of this conversion specifier
187 wxPrintfArgType m_type
;
189 // the minimum and maximum width
190 // when one of this var is set to -1 it means: use the following argument
191 // in the stack as minimum/maximum width for this conversion specifier
192 int m_nMinWidth
, m_nMaxWidth
;
194 // does the argument need to the be aligned to left ?
197 // pointer to the '%' of this conversion specifier in the format string
198 // NOTE: this points somewhere in the string given to the Parse() function -
199 // it's task of the caller ensure that memory is still valid !
200 const CharType
*m_pArgPos
;
202 // pointer to the last character of this conversion specifier in the
204 // NOTE: this points somewhere in the string given to the Parse() function -
205 // it's task of the caller ensure that memory is still valid !
206 const CharType
*m_pArgEnd
;
208 // a little buffer where formatting flags like #+\.hlqLZ are stored by Parse()
209 // for use in Process()
210 // NB: even if this buffer is used only for numeric conversion specifiers and
211 // thus could be safely declared as a char[] buffer, we want it to be wchar_t
212 // so that in Unicode builds we can avoid to convert its contents to Unicode
213 // chars when copying it in user's buffer.
214 char m_szFlags
[wxMAX_SVNPRINTF_FLAGBUFFER_LEN
];
219 // we don't declare this as a constructor otherwise it would be called
220 // automatically and we don't want this: to be optimized, wxCRT_VsnprintfW_
221 // calls this function only on really-used instances of this class.
224 // Parses the first conversion specifier in the given string, which must
225 // begin with a '%'. Returns false if the first '%' does not introduce a
226 // (valid) conversion specifier and thus should be ignored.
227 bool Parse(const CharType
*format
);
229 // Process this conversion specifier and puts the result in the given
230 // buffer. Returns the number of characters written in 'buf' or -1 if
231 // there's not enough space.
232 int Process(CharType
*buf
, size_t lenMax
, wxPrintfArg
*p
, size_t written
);
234 // Loads the argument of this conversion specifier from given va_list.
235 bool LoadArg(wxPrintfArg
*p
, va_list &argptr
);
238 // An helper function of LoadArg() which is used to handle the '*' flag
239 void ReplaceAsteriskWith(int w
);
242 template<typename CharType
>
243 void wxPrintfConvSpec
<CharType
>::Init()
246 m_nMaxWidth
= 0xFFFF;
248 m_bAlignLeft
= false;
249 m_pArgPos
= m_pArgEnd
= NULL
;
250 m_type
= wxPAT_INVALID
;
252 // this character will never be removed from m_szFlags array and
253 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
257 template<typename CharType
>
258 bool wxPrintfConvSpec
<CharType
>::Parse(const CharType
*format
)
262 // temporary parse data
264 bool in_prec
, // true if we found the dot in some previous iteration
265 prec_dot
; // true if the dot has been already added to m_szFlags
268 m_bAlignLeft
= in_prec
= prec_dot
= false;
269 m_pArgPos
= m_pArgEnd
= format
;
273 if (in_prec && !prec_dot) \
275 m_szFlags[flagofs++] = '.'; \
280 const CharType ch
= *(++m_pArgEnd
);
284 return false; // not really an argument
287 return false; // not really an argument
295 m_szFlags
[flagofs
++] = char(ch
);
301 m_szFlags
[flagofs
++] = char(ch
);
309 // dot will be auto-added to m_szFlags if non-negative
316 m_szFlags
[flagofs
++] = char(ch
);
320 // NB: it's safe to use flagofs-1 as flagofs always start from 1
321 if (m_szFlags
[flagofs
-1] == 'l') // 'll' modifier is the same as 'L' or 'q'
326 m_szFlags
[flagofs
++] = char(ch
);
333 m_szFlags
[flagofs
++] = char(ch
);
336 // under Windows we support the special '%I64' notation as longlong
337 // integer conversion specifier for MSVC compatibility
338 // (it behaves exactly as '%lli' or '%Li' or '%qi')
340 if (*(m_pArgEnd
+1) != wxT('6') ||
341 *(m_pArgEnd
+2) != wxT('4'))
342 return false; // bad format
349 m_szFlags
[flagofs
++] = char(ch
);
350 m_szFlags
[flagofs
++] = '6';
351 m_szFlags
[flagofs
++] = '4';
358 m_szFlags
[flagofs
++] = char(ch
);
366 // tell Process() to use the next argument
367 // in the stack as maxwidth...
372 // tell Process() to use the next argument
373 // in the stack as minwidth...
377 // save the * in our formatting buffer...
378 // will be replaced later by Process()
379 m_szFlags
[flagofs
++] = char(ch
);
382 case wxT('1'): case wxT('2'): case wxT('3'):
383 case wxT('4'): case wxT('5'): case wxT('6'):
384 case wxT('7'): case wxT('8'): case wxT('9'):
388 while ( (*m_pArgEnd
>= CharType('0')) &&
389 (*m_pArgEnd
<= CharType('9')) )
391 m_szFlags
[flagofs
++] = char(*m_pArgEnd
);
392 len
= len
*10 + (*m_pArgEnd
- wxT('0'));
401 m_pArgEnd
--; // the main loop pre-increments n again
405 case wxT('$'): // a positional parameter (e.g. %2$s) ?
407 if (m_nMinWidth
<= 0)
408 break; // ignore this formatting flag as no
409 // numbers are preceding it
411 // remove from m_szFlags all digits previously added
414 } while (m_szFlags
[flagofs
] >= '1' &&
415 m_szFlags
[flagofs
] <= '9');
417 // re-adjust the offset making it point to the
418 // next free char of m_szFlags
433 m_szFlags
[flagofs
++] = char(ch
);
434 m_szFlags
[flagofs
] = '\0';
438 // NB: 'short int' value passed through '...'
439 // is promoted to 'int', so we have to get
440 // an int from stack even if we need a short
443 m_type
= wxPAT_LONGINT
;
446 m_type
= wxPAT_LONGLONGINT
;
447 #else // !wxLongLong_t
448 m_type
= wxPAT_LONGINT
;
449 #endif // wxLongLong_t/!wxLongLong_t
451 m_type
= wxPAT_SIZET
;
461 m_szFlags
[flagofs
++] = char(ch
);
462 m_szFlags
[flagofs
] = '\0';
464 m_type
= wxPAT_LONGDOUBLE
;
466 m_type
= wxPAT_DOUBLE
;
471 m_type
= wxPAT_POINTER
;
472 m_szFlags
[flagofs
++] = char(ch
);
473 m_szFlags
[flagofs
] = '\0';
480 // in Unicode mode %hc == ANSI character
481 // and in ANSI mode, %hc == %c == ANSI...
486 // in ANSI mode %lc == Unicode character
487 // and in Unicode mode, %lc == %c == Unicode...
488 m_type
= wxPAT_WCHAR
;
493 // in Unicode mode, %c == Unicode character
494 m_type
= wxPAT_WCHAR
;
496 // in ANSI mode, %c == ANSI character
506 // Unicode mode wx extension: we'll let %hs mean non-Unicode
507 // strings (when in ANSI mode, %s == %hs == ANSI string)
508 m_type
= wxPAT_PCHAR
;
512 // in Unicode mode, %ls == %s == Unicode string
513 // in ANSI mode, %ls == Unicode string
514 m_type
= wxPAT_PWCHAR
;
519 m_type
= wxPAT_PWCHAR
;
521 m_type
= wxPAT_PCHAR
;
531 m_type
= wxPAT_NSHORTINT
;
533 m_type
= wxPAT_NLONGINT
;
538 // bad format, don't consider this an argument;
539 // leave it unchanged
543 if (flagofs
== wxMAX_SVNPRINTF_FLAGBUFFER_LEN
)
545 wxLogDebug(wxT("Too many flags specified for a single conversion specifier!"));
551 return true; // parsing was successful
554 template<typename CharType
>
555 void wxPrintfConvSpec
<CharType
>::ReplaceAsteriskWith(int width
)
557 char temp
[wxMAX_SVNPRINTF_FLAGBUFFER_LEN
];
559 // find the first * in our flag buffer
560 char *pwidth
= strchr(m_szFlags
, '*');
561 wxCHECK_RET(pwidth
, _T("field width must be specified"));
563 // save what follows the * (the +1 is to skip the asterisk itself!)
564 strcpy(temp
, pwidth
+1);
567 pwidth
[0] = wxT('-');
571 // replace * with the actual integer given as width
572 #ifndef SYSTEM_SPRINTF_IS_UNSAFE
573 int maxlen
= (m_szFlags
+ wxMAX_SVNPRINTF_FLAGBUFFER_LEN
- pwidth
) /
576 int offset
= system_sprintf(pwidth
, maxlen
, "%d", abs(width
));
578 // restore after the expanded * what was following it
579 strcpy(pwidth
+offset
, temp
);
582 template<typename CharType
>
583 bool wxPrintfConvSpec
<CharType
>::LoadArg(wxPrintfArg
*p
, va_list &argptr
)
585 // did the '*' width/precision specifier was used ?
586 if (m_nMaxWidth
== -1)
588 // take the maxwidth specifier from the stack
589 m_nMaxWidth
= va_arg(argptr
, int);
593 ReplaceAsteriskWith(m_nMaxWidth
);
596 if (m_nMinWidth
== -1)
598 // take the minwidth specifier from the stack
599 m_nMinWidth
= va_arg(argptr
, int);
601 ReplaceAsteriskWith(m_nMinWidth
);
604 m_bAlignLeft
= !m_bAlignLeft
;
605 m_nMinWidth
= -m_nMinWidth
;
611 p
->pad_int
= va_arg(argptr
, int);
614 p
->pad_longint
= va_arg(argptr
, long int);
617 case wxPAT_LONGLONGINT
:
618 p
->pad_longlongint
= va_arg(argptr
, wxLongLong_t
);
620 #endif // wxLongLong_t
622 p
->pad_sizet
= va_arg(argptr
, size_t);
625 p
->pad_double
= va_arg(argptr
, double);
627 case wxPAT_LONGDOUBLE
:
628 p
->pad_longdouble
= va_arg(argptr
, long double);
631 p
->pad_pointer
= va_arg(argptr
, void *);
635 p
->pad_char
= (char)va_arg(argptr
, int); // char is promoted to int when passed through '...'
638 p
->pad_wchar
= (wchar_t)va_arg(argptr
, int); // char is promoted to int when passed through '...'
643 p
->pad_str
= va_arg(argptr
, void *);
647 p
->pad_nint
= va_arg(argptr
, int *);
649 case wxPAT_NSHORTINT
:
650 p
->pad_nshortint
= va_arg(argptr
, short int *);
653 p
->pad_nlongint
= va_arg(argptr
, long int *);
661 return true; // loading was successful
664 template<typename CharType
>
665 int wxPrintfConvSpec
<CharType
>::Process(CharType
*buf
, size_t lenMax
, wxPrintfArg
*p
, size_t written
)
667 // buffer to avoid dynamic memory allocation each time for small strings;
668 // note that this buffer is used only to hold results of number formatting,
669 // %s directly writes user's string in buf, without using szScratch
670 char szScratch
[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
];
671 size_t lenScratch
= 0, lenCur
= 0;
673 #define APPEND_CH(ch) \
675 if ( lenCur == lenMax ) \
678 buf[lenCur++] = ch; \
684 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_int
);
688 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longint
);
692 case wxPAT_LONGLONGINT
:
693 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longlongint
);
695 #endif // SIZEOF_LONG_LONG
698 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_sizet
);
701 case wxPAT_LONGDOUBLE
:
702 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longdouble
);
706 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_double
);
710 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_pointer
);
717 if (m_type
== wxPAT_CHAR
)
719 else // m_type == wxPAT_WCHAR
727 for (i
= 1; i
< (size_t)m_nMinWidth
; i
++)
733 for (i
= 1; i
< (size_t)m_nMinWidth
; i
++)
741 wxArgNormalizedString
arg(p
->pad_str
);
744 if ( !arg
.IsValid() && m_nMaxWidth
>= 6 )
747 typename wxPrintfStringHelper
<CharType
>::ConvertedType
strbuf(
748 wxPrintfStringHelper
<CharType
>::Convert(s
));
750 // at this point we are sure that m_nMaxWidth is positive or
751 // null (see top of wxPrintfConvSpec::LoadArg)
752 int len
= wxMin((unsigned int)m_nMaxWidth
, wxStrlen(strbuf
));
758 for (i
= len
; i
< m_nMinWidth
; i
++)
762 len
= wxMin((unsigned int)len
, lenMax
-lenCur
);
763 wxStrncpy(buf
+lenCur
, strbuf
, len
);
768 for (i
= len
; i
< m_nMinWidth
; i
++)
775 *p
->pad_nint
= written
;
778 case wxPAT_NSHORTINT
:
779 *p
->pad_nshortint
= (short int)written
;
783 *p
->pad_nlongint
= written
;
791 // if we used system's sprintf() then we now need to append the s_szScratch
792 // buffer to the given one...
798 case wxPAT_LONGLONGINT
:
801 case wxPAT_LONGDOUBLE
:
804 wxASSERT(lenScratch
< wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
);
805 // NB: 1) we can compare lenMax (for CharType*, i.e. possibly
806 // wchar_t*) with lenScratch (char*) because this code is
807 // formatting integers and that will have the same length
808 // even in UTF-8 (the only case when char* length may be
809 // more than wchar_t* length of the same string)
810 // 2) wxStrncpy converts the 2nd argument to 1st argument's
811 // type transparently if their types differ, so this code
812 // works for both instantiations
813 if (lenMax
< lenScratch
)
815 // fill output buffer and then return -1
816 wxStrncpy(buf
, szScratch
, lenMax
);
819 wxStrncpy(buf
, szScratch
, lenScratch
);
820 lenCur
+= lenScratch
;
824 break; // all other cases were completed previously
830 // Copy chars from source to dest converting '%%' to '%'. Takes at most maxIn
831 // chars from source and write at most outMax chars to dest, returns the
832 // number of chars actually written. Does not treat null specially.
833 template<typename CharType
>
834 static int wxCopyStrWithPercents(
838 const CharType
*source
)
846 for ( i
= 0; i
< maxIn
-1 && written
< maxOut
; source
++, i
++)
848 dest
[written
++] = *source
;
849 if (*(source
+1) == wxT('%'))
851 // skip this additional '%' character
857 if (i
< maxIn
&& written
< maxOut
)
858 // copy last character inconditionally
859 dest
[written
++] = *source
;
864 template<typename CharType
>
865 static int wxDoVsnprintf(CharType
*buf
, size_t lenMax
,
866 const CharType
*format
, va_list argptr
)
868 // useful for debugging, to understand if we are really using this function
869 // rather than the system implementation
871 wprintf(L
"Using wxCRT_VsnprintfW_\n");
875 wxPrintfConvSpec
<CharType
> arg
[wxMAX_SVNPRINTF_ARGUMENTS
];
876 wxPrintfArg argdata
[wxMAX_SVNPRINTF_ARGUMENTS
];
877 wxPrintfConvSpec
<CharType
> *pspec
[wxMAX_SVNPRINTF_ARGUMENTS
] = { NULL
};
881 // number of characters in the buffer so far, must be less than lenMax
885 const CharType
*toparse
= format
;
887 // parse the format string
888 bool posarg_present
= false, nonposarg_present
= false;
889 for (; *toparse
!= wxT('\0'); toparse
++)
891 if (*toparse
== wxT('%') )
895 // let's see if this is a (valid) conversion specifier...
896 if (arg
[nargs
].Parse(toparse
))
899 wxPrintfConvSpec
<CharType
> *current
= &arg
[nargs
];
901 // make toparse point to the end of this specifier
902 toparse
= current
->m_pArgEnd
;
904 if (current
->m_pos
> 0)
906 // the positionals start from number 1... adjust the index
908 posarg_present
= true;
912 // not a positional argument...
913 current
->m_pos
= nargs
;
914 nonposarg_present
= true;
917 // this conversion specifier is tied to the pos-th argument...
918 pspec
[current
->m_pos
] = current
;
921 if (nargs
== wxMAX_SVNPRINTF_ARGUMENTS
)
923 wxLogDebug(wxT("A single call to wxVsnprintf() has more than %d arguments; ")
924 wxT("ignoring all remaining arguments."), wxMAX_SVNPRINTF_ARGUMENTS
);
925 break; // cannot handle any additional conv spec
930 // it's safe to look in the next character of toparse as at worst
932 if (*(toparse
+1) == wxT('%'))
933 toparse
++; // the Parse() returned false because we've found a %%
938 if (posarg_present
&& nonposarg_present
)
941 return -1; // format strings with both positional and
942 } // non-positional conversion specifier are unsupported !!
944 // on platforms where va_list is an array type, it is necessary to make a
945 // copy to be able to pass it to LoadArg as a reference.
948 wxVaCopy(ap
, argptr
);
950 // now load arguments from stack
951 for (i
=0; i
< nargs
&& ok
; i
++)
953 // !pspec[i] means that the user forgot a positional parameter (e.g. %$1s %$3s);
954 // LoadArg == false means that wxPrintfConvSpec::Parse failed to set the
955 // conversion specifier 'type' to a valid value...
956 ok
= pspec
[i
] && pspec
[i
]->LoadArg(&argdata
[i
], ap
);
961 // something failed while loading arguments from the variable list...
962 // (e.g. the user repeated twice the same positional argument)
969 // finally, process each conversion specifier with its own argument
971 for (i
=0; i
< nargs
; i
++)
973 // copy in the output buffer the portion of the format string between
974 // last specifier and the current one
975 size_t tocopy
= ( arg
[i
].m_pArgPos
- toparse
);
977 lenCur
+= wxCopyStrWithPercents(lenMax
- lenCur
, buf
+ lenCur
,
979 if (lenCur
== lenMax
)
982 return lenMax
+1; // not enough space in the output buffer !
985 // process this specifier directly in the output buffer
986 int n
= arg
[i
].Process(buf
+lenCur
, lenMax
- lenCur
, &argdata
[arg
[i
].m_pos
], lenCur
);
989 buf
[lenMax
-1] = wxT('\0'); // be sure to always NUL-terminate the string
990 return lenMax
+1; // not enough space in the output buffer !
994 // the +1 is because wxPrintfConvSpec::m_pArgEnd points to the last character
995 // of the format specifier, but we are not interested to it...
996 toparse
= arg
[i
].m_pArgEnd
+ 1;
999 // copy portion of the format string after last specifier
1000 // NOTE: toparse is pointing to the character just after the last processed
1001 // conversion specifier
1002 // NOTE2: the +1 is because we want to copy also the '\0'
1003 size_t tocopy
= wxStrlen(format
) + 1 - ( toparse
- format
) ;
1005 lenCur
+= wxCopyStrWithPercents(lenMax
- lenCur
, buf
+ lenCur
,
1006 tocopy
, toparse
) - 1;
1010 return lenMax
+1; // not enough space in the output buffer !
1014 // wxASSERT(lenCur == wxStrlen(buf));
1015 // in fact if we embedded NULLs in the output buffer (using %c with a '\0')
1016 // such check would fail
1024 } // anonymous namespace
1026 #endif // !defined(wxCRT_VsnprintfW_) || !defined(wxCRT_VsnprintfA)
1028 // ----------------------------------------------------------------------------
1029 // wxCRT_VsnprintfW_
1030 // ----------------------------------------------------------------------------
1032 #if !defined(wxCRT_VsnprintfW_)
1034 #if !wxUSE_WXVSNPRINTFW
1035 #error "wxUSE_WXVSNPRINTFW must be 1 if our wxCRT_VsnprintfW_ is used"
1038 int wxCRT_VsnprintfW_(wchar_t *buf
, size_t len
,
1039 const wchar_t *format
, va_list argptr
)
1041 return wxDoVsnprintf(buf
, len
, format
, argptr
);
1044 #else // wxCRT_VsnprintfW_ is defined
1046 #if wxUSE_WXVSNPRINTFW
1047 #error "wxUSE_WXVSNPRINTFW must be 0 if our wxCRT_VsnprintfW_ is not used"
1050 #endif // !wxCRT_VsnprintfW_
1052 // ----------------------------------------------------------------------------
1054 // ----------------------------------------------------------------------------
1056 #ifndef wxCRT_VsnprintfA
1058 #if !wxUSE_WXVSNPRINTFA
1059 #error "wxUSE_WXVSNPRINTFA must be 1 if our wxCRT_VsnprintfA is used"
1062 int wxCRT_VsnprintfA(char *buf
, size_t len
,
1063 const char *format
, va_list argptr
)
1065 return wxDoVsnprintf(buf
, len
, format
, argptr
);
1068 #else // wxCRT_VsnprintfA is defined
1070 #if wxUSE_WXVSNPRINTFA
1071 #error "wxUSE_WXVSNPRINTFA must be 0 if our wxCRT_VsnprintfA is not used"
1074 #endif // !wxCRT_VsnprintfA