]>
Commit | Line | Data |
---|---|---|
dd0ef332 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/wxcrt.cpp | |
3 | // Purpose: wxChar CRT wrappers implementation | |
4 | // Author: Ove Kaven | |
5 | // Modified by: Ron Lee, Francesco Montorsi | |
6 | // Created: 09/04/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) wxWidgets copyright | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
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__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #include "wx/wxchar.h" | |
24 | ||
25 | #define _ISOC9X_SOURCE 1 // to get vsscanf() | |
26 | #define _BSD_SOURCE 1 // to still get strdup() | |
27 | ||
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | ||
32 | #ifndef __WXWINCE__ | |
33 | #include <time.h> | |
34 | #include <locale.h> | |
35 | #else | |
36 | #include "wx/msw/wince/time.h" | |
37 | #endif | |
38 | ||
39 | #ifndef WX_PRECOMP | |
40 | #include "wx/string.h" | |
41 | #include "wx/hash.h" | |
42 | #include "wx/utils.h" // for wxMin and wxMax | |
43 | #include "wx/log.h" | |
44 | #endif | |
45 | ||
46 | #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H) | |
47 | #include <windef.h> | |
48 | #include <winbase.h> | |
49 | #include <winnls.h> | |
50 | #include <winnt.h> | |
51 | #endif | |
52 | ||
17482893 VZ |
53 | #ifndef wxStrtoll |
54 | #ifdef __WXWINCE__ | |
55 | // there is no errno.h under CE apparently | |
56 | #define wxSET_ERRNO(value) | |
57 | #else | |
58 | #include <errno.h> | |
59 | ||
60 | #define wxSET_ERRNO(value) errno = value | |
61 | #endif | |
62 | #endif | |
63 | ||
dd0ef332 VS |
64 | #if defined(__MWERKS__) && __MSL__ >= 0x6000 |
65 | namespace std {} | |
66 | using namespace std ; | |
67 | #endif | |
68 | ||
69 | #if wxUSE_WCHAR_T | |
70 | size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n) | |
71 | { | |
72 | // assume that we have mbsrtowcs() too if we have wcsrtombs() | |
73 | #ifdef HAVE_WCSRTOMBS | |
74 | mbstate_t mbstate; | |
75 | memset(&mbstate, 0, sizeof(mbstate_t)); | |
76 | #endif | |
77 | ||
78 | if (buf) { | |
79 | if (!n || !*psz) { | |
80 | if (n) *buf = wxT('\0'); | |
81 | return 0; | |
82 | } | |
83 | #ifdef HAVE_WCSRTOMBS | |
84 | return mbsrtowcs(buf, &psz, n, &mbstate); | |
85 | #else | |
86 | return wxMbstowcs(buf, psz, n); | |
87 | #endif | |
88 | } | |
89 | ||
90 | // note that we rely on common (and required by Unix98 but unfortunately not | |
91 | // C99) extension which allows to call mbs(r)towcs() with NULL output pointer | |
92 | // to just get the size of the needed buffer -- this is needed as otherwise | |
93 | // we have no idea about how much space we need and if the CRT doesn't | |
94 | // support it (the only currently known example being Metrowerks, see | |
95 | // wx/wxchar.h) we don't use its mbstowcs() at all | |
96 | #ifdef HAVE_WCSRTOMBS | |
97 | return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate); | |
98 | #else | |
99 | return wxMbstowcs((wchar_t *) NULL, psz, 0); | |
100 | #endif | |
101 | } | |
102 | ||
103 | size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n) | |
104 | { | |
105 | #ifdef HAVE_WCSRTOMBS | |
106 | mbstate_t mbstate; | |
107 | memset(&mbstate, 0, sizeof(mbstate_t)); | |
108 | #endif | |
109 | ||
110 | if (buf) { | |
111 | if (!n || !*pwz) { | |
112 | // glibc2.1 chokes on null input | |
113 | if (n) *buf = '\0'; | |
114 | return 0; | |
115 | } | |
116 | #ifdef HAVE_WCSRTOMBS | |
117 | return wcsrtombs(buf, &pwz, n, &mbstate); | |
118 | #else | |
119 | return wxWcstombs(buf, pwz, n); | |
120 | #endif | |
121 | } | |
122 | ||
123 | #ifdef HAVE_WCSRTOMBS | |
124 | return wcsrtombs((char *) NULL, &pwz, 0, &mbstate); | |
125 | #else | |
126 | return wxWcstombs((char *) NULL, pwz, 0); | |
127 | #endif | |
128 | } | |
129 | #endif // wxUSE_WCHAR_T | |
130 | ||
131 | bool WXDLLEXPORT wxOKlibc() | |
132 | { | |
133 | #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__) | |
134 | // glibc 2.0 uses UTF-8 even when it shouldn't | |
135 | wchar_t res = 0; | |
136 | if ((MB_CUR_MAX == 2) && | |
137 | (wxMB2WC(&res, "\xdd\xa5", 1) == 1) && | |
138 | (res==0x765)) { | |
139 | // this is UTF-8 allright, check whether that's what we want | |
140 | char *cur_locale = setlocale(LC_CTYPE, NULL); | |
141 | if ((strlen(cur_locale) < 4) || | |
142 | (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8")) || | |
143 | (strcasecmp(cur_locale + strlen(cur_locale) - 5, "utf-8"))) { | |
144 | // nope, don't use libc conversion | |
145 | return false; | |
146 | } | |
147 | } | |
148 | #endif | |
149 | return true; | |
150 | } | |
151 | ||
152 | // ============================================================================ | |
153 | // printf() functions business | |
154 | // ============================================================================ | |
155 | ||
156 | // special test mode: define all functions below even if we don't really need | |
157 | // them to be able to test them | |
158 | #ifdef wxTEST_PRINTF | |
159 | #undef wxFprintf | |
160 | #undef wxPrintf | |
161 | #undef wxSprintf | |
162 | #undef wxVfprintf | |
163 | #undef wxVsprintf | |
164 | #undef wxVprintf | |
165 | #undef wxVsnprintf_ | |
dd0ef332 VS |
166 | |
167 | #define wxNEED_WPRINTF | |
168 | ||
169 | int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr ); | |
170 | #endif | |
171 | ||
dd0ef332 VS |
172 | #if defined(__DMC__) |
173 | /* Digital Mars adds count to _stprintf (C99) so convert */ | |
174 | #if wxUSE_UNICODE | |
2523e9b7 | 175 | int wxSprintf (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... ) |
dd0ef332 VS |
176 | { |
177 | va_list arglist; | |
178 | ||
179 | va_start( arglist, format ); | |
180 | int iLen = swprintf ( s, -1, format, arglist ); | |
181 | va_end( arglist ); | |
182 | return iLen ; | |
183 | } | |
184 | ||
185 | #endif // wxUSE_UNICODE | |
186 | ||
187 | #endif //__DMC__ | |
188 | ||
189 | // ---------------------------------------------------------------------------- | |
190 | // implement the standard IO functions for wide char if libc doesn't have them | |
191 | // ---------------------------------------------------------------------------- | |
192 | ||
193 | #ifdef wxNEED_FPUTS | |
194 | int wxFputs(const wchar_t *ws, FILE *stream) | |
195 | { | |
196 | wxCharBuffer buf(wxConvLibc.cWC2MB(ws)); | |
197 | if ( !buf ) | |
198 | return -1; | |
199 | ||
200 | // counting the number of wide characters written isn't worth the trouble, | |
201 | // simply distinguish between ok and error | |
202 | return fputs(buf, stream) == -1 ? -1 : 0; | |
203 | } | |
204 | #endif // wxNEED_FPUTS | |
205 | ||
206 | #ifdef wxNEED_PUTS | |
207 | int wxPuts(const wxChar *ws) | |
208 | { | |
209 | int rc = wxFputs(ws, stdout); | |
210 | if ( rc != -1 ) | |
211 | { | |
212 | if ( wxFputs(L"\n", stdout) == -1 ) | |
213 | return -1; | |
214 | ||
215 | rc++; | |
216 | } | |
217 | ||
218 | return rc; | |
219 | } | |
220 | #endif // wxNEED_PUTS | |
221 | ||
222 | #ifdef wxNEED_PUTC | |
223 | int /* not wint_t */ wxPutc(wchar_t wc, FILE *stream) | |
224 | { | |
225 | wchar_t ws[2] = { wc, L'\0' }; | |
226 | ||
227 | return wxFputs(ws, stream); | |
228 | } | |
229 | #endif // wxNEED_PUTC | |
230 | ||
231 | // NB: we only implement va_list functions here, the ones taking ... are | |
232 | // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse | |
233 | // the definitions there to avoid duplicating them here | |
234 | #ifdef wxNEED_WPRINTF | |
235 | ||
236 | // TODO: implement the scanf() functions | |
237 | int vwscanf(const wxChar *format, va_list argptr) | |
238 | { | |
239 | wxFAIL_MSG( _T("TODO") ); | |
240 | ||
241 | return -1; | |
242 | } | |
243 | ||
244 | int vswscanf(const wxChar *ws, const wxChar *format, va_list argptr) | |
245 | { | |
246 | // The best we can do without proper Unicode support in glibc is to | |
247 | // convert the strings into MB representation and run ANSI version | |
248 | // of the function. This doesn't work with %c and %s because of difference | |
249 | // in size of char and wchar_t, though. | |
250 | ||
251 | wxCHECK_MSG( wxStrstr(format, _T("%s")) == NULL, -1, | |
252 | _T("incomplete vswscanf implementation doesn't allow %s") ); | |
253 | wxCHECK_MSG( wxStrstr(format, _T("%c")) == NULL, -1, | |
254 | _T("incomplete vswscanf implementation doesn't allow %c") ); | |
255 | ||
256 | va_list argcopy; | |
257 | wxVaCopy(argcopy, argptr); | |
258 | return vsscanf(wxConvLibc.cWX2MB(ws), wxConvLibc.cWX2MB(format), argcopy); | |
259 | } | |
260 | ||
261 | int vfwscanf(FILE *stream, const wxChar *format, va_list argptr) | |
262 | { | |
263 | wxFAIL_MSG( _T("TODO") ); | |
264 | ||
265 | return -1; | |
266 | } | |
267 | ||
268 | #define vswprintf wxVsnprintf_ | |
269 | ||
270 | int vfwprintf(FILE *stream, const wxChar *format, va_list argptr) | |
271 | { | |
272 | wxString s; | |
273 | int rc = s.PrintfV(format, argptr); | |
274 | ||
275 | if ( rc != -1 ) | |
276 | { | |
277 | // we can't do much better without Unicode support in libc... | |
278 | if ( fprintf(stream, "%s", (const char*)s.mb_str() ) == -1 ) | |
279 | return -1; | |
280 | } | |
281 | ||
282 | return rc; | |
283 | } | |
284 | ||
285 | int vwprintf(const wxChar *format, va_list argptr) | |
286 | { | |
287 | return wxVfprintf(stdout, format, argptr); | |
288 | } | |
289 | ||
290 | #endif // wxNEED_WPRINTF | |
291 | ||
292 | #ifdef wxNEED_PRINTF_CONVERSION | |
293 | ||
294 | // ---------------------------------------------------------------------------- | |
295 | // wxFormatConverter: class doing the "%s" -> "%ls" conversion | |
296 | // ---------------------------------------------------------------------------- | |
297 | ||
298 | /* | |
299 | Here are the gory details. We want to follow the Windows/MS conventions, | |
300 | that is to have | |
301 | ||
302 | In ANSI mode: | |
303 | ||
304 | format specifier results in | |
305 | ----------------------------------- | |
306 | %c, %hc, %hC char | |
307 | %lc, %C, %lC wchar_t | |
308 | ||
309 | In Unicode mode: | |
310 | ||
311 | format specifier results in | |
312 | ----------------------------------- | |
313 | %hc, %C, %hC char | |
314 | %c, %lc, %lC wchar_t | |
315 | ||
316 | ||
317 | while on POSIX systems we have %C identical to %lc and %c always means char | |
318 | (in any mode) while %lc always means wchar_t, | |
319 | ||
320 | So to use native functions in order to get our semantics we must do the | |
321 | following translations in Unicode mode (nothing to do in ANSI mode): | |
322 | ||
323 | wxWidgets specifier POSIX specifier | |
324 | ---------------------------------------- | |
325 | ||
326 | %hc, %C, %hC %c | |
327 | %c %lc | |
328 | ||
329 | ||
330 | And, of course, the same should be done for %s as well. | |
331 | */ | |
332 | ||
333 | class wxFormatConverter | |
334 | { | |
335 | public: | |
336 | wxFormatConverter(const wxChar *format); | |
337 | ||
338 | // notice that we only translated the string if m_fmtOrig == NULL (as set | |
339 | // by CopyAllBefore()), otherwise we should simply use the original format | |
340 | operator const wxChar *() const | |
341 | { return m_fmtOrig ? m_fmtOrig : m_fmt.c_str(); } | |
342 | ||
343 | private: | |
344 | // copy another character to the translated format: this function does the | |
345 | // copy if we are translating but doesn't do anything at all if we don't, | |
346 | // so we don't create the translated format string at all unless we really | |
347 | // need to (i.e. InsertFmtChar() is called) | |
348 | wxChar CopyFmtChar(wxChar ch) | |
349 | { | |
350 | if ( !m_fmtOrig ) | |
351 | { | |
352 | // we're translating, do copy | |
353 | m_fmt += ch; | |
354 | } | |
355 | else | |
356 | { | |
357 | // simply increase the count which should be copied by | |
358 | // CopyAllBefore() later if needed | |
359 | m_nCopied++; | |
360 | } | |
361 | ||
362 | return ch; | |
363 | } | |
364 | ||
365 | // insert an extra character | |
366 | void InsertFmtChar(wxChar ch) | |
367 | { | |
368 | if ( m_fmtOrig ) | |
369 | { | |
370 | // so far we haven't translated anything yet | |
371 | CopyAllBefore(); | |
372 | } | |
373 | ||
374 | m_fmt += ch; | |
375 | } | |
376 | ||
377 | void CopyAllBefore() | |
378 | { | |
379 | wxASSERT_MSG( m_fmtOrig && m_fmt.empty(), _T("logic error") ); | |
380 | ||
381 | m_fmt = wxString(m_fmtOrig, m_nCopied); | |
382 | ||
383 | // we won't need it any longer | |
384 | m_fmtOrig = NULL; | |
385 | } | |
386 | ||
387 | static bool IsFlagChar(wxChar ch) | |
388 | { | |
389 | return ch == _T('-') || ch == _T('+') || | |
390 | ch == _T('0') || ch == _T(' ') || ch == _T('#'); | |
391 | } | |
392 | ||
393 | void SkipDigits(const wxChar **ptpc) | |
394 | { | |
395 | while ( **ptpc >= _T('0') && **ptpc <= _T('9') ) | |
396 | CopyFmtChar(*(*ptpc)++); | |
397 | } | |
398 | ||
399 | // the translated format | |
400 | wxString m_fmt; | |
401 | ||
402 | // the original format | |
403 | const wxChar *m_fmtOrig; | |
404 | ||
405 | // the number of characters already copied | |
406 | size_t m_nCopied; | |
407 | }; | |
408 | ||
409 | wxFormatConverter::wxFormatConverter(const wxChar *format) | |
410 | { | |
411 | m_fmtOrig = format; | |
412 | m_nCopied = 0; | |
413 | ||
414 | while ( *format ) | |
415 | { | |
416 | if ( CopyFmtChar(*format++) == _T('%') ) | |
417 | { | |
418 | // skip any flags | |
419 | while ( IsFlagChar(*format) ) | |
420 | CopyFmtChar(*format++); | |
421 | ||
422 | // and possible width | |
423 | if ( *format == _T('*') ) | |
424 | CopyFmtChar(*format++); | |
425 | else | |
426 | SkipDigits(&format); | |
427 | ||
428 | // precision? | |
429 | if ( *format == _T('.') ) | |
430 | { | |
431 | CopyFmtChar(*format++); | |
432 | if ( *format == _T('*') ) | |
433 | CopyFmtChar(*format++); | |
434 | else | |
435 | SkipDigits(&format); | |
436 | } | |
437 | ||
438 | // next we can have a size modifier | |
439 | enum | |
440 | { | |
441 | Default, | |
442 | Short, | |
443 | Long | |
444 | } size; | |
445 | ||
446 | switch ( *format ) | |
447 | { | |
448 | case _T('h'): | |
449 | size = Short; | |
450 | format++; | |
451 | break; | |
452 | ||
453 | case _T('l'): | |
454 | // "ll" has a different meaning! | |
455 | if ( format[1] != _T('l') ) | |
456 | { | |
457 | size = Long; | |
458 | format++; | |
459 | break; | |
460 | } | |
461 | //else: fall through | |
462 | ||
463 | default: | |
464 | size = Default; | |
465 | } | |
466 | ||
467 | // and finally we should have the type | |
468 | switch ( *format ) | |
469 | { | |
470 | case _T('C'): | |
471 | case _T('S'): | |
472 | // %C and %hC -> %c and %lC -> %lc | |
473 | if ( size == Long ) | |
474 | CopyFmtChar(_T('l')); | |
475 | ||
476 | InsertFmtChar(*format++ == _T('C') ? _T('c') : _T('s')); | |
477 | break; | |
478 | ||
479 | case _T('c'): | |
480 | case _T('s'): | |
481 | // %c -> %lc but %hc stays %hc and %lc is still %lc | |
482 | if ( size == Default) | |
483 | InsertFmtChar(_T('l')); | |
484 | // fall through | |
485 | ||
486 | default: | |
487 | // nothing special to do | |
488 | if ( size != Default ) | |
489 | CopyFmtChar(*(format - 1)); | |
490 | CopyFmtChar(*format++); | |
491 | } | |
492 | } | |
493 | } | |
494 | } | |
495 | ||
496 | #else // !wxNEED_PRINTF_CONVERSION | |
497 | // no conversion necessary | |
498 | #define wxFormatConverter(x) (x) | |
499 | #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION | |
500 | ||
501 | #ifdef __WXDEBUG__ | |
502 | // For testing the format converter | |
503 | wxString wxConvertFormat(const wxChar *format) | |
504 | { | |
505 | return wxString(wxFormatConverter(format)); | |
506 | } | |
507 | #endif | |
508 | ||
509 | // ---------------------------------------------------------------------------- | |
510 | // wxPrintf(), wxScanf() and relatives | |
511 | // ---------------------------------------------------------------------------- | |
512 | ||
513 | #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF) | |
514 | ||
2523e9b7 | 515 | int wxCRT_Scanf( const wxChar *format, ... ) |
dd0ef332 VS |
516 | { |
517 | va_list argptr; | |
518 | va_start(argptr, format); | |
519 | ||
520 | int ret = vwscanf(wxFormatConverter(format), argptr ); | |
521 | ||
522 | va_end(argptr); | |
523 | ||
524 | return ret; | |
525 | } | |
526 | ||
2523e9b7 | 527 | int wxCRT_Sscanf( const wxChar *str, const wxChar *format, ... ) |
dd0ef332 VS |
528 | { |
529 | va_list argptr; | |
530 | va_start(argptr, format); | |
531 | ||
532 | int ret = vswscanf( str, wxFormatConverter(format), argptr ); | |
533 | ||
534 | va_end(argptr); | |
535 | ||
536 | return ret; | |
537 | } | |
538 | ||
2523e9b7 | 539 | int wxCRT_Fscanf( FILE *stream, const wxChar *format, ... ) |
dd0ef332 VS |
540 | { |
541 | va_list argptr; | |
542 | va_start(argptr, format); | |
543 | int ret = vfwscanf(stream, wxFormatConverter(format), argptr); | |
544 | ||
545 | va_end(argptr); | |
546 | ||
547 | return ret; | |
548 | } | |
549 | ||
2523e9b7 | 550 | int wxCRT_Printf( const wxChar *format, ... ) |
dd0ef332 VS |
551 | { |
552 | va_list argptr; | |
553 | va_start(argptr, format); | |
554 | ||
555 | int ret = vwprintf( wxFormatConverter(format), argptr ); | |
556 | ||
557 | va_end(argptr); | |
558 | ||
559 | return ret; | |
560 | } | |
561 | ||
2523e9b7 | 562 | int wxCRT_Fprintf( FILE *stream, const wxChar *format, ... ) |
dd0ef332 VS |
563 | { |
564 | va_list argptr; | |
2523e9b7 | 565 | va_start( argptr, format ); |
dd0ef332 | 566 | |
2523e9b7 | 567 | int ret = vfwprintf( stream, wxFormatConverter(format), argptr ); |
dd0ef332 VS |
568 | |
569 | va_end(argptr); | |
570 | ||
571 | return ret; | |
572 | } | |
dd0ef332 | 573 | |
2523e9b7 VS |
574 | int wxCRT_Vsscanf( const wxChar *str, const wxChar *format, va_list argptr ) |
575 | { | |
576 | return vswscanf( str, wxFormatConverter(format), argptr ); | |
577 | } | |
578 | ||
579 | int wxCRT_Vfprintf( FILE *stream, const wxChar *format, va_list argptr ) | |
580 | { | |
581 | return vfwprintf( stream, wxFormatConverter(format), argptr ); | |
582 | } | |
583 | ||
584 | int wxCRT_Vprintf( const wxChar *format, va_list argptr ) | |
585 | { | |
586 | return vwprintf( wxFormatConverter(format), argptr ); | |
587 | } | |
588 | ||
589 | #ifndef wxCRT_Vsnprintf | |
590 | int wxCRT_Vsnprintf( wxChar *str, size_t size, const wxChar *format, va_list argptr ) | |
591 | { | |
592 | return vswprintf( str, size, wxFormatConverter(format), argptr ); | |
593 | } | |
594 | #endif // wxCRT_Vsnprintf | |
595 | ||
596 | int wxCRT_Vsprintf( wxChar *str, const wxChar *format, va_list argptr ) | |
597 | { | |
598 | // same as for wxSprintf() | |
599 | return vswprintf(str, INT_MAX / 4, wxFormatConverter(format), argptr); | |
600 | } | |
601 | ||
602 | #endif // wxNEED_PRINTF_CONVERSION | |
603 | ||
604 | ||
605 | // ---------------------------------------------------------------------------- | |
606 | // wrappers to printf and scanf function families | |
607 | // ---------------------------------------------------------------------------- | |
608 | ||
d1f6e2cf VS |
609 | #if !wxUSE_UTF8_LOCALE_ONLY |
610 | int wxDoSprintfWchar(char *str, const wxChar *format, ...) | |
dd0ef332 VS |
611 | { |
612 | va_list argptr; | |
613 | va_start(argptr, format); | |
614 | ||
2523e9b7 | 615 | int rv = wxVsprintf(str, format, argptr); |
dd0ef332 VS |
616 | |
617 | va_end(argptr); | |
2523e9b7 VS |
618 | return rv; |
619 | } | |
d1f6e2cf VS |
620 | #endif // !wxUSE_UTF8_LOCALE_ONLY |
621 | ||
622 | #if wxUSE_UNICODE_UTF8 | |
623 | int wxDoSprintfUtf8(char *str, const char *format, ...) | |
624 | { | |
625 | va_list argptr; | |
626 | va_start(argptr, format); | |
627 | ||
628 | int rv = wxVsprintf(str, format, argptr); | |
629 | ||
630 | va_end(argptr); | |
631 | return rv; | |
632 | } | |
633 | #endif // wxUSE_UNICODE_UTF8 | |
2523e9b7 VS |
634 | |
635 | #if wxUSE_UNICODE | |
d1f6e2cf VS |
636 | |
637 | #if !wxUSE_UTF8_LOCALE_ONLY | |
638 | int wxDoSprintfWchar(wchar_t *str, const wxChar *format, ...) | |
2523e9b7 VS |
639 | { |
640 | va_list argptr; | |
641 | va_start(argptr, format); | |
dd0ef332 | 642 | |
2523e9b7 VS |
643 | int rv = wxVsprintf(str, format, argptr); |
644 | ||
645 | va_end(argptr); | |
646 | return rv; | |
dd0ef332 | 647 | } |
d1f6e2cf VS |
648 | #endif // !wxUSE_UTF8_LOCALE_ONLY |
649 | ||
650 | #if wxUSE_UNICODE_UTF8 | |
651 | int wxDoSprintfUtf8(wchar_t *str, const char *format, ...) | |
652 | { | |
653 | va_list argptr; | |
654 | va_start(argptr, format); | |
655 | ||
656 | int rv = wxVsprintf(str, format, argptr); | |
657 | ||
658 | va_end(argptr); | |
659 | return rv; | |
660 | } | |
661 | #endif // wxUSE_UNICODE_UTF8 | |
662 | ||
663 | #endif // wxUSE_UNICODE | |
664 | ||
665 | #if !wxUSE_UTF8_LOCALE_ONLY | |
666 | int wxDoSnprintfWchar(char *str, size_t size, const wxChar *format, ...) | |
667 | { | |
668 | va_list argptr; | |
669 | va_start(argptr, format); | |
dd0ef332 | 670 | |
d1f6e2cf VS |
671 | int rv = wxVsnprintf(str, size, format, argptr); |
672 | ||
673 | va_end(argptr); | |
674 | return rv; | |
675 | } | |
676 | #endif // !wxUSE_UTF8_LOCALE_ONLY | |
677 | ||
678 | #if wxUSE_UNICODE_UTF8 | |
679 | int wxDoSnprintfUtf8(char *str, size_t size, const char *format, ...) | |
dd0ef332 VS |
680 | { |
681 | va_list argptr; | |
2523e9b7 | 682 | va_start(argptr, format); |
dd0ef332 | 683 | |
2523e9b7 | 684 | int rv = wxVsnprintf(str, size, format, argptr); |
dd0ef332 VS |
685 | |
686 | va_end(argptr); | |
2523e9b7 VS |
687 | return rv; |
688 | } | |
d1f6e2cf | 689 | #endif // wxUSE_UNICODE_UTF8 |
dd0ef332 | 690 | |
2523e9b7 | 691 | #if wxUSE_UNICODE |
d1f6e2cf VS |
692 | |
693 | #if !wxUSE_UTF8_LOCALE_ONLY | |
694 | int wxDoSnprintfWchar(wchar_t *str, size_t size, const wxChar *format, ...) | |
2523e9b7 VS |
695 | { |
696 | va_list argptr; | |
697 | va_start(argptr, format); | |
698 | ||
699 | int rv = wxVsnprintf(str, size, format, argptr); | |
700 | ||
701 | va_end(argptr); | |
702 | return rv; | |
dd0ef332 | 703 | } |
d1f6e2cf VS |
704 | #endif // !wxUSE_UTF8_LOCALE_ONLY |
705 | ||
706 | #if wxUSE_UNICODE_UTF8 | |
707 | int wxDoSnprintfUtf8(wchar_t *str, size_t size, const char *format, ...) | |
708 | { | |
709 | va_list argptr; | |
710 | va_start(argptr, format); | |
711 | ||
712 | int rv = wxVsnprintf(str, size, format, argptr); | |
713 | ||
714 | va_end(argptr); | |
715 | return rv; | |
716 | } | |
717 | #endif // wxUSE_UNICODE_UTF8 | |
718 | ||
719 | #endif // wxUSE_UNICODE | |
dd0ef332 | 720 | |
2523e9b7 VS |
721 | |
722 | #ifdef HAVE_BROKEN_VSNPRINTF_DECL | |
723 | #define vsnprintf wx_fixed_vsnprintf | |
724 | #endif | |
725 | ||
726 | #if wxUSE_UNICODE | |
d1f6e2cf VS |
727 | |
728 | #if !wxUSE_UTF8_LOCALE_ONLY | |
2523e9b7 | 729 | static int ConvertStringToBuf(const wxString& s, char *out, size_t outsize) |
dd0ef332 | 730 | { |
2523e9b7 VS |
731 | const wxWX2WCbuf buf = s.wc_str(); |
732 | ||
733 | size_t len = wxConvLibc.FromWChar(out, outsize, buf); | |
734 | if ( len != wxCONV_FAILED ) | |
735 | return len-1; | |
736 | else | |
737 | return wxConvLibc.FromWChar(NULL, 0, buf); | |
dd0ef332 | 738 | } |
d1f6e2cf | 739 | #endif // !wxUSE_UTF8_LOCALE_ONLY |
dd0ef332 | 740 | |
2523e9b7 VS |
741 | #if wxUSE_UNICODE_UTF8 |
742 | static int ConvertStringToBuf(const wxString& s, wchar_t *out, size_t outsize) | |
dd0ef332 | 743 | { |
2523e9b7 VS |
744 | const wxWX2WCbuf buf(s.wc_str()); |
745 | size_t len = wxWcslen(buf); | |
746 | if ( outsize > len ) | |
747 | memcpy(out, buf, (len+1) * sizeof(wchar_t)); | |
748 | // else: not enough space | |
749 | return len; | |
dd0ef332 | 750 | } |
d1f6e2cf | 751 | #endif // wxUSE_UNICODE_UTF8 |
dd0ef332 | 752 | |
2523e9b7 VS |
753 | template<typename T> |
754 | static size_t PrintfViaString(T *out, size_t outsize, | |
755 | const wxString& format, va_list argptr) | |
dd0ef332 | 756 | { |
2523e9b7 VS |
757 | va_list argcopy; |
758 | wxVaCopy(argcopy, argptr); | |
759 | ||
760 | wxString s; | |
761 | s.PrintfV(format, argcopy); | |
762 | ||
763 | return ConvertStringToBuf(s, out, outsize); | |
dd0ef332 | 764 | } |
2523e9b7 | 765 | #endif // wxUSE_UNICODE |
dd0ef332 | 766 | |
2523e9b7 | 767 | int wxVsprintf(char *str, const wxString& format, va_list argptr) |
dd0ef332 | 768 | { |
2523e9b7 VS |
769 | va_list argcopy; |
770 | wxVaCopy(argcopy, argptr); | |
771 | ||
772 | #if wxUSE_UTF8_LOCALE_ONLY | |
773 | return vsprintf(str, format.wx_str(), argcopy); | |
774 | #else | |
775 | #if wxUSE_UNICODE_UTF8 | |
776 | if ( wxLocaleIsUtf8 ) | |
777 | return vsprintf(str, format.wx_str(), argcopy); | |
778 | else | |
779 | #endif | |
780 | #if wxUSE_UNICODE | |
781 | return PrintfViaString(str, wxNO_LEN, format, argcopy); | |
782 | #else | |
783 | return wxCRT_Vsprintf(str, format, argcopy); | |
784 | #endif | |
785 | #endif | |
dd0ef332 | 786 | } |
dd0ef332 | 787 | |
2523e9b7 VS |
788 | #if wxUSE_UNICODE |
789 | int wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) | |
dd0ef332 | 790 | { |
2523e9b7 VS |
791 | va_list argcopy; |
792 | wxVaCopy(argcopy, argptr); | |
793 | ||
794 | #if wxUSE_UNICODE_WCHAR | |
795 | return wxCRT_Vsprintf(str, format, argcopy); | |
796 | #else // wxUSE_UNICODE_UTF8 | |
797 | #if !wxUSE_UTF8_LOCALE_ONLY | |
798 | if ( !wxLocaleIsUtf8 ) | |
799 | return wxCRT_Vsprintf(str, format, argcopy); | |
800 | else | |
801 | #endif | |
802 | return PrintfViaString(str, wxNO_LEN, format, argcopy); | |
803 | #endif // wxUSE_UNICODE_UTF8 | |
dd0ef332 | 804 | } |
2523e9b7 | 805 | #endif // wxUSE_UNICODE |
dd0ef332 | 806 | |
2523e9b7 VS |
807 | int wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr) |
808 | { | |
809 | int rv; | |
810 | va_list argcopy; | |
811 | wxVaCopy(argcopy, argptr); | |
812 | ||
813 | #if wxUSE_UTF8_LOCALE_ONLY | |
814 | rv = vsnprintf(str, size, format.wx_str(), argcopy); | |
815 | #else | |
816 | #if wxUSE_UNICODE_UTF8 | |
817 | if ( wxLocaleIsUtf8 ) | |
818 | rv = vsnprintf(str, size, format.wx_str(), argcopy); | |
819 | else | |
820 | #endif | |
821 | #if wxUSE_UNICODE | |
822 | { | |
823 | // NB: if this code is called, then wxString::PrintV() would use the | |
824 | // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV() | |
825 | // from here | |
826 | rv = PrintfViaString(str, size, format, argcopy); | |
827 | } | |
828 | #else | |
829 | rv = wxCRT_Vsnprintf(str, size, format, argcopy); | |
830 | #endif | |
831 | #endif | |
832 | ||
833 | // VsnprintfTestCase reveals that glibc's implementation of vswprintf | |
834 | // doesn't nul terminate on truncation. | |
835 | str[size - 1] = 0; | |
836 | ||
837 | return rv; | |
838 | } | |
839 | ||
840 | #if wxUSE_UNICODE | |
841 | int wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr) | |
842 | { | |
843 | int rv; | |
844 | va_list argcopy; | |
845 | wxVaCopy(argcopy, argptr); | |
846 | ||
847 | #if wxUSE_UNICODE_WCHAR | |
848 | rv = wxCRT_Vsnprintf(str, size, format, argcopy); | |
849 | #else // wxUSE_UNICODE_UTF8 | |
850 | #if !wxUSE_UTF8_LOCALE_ONLY | |
851 | if ( !wxLocaleIsUtf8 ) | |
852 | rv = wxCRT_Vsnprintf(str, size, format, argcopy); | |
853 | else | |
854 | #endif | |
855 | { | |
856 | // NB: if this code is called, then wxString::PrintV() would use the | |
857 | // char* version of wxVsnprintf(), so it's safe to use PrintV() | |
858 | // from here | |
859 | rv = PrintfViaString(str, size, format, argcopy); | |
860 | } | |
861 | #endif // wxUSE_UNICODE_UTF8 | |
862 | ||
863 | // VsnprintfTestCase reveals that glibc's implementation of vswprintf | |
864 | // doesn't nul terminate on truncation. | |
865 | str[size - 1] = 0; | |
866 | ||
867 | return rv; | |
868 | } | |
869 | #endif // wxUSE_UNICODE | |
dd0ef332 VS |
870 | |
871 | #if wxUSE_WCHAR_T | |
872 | ||
873 | // ---------------------------------------------------------------------------- | |
874 | // ctype.h stuff (currently unused) | |
875 | // ---------------------------------------------------------------------------- | |
876 | ||
877 | #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H) | |
878 | inline WORD wxMSW_ctype(wxChar ch) | |
879 | { | |
880 | WORD ret; | |
881 | GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret); | |
882 | return ret; | |
883 | } | |
884 | ||
885 | WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); } | |
886 | WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); } | |
887 | WXDLLEXPORT int wxIscntrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; } | |
888 | WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; } | |
889 | WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); } | |
890 | WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); } | |
891 | WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); } | |
892 | WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; } | |
893 | WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; } | |
894 | WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); } | |
895 | WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; } | |
896 | WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); } | |
897 | WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); } | |
898 | #endif | |
899 | ||
900 | #ifdef wxNEED_WX_MBSTOWCS | |
901 | ||
902 | WXDLLEXPORT size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen) | |
903 | { | |
904 | if (!out) | |
905 | { | |
906 | size_t outsize = 0; | |
907 | while(*in++) | |
908 | outsize++; | |
909 | return outsize; | |
910 | } | |
911 | ||
912 | const char* origin = in; | |
913 | ||
914 | while (outlen-- && *in) | |
915 | { | |
916 | *out++ = (wchar_t) *in++; | |
917 | } | |
918 | ||
919 | *out = '\0'; | |
920 | ||
921 | return in - origin; | |
922 | } | |
923 | ||
924 | WXDLLEXPORT size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen) | |
925 | { | |
926 | if (!out) | |
927 | { | |
928 | size_t outsize = 0; | |
929 | while(*in++) | |
930 | outsize++; | |
931 | return outsize; | |
932 | } | |
933 | ||
934 | const wchar_t* origin = in; | |
935 | ||
936 | while (outlen-- && *in) | |
937 | { | |
938 | *out++ = (char) *in++; | |
939 | } | |
940 | ||
941 | *out = '\0'; | |
942 | ||
943 | return in - origin; | |
944 | } | |
945 | ||
946 | #endif // wxNEED_WX_MBSTOWCS | |
947 | ||
948 | #if defined(wxNEED_WX_CTYPE_H) | |
949 | ||
950 | #include <CoreFoundation/CoreFoundation.h> | |
951 | ||
952 | #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric) | |
953 | #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter) | |
954 | #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl) | |
955 | #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit) | |
956 | //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' ' | |
957 | #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter) | |
958 | //CFCharacterSetRef cfprintset = !kCFCharacterSetControl | |
959 | #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation) | |
960 | #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline) | |
961 | #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter) | |
962 | ||
963 | WXDLLEXPORT int wxIsalnum(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalnumset, ch); } | |
964 | WXDLLEXPORT int wxIsalpha(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalphaset, ch); } | |
965 | WXDLLEXPORT int wxIscntrl(wxChar ch) { return CFCharacterSetIsCharacterMember(cfcntrlset, ch); } | |
966 | WXDLLEXPORT int wxIsdigit(wxChar ch) { return CFCharacterSetIsCharacterMember(cfdigitset, ch); } | |
967 | WXDLLEXPORT int wxIsgraph(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch) && ch != ' '; } | |
968 | WXDLLEXPORT int wxIslower(wxChar ch) { return CFCharacterSetIsCharacterMember(cflowerset, ch); } | |
969 | WXDLLEXPORT int wxIsprint(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch); } | |
970 | WXDLLEXPORT int wxIspunct(wxChar ch) { return CFCharacterSetIsCharacterMember(cfpunctset, ch); } | |
971 | WXDLLEXPORT int wxIsspace(wxChar ch) { return CFCharacterSetIsCharacterMember(cfspaceset, ch); } | |
972 | WXDLLEXPORT int wxIsupper(wxChar ch) { return CFCharacterSetIsCharacterMember(cfupperset, ch); } | |
973 | WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxIsdigit(ch) || (ch>='a' && ch<='f') || (ch>='A' && ch<='F'); } | |
974 | WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)tolower((char)(ch)); } | |
975 | WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)toupper((char)(ch)); } | |
976 | ||
977 | #endif // wxNEED_WX_CTYPE_H | |
978 | ||
979 | #ifndef wxStrdupA | |
980 | ||
981 | WXDLLEXPORT char *wxStrdupA(const char *s) | |
982 | { | |
983 | return strcpy((char *)malloc(strlen(s) + 1), s); | |
984 | } | |
985 | ||
986 | #endif // wxStrdupA | |
987 | ||
988 | #ifndef wxStrdupW | |
989 | ||
990 | WXDLLEXPORT wchar_t * wxStrdupW(const wchar_t *pwz) | |
991 | { | |
992 | size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t); | |
993 | wchar_t *ret = (wchar_t *) malloc(size); | |
994 | memcpy(ret, pwz, size); | |
995 | return ret; | |
996 | } | |
997 | ||
998 | #endif // wxStrdupW | |
999 | ||
1000 | #ifndef wxStricmp | |
1001 | int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2) | |
1002 | { | |
1003 | register wxChar c1, c2; | |
1004 | do { | |
1005 | c1 = wxTolower(*psz1++); | |
1006 | c2 = wxTolower(*psz2++); | |
1007 | } while ( c1 && (c1 == c2) ); | |
1008 | return c1 - c2; | |
1009 | } | |
1010 | #endif | |
1011 | ||
1012 | #ifndef wxStricmp | |
1013 | int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n) | |
1014 | { | |
1015 | // initialize the variables just to suppress stupid gcc warning | |
1016 | register wxChar c1 = 0, c2 = 0; | |
1017 | while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++; | |
1018 | if (n) { | |
1019 | if (c1 < c2) return -1; | |
1020 | if (c1 > c2) return 1; | |
1021 | } | |
1022 | return 0; | |
1023 | } | |
1024 | #endif | |
1025 | ||
cb352236 VS |
1026 | #ifndef wxSetlocale_ |
1027 | wxWCharBuffer wxSetlocale_(int category, const wxChar *locale) | |
dd0ef332 VS |
1028 | { |
1029 | char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale)); | |
1030 | ||
1031 | return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld)); | |
1032 | } | |
cb352236 VS |
1033 | |
1034 | wxWCharBuffer wxSetlocale(int category, const wxChar *locale) | |
1035 | { | |
1036 | wxWCharBuffer rv = wxSetlocale_(category, locale); | |
1037 | if ( rv ) | |
1038 | wxUpdateLocaleIsUtf8(); | |
1039 | return rv; | |
1040 | } | |
1041 | #else // defined(wxSetlocale_) | |
db2a7aab | 1042 | wxChar *wxSetlocale(int category, const wxChar *locale) |
cb352236 | 1043 | { |
db2a7aab | 1044 | wxChar *rv = wxSetlocale_(category, locale); |
cb352236 VS |
1045 | if ( rv ) |
1046 | wxUpdateLocaleIsUtf8(); | |
1047 | return rv; | |
1048 | } | |
1049 | #endif // wxSetlocale_ defined or not | |
dd0ef332 VS |
1050 | |
1051 | #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN) | |
1052 | WXDLLEXPORT size_t wxWcslen(const wchar_t *s) | |
1053 | { | |
1054 | size_t n = 0; | |
1055 | while ( *s++ ) | |
1056 | n++; | |
1057 | ||
1058 | return n; | |
1059 | } | |
1060 | #endif | |
1061 | ||
1062 | // ---------------------------------------------------------------------------- | |
1063 | // string.h functions | |
1064 | // ---------------------------------------------------------------------------- | |
1065 | ||
1066 | #ifdef wxNEED_WX_STRING_H | |
1067 | ||
1068 | // RN: These need to be c externed for the regex lib | |
1069 | #ifdef __cplusplus | |
1070 | extern "C" { | |
1071 | #endif | |
1072 | ||
1073 | WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src) | |
1074 | { | |
1075 | wxChar *ret = dest; | |
1076 | while (*dest) dest++; | |
1077 | while ((*dest++ = *src++)); | |
1078 | return ret; | |
1079 | } | |
1080 | ||
1081 | WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c) | |
1082 | { | |
1083 | // be careful here as the terminating NUL makes part of the string | |
1084 | while ( *s != c ) | |
1085 | { | |
1086 | if ( !*s++ ) | |
1087 | return NULL; | |
1088 | } | |
1089 | ||
1090 | return s; | |
1091 | } | |
1092 | ||
1093 | WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2) | |
1094 | { | |
1095 | while ((*s1 == *s2) && *s1) s1++, s2++; | |
1096 | if ((wxUChar)*s1 < (wxUChar)*s2) return -1; | |
1097 | if ((wxUChar)*s1 > (wxUChar)*s2) return 1; | |
1098 | return 0; | |
1099 | } | |
1100 | ||
1101 | WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src) | |
1102 | { | |
1103 | wxChar *ret = dest; | |
1104 | while ((*dest++ = *src++)); | |
1105 | return ret; | |
1106 | } | |
1107 | ||
1108 | WXDLLEXPORT size_t wxStrlen_(const wxChar *s) | |
1109 | { | |
1110 | size_t n = 0; | |
1111 | while ( *s++ ) | |
1112 | n++; | |
1113 | ||
1114 | return n; | |
1115 | } | |
1116 | ||
1117 | ||
1118 | WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n) | |
1119 | { | |
1120 | wxChar *ret = dest; | |
1121 | while (*dest) dest++; | |
1122 | while (n && (*dest++ = *src++)) n--; | |
1123 | return ret; | |
1124 | } | |
1125 | ||
1126 | WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n) | |
1127 | { | |
1128 | while (n && (*s1 == *s2) && *s1) n--, s1++, s2++; | |
1129 | if (n) { | |
1130 | if ((wxUChar)*s1 < (wxUChar)*s2) return -1; | |
1131 | if ((wxUChar)*s1 > (wxUChar)*s2) return 1; | |
1132 | } | |
1133 | return 0; | |
1134 | } | |
1135 | ||
1136 | WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n) | |
1137 | { | |
1138 | wxChar *ret = dest; | |
1139 | while (n && (*dest++ = *src++)) n--; | |
1140 | while (n) *dest++=0, n--; // the docs specify padding with zeroes | |
1141 | return ret; | |
1142 | } | |
1143 | ||
1144 | WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept) | |
1145 | { | |
1146 | while (*s && !wxStrchr(accept, *s)) | |
1147 | s++; | |
1148 | ||
1149 | return *s ? s : NULL; | |
1150 | } | |
1151 | ||
1152 | WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c) | |
1153 | { | |
1154 | const wxChar *ret = NULL; | |
1155 | do | |
1156 | { | |
1157 | if ( *s == c ) | |
1158 | ret = s; | |
1159 | s++; | |
1160 | } | |
1161 | while ( *s ); | |
1162 | ||
1163 | return ret; | |
1164 | } | |
1165 | ||
1166 | WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept) | |
1167 | { | |
1168 | size_t len = 0; | |
1169 | while (wxStrchr(accept, *s++)) len++; | |
1170 | return len; | |
1171 | } | |
1172 | ||
1173 | WXDLLEXPORT const wxChar *wxStrstr(const wxChar *haystack, const wxChar *needle) | |
1174 | { | |
1175 | wxASSERT_MSG( needle != NULL, _T("NULL argument in wxStrstr") ); | |
1176 | ||
1177 | // VZ: this is not exactly the most efficient string search algorithm... | |
1178 | ||
1179 | const size_t len = wxStrlen(needle); | |
1180 | ||
1181 | while ( const wxChar *fnd = wxStrchr(haystack, *needle) ) | |
1182 | { | |
1183 | if ( !wxStrncmp(fnd, needle, len) ) | |
1184 | return fnd; | |
1185 | ||
1186 | haystack = fnd + 1; | |
1187 | } | |
1188 | ||
1189 | return NULL; | |
1190 | } | |
1191 | ||
1192 | #ifdef __cplusplus | |
1193 | } | |
1194 | #endif | |
1195 | ||
1196 | WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr) | |
1197 | { | |
1198 | const wxChar *start = nptr; | |
1199 | ||
1200 | // FIXME: only correct for C locale | |
1201 | while (wxIsspace(*nptr)) nptr++; | |
1202 | if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++; | |
1203 | while (wxIsdigit(*nptr)) nptr++; | |
1204 | if (*nptr == wxT('.')) { | |
1205 | nptr++; | |
1206 | while (wxIsdigit(*nptr)) nptr++; | |
1207 | } | |
1208 | if (*nptr == wxT('E') || *nptr == wxT('e')) { | |
1209 | nptr++; | |
1210 | if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++; | |
1211 | while (wxIsdigit(*nptr)) nptr++; | |
1212 | } | |
1213 | ||
1214 | wxString data(nptr, nptr-start); | |
1215 | wxWX2MBbuf dat = data.mb_str(wxConvLibc); | |
1216 | char *rdat = wxMBSTRINGCAST dat; | |
1217 | double ret = strtod(dat, &rdat); | |
1218 | ||
1219 | if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat)); | |
1220 | ||
1221 | return ret; | |
1222 | } | |
1223 | ||
1224 | WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base) | |
1225 | { | |
1226 | const wxChar *start = nptr; | |
1227 | ||
1228 | // FIXME: only correct for C locale | |
1229 | while (wxIsspace(*nptr)) nptr++; | |
1230 | if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++; | |
1231 | if (((base == 0) || (base == 16)) && | |
1232 | (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) { | |
1233 | nptr += 2; | |
1234 | base = 16; | |
1235 | } | |
1236 | else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8; | |
1237 | else if (base == 0) base = 10; | |
1238 | ||
1239 | while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) || | |
1240 | (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++; | |
1241 | ||
1242 | wxString data(start, nptr-start); | |
1243 | wxWX2MBbuf dat = data.mb_str(wxConvLibc); | |
1244 | char *rdat = wxMBSTRINGCAST dat; | |
1245 | long int ret = strtol(dat, &rdat, base); | |
1246 | ||
1247 | if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat)); | |
1248 | ||
1249 | return ret; | |
1250 | } | |
1251 | ||
1252 | WXDLLEXPORT unsigned long int wxStrtoul(const wxChar *nptr, wxChar **endptr, int base) | |
1253 | { | |
1254 | return (unsigned long int) wxStrtol(nptr, endptr, base); | |
1255 | } | |
1256 | ||
1257 | #endif // wxNEED_WX_STRING_H | |
1258 | ||
1259 | #ifdef wxNEED_WX_STDIO_H | |
1260 | WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode) | |
1261 | { | |
1262 | char mode_buffer[10]; | |
1263 | for (size_t i = 0; i < wxStrlen(mode)+1; i++) | |
1264 | mode_buffer[i] = (char) mode[i]; | |
1265 | ||
1266 | return fopen( wxConvFile.cWX2MB(path), mode_buffer ); | |
1267 | } | |
1268 | ||
1269 | WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream) | |
1270 | { | |
1271 | char mode_buffer[10]; | |
1272 | for (size_t i = 0; i < wxStrlen(mode)+1; i++) | |
1273 | mode_buffer[i] = (char) mode[i]; | |
1274 | ||
1275 | return freopen( wxConvFile.cWX2MB(path), mode_buffer, stream ); | |
1276 | } | |
1277 | ||
1278 | WXDLLEXPORT int wxRemove(const wxChar *path) | |
1279 | { | |
1280 | return remove( wxConvFile.cWX2MB(path) ); | |
1281 | } | |
1282 | ||
1283 | WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath) | |
1284 | { | |
1285 | return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) ); | |
1286 | } | |
1287 | #endif | |
1288 | ||
1289 | #ifndef wxAtof | |
1290 | double WXDLLEXPORT wxAtof(const wxChar *psz) | |
1291 | { | |
1292 | #ifdef __WXWINCE__ | |
1293 | double d; | |
1294 | wxString str(psz); | |
1295 | if (str.ToDouble(& d)) | |
1296 | return d; | |
1297 | ||
1298 | return 0.0; | |
1299 | #else | |
1300 | return atof(wxConvLibc.cWX2MB(psz)); | |
1301 | #endif | |
1302 | } | |
1303 | #endif | |
1304 | ||
1305 | #ifdef wxNEED_WX_STDLIB_H | |
1306 | int WXDLLEXPORT wxAtoi(const wxChar *psz) | |
1307 | { | |
1308 | return atoi(wxConvLibc.cWX2MB(psz)); | |
1309 | } | |
1310 | ||
1311 | long WXDLLEXPORT wxAtol(const wxChar *psz) | |
1312 | { | |
1313 | return atol(wxConvLibc.cWX2MB(psz)); | |
1314 | } | |
1315 | ||
1316 | wxChar * WXDLLEXPORT wxGetenv(const wxChar *name) | |
1317 | { | |
1318 | #if wxUSE_UNICODE | |
1319 | // NB: buffer returned by getenv() is allowed to be overwritten next | |
1320 | // time getenv() is called, so it is OK to use static string | |
1321 | // buffer to hold the data. | |
1322 | static wxWCharBuffer value((wxChar*)NULL); | |
1323 | value = wxConvLibc.cMB2WX(getenv(wxConvLibc.cWX2MB(name))); | |
1324 | return value.data(); | |
1325 | #else | |
1326 | return getenv(name); | |
1327 | #endif | |
1328 | } | |
1329 | ||
1330 | int WXDLLEXPORT wxSystem(const wxChar *psz) | |
1331 | { | |
1332 | return system(wxConvLibc.cWX2MB(psz)); | |
1333 | } | |
1334 | ||
1335 | #endif // wxNEED_WX_STDLIB_H | |
1336 | ||
1337 | #ifdef wxNEED_WX_TIME_H | |
1338 | WXDLLEXPORT size_t | |
1339 | wxStrftime(wxChar *s, size_t maxsize, const wxChar *fmt, const struct tm *tm) | |
1340 | { | |
1341 | if ( !maxsize ) | |
1342 | return 0; | |
1343 | ||
1344 | wxCharBuffer buf(maxsize); | |
1345 | ||
1346 | wxCharBuffer bufFmt(wxConvLibc.cWX2MB(fmt)); | |
1347 | if ( !bufFmt ) | |
1348 | return 0; | |
1349 | ||
1350 | size_t ret = strftime(buf.data(), maxsize, bufFmt, tm); | |
1351 | if ( !ret ) | |
1352 | return 0; | |
1353 | ||
1354 | wxWCharBuffer wbuf = wxConvLibc.cMB2WX(buf); | |
1355 | if ( !wbuf ) | |
1356 | return 0; | |
1357 | ||
1358 | wxStrncpy(s, wbuf, maxsize); | |
1359 | return wxStrlen(s); | |
1360 | } | |
1361 | #endif // wxNEED_WX_TIME_H | |
1362 | ||
1363 | #ifndef wxCtime | |
1364 | WXDLLEXPORT wxChar *wxCtime(const time_t *timep) | |
1365 | { | |
1366 | // normally the string is 26 chars but give one more in case some broken | |
1367 | // DOS compiler decides to use "\r\n" instead of "\n" at the end | |
1368 | static wxChar buf[27]; | |
1369 | ||
1370 | // ctime() is guaranteed to return a string containing only ASCII | |
1371 | // characters, as its format is always the same for any locale | |
1372 | wxStrncpy(buf, wxString::FromAscii(ctime(timep)), WXSIZEOF(buf)); | |
1373 | buf[WXSIZEOF(buf) - 1] = _T('\0'); | |
1374 | ||
1375 | return buf; | |
1376 | } | |
1377 | #endif // wxCtime | |
1378 | ||
1379 | #endif // wxUSE_WCHAR_T | |
1380 | ||
17482893 VZ |
1381 | #ifndef wxStrtoll |
1382 | static wxULongLong_t wxStrtoullBase(const wxChar* nptr, wxChar** endptr, int base, wxChar* sign) | |
1383 | { | |
1384 | wxULongLong_t sum = 0; | |
1385 | wxString wxstr(nptr); | |
1386 | wxString::const_iterator i = wxstr.begin(); | |
1387 | wxString::const_iterator end = wxstr.end(); | |
1388 | ||
1389 | // Skip spaces | |
1390 | while ( i != end && wxIsspace(*i) ) i++; | |
1391 | ||
1392 | // Starts with sign? | |
1393 | *sign = wxT(' '); | |
1394 | if ( i != end ) | |
1395 | { | |
1396 | wxChar c = *i; | |
1397 | if ( c == wxT('+') || c == wxT('-') ) | |
1398 | { | |
1399 | *sign = c; | |
1400 | i++; | |
1401 | } | |
1402 | } | |
1403 | ||
1404 | // Starts with 0x? | |
1405 | if ( i != end && *i == wxT('0') ) | |
1406 | { | |
1407 | i++; | |
1408 | if ( i != end ) | |
1409 | { | |
1410 | if ( *i == wxT('x') && (base == 16 || base == 0) ) | |
1411 | { | |
1412 | base = 16; | |
1413 | i++; | |
1414 | } | |
1415 | else | |
1416 | { | |
1417 | if ( endptr ) | |
1418 | *endptr = (wxChar*) nptr; | |
1419 | wxSET_ERRNO(EINVAL); | |
1420 | return sum; | |
1421 | } | |
1422 | } | |
1423 | else | |
1424 | i--; | |
1425 | } | |
1426 | ||
1427 | if ( base == 0 ) | |
1428 | base = 10; | |
1429 | ||
1430 | for ( ; i != end; i++ ) | |
1431 | { | |
1432 | unsigned int n; | |
1433 | ||
1434 | wxChar c = *i; | |
1435 | if ( c >= wxT('0') ) | |
1436 | { | |
1437 | if ( c <= wxT('9') ) | |
1438 | n = c - wxT('0'); | |
1439 | else | |
1440 | n = wxTolower(c) - wxT('a') + 10; | |
1441 | } | |
1442 | else | |
1443 | break; | |
1444 | ||
1445 | if ( n >= (unsigned int)base ) | |
1446 | // Invalid character (for this base) | |
1447 | break; | |
1448 | ||
1449 | wxULongLong_t prevsum = sum; | |
1450 | sum = (sum * base) + n; | |
1451 | ||
1452 | if ( sum < prevsum ) | |
1453 | { | |
1454 | wxSET_ERRNO(ERANGE); | |
1455 | break; | |
1456 | } | |
1457 | } | |
1458 | ||
1459 | if ( endptr ) | |
1460 | { | |
c57fe715 | 1461 | *endptr = (wxChar*)(nptr + (i - wxstr.begin())); |
17482893 VZ |
1462 | } |
1463 | ||
1464 | return sum; | |
1465 | } | |
1466 | ||
1467 | wxULongLong_t wxStrtoull(const wxChar* nptr, wxChar** endptr, int base) | |
1468 | { | |
1469 | wxChar sign; | |
1470 | wxULongLong_t uval = wxStrtoullBase(nptr, endptr, base, &sign); | |
1471 | ||
1472 | if ( sign == wxT('-') ) | |
1473 | { | |
1474 | wxSET_ERRNO(ERANGE); | |
1475 | uval = 0; | |
1476 | } | |
1477 | ||
1478 | return uval; | |
1479 | } | |
1480 | ||
1481 | wxLongLong_t wxStrtoll(const wxChar* nptr, wxChar** endptr, int base) | |
1482 | { | |
1483 | wxChar sign; | |
1484 | wxULongLong_t uval = wxStrtoullBase(nptr, endptr, base, &sign); | |
1485 | wxLongLong_t val = 0; | |
1486 | ||
1487 | if ( sign == wxT('-') ) | |
1488 | { | |
1489 | if ( uval <= wxULL(wxINT64_MAX+1) ) | |
1490 | { | |
1491 | if ( uval == wxULL(wxINT64_MAX+1)) | |
1492 | val = -((wxLongLong_t)wxINT64_MAX) - 1; | |
1493 | else | |
1494 | val = -((wxLongLong_t)uval); | |
1495 | } | |
1496 | else | |
1497 | { | |
1498 | wxSET_ERRNO(ERANGE); | |
1499 | } | |
1500 | } | |
1501 | else if ( uval <= wxINT64_MAX ) | |
1502 | { | |
1503 | val = uval; | |
1504 | } | |
1505 | else | |
1506 | { | |
1507 | wxSET_ERRNO(ERANGE); | |
1508 | } | |
1509 | ||
1510 | return val; | |
1511 | } | |
1512 | #endif // wxStrtoll | |
1513 | ||
dd0ef332 VS |
1514 | // ---------------------------------------------------------------------------- |
1515 | // functions which we may need even if !wxUSE_WCHAR_T | |
1516 | // ---------------------------------------------------------------------------- | |
1517 | ||
1518 | #ifndef wxStrtok | |
1519 | ||
1520 | WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr) | |
1521 | { | |
1522 | if (!psz) | |
1523 | { | |
1524 | psz = *save_ptr; | |
1525 | if ( !psz ) | |
1526 | return NULL; | |
1527 | } | |
1528 | ||
1529 | psz += wxStrspn(psz, delim); | |
1530 | if (!*psz) | |
1531 | { | |
1532 | *save_ptr = (wxChar *)NULL; | |
1533 | return (wxChar *)NULL; | |
1534 | } | |
1535 | ||
1536 | wxChar *ret = psz; | |
1537 | psz = wxStrpbrk(psz, delim); | |
1538 | if (!psz) | |
1539 | { | |
1540 | *save_ptr = (wxChar*)NULL; | |
1541 | } | |
1542 | else | |
1543 | { | |
1544 | *psz = wxT('\0'); | |
1545 | *save_ptr = psz + 1; | |
1546 | } | |
1547 | ||
1548 | return ret; | |
1549 | } | |
1550 | ||
1551 | #endif // wxStrtok | |
1552 | ||
1553 | // ---------------------------------------------------------------------------- | |
1554 | // missing C RTL functions | |
1555 | // ---------------------------------------------------------------------------- | |
1556 | ||
1557 | #ifdef wxNEED_STRDUP | |
1558 | ||
1559 | char *strdup(const char *s) | |
1560 | { | |
1561 | char *dest = (char*) malloc( strlen( s ) + 1 ) ; | |
1562 | if ( dest ) | |
1563 | strcpy( dest , s ) ; | |
1564 | return dest ; | |
1565 | } | |
1566 | #endif // wxNEED_STRDUP | |
1567 | ||
1568 | #if defined(__WXWINCE__) && (_WIN32_WCE <= 211) | |
1569 | ||
1570 | void *calloc( size_t num, size_t size ) | |
1571 | { | |
1572 | void** ptr = (void **)malloc(num * size); | |
1573 | memset( ptr, 0, num * size); | |
1574 | return ptr; | |
1575 | } | |
1576 | ||
1577 | #endif // __WXWINCE__ <= 211 | |
1578 | ||
1579 | #ifdef __WXWINCE__ | |
1580 | ||
1581 | int wxRemove(const wxChar *path) | |
1582 | { | |
1583 | return ::DeleteFile(path) == 0; | |
1584 | } | |
1585 | ||
1586 | #endif | |
cb352236 VS |
1587 | |
1588 | ||
1589 | // ---------------------------------------------------------------------------- | |
1590 | // wxLocaleIsUtf8 | |
1591 | // ---------------------------------------------------------------------------- | |
1592 | ||
1593 | #if wxUSE_UNICODE_UTF8 | |
1594 | ||
1595 | #if !wxUSE_UTF8_LOCALE_ONLY | |
1596 | bool wxLocaleIsUtf8 = false; // the safer setting if not known | |
1597 | #endif | |
1598 | ||
1599 | static bool wxIsLocaleUtf8() | |
1600 | { | |
1601 | // NB: we intentionally don't use wxLocale::GetSystemEncodingName(), | |
1602 | // because a) it may be unavailable in some builds and b) has slightly | |
1603 | // different semantics (default locale instead of current) | |
1604 | ||
1605 | #if defined(HAVE_LANGINFO_H) && defined(CODESET) | |
1606 | // GNU libc provides current character set this way (this conforms to | |
1607 | // Unix98) | |
1608 | const char *charset = nl_langinfo(CODESET); | |
1609 | if ( charset ) | |
1610 | { | |
1611 | // "UTF-8" is used by modern glibc versions, but test other variants | |
1612 | // as well, just in case: | |
e40dfb3a VS |
1613 | if ( strcmp(charset, "UTF-8") == 0 || |
1614 | strcmp(charset, "utf-8") == 0 || | |
1615 | strcmp(charset, "UTF8") == 0 || | |
1616 | strcmp(charset, "utf8") == 0 ) | |
1617 | { | |
1618 | return true; | |
1619 | } | |
cb352236 | 1620 | } |
cb352236 | 1621 | #endif |
e40dfb3a VS |
1622 | |
1623 | // check if we're running under the "C" locale: it is 7bit subset | |
1624 | // of UTF-8, so it can be safely used with the UTF-8 build: | |
1625 | const char *lc_ctype = setlocale(LC_CTYPE, NULL); | |
1626 | if ( lc_ctype && | |
1627 | (strcmp(lc_ctype, "C") == 0 || strcmp(lc_ctype, "POSIX") == 0) ) | |
cb352236 | 1628 | { |
e40dfb3a | 1629 | return true; |
cb352236 | 1630 | } |
e40dfb3a VS |
1631 | |
1632 | // we don't know what charset libc is using, so assume the worst | |
1633 | // to be safe: | |
1634 | return false; | |
cb352236 VS |
1635 | } |
1636 | ||
1637 | void wxUpdateLocaleIsUtf8() | |
1638 | { | |
1639 | #if wxUSE_UTF8_LOCALE_ONLY | |
1640 | if ( !wxIsLocaleUtf8() ) | |
1641 | { | |
1642 | wxLogFatalError(_T("This program requires UTF-8 locale to run.")); | |
1643 | } | |
1644 | #else // !wxUSE_UTF8_LOCALE_ONLY | |
1645 | wxLocaleIsUtf8 = wxIsLocaleUtf8(); | |
1646 | #endif | |
1647 | } | |
1648 | ||
1649 | #endif // wxUSE_UTF8_LOCALE_ONLY |