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