]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wxcrt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/wxcrt.cpp
3 // Purpose: wxChar CRT wrappers implementation
5 // Modified by: Ron Lee, Francesco Montorsi
8 // Copyright: (c) wxWidgets copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
13 // headers, declarations, constants
14 // ===========================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #define _ISOC9X_SOURCE 1 // to get vsscanf()
26 #define _BSD_SOURCE 1 // to still get strdup()
36 #include "wx/msw/wince/time.h"
40 #include "wx/string.h"
42 #include "wx/utils.h" // for wxMin and wxMax
47 // there is no errno.h under CE apparently
48 #define wxSET_ERRNO(value)
52 #define wxSET_ERRNO(value) errno = value
55 #if defined(__MWERKS__) && __MSL__ >= 0x6000
61 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
63 // assume that we have mbsrtowcs() too if we have wcsrtombs()
66 memset(&mbstate
, 0, sizeof(mbstate_t));
71 if (n
) *buf
= wxT('\0');
75 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
77 return wxMbstowcs(buf
, psz
, n
);
81 // note that we rely on common (and required by Unix98 but unfortunately not
82 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
83 // to just get the size of the needed buffer -- this is needed as otherwise
84 // we have no idea about how much space we need and if the CRT doesn't
85 // support it (the only currently known example being Metrowerks, see
86 // wx/crt.h) we don't use its mbstowcs() at all
88 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
90 return wxMbstowcs((wchar_t *) NULL
, psz
, 0);
94 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
98 memset(&mbstate
, 0, sizeof(mbstate_t));
103 // glibc2.1 chokes on null input
107 #ifdef HAVE_WCSRTOMBS
108 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
110 return wxWcstombs(buf
, pwz
, n
);
114 #ifdef HAVE_WCSRTOMBS
115 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
117 return wxWcstombs((char *) NULL
, pwz
, 0);
120 #endif // wxUSE_WCHAR_T
122 bool WXDLLEXPORT
wxOKlibc()
124 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
125 // glibc 2.0 uses UTF-8 even when it shouldn't
127 if ((MB_CUR_MAX
== 2) &&
128 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
130 // this is UTF-8 allright, check whether that's what we want
131 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
132 if ((strlen(cur_locale
) < 4) ||
133 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
134 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
135 // nope, don't use libc conversion
143 char* wxSetlocale(int category
, const char *locale
)
145 char *rv
= setlocale(category
, locale
);
146 if ( locale
!= NULL
/* setting locale, not querying */ &&
147 rv
/* call was successful */ )
149 wxUpdateLocaleIsUtf8();
154 // ============================================================================
155 // printf() functions business
156 // ============================================================================
158 // special test mode: define all functions below even if we don't really need
159 // them to be able to test them
169 #define wxNEED_WPRINTF
171 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
);
175 /* Digital Mars adds count to _stprintf (C99) so convert */
176 int wxCRT_SprintfW (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
180 va_start( arglist
, format
);
181 int iLen
= swprintf ( s
, -1, format
, arglist
);
187 // ----------------------------------------------------------------------------
188 // implement the standard IO functions for wide char if libc doesn't have them
189 // ----------------------------------------------------------------------------
192 int wxCRT_FputsW(const wchar_t *ws
, FILE *stream
)
194 wxCharBuffer
buf(wxConvLibc
.cWC2MB(ws
));
198 // counting the number of wide characters written isn't worth the trouble,
199 // simply distinguish between ok and error
200 return wxCRT_FputsA(buf
, stream
) == -1 ? -1 : 0;
202 #endif // !wxCRT_FputsW
205 int wxCRT_PutsW(const wchar_t *ws
)
207 int rc
= wxCRT_FputsW(ws
, stdout
);
210 if ( wxCRT_FputsW(L
"\n", stdout
) == -1 )
218 #endif // !wxCRT_PutsW
221 int /* not wint_t */ wxCRT_FputcW(wchar_t wc
, FILE *stream
)
223 wchar_t ws
[2] = { wc
, L
'\0' };
225 return wxCRT_FputsW(ws
, stream
);
227 #endif // !wxCRT_FputcW
229 // NB: we only implement va_list functions here, the ones taking ... are
230 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
231 // the definitions there to avoid duplicating them here
232 #ifdef wxNEED_WPRINTF
234 // TODO: implement the scanf() functions
235 static int vwscanf(const wchar_t *format
, va_list argptr
)
237 wxFAIL_MSG( _T("TODO") );
242 static int vswscanf(const wchar_t *ws
, const wchar_t *format
, va_list argptr
)
244 // The best we can do without proper Unicode support in glibc is to
245 // convert the strings into MB representation and run ANSI version
246 // of the function. This doesn't work with %c and %s because of difference
247 // in size of char and wchar_t, though.
249 wxCHECK_MSG( wxStrstr(format
, _T("%s")) == NULL
, -1,
250 _T("incomplete vswscanf implementation doesn't allow %s") );
251 wxCHECK_MSG( wxStrstr(format
, _T("%c")) == NULL
, -1,
252 _T("incomplete vswscanf implementation doesn't allow %c") );
254 return vsscanf(wxConvLibc
.cWX2MB(ws
), wxConvLibc
.cWX2MB(format
), argptr
);
257 static int vfwscanf(FILE *stream
, const wchar_t *format
, va_list argptr
)
259 wxFAIL_MSG( _T("TODO") );
264 #define vswprintf wxCRT_VsnprintfW_
266 static int vfwprintf(FILE *stream
, const wchar_t *format
, va_list argptr
)
269 int rc
= s
.PrintfV(format
, argptr
);
273 // we can't do much better without Unicode support in libc...
274 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
281 static int vwprintf(const wchar_t *format
, va_list argptr
)
283 return wxCRT_VfprintfW(stdout
, format
, argptr
);
286 #endif // wxNEED_WPRINTF
288 #ifdef wxNEED_PRINTF_CONVERSION
290 // ----------------------------------------------------------------------------
291 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
292 // ----------------------------------------------------------------------------
295 Here are the gory details. We want to follow the Windows/MS conventions,
300 format specifier results in
301 -----------------------------------
307 format specifier results in
308 -----------------------------------
313 while on POSIX systems we have %C identical to %lc and %c always means char
314 (in any mode) while %lc always means wchar_t,
316 So to use native functions in order to get our semantics we must do the
317 following translations in Unicode mode (nothing to do in ANSI mode):
319 wxWidgets specifier POSIX specifier
320 ----------------------------------------
326 And, of course, the same should be done for %s as well.
329 class wxFormatConverter
332 wxFormatConverter(const wchar_t *format
);
334 // notice that we only translated the string if m_fmtOrig == NULL (as set
335 // by CopyAllBefore()), otherwise we should simply use the original format
336 operator const wchar_t *() const
337 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
340 // copy another character to the translated format: this function does the
341 // copy if we are translating but doesn't do anything at all if we don't,
342 // so we don't create the translated format string at all unless we really
343 // need to (i.e. InsertFmtChar() is called)
344 wchar_t CopyFmtChar(wchar_t ch
)
348 // we're translating, do copy
353 // simply increase the count which should be copied by
354 // CopyAllBefore() later if needed
361 // insert an extra character
362 void InsertFmtChar(wchar_t ch
)
366 // so far we haven't translated anything yet
375 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
377 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
379 // we won't need it any longer
383 static bool IsFlagChar(wchar_t ch
)
385 return ch
== _T('-') || ch
== _T('+') ||
386 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
389 void SkipDigits(const wchar_t **ptpc
)
391 while ( **ptpc
>= _T('0') && **ptpc
<= _T('9') )
392 CopyFmtChar(*(*ptpc
)++);
395 // the translated format
398 // the original format
399 const wchar_t *m_fmtOrig
;
401 // the number of characters already copied
405 wxFormatConverter::wxFormatConverter(const wchar_t *format
)
412 if ( CopyFmtChar(*format
++) == _T('%') )
415 while ( IsFlagChar(*format
) )
416 CopyFmtChar(*format
++);
418 // and possible width
419 if ( *format
== _T('*') )
420 CopyFmtChar(*format
++);
425 if ( *format
== _T('.') )
427 CopyFmtChar(*format
++);
428 if ( *format
== _T('*') )
429 CopyFmtChar(*format
++);
434 // next we can have a size modifier
450 // "ll" has a different meaning!
451 if ( format
[1] != _T('l') )
463 // and finally we should have the type
468 // %C and %hC -> %c and %lC -> %lc
470 CopyFmtChar(_T('l'));
472 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
477 // %c -> %lc but %hc stays %hc and %lc is still %lc
478 if ( size
== Default
)
479 InsertFmtChar(_T('l'));
483 // nothing special to do
484 if ( size
!= Default
)
485 CopyFmtChar(*(format
- 1));
486 CopyFmtChar(*format
++);
492 #else // !wxNEED_PRINTF_CONVERSION
493 // no conversion necessary
494 #define wxFormatConverter(x) (x)
495 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
498 // For testing the format converter
499 wxString
wxConvertFormat(const wchar_t *format
)
501 return wxString(wxFormatConverter(format
));
505 // ----------------------------------------------------------------------------
506 // wxPrintf(), wxScanf() and relatives
507 // ----------------------------------------------------------------------------
509 // FIXME-UTF8: do format conversion using (modified) wxFormatConverter in
510 // template wrappers, not here; note that it will needed to
511 // translate all forms of string specifiers to %(l)s for wxPrintf(),
512 // but it only should do what it did in 2.8 for wxScanf()!
514 #ifndef wxCRT_PrintfW
515 int wxCRT_PrintfW( const wchar_t *format
, ... )
518 va_start(argptr
, format
);
520 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
528 #ifndef wxCRT_FprintfW
529 int wxCRT_FprintfW( FILE *stream
, const wchar_t *format
, ... )
532 va_start( argptr
, format
);
534 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
542 #ifndef wxCRT_VfprintfW
543 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
)
545 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
549 #ifndef wxCRT_VprintfW
550 int wxCRT_VprintfW( const wchar_t *format
, va_list argptr
)
552 return vwprintf( wxFormatConverter(format
), argptr
);
556 #ifndef wxCRT_VsnprintfW
557 int wxCRT_VsnprintfW(wchar_t *str
, size_t size
, const wchar_t *format
, va_list argptr
)
559 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
561 #endif // !wxCRT_VsnprintfW
563 #ifndef wxCRT_VsprintfW
564 int wxCRT_VsprintfW( wchar_t *str
, const wchar_t *format
, va_list argptr
)
566 // same as for wxSprintf()
567 return vswprintf(str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
572 int wxCRT_ScanfW(const wchar_t *format
, ...)
575 va_start(argptr
, format
);
578 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
579 int ret
= std::vwscanf(wxFormatConverter(format
), argptr
);
581 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
584 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
593 #ifndef wxCRT_SscanfW
594 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...)
597 va_start(argptr
, format
);
600 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
601 int ret
= std::vswscanf(str
, wxFormatConverter(format
), argptr
);
603 int ret
= vswscanf(str
, wxFormatConverter(format
), argptr
);
606 int ret
= vswscanf(str
, wxFormatConverter(format
), argptr
);
615 #ifndef wxCRT_FscanfW
616 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...)
619 va_start(argptr
, format
);
621 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
622 int ret
= std::vfwscanf(stream
, wxFormatConverter(format
), argptr
);
624 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
627 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
636 #ifndef wxCRT_VsscanfW
637 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list argptr
)
640 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
641 return std::vswscanf(str
, wxFormatConverter(format
), argptr
);
643 return vswscanf(str
, wxFormatConverter(format
), argptr
);
646 return vswscanf(str
, wxFormatConverter(format
), argptr
);
652 // ----------------------------------------------------------------------------
653 // wrappers to printf and scanf function families
654 // ----------------------------------------------------------------------------
656 #if !wxUSE_UTF8_LOCALE_ONLY
657 int wxDoSprintfWchar(char *str
, const wxChar
*format
, ...)
660 va_start(argptr
, format
);
662 int rv
= wxVsprintf(str
, format
, argptr
);
667 #endif // !wxUSE_UTF8_LOCALE_ONLY
669 #if wxUSE_UNICODE_UTF8
670 int wxDoSprintfUtf8(char *str
, const char *format
, ...)
673 va_start(argptr
, format
);
675 int rv
= wxVsprintf(str
, format
, argptr
);
680 #endif // wxUSE_UNICODE_UTF8
684 #if !wxUSE_UTF8_LOCALE_ONLY
685 int wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...)
688 va_start(argptr
, format
);
690 int rv
= wxVsprintf(str
, format
, argptr
);
695 #endif // !wxUSE_UTF8_LOCALE_ONLY
697 #if wxUSE_UNICODE_UTF8
698 int wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...)
701 va_start(argptr
, format
);
703 int rv
= wxVsprintf(str
, format
, argptr
);
708 #endif // wxUSE_UNICODE_UTF8
710 #endif // wxUSE_UNICODE
712 #if !wxUSE_UTF8_LOCALE_ONLY
713 int wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...)
716 va_start(argptr
, format
);
718 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
723 #endif // !wxUSE_UTF8_LOCALE_ONLY
725 #if wxUSE_UNICODE_UTF8
726 int wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...)
729 va_start(argptr
, format
);
731 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
736 #endif // wxUSE_UNICODE_UTF8
740 #if !wxUSE_UTF8_LOCALE_ONLY
741 int wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...)
744 va_start(argptr
, format
);
746 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
751 #endif // !wxUSE_UTF8_LOCALE_ONLY
753 #if wxUSE_UNICODE_UTF8
754 int wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...)
757 va_start(argptr
, format
);
759 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
764 #endif // wxUSE_UNICODE_UTF8
766 #endif // wxUSE_UNICODE
769 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
770 #define vsnprintf wx_fixed_vsnprintf
775 #if !wxUSE_UTF8_LOCALE_ONLY
776 static int ConvertStringToBuf(const wxString
& s
, char *out
, size_t outsize
)
778 const wxWX2WCbuf buf
= s
.wc_str();
780 size_t len
= wxConvLibc
.FromWChar(out
, outsize
, buf
);
781 if ( len
!= wxCONV_FAILED
)
784 return wxConvLibc
.FromWChar(NULL
, 0, buf
);
786 #endif // !wxUSE_UTF8_LOCALE_ONLY
788 #if wxUSE_UNICODE_UTF8
789 static int ConvertStringToBuf(const wxString
& s
, wchar_t *out
, size_t outsize
)
791 const wxWX2WCbuf
buf(s
.wc_str());
792 size_t len
= wxWcslen(buf
);
794 memcpy(out
, buf
, (len
+1) * sizeof(wchar_t));
795 // else: not enough space
798 #endif // wxUSE_UNICODE_UTF8
801 static size_t PrintfViaString(T
*out
, size_t outsize
,
802 const wxString
& format
, va_list argptr
)
805 s
.PrintfV(format
, argptr
);
807 return ConvertStringToBuf(s
, out
, outsize
);
809 #endif // wxUSE_UNICODE
811 int wxVsprintf(char *str
, const wxString
& format
, va_list argptr
)
813 #if wxUSE_UTF8_LOCALE_ONLY
814 return vsprintf(str
, format
.wx_str(), argptr
);
816 #if wxUSE_UNICODE_UTF8
817 if ( wxLocaleIsUtf8
)
818 return vsprintf(str
, format
.wx_str(), argptr
);
822 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
824 return wxCRT_VsprintfA(str
, format
.mb_str(), argptr
);
830 int wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
)
832 #if wxUSE_UNICODE_WCHAR
833 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
834 #else // wxUSE_UNICODE_UTF8
835 #if !wxUSE_UTF8_LOCALE_ONLY
836 if ( !wxLocaleIsUtf8
)
837 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
840 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
841 #endif // wxUSE_UNICODE_UTF8
843 #endif // wxUSE_UNICODE
845 int wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
)
848 #if wxUSE_UTF8_LOCALE_ONLY
849 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
851 #if wxUSE_UNICODE_UTF8
852 if ( wxLocaleIsUtf8
)
853 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
858 // NB: if this code is called, then wxString::PrintV() would use the
859 // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
861 rv
= PrintfViaString(str
, size
, format
, argptr
);
864 rv
= wxCRT_VsnprintfA(str
, size
, format
.mb_str(), argptr
);
868 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
869 // doesn't nul terminate on truncation.
876 int wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
)
880 #if wxUSE_UNICODE_WCHAR
881 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
882 #else // wxUSE_UNICODE_UTF8
883 #if !wxUSE_UTF8_LOCALE_ONLY
884 if ( !wxLocaleIsUtf8
)
885 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
889 // NB: if this code is called, then wxString::PrintV() would use the
890 // char* version of wxVsnprintf(), so it's safe to use PrintV()
892 rv
= PrintfViaString(str
, size
, format
, argptr
);
894 #endif // wxUSE_UNICODE_UTF8
896 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
897 // doesn't nul terminate on truncation.
902 #endif // wxUSE_UNICODE
906 // ----------------------------------------------------------------------------
907 // ctype.h stuff (currently unused)
908 // ----------------------------------------------------------------------------
910 #ifdef wxNEED_WX_MBSTOWCS
912 WXDLLEXPORT
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
922 const char* origin
= in
;
924 while (outlen
-- && *in
)
926 *out
++ = (wchar_t) *in
++;
934 WXDLLEXPORT
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
944 const wchar_t* origin
= in
;
946 while (outlen
-- && *in
)
948 *out
++ = (char) *in
++;
956 #endif // wxNEED_WX_MBSTOWCS
958 #ifndef wxCRT_StrdupA
959 WXDLLEXPORT
char *wxCRT_StrdupA(const char *s
)
961 return strcpy((char *)malloc(strlen(s
) + 1), s
);
963 #endif // wxCRT_StrdupA
965 #ifndef wxCRT_StrdupW
966 WXDLLEXPORT
wchar_t * wxCRT_StrdupW(const wchar_t *pwz
)
968 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
969 wchar_t *ret
= (wchar_t *) malloc(size
);
970 memcpy(ret
, pwz
, size
);
973 #endif // wxCRT_StrdupW
975 #ifndef wxCRT_StricmpA
976 int WXDLLEXPORT
wxCRT_StricmpA(const char *psz1
, const char *psz2
)
978 register char c1
, c2
;
980 c1
= wxTolower(*psz1
++);
981 c2
= wxTolower(*psz2
++);
982 } while ( c1
&& (c1
== c2
) );
985 #endif // !defined(wxCRT_StricmpA)
987 #ifndef wxCRT_StricmpW
988 int WXDLLEXPORT
wxCRT_StricmpW(const wchar_t *psz1
, const wchar_t *psz2
)
990 register wchar_t c1
, c2
;
992 c1
= wxTolower(*psz1
++);
993 c2
= wxTolower(*psz2
++);
994 } while ( c1
&& (c1
== c2
) );
997 #endif // !defined(wxCRT_StricmpW)
999 #ifndef wxCRT_StrnicmpA
1000 int WXDLLEXPORT
wxCRT_StrnicmpA(const char *s1
, const char *s2
, size_t n
)
1002 // initialize the variables just to suppress stupid gcc warning
1003 register char c1
= 0, c2
= 0;
1004 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
1006 if (c1
< c2
) return -1;
1007 if (c1
> c2
) return 1;
1011 #endif // !defined(wxCRT_StrnicmpA)
1013 #ifndef wxCRT_StrnicmpW
1014 int WXDLLEXPORT
wxCRT_StrnicmpW(const wchar_t *s1
, const wchar_t *s2
, size_t n
)
1016 // initialize the variables just to suppress stupid gcc warning
1017 register wchar_t c1
= 0, c2
= 0;
1018 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
1020 if (c1
< c2
) return -1;
1021 if (c1
> c2
) return 1;
1025 #endif // !defined(wxCRT_StrnicmpW)
1027 // ----------------------------------------------------------------------------
1028 // string.h functions
1029 // ----------------------------------------------------------------------------
1031 // this (and wxCRT_StrncmpW below) are extern "C" because they are needed
1032 // by regex code, the rest isn't needed, so it's not declared as extern "C"
1033 #ifndef wxCRT_StrlenW
1034 extern "C" WXDLLEXPORT
size_t wxCRT_StrlenW(const wchar_t *s
)
1044 // ----------------------------------------------------------------------------
1045 // stdlib.h functions
1046 // ----------------------------------------------------------------------------
1048 #ifndef wxCRT_GetenvW
1049 wchar_t* WXDLLEXPORT
wxCRT_GetenvW(const wchar_t *name
)
1051 // NB: buffer returned by getenv() is allowed to be overwritten next
1052 // time getenv() is called, so it is OK to use static string
1053 // buffer to hold the data.
1054 static wxWCharBuffer
value((wchar_t*)NULL
);
1055 value
= wxConvLibc
.cMB2WC(getenv(wxConvLibc
.cWC2MB(name
)));
1056 return value
.data();
1058 #endif // !wxCRT_GetenvW
1060 #ifndef wxCRT_StrftimeW
1062 wxCRT_StrftimeW(wchar_t *s
, size_t maxsize
, const wchar_t *fmt
, const struct tm
*tm
)
1067 wxCharBuffer
buf(maxsize
);
1069 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
1073 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
1077 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
1081 wxCRT_StrncpyW(s
, wbuf
, maxsize
);
1082 return wxCRT_StrlenW(s
);
1084 #endif // !wxCRT_StrftimeW
1086 #endif // wxUSE_WCHAR_T
1088 template<typename T
>
1089 static wxULongLong_t
1090 wxCRT_StrtoullBase(const T
* nptr
, T
** endptr
, int base
, T
* sign
)
1092 wxULongLong_t sum
= 0;
1093 wxString
wxstr(nptr
);
1094 wxString::const_iterator i
= wxstr
.begin();
1095 wxString::const_iterator end
= wxstr
.end();
1098 while ( i
!= end
&& wxIsspace(*i
) ) i
++;
1100 // Starts with sign?
1105 if ( c
== wxT('+') || c
== wxT('-') )
1113 if ( i
!= end
&& *i
== wxT('0') )
1118 if ( *i
== wxT('x') && (base
== 16 || base
== 0) )
1126 *endptr
= (T
*) nptr
;
1127 wxSET_ERRNO(EINVAL
);
1138 for ( ; i
!= end
; i
++ )
1143 if ( c
>= wxT('0') )
1145 if ( c
<= wxT('9') )
1148 n
= wxTolower(c
) - wxT('a') + 10;
1153 if ( n
>= (unsigned int)base
)
1154 // Invalid character (for this base)
1157 wxULongLong_t prevsum
= sum
;
1158 sum
= (sum
* base
) + n
;
1160 if ( sum
< prevsum
)
1162 wxSET_ERRNO(ERANGE
);
1169 *endptr
= (T
*)(nptr
+ (i
- wxstr
.begin()));
1175 template<typename T
>
1176 static wxULongLong_t
wxCRT_DoStrtoull(const T
* nptr
, T
** endptr
, int base
)
1179 wxULongLong_t uval
= wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1181 if ( sign
== wxT('-') )
1183 wxSET_ERRNO(ERANGE
);
1190 template<typename T
>
1191 static wxLongLong_t
wxCRT_DoStrtoll(const T
* nptr
, T
** endptr
, int base
)
1194 wxULongLong_t uval
= wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1195 wxLongLong_t val
= 0;
1197 if ( sign
== wxT('-') )
1199 if ( uval
<= wxULL(wxINT64_MAX
+1) )
1201 if ( uval
== wxULL(wxINT64_MAX
+1))
1202 val
= -((wxLongLong_t
)wxINT64_MAX
) - 1;
1204 val
= -((wxLongLong_t
)uval
);
1208 wxSET_ERRNO(ERANGE
);
1211 else if ( uval
<= wxINT64_MAX
)
1217 wxSET_ERRNO(ERANGE
);
1223 #ifndef wxCRT_StrtollA
1224 wxLongLong_t
wxCRT_StrtollA(const char* nptr
, char** endptr
, int base
)
1225 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1227 #ifndef wxCRT_StrtollW
1228 wxLongLong_t
wxCRT_StrtollW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1229 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1232 #ifndef wxCRT_StrtoullA
1233 wxULongLong_t
wxCRT_StrtoullA(const char* nptr
, char** endptr
, int base
)
1234 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1236 #ifndef wxCRT_StrtoullW
1237 wxULongLong_t
wxCRT_StrtoullW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1238 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1241 // ----------------------------------------------------------------------------
1242 // functions which we may need even if !wxUSE_WCHAR_T
1243 // ----------------------------------------------------------------------------
1245 template<typename T
>
1246 static T
*wxCRT_DoStrtok(T
*psz
, const T
*delim
, T
**save_ptr
)
1255 psz
+= wxStrspn(psz
, delim
);
1258 *save_ptr
= (T
*)NULL
;
1263 psz
= wxStrpbrk(psz
, delim
);
1266 *save_ptr
= (T
*)NULL
;
1271 *save_ptr
= psz
+ 1;
1277 #ifndef wxCRT_StrtokA
1278 char *wxCRT_StrtokA(char *psz
, const char *delim
, char **save_ptr
)
1279 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1281 #ifndef wxCRT_StrtokW
1282 wchar_t *wxCRT_StrtokW(wchar_t *psz
, const wchar_t *delim
, wchar_t **save_ptr
)
1283 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1286 // ----------------------------------------------------------------------------
1287 // missing C RTL functions
1288 // ----------------------------------------------------------------------------
1290 #ifdef wxNEED_STRDUP
1292 char *strdup(const char *s
)
1294 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1296 strcpy( dest
, s
) ;
1299 #endif // wxNEED_STRDUP
1301 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1303 void *calloc( size_t num
, size_t size
)
1305 void** ptr
= (void **)malloc(num
* size
);
1306 memset( ptr
, 0, num
* size
);
1310 #endif // __WXWINCE__ <= 211
1313 int wxCRT_RemoveW(const wchar_t *path
)
1315 return ::DeleteFile(path
) == 0;
1319 #ifndef wxCRT_TmpnamW
1320 wchar_t *wxCRT_TmpnamW(wchar_t *s
)
1322 // tmpnam_r() returns NULL if s=NULL, do the same
1323 wxCHECK_MSG( s
, NULL
, "wxTmpnam must be called with a buffer" );
1326 #define L_tmpnam 1024
1328 wxCharBuffer
buf(L_tmpnam
);
1331 wxConvLibc
.ToWChar(s
, L_tmpnam
+1, buf
.data());
1334 #endif // !wxCRT_TmpnamW
1337 // ============================================================================
1339 // ============================================================================
1341 #if wxUSE_UNICODE_UTF8
1343 #if !wxUSE_UTF8_LOCALE_ONLY
1344 bool wxLocaleIsUtf8
= false; // the safer setting if not known
1347 static bool wxIsLocaleUtf8()
1349 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1350 // because a) it may be unavailable in some builds and b) has slightly
1351 // different semantics (default locale instead of current)
1353 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1354 // GNU libc provides current character set this way (this conforms to
1356 const char *charset
= nl_langinfo(CODESET
);
1359 // "UTF-8" is used by modern glibc versions, but test other variants
1360 // as well, just in case:
1361 if ( strcmp(charset
, "UTF-8") == 0 ||
1362 strcmp(charset
, "utf-8") == 0 ||
1363 strcmp(charset
, "UTF8") == 0 ||
1364 strcmp(charset
, "utf8") == 0 )
1371 // check if we're running under the "C" locale: it is 7bit subset
1372 // of UTF-8, so it can be safely used with the UTF-8 build:
1373 const char *lc_ctype
= setlocale(LC_CTYPE
, NULL
);
1375 (strcmp(lc_ctype
, "C") == 0 || strcmp(lc_ctype
, "POSIX") == 0) )
1380 // we don't know what charset libc is using, so assume the worst
1385 void wxUpdateLocaleIsUtf8()
1387 #if wxUSE_UTF8_LOCALE_ONLY
1388 if ( !wxIsLocaleUtf8() )
1390 wxLogFatalError(_T("This program requires UTF-8 locale to run."));
1392 #else // !wxUSE_UTF8_LOCALE_ONLY
1393 wxLocaleIsUtf8
= wxIsLocaleUtf8();
1397 #endif // wxUSE_UNICODE_UTF8
1399 // ============================================================================
1400 // wx wrappers for CRT functions
1401 // ============================================================================
1403 #if wxUSE_UNICODE_WCHAR
1404 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW
1405 #elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
1406 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \
1407 return_kw wxLocaleIsUtf8 ? callA : callW
1408 #else // ANSI or UTF8 only
1409 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA
1412 int wxPuts(const wxString
& s
)
1414 CALL_ANSI_OR_UNICODE(return,
1415 wxCRT_PutsA(s
.mb_str()),
1416 wxCRT_PutsW(s
.wc_str()));
1419 int wxFputs(const wxString
& s
, FILE *stream
)
1421 CALL_ANSI_OR_UNICODE(return,
1422 wxCRT_FputsA(s
.mb_str(), stream
),
1423 wxCRT_FputsW(s
.wc_str(), stream
));
1426 int wxFputc(const wxUniChar
& c
, FILE *stream
)
1428 #if !wxUSE_UNICODE // FIXME-UTF8: temporary, remove this with ANSI build
1429 return wxCRT_FputcA((char)c
, stream
);
1431 CALL_ANSI_OR_UNICODE(return,
1432 wxCRT_FputsA(c
.AsUTF8(), stream
),
1433 wxCRT_FputcW((wchar_t)c
, stream
));
1437 void wxPerror(const wxString
& s
)
1439 #ifdef wxCRT_PerrorW
1440 CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE
,
1441 wxCRT_PerrorA(s
.mb_str()),
1442 wxCRT_PerrorW(s
.wc_str()));
1444 wxCRT_PerrorA(s
.mb_str());
1448 wchar_t *wxFgets(wchar_t *s
, int size
, FILE *stream
)
1450 wxCHECK_MSG( s
, NULL
, "empty buffer passed to wxFgets()" );
1452 wxCharBuffer
buf(size
- 1);
1453 // FIXME: this reads too little data if wxConvLibc uses UTF-8 ('size' wide
1454 // characters may be encoded by up to 'size'*4 bytes), but what
1456 if ( wxFgets(buf
.data(), size
, stream
) == NULL
)
1459 if ( wxConvLibc
.ToWChar(s
, size
, buf
, wxNO_LEN
) == wxCONV_FAILED
)
1465 // ----------------------------------------------------------------------------
1466 // wxScanf() and friends
1467 // ----------------------------------------------------------------------------
1470 int wxVsscanf(const char *str
, const char *format
, va_list ap
)
1471 { return wxCRT_VsscanfA(str
, format
, ap
); }
1472 int wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
)
1473 { return wxCRT_VsscanfW(str
, format
, ap
); }
1474 int wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
)
1475 { return wxCRT_VsscanfA(str
, format
, ap
); }
1476 int wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
)
1477 { return wxCRT_VsscanfW(str
, format
, ap
); }
1478 int wxVsscanf(const wxString
& str
, const char *format
, va_list ap
)
1479 { return wxCRT_VsscanfA(str
.mb_str(), format
, ap
); }
1480 int wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
)
1481 { return wxCRT_VsscanfW(str
.wc_str(), format
, ap
); }
1482 int wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
)
1483 { return wxCRT_VsscanfA(str
.AsCharBuf(), format
, ap
); }
1484 int wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
)
1485 { return wxCRT_VsscanfW(str
.AsWCharBuf(), format
, ap
); }
1486 #endif // !__VISUALC__