]>
Commit | Line | Data |
---|---|---|
c9e089e9 | 1 | ///////////////////////////////////////////////////////////////////////////// |
41e155b4 | 2 | // Name: src/common/wxchar.cpp |
c9e089e9 | 3 | // Purpose: wxChar implementation |
247c23b4 VZ |
4 | // Author: Ove Kaven |
5 | // Modified by: Ron Lee, Francesco Montorsi | |
c9e089e9 OK |
6 | // Created: 09/04/99 |
7 | // RCS-ID: $Id$ | |
77ffb593 | 8 | // Copyright: (c) wxWidgets copyright |
65571936 | 9 | // Licence: wxWindows licence |
c9e089e9 OK |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
c9e089e9 OK |
12 | // =========================================================================== |
13 | // headers, declarations, constants | |
14 | // =========================================================================== | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
8898456d | 20 | #pragma hdrstop |
c9e089e9 OK |
21 | #endif |
22 | ||
5ff14574 PC |
23 | #include "wx/wxchar.h" |
24 | ||
4400c1a4 OK |
25 | #define _ISOC9X_SOURCE 1 // to get vsscanf() |
26 | #define _BSD_SOURCE 1 // to still get strdup() | |
27 | ||
84fff0b3 | 28 | #include <stdio.h> |
c9e089e9 OK |
29 | #include <stdlib.h> |
30 | #include <string.h> | |
1c193821 JS |
31 | |
32 | #ifndef __WXWINCE__ | |
8898456d WS |
33 | #include <time.h> |
34 | #include <locale.h> | |
1c193821 | 35 | #else |
8898456d | 36 | #include "wx/msw/wince/time.h" |
1c193821 | 37 | #endif |
c9e089e9 OK |
38 | |
39 | #ifndef WX_PRECOMP | |
8898456d WS |
40 | #include "wx/string.h" |
41 | #include "wx/hash.h" | |
5ff14574 PC |
42 | #include "wx/utils.h" // for wxMin and wxMax |
43 | #include "wx/log.h" | |
c9e089e9 OK |
44 | #endif |
45 | ||
57f6da0d | 46 | #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H) |
b3e8d00a | 47 | #include <windef.h> |
8898456d WS |
48 | #include <winbase.h> |
49 | #include <winnls.h> | |
50 | #include <winnt.h> | |
57f6da0d OK |
51 | #endif |
52 | ||
31907d03 | 53 | #if defined(__MWERKS__) && __MSL__ >= 0x6000 |
52cbcda3 | 54 | namespace std {} |
31907d03 SC |
55 | using namespace std ; |
56 | #endif | |
57 | ||
d0bdc3ca | 58 | #if wxUSE_WCHAR_T |
434d2cb3 | 59 | size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n) |
c9e089e9 | 60 | { |
2b5f62a0 | 61 | // assume that we have mbsrtowcs() too if we have wcsrtombs() |
fe190003 | 62 | #ifdef HAVE_WCSRTOMBS |
2b5f62a0 VZ |
63 | mbstate_t mbstate; |
64 | memset(&mbstate, 0, sizeof(mbstate_t)); | |
65 | #endif | |
66 | ||
c9e089e9 | 67 | if (buf) { |
923d3156 | 68 | if (!n || !*psz) { |
223d09f6 | 69 | if (n) *buf = wxT('\0'); |
923d3156 OK |
70 | return 0; |
71 | } | |
2b5f62a0 VZ |
72 | #ifdef HAVE_WCSRTOMBS |
73 | return mbsrtowcs(buf, &psz, n, &mbstate); | |
74 | #else | |
1e96e503 | 75 | return wxMbstowcs(buf, psz, n); |
2b5f62a0 | 76 | #endif |
c9e089e9 OK |
77 | } |
78 | ||
0d8c57c0 VZ |
79 | // note that we rely on common (and required by Unix98 but unfortunately not |
80 | // C99) extension which allows to call mbs(r)towcs() with NULL output pointer | |
81 | // to just get the size of the needed buffer -- this is needed as otherwise | |
82 | // we have no idea about how much space we need and if the CRT doesn't | |
83 | // support it (the only currently known example being Metrowerks, see | |
84 | // wx/wxchar.h) we don't use its mbstowcs() at all | |
b3e8d00a | 85 | #ifdef HAVE_WCSRTOMBS |
c9e089e9 | 86 | return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate); |
2b5f62a0 | 87 | #else |
1e96e503 | 88 | return wxMbstowcs((wchar_t *) NULL, psz, 0); |
2b5f62a0 | 89 | #endif |
c9e089e9 OK |
90 | } |
91 | ||
434d2cb3 | 92 | size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n) |
c9e089e9 | 93 | { |
fe190003 | 94 | #ifdef HAVE_WCSRTOMBS |
2b5f62a0 VZ |
95 | mbstate_t mbstate; |
96 | memset(&mbstate, 0, sizeof(mbstate_t)); | |
97 | #endif | |
98 | ||
c9e089e9 | 99 | if (buf) { |
923d3156 OK |
100 | if (!n || !*pwz) { |
101 | // glibc2.1 chokes on null input | |
102 | if (n) *buf = '\0'; | |
103 | return 0; | |
104 | } | |
fe190003 | 105 | #ifdef HAVE_WCSRTOMBS |
2b5f62a0 VZ |
106 | return wcsrtombs(buf, &pwz, n, &mbstate); |
107 | #else | |
1e96e503 | 108 | return wxWcstombs(buf, pwz, n); |
2b5f62a0 | 109 | #endif |
c9e089e9 OK |
110 | } |
111 | ||
fe190003 | 112 | #ifdef HAVE_WCSRTOMBS |
c9e089e9 | 113 | return wcsrtombs((char *) NULL, &pwz, 0, &mbstate); |
2b5f62a0 | 114 | #else |
1e96e503 | 115 | return wxWcstombs((char *) NULL, pwz, 0); |
2b5f62a0 | 116 | #endif |
c9e089e9 | 117 | } |
b3e8d00a | 118 | #endif // wxUSE_WCHAR_T |
c9e089e9 | 119 | |
434d2cb3 OK |
120 | bool WXDLLEXPORT wxOKlibc() |
121 | { | |
5283098e | 122 | #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__) |
66b3ec7f OK |
123 | // glibc 2.0 uses UTF-8 even when it shouldn't |
124 | wchar_t res = 0; | |
434d2cb3 | 125 | if ((MB_CUR_MAX == 2) && |
66b3ec7f | 126 | (wxMB2WC(&res, "\xdd\xa5", 1) == 1) && |
434d2cb3 OK |
127 | (res==0x765)) { |
128 | // this is UTF-8 allright, check whether that's what we want | |
66b3ec7f | 129 | char *cur_locale = setlocale(LC_CTYPE, NULL); |
434d2cb3 | 130 | if ((strlen(cur_locale) < 4) || |
f6f5941b VZ |
131 | (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8")) || |
132 | (strcasecmp(cur_locale + strlen(cur_locale) - 5, "utf-8"))) { | |
434d2cb3 | 133 | // nope, don't use libc conversion |
cab1a605 | 134 | return false; |
434d2cb3 OK |
135 | } |
136 | } | |
137 | #endif | |
cab1a605 | 138 | return true; |
434d2cb3 OK |
139 | } |
140 | ||
f6f5941b VZ |
141 | // ============================================================================ |
142 | // printf() functions business | |
143 | // ============================================================================ | |
144 | ||
df17b887 VZ |
145 | // special test mode: define all functions below even if we don't really need |
146 | // them to be able to test them | |
147 | #ifdef wxTEST_PRINTF | |
148 | #undef wxFprintf | |
149 | #undef wxPrintf | |
150 | #undef wxSprintf | |
151 | #undef wxVfprintf | |
152 | #undef wxVsprintf | |
153 | #undef wxVprintf | |
154 | #undef wxVsnprintf_ | |
155 | #undef wxSnprintf_ | |
156 | ||
157 | #define wxNEED_WPRINTF | |
158 | ||
159 | int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr ); | |
160 | #endif | |
161 | ||
f6f5941b VZ |
162 | // ---------------------------------------------------------------------------- |
163 | // implement [v]snprintf() if the system doesn't provide a safe one | |
7a828c7f VZ |
164 | // or if the system's one does not support positional parameters |
165 | // (very useful for i18n purposes) | |
f6f5941b VZ |
166 | // ---------------------------------------------------------------------------- |
167 | ||
168 | #if !defined(wxVsnprintf_) | |
7a828c7f VZ |
169 | |
170 | // wxUSE_STRUTILS says our wxVsnprintf_ implementation to use or not to | |
171 | // use wxStrlen and wxStrncpy functions over one-char processing loops. | |
172 | // | |
173 | // Some benchmarking revealed that wxUSE_STRUTILS == 1 has the following | |
174 | // effects: | |
175 | // -> on Windows: | |
176 | // when in ANSI mode, this setting does not change almost anything | |
177 | // when in Unicode mode, it gives ~ 50% of slowdown ! | |
178 | // -> on Linux: | |
179 | // both in ANSI and Unicode mode it gives ~ 60% of speedup ! | |
180 | // | |
181 | #if defined(WIN32) && wxUSE_UNICODE | |
182 | #define wxUSE_STRUTILS 0 | |
183 | #else | |
184 | #define wxUSE_STRUTILS 1 | |
185 | #endif | |
186 | ||
187 | // some limits of our implementation | |
412a5c57 | 188 | #define wxMAX_SVNPRINTF_ARGUMENTS 16 |
7a828c7f | 189 | #define wxMAX_SVNPRINTF_FLAGBUFFER_LEN 32 |
297eb8e6 RR |
190 | #define wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN 512 |
191 | ||
192 | ||
193 | // wxVsnprintf() needs to use a *system* implementation of swnprintf() | |
194 | // in order to perform some internal tasks. | |
195 | // NB: we cannot just use wxSnprintf() because for some systems it maybe | |
196 | // implemented later in this file using wxVsnprintf() and that would | |
197 | // result in an endless recursion and thus in a stack overflow | |
198 | #if wxUSE_UNICODE | |
199 | ||
200 | #if defined(__WXWINCE__) || ( defined(__VISUALC__) && __VISUALC__ <= 1200 ) | |
201 | #define HAVE_BROKEN_SWPRINTF_DECL | |
202 | #endif | |
203 | ||
204 | ||
205 | // problem: on some systems swprintf takes the 'max' argument while on others | |
206 | // it doesn't | |
207 | #if defined(HAVE_BROKEN_SWPRINTF_DECL) | |
208 | ||
209 | // like when using sprintf(), since 'max' is not used, wxVsnprintf() should | |
210 | // always ensure that 'buff' is big enough for all common needs | |
211 | #define system_sprintf(buff, max, flags, data) \ | |
212 | ::swprintf(buff, flags, data) | |
213 | #else | |
214 | ||
215 | #if !defined(HAVE_SWPRINTF) | |
216 | #error wxVsnprintf() needs a system swprintf() implementation! | |
217 | #endif | |
218 | ||
219 | #define system_sprintf(buff, max, flags, data) \ | |
220 | ::swprintf(buff, max, flags, data) | |
221 | #endif | |
222 | ||
223 | #else | |
224 | ||
225 | #if defined(HAVE_SNPRINTF) | |
226 | ||
227 | #define system_sprintf(buff, max, flags, data) \ | |
228 | ::snprintf(buff, max, flags, data) | |
229 | ||
230 | #else // NB: at least sprintf() should *always* be available | |
231 | ||
232 | // since 'max' is not used in this case, wxVsnprintf() should always ensure | |
233 | // that 'buff' is big enough for all common needs | |
234 | // (see wxMAX_SVNPRINTF_FLAGBUFFER_LEN and wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN) | |
235 | #define system_sprintf(buff, max, flags, data) \ | |
236 | ::sprintf(buff, flags, data) | |
237 | ||
238 | #endif | |
239 | #endif | |
240 | ||
241 | ||
7a828c7f | 242 | |
7d70c309 | 243 | // the conversion specifiers accepted by wxVsnprintf_ |
7a828c7f VZ |
244 | enum wxPrintfArgType { |
245 | wxPAT_INVALID = -1, | |
246 | ||
247 | wxPAT_INT, // %d, %i, %o, %u, %x, %X | |
248 | wxPAT_LONGINT, // %ld, etc | |
249 | #if SIZEOF_LONG_LONG | |
250 | wxPAT_LONGLONGINT, // %Ld, etc | |
251 | #endif | |
252 | wxPAT_SIZET, // %Zd, etc | |
253 | ||
254 | wxPAT_DOUBLE, // %e, %E, %f, %g, %G | |
255 | wxPAT_LONGDOUBLE, // %le, etc | |
256 | ||
257 | wxPAT_POINTER, // %p | |
258 | ||
259 | wxPAT_CHAR, // %hc (in ANSI mode: %c, too) | |
260 | wxPAT_WCHAR, // %lc (in Unicode mode: %c, too) | |
261 | ||
262 | wxPAT_PCHAR, // %s (related to a char *) | |
263 | wxPAT_PWCHAR, // %s (related to a wchar_t *) | |
264 | ||
265 | wxPAT_NINT, // %n | |
266 | wxPAT_NSHORTINT, // %hn | |
267 | wxPAT_NLONGINT // %ln | |
268 | }; | |
269 | ||
7d70c309 | 270 | // an argument passed to wxVsnprintf_ |
7a828c7f VZ |
271 | typedef union { |
272 | int pad_int; // %d, %i, %o, %u, %x, %X | |
273 | long int pad_longint; // %ld, etc | |
274 | #if SIZEOF_LONG_LONG | |
275 | long long int pad_longlongint; // %Ld, etc | |
276 | #endif | |
277 | size_t pad_sizet; // %Zd, etc | |
278 | ||
279 | double pad_double; // %e, %E, %f, %g, %G | |
280 | long double pad_longdouble; // %le, etc | |
281 | ||
282 | void *pad_pointer; // %p | |
283 | ||
284 | char pad_char; // %hc (in ANSI mode: %c, too) | |
285 | wchar_t pad_wchar; // %lc (in Unicode mode: %c, too) | |
286 | ||
287 | char *pad_pchar; // %s (related to a char *) | |
288 | wchar_t *pad_pwchar; // %s (related to a wchar_t *) | |
289 | ||
290 | int *pad_nint; // %n | |
291 | short int *pad_nshortint; // %hn | |
292 | long int *pad_nlongint; // %ln | |
293 | } wxPrintfArg; | |
294 | ||
295 | ||
296 | // Contains parsed data relative to a conversion specifier given to | |
7d70c309 | 297 | // wxVsnprintf_ and parsed from the format string |
7a828c7f VZ |
298 | // NOTE: in C++ there is almost no difference between struct & classes thus |
299 | // there is no performance gain by using a struct here... | |
300 | class wxPrintfConvSpec | |
f6f5941b | 301 | { |
7a828c7f | 302 | public: |
f6f5941b | 303 | |
7a828c7f | 304 | // the position of the argument relative to this conversion specifier |
412a5c57 | 305 | size_t m_pos; |
7a828c7f VZ |
306 | |
307 | // the type of this conversion specifier | |
412a5c57 | 308 | wxPrintfArgType m_type; |
7a828c7f VZ |
309 | |
310 | // the minimum and maximum width | |
311 | // when one of this var is set to -1 it means: use the following argument | |
312 | // in the stack as minimum/maximum width for this conversion specifier | |
412a5c57 | 313 | int m_nMinWidth, m_nMaxWidth; |
5f1d3069 | 314 | |
7a828c7f | 315 | // does the argument need to the be aligned to left ? |
412a5c57 | 316 | bool m_bAlignLeft; |
7a828c7f VZ |
317 | |
318 | // pointer to the '%' of this conversion specifier in the format string | |
319 | // NOTE: this points somewhere in the string given to the Parse() function - | |
320 | // it's task of the caller ensure that memory is still valid ! | |
412a5c57 | 321 | const wxChar *m_pArgPos; |
7a828c7f VZ |
322 | |
323 | // pointer to the last character of this conversion specifier in the | |
324 | // format string | |
325 | // NOTE: this points somewhere in the string given to the Parse() function - | |
326 | // it's task of the caller ensure that memory is still valid ! | |
412a5c57 | 327 | const wxChar *m_pArgEnd; |
7a828c7f VZ |
328 | |
329 | // a little buffer where formatting flags like #+\.hlqLZ are stored by Parse() | |
330 | // for use in Process() | |
412a5c57 VZ |
331 | // NB: even if this buffer is used only for numeric conversion specifiers and |
332 | // thus could be safely declared as a char[] buffer, we want it to be wxChar | |
333 | // so that in Unicode builds we can avoid to convert its contents to Unicode | |
334 | // chars when copying it in user's buffer. | |
335 | wxChar m_szFlags[wxMAX_SVNPRINTF_FLAGBUFFER_LEN]; | |
7a828c7f VZ |
336 | |
337 | ||
338 | public: | |
339 | ||
340 | // we don't declare this as a constructor otherwise it would be called | |
7d70c309 | 341 | // automatically and we don't want this: to be optimized, wxVsnprintf_ |
7a828c7f VZ |
342 | // calls this function only on really-used instances of this class. |
343 | void Init(); | |
344 | ||
345 | // Parses the first conversion specifier in the given string, which must | |
346 | // begin with a '%'. Returns false if the first '%' does not introduce a | |
347 | // (valid) conversion specifier and thus should be ignored. | |
348 | bool Parse(const wxChar *format); | |
349 | ||
350 | // Process this conversion specifier and puts the result in the given | |
351 | // buffer. Returns the number of characters written in 'buf' or -1 if | |
352 | // there's not enough space. | |
353 | int Process(wxChar *buf, size_t lenMax, wxPrintfArg *p); | |
354 | ||
355 | // Loads the argument of this conversion specifier from given va_list. | |
356 | bool LoadArg(wxPrintfArg *p, va_list &argptr); | |
357 | ||
358 | private: | |
359 | // An helper function of LoadArg() which is used to handle the '*' flag | |
360 | void ReplaceAsteriskWith(int w); | |
361 | }; | |
362 | ||
363 | void wxPrintfConvSpec::Init() | |
364 | { | |
412a5c57 VZ |
365 | m_nMinWidth = 0; |
366 | m_nMaxWidth = 0xFFFF; | |
367 | m_pos = 0; | |
368 | m_bAlignLeft = false; | |
369 | m_pArgPos = m_pArgEnd = NULL; | |
370 | m_type = wxPAT_INVALID; | |
371 | ||
372 | // this character will never be removed from m_szFlags array and | |
7d70c309 | 373 | // is important when calling sprintf() in wxPrintfConvSpec::Process() ! |
412a5c57 | 374 | m_szFlags[0] = wxT('%'); |
7a828c7f VZ |
375 | } |
376 | ||
377 | bool wxPrintfConvSpec::Parse(const wxChar *format) | |
378 | { | |
379 | bool done = false; | |
380 | ||
381 | // temporary parse data | |
382 | size_t flagofs = 1; | |
383 | bool in_prec, prec_dot; | |
384 | int ilen = 0; | |
385 | ||
412a5c57 VZ |
386 | m_bAlignLeft = in_prec = prec_dot = false; |
387 | m_pArgPos = m_pArgEnd = format; | |
7a828c7f | 388 | do |
f6f5941b | 389 | { |
7a828c7f VZ |
390 | #define CHECK_PREC \ |
391 | if (in_prec && !prec_dot) \ | |
392 | { \ | |
412a5c57 | 393 | m_szFlags[flagofs++] = '.'; \ |
7a828c7f VZ |
394 | prec_dot = true; \ |
395 | } | |
df17b887 | 396 | |
7a828c7f | 397 | // what follows '%'? |
412a5c57 | 398 | const wxChar ch = *(++m_pArgEnd); |
7a828c7f | 399 | switch ( ch ) |
df17b887 | 400 | { |
7a828c7f VZ |
401 | case wxT('\0'): |
402 | return false; // not really an argument | |
403 | ||
404 | case wxT('%'): | |
405 | return false; // not really an argument | |
406 | ||
407 | case wxT('#'): | |
408 | case wxT('0'): | |
409 | case wxT(' '): | |
410 | case wxT('+'): | |
411 | case wxT('\''): | |
412 | CHECK_PREC | |
412a5c57 | 413 | m_szFlags[flagofs++] = ch; |
7a828c7f VZ |
414 | break; |
415 | ||
416 | case wxT('-'): | |
417 | CHECK_PREC | |
412a5c57 VZ |
418 | m_bAlignLeft = true; |
419 | m_szFlags[flagofs++] = ch; | |
7a828c7f VZ |
420 | break; |
421 | ||
422 | case wxT('.'): | |
423 | CHECK_PREC | |
424 | in_prec = true; | |
425 | prec_dot = false; | |
412a5c57 VZ |
426 | m_nMaxWidth = 0; |
427 | // dot will be auto-added to m_szFlags if non-negative | |
7a828c7f VZ |
428 | // number follows |
429 | break; | |
430 | ||
431 | case wxT('h'): | |
432 | ilen = -1; | |
433 | CHECK_PREC | |
412a5c57 | 434 | m_szFlags[flagofs++] = ch; |
7a828c7f VZ |
435 | break; |
436 | ||
437 | case wxT('l'): | |
412a5c57 VZ |
438 | // NB: it's safe to use flagofs-1 as flagofs always start from 1 |
439 | if (m_szFlags[flagofs-1] == 'l') // 'll' modifier is the same as 'L' or 'q' | |
440 | ilen = 2; | |
441 | else | |
7a828c7f VZ |
442 | ilen = 1; |
443 | CHECK_PREC | |
412a5c57 | 444 | m_szFlags[flagofs++] = ch; |
7a828c7f VZ |
445 | break; |
446 | ||
447 | case wxT('q'): | |
448 | case wxT('L'): | |
449 | ilen = 2; | |
450 | CHECK_PREC | |
412a5c57 | 451 | m_szFlags[flagofs++] = ch; |
7a828c7f VZ |
452 | break; |
453 | ||
454 | case wxT('Z'): | |
455 | ilen = 3; | |
456 | CHECK_PREC | |
412a5c57 | 457 | m_szFlags[flagofs++] = ch; |
7a828c7f VZ |
458 | break; |
459 | ||
460 | case wxT('*'): | |
461 | if (in_prec) | |
462 | { | |
463 | CHECK_PREC | |
f6f5941b | 464 | |
7a828c7f VZ |
465 | // tell Process() to use the next argument |
466 | // in the stack as maxwidth... | |
412a5c57 | 467 | m_nMaxWidth = -1; |
7a828c7f VZ |
468 | } |
469 | else | |
470 | { | |
471 | // tell Process() to use the next argument | |
472 | // in the stack as minwidth... | |
412a5c57 | 473 | m_nMinWidth = -1; |
7a828c7f VZ |
474 | } |
475 | ||
476 | // save the * in our formatting buffer... | |
477 | // will be replaced later by Process() | |
412a5c57 | 478 | m_szFlags[flagofs++] = ch; |
7a828c7f VZ |
479 | break; |
480 | ||
481 | case wxT('1'): case wxT('2'): case wxT('3'): | |
482 | case wxT('4'): case wxT('5'): case wxT('6'): | |
483 | case wxT('7'): case wxT('8'): case wxT('9'): | |
484 | { | |
485 | int len = 0; | |
486 | CHECK_PREC | |
412a5c57 VZ |
487 | while ( (*m_pArgEnd >= wxT('0')) && |
488 | (*m_pArgEnd <= wxT('9')) ) | |
7a828c7f | 489 | { |
412a5c57 VZ |
490 | m_szFlags[flagofs++] = (*m_pArgEnd); |
491 | len = len*10 + (*m_pArgEnd - wxT('0')); | |
492 | m_pArgEnd++; | |
7a828c7f VZ |
493 | } |
494 | ||
495 | if (in_prec) | |
412a5c57 | 496 | m_nMaxWidth = len; |
7a828c7f | 497 | else |
412a5c57 | 498 | m_nMinWidth = len; |
7a828c7f | 499 | |
412a5c57 | 500 | m_pArgEnd--; // the main loop pre-increments n again |
f6f5941b | 501 | } |
7a828c7f VZ |
502 | break; |
503 | ||
504 | case wxT('$'): // a positional parameter (e.g. %2$s) ? | |
505 | { | |
412a5c57 | 506 | if (m_nMinWidth <= 0) |
7a828c7f VZ |
507 | break; // ignore this formatting flag as no |
508 | // numbers are preceding it | |
509 | ||
412a5c57 | 510 | // remove from m_szFlags all digits previously added |
7a828c7f VZ |
511 | do { |
512 | flagofs--; | |
412a5c57 VZ |
513 | } while (m_szFlags[flagofs] >= '1' && |
514 | m_szFlags[flagofs] <= '9'); | |
7a828c7f VZ |
515 | |
516 | // re-adjust the offset making it point to the | |
412a5c57 | 517 | // next free char of m_szFlags |
7a828c7f VZ |
518 | flagofs++; |
519 | ||
412a5c57 VZ |
520 | m_pos = m_nMinWidth; |
521 | m_nMinWidth = 0; | |
7a828c7f VZ |
522 | } |
523 | break; | |
524 | ||
525 | case wxT('d'): | |
526 | case wxT('i'): | |
527 | case wxT('o'): | |
528 | case wxT('u'): | |
529 | case wxT('x'): | |
530 | case wxT('X'): | |
531 | CHECK_PREC | |
412a5c57 VZ |
532 | m_szFlags[flagofs++] = ch; |
533 | m_szFlags[flagofs] = '\0'; | |
7a828c7f | 534 | if (ilen == 0) |
412a5c57 | 535 | m_type = wxPAT_INT; |
7a828c7f VZ |
536 | else if (ilen == -1) |
537 | // NB: 'short int' value passed through '...' | |
538 | // is promoted to 'int', so we have to get | |
539 | // an int from stack even if we need a short | |
412a5c57 | 540 | m_type = wxPAT_INT; |
7a828c7f | 541 | else if (ilen == 1) |
412a5c57 | 542 | m_type = wxPAT_LONGINT; |
7a828c7f VZ |
543 | else if (ilen == 2) |
544 | #if SIZEOF_LONG_LONG | |
412a5c57 | 545 | m_type = wxPAT_LONGLONGINT; |
7a828c7f | 546 | #else // !long long |
412a5c57 | 547 | m_type = wxPAT_LONGINT; |
7a828c7f VZ |
548 | #endif // long long/!long long |
549 | else if (ilen == 3) | |
412a5c57 | 550 | m_type = wxPAT_SIZET; |
7a828c7f VZ |
551 | done = true; |
552 | break; | |
553 | ||
554 | case wxT('e'): | |
555 | case wxT('E'): | |
556 | case wxT('f'): | |
557 | case wxT('g'): | |
558 | case wxT('G'): | |
559 | CHECK_PREC | |
412a5c57 VZ |
560 | m_szFlags[flagofs++] = ch; |
561 | m_szFlags[flagofs] = '\0'; | |
7a828c7f | 562 | if (ilen == 2) |
412a5c57 | 563 | m_type = wxPAT_LONGDOUBLE; |
7a828c7f | 564 | else |
412a5c57 | 565 | m_type = wxPAT_DOUBLE; |
7a828c7f VZ |
566 | done = true; |
567 | break; | |
568 | ||
569 | case wxT('p'): | |
412a5c57 | 570 | m_type = wxPAT_POINTER; |
7a828c7f VZ |
571 | done = true; |
572 | break; | |
573 | ||
574 | case wxT('c'): | |
575 | if (ilen == -1) | |
576 | { | |
577 | // in Unicode mode %hc == ANSI character | |
578 | // and in ANSI mode, %hc == %c == ANSI... | |
412a5c57 | 579 | m_type = wxPAT_CHAR; |
7a828c7f VZ |
580 | } |
581 | else if (ilen == 1) | |
582 | { | |
583 | // in ANSI mode %lc == Unicode character | |
584 | // and in Unicode mode, %lc == %c == Unicode... | |
412a5c57 | 585 | m_type = wxPAT_WCHAR; |
7a828c7f VZ |
586 | } |
587 | else | |
588 | { | |
589 | #if wxUSE_UNICODE | |
590 | // in Unicode mode, %c == Unicode character | |
412a5c57 | 591 | m_type = wxPAT_WCHAR; |
7a828c7f VZ |
592 | #else |
593 | // in ANSI mode, %c == ANSI character | |
412a5c57 | 594 | m_type = wxPAT_CHAR; |
7a828c7f VZ |
595 | #endif |
596 | } | |
597 | done = true; | |
598 | break; | |
599 | ||
600 | case wxT('s'): | |
601 | if (ilen == -1) | |
602 | { | |
603 | // Unicode mode wx extension: we'll let %hs mean non-Unicode | |
604 | // strings (when in ANSI mode, %s == %hs == ANSI string) | |
412a5c57 | 605 | m_type = wxPAT_PCHAR; |
7a828c7f VZ |
606 | } |
607 | else if (ilen == 1) | |
608 | { | |
609 | // in Unicode mode, %ls == %s == Unicode string | |
610 | // in ANSI mode, %ls == Unicode string | |
412a5c57 | 611 | m_type = wxPAT_PWCHAR; |
7a828c7f VZ |
612 | } |
613 | else | |
614 | { | |
615 | #if wxUSE_UNICODE | |
412a5c57 | 616 | m_type = wxPAT_PWCHAR; |
7a828c7f | 617 | #else |
412a5c57 | 618 | m_type = wxPAT_PCHAR; |
7a828c7f VZ |
619 | #endif |
620 | } | |
621 | done = true; | |
622 | break; | |
623 | ||
624 | case wxT('n'): | |
625 | if (ilen == 0) | |
412a5c57 | 626 | m_type = wxPAT_NINT; |
7a828c7f | 627 | else if (ilen == -1) |
412a5c57 | 628 | m_type = wxPAT_NSHORTINT; |
7a828c7f | 629 | else if (ilen >= 1) |
412a5c57 | 630 | m_type = wxPAT_NLONGINT; |
7a828c7f VZ |
631 | done = true; |
632 | break; | |
633 | ||
634 | default: | |
635 | // bad format, don't consider this an argument; | |
636 | // leave it unchanged | |
637 | return false; | |
638 | } | |
412a5c57 VZ |
639 | |
640 | if (flagofs == wxMAX_SVNPRINTF_FLAGBUFFER_LEN) | |
641 | { | |
642 | wxLogDebug(wxT("Too many flags specified for a single conversion specifier!")); | |
643 | return false; | |
644 | } | |
7a828c7f VZ |
645 | } |
646 | while (!done); | |
647 | ||
648 | return true; // parsing was successful | |
649 | } | |
650 | ||
651 | ||
412a5c57 | 652 | void wxPrintfConvSpec::ReplaceAsteriskWith(int width) |
7a828c7f | 653 | { |
412a5c57 | 654 | wxChar temp[wxMAX_SVNPRINTF_FLAGBUFFER_LEN]; |
7a828c7f VZ |
655 | |
656 | // find the first * in our flag buffer | |
412a5c57 | 657 | wxChar *pwidth = wxStrchr(m_szFlags, wxT('*')); |
7a828c7f VZ |
658 | wxASSERT(pwidth); |
659 | ||
412a5c57 VZ |
660 | // save what follows the * (the +1 is to skip the asterisk itself!) |
661 | wxStrcpy(temp, pwidth+1); | |
662 | if (width < 0) | |
663 | { | |
664 | pwidth[0] = wxT('-'); | |
7a828c7f VZ |
665 | pwidth++; |
666 | } | |
667 | ||
668 | // replace * with the actual integer given as width | |
412a5c57 | 669 | int maxlen = (m_szFlags + wxMAX_SVNPRINTF_FLAGBUFFER_LEN - pwidth) / sizeof(wxChar); |
297eb8e6 | 670 | int offset = system_sprintf(pwidth, maxlen, wxT("%d"), abs(width)); |
7a828c7f VZ |
671 | |
672 | // restore after the expanded * what was following it | |
412a5c57 | 673 | wxStrcpy(pwidth+offset, temp); |
7a828c7f VZ |
674 | } |
675 | ||
676 | bool wxPrintfConvSpec::LoadArg(wxPrintfArg *p, va_list &argptr) | |
677 | { | |
678 | // did the '*' width/precision specifier was used ? | |
412a5c57 | 679 | if (m_nMaxWidth == -1) |
7a828c7f VZ |
680 | { |
681 | // take the maxwidth specifier from the stack | |
412a5c57 VZ |
682 | m_nMaxWidth = va_arg(argptr, int); |
683 | if (m_nMaxWidth < 0) | |
684 | m_nMaxWidth = 0; | |
7a828c7f | 685 | else |
412a5c57 | 686 | ReplaceAsteriskWith(m_nMaxWidth); |
7a828c7f VZ |
687 | } |
688 | ||
412a5c57 | 689 | if (m_nMinWidth == -1) |
7a828c7f VZ |
690 | { |
691 | // take the minwidth specifier from the stack | |
412a5c57 | 692 | m_nMinWidth = va_arg(argptr, int); |
7a828c7f | 693 | |
412a5c57 VZ |
694 | ReplaceAsteriskWith(m_nMinWidth); |
695 | if (m_nMinWidth < 0) | |
7a828c7f | 696 | { |
412a5c57 VZ |
697 | m_bAlignLeft = !m_bAlignLeft; |
698 | m_nMinWidth = -m_nMinWidth; | |
7a828c7f VZ |
699 | } |
700 | } | |
701 | ||
412a5c57 | 702 | switch (m_type) { |
7a828c7f VZ |
703 | case wxPAT_INT: |
704 | p->pad_int = va_arg(argptr, int); | |
705 | break; | |
706 | case wxPAT_LONGINT: | |
707 | p->pad_longint = va_arg(argptr, long int); | |
708 | break; | |
709 | #if SIZEOF_LONG_LONG | |
710 | case wxPAT_LONGLONGINT: | |
711 | p->pad_longlongint = va_arg(argptr, long long int); | |
712 | break; | |
713 | #endif | |
714 | case wxPAT_SIZET: | |
715 | p->pad_sizet = va_arg(argptr, size_t); | |
716 | break; | |
717 | case wxPAT_DOUBLE: | |
718 | p->pad_double = va_arg(argptr, double); | |
719 | break; | |
720 | case wxPAT_LONGDOUBLE: | |
721 | p->pad_longdouble = va_arg(argptr, long double); | |
722 | break; | |
723 | case wxPAT_POINTER: | |
724 | p->pad_pointer = va_arg(argptr, void *); | |
725 | break; | |
726 | ||
727 | case wxPAT_CHAR: | |
412a5c57 | 728 | p->pad_char = va_arg(argptr, int); // char is promoted to int when passed through '...' |
7a828c7f VZ |
729 | break; |
730 | case wxPAT_WCHAR: | |
7d70c309 | 731 | p->pad_wchar = (wchar_t)va_arg(argptr, int); // char is promoted to int when passed through '...' |
7a828c7f VZ |
732 | break; |
733 | ||
734 | case wxPAT_PCHAR: | |
735 | p->pad_pchar = va_arg(argptr, char *); | |
736 | break; | |
737 | case wxPAT_PWCHAR: | |
738 | p->pad_pwchar = va_arg(argptr, wchar_t *); | |
739 | break; | |
740 | ||
741 | case wxPAT_NINT: | |
742 | p->pad_nint = va_arg(argptr, int *); | |
743 | break; | |
744 | case wxPAT_NSHORTINT: | |
745 | p->pad_nshortint = va_arg(argptr, short int *); | |
746 | break; | |
747 | case wxPAT_NLONGINT: | |
748 | p->pad_nlongint = va_arg(argptr, long int *); | |
749 | break; | |
750 | ||
751 | case wxPAT_INVALID: | |
752 | default: | |
753 | return false; | |
754 | } | |
755 | ||
756 | return true; // loading was successful | |
757 | } | |
758 | ||
759 | int wxPrintfConvSpec::Process(wxChar *buf, size_t lenMax, wxPrintfArg *p) | |
760 | { | |
412a5c57 VZ |
761 | // buffer to avoid dynamic memory allocation each time for small strings; |
762 | // note that this buffer is used only to hold results of number formatting, | |
763 | // %s directly writes user's string in buf, without using szScratch | |
297eb8e6 | 764 | wxChar szScratch[wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN]; |
412a5c57 VZ |
765 | size_t lenScratch = 0, lenCur = 0; |
766 | ||
f6f5941b | 767 | #define APPEND_CH(ch) \ |
210f4bcd VS |
768 | { \ |
769 | if ( lenCur == lenMax ) \ | |
770 | return -1; \ | |
771 | \ | |
772 | buf[lenCur++] = ch; \ | |
773 | } | |
f6f5941b VZ |
774 | |
775 | #define APPEND_STR(s) \ | |
f6f5941b | 776 | { \ |
1af48460 VZ |
777 | for ( const wxChar *p = s; *p; p++ ) \ |
778 | { \ | |
779 | APPEND_CH(*p); \ | |
780 | } \ | |
f6f5941b VZ |
781 | } |
782 | ||
412a5c57 | 783 | switch ( m_type ) |
7a828c7f VZ |
784 | { |
785 | case wxPAT_INT: | |
297eb8e6 | 786 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_int); |
7a828c7f VZ |
787 | break; |
788 | ||
789 | case wxPAT_LONGINT: | |
297eb8e6 | 790 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longint); |
7a828c7f VZ |
791 | break; |
792 | ||
f6f5941b | 793 | #if SIZEOF_LONG_LONG |
7a828c7f | 794 | case wxPAT_LONGLONGINT: |
297eb8e6 | 795 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longlongint); |
7a828c7f VZ |
796 | break; |
797 | #endif // SIZEOF_LONG_LONG | |
798 | ||
799 | case wxPAT_SIZET: | |
297eb8e6 | 800 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_sizet); |
7a828c7f VZ |
801 | break; |
802 | ||
803 | case wxPAT_LONGDOUBLE: | |
297eb8e6 | 804 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_longdouble); |
7a828c7f VZ |
805 | break; |
806 | ||
807 | case wxPAT_DOUBLE: | |
297eb8e6 | 808 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_double); |
7a828c7f VZ |
809 | break; |
810 | ||
811 | case wxPAT_POINTER: | |
297eb8e6 | 812 | lenScratch = system_sprintf(szScratch, wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN, m_szFlags, p->pad_pointer); |
7a828c7f VZ |
813 | break; |
814 | ||
815 | case wxPAT_CHAR: | |
816 | case wxPAT_WCHAR: | |
817 | { | |
818 | wxChar val = | |
210f4bcd | 819 | #if wxUSE_UNICODE |
7a828c7f VZ |
820 | p->pad_wchar; |
821 | ||
412a5c57 VZ |
822 | if (m_type == wxPAT_CHAR) |
823 | { | |
7a828c7f VZ |
824 | // user passed a character explicitely indicated as ANSI... |
825 | const char buf[2] = { p->pad_char, 0 }; | |
826 | val = wxString(buf, wxConvLibc)[0u]; | |
412a5c57 VZ |
827 | |
828 | //wprintf(L"converting ANSI=>Unicode"); // for debug | |
7a828c7f VZ |
829 | } |
830 | #else | |
831 | p->pad_char; | |
832 | ||
34deaa93 | 833 | #if wxUSE_WCHAR_T |
412a5c57 VZ |
834 | if (m_type == wxPAT_WCHAR) |
835 | { | |
7a828c7f VZ |
836 | // user passed a character explicitely indicated as Unicode... |
837 | const wchar_t buf[2] = { p->pad_wchar, 0 }; | |
838 | val = wxString(buf, wxConvLibc)[0u]; | |
412a5c57 VZ |
839 | |
840 | //printf("converting Unicode=>ANSI"); // for debug | |
7a828c7f | 841 | } |
34deaa93 | 842 | #endif |
210f4bcd | 843 | #endif |
210f4bcd | 844 | |
7a828c7f | 845 | size_t i; |
210f4bcd | 846 | |
412a5c57 VZ |
847 | if (!m_bAlignLeft) |
848 | for (i = 1; i < (size_t)m_nMinWidth; i++) | |
7a828c7f | 849 | APPEND_CH(_T(' ')); |
f6f5941b | 850 | |
7a828c7f | 851 | APPEND_CH(val); |
210f4bcd | 852 | |
412a5c57 VZ |
853 | if (m_bAlignLeft) |
854 | for (i = 1; i < (size_t)m_nMinWidth; i++) | |
7a828c7f VZ |
855 | APPEND_CH(_T(' ')); |
856 | } | |
857 | break; | |
f6f5941b | 858 | |
7a828c7f VZ |
859 | case wxPAT_PCHAR: |
860 | case wxPAT_PWCHAR: | |
861 | { | |
862 | wxString s; | |
863 | const wxChar *val = | |
f6f5941b | 864 | #if wxUSE_UNICODE |
7a828c7f VZ |
865 | p->pad_pwchar; |
866 | ||
412a5c57 VZ |
867 | if (m_type == wxPAT_PCHAR) |
868 | { | |
7a828c7f | 869 | // user passed a string explicitely indicated as ANSI... |
a31746c7 | 870 | val = s = wxString(p->pad_pchar, wxConvLibc); |
412a5c57 VZ |
871 | |
872 | //wprintf(L"converting ANSI=>Unicode"); // for debug | |
7a828c7f VZ |
873 | } |
874 | #else | |
875 | p->pad_pchar; | |
876 | ||
34deaa93 | 877 | #if wxUSE_WCHAR_T |
412a5c57 VZ |
878 | if (m_type == wxPAT_PWCHAR) |
879 | { | |
7a828c7f | 880 | // user passed a string explicitely indicated as Unicode... |
a31746c7 | 881 | val = s = wxString(p->pad_pwchar, wxConvLibc); |
412a5c57 VZ |
882 | |
883 | //printf("converting Unicode=>ANSI"); // for debug | |
7a828c7f | 884 | } |
34deaa93 | 885 | #endif |
7a828c7f VZ |
886 | #endif |
887 | int len; | |
888 | ||
889 | if (val) | |
890 | { | |
891 | #if wxUSE_STRUTILS | |
412a5c57 | 892 | // at this point we are sure that m_nMaxWidth is positive or null |
a31746c7 | 893 | // (see top of wxPrintfConvSpec::LoadArg) |
412a5c57 | 894 | len = wxMin((unsigned int)m_nMaxWidth, wxStrlen(val)); |
7a828c7f | 895 | #else |
412a5c57 | 896 | for ( len = 0; val[len] && (len < m_nMaxWidth); len++ ) |
7a828c7f | 897 | ; |
f6f5941b | 898 | #endif |
7a828c7f | 899 | } |
412a5c57 | 900 | else if (m_nMaxWidth >= 6) |
7a828c7f VZ |
901 | { |
902 | val = wxT("(null)"); | |
903 | len = 6; | |
904 | } | |
905 | else | |
906 | { | |
907 | val = wxEmptyString; | |
908 | len = 0; | |
909 | } | |
910 | ||
911 | int i; | |
912 | ||
412a5c57 | 913 | if (!m_bAlignLeft) |
7a828c7f | 914 | { |
412a5c57 | 915 | for (i = len; i < m_nMinWidth; i++) |
7a828c7f VZ |
916 | APPEND_CH(_T(' ')); |
917 | } | |
918 | ||
919 | #if wxUSE_STRUTILS | |
a31746c7 | 920 | len = wxMin((unsigned int)len, lenMax-lenCur); |
7a828c7f VZ |
921 | wxStrncpy(buf+lenCur, val, len); |
922 | lenCur += len; | |
923 | #else | |
924 | for (i = 0; i < len; i++) | |
925 | APPEND_CH(val[i]); | |
926 | #endif | |
927 | ||
412a5c57 | 928 | if (m_bAlignLeft) |
7a828c7f | 929 | { |
412a5c57 | 930 | for (i = len; i < m_nMinWidth; i++) |
7a828c7f | 931 | APPEND_CH(_T(' ')); |
f6f5941b | 932 | } |
df17b887 | 933 | } |
7a828c7f VZ |
934 | break; |
935 | ||
936 | case wxPAT_NINT: | |
937 | *p->pad_nint = lenCur; | |
938 | break; | |
939 | ||
940 | case wxPAT_NSHORTINT: | |
7d70c309 | 941 | *p->pad_nshortint = (short int)lenCur; |
7a828c7f VZ |
942 | break; |
943 | ||
944 | case wxPAT_NLONGINT: | |
945 | *p->pad_nlongint = lenCur; | |
946 | break; | |
947 | ||
948 | case wxPAT_INVALID: | |
949 | default: | |
950 | return -1; | |
951 | } | |
952 | ||
953 | // if we used system's sprintf() then we now need to append the s_szScratch | |
954 | // buffer to the given one... | |
412a5c57 | 955 | switch (m_type) |
7a828c7f VZ |
956 | { |
957 | case wxPAT_INT: | |
958 | case wxPAT_LONGINT: | |
959 | #if SIZEOF_LONG_LONG | |
960 | case wxPAT_LONGLONGINT: | |
961 | #endif | |
962 | case wxPAT_SIZET: | |
963 | case wxPAT_LONGDOUBLE: | |
964 | case wxPAT_DOUBLE: | |
965 | case wxPAT_POINTER: | |
966 | #if wxUSE_STRUTILS | |
967 | { | |
297eb8e6 | 968 | wxASSERT(lenScratch >= 0 && lenScratch < wxMAX_SVNPRINTF_SCRATCHBUFFER_LEN); |
412a5c57 VZ |
969 | if (lenMax < lenScratch) |
970 | { | |
971 | // fill output buffer and then return -1 | |
972 | wxStrncpy(buf, szScratch, lenMax); | |
973 | return -1; | |
974 | } | |
975 | wxStrncpy(buf, szScratch, lenScratch); | |
976 | lenCur += lenScratch; | |
7a828c7f VZ |
977 | } |
978 | #else | |
979 | { | |
412a5c57 | 980 | APPEND_STR(szScratch); |
7a828c7f VZ |
981 | } |
982 | #endif | |
983 | break; | |
984 | ||
985 | default: | |
986 | break; // all other cases were completed previously | |
987 | } | |
988 | ||
989 | return lenCur; | |
990 | } | |
991 | ||
247c23b4 VZ |
992 | // differences from standard strncpy: |
993 | // 1) copies everything from 'source' except for '%%' sequence which is copied as '%' | |
994 | // 2) returns the number of written characters in 'dest' as it could differ from given 'n' | |
995 | // 3) much less optimized, unfortunately... | |
996 | static int wxCopyStrWithPercents(wxChar *dest, const wxChar *source, size_t n) | |
997 | { | |
998 | size_t written = 0; | |
999 | ||
1000 | if (n == 0) | |
1001 | return 0; | |
1002 | ||
1003 | size_t i; | |
1004 | for ( i = 0; i < n-1; source++, i++) | |
1005 | { | |
1006 | dest[written++] = *source; | |
1007 | if (*(source+1) == wxT('%')) | |
1008 | { | |
1009 | // skip this additional '%' character | |
1010 | source++; | |
1011 | i++; | |
1012 | } | |
1013 | } | |
1014 | ||
1015 | if (i < n) | |
1016 | // copy last character inconditionally | |
1017 | dest[written++] = *source; | |
1018 | ||
1019 | return written; | |
1020 | } | |
1021 | ||
7a828c7f VZ |
1022 | int WXDLLEXPORT wxVsnprintf_(wxChar *buf, size_t lenMax, |
1023 | const wxChar *format, va_list argptr) | |
1024 | { | |
412a5c57 VZ |
1025 | // useful for debugging, to understand if we are really using this function |
1026 | // rather than the system implementation | |
1027 | #if 0 | |
1028 | wprintf(L"Using wxVsnprintf_\n"); | |
1029 | #endif | |
1030 | ||
1031 | // required memory: | |
1032 | wxPrintfConvSpec arg[wxMAX_SVNPRINTF_ARGUMENTS]; | |
1033 | wxPrintfArg argdata[wxMAX_SVNPRINTF_ARGUMENTS]; | |
1034 | wxPrintfConvSpec *pspec[wxMAX_SVNPRINTF_ARGUMENTS] = { NULL }; | |
7a828c7f VZ |
1035 | |
1036 | size_t i; | |
1037 | ||
1038 | // number of characters in the buffer so far, must be less than lenMax | |
1039 | size_t lenCur = 0; | |
1040 | ||
1041 | size_t nargs = 0; | |
1042 | const wxChar *toparse = format; | |
1043 | ||
1044 | // parse the format string | |
1045 | bool posarg_present = false, nonposarg_present = false; | |
1046 | for (; *toparse != wxT('\0'); toparse++) | |
1047 | { | |
1048 | if (*toparse == wxT('%') ) | |
f6f5941b | 1049 | { |
7a828c7f VZ |
1050 | arg[nargs].Init(); |
1051 | ||
1052 | // let's see if this is a (valid) conversion specifier... | |
1053 | if (arg[nargs].Parse(toparse)) | |
1054 | { | |
1055 | // ...yes it is | |
1056 | wxPrintfConvSpec *current = &arg[nargs]; | |
1057 | ||
1058 | // make toparse point to the end of this specifier | |
412a5c57 | 1059 | toparse = current->m_pArgEnd; |
7a828c7f | 1060 | |
412a5c57 VZ |
1061 | if (current->m_pos > 0) |
1062 | { | |
7a828c7f | 1063 | // the positionals start from number 1... adjust the index |
412a5c57 | 1064 | current->m_pos--; |
7a828c7f | 1065 | posarg_present = true; |
412a5c57 VZ |
1066 | } |
1067 | else | |
1068 | { | |
7a828c7f | 1069 | // not a positional argument... |
412a5c57 | 1070 | current->m_pos = nargs; |
7a828c7f VZ |
1071 | nonposarg_present = true; |
1072 | } | |
1073 | ||
1074 | // this conversion specifier is tied to the pos-th argument... | |
412a5c57 | 1075 | pspec[current->m_pos] = current; |
7a828c7f VZ |
1076 | nargs++; |
1077 | ||
1078 | if (nargs == wxMAX_SVNPRINTF_ARGUMENTS) | |
412a5c57 VZ |
1079 | { |
1080 | wxLogDebug(wxT("A single call to wxVsnprintf() has more than %d arguments; ") | |
1081 | wxT("ignoring all remaining arguments."), wxMAX_SVNPRINTF_ARGUMENTS); | |
7a828c7f | 1082 | break; // cannot handle any additional conv spec |
412a5c57 VZ |
1083 | } |
1084 | } | |
1085 | else | |
1086 | { | |
1087 | // it's safe to look in the next character of toparse as at worst | |
1088 | // we'll hit its \0 | |
1089 | if (*(toparse+1) == wxT('%')) | |
1090 | toparse++; // the Parse() returned false because we've found a %% | |
7a828c7f | 1091 | } |
f6f5941b | 1092 | } |
7a828c7f | 1093 | } |
df17b887 | 1094 | |
7a828c7f VZ |
1095 | if (posarg_present && nonposarg_present) |
1096 | return -1; // format strings with both positional and | |
1097 | // non-positional conversion specifier are unsupported !! | |
1098 | ||
3aa077ce MW |
1099 | // on platforms where va_list is an array type, it is necessary to make a |
1100 | // copy to be able to pass it to LoadArg as a reference. | |
1101 | bool ok = true; | |
1102 | va_list ap; | |
1103 | wxVaCopy(ap, argptr); | |
1104 | ||
7a828c7f | 1105 | // now load arguments from stack |
412a5c57 VZ |
1106 | for (i=0; i < nargs && ok; i++) |
1107 | { | |
1108 | // !pspec[i] means that the user forgot a positional parameter (e.g. %$1s %$3s); | |
1109 | // LoadArg == false means that wxPrintfConvSpec::Parse failed to set the | |
1110 | // conversion specifier 'type' to a valid value... | |
3aa077ce | 1111 | ok = pspec[i] && pspec[i]->LoadArg(&argdata[i], ap); |
7a828c7f VZ |
1112 | } |
1113 | ||
3aa077ce MW |
1114 | va_end(ap); |
1115 | ||
247c23b4 | 1116 | // something failed while loading arguments from the variable list... |
3aa077ce MW |
1117 | if (!ok) |
1118 | return -1; | |
1119 | ||
7a828c7f VZ |
1120 | // finally, process each conversion specifier with its own argument |
1121 | toparse = format; | |
1122 | for (i=0; i < nargs; i++) | |
1123 | { | |
1124 | // copy in the output buffer the portion of the format string between | |
1125 | // last specifier and the current one | |
412a5c57 | 1126 | size_t tocopy = ( arg[i].m_pArgPos - toparse ); |
7a828c7f | 1127 | if (lenCur+tocopy >= lenMax) |
412a5c57 VZ |
1128 | { |
1129 | // not enough space in the output buffer ! | |
1130 | // copy until the end of remaining space and then stop | |
1131 | wxCopyStrWithPercents(buf+lenCur, toparse, lenMax - lenCur - 1); | |
1132 | buf[lenMax-1] = wxT('\0'); | |
1133 | return -1; | |
1134 | } | |
7a828c7f | 1135 | |
247c23b4 | 1136 | lenCur += wxCopyStrWithPercents(buf+lenCur, toparse, tocopy); |
7a828c7f VZ |
1137 | |
1138 | // process this specifier directly in the output buffer | |
412a5c57 | 1139 | int n = arg[i].Process(buf+lenCur, lenMax - lenCur, &argdata[arg[i].m_pos]); |
7a828c7f | 1140 | if (n == -1) |
412a5c57 VZ |
1141 | { |
1142 | buf[lenMax-1] = wxT('\0'); // be sure to always NUL-terminate the string | |
7a828c7f | 1143 | return -1; // not enough space in the output buffer ! |
412a5c57 | 1144 | } |
7a828c7f VZ |
1145 | lenCur += n; |
1146 | ||
412a5c57 | 1147 | // the +1 is because wxPrintfConvSpec::m_pArgEnd points to the last character |
7a828c7f | 1148 | // of the format specifier, but we are not interested to it... |
412a5c57 | 1149 | toparse = arg[i].m_pArgEnd + 1; |
5f1d3069 RR |
1150 | } |
1151 | ||
7a828c7f VZ |
1152 | // copy portion of the format string after last specifier |
1153 | // NOTE: toparse is pointing to the character just after the last processed | |
1154 | // conversion specifier | |
1155 | // NOTE2: the +1 is because we want to copy also the '\0' | |
1156 | size_t tocopy = wxStrlen(format) + 1 - ( toparse - format ) ; | |
1157 | if (lenCur+tocopy >= lenMax) | |
1158 | return -1; // not enough space in the output buffer ! | |
247c23b4 VZ |
1159 | |
1160 | // the -1 is because of the '\0' | |
1161 | lenCur += wxCopyStrWithPercents(buf+lenCur, toparse, tocopy) - 1; | |
7a828c7f | 1162 | |
7a828c7f | 1163 | wxASSERT(lenCur == wxStrlen(buf)); |
f6f5941b VZ |
1164 | return lenCur; |
1165 | } | |
5f1d3069 | 1166 | |
f6f5941b VZ |
1167 | #undef APPEND_CH |
1168 | #undef APPEND_STR | |
1169 | #undef CHECK_PREC | |
5f1d3069 | 1170 | |
f6f5941b VZ |
1171 | #endif // !wxVsnprintfA |
1172 | ||
1173 | #if !defined(wxSnprintf_) | |
1174 | int WXDLLEXPORT wxSnprintf_(wxChar *buf, size_t len, const wxChar *format, ...) | |
1175 | { | |
5f1d3069 RR |
1176 | va_list argptr; |
1177 | va_start(argptr, format); | |
1178 | ||
f6f5941b | 1179 | int iLen = wxVsnprintf_(buf, len, format, argptr); |
5f1d3069 RR |
1180 | |
1181 | va_end(argptr); | |
f6f5941b VZ |
1182 | |
1183 | return iLen; | |
5f1d3069 | 1184 | } |
f6f5941b | 1185 | #endif // wxSnprintf_ |
5f1d3069 | 1186 | |
8dfb846e CE |
1187 | #if defined(__DMC__) |
1188 | /* Digital Mars adds count to _stprintf (C99) so convert */ | |
1189 | #if wxUSE_UNICODE | |
1190 | int wxSprintf (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... ) | |
1191 | { | |
1192 | va_list arglist; | |
1193 | ||
1194 | va_start( arglist, format ); | |
1195 | int iLen = swprintf ( s, -1, format, arglist ); | |
1196 | va_end( arglist ); | |
1197 | return iLen ; | |
1198 | } | |
1199 | ||
1200 | #endif // wxUSE_UNICODE | |
1201 | ||
1202 | #endif //__DMC__ | |
1203 | ||
f6f5941b VZ |
1204 | // ---------------------------------------------------------------------------- |
1205 | // implement the standard IO functions for wide char if libc doesn't have them | |
1206 | // ---------------------------------------------------------------------------- | |
5f1d3069 | 1207 | |
fbe47c7b | 1208 | #ifdef wxNEED_FPUTS |
f6f5941b VZ |
1209 | int wxFputs(const wchar_t *ws, FILE *stream) |
1210 | { | |
1211 | // counting the number of wide characters written isn't worth the trouble, | |
1212 | // simply distinguish between ok and error | |
1213 | return fputs(wxConvLibc.cWC2MB(ws), stream) == -1 ? -1 : 0; | |
1214 | } | |
fbe47c7b | 1215 | #endif // wxNEED_FPUTS |
5f1d3069 | 1216 | |
19b65a30 VZ |
1217 | #ifdef wxNEED_PUTS |
1218 | int wxPuts(const wxChar *ws) | |
1219 | { | |
1220 | int rc = wxFputs(ws, stdout); | |
1221 | if ( rc != -1 ) | |
1222 | { | |
1223 | if ( wxFputs(L"\n", stdout) == -1 ) | |
1224 | return -1; | |
1225 | ||
1226 | rc++; | |
1227 | } | |
1228 | ||
1229 | return rc; | |
1230 | } | |
1231 | #endif // wxNEED_PUTS | |
1232 | ||
fbe47c7b | 1233 | #ifdef wxNEED_PUTC |
f6f5941b VZ |
1234 | int /* not wint_t */ wxPutc(wchar_t wc, FILE *stream) |
1235 | { | |
1236 | wchar_t ws[2] = { wc, L'\0' }; | |
5f1d3069 | 1237 | |
f6f5941b | 1238 | return wxFputs(ws, stream); |
5f1d3069 | 1239 | } |
fbe47c7b | 1240 | #endif // wxNEED_PUTC |
f6f5941b VZ |
1241 | |
1242 | // NB: we only implement va_list functions here, the ones taking ... are | |
1243 | // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse | |
1244 | // the definitions there to avoid duplicating them here | |
1245 | #ifdef wxNEED_WPRINTF | |
1246 | ||
1247 | // TODO: implement the scanf() functions | |
df17b887 | 1248 | int vwscanf(const wxChar *format, va_list argptr) |
5f1d3069 | 1249 | { |
f6f5941b | 1250 | wxFAIL_MSG( _T("TODO") ); |
5f1d3069 | 1251 | |
f6f5941b VZ |
1252 | return -1; |
1253 | } | |
1254 | ||
df17b887 | 1255 | int vswscanf(const wxChar *ws, const wxChar *format, va_list argptr) |
f6f5941b | 1256 | { |
63de666d VS |
1257 | // The best we can do without proper Unicode support in glibc is to |
1258 | // convert the strings into MB representation and run ANSI version | |
1259 | // of the function. This doesn't work with %c and %s because of difference | |
1260 | // in size of char and wchar_t, though. | |
525d8583 | 1261 | |
63de666d VS |
1262 | wxCHECK_MSG( wxStrstr(format, _T("%s")) == NULL, -1, |
1263 | _T("incomplete vswscanf implementation doesn't allow %s") ); | |
1264 | wxCHECK_MSG( wxStrstr(format, _T("%c")) == NULL, -1, | |
1265 | _T("incomplete vswscanf implementation doesn't allow %c") ); | |
525d8583 | 1266 | |
63de666d VS |
1267 | va_list argcopy; |
1268 | wxVaCopy(argcopy, argptr); | |
1269 | return vsscanf(wxConvLibc.cWX2MB(ws), wxConvLibc.cWX2MB(format), argcopy); | |
f6f5941b | 1270 | } |
5f1d3069 | 1271 | |
df17b887 | 1272 | int vfwscanf(FILE *stream, const wxChar *format, va_list argptr) |
f6f5941b VZ |
1273 | { |
1274 | wxFAIL_MSG( _T("TODO") ); | |
5f1d3069 | 1275 | |
f6f5941b | 1276 | return -1; |
5f1d3069 RR |
1277 | } |
1278 | ||
f6f5941b VZ |
1279 | #define vswprintf wxVsnprintf_ |
1280 | ||
df17b887 | 1281 | int vfwprintf(FILE *stream, const wxChar *format, va_list argptr) |
5f1d3069 | 1282 | { |
f6f5941b VZ |
1283 | wxString s; |
1284 | int rc = s.PrintfV(format, argptr); | |
5f1d3069 | 1285 | |
f6f5941b VZ |
1286 | if ( rc != -1 ) |
1287 | { | |
1288 | // we can't do much better without Unicode support in libc... | |
50b079e5 | 1289 | if ( fprintf(stream, "%s", (const char*)s.mb_str() ) == -1 ) |
f6f5941b VZ |
1290 | return -1; |
1291 | } | |
5f1d3069 | 1292 | |
f6f5941b | 1293 | return rc; |
5f1d3069 RR |
1294 | } |
1295 | ||
df17b887 | 1296 | int vwprintf(const wxChar *format, va_list argptr) |
5f1d3069 | 1297 | { |
f6f5941b VZ |
1298 | return wxVfprintf(stdout, format, argptr); |
1299 | } | |
5f1d3069 | 1300 | |
f6f5941b | 1301 | #endif // wxNEED_WPRINTF |
5f1d3069 | 1302 | |
f6f5941b | 1303 | #ifdef wxNEED_PRINTF_CONVERSION |
5f1d3069 | 1304 | |
f6f5941b VZ |
1305 | // ---------------------------------------------------------------------------- |
1306 | // wxFormatConverter: class doing the "%s" -> "%ls" conversion | |
1307 | // ---------------------------------------------------------------------------- | |
5f1d3069 | 1308 | |
f6f5941b VZ |
1309 | /* |
1310 | Here are the gory details. We want to follow the Windows/MS conventions, | |
1311 | that is to have | |
1312 | ||
1313 | In ANSI mode: | |
1314 | ||
1315 | format specifier results in | |
1316 | ----------------------------------- | |
1317 | %c, %hc, %hC char | |
1318 | %lc, %C, %lC wchar_t | |
1319 | ||
1320 | In Unicode mode: | |
1321 | ||
1322 | format specifier results in | |
1323 | ----------------------------------- | |
1324 | %hc, %C, %hC char | |
1325 | %c, %lc, %lC wchar_t | |
1326 | ||
1327 | ||
1328 | while on POSIX systems we have %C identical to %lc and %c always means char | |
1329 | (in any mode) while %lc always means wchar_t, | |
1330 | ||
1331 | So to use native functions in order to get our semantics we must do the | |
1332 | following translations in Unicode mode (nothing to do in ANSI mode): | |
1333 | ||
77ffb593 | 1334 | wxWidgets specifier POSIX specifier |
f6f5941b VZ |
1335 | ---------------------------------------- |
1336 | ||
1337 | %hc, %C, %hC %c | |
1338 | %c %lc | |
1339 | ||
1340 | ||
1341 | And, of course, the same should be done for %s as well. | |
1342 | */ | |
1343 | ||
1344 | class wxFormatConverter | |
5f1d3069 | 1345 | { |
f6f5941b VZ |
1346 | public: |
1347 | wxFormatConverter(const wxChar *format); | |
1348 | ||
c03d0035 VZ |
1349 | // notice that we only translated the string if m_fmtOrig == NULL (as set |
1350 | // by CopyAllBefore()), otherwise we should simply use the original format | |
1351 | operator const wxChar *() const | |
1352 | { return m_fmtOrig ? m_fmtOrig : m_fmt.c_str(); } | |
f6f5941b VZ |
1353 | |
1354 | private: | |
1355 | // copy another character to the translated format: this function does the | |
1356 | // copy if we are translating but doesn't do anything at all if we don't, | |
1357 | // so we don't create the translated format string at all unless we really | |
1358 | // need to (i.e. InsertFmtChar() is called) | |
1359 | wxChar CopyFmtChar(wxChar ch) | |
1360 | { | |
1361 | if ( !m_fmtOrig ) | |
1362 | { | |
c03d0035 | 1363 | // we're translating, do copy |
f6f5941b VZ |
1364 | m_fmt += ch; |
1365 | } | |
1366 | else | |
1367 | { | |
1368 | // simply increase the count which should be copied by | |
1369 | // CopyAllBefore() later if needed | |
1370 | m_nCopied++; | |
1371 | } | |
1372 | ||
1373 | return ch; | |
1374 | } | |
1375 | ||
1376 | // insert an extra character | |
1377 | void InsertFmtChar(wxChar ch) | |
1378 | { | |
1379 | if ( m_fmtOrig ) | |
1380 | { | |
1381 | // so far we haven't translated anything yet | |
1382 | CopyAllBefore(); | |
1383 | } | |
1384 | ||
1385 | m_fmt += ch; | |
1386 | } | |
1387 | ||
1388 | void CopyAllBefore() | |
1389 | { | |
1390 | wxASSERT_MSG( m_fmtOrig && m_fmt.empty(), _T("logic error") ); | |
1391 | ||
1392 | m_fmt = wxString(m_fmtOrig, m_nCopied); | |
1393 | ||
1394 | // we won't need it any longer | |
1395 | m_fmtOrig = NULL; | |
1396 | } | |
5f1d3069 | 1397 | |
f6f5941b VZ |
1398 | static bool IsFlagChar(wxChar ch) |
1399 | { | |
1400 | return ch == _T('-') || ch == _T('+') || | |
1401 | ch == _T('0') || ch == _T(' ') || ch == _T('#'); | |
1402 | } | |
1403 | ||
41d9940d | 1404 | void SkipDigits(const wxChar **ptpc) |
f6f5941b | 1405 | { |
41d9940d SC |
1406 | while ( **ptpc >= _T('0') && **ptpc <= _T('9') ) |
1407 | CopyFmtChar(*(*ptpc)++); | |
f6f5941b VZ |
1408 | } |
1409 | ||
1410 | // the translated format | |
1411 | wxString m_fmt; | |
1412 | ||
1413 | // the original format | |
1414 | const wxChar *m_fmtOrig; | |
1415 | ||
1416 | // the number of characters already copied | |
1417 | size_t m_nCopied; | |
1418 | }; | |
1419 | ||
1420 | wxFormatConverter::wxFormatConverter(const wxChar *format) | |
1421 | { | |
1422 | m_fmtOrig = format; | |
1423 | m_nCopied = 0; | |
1424 | ||
1425 | while ( *format ) | |
1426 | { | |
1427 | if ( CopyFmtChar(*format++) == _T('%') ) | |
1428 | { | |
1429 | // skip any flags | |
1430 | while ( IsFlagChar(*format) ) | |
1431 | CopyFmtChar(*format++); | |
1432 | ||
1433 | // and possible width | |
1434 | if ( *format == _T('*') ) | |
1435 | CopyFmtChar(*format++); | |
1436 | else | |
1437 | SkipDigits(&format); | |
1438 | ||
1439 | // precision? | |
1440 | if ( *format == _T('.') ) | |
1441 | { | |
cfee166d JS |
1442 | CopyFmtChar(*format++); |
1443 | if ( *format == _T('*') ) | |
1444 | CopyFmtChar(*format++); | |
1445 | else | |
1446 | SkipDigits(&format); | |
f6f5941b VZ |
1447 | } |
1448 | ||
1449 | // next we can have a size modifier | |
1450 | enum | |
1451 | { | |
1452 | Default, | |
1453 | Short, | |
1454 | Long | |
1455 | } size; | |
1456 | ||
1457 | switch ( *format ) | |
1458 | { | |
1459 | case _T('h'): | |
1460 | size = Short; | |
1461 | format++; | |
1462 | break; | |
1463 | ||
1464 | case _T('l'): | |
1465 | // "ll" has a different meaning! | |
1466 | if ( format[1] != _T('l') ) | |
1467 | { | |
1468 | size = Long; | |
1469 | format++; | |
1470 | break; | |
1471 | } | |
1472 | //else: fall through | |
1473 | ||
1474 | default: | |
1475 | size = Default; | |
1476 | } | |
1477 | ||
1478 | // and finally we should have the type | |
1479 | switch ( *format ) | |
1480 | { | |
1481 | case _T('C'): | |
1482 | case _T('S'): | |
1483 | // %C and %hC -> %c and %lC -> %lc | |
1484 | if ( size == Long ) | |
1485 | CopyFmtChar(_T('l')); | |
1486 | ||
1487 | InsertFmtChar(*format++ == _T('C') ? _T('c') : _T('s')); | |
1488 | break; | |
1489 | ||
1490 | case _T('c'): | |
1491 | case _T('s'): | |
1492 | // %c -> %lc but %hc stays %hc and %lc is still %lc | |
cfee166d JS |
1493 | if ( size == Default) |
1494 | InsertFmtChar(_T('l')); | |
f6f5941b VZ |
1495 | // fall through |
1496 | ||
1497 | default: | |
1498 | // nothing special to do | |
cfee166d JS |
1499 | if ( size != Default ) |
1500 | CopyFmtChar(*(format - 1)); | |
f6f5941b VZ |
1501 | CopyFmtChar(*format++); |
1502 | } | |
1503 | } | |
1504 | } | |
1505 | } | |
1506 | ||
1507 | #else // !wxNEED_PRINTF_CONVERSION | |
1508 | // no conversion necessary | |
1509 | #define wxFormatConverter(x) (x) | |
1510 | #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION | |
1511 | ||
cfee166d JS |
1512 | #ifdef __WXDEBUG__ |
1513 | // For testing the format converter | |
1514 | wxString wxConvertFormat(const wxChar *format) | |
1515 | { | |
1516 | return wxString(wxFormatConverter(format)); | |
1517 | } | |
1518 | #endif | |
1519 | ||
f6f5941b VZ |
1520 | // ---------------------------------------------------------------------------- |
1521 | // wxPrintf(), wxScanf() and relatives | |
1522 | // ---------------------------------------------------------------------------- | |
1523 | ||
1524 | #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF) | |
1525 | ||
df17b887 | 1526 | int wxScanf( const wxChar *format, ... ) |
f6f5941b | 1527 | { |
5f1d3069 RR |
1528 | va_list argptr; |
1529 | va_start(argptr, format); | |
1530 | ||
f6f5941b | 1531 | int ret = vwscanf(wxFormatConverter(format), argptr ); |
5f1d3069 RR |
1532 | |
1533 | va_end(argptr); | |
1534 | ||
1535 | return ret; | |
1536 | } | |
1537 | ||
df17b887 | 1538 | int wxSscanf( const wxChar *str, const wxChar *format, ... ) |
5f1d3069 | 1539 | { |
5f1d3069 RR |
1540 | va_list argptr; |
1541 | va_start(argptr, format); | |
1542 | ||
f6f5941b | 1543 | int ret = vswscanf( str, wxFormatConverter(format), argptr ); |
5f1d3069 RR |
1544 | |
1545 | va_end(argptr); | |
1546 | ||
1547 | return ret; | |
1548 | } | |
1549 | ||
df17b887 | 1550 | int wxFscanf( FILE *stream, const wxChar *format, ... ) |
5f1d3069 | 1551 | { |
5f1d3069 | 1552 | va_list argptr; |
f6f5941b | 1553 | va_start(argptr, format); |
f6f5941b | 1554 | int ret = vfwscanf(stream, wxFormatConverter(format), argptr); |
5f1d3069 RR |
1555 | |
1556 | va_end(argptr); | |
1557 | ||
1558 | return ret; | |
1559 | } | |
1560 | ||
df17b887 | 1561 | int wxPrintf( const wxChar *format, ... ) |
5f1d3069 | 1562 | { |
f6f5941b VZ |
1563 | va_list argptr; |
1564 | va_start(argptr, format); | |
df17b887 | 1565 | |
f6f5941b | 1566 | int ret = vwprintf( wxFormatConverter(format), argptr ); |
5f1d3069 | 1567 | |
f6f5941b | 1568 | va_end(argptr); |
5f1d3069 RR |
1569 | |
1570 | return ret; | |
1571 | } | |
1572 | ||
f6f5941b | 1573 | #ifndef wxSnprintf |
df17b887 | 1574 | int wxSnprintf( wxChar *str, size_t size, const wxChar *format, ... ) |
5f1d3069 | 1575 | { |
f6f5941b VZ |
1576 | va_list argptr; |
1577 | va_start(argptr, format); | |
5f1d3069 | 1578 | |
f6f5941b | 1579 | int ret = vswprintf( str, size, wxFormatConverter(format), argptr ); |
5f1d3069 | 1580 | |
8dd57590 MW |
1581 | // VsnprintfTestCase reveals that glibc's implementation of vswprintf |
1582 | // doesn't nul terminate on truncation. | |
1583 | str[size - 1] = 0; | |
1584 | ||
f6f5941b | 1585 | va_end(argptr); |
5f1d3069 RR |
1586 | |
1587 | return ret; | |
1588 | } | |
f6f5941b | 1589 | #endif // wxSnprintf |
5f1d3069 | 1590 | |
df17b887 | 1591 | int wxSprintf( wxChar *str, const wxChar *format, ... ) |
5f1d3069 | 1592 | { |
f6f5941b VZ |
1593 | va_list argptr; |
1594 | va_start(argptr, format); | |
5f1d3069 | 1595 | |
56413ebf | 1596 | // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so |
f8991003 | 1597 | // it's safe to implement this one in terms of it |
56413ebf | 1598 | wxString s(wxString::FormatV(format, argptr)); |
a4e0917b | 1599 | wxStrcpy(str, s); |
5f1d3069 | 1600 | |
f6f5941b | 1601 | va_end(argptr); |
5f1d3069 | 1602 | |
a4e0917b | 1603 | return s.length(); |
5f1d3069 RR |
1604 | } |
1605 | ||
df17b887 | 1606 | int wxFprintf( FILE *stream, const wxChar *format, ... ) |
5f1d3069 | 1607 | { |
f6f5941b VZ |
1608 | va_list argptr; |
1609 | va_start( argptr, format ); | |
5f1d3069 | 1610 | |
f6f5941b | 1611 | int ret = vfwprintf( stream, wxFormatConverter(format), argptr ); |
5f1d3069 | 1612 | |
f6f5941b | 1613 | va_end(argptr); |
5f1d3069 RR |
1614 | |
1615 | return ret; | |
1616 | } | |
f06e3075 | 1617 | |
f6f5941b | 1618 | int wxVsscanf( const wxChar *str, const wxChar *format, va_list argptr ) |
5f1d3069 | 1619 | { |
f6f5941b VZ |
1620 | return vswscanf( str, wxFormatConverter(format), argptr ); |
1621 | } | |
5f1d3069 | 1622 | |
f6f5941b VZ |
1623 | int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr ) |
1624 | { | |
1625 | return vfwprintf( stream, wxFormatConverter(format), argptr ); | |
1626 | } | |
5f1d3069 | 1627 | |
f6f5941b VZ |
1628 | int wxVprintf( const wxChar *format, va_list argptr ) |
1629 | { | |
1630 | return vwprintf( wxFormatConverter(format), argptr ); | |
5f1d3069 | 1631 | } |
5f1d3069 | 1632 | |
f6f5941b VZ |
1633 | #ifndef wxVsnprintf |
1634 | int wxVsnprintf( wxChar *str, size_t size, const wxChar *format, va_list argptr ) | |
5f1d3069 | 1635 | { |
f6f5941b VZ |
1636 | return vswprintf( str, size, wxFormatConverter(format), argptr ); |
1637 | } | |
1638 | #endif // wxVsnprintf | |
5f1d3069 | 1639 | |
f6f5941b VZ |
1640 | int wxVsprintf( wxChar *str, const wxChar *format, va_list argptr ) |
1641 | { | |
1642 | // same as for wxSprintf() | |
b63b07a8 | 1643 | return vswprintf(str, INT_MAX / 4, wxFormatConverter(format), argptr); |
f6f5941b | 1644 | } |
5f1d3069 | 1645 | |
f6f5941b | 1646 | #endif // wxNEED_PRINTF_CONVERSION |
5f1d3069 | 1647 | |
33a7c3dd VZ |
1648 | #if wxUSE_WCHAR_T |
1649 | ||
f6f5941b VZ |
1650 | // ---------------------------------------------------------------------------- |
1651 | // ctype.h stuff (currently unused) | |
1652 | // ---------------------------------------------------------------------------- | |
5f1d3069 | 1653 | |
57f6da0d OK |
1654 | #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H) |
1655 | inline WORD wxMSW_ctype(wxChar ch) | |
1656 | { | |
1657 | WORD ret; | |
1658 | GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret); | |
1659 | return ret; | |
1660 | } | |
1661 | ||
b0655a87 OK |
1662 | WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); } |
1663 | WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); } | |
4eb7c4b1 | 1664 | WXDLLEXPORT int wxIscntrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; } |
b0655a87 OK |
1665 | WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; } |
1666 | WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); } | |
1667 | WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); } | |
1668 | WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); } | |
1669 | WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; } | |
1670 | WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; } | |
1671 | WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); } | |
1672 | WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; } | |
1673 | WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); } | |
1674 | WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); } | |
57f6da0d OK |
1675 | #endif |
1676 | ||
dbf9aa46 | 1677 | #ifdef wxNEED_WX_MBSTOWCS |
dcb68102 | 1678 | |
1e96e503 | 1679 | WXDLLEXPORT size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen) |
dcb68102 RN |
1680 | { |
1681 | if (!out) | |
1682 | { | |
1683 | size_t outsize = 0; | |
1684 | while(*in++) | |
1685 | outsize++; | |
1686 | return outsize; | |
1687 | } | |
525d8583 | 1688 | |
dcb68102 | 1689 | const char* origin = in; |
525d8583 | 1690 | |
dcb68102 RN |
1691 | while (outlen-- && *in) |
1692 | { | |
1693 | *out++ = (wchar_t) *in++; | |
1694 | } | |
525d8583 | 1695 | |
dcb68102 | 1696 | *out = '\0'; |
525d8583 | 1697 | |
dcb68102 RN |
1698 | return in - origin; |
1699 | } | |
1700 | ||
41e155b4 | 1701 | WXDLLEXPORT size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen) |
dcb68102 RN |
1702 | { |
1703 | if (!out) | |
1704 | { | |
1705 | size_t outsize = 0; | |
1706 | while(*in++) | |
1707 | outsize++; | |
1708 | return outsize; | |
1709 | } | |
525d8583 | 1710 | |
dcb68102 | 1711 | const wchar_t* origin = in; |
525d8583 | 1712 | |
dcb68102 RN |
1713 | while (outlen-- && *in) |
1714 | { | |
1715 | *out++ = (char) *in++; | |
1716 | } | |
525d8583 | 1717 | |
dcb68102 | 1718 | *out = '\0'; |
525d8583 | 1719 | |
dcb68102 RN |
1720 | return in - origin; |
1721 | } | |
525d8583 | 1722 | |
dbf9aa46 VZ |
1723 | #endif // wxNEED_WX_MBSTOWCS |
1724 | ||
dcb68102 RN |
1725 | #if defined(wxNEED_WX_CTYPE_H) |
1726 | ||
1727 | #include <CoreFoundation/CoreFoundation.h> | |
1728 | ||
eaceebf6 RN |
1729 | #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric) |
1730 | #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter) | |
1731 | #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl) | |
1732 | #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit) | |
dcb68102 | 1733 | //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' ' |
eaceebf6 | 1734 | #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter) |
dcb68102 | 1735 | //CFCharacterSetRef cfprintset = !kCFCharacterSetControl |
eaceebf6 RN |
1736 | #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation) |
1737 | #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline) | |
1738 | #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter) | |
dcb68102 RN |
1739 | |
1740 | WXDLLEXPORT int wxIsalnum(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalnumset, ch); } | |
1741 | WXDLLEXPORT int wxIsalpha(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalphaset, ch); } | |
1742 | WXDLLEXPORT int wxIscntrl(wxChar ch) { return CFCharacterSetIsCharacterMember(cfcntrlset, ch); } | |
1743 | WXDLLEXPORT int wxIsdigit(wxChar ch) { return CFCharacterSetIsCharacterMember(cfdigitset, ch); } | |
1744 | WXDLLEXPORT int wxIsgraph(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch) && ch != ' '; } | |
1745 | WXDLLEXPORT int wxIslower(wxChar ch) { return CFCharacterSetIsCharacterMember(cflowerset, ch); } | |
1746 | WXDLLEXPORT int wxIsprint(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch); } | |
1747 | WXDLLEXPORT int wxIspunct(wxChar ch) { return CFCharacterSetIsCharacterMember(cfpunctset, ch); } | |
1748 | WXDLLEXPORT int wxIsspace(wxChar ch) { return CFCharacterSetIsCharacterMember(cfspaceset, ch); } | |
1749 | WXDLLEXPORT int wxIsupper(wxChar ch) { return CFCharacterSetIsCharacterMember(cfupperset, ch); } | |
1750 | WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxIsdigit(ch) || (ch>='a' && ch<='f') || (ch>='A' && ch<='F'); } | |
1751 | WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)tolower((char)(ch)); } | |
1752 | WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)toupper((char)(ch)); } | |
dcb68102 | 1753 | |
8a9c20b0 RN |
1754 | #endif // wxNEED_WX_CTYPE_H |
1755 | ||
07243717 VZ |
1756 | #ifndef wxStrdupA |
1757 | ||
1758 | WXDLLEXPORT char *wxStrdupA(const char *s) | |
1759 | { | |
1760 | return strcpy((char *)malloc(strlen(s) + 1), s); | |
1761 | } | |
1762 | ||
1763 | #endif // wxStrdupA | |
1764 | ||
1765 | #ifndef wxStrdupW | |
1766 | ||
1767 | WXDLLEXPORT wchar_t * wxStrdupW(const wchar_t *pwz) | |
c9e089e9 | 1768 | { |
07243717 VZ |
1769 | size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t); |
1770 | wchar_t *ret = (wchar_t *) malloc(size); | |
1771 | memcpy(ret, pwz, size); | |
c9e089e9 OK |
1772 | return ret; |
1773 | } | |
07243717 VZ |
1774 | |
1775 | #endif // wxStrdupW | |
c9e089e9 | 1776 | |
d0bdc3ca OK |
1777 | #ifndef wxStricmp |
1778 | int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2) | |
1779 | { | |
1780 | register wxChar c1, c2; | |
1781 | do { | |
1782 | c1 = wxTolower(*psz1++); | |
1783 | c2 = wxTolower(*psz2++); | |
1784 | } while ( c1 && (c1 == c2) ); | |
1785 | return c1 - c2; | |
1786 | } | |
1787 | #endif | |
1788 | ||
e766c8a9 SC |
1789 | #ifndef wxStricmp |
1790 | int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n) | |
1791 | { | |
a400a823 VZ |
1792 | // initialize the variables just to suppress stupid gcc warning |
1793 | register wxChar c1 = 0, c2 = 0; | |
e766c8a9 SC |
1794 | while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++; |
1795 | if (n) { | |
1796 | if (c1 < c2) return -1; | |
1797 | if (c1 > c2) return 1; | |
1798 | } | |
1799 | return 0; | |
1800 | } | |
1801 | #endif | |
1802 | ||
c9e089e9 | 1803 | #ifndef wxSetlocale |
191ab39a | 1804 | WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale) |
c9e089e9 | 1805 | { |
ddf14c13 | 1806 | char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale)); |
929eed47 | 1807 | |
ddf14c13 | 1808 | return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld)); |
c9e089e9 OK |
1809 | } |
1810 | #endif | |
1811 | ||
30261041 RN |
1812 | #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN) |
1813 | WXDLLEXPORT size_t wxWcslen(const wchar_t *s) | |
1814 | { | |
1815 | size_t n = 0; | |
1816 | while ( *s++ ) | |
1817 | n++; | |
1818 | ||
1819 | return n; | |
1820 | } | |
1821 | #endif | |
1822 | ||
f6f5941b VZ |
1823 | // ---------------------------------------------------------------------------- |
1824 | // string.h functions | |
1825 | // ---------------------------------------------------------------------------- | |
1826 | ||
b0655a87 | 1827 | #ifdef wxNEED_WX_STRING_H |
30261041 RN |
1828 | |
1829 | // RN: These need to be c externed for the regex lib | |
1830 | #ifdef __cplusplus | |
1831 | extern "C" { | |
1832 | #endif | |
1833 | ||
b0655a87 OK |
1834 | WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src) |
1835 | { | |
1836 | wxChar *ret = dest; | |
1837 | while (*dest) dest++; | |
1838 | while ((*dest++ = *src++)); | |
1839 | return ret; | |
1840 | } | |
1841 | ||
109c7768 | 1842 | WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c) |
b0655a87 | 1843 | { |
109c7768 VZ |
1844 | // be careful here as the terminating NUL makes part of the string |
1845 | while ( *s != c ) | |
1846 | { | |
1847 | if ( !*s++ ) | |
1848 | return NULL; | |
1849 | } | |
1850 | ||
1851 | return s; | |
b0655a87 OK |
1852 | } |
1853 | ||
1854 | WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2) | |
1855 | { | |
1856 | while ((*s1 == *s2) && *s1) s1++, s2++; | |
1857 | if ((wxUChar)*s1 < (wxUChar)*s2) return -1; | |
1858 | if ((wxUChar)*s1 > (wxUChar)*s2) return 1; | |
1859 | return 0; | |
1860 | } | |
1861 | ||
1862 | WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src) | |
1863 | { | |
1864 | wxChar *ret = dest; | |
1865 | while ((*dest++ = *src++)); | |
1866 | return ret; | |
1867 | } | |
1868 | ||
dcb68102 RN |
1869 | WXDLLEXPORT size_t wxStrlen_(const wxChar *s) |
1870 | { | |
1871 | size_t n = 0; | |
1872 | while ( *s++ ) | |
1873 | n++; | |
525d8583 | 1874 | |
dcb68102 RN |
1875 | return n; |
1876 | } | |
1877 | ||
1878 | ||
b0655a87 OK |
1879 | WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n) |
1880 | { | |
1881 | wxChar *ret = dest; | |
1882 | while (*dest) dest++; | |
1883 | while (n && (*dest++ = *src++)) n--; | |
1884 | return ret; | |
1885 | } | |
1886 | ||
a5e0b1e8 OK |
1887 | WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n) |
1888 | { | |
1889 | while (n && (*s1 == *s2) && *s1) n--, s1++, s2++; | |
1890 | if (n) { | |
1891 | if ((wxUChar)*s1 < (wxUChar)*s2) return -1; | |
1892 | if ((wxUChar)*s1 > (wxUChar)*s2) return 1; | |
1893 | } | |
1894 | return 0; | |
1895 | } | |
1896 | ||
b0655a87 OK |
1897 | WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n) |
1898 | { | |
1899 | wxChar *ret = dest; | |
1900 | while (n && (*dest++ = *src++)) n--; | |
1901 | while (n) *dest++=0, n--; // the docs specify padding with zeroes | |
1902 | return ret; | |
1903 | } | |
1904 | ||
109c7768 | 1905 | WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept) |
b0655a87 | 1906 | { |
109c7768 VZ |
1907 | while (*s && !wxStrchr(accept, *s)) |
1908 | s++; | |
1909 | ||
1910 | return *s ? s : NULL; | |
b0655a87 OK |
1911 | } |
1912 | ||
109c7768 | 1913 | WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c) |
b0655a87 | 1914 | { |
109c7768 VZ |
1915 | const wxChar *ret = NULL; |
1916 | do | |
1917 | { | |
1918 | if ( *s == c ) | |
1919 | ret = s; | |
1920 | s++; | |
1921 | } | |
1922 | while ( *s ); | |
1923 | ||
1924 | return ret; | |
b0655a87 OK |
1925 | } |
1926 | ||
1927 | WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept) | |
1928 | { | |
1929 | size_t len = 0; | |
1930 | while (wxStrchr(accept, *s++)) len++; | |
1931 | return len; | |
1932 | } | |
1933 | ||
109c7768 | 1934 | WXDLLEXPORT const wxChar *wxStrstr(const wxChar *haystack, const wxChar *needle) |
b0655a87 | 1935 | { |
dcb68102 | 1936 | wxASSERT_MSG( needle != NULL, _T("NULL argument in wxStrstr") ); |
9f6fe288 | 1937 | |
109c7768 VZ |
1938 | // VZ: this is not exactly the most efficient string search algorithm... |
1939 | ||
9f6fe288 VZ |
1940 | const size_t len = wxStrlen(needle); |
1941 | ||
109c7768 | 1942 | while ( const wxChar *fnd = wxStrchr(haystack, *needle) ) |
9f6fe288 VZ |
1943 | { |
1944 | if ( !wxStrncmp(fnd, needle, len) ) | |
1945 | return fnd; | |
1946 | ||
1947 | haystack = fnd + 1; | |
1948 | } | |
1949 | ||
109c7768 | 1950 | return NULL; |
b0655a87 OK |
1951 | } |
1952 | ||
30261041 RN |
1953 | #ifdef __cplusplus |
1954 | } | |
1955 | #endif | |
1956 | ||
b0655a87 OK |
1957 | WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr) |
1958 | { | |
1959 | const wxChar *start = nptr; | |
1960 | ||
1961 | // FIXME: only correct for C locale | |
1962 | while (wxIsspace(*nptr)) nptr++; | |
223d09f6 | 1963 | if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++; |
b0655a87 | 1964 | while (wxIsdigit(*nptr)) nptr++; |
223d09f6 | 1965 | if (*nptr == wxT('.')) { |
b0655a87 OK |
1966 | nptr++; |
1967 | while (wxIsdigit(*nptr)) nptr++; | |
1968 | } | |
223d09f6 | 1969 | if (*nptr == wxT('E') || *nptr == wxT('e')) { |
b0655a87 | 1970 | nptr++; |
223d09f6 | 1971 | if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++; |
b0655a87 OK |
1972 | while (wxIsdigit(*nptr)) nptr++; |
1973 | } | |
1974 | ||
1975 | wxString data(nptr, nptr-start); | |
ddf14c13 | 1976 | wxWX2MBbuf dat = data.mb_str(wxConvLibc); |
e90c1d2a | 1977 | char *rdat = wxMBSTRINGCAST dat; |
b0655a87 OK |
1978 | double ret = strtod(dat, &rdat); |
1979 | ||
1980 | if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat)); | |
1981 | ||
1982 | return ret; | |
1983 | } | |
1984 | ||
1985 | WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base) | |
1986 | { | |
1987 | const wxChar *start = nptr; | |
1988 | ||
1989 | // FIXME: only correct for C locale | |
1990 | while (wxIsspace(*nptr)) nptr++; | |
223d09f6 | 1991 | if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++; |
b0655a87 | 1992 | if (((base == 0) || (base == 16)) && |
223d09f6 | 1993 | (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) { |
b0655a87 OK |
1994 | nptr += 2; |
1995 | base = 16; | |
1996 | } | |
223d09f6 | 1997 | else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8; |
b0655a87 OK |
1998 | else if (base == 0) base = 10; |
1999 | ||
223d09f6 KB |
2000 | while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) || |
2001 | (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++; | |
b0655a87 | 2002 | |
dcb68102 | 2003 | wxString data(start, nptr-start); |
ddf14c13 | 2004 | wxWX2MBbuf dat = data.mb_str(wxConvLibc); |
e90c1d2a | 2005 | char *rdat = wxMBSTRINGCAST dat; |
b0655a87 OK |
2006 | long int ret = strtol(dat, &rdat, base); |
2007 | ||
2008 | if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat)); | |
2009 | ||
2010 | return ret; | |
2011 | } | |
dcb68102 RN |
2012 | |
2013 | WXDLLEXPORT unsigned long int wxStrtoul(const wxChar *nptr, wxChar **endptr, int base) | |
2014 | { | |
2015 | return (unsigned long int) wxStrtol(nptr, endptr, base); | |
2016 | } | |
2017 | ||
f6f5941b | 2018 | #endif // wxNEED_WX_STRING_H |
b0655a87 | 2019 | |
c9e089e9 | 2020 | #ifdef wxNEED_WX_STDIO_H |
e7b3d6ba OK |
2021 | WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode) |
2022 | { | |
b1ac3b56 RR |
2023 | char mode_buffer[10]; |
2024 | for (size_t i = 0; i < wxStrlen(mode)+1; i++) | |
2025 | mode_buffer[i] = (char) mode[i]; | |
f6f5941b | 2026 | |
b1ac3b56 | 2027 | return fopen( wxConvFile.cWX2MB(path), mode_buffer ); |
e7b3d6ba OK |
2028 | } |
2029 | ||
2030 | WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream) | |
2031 | { | |
b1ac3b56 RR |
2032 | char mode_buffer[10]; |
2033 | for (size_t i = 0; i < wxStrlen(mode)+1; i++) | |
2034 | mode_buffer[i] = (char) mode[i]; | |
f6f5941b | 2035 | |
b1ac3b56 | 2036 | return freopen( wxConvFile.cWX2MB(path), mode_buffer, stream ); |
e7b3d6ba OK |
2037 | } |
2038 | ||
f6bcfd97 BP |
2039 | WXDLLEXPORT int wxRemove(const wxChar *path) |
2040 | { | |
92980e90 | 2041 | return remove( wxConvFile.cWX2MB(path) ); |
f6bcfd97 BP |
2042 | } |
2043 | ||
2044 | WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath) | |
2045 | { | |
92980e90 | 2046 | return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) ); |
f6bcfd97 | 2047 | } |
c9e089e9 OK |
2048 | #endif |
2049 | ||
b7a5d6ca | 2050 | #ifndef wxAtof |
c9e089e9 OK |
2051 | double WXDLLEXPORT wxAtof(const wxChar *psz) |
2052 | { | |
4676948b JS |
2053 | #ifdef __WXWINCE__ |
2054 | double d; | |
2055 | wxString str(psz); | |
2056 | if (str.ToDouble(& d)) | |
2057 | return d; | |
41e155b4 WS |
2058 | |
2059 | return 0.0; | |
4676948b | 2060 | #else |
ddf14c13 | 2061 | return atof(wxConvLibc.cWX2MB(psz)); |
4676948b | 2062 | #endif |
c9e089e9 | 2063 | } |
b7a5d6ca | 2064 | #endif |
c9e089e9 | 2065 | |
b7a5d6ca | 2066 | #ifdef wxNEED_WX_STDLIB_H |
c9e089e9 OK |
2067 | int WXDLLEXPORT wxAtoi(const wxChar *psz) |
2068 | { | |
ddf14c13 | 2069 | return atoi(wxConvLibc.cWX2MB(psz)); |
c9e089e9 OK |
2070 | } |
2071 | ||
2072 | long WXDLLEXPORT wxAtol(const wxChar *psz) | |
2073 | { | |
ddf14c13 | 2074 | return atol(wxConvLibc.cWX2MB(psz)); |
c9e089e9 OK |
2075 | } |
2076 | ||
2077 | wxChar * WXDLLEXPORT wxGetenv(const wxChar *name) | |
2078 | { | |
78868257 | 2079 | #if wxUSE_UNICODE |
4772f8e1 VS |
2080 | // NB: buffer returned by getenv() is allowed to be overwritten next |
2081 | // time getenv() is called, so it is OK to use static string | |
2082 | // buffer to hold the data. | |
2083 | static wxWCharBuffer value((wxChar*)NULL); | |
ddf14c13 | 2084 | value = wxConvLibc.cMB2WX(getenv(wxConvLibc.cWX2MB(name))); |
4772f8e1 | 2085 | return value.data(); |
92980e90 | 2086 | #else |
4772f8e1 | 2087 | return getenv(name); |
92980e90 | 2088 | #endif |
c9e089e9 OK |
2089 | } |
2090 | ||
b1ac3b56 | 2091 | int WXDLLEXPORT wxSystem(const wxChar *psz) |
c9e089e9 | 2092 | { |
ddf14c13 | 2093 | return system(wxConvLibc.cWX2MB(psz)); |
c9e089e9 OK |
2094 | } |
2095 | ||
33a7c3dd | 2096 | #endif // wxNEED_WX_STDLIB_H |
e7b3d6ba OK |
2097 | |
2098 | #ifdef wxNEED_WX_TIME_H | |
26378b41 VZ |
2099 | WXDLLEXPORT size_t |
2100 | wxStrftime(wxChar *s, size_t maxsize, const wxChar *fmt, const struct tm *tm) | |
e7b3d6ba | 2101 | { |
26378b41 VZ |
2102 | if ( !maxsize ) |
2103 | return 0; | |
f6f5941b | 2104 | |
26378b41 VZ |
2105 | wxCharBuffer buf(maxsize); |
2106 | ||
ddf14c13 | 2107 | wxCharBuffer bufFmt(wxConvLibc.cWX2MB(fmt)); |
26378b41 | 2108 | if ( !bufFmt ) |
b1ac3b56 | 2109 | return 0; |
26378b41 VZ |
2110 | |
2111 | size_t ret = strftime(buf.data(), maxsize, bufFmt, tm); | |
2112 | if ( !ret ) | |
2113 | return 0; | |
2114 | ||
ddf14c13 | 2115 | wxWCharBuffer wbuf = wxConvLibc.cMB2WX(buf); |
26378b41 VZ |
2116 | if ( !wbuf ) |
2117 | return 0; | |
2118 | ||
2119 | wxStrncpy(s, wbuf, maxsize); | |
2120 | return wxStrlen(s); | |
e7b3d6ba | 2121 | } |
33a7c3dd VZ |
2122 | #endif // wxNEED_WX_TIME_H |
2123 | ||
b63b07a8 RL |
2124 | #ifndef wxCtime |
2125 | WXDLLEXPORT wxChar *wxCtime(const time_t *timep) | |
2126 | { | |
9348da2f VZ |
2127 | // normally the string is 26 chars but give one more in case some broken |
2128 | // DOS compiler decides to use "\r\n" instead of "\n" at the end | |
2129 | static wxChar buf[27]; | |
2130 | ||
2131 | // ctime() is guaranteed to return a string containing only ASCII | |
2132 | // characters, as its format is always the same for any locale | |
2133 | wxStrncpy(buf, wxString::FromAscii(ctime(timep)), WXSIZEOF(buf)); | |
2134 | buf[WXSIZEOF(buf) - 1] = _T('\0'); | |
b63b07a8 RL |
2135 | |
2136 | return buf; | |
2137 | } | |
2138 | #endif // wxCtime | |
2139 | ||
33a7c3dd VZ |
2140 | #endif // wxUSE_WCHAR_T |
2141 | ||
2142 | // ---------------------------------------------------------------------------- | |
2143 | // functions which we may need even if !wxUSE_WCHAR_T | |
2144 | // ---------------------------------------------------------------------------- | |
2145 | ||
2146 | #ifndef wxStrtok | |
2147 | ||
2148 | WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr) | |
2149 | { | |
2150 | if (!psz) | |
2151 | { | |
2152 | psz = *save_ptr; | |
2153 | if ( !psz ) | |
2154 | return NULL; | |
2155 | } | |
2156 | ||
2157 | psz += wxStrspn(psz, delim); | |
2158 | if (!*psz) | |
2159 | { | |
2160 | *save_ptr = (wxChar *)NULL; | |
2161 | return (wxChar *)NULL; | |
2162 | } | |
2163 | ||
2164 | wxChar *ret = psz; | |
2165 | psz = wxStrpbrk(psz, delim); | |
2166 | if (!psz) | |
2167 | { | |
2168 | *save_ptr = (wxChar*)NULL; | |
2169 | } | |
2170 | else | |
2171 | { | |
2172 | *psz = wxT('\0'); | |
2173 | *save_ptr = psz + 1; | |
2174 | } | |
2175 | ||
2176 | return ret; | |
2177 | } | |
2178 | ||
2179 | #endif // wxStrtok | |
2180 | ||
2bb1e1f4 SC |
2181 | // ---------------------------------------------------------------------------- |
2182 | // missing C RTL functions | |
2183 | // ---------------------------------------------------------------------------- | |
2184 | ||
896e226a | 2185 | #ifdef wxNEED_STRDUP |
27db4210 | 2186 | |
2bb1e1f4 SC |
2187 | char *strdup(const char *s) |
2188 | { | |
d314acc0 SC |
2189 | char *dest = (char*) malloc( strlen( s ) + 1 ) ; |
2190 | if ( dest ) | |
2191 | strcpy( dest , s ) ; | |
2192 | return dest ; | |
2bb1e1f4 | 2193 | } |
27db4210 | 2194 | #endif // wxNEED_STRDUP |
0be9ace2 | 2195 | |
eae4425d | 2196 | #if defined(__WXWINCE__) && (_WIN32_WCE <= 211) |
27db4210 | 2197 | |
0be9ace2 JS |
2198 | void *calloc( size_t num, size_t size ) |
2199 | { | |
2200 | void** ptr = (void **)malloc(num * size); | |
2201 | memset( ptr, 0, num * size); | |
2202 | return ptr; | |
2203 | } | |
42d11c8e | 2204 | |
27db4210 | 2205 | #endif // __WXWINCE__ <= 211 |
4c663122 | 2206 | |
619be6d0 JS |
2207 | #ifdef __WXWINCE__ |
2208 | ||
2209 | int wxRemove(const wxChar *path) | |
2210 | { | |
2211 | return ::DeleteFile(path) == 0; | |
2212 | } | |
2213 | ||
2214 | #endif |