added code for checking if the current locale is UTF-8 at runtime
[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 #undef wxSnprintf_
167
168 #define wxNEED_WPRINTF
169
170 int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr );
171 #endif
172
173 #if !defined(wxSnprintf_)
174 int WXDLLEXPORT wxDoSnprintf_(wxChar *buf, size_t len, const wxChar *format, ...)
175 {
176 va_list argptr;
177 va_start(argptr, format);
178
179 int iLen = wxVsnprintf_(buf, len, format, argptr);
180
181 va_end(argptr);
182
183 return iLen;
184 }
185 #endif // wxSnprintf_
186
187 #if defined(__DMC__)
188 /* Digital Mars adds count to _stprintf (C99) so convert */
189 #if wxUSE_UNICODE
190 int wxDoSprintf (wchar_t * __RESTRICT s, const wchar_t * __RESTRICT format, ... )
191 {
192 va_list arglist;
193
194 va_start( arglist, format );
195 int iLen = swprintf ( s, -1, format, arglist );
196 va_end( arglist );
197 return iLen ;
198 }
199
200 #endif // wxUSE_UNICODE
201
202 #endif //__DMC__
203
204 // ----------------------------------------------------------------------------
205 // implement the standard IO functions for wide char if libc doesn't have them
206 // ----------------------------------------------------------------------------
207
208 #ifdef wxNEED_FPUTS
209 int wxFputs(const wchar_t *ws, FILE *stream)
210 {
211 wxCharBuffer buf(wxConvLibc.cWC2MB(ws));
212 if ( !buf )
213 return -1;
214
215 // counting the number of wide characters written isn't worth the trouble,
216 // simply distinguish between ok and error
217 return fputs(buf, stream) == -1 ? -1 : 0;
218 }
219 #endif // wxNEED_FPUTS
220
221 #ifdef wxNEED_PUTS
222 int wxPuts(const wxChar *ws)
223 {
224 int rc = wxFputs(ws, stdout);
225 if ( rc != -1 )
226 {
227 if ( wxFputs(L"\n", stdout) == -1 )
228 return -1;
229
230 rc++;
231 }
232
233 return rc;
234 }
235 #endif // wxNEED_PUTS
236
237 #ifdef wxNEED_PUTC
238 int /* not wint_t */ wxPutc(wchar_t wc, FILE *stream)
239 {
240 wchar_t ws[2] = { wc, L'\0' };
241
242 return wxFputs(ws, stream);
243 }
244 #endif // wxNEED_PUTC
245
246 // NB: we only implement va_list functions here, the ones taking ... are
247 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
248 // the definitions there to avoid duplicating them here
249 #ifdef wxNEED_WPRINTF
250
251 // TODO: implement the scanf() functions
252 int vwscanf(const wxChar *format, va_list argptr)
253 {
254 wxFAIL_MSG( _T("TODO") );
255
256 return -1;
257 }
258
259 int vswscanf(const wxChar *ws, const wxChar *format, va_list argptr)
260 {
261 // The best we can do without proper Unicode support in glibc is to
262 // convert the strings into MB representation and run ANSI version
263 // of the function. This doesn't work with %c and %s because of difference
264 // in size of char and wchar_t, though.
265
266 wxCHECK_MSG( wxStrstr(format, _T("%s")) == NULL, -1,
267 _T("incomplete vswscanf implementation doesn't allow %s") );
268 wxCHECK_MSG( wxStrstr(format, _T("%c")) == NULL, -1,
269 _T("incomplete vswscanf implementation doesn't allow %c") );
270
271 va_list argcopy;
272 wxVaCopy(argcopy, argptr);
273 return vsscanf(wxConvLibc.cWX2MB(ws), wxConvLibc.cWX2MB(format), argcopy);
274 }
275
276 int vfwscanf(FILE *stream, const wxChar *format, va_list argptr)
277 {
278 wxFAIL_MSG( _T("TODO") );
279
280 return -1;
281 }
282
283 #define vswprintf wxVsnprintf_
284
285 int vfwprintf(FILE *stream, const wxChar *format, va_list argptr)
286 {
287 wxString s;
288 int rc = s.PrintfV(format, argptr);
289
290 if ( rc != -1 )
291 {
292 // we can't do much better without Unicode support in libc...
293 if ( fprintf(stream, "%s", (const char*)s.mb_str() ) == -1 )
294 return -1;
295 }
296
297 return rc;
298 }
299
300 int vwprintf(const wxChar *format, va_list argptr)
301 {
302 return wxVfprintf(stdout, format, argptr);
303 }
304
305 #endif // wxNEED_WPRINTF
306
307 #ifdef wxNEED_PRINTF_CONVERSION
308
309 // ----------------------------------------------------------------------------
310 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
311 // ----------------------------------------------------------------------------
312
313 /*
314 Here are the gory details. We want to follow the Windows/MS conventions,
315 that is to have
316
317 In ANSI mode:
318
319 format specifier results in
320 -----------------------------------
321 %c, %hc, %hC char
322 %lc, %C, %lC wchar_t
323
324 In Unicode mode:
325
326 format specifier results in
327 -----------------------------------
328 %hc, %C, %hC char
329 %c, %lc, %lC wchar_t
330
331
332 while on POSIX systems we have %C identical to %lc and %c always means char
333 (in any mode) while %lc always means wchar_t,
334
335 So to use native functions in order to get our semantics we must do the
336 following translations in Unicode mode (nothing to do in ANSI mode):
337
338 wxWidgets specifier POSIX specifier
339 ----------------------------------------
340
341 %hc, %C, %hC %c
342 %c %lc
343
344
345 And, of course, the same should be done for %s as well.
346 */
347
348 class wxFormatConverter
349 {
350 public:
351 wxFormatConverter(const wxChar *format);
352
353 // notice that we only translated the string if m_fmtOrig == NULL (as set
354 // by CopyAllBefore()), otherwise we should simply use the original format
355 operator const wxChar *() const
356 { return m_fmtOrig ? m_fmtOrig : m_fmt.c_str(); }
357
358 private:
359 // copy another character to the translated format: this function does the
360 // copy if we are translating but doesn't do anything at all if we don't,
361 // so we don't create the translated format string at all unless we really
362 // need to (i.e. InsertFmtChar() is called)
363 wxChar CopyFmtChar(wxChar ch)
364 {
365 if ( !m_fmtOrig )
366 {
367 // we're translating, do copy
368 m_fmt += ch;
369 }
370 else
371 {
372 // simply increase the count which should be copied by
373 // CopyAllBefore() later if needed
374 m_nCopied++;
375 }
376
377 return ch;
378 }
379
380 // insert an extra character
381 void InsertFmtChar(wxChar ch)
382 {
383 if ( m_fmtOrig )
384 {
385 // so far we haven't translated anything yet
386 CopyAllBefore();
387 }
388
389 m_fmt += ch;
390 }
391
392 void CopyAllBefore()
393 {
394 wxASSERT_MSG( m_fmtOrig && m_fmt.empty(), _T("logic error") );
395
396 m_fmt = wxString(m_fmtOrig, m_nCopied);
397
398 // we won't need it any longer
399 m_fmtOrig = NULL;
400 }
401
402 static bool IsFlagChar(wxChar ch)
403 {
404 return ch == _T('-') || ch == _T('+') ||
405 ch == _T('0') || ch == _T(' ') || ch == _T('#');
406 }
407
408 void SkipDigits(const wxChar **ptpc)
409 {
410 while ( **ptpc >= _T('0') && **ptpc <= _T('9') )
411 CopyFmtChar(*(*ptpc)++);
412 }
413
414 // the translated format
415 wxString m_fmt;
416
417 // the original format
418 const wxChar *m_fmtOrig;
419
420 // the number of characters already copied
421 size_t m_nCopied;
422 };
423
424 wxFormatConverter::wxFormatConverter(const wxChar *format)
425 {
426 m_fmtOrig = format;
427 m_nCopied = 0;
428
429 while ( *format )
430 {
431 if ( CopyFmtChar(*format++) == _T('%') )
432 {
433 // skip any flags
434 while ( IsFlagChar(*format) )
435 CopyFmtChar(*format++);
436
437 // and possible width
438 if ( *format == _T('*') )
439 CopyFmtChar(*format++);
440 else
441 SkipDigits(&format);
442
443 // precision?
444 if ( *format == _T('.') )
445 {
446 CopyFmtChar(*format++);
447 if ( *format == _T('*') )
448 CopyFmtChar(*format++);
449 else
450 SkipDigits(&format);
451 }
452
453 // next we can have a size modifier
454 enum
455 {
456 Default,
457 Short,
458 Long
459 } size;
460
461 switch ( *format )
462 {
463 case _T('h'):
464 size = Short;
465 format++;
466 break;
467
468 case _T('l'):
469 // "ll" has a different meaning!
470 if ( format[1] != _T('l') )
471 {
472 size = Long;
473 format++;
474 break;
475 }
476 //else: fall through
477
478 default:
479 size = Default;
480 }
481
482 // and finally we should have the type
483 switch ( *format )
484 {
485 case _T('C'):
486 case _T('S'):
487 // %C and %hC -> %c and %lC -> %lc
488 if ( size == Long )
489 CopyFmtChar(_T('l'));
490
491 InsertFmtChar(*format++ == _T('C') ? _T('c') : _T('s'));
492 break;
493
494 case _T('c'):
495 case _T('s'):
496 // %c -> %lc but %hc stays %hc and %lc is still %lc
497 if ( size == Default)
498 InsertFmtChar(_T('l'));
499 // fall through
500
501 default:
502 // nothing special to do
503 if ( size != Default )
504 CopyFmtChar(*(format - 1));
505 CopyFmtChar(*format++);
506 }
507 }
508 }
509 }
510
511 #else // !wxNEED_PRINTF_CONVERSION
512 // no conversion necessary
513 #define wxFormatConverter(x) (x)
514 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
515
516 #ifdef __WXDEBUG__
517 // For testing the format converter
518 wxString wxConvertFormat(const wxChar *format)
519 {
520 return wxString(wxFormatConverter(format));
521 }
522 #endif
523
524 // ----------------------------------------------------------------------------
525 // wxPrintf(), wxScanf() and relatives
526 // ----------------------------------------------------------------------------
527
528 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
529
530 int wxDoScanf( const wxChar *format, ... )
531 {
532 va_list argptr;
533 va_start(argptr, format);
534
535 int ret = vwscanf(wxFormatConverter(format), argptr );
536
537 va_end(argptr);
538
539 return ret;
540 }
541
542 int wxDoSscanf( const wxChar *str, const wxChar *format, ... )
543 {
544 va_list argptr;
545 va_start(argptr, format);
546
547 int ret = vswscanf( str, wxFormatConverter(format), argptr );
548
549 va_end(argptr);
550
551 return ret;
552 }
553
554 int wxDoFscanf( FILE *stream, const wxChar *format, ... )
555 {
556 va_list argptr;
557 va_start(argptr, format);
558 int ret = vfwscanf(stream, wxFormatConverter(format), argptr);
559
560 va_end(argptr);
561
562 return ret;
563 }
564
565 int wxDoPrintf( const wxChar *format, ... )
566 {
567 va_list argptr;
568 va_start(argptr, format);
569
570 int ret = vwprintf( wxFormatConverter(format), argptr );
571
572 va_end(argptr);
573
574 return ret;
575 }
576
577 #ifndef wxSnprintf
578 int wxDoSnprintf( wxChar *str, size_t size, const wxChar *format, ... )
579 {
580 va_list argptr;
581 va_start(argptr, format);
582
583 int ret = vswprintf( str, size, wxFormatConverter(format), argptr );
584
585 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
586 // doesn't nul terminate on truncation.
587 str[size - 1] = 0;
588
589 va_end(argptr);
590
591 return ret;
592 }
593 #endif // wxSnprintf
594
595 int wxDoSprintf( wxChar *str, const wxChar *format, ... )
596 {
597 va_list argptr;
598 va_start(argptr, format);
599
600 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
601 // it's safe to implement this one in terms of it
602 wxString s(wxString::FormatV(format, argptr));
603 wxStrcpy(str, s);
604
605 va_end(argptr);
606
607 return s.length();
608 }
609
610 int wxDoFprintf( FILE *stream, const wxChar *format, ... )
611 {
612 va_list argptr;
613 va_start( argptr, format );
614
615 int ret = vfwprintf( stream, wxFormatConverter(format), argptr );
616
617 va_end(argptr);
618
619 return ret;
620 }
621
622 int wxVsscanf( const wxChar *str, const wxChar *format, va_list argptr )
623 {
624 return vswscanf( str, wxFormatConverter(format), argptr );
625 }
626
627 int wxVfprintf( FILE *stream, const wxChar *format, va_list argptr )
628 {
629 return vfwprintf( stream, wxFormatConverter(format), argptr );
630 }
631
632 int wxVprintf( const wxChar *format, va_list argptr )
633 {
634 return vwprintf( wxFormatConverter(format), argptr );
635 }
636
637 #ifndef wxVsnprintf
638 int wxVsnprintf( wxChar *str, size_t size, const wxChar *format, va_list argptr )
639 {
640 return vswprintf( str, size, wxFormatConverter(format), argptr );
641 }
642 #endif // wxVsnprintf
643
644 int wxVsprintf( wxChar *str, const wxChar *format, va_list argptr )
645 {
646 // same as for wxSprintf()
647 return vswprintf(str, INT_MAX / 4, wxFormatConverter(format), argptr);
648 }
649
650 #endif // wxNEED_PRINTF_CONVERSION
651
652 #if wxUSE_WCHAR_T
653
654 // ----------------------------------------------------------------------------
655 // ctype.h stuff (currently unused)
656 // ----------------------------------------------------------------------------
657
658 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
659 inline WORD wxMSW_ctype(wxChar ch)
660 {
661 WORD ret;
662 GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
663 return ret;
664 }
665
666 WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); }
667 WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); }
668 WXDLLEXPORT int wxIscntrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; }
669 WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; }
670 WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); }
671 WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); }
672 WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); }
673 WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; }
674 WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; }
675 WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); }
676 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; }
677 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); }
678 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
679 #endif
680
681 #ifdef wxNEED_WX_MBSTOWCS
682
683 WXDLLEXPORT size_t wxMbstowcs (wchar_t * out, const char * in, size_t outlen)
684 {
685 if (!out)
686 {
687 size_t outsize = 0;
688 while(*in++)
689 outsize++;
690 return outsize;
691 }
692
693 const char* origin = in;
694
695 while (outlen-- && *in)
696 {
697 *out++ = (wchar_t) *in++;
698 }
699
700 *out = '\0';
701
702 return in - origin;
703 }
704
705 WXDLLEXPORT size_t wxWcstombs (char * out, const wchar_t * in, size_t outlen)
706 {
707 if (!out)
708 {
709 size_t outsize = 0;
710 while(*in++)
711 outsize++;
712 return outsize;
713 }
714
715 const wchar_t* origin = in;
716
717 while (outlen-- && *in)
718 {
719 *out++ = (char) *in++;
720 }
721
722 *out = '\0';
723
724 return in - origin;
725 }
726
727 #endif // wxNEED_WX_MBSTOWCS
728
729 #if defined(wxNEED_WX_CTYPE_H)
730
731 #include <CoreFoundation/CoreFoundation.h>
732
733 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
734 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
735 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
736 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
737 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
738 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
739 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
740 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
741 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
742 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
743
744 WXDLLEXPORT int wxIsalnum(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalnumset, ch); }
745 WXDLLEXPORT int wxIsalpha(wxChar ch) { return CFCharacterSetIsCharacterMember(cfalphaset, ch); }
746 WXDLLEXPORT int wxIscntrl(wxChar ch) { return CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
747 WXDLLEXPORT int wxIsdigit(wxChar ch) { return CFCharacterSetIsCharacterMember(cfdigitset, ch); }
748 WXDLLEXPORT int wxIsgraph(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch) && ch != ' '; }
749 WXDLLEXPORT int wxIslower(wxChar ch) { return CFCharacterSetIsCharacterMember(cflowerset, ch); }
750 WXDLLEXPORT int wxIsprint(wxChar ch) { return !CFCharacterSetIsCharacterMember(cfcntrlset, ch); }
751 WXDLLEXPORT int wxIspunct(wxChar ch) { return CFCharacterSetIsCharacterMember(cfpunctset, ch); }
752 WXDLLEXPORT int wxIsspace(wxChar ch) { return CFCharacterSetIsCharacterMember(cfspaceset, ch); }
753 WXDLLEXPORT int wxIsupper(wxChar ch) { return CFCharacterSetIsCharacterMember(cfupperset, ch); }
754 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxIsdigit(ch) || (ch>='a' && ch<='f') || (ch>='A' && ch<='F'); }
755 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)tolower((char)(ch)); }
756 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)toupper((char)(ch)); }
757
758 #endif // wxNEED_WX_CTYPE_H
759
760 #ifndef wxStrdupA
761
762 WXDLLEXPORT char *wxStrdupA(const char *s)
763 {
764 return strcpy((char *)malloc(strlen(s) + 1), s);
765 }
766
767 #endif // wxStrdupA
768
769 #ifndef wxStrdupW
770
771 WXDLLEXPORT wchar_t * wxStrdupW(const wchar_t *pwz)
772 {
773 size_t size = (wxWcslen(pwz) + 1) * sizeof(wchar_t);
774 wchar_t *ret = (wchar_t *) malloc(size);
775 memcpy(ret, pwz, size);
776 return ret;
777 }
778
779 #endif // wxStrdupW
780
781 #ifndef wxStricmp
782 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
783 {
784 register wxChar c1, c2;
785 do {
786 c1 = wxTolower(*psz1++);
787 c2 = wxTolower(*psz2++);
788 } while ( c1 && (c1 == c2) );
789 return c1 - c2;
790 }
791 #endif
792
793 #ifndef wxStricmp
794 int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n)
795 {
796 // initialize the variables just to suppress stupid gcc warning
797 register wxChar c1 = 0, c2 = 0;
798 while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++;
799 if (n) {
800 if (c1 < c2) return -1;
801 if (c1 > c2) return 1;
802 }
803 return 0;
804 }
805 #endif
806
807 #ifndef wxSetlocale_
808 wxWCharBuffer wxSetlocale_(int category, const wxChar *locale)
809 {
810 char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale));
811
812 return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld));
813 }
814
815 wxWCharBuffer wxSetlocale(int category, const wxChar *locale)
816 {
817 wxWCharBuffer rv = wxSetlocale_(category, locale);
818 if ( rv )
819 wxUpdateLocaleIsUtf8();
820 return rv;
821 }
822 #else // defined(wxSetlocale_)
823 const wxChar *wxSetlocale(int category, const wxChar *locale)
824 {
825 const wxChar *rv = wxSetlocale_(category, locale);
826 if ( rv )
827 wxUpdateLocaleIsUtf8();
828 return rv;
829 }
830 #endif // wxSetlocale_ defined or not
831
832 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
833 WXDLLEXPORT size_t wxWcslen(const wchar_t *s)
834 {
835 size_t n = 0;
836 while ( *s++ )
837 n++;
838
839 return n;
840 }
841 #endif
842
843 // ----------------------------------------------------------------------------
844 // string.h functions
845 // ----------------------------------------------------------------------------
846
847 #ifdef wxNEED_WX_STRING_H
848
849 // RN: These need to be c externed for the regex lib
850 #ifdef __cplusplus
851 extern "C" {
852 #endif
853
854 WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
855 {
856 wxChar *ret = dest;
857 while (*dest) dest++;
858 while ((*dest++ = *src++));
859 return ret;
860 }
861
862 WXDLLEXPORT const wxChar * wxStrchr(const wxChar *s, wxChar c)
863 {
864 // be careful here as the terminating NUL makes part of the string
865 while ( *s != c )
866 {
867 if ( !*s++ )
868 return NULL;
869 }
870
871 return s;
872 }
873
874 WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
875 {
876 while ((*s1 == *s2) && *s1) s1++, s2++;
877 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
878 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
879 return 0;
880 }
881
882 WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
883 {
884 wxChar *ret = dest;
885 while ((*dest++ = *src++));
886 return ret;
887 }
888
889 WXDLLEXPORT size_t wxStrlen_(const wxChar *s)
890 {
891 size_t n = 0;
892 while ( *s++ )
893 n++;
894
895 return n;
896 }
897
898
899 WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
900 {
901 wxChar *ret = dest;
902 while (*dest) dest++;
903 while (n && (*dest++ = *src++)) n--;
904 return ret;
905 }
906
907 WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
908 {
909 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
910 if (n) {
911 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
912 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
913 }
914 return 0;
915 }
916
917 WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
918 {
919 wxChar *ret = dest;
920 while (n && (*dest++ = *src++)) n--;
921 while (n) *dest++=0, n--; // the docs specify padding with zeroes
922 return ret;
923 }
924
925 WXDLLEXPORT const wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
926 {
927 while (*s && !wxStrchr(accept, *s))
928 s++;
929
930 return *s ? s : NULL;
931 }
932
933 WXDLLEXPORT const wxChar * wxStrrchr(const wxChar *s, wxChar c)
934 {
935 const wxChar *ret = NULL;
936 do
937 {
938 if ( *s == c )
939 ret = s;
940 s++;
941 }
942 while ( *s );
943
944 return ret;
945 }
946
947 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
948 {
949 size_t len = 0;
950 while (wxStrchr(accept, *s++)) len++;
951 return len;
952 }
953
954 WXDLLEXPORT const wxChar *wxStrstr(const wxChar *haystack, const wxChar *needle)
955 {
956 wxASSERT_MSG( needle != NULL, _T("NULL argument in wxStrstr") );
957
958 // VZ: this is not exactly the most efficient string search algorithm...
959
960 const size_t len = wxStrlen(needle);
961
962 while ( const wxChar *fnd = wxStrchr(haystack, *needle) )
963 {
964 if ( !wxStrncmp(fnd, needle, len) )
965 return fnd;
966
967 haystack = fnd + 1;
968 }
969
970 return NULL;
971 }
972
973 #ifdef __cplusplus
974 }
975 #endif
976
977 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
978 {
979 const wxChar *start = nptr;
980
981 // FIXME: only correct for C locale
982 while (wxIsspace(*nptr)) nptr++;
983 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
984 while (wxIsdigit(*nptr)) nptr++;
985 if (*nptr == wxT('.')) {
986 nptr++;
987 while (wxIsdigit(*nptr)) nptr++;
988 }
989 if (*nptr == wxT('E') || *nptr == wxT('e')) {
990 nptr++;
991 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
992 while (wxIsdigit(*nptr)) nptr++;
993 }
994
995 wxString data(nptr, nptr-start);
996 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
997 char *rdat = wxMBSTRINGCAST dat;
998 double ret = strtod(dat, &rdat);
999
1000 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
1001
1002 return ret;
1003 }
1004
1005 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
1006 {
1007 const wxChar *start = nptr;
1008
1009 // FIXME: only correct for C locale
1010 while (wxIsspace(*nptr)) nptr++;
1011 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
1012 if (((base == 0) || (base == 16)) &&
1013 (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
1014 nptr += 2;
1015 base = 16;
1016 }
1017 else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
1018 else if (base == 0) base = 10;
1019
1020 while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
1021 (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
1022
1023 wxString data(start, nptr-start);
1024 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
1025 char *rdat = wxMBSTRINGCAST dat;
1026 long int ret = strtol(dat, &rdat, base);
1027
1028 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
1029
1030 return ret;
1031 }
1032
1033 WXDLLEXPORT unsigned long int wxStrtoul(const wxChar *nptr, wxChar **endptr, int base)
1034 {
1035 return (unsigned long int) wxStrtol(nptr, endptr, base);
1036 }
1037
1038 #endif // wxNEED_WX_STRING_H
1039
1040 #ifdef wxNEED_WX_STDIO_H
1041 WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
1042 {
1043 char mode_buffer[10];
1044 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
1045 mode_buffer[i] = (char) mode[i];
1046
1047 return fopen( wxConvFile.cWX2MB(path), mode_buffer );
1048 }
1049
1050 WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
1051 {
1052 char mode_buffer[10];
1053 for (size_t i = 0; i < wxStrlen(mode)+1; i++)
1054 mode_buffer[i] = (char) mode[i];
1055
1056 return freopen( wxConvFile.cWX2MB(path), mode_buffer, stream );
1057 }
1058
1059 WXDLLEXPORT int wxRemove(const wxChar *path)
1060 {
1061 return remove( wxConvFile.cWX2MB(path) );
1062 }
1063
1064 WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
1065 {
1066 return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) );
1067 }
1068 #endif
1069
1070 #ifndef wxAtof
1071 double WXDLLEXPORT wxAtof(const wxChar *psz)
1072 {
1073 #ifdef __WXWINCE__
1074 double d;
1075 wxString str(psz);
1076 if (str.ToDouble(& d))
1077 return d;
1078
1079 return 0.0;
1080 #else
1081 return atof(wxConvLibc.cWX2MB(psz));
1082 #endif
1083 }
1084 #endif
1085
1086 #ifdef wxNEED_WX_STDLIB_H
1087 int WXDLLEXPORT wxAtoi(const wxChar *psz)
1088 {
1089 return atoi(wxConvLibc.cWX2MB(psz));
1090 }
1091
1092 long WXDLLEXPORT wxAtol(const wxChar *psz)
1093 {
1094 return atol(wxConvLibc.cWX2MB(psz));
1095 }
1096
1097 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
1098 {
1099 #if wxUSE_UNICODE
1100 // NB: buffer returned by getenv() is allowed to be overwritten next
1101 // time getenv() is called, so it is OK to use static string
1102 // buffer to hold the data.
1103 static wxWCharBuffer value((wxChar*)NULL);
1104 value = wxConvLibc.cMB2WX(getenv(wxConvLibc.cWX2MB(name)));
1105 return value.data();
1106 #else
1107 return getenv(name);
1108 #endif
1109 }
1110
1111 int WXDLLEXPORT wxSystem(const wxChar *psz)
1112 {
1113 return system(wxConvLibc.cWX2MB(psz));
1114 }
1115
1116 #endif // wxNEED_WX_STDLIB_H
1117
1118 #ifdef wxNEED_WX_TIME_H
1119 WXDLLEXPORT size_t
1120 wxStrftime(wxChar *s, size_t maxsize, const wxChar *fmt, const struct tm *tm)
1121 {
1122 if ( !maxsize )
1123 return 0;
1124
1125 wxCharBuffer buf(maxsize);
1126
1127 wxCharBuffer bufFmt(wxConvLibc.cWX2MB(fmt));
1128 if ( !bufFmt )
1129 return 0;
1130
1131 size_t ret = strftime(buf.data(), maxsize, bufFmt, tm);
1132 if ( !ret )
1133 return 0;
1134
1135 wxWCharBuffer wbuf = wxConvLibc.cMB2WX(buf);
1136 if ( !wbuf )
1137 return 0;
1138
1139 wxStrncpy(s, wbuf, maxsize);
1140 return wxStrlen(s);
1141 }
1142 #endif // wxNEED_WX_TIME_H
1143
1144 #ifndef wxCtime
1145 WXDLLEXPORT wxChar *wxCtime(const time_t *timep)
1146 {
1147 // normally the string is 26 chars but give one more in case some broken
1148 // DOS compiler decides to use "\r\n" instead of "\n" at the end
1149 static wxChar buf[27];
1150
1151 // ctime() is guaranteed to return a string containing only ASCII
1152 // characters, as its format is always the same for any locale
1153 wxStrncpy(buf, wxString::FromAscii(ctime(timep)), WXSIZEOF(buf));
1154 buf[WXSIZEOF(buf) - 1] = _T('\0');
1155
1156 return buf;
1157 }
1158 #endif // wxCtime
1159
1160 #endif // wxUSE_WCHAR_T
1161
1162 #ifndef wxStrtoll
1163 static wxULongLong_t wxStrtoullBase(const wxChar* nptr, wxChar** endptr, int base, wxChar* sign)
1164 {
1165 wxULongLong_t sum = 0;
1166 wxString wxstr(nptr);
1167 wxString::const_iterator i = wxstr.begin();
1168 wxString::const_iterator end = wxstr.end();
1169
1170 // Skip spaces
1171 while ( i != end && wxIsspace(*i) ) i++;
1172
1173 // Starts with sign?
1174 *sign = wxT(' ');
1175 if ( i != end )
1176 {
1177 wxChar c = *i;
1178 if ( c == wxT('+') || c == wxT('-') )
1179 {
1180 *sign = c;
1181 i++;
1182 }
1183 }
1184
1185 // Starts with 0x?
1186 if ( i != end && *i == wxT('0') )
1187 {
1188 i++;
1189 if ( i != end )
1190 {
1191 if ( *i == wxT('x') && (base == 16 || base == 0) )
1192 {
1193 base = 16;
1194 i++;
1195 }
1196 else
1197 {
1198 if ( endptr )
1199 *endptr = (wxChar*) nptr;
1200 wxSET_ERRNO(EINVAL);
1201 return sum;
1202 }
1203 }
1204 else
1205 i--;
1206 }
1207
1208 if ( base == 0 )
1209 base = 10;
1210
1211 for ( ; i != end; i++ )
1212 {
1213 unsigned int n;
1214
1215 wxChar c = *i;
1216 if ( c >= wxT('0') )
1217 {
1218 if ( c <= wxT('9') )
1219 n = c - wxT('0');
1220 else
1221 n = wxTolower(c) - wxT('a') + 10;
1222 }
1223 else
1224 break;
1225
1226 if ( n >= (unsigned int)base )
1227 // Invalid character (for this base)
1228 break;
1229
1230 wxULongLong_t prevsum = sum;
1231 sum = (sum * base) + n;
1232
1233 if ( sum < prevsum )
1234 {
1235 wxSET_ERRNO(ERANGE);
1236 break;
1237 }
1238 }
1239
1240 if ( endptr )
1241 {
1242 const wxChar& endref = *i;
1243 *endptr = &(wxChar&)endref;
1244 }
1245
1246 return sum;
1247 }
1248
1249 wxULongLong_t wxStrtoull(const wxChar* nptr, wxChar** endptr, int base)
1250 {
1251 wxChar sign;
1252 wxULongLong_t uval = wxStrtoullBase(nptr, endptr, base, &sign);
1253
1254 if ( sign == wxT('-') )
1255 {
1256 wxSET_ERRNO(ERANGE);
1257 uval = 0;
1258 }
1259
1260 return uval;
1261 }
1262
1263 wxLongLong_t wxStrtoll(const wxChar* nptr, wxChar** endptr, int base)
1264 {
1265 wxChar sign;
1266 wxULongLong_t uval = wxStrtoullBase(nptr, endptr, base, &sign);
1267 wxLongLong_t val = 0;
1268
1269 if ( sign == wxT('-') )
1270 {
1271 if ( uval <= wxULL(wxINT64_MAX+1) )
1272 {
1273 if ( uval == wxULL(wxINT64_MAX+1))
1274 val = -((wxLongLong_t)wxINT64_MAX) - 1;
1275 else
1276 val = -((wxLongLong_t)uval);
1277 }
1278 else
1279 {
1280 wxSET_ERRNO(ERANGE);
1281 }
1282 }
1283 else if ( uval <= wxINT64_MAX )
1284 {
1285 val = uval;
1286 }
1287 else
1288 {
1289 wxSET_ERRNO(ERANGE);
1290 }
1291
1292 return val;
1293 }
1294 #endif // wxStrtoll
1295
1296 // ----------------------------------------------------------------------------
1297 // functions which we may need even if !wxUSE_WCHAR_T
1298 // ----------------------------------------------------------------------------
1299
1300 #ifndef wxStrtok
1301
1302 WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
1303 {
1304 if (!psz)
1305 {
1306 psz = *save_ptr;
1307 if ( !psz )
1308 return NULL;
1309 }
1310
1311 psz += wxStrspn(psz, delim);
1312 if (!*psz)
1313 {
1314 *save_ptr = (wxChar *)NULL;
1315 return (wxChar *)NULL;
1316 }
1317
1318 wxChar *ret = psz;
1319 psz = wxStrpbrk(psz, delim);
1320 if (!psz)
1321 {
1322 *save_ptr = (wxChar*)NULL;
1323 }
1324 else
1325 {
1326 *psz = wxT('\0');
1327 *save_ptr = psz + 1;
1328 }
1329
1330 return ret;
1331 }
1332
1333 #endif // wxStrtok
1334
1335 // ----------------------------------------------------------------------------
1336 // missing C RTL functions
1337 // ----------------------------------------------------------------------------
1338
1339 #ifdef wxNEED_STRDUP
1340
1341 char *strdup(const char *s)
1342 {
1343 char *dest = (char*) malloc( strlen( s ) + 1 ) ;
1344 if ( dest )
1345 strcpy( dest , s ) ;
1346 return dest ;
1347 }
1348 #endif // wxNEED_STRDUP
1349
1350 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1351
1352 void *calloc( size_t num, size_t size )
1353 {
1354 void** ptr = (void **)malloc(num * size);
1355 memset( ptr, 0, num * size);
1356 return ptr;
1357 }
1358
1359 #endif // __WXWINCE__ <= 211
1360
1361 #ifdef __WXWINCE__
1362
1363 int wxRemove(const wxChar *path)
1364 {
1365 return ::DeleteFile(path) == 0;
1366 }
1367
1368 #endif
1369
1370
1371 // ----------------------------------------------------------------------------
1372 // wxLocaleIsUtf8
1373 // ----------------------------------------------------------------------------
1374
1375 #if wxUSE_UNICODE_UTF8
1376
1377 #if !wxUSE_UTF8_LOCALE_ONLY
1378 bool wxLocaleIsUtf8 = false; // the safer setting if not known
1379 #endif
1380
1381 static bool wxIsLocaleUtf8()
1382 {
1383 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1384 // because a) it may be unavailable in some builds and b) has slightly
1385 // different semantics (default locale instead of current)
1386
1387 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1388 // GNU libc provides current character set this way (this conforms to
1389 // Unix98)
1390 const char *charset = nl_langinfo(CODESET);
1391 if ( charset )
1392 {
1393 // "UTF-8" is used by modern glibc versions, but test other variants
1394 // as well, just in case:
1395 return strcmp(charset, "UTF-8") == 0 ||
1396 strcmp(charset, "utf-8") == 0 ||
1397 strcmp(charset, "UTF8") == 0 ||
1398 strcmp(charset, "utf8") == 0;
1399 }
1400 else // nl_langinfo() failed
1401 #endif
1402 {
1403 // we don't know what charset libc is using, so assume the worst
1404 // to be safe:
1405 return false;
1406 }
1407 }
1408
1409 void wxUpdateLocaleIsUtf8()
1410 {
1411 #if wxUSE_UTF8_LOCALE_ONLY
1412 if ( !wxIsLocaleUtf8() )
1413 {
1414 wxLogFatalError(_T("This program requires UTF-8 locale to run."));
1415 }
1416 #else // !wxUSE_UTF8_LOCALE_ONLY
1417 wxLocaleIsUtf8 = wxIsLocaleUtf8();
1418 #endif
1419 }
1420
1421 #endif // wxUSE_UTF8_LOCALE_ONLY