]>
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 // ----------------------------------------------------------------------------
289 // wxPrintf(), wxScanf() and relatives
290 // ----------------------------------------------------------------------------
292 // FIXME-UTF8: do format conversion using (modified) wxFormatConverter in
293 // template wrappers, not here; note that it will needed to
294 // translate all forms of string specifiers to %(l)s for wxPrintf(),
295 // but it only should do what it did in 2.8 for wxScanf()!
297 #ifndef wxCRT_PrintfW
298 int wxCRT_PrintfW( const wchar_t *format
, ... )
301 va_start(argptr
, format
);
303 int ret
= vwprintf( format
, argptr
);
311 #ifndef wxCRT_FprintfW
312 int wxCRT_FprintfW( FILE *stream
, const wchar_t *format
, ... )
315 va_start( argptr
, format
);
317 int ret
= vfwprintf( stream
, format
, argptr
);
325 #ifndef wxCRT_VfprintfW
326 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
)
328 return vfwprintf( stream
, format
, argptr
);
332 #ifndef wxCRT_VprintfW
333 int wxCRT_VprintfW( const wchar_t *format
, va_list argptr
)
335 return vwprintf( format
, argptr
);
339 #ifndef wxCRT_VsprintfW
340 int wxCRT_VsprintfW( wchar_t *str
, const wchar_t *format
, va_list argptr
)
342 // same as for wxSprintf()
343 return vswprintf(str
, INT_MAX
/ 4, format
, argptr
);
348 int wxCRT_ScanfW(const wchar_t *format
, ...)
351 va_start(argptr
, format
);
354 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
355 int ret
= std::vwscanf(format
, argptr
);
357 int ret
= vwscanf(format
, argptr
);
360 int ret
= vwscanf(format
, argptr
);
369 #ifndef wxCRT_SscanfW
370 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...)
373 va_start(argptr
, format
);
376 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
377 int ret
= std::vswscanf(str
, format
, argptr
);
379 int ret
= vswscanf(str
, format
, argptr
);
382 int ret
= vswscanf(str
, format
, argptr
);
391 #ifndef wxCRT_FscanfW
392 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...)
395 va_start(argptr
, format
);
397 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
398 int ret
= std::vfwscanf(stream
, format
, argptr
);
400 int ret
= vfwscanf(stream
, format
, argptr
);
403 int ret
= vfwscanf(stream
, format
, argptr
);
412 #ifndef wxCRT_VsscanfW
413 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list argptr
)
416 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
417 return std::vswscanf(str
, format
, argptr
);
419 return vswscanf(str
, format
, argptr
);
422 return vswscanf(str
, format
, argptr
);
428 // ----------------------------------------------------------------------------
429 // wrappers to printf and scanf function families
430 // ----------------------------------------------------------------------------
432 #if !wxUSE_UTF8_LOCALE_ONLY
433 int wxDoSprintfWchar(char *str
, const wxChar
*format
, ...)
436 va_start(argptr
, format
);
438 int rv
= wxVsprintf(str
, format
, argptr
);
443 #endif // !wxUSE_UTF8_LOCALE_ONLY
445 #if wxUSE_UNICODE_UTF8
446 int wxDoSprintfUtf8(char *str
, const char *format
, ...)
449 va_start(argptr
, format
);
451 int rv
= wxVsprintf(str
, format
, argptr
);
456 #endif // wxUSE_UNICODE_UTF8
460 #if !wxUSE_UTF8_LOCALE_ONLY
461 int wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...)
464 va_start(argptr
, format
);
466 int rv
= wxVsprintf(str
, format
, argptr
);
471 #endif // !wxUSE_UTF8_LOCALE_ONLY
473 #if wxUSE_UNICODE_UTF8
474 int wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...)
477 va_start(argptr
, format
);
479 int rv
= wxVsprintf(str
, format
, argptr
);
484 #endif // wxUSE_UNICODE_UTF8
486 #endif // wxUSE_UNICODE
488 #if !wxUSE_UTF8_LOCALE_ONLY
489 int wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...)
492 va_start(argptr
, format
);
494 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
499 #endif // !wxUSE_UTF8_LOCALE_ONLY
501 #if wxUSE_UNICODE_UTF8
502 int wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...)
505 va_start(argptr
, format
);
507 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
512 #endif // wxUSE_UNICODE_UTF8
516 #if !wxUSE_UTF8_LOCALE_ONLY
517 int wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...)
520 va_start(argptr
, format
);
522 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
527 #endif // !wxUSE_UTF8_LOCALE_ONLY
529 #if wxUSE_UNICODE_UTF8
530 int wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...)
533 va_start(argptr
, format
);
535 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
540 #endif // wxUSE_UNICODE_UTF8
542 #endif // wxUSE_UNICODE
545 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
546 #define vsnprintf wx_fixed_vsnprintf
551 #if !wxUSE_UTF8_LOCALE_ONLY
552 int wxInternalConvertStringToBuf(const wxString
& s
, char *out
, size_t outsize
)
554 const wxWX2WCbuf buf
= s
.wc_str();
556 size_t len
= wxConvLibc
.FromWChar(out
, outsize
, buf
);
557 if ( len
!= wxCONV_FAILED
)
560 return wxConvLibc
.FromWChar(NULL
, 0, buf
);
562 #endif // !wxUSE_UTF8_LOCALE_ONLY
564 #if wxUSE_UNICODE_UTF8
565 int wxInternalConvertStringToBuf(const wxString
& s
, wchar_t *out
, size_t outsize
)
567 const wxWX2WCbuf
buf(s
.wc_str());
568 size_t len
= wxWcslen(buf
);
570 memcpy(out
, buf
, (len
+1) * sizeof(wchar_t));
571 // else: not enough space
574 #endif // wxUSE_UNICODE_UTF8
577 static size_t PrintfViaString(T
*out
, size_t outsize
,
578 const wxString
& format
, va_list argptr
)
581 s
.PrintfV(format
, argptr
);
583 return wxInternalConvertStringToBuf(s
, out
, outsize
);
585 #endif // wxUSE_UNICODE
587 int wxVsprintf(char *str
, const wxString
& format
, va_list argptr
)
589 #if wxUSE_UTF8_LOCALE_ONLY
590 return vsprintf(str
, format
.wx_str(), argptr
);
592 #if wxUSE_UNICODE_UTF8
593 if ( wxLocaleIsUtf8
)
594 return vsprintf(str
, format
.wx_str(), argptr
);
598 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
600 return wxCRT_VsprintfA(str
, format
.mb_str(), argptr
);
606 int wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
)
608 #if wxUSE_UNICODE_WCHAR
609 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
610 #else // wxUSE_UNICODE_UTF8
611 #if !wxUSE_UTF8_LOCALE_ONLY
612 if ( !wxLocaleIsUtf8
)
613 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
616 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
617 #endif // wxUSE_UNICODE_UTF8
619 #endif // wxUSE_UNICODE
621 int wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
)
624 #if wxUSE_UTF8_LOCALE_ONLY
625 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
627 #if wxUSE_UNICODE_UTF8
628 if ( wxLocaleIsUtf8
)
629 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
634 // NB: if this code is called, then wxString::PrintV() would use the
635 // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
637 rv
= PrintfViaString(str
, size
, format
, argptr
);
640 rv
= wxCRT_VsnprintfA(str
, size
, format
.mb_str(), argptr
);
644 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
645 // doesn't nul terminate on truncation.
652 int wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
)
656 #if wxUSE_UNICODE_WCHAR
657 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
658 #else // wxUSE_UNICODE_UTF8
659 #if !wxUSE_UTF8_LOCALE_ONLY
660 if ( !wxLocaleIsUtf8
)
661 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
665 // NB: if this code is called, then wxString::PrintV() would use the
666 // char* version of wxVsnprintf(), so it's safe to use PrintV()
668 rv
= PrintfViaString(str
, size
, format
, argptr
);
670 #endif // wxUSE_UNICODE_UTF8
672 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
673 // doesn't nul terminate on truncation.
678 #endif // wxUSE_UNICODE
682 // ----------------------------------------------------------------------------
683 // ctype.h stuff (currently unused)
684 // ----------------------------------------------------------------------------
686 #ifdef wxNEED_WX_MBSTOWCS
688 WXDLLEXPORT
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
698 const char* origin
= in
;
700 while (outlen
-- && *in
)
702 *out
++ = (wchar_t) *in
++;
710 WXDLLEXPORT
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
720 const wchar_t* origin
= in
;
722 while (outlen
-- && *in
)
724 *out
++ = (char) *in
++;
732 #endif // wxNEED_WX_MBSTOWCS
734 #ifndef wxCRT_StrdupA
735 WXDLLEXPORT
char *wxCRT_StrdupA(const char *s
)
737 return strcpy((char *)malloc(strlen(s
) + 1), s
);
739 #endif // wxCRT_StrdupA
741 #ifndef wxCRT_StrdupW
742 WXDLLEXPORT
wchar_t * wxCRT_StrdupW(const wchar_t *pwz
)
744 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
745 wchar_t *ret
= (wchar_t *) malloc(size
);
746 memcpy(ret
, pwz
, size
);
749 #endif // wxCRT_StrdupW
751 #ifndef wxCRT_StricmpA
752 int WXDLLEXPORT
wxCRT_StricmpA(const char *psz1
, const char *psz2
)
754 register char c1
, c2
;
756 c1
= wxTolower(*psz1
++);
757 c2
= wxTolower(*psz2
++);
758 } while ( c1
&& (c1
== c2
) );
761 #endif // !defined(wxCRT_StricmpA)
763 #ifndef wxCRT_StricmpW
764 int WXDLLEXPORT
wxCRT_StricmpW(const wchar_t *psz1
, const wchar_t *psz2
)
766 register wchar_t c1
, c2
;
768 c1
= wxTolower(*psz1
++);
769 c2
= wxTolower(*psz2
++);
770 } while ( c1
&& (c1
== c2
) );
773 #endif // !defined(wxCRT_StricmpW)
775 #ifndef wxCRT_StrnicmpA
776 int WXDLLEXPORT
wxCRT_StrnicmpA(const char *s1
, const char *s2
, size_t n
)
778 // initialize the variables just to suppress stupid gcc warning
779 register char c1
= 0, c2
= 0;
780 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
782 if (c1
< c2
) return -1;
783 if (c1
> c2
) return 1;
787 #endif // !defined(wxCRT_StrnicmpA)
789 #ifndef wxCRT_StrnicmpW
790 int WXDLLEXPORT
wxCRT_StrnicmpW(const wchar_t *s1
, const wchar_t *s2
, size_t n
)
792 // initialize the variables just to suppress stupid gcc warning
793 register wchar_t c1
= 0, c2
= 0;
794 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
796 if (c1
< c2
) return -1;
797 if (c1
> c2
) return 1;
801 #endif // !defined(wxCRT_StrnicmpW)
803 // ----------------------------------------------------------------------------
804 // string.h functions
805 // ----------------------------------------------------------------------------
807 // this (and wxCRT_StrncmpW below) are extern "C" because they are needed
808 // by regex code, the rest isn't needed, so it's not declared as extern "C"
809 #ifndef wxCRT_StrlenW
810 extern "C" WXDLLEXPORT
size_t wxCRT_StrlenW(const wchar_t *s
)
820 // ----------------------------------------------------------------------------
821 // stdlib.h functions
822 // ----------------------------------------------------------------------------
824 #ifndef wxCRT_GetenvW
825 wchar_t* WXDLLEXPORT
wxCRT_GetenvW(const wchar_t *name
)
827 // NB: buffer returned by getenv() is allowed to be overwritten next
828 // time getenv() is called, so it is OK to use static string
829 // buffer to hold the data.
830 static wxWCharBuffer
value((wchar_t*)NULL
);
831 value
= wxConvLibc
.cMB2WC(getenv(wxConvLibc
.cWC2MB(name
)));
834 #endif // !wxCRT_GetenvW
836 #ifndef wxCRT_StrftimeW
838 wxCRT_StrftimeW(wchar_t *s
, size_t maxsize
, const wchar_t *fmt
, const struct tm
*tm
)
843 wxCharBuffer
buf(maxsize
);
845 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
849 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
853 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
857 wxCRT_StrncpyW(s
, wbuf
, maxsize
);
858 return wxCRT_StrlenW(s
);
860 #endif // !wxCRT_StrftimeW
862 #endif // wxUSE_WCHAR_T
866 wxCRT_StrtoullBase(const T
* nptr
, T
** endptr
, int base
, T
* sign
)
868 wxULongLong_t sum
= 0;
869 wxString
wxstr(nptr
);
870 wxString::const_iterator i
= wxstr
.begin();
871 wxString::const_iterator end
= wxstr
.end();
874 while ( i
!= end
&& wxIsspace(*i
) ) i
++;
881 if ( c
== wxT('+') || c
== wxT('-') )
889 if ( i
!= end
&& *i
== wxT('0') )
894 if ( *i
== wxT('x') && (base
== 16 || base
== 0) )
914 for ( ; i
!= end
; i
++ )
924 n
= wxTolower(c
) - wxT('a') + 10;
929 if ( n
>= (unsigned int)base
)
930 // Invalid character (for this base)
933 wxULongLong_t prevsum
= sum
;
934 sum
= (sum
* base
) + n
;
945 *endptr
= (T
*)(nptr
+ (i
- wxstr
.begin()));
952 static wxULongLong_t
wxCRT_DoStrtoull(const T
* nptr
, T
** endptr
, int base
)
955 wxULongLong_t uval
= wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
957 if ( sign
== wxT('-') )
967 static wxLongLong_t
wxCRT_DoStrtoll(const T
* nptr
, T
** endptr
, int base
)
970 wxULongLong_t uval
= wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
971 wxLongLong_t val
= 0;
973 if ( sign
== wxT('-') )
975 if ( uval
<= wxULL(wxINT64_MAX
+1) )
977 if ( uval
== wxULL(wxINT64_MAX
+1))
978 val
= -((wxLongLong_t
)wxINT64_MAX
) - 1;
980 val
= -((wxLongLong_t
)uval
);
987 else if ( uval
<= wxINT64_MAX
)
999 #ifndef wxCRT_StrtollA
1000 wxLongLong_t
wxCRT_StrtollA(const char* nptr
, char** endptr
, int base
)
1001 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1003 #ifndef wxCRT_StrtollW
1004 wxLongLong_t
wxCRT_StrtollW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1005 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1008 #ifndef wxCRT_StrtoullA
1009 wxULongLong_t
wxCRT_StrtoullA(const char* nptr
, char** endptr
, int base
)
1010 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1012 #ifndef wxCRT_StrtoullW
1013 wxULongLong_t
wxCRT_StrtoullW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1014 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1017 // ----------------------------------------------------------------------------
1018 // functions which we may need even if !wxUSE_WCHAR_T
1019 // ----------------------------------------------------------------------------
1021 template<typename T
>
1022 static T
*wxCRT_DoStrtok(T
*psz
, const T
*delim
, T
**save_ptr
)
1031 psz
+= wxStrspn(psz
, delim
);
1034 *save_ptr
= (T
*)NULL
;
1039 psz
= wxStrpbrk(psz
, delim
);
1042 *save_ptr
= (T
*)NULL
;
1047 *save_ptr
= psz
+ 1;
1053 #ifndef wxCRT_StrtokA
1054 char *wxCRT_StrtokA(char *psz
, const char *delim
, char **save_ptr
)
1055 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1057 #ifndef wxCRT_StrtokW
1058 wchar_t *wxCRT_StrtokW(wchar_t *psz
, const wchar_t *delim
, wchar_t **save_ptr
)
1059 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1062 // ----------------------------------------------------------------------------
1063 // missing C RTL functions
1064 // ----------------------------------------------------------------------------
1066 #ifdef wxNEED_STRDUP
1068 char *strdup(const char *s
)
1070 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1072 strcpy( dest
, s
) ;
1075 #endif // wxNEED_STRDUP
1077 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1079 void *calloc( size_t num
, size_t size
)
1081 void** ptr
= (void **)malloc(num
* size
);
1082 memset( ptr
, 0, num
* size
);
1086 #endif // __WXWINCE__ <= 211
1089 int wxCRT_RemoveW(const wchar_t *path
)
1091 return ::DeleteFile(path
) == 0;
1095 #ifndef wxCRT_TmpnamW
1096 wchar_t *wxCRT_TmpnamW(wchar_t *s
)
1098 // tmpnam_r() returns NULL if s=NULL, do the same
1099 wxCHECK_MSG( s
, NULL
, "wxTmpnam must be called with a buffer" );
1102 #define L_tmpnam 1024
1104 wxCharBuffer
buf(L_tmpnam
);
1107 wxConvLibc
.ToWChar(s
, L_tmpnam
+1, buf
.data());
1110 #endif // !wxCRT_TmpnamW
1113 // ============================================================================
1115 // ============================================================================
1117 #if wxUSE_UNICODE_UTF8
1119 #if !wxUSE_UTF8_LOCALE_ONLY
1120 bool wxLocaleIsUtf8
= false; // the safer setting if not known
1123 static bool wxIsLocaleUtf8()
1125 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1126 // because a) it may be unavailable in some builds and b) has slightly
1127 // different semantics (default locale instead of current)
1129 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1130 // GNU libc provides current character set this way (this conforms to
1132 const char *charset
= nl_langinfo(CODESET
);
1135 // "UTF-8" is used by modern glibc versions, but test other variants
1136 // as well, just in case:
1137 if ( strcmp(charset
, "UTF-8") == 0 ||
1138 strcmp(charset
, "utf-8") == 0 ||
1139 strcmp(charset
, "UTF8") == 0 ||
1140 strcmp(charset
, "utf8") == 0 )
1147 // check if we're running under the "C" locale: it is 7bit subset
1148 // of UTF-8, so it can be safely used with the UTF-8 build:
1149 const char *lc_ctype
= setlocale(LC_CTYPE
, NULL
);
1151 (strcmp(lc_ctype
, "C") == 0 || strcmp(lc_ctype
, "POSIX") == 0) )
1156 // we don't know what charset libc is using, so assume the worst
1161 void wxUpdateLocaleIsUtf8()
1163 #if wxUSE_UTF8_LOCALE_ONLY
1164 if ( !wxIsLocaleUtf8() )
1166 wxLogFatalError(_T("This program requires UTF-8 locale to run."));
1168 #else // !wxUSE_UTF8_LOCALE_ONLY
1169 wxLocaleIsUtf8
= wxIsLocaleUtf8();
1173 #endif // wxUSE_UNICODE_UTF8
1175 // ============================================================================
1176 // wx wrappers for CRT functions
1177 // ============================================================================
1179 #if wxUSE_UNICODE_WCHAR
1180 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW
1181 #elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
1182 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \
1183 return_kw wxLocaleIsUtf8 ? callA : callW
1184 #else // ANSI or UTF8 only
1185 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA
1188 int wxPuts(const wxString
& s
)
1190 CALL_ANSI_OR_UNICODE(return,
1191 wxCRT_PutsA(s
.mb_str()),
1192 wxCRT_PutsW(s
.wc_str()));
1195 int wxFputs(const wxString
& s
, FILE *stream
)
1197 CALL_ANSI_OR_UNICODE(return,
1198 wxCRT_FputsA(s
.mb_str(), stream
),
1199 wxCRT_FputsW(s
.wc_str(), stream
));
1202 int wxFputc(const wxUniChar
& c
, FILE *stream
)
1204 #if !wxUSE_UNICODE // FIXME-UTF8: temporary, remove this with ANSI build
1205 return wxCRT_FputcA((char)c
, stream
);
1207 CALL_ANSI_OR_UNICODE(return,
1208 wxCRT_FputsA(c
.AsUTF8(), stream
),
1209 wxCRT_FputcW((wchar_t)c
, stream
));
1213 void wxPerror(const wxString
& s
)
1215 #ifdef wxCRT_PerrorW
1216 CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE
,
1217 wxCRT_PerrorA(s
.mb_str()),
1218 wxCRT_PerrorW(s
.wc_str()));
1220 wxCRT_PerrorA(s
.mb_str());
1224 wchar_t *wxFgets(wchar_t *s
, int size
, FILE *stream
)
1226 wxCHECK_MSG( s
, NULL
, "empty buffer passed to wxFgets()" );
1228 wxCharBuffer
buf(size
- 1);
1229 // FIXME: this reads too little data if wxConvLibc uses UTF-8 ('size' wide
1230 // characters may be encoded by up to 'size'*4 bytes), but what
1232 if ( wxFgets(buf
.data(), size
, stream
) == NULL
)
1235 if ( wxConvLibc
.ToWChar(s
, size
, buf
, wxNO_LEN
) == wxCONV_FAILED
)
1241 // ----------------------------------------------------------------------------
1242 // wxScanf() and friends
1243 // ----------------------------------------------------------------------------
1246 int wxVsscanf(const char *str
, const char *format
, va_list ap
)
1247 { return wxCRT_VsscanfA(str
, format
, ap
); }
1248 int wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
)
1249 { return wxCRT_VsscanfW(str
, format
, ap
); }
1250 int wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
)
1251 { return wxCRT_VsscanfA(str
, format
, ap
); }
1252 int wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
)
1253 { return wxCRT_VsscanfW(str
, format
, ap
); }
1254 int wxVsscanf(const wxString
& str
, const char *format
, va_list ap
)
1255 { return wxCRT_VsscanfA(str
.mb_str(), format
, ap
); }
1256 int wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
)
1257 { return wxCRT_VsscanfW(str
.wc_str(), format
, ap
); }
1258 int wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
)
1259 { return wxCRT_VsscanfA(str
.AsCharBuf(), format
, ap
); }
1260 int wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
)
1261 { return wxCRT_VsscanfW(str
.AsWCharBuf(), format
, ap
); }
1262 #endif // !__VISUALC__