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