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"
24 #include "wx/string.h"
26 #include "wx/utils.h" // for wxMin and wxMax
30 #include "wx/private/wxprintf.h"
33 // ============================================================================
34 // printf() implementation
35 // ============================================================================
37 // special test mode: define all functions below even if we don't really need
38 // them to be able to test them
40 #undef wxCRT_VsnprintfW
41 #undef wxCRT_VsnprintfA
44 // ----------------------------------------------------------------------------
45 // implement [v]snprintf() if the system doesn't provide a safe one
46 // or if the system's one does not support positional parameters
47 // (very useful for i18n purposes)
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
51 // common code for both ANSI and Unicode versions
52 // ----------------------------------------------------------------------------
54 #if !defined(wxCRT_VsnprintfW) || !defined(wxCRT_VsnprintfA)
57 // Copy chars from source to dest converting '%%' to '%'. Takes at most maxIn
58 // chars from source and write at most outMax chars to dest, returns the
59 // number of chars actually written. Does not treat null specially.
60 template<typename CharType
>
61 static int wxCopyStrWithPercents(
65 const CharType
*source
)
73 for ( i
= 0; i
< maxIn
-1 && written
< maxOut
; source
++, i
++)
75 dest
[written
++] = *source
;
76 if (*(source
+1) == wxT('%'))
78 // skip this additional '%' character
84 if (i
< maxIn
&& written
< maxOut
)
85 // copy last character inconditionally
86 dest
[written
++] = *source
;
91 template<typename CharType
>
92 static int wxDoVsnprintf(CharType
*buf
, size_t lenMax
,
93 const CharType
*format
, va_list argptr
)
95 // useful for debugging, to understand if we are really using this function
96 // rather than the system implementation
98 wprintf(L
"Using wxCRT_VsnprintfW\n");
101 wxPrintfConvSpecParser
<CharType
> parser(format
);
103 wxPrintfArg argdata
[wxMAX_SVNPRINTF_ARGUMENTS
];
107 // number of characters in the buffer so far, must be less than lenMax
110 if (parser
.posarg_present
&& parser
.nonposarg_present
)
113 return -1; // format strings with both positional and
114 } // non-positional conversion specifier are unsupported !!
116 // on platforms where va_list is an array type, it is necessary to make a
117 // copy to be able to pass it to LoadArg as a reference.
120 wxVaCopy(ap
, argptr
);
122 // now load arguments from stack
123 for (i
=0; i
< parser
.nargs
&& ok
; i
++)
125 // !pspec[i] means that the user forgot a positional parameter (e.g. %$1s %$3s);
126 // LoadArg == false means that wxPrintfConvSpec::Parse failed to set the
127 // conversion specifier 'type' to a valid value...
128 ok
= parser
.pspec
[i
] && parser
.pspec
[i
]->LoadArg(&argdata
[i
], ap
);
133 // something failed while loading arguments from the variable list...
134 // (e.g. the user repeated twice the same positional argument)
141 // finally, process each conversion specifier with its own argument
142 const CharType
*toparse
= format
;
143 for (i
=0; i
< parser
.nargs
; i
++)
145 // copy in the output buffer the portion of the format string between
146 // last specifier and the current one
147 size_t tocopy
= ( parser
.arg
[i
].m_pArgPos
- toparse
);
149 lenCur
+= wxCopyStrWithPercents(lenMax
- lenCur
, buf
+ lenCur
,
151 if (lenCur
== lenMax
)
154 return lenMax
+1; // not enough space in the output buffer !
157 // process this specifier directly in the output buffer
158 int n
= parser
.arg
[i
].Process(buf
+lenCur
, lenMax
- lenCur
,
159 &argdata
[parser
.arg
[i
].m_pos
], lenCur
);
162 buf
[lenMax
-1] = wxT('\0'); // be sure to always NUL-terminate the string
163 return lenMax
+1; // not enough space in the output buffer !
167 // the +1 is because wxPrintfConvSpec::m_pArgEnd points to the last character
168 // of the format specifier, but we are not interested to it...
169 toparse
= parser
.arg
[i
].m_pArgEnd
+ 1;
172 // copy portion of the format string after last specifier
173 // NOTE: toparse is pointing to the character just after the last processed
174 // conversion specifier
175 // NOTE2: the +1 is because we want to copy also the '\0'
176 size_t tocopy
= wxStrlen(format
) + 1 - ( toparse
- format
) ;
178 lenCur
+= wxCopyStrWithPercents(lenMax
- lenCur
, buf
+ lenCur
,
179 tocopy
, toparse
) - 1;
183 return lenMax
+1; // not enough space in the output buffer !
187 // wxASSERT(lenCur == wxStrlen(buf));
188 // in fact if we embedded NULLs in the output buffer (using %c with a '\0')
189 // such check would fail
194 #endif // !defined(wxCRT_VsnprintfW) || !defined(wxCRT_VsnprintfA)
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
200 #if !defined(wxCRT_VsnprintfW)
202 #if !wxUSE_WXVSNPRINTFW
203 #error "wxUSE_WXVSNPRINTFW must be 1 if our wxCRT_VsnprintfW is used"
206 int wxCRT_VsnprintfW(wchar_t *buf
, size_t len
,
207 const wchar_t *format
, va_list argptr
)
209 return wxDoVsnprintf(buf
, len
, format
, argptr
);
212 #else // wxCRT_VsnprintfW is defined
214 #if wxUSE_WXVSNPRINTFW
215 #error "wxUSE_WXVSNPRINTFW must be 0 if our wxCRT_VsnprintfW is not used"
218 #endif // !wxCRT_VsnprintfW
220 // ----------------------------------------------------------------------------
222 // ----------------------------------------------------------------------------
224 #ifndef wxCRT_VsnprintfA
226 #if !wxUSE_WXVSNPRINTFA
227 #error "wxUSE_WXVSNPRINTFA must be 1 if our wxCRT_VsnprintfA is used"
230 int wxCRT_VsnprintfA(char *buf
, size_t len
,
231 const char *format
, va_list argptr
)
233 return wxDoVsnprintf(buf
, len
, format
, argptr
);
236 #else // wxCRT_VsnprintfA is defined
238 #if wxUSE_WXVSNPRINTFA
239 #error "wxUSE_WXVSNPRINTFA must be 0 if our wxCRT_VsnprintfA is not used"
242 #endif // !wxCRT_VsnprintfA