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 // some limits of our implementation
64 #define wxMAX_SVNPRINTF_ARGUMENTS 64
65 #define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32
66 #define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512
68 // prefer snprintf over sprintf
69 #if defined(__VISUALC__) || \
70 (defined(__BORLANDC__) && __BORLANDC__ >= 0x540)
71 #define system_sprintf(buff, max, flags, data) \
72 ::_snprintf(buff, max, flags, data)
73 #elif defined(HAVE_SNPRINTF)
74 #define system_sprintf(buff, max, flags, data) \
75 ::snprintf(buff, max, flags, data)
76 #else // NB: at least sprintf() should always be available
77 // since 'max' is not used in this case, wxVsnprintf() should always
78 // ensure that 'buff' is big enough for all common needs
79 // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN)
80 #define system_sprintf(buff, max, flags, data) \
81 ::sprintf(buff, flags, data)
83 #define SYSTEM_SPRINTF_IS_UNSAFE
89 // the conversion specifiers accepted by wxCRT_VsnprintfW
90 enum wxPrintfArgType
{
93 wxPAT_INT
, // %d, %i, %o, %u, %x, %X
94 wxPAT_LONGINT
, // %ld, etc
96 wxPAT_LONGLONGINT
, // %Ld, etc
98 wxPAT_SIZET
, // %Zd, etc
100 wxPAT_DOUBLE
, // %e, %E, %f, %g, %G
101 wxPAT_LONGDOUBLE
, // %le, etc
105 wxPAT_CHAR
, // %hc (in ANSI mode: %c, too)
106 wxPAT_WCHAR
, // %lc (in Unicode mode: %c, too)
108 wxPAT_PCHAR
, // %s (related to a char *)
109 wxPAT_PWCHAR
, // %s (related to a wchar_t *)
112 wxPAT_NSHORTINT
, // %hn
113 wxPAT_NLONGINT
// %ln
116 // an argument passed to wxCRT_VsnprintfW
118 int pad_int
; // %d, %i, %o, %u, %x, %X
119 long int pad_longint
; // %ld, etc
121 wxLongLong_t pad_longlongint
; // %Ld, etc
123 size_t pad_sizet
; // %Zd, etc
125 double pad_double
; // %e, %E, %f, %g, %G
126 long double pad_longdouble
; // %le, etc
128 void *pad_pointer
; // %p
130 char pad_char
; // %hc (in ANSI mode: %c, too)
131 wchar_t pad_wchar
; // %lc (in Unicode mode: %c, too)
136 short int *pad_nshortint
; // %hn
137 long int *pad_nlongint
; // %ln
140 // helper for converting string into either char* or wchar_t* dependening
141 // on the type of wxPrintfConvSpec<T> instantiation:
142 template<typename CharType
> struct wxPrintfStringHelper
{};
144 template<> struct wxPrintfStringHelper
<char>
146 typedef const wxWX2MBbuf ConvertedType
;
147 static ConvertedType
Convert(const wxString
& s
) { return s
.mb_str(); }
150 template<> struct wxPrintfStringHelper
<wchar_t>
152 typedef const wxWX2WCbuf ConvertedType
;
153 static ConvertedType
Convert(const wxString
& s
) { return s
.wc_str(); }
157 // Contains parsed data relative to a conversion specifier given to
158 // wxCRT_VsnprintfW and parsed from the format string
159 // NOTE: in C++ there is almost no difference between struct & classes thus
160 // there is no performance gain by using a struct here...
161 template<typename CharType
>
162 class wxPrintfConvSpec
166 // the position of the argument relative to this conversion specifier
169 // the type of this conversion specifier
170 wxPrintfArgType m_type
;
172 // the minimum and maximum width
173 // when one of this var is set to -1 it means: use the following argument
174 // in the stack as minimum/maximum width for this conversion specifier
175 int m_nMinWidth
, m_nMaxWidth
;
177 // does the argument need to the be aligned to left ?
180 // pointer to the '%' of this conversion specifier in the format string
181 // NOTE: this points somewhere in the string given to the Parse() function -
182 // it's task of the caller ensure that memory is still valid !
183 const CharType
*m_pArgPos
;
185 // pointer to the last character of this conversion specifier in the
187 // NOTE: this points somewhere in the string given to the Parse() function -
188 // it's task of the caller ensure that memory is still valid !
189 const CharType
*m_pArgEnd
;
191 // a little buffer where formatting flags like #+\.hlqLZ are stored by Parse()
192 // for use in Process()
193 // NB: even if this buffer is used only for numeric conversion specifiers and
194 // thus could be safely declared as a char[] buffer, we want it to be wchar_t
195 // so that in Unicode builds we can avoid to convert its contents to Unicode
196 // chars when copying it in user's buffer.
197 char m_szFlags
[wxMAX_SVNPRINTF_FLAGBUFFER_LEN
];
202 // we don't declare this as a constructor otherwise it would be called
203 // automatically and we don't want this: to be optimized, wxCRT_VsnprintfW
204 // calls this function only on really-used instances of this class.
207 // Parses the first conversion specifier in the given string, which must
208 // begin with a '%'. Returns false if the first '%' does not introduce a
209 // (valid) conversion specifier and thus should be ignored.
210 bool Parse(const CharType
*format
);
212 // Process this conversion specifier and puts the result in the given
213 // buffer. Returns the number of characters written in 'buf' or -1 if
214 // there's not enough space.
215 int Process(CharType
*buf
, size_t lenMax
, wxPrintfArg
*p
, size_t written
);
217 // Loads the argument of this conversion specifier from given va_list.
218 bool LoadArg(wxPrintfArg
*p
, va_list &argptr
);
221 // An helper function of LoadArg() which is used to handle the '*' flag
222 void ReplaceAsteriskWith(int w
);
225 template<typename CharType
>
226 void wxPrintfConvSpec
<CharType
>::Init()
229 m_nMaxWidth
= 0xFFFF;
231 m_bAlignLeft
= false;
232 m_pArgPos
= m_pArgEnd
= NULL
;
233 m_type
= wxPAT_INVALID
;
235 // this character will never be removed from m_szFlags array and
236 // is important when calling sprintf() in wxPrintfConvSpec::Process() !
240 template<typename CharType
>
241 bool wxPrintfConvSpec
<CharType
>::Parse(const CharType
*format
)
245 // temporary parse data
247 bool in_prec
, // true if we found the dot in some previous iteration
248 prec_dot
; // true if the dot has been already added to m_szFlags
251 m_bAlignLeft
= in_prec
= prec_dot
= false;
252 m_pArgPos
= m_pArgEnd
= format
;
256 if (in_prec && !prec_dot) \
258 m_szFlags[flagofs++] = '.'; \
263 const CharType ch
= *(++m_pArgEnd
);
267 return false; // not really an argument
270 return false; // not really an argument
278 m_szFlags
[flagofs
++] = char(ch
);
284 m_szFlags
[flagofs
++] = char(ch
);
292 // dot will be auto-added to m_szFlags if non-negative
299 m_szFlags
[flagofs
++] = char(ch
);
303 // NB: it's safe to use flagofs-1 as flagofs always start from 1
304 if (m_szFlags
[flagofs
-1] == 'l') // 'll' modifier is the same as 'L' or 'q'
309 m_szFlags
[flagofs
++] = char(ch
);
316 m_szFlags
[flagofs
++] = char(ch
);
319 // under Windows we support the special '%I64' notation as longlong
320 // integer conversion specifier for MSVC compatibility
321 // (it behaves exactly as '%lli' or '%Li' or '%qi')
323 if (*(m_pArgEnd
+1) != wxT('6') ||
324 *(m_pArgEnd
+2) != wxT('4'))
325 return false; // bad format
332 m_szFlags
[flagofs
++] = char(ch
);
333 m_szFlags
[flagofs
++] = '6';
334 m_szFlags
[flagofs
++] = '4';
341 m_szFlags
[flagofs
++] = char(ch
);
349 // tell Process() to use the next argument
350 // in the stack as maxwidth...
355 // tell Process() to use the next argument
356 // in the stack as minwidth...
360 // save the * in our formatting buffer...
361 // will be replaced later by Process()
362 m_szFlags
[flagofs
++] = char(ch
);
365 case wxT('1'): case wxT('2'): case wxT('3'):
366 case wxT('4'): case wxT('5'): case wxT('6'):
367 case wxT('7'): case wxT('8'): case wxT('9'):
371 while ( (*m_pArgEnd
>= CharType('0')) &&
372 (*m_pArgEnd
<= CharType('9')) )
374 m_szFlags
[flagofs
++] = char(*m_pArgEnd
);
375 len
= len
*10 + (*m_pArgEnd
- wxT('0'));
384 m_pArgEnd
--; // the main loop pre-increments n again
388 case wxT('$'): // a positional parameter (e.g. %2$s) ?
390 if (m_nMinWidth
<= 0)
391 break; // ignore this formatting flag as no
392 // numbers are preceding it
394 // remove from m_szFlags all digits previously added
397 } while (m_szFlags
[flagofs
] >= '1' &&
398 m_szFlags
[flagofs
] <= '9');
400 // re-adjust the offset making it point to the
401 // next free char of m_szFlags
416 m_szFlags
[flagofs
++] = char(ch
);
417 m_szFlags
[flagofs
] = '\0';
421 // NB: 'short int' value passed through '...'
422 // is promoted to 'int', so we have to get
423 // an int from stack even if we need a short
426 m_type
= wxPAT_LONGINT
;
429 m_type
= wxPAT_LONGLONGINT
;
430 #else // !wxLongLong_t
431 m_type
= wxPAT_LONGINT
;
432 #endif // wxLongLong_t/!wxLongLong_t
434 m_type
= wxPAT_SIZET
;
444 m_szFlags
[flagofs
++] = char(ch
);
445 m_szFlags
[flagofs
] = '\0';
447 m_type
= wxPAT_LONGDOUBLE
;
449 m_type
= wxPAT_DOUBLE
;
454 m_type
= wxPAT_POINTER
;
455 m_szFlags
[flagofs
++] = char(ch
);
456 m_szFlags
[flagofs
] = '\0';
463 // in Unicode mode %hc == ANSI character
464 // and in ANSI mode, %hc == %c == ANSI...
469 // in ANSI mode %lc == Unicode character
470 // and in Unicode mode, %lc == %c == Unicode...
471 m_type
= wxPAT_WCHAR
;
476 // in Unicode mode, %c == Unicode character
477 m_type
= wxPAT_WCHAR
;
479 // in ANSI mode, %c == ANSI character
489 // Unicode mode wx extension: we'll let %hs mean non-Unicode
490 // strings (when in ANSI mode, %s == %hs == ANSI string)
491 m_type
= wxPAT_PCHAR
;
495 // in Unicode mode, %ls == %s == Unicode string
496 // in ANSI mode, %ls == Unicode string
497 m_type
= wxPAT_PWCHAR
;
502 m_type
= wxPAT_PWCHAR
;
504 m_type
= wxPAT_PCHAR
;
514 m_type
= wxPAT_NSHORTINT
;
516 m_type
= wxPAT_NLONGINT
;
521 // bad format, don't consider this an argument;
522 // leave it unchanged
526 if (flagofs
== wxMAX_SVNPRINTF_FLAGBUFFER_LEN
)
528 wxLogDebug(wxT("Too many flags specified for a single conversion specifier!"));
534 return true; // parsing was successful
537 template<typename CharType
>
538 void wxPrintfConvSpec
<CharType
>::ReplaceAsteriskWith(int width
)
540 char temp
[wxMAX_SVNPRINTF_FLAGBUFFER_LEN
];
542 // find the first * in our flag buffer
543 char *pwidth
= strchr(m_szFlags
, '*');
544 wxCHECK_RET(pwidth
, _T("field width must be specified"));
546 // save what follows the * (the +1 is to skip the asterisk itself!)
547 strcpy(temp
, pwidth
+1);
550 pwidth
[0] = wxT('-');
554 // replace * with the actual integer given as width
555 #ifndef SYSTEM_SPRINTF_IS_UNSAFE
556 int maxlen
= (m_szFlags
+ wxMAX_SVNPRINTF_FLAGBUFFER_LEN
- pwidth
) /
559 int offset
= system_sprintf(pwidth
, maxlen
, "%d", abs(width
));
561 // restore after the expanded * what was following it
562 strcpy(pwidth
+offset
, temp
);
565 template<typename CharType
>
566 bool wxPrintfConvSpec
<CharType
>::LoadArg(wxPrintfArg
*p
, va_list &argptr
)
568 // did the '*' width/precision specifier was used ?
569 if (m_nMaxWidth
== -1)
571 // take the maxwidth specifier from the stack
572 m_nMaxWidth
= va_arg(argptr
, int);
576 ReplaceAsteriskWith(m_nMaxWidth
);
579 if (m_nMinWidth
== -1)
581 // take the minwidth specifier from the stack
582 m_nMinWidth
= va_arg(argptr
, int);
584 ReplaceAsteriskWith(m_nMinWidth
);
587 m_bAlignLeft
= !m_bAlignLeft
;
588 m_nMinWidth
= -m_nMinWidth
;
594 p
->pad_int
= va_arg(argptr
, int);
597 p
->pad_longint
= va_arg(argptr
, long int);
600 case wxPAT_LONGLONGINT
:
601 p
->pad_longlongint
= va_arg(argptr
, wxLongLong_t
);
603 #endif // wxLongLong_t
605 p
->pad_sizet
= va_arg(argptr
, size_t);
608 p
->pad_double
= va_arg(argptr
, double);
610 case wxPAT_LONGDOUBLE
:
611 p
->pad_longdouble
= va_arg(argptr
, long double);
614 p
->pad_pointer
= va_arg(argptr
, void *);
618 p
->pad_char
= (char)va_arg(argptr
, int); // char is promoted to int when passed through '...'
621 p
->pad_wchar
= (wchar_t)va_arg(argptr
, int); // char is promoted to int when passed through '...'
626 p
->pad_str
= va_arg(argptr
, void *);
630 p
->pad_nint
= va_arg(argptr
, int *);
632 case wxPAT_NSHORTINT
:
633 p
->pad_nshortint
= va_arg(argptr
, short int *);
636 p
->pad_nlongint
= va_arg(argptr
, long int *);
644 return true; // loading was successful
647 template<typename CharType
>
648 int wxPrintfConvSpec
<CharType
>::Process(CharType
*buf
, size_t lenMax
, wxPrintfArg
*p
, size_t written
)
650 // buffer to avoid dynamic memory allocation each time for small strings;
651 // note that this buffer is used only to hold results of number formatting,
652 // %s directly writes user's string in buf, without using szScratch
653 char szScratch
[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
];
654 size_t lenScratch
= 0, lenCur
= 0;
656 #define APPEND_CH(ch) \
658 if ( lenCur == lenMax ) \
661 buf[lenCur++] = ch; \
667 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_int
);
671 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longint
);
675 case wxPAT_LONGLONGINT
:
676 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longlongint
);
678 #endif // SIZEOF_LONG_LONG
681 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_sizet
);
684 case wxPAT_LONGDOUBLE
:
685 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_longdouble
);
689 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_double
);
693 lenScratch
= system_sprintf(szScratch
, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
, m_szFlags
, p
->pad_pointer
);
700 if (m_type
== wxPAT_CHAR
)
702 else // m_type == wxPAT_WCHAR
710 for (i
= 1; i
< (size_t)m_nMinWidth
; i
++)
716 for (i
= 1; i
< (size_t)m_nMinWidth
; i
++)
724 wxArgNormalizedString
arg(p
->pad_str
);
727 if ( !arg
.IsValid() && m_nMaxWidth
>= 6 )
730 typename wxPrintfStringHelper
<CharType
>::ConvertedType
strbuf(
731 wxPrintfStringHelper
<CharType
>::Convert(s
));
733 // at this point we are sure that m_nMaxWidth is positive or
734 // null (see top of wxPrintfConvSpec::LoadArg)
735 int len
= wxMin((unsigned int)m_nMaxWidth
, wxStrlen(strbuf
));
741 for (i
= len
; i
< m_nMinWidth
; i
++)
745 len
= wxMin((unsigned int)len
, lenMax
-lenCur
);
746 wxStrncpy(buf
+lenCur
, strbuf
, len
);
751 for (i
= len
; i
< m_nMinWidth
; i
++)
758 *p
->pad_nint
= written
;
761 case wxPAT_NSHORTINT
:
762 *p
->pad_nshortint
= (short int)written
;
766 *p
->pad_nlongint
= written
;
774 // if we used system's sprintf() then we now need to append the s_szScratch
775 // buffer to the given one...
781 case wxPAT_LONGLONGINT
:
784 case wxPAT_LONGDOUBLE
:
787 wxASSERT(lenScratch
< wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN
);
788 // NB: 1) we can compare lenMax (for CharType*, i.e. possibly
789 // wchar_t*) with lenScratch (char*) because this code is
790 // formatting integers and that will have the same length
791 // even in UTF-8 (the only case when char* length may be
792 // more than wchar_t* length of the same string)
793 // 2) wxStrncpy converts the 2nd argument to 1st argument's
794 // type transparently if their types differ, so this code
795 // works for both instantiations
796 if (lenMax
< lenScratch
)
798 // fill output buffer and then return -1
799 wxStrncpy(buf
, szScratch
, lenMax
);
802 wxStrncpy(buf
, szScratch
, lenScratch
);
803 lenCur
+= lenScratch
;
807 break; // all other cases were completed previously
813 // Copy chars from source to dest converting '%%' to '%'. Takes at most maxIn
814 // chars from source and write at most outMax chars to dest, returns the
815 // number of chars actually written. Does not treat null specially.
816 template<typename CharType
>
817 static int wxCopyStrWithPercents(
821 const CharType
*source
)
829 for ( i
= 0; i
< maxIn
-1 && written
< maxOut
; source
++, i
++)
831 dest
[written
++] = *source
;
832 if (*(source
+1) == wxT('%'))
834 // skip this additional '%' character
840 if (i
< maxIn
&& written
< maxOut
)
841 // copy last character inconditionally
842 dest
[written
++] = *source
;
847 template<typename CharType
>
848 static int wxDoVsnprintf(CharType
*buf
, size_t lenMax
,
849 const CharType
*format
, va_list argptr
)
851 // useful for debugging, to understand if we are really using this function
852 // rather than the system implementation
854 wprintf(L
"Using wxCRT_VsnprintfW\n");
858 wxPrintfConvSpec
<CharType
> arg
[wxMAX_SVNPRINTF_ARGUMENTS
];
859 wxPrintfArg argdata
[wxMAX_SVNPRINTF_ARGUMENTS
];
860 wxPrintfConvSpec
<CharType
> *pspec
[wxMAX_SVNPRINTF_ARGUMENTS
] = { NULL
};
864 // number of characters in the buffer so far, must be less than lenMax
868 const CharType
*toparse
= format
;
870 // parse the format string
871 bool posarg_present
= false, nonposarg_present
= false;
872 for (; *toparse
!= wxT('\0'); toparse
++)
874 if (*toparse
== wxT('%') )
878 // let's see if this is a (valid) conversion specifier...
879 if (arg
[nargs
].Parse(toparse
))
882 wxPrintfConvSpec
<CharType
> *current
= &arg
[nargs
];
884 // make toparse point to the end of this specifier
885 toparse
= current
->m_pArgEnd
;
887 if (current
->m_pos
> 0)
889 // the positionals start from number 1... adjust the index
891 posarg_present
= true;
895 // not a positional argument...
896 current
->m_pos
= nargs
;
897 nonposarg_present
= true;
900 // this conversion specifier is tied to the pos-th argument...
901 pspec
[current
->m_pos
] = current
;
904 if (nargs
== wxMAX_SVNPRINTF_ARGUMENTS
)
906 wxLogDebug(wxT("A single call to wxVsnprintf() has more than %d arguments; ")
907 wxT("ignoring all remaining arguments."), wxMAX_SVNPRINTF_ARGUMENTS
);
908 break; // cannot handle any additional conv spec
913 // it's safe to look in the next character of toparse as at worst
915 if (*(toparse
+1) == wxT('%'))
916 toparse
++; // the Parse() returned false because we've found a %%
921 if (posarg_present
&& nonposarg_present
)
924 return -1; // format strings with both positional and
925 } // non-positional conversion specifier are unsupported !!
927 // on platforms where va_list is an array type, it is necessary to make a
928 // copy to be able to pass it to LoadArg as a reference.
931 wxVaCopy(ap
, argptr
);
933 // now load arguments from stack
934 for (i
=0; i
< nargs
&& ok
; i
++)
936 // !pspec[i] means that the user forgot a positional parameter (e.g. %$1s %$3s);
937 // LoadArg == false means that wxPrintfConvSpec::Parse failed to set the
938 // conversion specifier 'type' to a valid value...
939 ok
= pspec
[i
] && pspec
[i
]->LoadArg(&argdata
[i
], ap
);
944 // something failed while loading arguments from the variable list...
945 // (e.g. the user repeated twice the same positional argument)
952 // finally, process each conversion specifier with its own argument
954 for (i
=0; i
< nargs
; i
++)
956 // copy in the output buffer the portion of the format string between
957 // last specifier and the current one
958 size_t tocopy
= ( arg
[i
].m_pArgPos
- toparse
);
960 lenCur
+= wxCopyStrWithPercents(lenMax
- lenCur
, buf
+ lenCur
,
962 if (lenCur
== lenMax
)
965 return lenMax
+1; // not enough space in the output buffer !
968 // process this specifier directly in the output buffer
969 int n
= arg
[i
].Process(buf
+lenCur
, lenMax
- lenCur
, &argdata
[arg
[i
].m_pos
], lenCur
);
972 buf
[lenMax
-1] = wxT('\0'); // be sure to always NUL-terminate the string
973 return lenMax
+1; // not enough space in the output buffer !
977 // the +1 is because wxPrintfConvSpec::m_pArgEnd points to the last character
978 // of the format specifier, but we are not interested to it...
979 toparse
= arg
[i
].m_pArgEnd
+ 1;
982 // copy portion of the format string after last specifier
983 // NOTE: toparse is pointing to the character just after the last processed
984 // conversion specifier
985 // NOTE2: the +1 is because we want to copy also the '\0'
986 size_t tocopy
= wxStrlen(format
) + 1 - ( toparse
- format
) ;
988 lenCur
+= wxCopyStrWithPercents(lenMax
- lenCur
, buf
+ lenCur
,
989 tocopy
, toparse
) - 1;
993 return lenMax
+1; // not enough space in the output buffer !
997 // wxASSERT(lenCur == wxStrlen(buf));
998 // in fact if we embedded NULLs in the output buffer (using %c with a '\0')
999 // such check would fail
1007 } // anonymous namespace
1009 #endif // !defined(wxCRT_VsnprintfW) || !defined(wxCRT_VsnprintfA)
1011 // ----------------------------------------------------------------------------
1013 // ----------------------------------------------------------------------------
1015 #if !defined(wxCRT_VsnprintfW)
1017 #if !wxUSE_WXVSNPRINTFW
1018 #error "wxUSE_WXVSNPRINTFW must be 1 if our wxCRT_VsnprintfW is used"
1021 int wxCRT_VsnprintfW(wchar_t *buf
, size_t len
,
1022 const wchar_t *format
, va_list argptr
)
1024 return wxDoVsnprintf(buf
, len
, format
, argptr
);
1027 #else // wxCRT_VsnprintfW is defined
1029 #if wxUSE_WXVSNPRINTFW
1030 #error "wxUSE_WXVSNPRINTFW must be 0 if our wxCRT_VsnprintfW is not used"
1033 #endif // !wxCRT_VsnprintfW
1035 // ----------------------------------------------------------------------------
1037 // ----------------------------------------------------------------------------
1039 #ifndef wxCRT_VsnprintfA
1041 #if !wxUSE_WXVSNPRINTFA
1042 #error "wxUSE_WXVSNPRINTFA must be 1 if our wxCRT_VsnprintfA is used"
1045 int wxCRT_VsnprintfA(char *buf
, size_t len
,
1046 const char *format
, va_list argptr
)
1048 return wxDoVsnprintf(buf
, len
, format
, argptr
);
1051 #else // wxCRT_VsnprintfA is defined
1053 #if wxUSE_WXVSNPRINTFA
1054 #error "wxUSE_WXVSNPRINTFA must be 0 if our wxCRT_VsnprintfA is not used"
1057 #endif // !wxCRT_VsnprintfA