]>
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 // FIXME-UTF8: we only implement widechar version of vsnprintf() in wxprint.cpp,
564 // so this one has to convert the data for now
565 #ifndef wxCRT_VsnprintfA
566 int wxCRT_VsnprintfA(char *buf
, size_t len
, const char *format
, va_list argptr
)
568 wxWCharBuffer
wbuf(len
);
569 int rt
= wxCRT_VsnprintfW(wbuf
.data(), len
,
570 (const wchar_t*)wxConvLibc
.cMB2WC(format
),
572 if ( rt
< 0 || rt
>= (int)len
)
575 if ( wxConvLibc
.FromWChar(buf
, len
, wbuf
) == wxCONV_FAILED
)
580 #endif // !wxCRT_VsnprintfA
582 #ifndef wxCRT_VsprintfW
583 int wxCRT_VsprintfW( wchar_t *str
, const wchar_t *format
, va_list argptr
)
585 // same as for wxSprintf()
586 return vswprintf(str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
591 int wxCRT_ScanfW(const wchar_t *format
, ...)
594 va_start(argptr
, format
);
597 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
598 int ret
= std::vwscanf(wxFormatConverter(format
), argptr
);
600 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
603 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
612 #ifndef wxCRT_SscanfW
613 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...)
616 va_start(argptr
, format
);
619 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
620 int ret
= std::vswscanf(str
, wxFormatConverter(format
), argptr
);
622 int ret
= vswscanf(str
, wxFormatConverter(format
), argptr
);
625 int ret
= vswscanf(str
, wxFormatConverter(format
), argptr
);
634 #ifndef wxCRT_FscanfW
635 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...)
638 va_start(argptr
, format
);
640 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
641 int ret
= std::vfwscanf(stream
, wxFormatConverter(format
), argptr
);
643 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
646 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
655 #ifndef wxCRT_VsscanfW
656 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list argptr
)
659 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
660 return std::vswscanf(str
, wxFormatConverter(format
), argptr
);
662 return vswscanf(str
, wxFormatConverter(format
), argptr
);
665 return vswscanf(str
, wxFormatConverter(format
), argptr
);
671 // ----------------------------------------------------------------------------
672 // wrappers to printf and scanf function families
673 // ----------------------------------------------------------------------------
675 #if !wxUSE_UTF8_LOCALE_ONLY
676 int wxDoSprintfWchar(char *str
, const wxChar
*format
, ...)
679 va_start(argptr
, format
);
681 int rv
= wxVsprintf(str
, format
, argptr
);
686 #endif // !wxUSE_UTF8_LOCALE_ONLY
688 #if wxUSE_UNICODE_UTF8
689 int wxDoSprintfUtf8(char *str
, const char *format
, ...)
692 va_start(argptr
, format
);
694 int rv
= wxVsprintf(str
, format
, argptr
);
699 #endif // wxUSE_UNICODE_UTF8
703 #if !wxUSE_UTF8_LOCALE_ONLY
704 int wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...)
707 va_start(argptr
, format
);
709 int rv
= wxVsprintf(str
, format
, argptr
);
714 #endif // !wxUSE_UTF8_LOCALE_ONLY
716 #if wxUSE_UNICODE_UTF8
717 int wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...)
720 va_start(argptr
, format
);
722 int rv
= wxVsprintf(str
, format
, argptr
);
727 #endif // wxUSE_UNICODE_UTF8
729 #endif // wxUSE_UNICODE
731 #if !wxUSE_UTF8_LOCALE_ONLY
732 int wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...)
735 va_start(argptr
, format
);
737 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
742 #endif // !wxUSE_UTF8_LOCALE_ONLY
744 #if wxUSE_UNICODE_UTF8
745 int wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...)
748 va_start(argptr
, format
);
750 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
755 #endif // wxUSE_UNICODE_UTF8
759 #if !wxUSE_UTF8_LOCALE_ONLY
760 int wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...)
763 va_start(argptr
, format
);
765 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
770 #endif // !wxUSE_UTF8_LOCALE_ONLY
772 #if wxUSE_UNICODE_UTF8
773 int wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...)
776 va_start(argptr
, format
);
778 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
783 #endif // wxUSE_UNICODE_UTF8
785 #endif // wxUSE_UNICODE
788 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
789 #define vsnprintf wx_fixed_vsnprintf
794 #if !wxUSE_UTF8_LOCALE_ONLY
795 static int ConvertStringToBuf(const wxString
& s
, char *out
, size_t outsize
)
797 const wxWX2WCbuf buf
= s
.wc_str();
799 size_t len
= wxConvLibc
.FromWChar(out
, outsize
, buf
);
800 if ( len
!= wxCONV_FAILED
)
803 return wxConvLibc
.FromWChar(NULL
, 0, buf
);
805 #endif // !wxUSE_UTF8_LOCALE_ONLY
807 #if wxUSE_UNICODE_UTF8
808 static int ConvertStringToBuf(const wxString
& s
, wchar_t *out
, size_t outsize
)
810 const wxWX2WCbuf
buf(s
.wc_str());
811 size_t len
= wxWcslen(buf
);
813 memcpy(out
, buf
, (len
+1) * sizeof(wchar_t));
814 // else: not enough space
817 #endif // wxUSE_UNICODE_UTF8
820 static size_t PrintfViaString(T
*out
, size_t outsize
,
821 const wxString
& format
, va_list argptr
)
824 s
.PrintfV(format
, argptr
);
826 return ConvertStringToBuf(s
, out
, outsize
);
828 #endif // wxUSE_UNICODE
830 int wxVsprintf(char *str
, const wxString
& format
, va_list argptr
)
832 #if wxUSE_UTF8_LOCALE_ONLY
833 return vsprintf(str
, format
.wx_str(), argptr
);
835 #if wxUSE_UNICODE_UTF8
836 if ( wxLocaleIsUtf8
)
837 return vsprintf(str
, format
.wx_str(), argptr
);
841 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
843 return wxCRT_VsprintfA(str
, format
.mb_str(), argptr
);
849 int wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
)
851 #if wxUSE_UNICODE_WCHAR
852 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
853 #else // wxUSE_UNICODE_UTF8
854 #if !wxUSE_UTF8_LOCALE_ONLY
855 if ( !wxLocaleIsUtf8
)
856 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
859 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
860 #endif // wxUSE_UNICODE_UTF8
862 #endif // wxUSE_UNICODE
864 int wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
)
867 #if wxUSE_UTF8_LOCALE_ONLY
868 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
870 #if wxUSE_UNICODE_UTF8
871 if ( wxLocaleIsUtf8
)
872 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
877 // NB: if this code is called, then wxString::PrintV() would use the
878 // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
880 rv
= PrintfViaString(str
, size
, format
, argptr
);
883 rv
= wxCRT_VsnprintfA(str
, size
, format
.mb_str(), argptr
);
887 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
888 // doesn't nul terminate on truncation.
895 int wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
)
899 #if wxUSE_UNICODE_WCHAR
900 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
901 #else // wxUSE_UNICODE_UTF8
902 #if !wxUSE_UTF8_LOCALE_ONLY
903 if ( !wxLocaleIsUtf8
)
904 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
908 // NB: if this code is called, then wxString::PrintV() would use the
909 // char* version of wxVsnprintf(), so it's safe to use PrintV()
911 rv
= PrintfViaString(str
, size
, format
, argptr
);
913 #endif // wxUSE_UNICODE_UTF8
915 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
916 // doesn't nul terminate on truncation.
921 #endif // wxUSE_UNICODE
925 // ----------------------------------------------------------------------------
926 // ctype.h stuff (currently unused)
927 // ----------------------------------------------------------------------------
929 #ifdef wxNEED_WX_MBSTOWCS
931 WXDLLEXPORT
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
941 const char* origin
= in
;
943 while (outlen
-- && *in
)
945 *out
++ = (wchar_t) *in
++;
953 WXDLLEXPORT
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
963 const wchar_t* origin
= in
;
965 while (outlen
-- && *in
)
967 *out
++ = (char) *in
++;
975 #endif // wxNEED_WX_MBSTOWCS
977 #ifndef wxCRT_StrdupA
978 WXDLLEXPORT
char *wxCRT_StrdupA(const char *s
)
980 return strcpy((char *)malloc(strlen(s
) + 1), s
);
982 #endif // wxCRT_StrdupA
984 #ifndef wxCRT_StrdupW
985 WXDLLEXPORT
wchar_t * wxCRT_StrdupW(const wchar_t *pwz
)
987 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
988 wchar_t *ret
= (wchar_t *) malloc(size
);
989 memcpy(ret
, pwz
, size
);
992 #endif // wxCRT_StrdupW
994 #ifndef wxCRT_StricmpA
995 int WXDLLEXPORT
wxCRT_StricmpA(const char *psz1
, const char *psz2
)
997 register char c1
, c2
;
999 c1
= wxTolower(*psz1
++);
1000 c2
= wxTolower(*psz2
++);
1001 } while ( c1
&& (c1
== c2
) );
1004 #endif // !defined(wxCRT_StricmpA)
1006 #ifndef wxCRT_StricmpW
1007 int WXDLLEXPORT
wxCRT_StricmpW(const wchar_t *psz1
, const wchar_t *psz2
)
1009 register wchar_t c1
, c2
;
1011 c1
= wxTolower(*psz1
++);
1012 c2
= wxTolower(*psz2
++);
1013 } while ( c1
&& (c1
== c2
) );
1016 #endif // !defined(wxCRT_StricmpW)
1018 #ifndef wxCRT_StrnicmpA
1019 int WXDLLEXPORT
wxCRT_StrnicmpA(const char *s1
, const char *s2
, size_t n
)
1021 // initialize the variables just to suppress stupid gcc warning
1022 register char c1
= 0, c2
= 0;
1023 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
1025 if (c1
< c2
) return -1;
1026 if (c1
> c2
) return 1;
1030 #endif // !defined(wxCRT_StrnicmpA)
1032 #ifndef wxCRT_StrnicmpW
1033 int WXDLLEXPORT
wxCRT_StrnicmpW(const wchar_t *s1
, const wchar_t *s2
, size_t n
)
1035 // initialize the variables just to suppress stupid gcc warning
1036 register wchar_t c1
= 0, c2
= 0;
1037 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
1039 if (c1
< c2
) return -1;
1040 if (c1
> c2
) return 1;
1044 #endif // !defined(wxCRT_StrnicmpW)
1046 // ----------------------------------------------------------------------------
1047 // string.h functions
1048 // ----------------------------------------------------------------------------
1050 // this (and wxCRT_StrncmpW below) are extern "C" because they are needed
1051 // by regex code, the rest isn't needed, so it's not declared as extern "C"
1052 #ifndef wxCRT_StrlenW
1053 extern "C" WXDLLEXPORT
size_t wxCRT_StrlenW(const wchar_t *s
)
1063 // ----------------------------------------------------------------------------
1064 // stdlib.h functions
1065 // ----------------------------------------------------------------------------
1067 #ifndef wxCRT_GetenvW
1068 wchar_t* WXDLLEXPORT
wxCRT_GetenvW(const wchar_t *name
)
1070 // NB: buffer returned by getenv() is allowed to be overwritten next
1071 // time getenv() is called, so it is OK to use static string
1072 // buffer to hold the data.
1073 static wxWCharBuffer
value((wchar_t*)NULL
);
1074 value
= wxConvLibc
.cMB2WC(getenv(wxConvLibc
.cWC2MB(name
)));
1075 return value
.data();
1077 #endif // !wxCRT_GetenvW
1079 #ifndef wxCRT_StrftimeW
1081 wxCRT_StrftimeW(wchar_t *s
, size_t maxsize
, const wchar_t *fmt
, const struct tm
*tm
)
1086 wxCharBuffer
buf(maxsize
);
1088 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
1092 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
1096 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
1100 wxCRT_StrncpyW(s
, wbuf
, maxsize
);
1101 return wxCRT_StrlenW(s
);
1103 #endif // !wxCRT_StrftimeW
1105 #endif // wxUSE_WCHAR_T
1107 template<typename T
>
1108 static wxULongLong_t
1109 wxCRT_StrtoullBase(const T
* nptr
, T
** endptr
, int base
, T
* sign
)
1111 wxULongLong_t sum
= 0;
1112 wxString
wxstr(nptr
);
1113 wxString::const_iterator i
= wxstr
.begin();
1114 wxString::const_iterator end
= wxstr
.end();
1117 while ( i
!= end
&& wxIsspace(*i
) ) i
++;
1119 // Starts with sign?
1124 if ( c
== wxT('+') || c
== wxT('-') )
1132 if ( i
!= end
&& *i
== wxT('0') )
1137 if ( *i
== wxT('x') && (base
== 16 || base
== 0) )
1145 *endptr
= (T
*) nptr
;
1146 wxSET_ERRNO(EINVAL
);
1157 for ( ; i
!= end
; i
++ )
1162 if ( c
>= wxT('0') )
1164 if ( c
<= wxT('9') )
1167 n
= wxTolower(c
) - wxT('a') + 10;
1172 if ( n
>= (unsigned int)base
)
1173 // Invalid character (for this base)
1176 wxULongLong_t prevsum
= sum
;
1177 sum
= (sum
* base
) + n
;
1179 if ( sum
< prevsum
)
1181 wxSET_ERRNO(ERANGE
);
1188 *endptr
= (T
*)(nptr
+ (i
- wxstr
.begin()));
1194 template<typename T
>
1195 static wxULongLong_t
wxCRT_DoStrtoull(const T
* nptr
, T
** endptr
, int base
)
1198 wxULongLong_t uval
= wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1200 if ( sign
== wxT('-') )
1202 wxSET_ERRNO(ERANGE
);
1209 template<typename T
>
1210 static wxLongLong_t
wxCRT_DoStrtoll(const T
* nptr
, T
** endptr
, int base
)
1213 wxULongLong_t uval
= wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1214 wxLongLong_t val
= 0;
1216 if ( sign
== wxT('-') )
1218 if ( uval
<= wxULL(wxINT64_MAX
+1) )
1220 if ( uval
== wxULL(wxINT64_MAX
+1))
1221 val
= -((wxLongLong_t
)wxINT64_MAX
) - 1;
1223 val
= -((wxLongLong_t
)uval
);
1227 wxSET_ERRNO(ERANGE
);
1230 else if ( uval
<= wxINT64_MAX
)
1236 wxSET_ERRNO(ERANGE
);
1242 #ifndef wxCRT_StrtollA
1243 wxLongLong_t
wxCRT_StrtollA(const char* nptr
, char** endptr
, int base
)
1244 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1246 #ifndef wxCRT_StrtollW
1247 wxLongLong_t
wxCRT_StrtollW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1248 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1251 #ifndef wxCRT_StrtoullA
1252 wxULongLong_t
wxCRT_StrtoullA(const char* nptr
, char** endptr
, int base
)
1253 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1255 #ifndef wxCRT_StrtoullW
1256 wxULongLong_t
wxCRT_StrtoullW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1257 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1260 // ----------------------------------------------------------------------------
1261 // functions which we may need even if !wxUSE_WCHAR_T
1262 // ----------------------------------------------------------------------------
1264 template<typename T
>
1265 static T
*wxCRT_DoStrtok(T
*psz
, const T
*delim
, T
**save_ptr
)
1274 psz
+= wxStrspn(psz
, delim
);
1277 *save_ptr
= (T
*)NULL
;
1282 psz
= wxStrpbrk(psz
, delim
);
1285 *save_ptr
= (T
*)NULL
;
1290 *save_ptr
= psz
+ 1;
1296 #ifndef wxCRT_StrtokA
1297 char *wxCRT_StrtokA(char *psz
, const char *delim
, char **save_ptr
)
1298 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1300 #ifndef wxCRT_StrtokW
1301 wchar_t *wxCRT_StrtokW(wchar_t *psz
, const wchar_t *delim
, wchar_t **save_ptr
)
1302 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1305 // ----------------------------------------------------------------------------
1306 // missing C RTL functions
1307 // ----------------------------------------------------------------------------
1309 #ifdef wxNEED_STRDUP
1311 char *strdup(const char *s
)
1313 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1315 strcpy( dest
, s
) ;
1318 #endif // wxNEED_STRDUP
1320 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1322 void *calloc( size_t num
, size_t size
)
1324 void** ptr
= (void **)malloc(num
* size
);
1325 memset( ptr
, 0, num
* size
);
1329 #endif // __WXWINCE__ <= 211
1332 int wxCRT_RemoveW(const wchar_t *path
)
1334 return ::DeleteFile(path
) == 0;
1338 #ifndef wxCRT_TmpnamW
1339 wchar_t *wxCRT_TmpnamW(wchar_t *s
)
1341 // tmpnam_r() returns NULL if s=NULL, do the same
1342 wxCHECK_MSG( s
, NULL
, "wxTmpnam must be called with a buffer" );
1345 #define L_tmpnam 1024
1347 wxCharBuffer
buf(L_tmpnam
);
1350 wxConvLibc
.ToWChar(s
, L_tmpnam
+1, buf
.data());
1353 #endif // !wxCRT_TmpnamW
1356 // ============================================================================
1358 // ============================================================================
1360 #if wxUSE_UNICODE_UTF8
1362 #if !wxUSE_UTF8_LOCALE_ONLY
1363 bool wxLocaleIsUtf8
= false; // the safer setting if not known
1366 static bool wxIsLocaleUtf8()
1368 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1369 // because a) it may be unavailable in some builds and b) has slightly
1370 // different semantics (default locale instead of current)
1372 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1373 // GNU libc provides current character set this way (this conforms to
1375 const char *charset
= nl_langinfo(CODESET
);
1378 // "UTF-8" is used by modern glibc versions, but test other variants
1379 // as well, just in case:
1380 if ( strcmp(charset
, "UTF-8") == 0 ||
1381 strcmp(charset
, "utf-8") == 0 ||
1382 strcmp(charset
, "UTF8") == 0 ||
1383 strcmp(charset
, "utf8") == 0 )
1390 // check if we're running under the "C" locale: it is 7bit subset
1391 // of UTF-8, so it can be safely used with the UTF-8 build:
1392 const char *lc_ctype
= setlocale(LC_CTYPE
, NULL
);
1394 (strcmp(lc_ctype
, "C") == 0 || strcmp(lc_ctype
, "POSIX") == 0) )
1399 // we don't know what charset libc is using, so assume the worst
1404 void wxUpdateLocaleIsUtf8()
1406 #if wxUSE_UTF8_LOCALE_ONLY
1407 if ( !wxIsLocaleUtf8() )
1409 wxLogFatalError(_T("This program requires UTF-8 locale to run."));
1411 #else // !wxUSE_UTF8_LOCALE_ONLY
1412 wxLocaleIsUtf8
= wxIsLocaleUtf8();
1416 #endif // wxUSE_UNICODE_UTF8
1418 // ============================================================================
1419 // wx wrappers for CRT functions
1420 // ============================================================================
1422 #if wxUSE_UNICODE_WCHAR
1423 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW
1424 #elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
1425 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \
1426 return_kw wxLocaleIsUtf8 ? callA : callW
1427 #else // ANSI or UTF8 only
1428 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA
1431 int wxPuts(const wxString
& s
)
1433 CALL_ANSI_OR_UNICODE(return,
1434 wxCRT_PutsA(s
.mb_str()),
1435 wxCRT_PutsW(s
.wc_str()));
1438 int wxFputs(const wxString
& s
, FILE *stream
)
1440 CALL_ANSI_OR_UNICODE(return,
1441 wxCRT_FputsA(s
.mb_str(), stream
),
1442 wxCRT_FputsW(s
.wc_str(), stream
));
1445 int wxFputc(const wxUniChar
& c
, FILE *stream
)
1447 #if !wxUSE_UNICODE // FIXME-UTF8: temporary, remove this with ANSI build
1448 return wxCRT_FputcA((char)c
, stream
);
1450 CALL_ANSI_OR_UNICODE(return,
1451 wxCRT_FputsA(c
.AsUTF8(), stream
),
1452 wxCRT_FputcW((wchar_t)c
, stream
));
1456 void wxPerror(const wxString
& s
)
1458 #ifdef wxCRT_PerrorW
1459 CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE
,
1460 wxCRT_PerrorA(s
.mb_str()),
1461 wxCRT_PerrorW(s
.wc_str()));
1463 wxCRT_PerrorA(s
.mb_str());
1467 wchar_t *wxFgets(wchar_t *s
, int size
, FILE *stream
)
1469 wxCHECK_MSG( s
, NULL
, "empty buffer passed to wxFgets()" );
1471 wxCharBuffer
buf(size
- 1);
1472 // FIXME: this reads too little data if wxConvLibc uses UTF-8 ('size' wide
1473 // characters may be encoded by up to 'size'*4 bytes), but what
1475 if ( wxFgets(buf
.data(), size
, stream
) == NULL
)
1478 if ( wxConvLibc
.ToWChar(s
, size
, buf
, wxNO_LEN
) == wxCONV_FAILED
)
1484 // ----------------------------------------------------------------------------
1485 // wxScanf() and friends
1486 // ----------------------------------------------------------------------------
1489 int wxVsscanf(const char *str
, const char *format
, va_list ap
)
1490 { return wxCRT_VsscanfA(str
, format
, ap
); }
1491 int wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
)
1492 { return wxCRT_VsscanfW(str
, format
, ap
); }
1493 int wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
)
1494 { return wxCRT_VsscanfA(str
, format
, ap
); }
1495 int wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
)
1496 { return wxCRT_VsscanfW(str
, format
, ap
); }
1497 int wxVsscanf(const wxString
& str
, const char *format
, va_list ap
)
1498 { return wxCRT_VsscanfA(str
.mb_str(), format
, ap
); }
1499 int wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
)
1500 { return wxCRT_VsscanfW(str
.wc_str(), format
, ap
); }
1501 int wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
)
1502 { return wxCRT_VsscanfA(str
.AsCharBuf(), format
, ap
); }
1503 int wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
)
1504 { return wxCRT_VsscanfW(str
.AsWCharBuf(), format
, ap
); }
1505 #endif // !__VISUALC__