allow the UTF8 build to treat C locale as UTF8, too, it's a 7bit subset of it, so...
[wxWidgets.git] / src / common / wxcrt.cpp
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
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
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_
166
167 #define wxNEED_WPRINTF
168
169 int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr );
170 #endif
171
172 #if defined(__DMC__)
173 /* Digital Mars adds count to _stprintf (C99) so convert */
174 #if wxUSE_UNICODE
175 int wxSprintf (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... )
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
515 int wxCRT_Scanf( const wxChar *format, ... )
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
527 int wxCRT_Sscanf( const wxChar *str, const wxChar *format, ... )
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
539 int wxCRT_Fscanf( FILE *stream, const wxChar *format, ... )
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
550 int wxCRT_Printf( const wxChar *format, ... )
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
562 int wxCRT_Fprintf( FILE *stream, const wxChar *format, ... )
563 {
564 va_list argptr;
565 va_start( argptr, format );
566
567 int ret = vfwprintf( stream, wxFormatConverter(format), argptr );
568
569 va_end(argptr);
570
571 return ret;
572 }
573
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
609 int wxDoSprintf(char *str, const wxString& format, ...)
610 {
611 va_list argptr;
612 va_start(argptr, format);
613
614 int rv = wxVsprintf(str, format, argptr);
615
616 va_end(argptr);
617 return rv;
618 }
619
620 #if wxUSE_UNICODE
621 int wxDoSprintf(wchar_t *str, const wxString& format, ...)
622 {
623 va_list argptr;
624 va_start(argptr, format);
625
626 int rv = wxVsprintf(str, format, argptr);
627
628 va_end(argptr);
629 return rv;
630 }
631 #endif
632
633 int wxDoSnprintf(char *str, size_t size, const wxString& format, ...)
634 {
635 va_list argptr;
636 va_start(argptr, format);
637
638 int rv = wxVsnprintf(str, size, format, argptr);
639
640 va_end(argptr);
641 return rv;
642 }
643
644 #if wxUSE_UNICODE
645 int wxDoSnprintf(wchar_t *str, size_t size, const wxString& format, ...)
646 {
647 va_list argptr;
648 va_start(argptr, format);
649
650 int rv = wxVsnprintf(str, size, format, argptr);
651
652 va_end(argptr);
653 return rv;
654 }
655 #endif
656
657
658 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
659 #define vsnprintf wx_fixed_vsnprintf
660 #endif
661
662 #if wxUSE_UNICODE
663 static int ConvertStringToBuf(const wxString& s, char *out, size_t outsize)
664 {
665 const wxWX2WCbuf buf = s.wc_str();
666
667 size_t len = wxConvLibc.FromWChar(out, outsize, buf);
668 if ( len != wxCONV_FAILED )
669 return len-1;
670 else
671 return wxConvLibc.FromWChar(NULL, 0, buf);
672 }
673
674 #if wxUSE_UNICODE_UTF8
675 static int ConvertStringToBuf(const wxString& s, wchar_t *out, size_t outsize)
676 {
677 const wxWX2WCbuf buf(s.wc_str());
678 size_t len = wxWcslen(buf);
679 if ( outsize > len )
680 memcpy(out, buf, (len+1) * sizeof(wchar_t));
681 // else: not enough space
682 return len;
683 }
684 #endif
685
686 template<typename T>
687 static size_t PrintfViaString(T *out, size_t outsize,
688 const wxString& format, va_list argptr)
689 {
690 va_list argcopy;
691 wxVaCopy(argcopy, argptr);
692
693 wxString s;
694 s.PrintfV(format, argcopy);
695
696 return ConvertStringToBuf(s, out, outsize);
697 }
698 #endif // wxUSE_UNICODE
699
700 int wxVsprintf(char *str, const wxString& format, va_list argptr)
701 {
702 va_list argcopy;
703 wxVaCopy(argcopy, argptr);
704
705 #if wxUSE_UTF8_LOCALE_ONLY
706 return vsprintf(str, format.wx_str(), argcopy);
707 #else
708 #if wxUSE_UNICODE_UTF8
709 if ( wxLocaleIsUtf8 )
710 return vsprintf(str, format.wx_str(), argcopy);
711 else
712 #endif
713 #if wxUSE_UNICODE
714 return PrintfViaString(str, wxNO_LEN, format, argcopy);
715 #else
716 return wxCRT_Vsprintf(str, format, argcopy);
717 #endif
718 #endif
719 }
720
721 #if wxUSE_UNICODE
722 int wxVsprintf(wchar_t *str, const wxString& format, va_list argptr)
723 {
724 va_list argcopy;
725 wxVaCopy(argcopy, argptr);
726
727 #if wxUSE_UNICODE_WCHAR
728 return wxCRT_Vsprintf(str, format, argcopy);
729 #else // wxUSE_UNICODE_UTF8
730 #if !wxUSE_UTF8_LOCALE_ONLY
731 if ( !wxLocaleIsUtf8 )
732 return wxCRT_Vsprintf(str, format, argcopy);
733 else
734 #endif
735 return PrintfViaString(str, wxNO_LEN, format, argcopy);
736 #endif // wxUSE_UNICODE_UTF8
737 }
738 #endif // wxUSE_UNICODE
739
740 int wxVsnprintf(char *str, size_t size, const wxString& format, va_list argptr)
741 {
742 int rv;
743 va_list argcopy;
744 wxVaCopy(argcopy, argptr);
745
746 #if wxUSE_UTF8_LOCALE_ONLY
747 rv = vsnprintf(str, size, format.wx_str(), argcopy);
748 #else
749 #if wxUSE_UNICODE_UTF8
750 if ( wxLocaleIsUtf8 )
751 rv = vsnprintf(str, size, format.wx_str(), argcopy);
752 else
753 #endif
754 #if wxUSE_UNICODE
755 {
756 // NB: if this code is called, then wxString::PrintV() would use the
757 // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
758 // from here
759 rv = PrintfViaString(str, size, format, argcopy);
760 }
761 #else
762 rv = wxCRT_Vsnprintf(str, size, format, argcopy);
763 #endif
764 #endif
765
766 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
767 // doesn't nul terminate on truncation.
768 str[size - 1] = 0;
769
770 return rv;
771 }
772
773 #if wxUSE_UNICODE
774 int wxVsnprintf(wchar_t *str, size_t size, const wxString& format, va_list argptr)
775 {
776 int rv;
777 va_list argcopy;
778 wxVaCopy(argcopy, argptr);
779
780 #if wxUSE_UNICODE_WCHAR
781 rv = wxCRT_Vsnprintf(str, size, format, argcopy);
782 #else // wxUSE_UNICODE_UTF8
783 #if !wxUSE_UTF8_LOCALE_ONLY
784 if ( !wxLocaleIsUtf8 )
785 rv = wxCRT_Vsnprintf(str, size, format, argcopy);
786 else
787 #endif
788 {
789 // NB: if this code is called, then wxString::PrintV() would use the
790 // char* version of wxVsnprintf(), so it's safe to use PrintV()
791 // from here
792 rv = PrintfViaString(str, size, format, argcopy);
793 }
794 #endif // wxUSE_UNICODE_UTF8
795
796 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
797 // doesn't nul terminate on truncation.
798 str[size - 1] = 0;
799
800 return rv;
801 }
802 #endif // wxUSE_UNICODE
803
804 #if wxUSE_WCHAR_T
805
806 // ----------------------------------------------------------------------------
807 // ctype.h stuff (currently unused)
808 // ----------------------------------------------------------------------------
809
810 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
811 inline WORD wxMSW_ctype(wxChar ch)
812 {
813 WORD ret;
814 GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
815 return ret;
816 }
817
818 WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); }
819 WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); }
820 WXDLLEXPORT int wxIscntrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; }
821 WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; }
822 WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); }
823 WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); }
824 WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); }
825 WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; }
826 WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; }
827 WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); }
828 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; }
829 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); }
830 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
831 #endif
832
833 #ifdef wxNEED_WX_MBSTOWCS
834
835 WXDLLEXPORT size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen)
836 {
837 if (!out)
838 {
839 size_t outsize = 0;
840 while(*in++)
841 outsize++;
842 return outsize;
843 }
844
845 const char* origin = in;
846
847 while (outlen-- && *in)
848 {
849 *out++ = (wchar_t) *in++;
850 }
851
852 *out = '\0';
853
854 return in - origin;
855 }
856
857 WXDLLEXPORT size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen)
858 {
859 if (!out)
860 {
861 size_t outsize = 0;
862 while(*in++)
863 outsize++;
864 return outsize;
865 }
866
867 const wchar_t* origin = in;
868
869 while (outlen-- && *in)
870 {
871 *out++ = (char) *in++;
872 }
873
874 *out = '\0';
875
876 return in - origin;
877 }
878
879 #endif // wxNEED_WX_MBSTOWCS
880
881 #if defined(wxNEED_WX_CTYPE_H)
882
883 #include <CoreFoundation/CoreFoundation.h>
884
885 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
886 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
887 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
888 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
889 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
890 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
891 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
892 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
893 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
894 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
895
896 WXDLLEXPORT int wxIsalnum(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalnumset, ch); }
897 WXDLLEXPORT int wxIsalpha(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalphaset, ch); }
898 WXDLLEXPORT int wxIscntrl(wxChar ch) { return CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
899 WXDLLEXPORT int wxIsdigit(wxChar ch) { return CFCharacterSetIsCharacterMember(cfdigitset, ch); }
900 WXDLLEXPORT int wxIsgraph(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch) && ch != ' '; }
901 WXDLLEXPORT int wxIslower(wxChar ch) { return CFCharacterSetIsCharacterMember(cflowerset, ch); }
902 WXDLLEXPORT int wxIsprint(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
903 WXDLLEXPORT int wxIspunct(wxChar ch) { return CFCharacterSetIsCharacterMember(cfpunctset, ch); }
904 WXDLLEXPORT int wxIsspace(wxChar ch) { return CFCharacterSetIsCharacterMember(cfspaceset, ch); }
905 WXDLLEXPORT int wxIsupper(wxChar ch) { return CFCharacterSetIsCharacterMember(cfupperset, ch); }
906 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxIsdigit(ch) || (ch>='a' && ch<='f') || (ch>='A' && ch<='F'); }
907 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)tolower((char)(ch)); }
908 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)toupper((char)(ch)); }
909
910 #endif // wxNEED_WX_CTYPE_H
911
912 #ifndef wxStrdupA
913
914 WXDLLEXPORT char *wxStrdupA(const char *s)
915 {
916 return strcpy((char *)malloc(strlen(s) + 1), s);
917 }
918
919 #endif // wxStrdupA
920
921 #ifndef wxStrdupW
922
923 WXDLLEXPORT wchar_t * wxStrdupW(const wchar_t *pwz)
924 {
925 size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t);
926 wchar_t *ret = (wchar_t *) malloc(size);
927 memcpy(ret, pwz, size);
928 return ret;
929 }
930
931 #endif // wxStrdupW
932
933 #ifndef wxStricmp
934 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
935 {
936 register wxChar c1, c2;
937 do {
938 c1 = wxTolower(*psz1++);
939 c2 = wxTolower(*psz2++);
940 } while ( c1 && (c1 == c2) );
941 return c1 - c2;
942 }
943 #endif
944
945 #ifndef wxStricmp
946 int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n)
947 {
948 // initialize the variables just to suppress stupid gcc warning
949 register wxChar c1 = 0, c2 = 0;
950 while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++;
951 if (n) {
952 if (c1 < c2) return -1;
953 if (c1 > c2) return 1;
954 }
955 return 0;
956 }
957 #endif
958
959 #ifndef wxSetlocale_
960 wxWCharBuffer wxSetlocale_(int category, const wxChar *locale)
961 {
962 char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale));
963
964 return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld));
965 }
966
967 wxWCharBuffer wxSetlocale(int category, const wxChar *locale)
968 {
969 wxWCharBuffer rv = wxSetlocale_(category, locale);
970 if ( rv )
971 wxUpdateLocaleIsUtf8();
972 return rv;
973 }
974 #else // defined(wxSetlocale_)
975 wxChar *wxSetlocale(int category, const wxChar *locale)
976 {
977 wxChar *rv = wxSetlocale_(category, locale);
978 if ( rv )
979 wxUpdateLocaleIsUtf8();
980 return rv;
981 }
982 #endif // wxSetlocale_ defined or not
983
984 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
985 WXDLLEXPORT size_t wxWcslen(const wchar_t *s)
986 {
987 size_t n = 0;
988 while ( *s++ )
989 n++;
990
991 return n;
992 }
993 #endif
994
995 // ----------------------------------------------------------------------------
996 // string.h functions
997 // ----------------------------------------------------------------------------
998
999 #ifdef wxNEED_WX_STRING_H
1000
1001 // RN: These need to be c externed for the regex lib
1002 #ifdef __cplusplus
1003 extern "C" {
1004 #endif
1005
1006 WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
1007 {
1008 wxChar *ret = dest;
1009 while (*dest) dest++;
1010 while ((*dest++ = *src++));
1011 return ret;
1012 }
1013
1014 WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c)
1015 {
1016 // be careful here as the terminating NUL makes part of the string
1017 while ( *s != c )
1018 {
1019 if ( !*s++ )
1020 return NULL;
1021 }
1022
1023 return s;
1024 }
1025
1026 WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
1027 {
1028 while ((*s1 == *s2) && *s1) s1++, s2++;
1029 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
1030 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
1031 return 0;
1032 }
1033
1034 WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
1035 {
1036 wxChar *ret = dest;
1037 while ((*dest++ = *src++));
1038 return ret;
1039 }
1040
1041 WXDLLEXPORT size_t wxStrlen_(const wxChar *s)
1042 {
1043 size_t n = 0;
1044 while ( *s++ )
1045 n++;
1046
1047 return n;
1048 }
1049
1050
1051 WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
1052 {
1053 wxChar *ret = dest;
1054 while (*dest) dest++;
1055 while (n && (*dest++ = *src++)) n--;
1056 return ret;
1057 }
1058
1059 WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
1060 {
1061 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
1062 if (n) {
1063 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
1064 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
1065 }
1066 return 0;
1067 }
1068
1069 WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
1070 {
1071 wxChar *ret = dest;
1072 while (n && (*dest++ = *src++)) n--;
1073 while (n) *dest++=0, n--; // the docs specify padding with zeroes
1074 return ret;
1075 }
1076
1077 WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
1078 {
1079 while (*s && !wxStrchr(accept, *s))
1080 s++;
1081
1082 return *s ? s : NULL;
1083 }
1084
1085 WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c)
1086 {
1087 const wxChar *ret = NULL;
1088 do
1089 {
1090 if ( *s == c )
1091 ret = s;
1092 s++;
1093 }
1094 while ( *s );
1095
1096 return ret;
1097 }
1098
1099 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
1100 {
1101 size_t len = 0;
1102 while (wxStrchr(accept, *s++)) len++;
1103 return len;
1104 }
1105
1106 WXDLLEXPORT const wxChar *wxStrstr(const wxChar *haystack, const wxChar *needle)
1107 {
1108 wxASSERT_MSG( needle != NULL, _T("NULL argument in wxStrstr") );
1109
1110 // VZ: this is not exactly the most efficient string search algorithm...
1111
1112 const size_t len = wxStrlen(needle);
1113
1114 while ( const wxChar *fnd = wxStrchr(haystack, *needle) )
1115 {
1116 if ( !wxStrncmp(fnd, needle, len) )
1117 return fnd;
1118
1119 haystack = fnd + 1;
1120 }
1121
1122 return NULL;
1123 }
1124
1125 #ifdef __cplusplus
1126 }
1127 #endif
1128
1129 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
1130 {
1131 const wxChar *start = nptr;
1132
1133 // FIXME: only correct for C locale
1134 while (wxIsspace(*nptr)) nptr++;
1135 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1136 while (wxIsdigit(*nptr)) nptr++;
1137 if (*nptr == wxT('.')) {
1138 nptr++;
1139 while (wxIsdigit(*nptr)) nptr++;
1140 }
1141 if (*nptr == wxT('E') || *nptr == wxT('e')) {
1142 nptr++;
1143 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1144 while (wxIsdigit(*nptr)) nptr++;
1145 }
1146
1147 wxString data(nptr, nptr-start);
1148 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
1149 char *rdat = wxMBSTRINGCAST dat;
1150 double ret = strtod(dat, &rdat);
1151
1152 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
1153
1154 return ret;
1155 }
1156
1157 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
1158 {
1159 const wxChar *start = nptr;
1160
1161 // FIXME: only correct for C locale
1162 while (wxIsspace(*nptr)) nptr++;
1163 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1164 if (((base == 0) || (base == 16)) &&
1165 (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
1166 nptr += 2;
1167 base = 16;
1168 }
1169 else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
1170 else if (base == 0) base = 10;
1171
1172 while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
1173 (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
1174
1175 wxString data(start, nptr-start);
1176 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
1177 char *rdat = wxMBSTRINGCAST dat;
1178 long int ret = strtol(dat, &rdat, base);
1179
1180 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
1181
1182 return ret;
1183 }
1184
1185 WXDLLEXPORT unsigned long int wxStrtoul(const wxChar *nptr, wxChar **endptr, int base)
1186 {
1187 return (unsigned long int) wxStrtol(nptr, endptr, base);
1188 }
1189
1190 #endif // wxNEED_WX_STRING_H
1191
1192 #ifdef wxNEED_WX_STDIO_H
1193 WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
1194 {
1195 char mode_buffer[10];
1196 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
1197 mode_buffer[i] = (char) mode[i];
1198
1199 return fopen( wxConvFile.cWX2MB(path), mode_buffer );
1200 }
1201
1202 WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
1203 {
1204 char mode_buffer[10];
1205 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
1206 mode_buffer[i] = (char) mode[i];
1207
1208 return freopen( wxConvFile.cWX2MB(path), mode_buffer, stream );
1209 }
1210
1211 WXDLLEXPORT int wxRemove(const wxChar *path)
1212 {
1213 return remove( wxConvFile.cWX2MB(path) );
1214 }
1215
1216 WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
1217 {
1218 return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) );
1219 }
1220 #endif
1221
1222 #ifndef wxAtof
1223 double WXDLLEXPORT wxAtof(const wxChar *psz)
1224 {
1225 #ifdef __WXWINCE__
1226 double d;
1227 wxString str(psz);
1228 if (str.ToDouble(& d))
1229 return d;
1230
1231 return 0.0;
1232 #else
1233 return atof(wxConvLibc.cWX2MB(psz));
1234 #endif
1235 }
1236 #endif
1237
1238 #ifdef wxNEED_WX_STDLIB_H
1239 int WXDLLEXPORT wxAtoi(const wxChar *psz)
1240 {
1241 return atoi(wxConvLibc.cWX2MB(psz));
1242 }
1243
1244 long WXDLLEXPORT wxAtol(const wxChar *psz)
1245 {
1246 return atol(wxConvLibc.cWX2MB(psz));
1247 }
1248
1249 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
1250 {
1251 #if wxUSE_UNICODE
1252 // NB: buffer returned by getenv() is allowed to be overwritten next
1253 // time getenv() is called, so it is OK to use static string
1254 // buffer to hold the data.
1255 static wxWCharBuffer value((wxChar*)NULL);
1256 value = wxConvLibc.cMB2WX(getenv(wxConvLibc.cWX2MB(name)));
1257 return value.data();
1258 #else
1259 return getenv(name);
1260 #endif
1261 }
1262
1263 int WXDLLEXPORT wxSystem(const wxChar *psz)
1264 {
1265 return system(wxConvLibc.cWX2MB(psz));
1266 }
1267
1268 #endif // wxNEED_WX_STDLIB_H
1269
1270 #ifdef wxNEED_WX_TIME_H
1271 WXDLLEXPORT size_t
1272 wxStrftime(wxChar *s, size_t maxsize, const wxChar *fmt, const struct tm *tm)
1273 {
1274 if ( !maxsize )
1275 return 0;
1276
1277 wxCharBuffer buf(maxsize);
1278
1279 wxCharBuffer bufFmt(wxConvLibc.cWX2MB(fmt));
1280 if ( !bufFmt )
1281 return 0;
1282
1283 size_t ret = strftime(buf.data(), maxsize, bufFmt, tm);
1284 if ( !ret )
1285 return 0;
1286
1287 wxWCharBuffer wbuf = wxConvLibc.cMB2WX(buf);
1288 if ( !wbuf )
1289 return 0;
1290
1291 wxStrncpy(s, wbuf, maxsize);
1292 return wxStrlen(s);
1293 }
1294 #endif // wxNEED_WX_TIME_H
1295
1296 #ifndef wxCtime
1297 WXDLLEXPORT wxChar *wxCtime(const time_t *timep)
1298 {
1299 // normally the string is 26 chars but give one more in case some broken
1300 // DOS compiler decides to use "\r\n" instead of "\n" at the end
1301 static wxChar buf[27];
1302
1303 // ctime() is guaranteed to return a string containing only ASCII
1304 // characters, as its format is always the same for any locale
1305 wxStrncpy(buf, wxString::FromAscii(ctime(timep)), WXSIZEOF(buf));
1306 buf[WXSIZEOF(buf) - 1] = _T('\0');
1307
1308 return buf;
1309 }
1310 #endif // wxCtime
1311
1312 #endif // wxUSE_WCHAR_T
1313
1314 #ifndef wxStrtoll
1315 static wxULongLong_t wxStrtoullBase(const wxChar* nptr, wxChar** endptr, int base, wxChar* sign)
1316 {
1317 wxULongLong_t sum = 0;
1318 wxString wxstr(nptr);
1319 wxString::const_iterator i = wxstr.begin();
1320 wxString::const_iterator end = wxstr.end();
1321
1322 // Skip spaces
1323 while ( i != end && wxIsspace(*i) ) i++;
1324
1325 // Starts with sign?
1326 *sign = wxT(' ');
1327 if ( i != end )
1328 {
1329 wxChar c = *i;
1330 if ( c == wxT('+') || c == wxT('-') )
1331 {
1332 *sign = c;
1333 i++;
1334 }
1335 }
1336
1337 // Starts with 0x?
1338 if ( i != end && *i == wxT('0') )
1339 {
1340 i++;
1341 if ( i != end )
1342 {
1343 if ( *i == wxT('x') && (base == 16 || base == 0) )
1344 {
1345 base = 16;
1346 i++;
1347 }
1348 else
1349 {
1350 if ( endptr )
1351 *endptr = (wxChar*) nptr;
1352 wxSET_ERRNO(EINVAL);
1353 return sum;
1354 }
1355 }
1356 else
1357 i--;
1358 }
1359
1360 if ( base == 0 )
1361 base = 10;
1362
1363 for ( ; i != end; i++ )
1364 {
1365 unsigned int n;
1366
1367 wxChar c = *i;
1368 if ( c >= wxT('0') )
1369 {
1370 if ( c <= wxT('9') )
1371 n = c - wxT('0');
1372 else
1373 n = wxTolower(c) - wxT('a') + 10;
1374 }
1375 else
1376 break;
1377
1378 if ( n >= (unsigned int)base )
1379 // Invalid character (for this base)
1380 break;
1381
1382 wxULongLong_t prevsum = sum;
1383 sum = (sum * base) + n;
1384
1385 if ( sum < prevsum )
1386 {
1387 wxSET_ERRNO(ERANGE);
1388 break;
1389 }
1390 }
1391
1392 if ( endptr )
1393 {
1394 const wxChar& endref = *i;
1395 *endptr = &(wxChar&)endref;
1396 }
1397
1398 return sum;
1399 }
1400
1401 wxULongLong_t wxStrtoull(const wxChar* nptr, wxChar** endptr, int base)
1402 {
1403 wxChar sign;
1404 wxULongLong_t uval = wxStrtoullBase(nptr, endptr, base, &sign);
1405
1406 if ( sign == wxT('-') )
1407 {
1408 wxSET_ERRNO(ERANGE);
1409 uval = 0;
1410 }
1411
1412 return uval;
1413 }
1414
1415 wxLongLong_t wxStrtoll(const wxChar* nptr, wxChar** endptr, int base)
1416 {
1417 wxChar sign;
1418 wxULongLong_t uval = wxStrtoullBase(nptr, endptr, base, &sign);
1419 wxLongLong_t val = 0;
1420
1421 if ( sign == wxT('-') )
1422 {
1423 if ( uval <= wxULL(wxINT64_MAX+1) )
1424 {
1425 if ( uval == wxULL(wxINT64_MAX+1))
1426 val = -((wxLongLong_t)wxINT64_MAX) - 1;
1427 else
1428 val = -((wxLongLong_t)uval);
1429 }
1430 else
1431 {
1432 wxSET_ERRNO(ERANGE);
1433 }
1434 }
1435 else if ( uval <= wxINT64_MAX )
1436 {
1437 val = uval;
1438 }
1439 else
1440 {
1441 wxSET_ERRNO(ERANGE);
1442 }
1443
1444 return val;
1445 }
1446 #endif // wxStrtoll
1447
1448 // ----------------------------------------------------------------------------
1449 // functions which we may need even if !wxUSE_WCHAR_T
1450 // ----------------------------------------------------------------------------
1451
1452 #ifndef wxStrtok
1453
1454 WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
1455 {
1456 if (!psz)
1457 {
1458 psz = *save_ptr;
1459 if ( !psz )
1460 return NULL;
1461 }
1462
1463 psz += wxStrspn(psz, delim);
1464 if (!*psz)
1465 {
1466 *save_ptr = (wxChar *)NULL;
1467 return (wxChar *)NULL;
1468 }
1469
1470 wxChar *ret = psz;
1471 psz = wxStrpbrk(psz, delim);
1472 if (!psz)
1473 {
1474 *save_ptr = (wxChar*)NULL;
1475 }
1476 else
1477 {
1478 *psz = wxT('\0');
1479 *save_ptr = psz + 1;
1480 }
1481
1482 return ret;
1483 }
1484
1485 #endif // wxStrtok
1486
1487 // ----------------------------------------------------------------------------
1488 // missing C RTL functions
1489 // ----------------------------------------------------------------------------
1490
1491 #ifdef wxNEED_STRDUP
1492
1493 char *strdup(const char *s)
1494 {
1495 char *dest = (char*) malloc( strlen( s ) + 1 ) ;
1496 if ( dest )
1497 strcpy( dest , s ) ;
1498 return dest ;
1499 }
1500 #endif // wxNEED_STRDUP
1501
1502 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1503
1504 void *calloc( size_t num, size_t size )
1505 {
1506 void** ptr = (void **)malloc(num * size);
1507 memset( ptr, 0, num * size);
1508 return ptr;
1509 }
1510
1511 #endif // __WXWINCE__ <= 211
1512
1513 #ifdef __WXWINCE__
1514
1515 int wxRemove(const wxChar *path)
1516 {
1517 return ::DeleteFile(path) == 0;
1518 }
1519
1520 #endif
1521
1522
1523 // ----------------------------------------------------------------------------
1524 // wxLocaleIsUtf8
1525 // ----------------------------------------------------------------------------
1526
1527 #if wxUSE_UNICODE_UTF8
1528
1529 #if !wxUSE_UTF8_LOCALE_ONLY
1530 bool wxLocaleIsUtf8 = false; // the safer setting if not known
1531 #endif
1532
1533 static bool wxIsLocaleUtf8()
1534 {
1535 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1536 // because a) it may be unavailable in some builds and b) has slightly
1537 // different semantics (default locale instead of current)
1538
1539 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1540 // GNU libc provides current character set this way (this conforms to
1541 // Unix98)
1542 const char *charset = nl_langinfo(CODESET);
1543 if ( charset )
1544 {
1545 // "UTF-8" is used by modern glibc versions, but test other variants
1546 // as well, just in case:
1547 if ( strcmp(charset, "UTF-8") == 0 ||
1548 strcmp(charset, "utf-8") == 0 ||
1549 strcmp(charset, "UTF8") == 0 ||
1550 strcmp(charset, "utf8") == 0 )
1551 {
1552 return true;
1553 }
1554 }
1555 #endif
1556
1557 // check if we're running under the "C" locale: it is 7bit subset
1558 // of UTF-8, so it can be safely used with the UTF-8 build:
1559 const char *lc_ctype = setlocale(LC_CTYPE, NULL);
1560 if ( lc_ctype &&
1561 (strcmp(lc_ctype, "C") == 0 || strcmp(lc_ctype, "POSIX") == 0) )
1562 {
1563 return true;
1564 }
1565
1566 // we don't know what charset libc is using, so assume the worst
1567 // to be safe:
1568 return false;
1569 }
1570
1571 void wxUpdateLocaleIsUtf8()
1572 {
1573 #if wxUSE_UTF8_LOCALE_ONLY
1574 if ( !wxIsLocaleUtf8() )
1575 {
1576 wxLogFatalError(_T("This program requires UTF-8 locale to run."));
1577 }
1578 #else // !wxUSE_UTF8_LOCALE_ONLY
1579 wxLocaleIsUtf8 = wxIsLocaleUtf8();
1580 #endif
1581 }
1582
1583 #endif // wxUSE_UTF8_LOCALE_ONLY