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