]>
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
46 #ifdef HAVE_LANGINFO_H
51 // there is no errno.h under CE apparently
52 #define wxSET_ERRNO(value)
56 #define wxSET_ERRNO(value) errno = value
59 #if defined(__MWERKS__) && __MSL__ >= 0x6000
64 #if defined(__DARWIN__)
65 #include "wx/mac/corefoundation/cfref.h"
66 #include <CoreFoundation/CFLocale.h>
67 #include "wx/mac/corefoundation/cfstring.h"
72 WXDLLIMPEXP_BASE
size_t wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
74 // assume that we have mbsrtowcs() too if we have wcsrtombs()
77 memset(&mbstate
, 0, sizeof(mbstate_t));
82 if (n
) *buf
= wxT('\0');
86 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
88 return wxMbstowcs(buf
, psz
, n
);
92 // note that we rely on common (and required by Unix98 but unfortunately not
93 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
94 // to just get the size of the needed buffer -- this is needed as otherwise
95 // we have no idea about how much space we need and if the CRT doesn't
96 // support it (the only currently known example being Metrowerks, see
97 // wx/crt.h) we don't use its mbstowcs() at all
99 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
101 return wxMbstowcs((wchar_t *) NULL
, psz
, 0);
105 WXDLLIMPEXP_BASE
size_t wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
107 #ifdef HAVE_WCSRTOMBS
109 memset(&mbstate
, 0, sizeof(mbstate_t));
114 // glibc2.1 chokes on null input
118 #ifdef HAVE_WCSRTOMBS
119 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
121 return wxWcstombs(buf
, pwz
, n
);
125 #ifdef HAVE_WCSRTOMBS
126 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
128 return wxWcstombs((char *) NULL
, pwz
, 0);
131 #endif // wxUSE_WCHAR_T
133 WXDLLIMPEXP_BASE
bool wxOKlibc()
135 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
136 // glibc 2.0 uses UTF-8 even when it shouldn't
138 if ((MB_CUR_MAX
== 2) &&
139 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
141 // this is UTF-8 allright, check whether that's what we want
142 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
143 if ((strlen(cur_locale
) < 4) ||
144 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
145 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
146 // nope, don't use libc conversion
154 char* wxSetlocale(int category
, const char *locale
)
157 // FIXME-CE: there is no setlocale() in CE CRT, use SetThreadLocale()?
158 wxUnusedVar(category
);
162 #else // !__WXWINCE__
165 if ( locale
!= NULL
&& locale
[0] == 0 )
167 locale_t lt
= newlocale(LC_ALL_MASK
, "", NULL
);
170 rv
= (char*) querylocale( LC_ALL_MASK
, lt
);
173 if ( rv
== NULL
|| rv
[0] == 0 || strcmp( rv
, "C" ) == 0 || strcmp( rv
, "POSIX" ) == 0 )
175 // we have to emulate the behaviour under OS X
176 wxCFRef
<CFLocaleRef
> userLocaleRef(CFLocaleCopyCurrent());
177 wxCFStringRef
str(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleLanguageCode
)));
178 wxString langFull
= str
.AsString()+"_";
179 str
.reset(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleCountryCode
)));
180 langFull
+= str
.AsString();
181 rv
= setlocale(category
, langFull
.c_str());
185 rv
= setlocale(category
, rv
);
189 rv
= setlocale(category
, locale
);
191 char *rv
= setlocale(category
, locale
);
193 if ( locale
!= NULL
/* setting locale, not querying */ &&
194 rv
/* call was successful */ )
196 wxUpdateLocaleIsUtf8();
199 #endif // __WXWINCE__/!__WXWINCE__
202 // ============================================================================
203 // printf() functions business
204 // ============================================================================
206 // special test mode: define all functions below even if we don't really need
207 // them to be able to test them
217 #define wxNEED_WPRINTF
219 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
);
223 /* Digital Mars adds count to _stprintf (C99) so convert */
224 int wxCRT_SprintfW (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
228 va_start( arglist
, format
);
229 int iLen
= swprintf ( s
, -1, format
, arglist
);
235 // ----------------------------------------------------------------------------
236 // implement the standard IO functions for wide char if libc doesn't have them
237 // ----------------------------------------------------------------------------
240 int wxCRT_FputsW(const wchar_t *ws
, FILE *stream
)
242 wxCharBuffer
buf(wxConvLibc
.cWC2MB(ws
));
246 // counting the number of wide characters written isn't worth the trouble,
247 // simply distinguish between ok and error
248 return wxCRT_FputsA(buf
, stream
) == -1 ? -1 : 0;
250 #endif // !wxCRT_FputsW
253 int wxCRT_PutsW(const wchar_t *ws
)
255 int rc
= wxCRT_FputsW(ws
, stdout
);
258 if ( wxCRT_FputsW(L
"\n", stdout
) == -1 )
266 #endif // !wxCRT_PutsW
269 int /* not wint_t */ wxCRT_FputcW(wchar_t wc
, FILE *stream
)
271 wchar_t ws
[2] = { wc
, L
'\0' };
273 return wxCRT_FputsW(ws
, stream
);
275 #endif // !wxCRT_FputcW
277 // NB: we only implement va_list functions here, the ones taking ... are
278 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
279 // the definitions there to avoid duplicating them here
280 #ifdef wxNEED_WPRINTF
282 // TODO: implement the scanf() functions
283 static int vwscanf(const wchar_t *format
, va_list argptr
)
285 wxFAIL_MSG( _T("TODO") );
290 static int vswscanf(const wchar_t *ws
, const wchar_t *format
, va_list argptr
)
292 // The best we can do without proper Unicode support in glibc is to
293 // convert the strings into MB representation and run ANSI version
294 // of the function. This doesn't work with %c and %s because of difference
295 // in size of char and wchar_t, though.
297 wxCHECK_MSG( wxStrstr(format
, _T("%s")) == NULL
, -1,
298 _T("incomplete vswscanf implementation doesn't allow %s") );
299 wxCHECK_MSG( wxStrstr(format
, _T("%c")) == NULL
, -1,
300 _T("incomplete vswscanf implementation doesn't allow %c") );
302 return vsscanf(wxConvLibc
.cWX2MB(ws
), wxConvLibc
.cWX2MB(format
), argptr
);
305 static int vfwscanf(FILE *stream
, const wchar_t *format
, va_list argptr
)
307 wxFAIL_MSG( _T("TODO") );
312 #define vswprintf wxCRT_VsnprintfW
314 static int vfwprintf(FILE *stream
, const wchar_t *format
, va_list argptr
)
317 int rc
= s
.PrintfV(format
, argptr
);
321 // we can't do much better without Unicode support in libc...
322 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
329 static int vwprintf(const wchar_t *format
, va_list argptr
)
331 return wxCRT_VfprintfW(stdout
, format
, argptr
);
334 #endif // wxNEED_WPRINTF
336 // ----------------------------------------------------------------------------
337 // wxPrintf(), wxScanf() and relatives
338 // ----------------------------------------------------------------------------
340 // FIXME-UTF8: do format conversion using (modified) wxFormatConverter in
341 // template wrappers, not here; note that it will needed to
342 // translate all forms of string specifiers to %(l)s for wxPrintf(),
343 // but it only should do what it did in 2.8 for wxScanf()!
345 #ifndef wxCRT_PrintfW
346 int wxCRT_PrintfW( const wchar_t *format
, ... )
349 va_start(argptr
, format
);
351 int ret
= vwprintf( format
, argptr
);
359 #ifndef wxCRT_FprintfW
360 int wxCRT_FprintfW( FILE *stream
, const wchar_t *format
, ... )
363 va_start( argptr
, format
);
365 int ret
= vfwprintf( stream
, format
, argptr
);
373 #ifndef wxCRT_VfprintfW
374 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
)
376 return vfwprintf( stream
, format
, argptr
);
380 #ifndef wxCRT_VprintfW
381 int wxCRT_VprintfW( const wchar_t *format
, va_list argptr
)
383 return vwprintf( format
, argptr
);
387 #ifndef wxCRT_VsprintfW
388 int wxCRT_VsprintfW( wchar_t *str
, const wchar_t *format
, va_list argptr
)
390 // same as for wxSprintf()
391 return vswprintf(str
, INT_MAX
/ 4, format
, argptr
);
396 int wxCRT_ScanfW(const wchar_t *format
, ...)
399 va_start(argptr
, format
);
402 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
403 int ret
= std::vwscanf(format
, argptr
);
405 int ret
= vwscanf(format
, argptr
);
408 int ret
= vwscanf(format
, argptr
);
417 #ifndef wxCRT_SscanfW
418 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...)
421 va_start(argptr
, format
);
424 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
425 int ret
= std::vswscanf(str
, format
, argptr
);
427 int ret
= vswscanf(str
, format
, argptr
);
430 int ret
= vswscanf(str
, format
, argptr
);
439 #ifndef wxCRT_FscanfW
440 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...)
443 va_start(argptr
, format
);
445 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
446 int ret
= std::vfwscanf(stream
, format
, argptr
);
448 int ret
= vfwscanf(stream
, format
, argptr
);
451 int ret
= vfwscanf(stream
, format
, argptr
);
460 #ifndef wxCRT_VsscanfW
461 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list argptr
)
464 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
465 return std::vswscanf(str
, format
, argptr
);
467 return vswscanf(str
, format
, argptr
);
470 return vswscanf(str
, format
, argptr
);
476 // ----------------------------------------------------------------------------
477 // wrappers to printf and scanf function families
478 // ----------------------------------------------------------------------------
480 #if !wxUSE_UTF8_LOCALE_ONLY
481 int wxDoSprintfWchar(char *str
, const wxChar
*format
, ...)
484 va_start(argptr
, format
);
486 int rv
= wxVsprintf(str
, format
, argptr
);
491 #endif // !wxUSE_UTF8_LOCALE_ONLY
493 #if wxUSE_UNICODE_UTF8
494 int wxDoSprintfUtf8(char *str
, const char *format
, ...)
497 va_start(argptr
, format
);
499 int rv
= wxVsprintf(str
, format
, argptr
);
504 #endif // wxUSE_UNICODE_UTF8
508 #if !wxUSE_UTF8_LOCALE_ONLY
509 int wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...)
512 va_start(argptr
, format
);
514 int rv
= wxVsprintf(str
, format
, argptr
);
519 #endif // !wxUSE_UTF8_LOCALE_ONLY
521 #if wxUSE_UNICODE_UTF8
522 int wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...)
525 va_start(argptr
, format
);
527 int rv
= wxVsprintf(str
, format
, argptr
);
532 #endif // wxUSE_UNICODE_UTF8
534 #endif // wxUSE_UNICODE
536 #if !wxUSE_UTF8_LOCALE_ONLY
537 int wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...)
540 va_start(argptr
, format
);
542 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
547 #endif // !wxUSE_UTF8_LOCALE_ONLY
549 #if wxUSE_UNICODE_UTF8
550 int wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...)
553 va_start(argptr
, format
);
555 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
560 #endif // wxUSE_UNICODE_UTF8
564 #if !wxUSE_UTF8_LOCALE_ONLY
565 int wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...)
568 va_start(argptr
, format
);
570 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
575 #endif // !wxUSE_UTF8_LOCALE_ONLY
577 #if wxUSE_UNICODE_UTF8
578 int wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...)
581 va_start(argptr
, format
);
583 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
588 #endif // wxUSE_UNICODE_UTF8
590 #endif // wxUSE_UNICODE
593 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
594 #define vsnprintf wx_fixed_vsnprintf
602 #if !wxUSE_UTF8_LOCALE_ONLY
603 int ConvertStringToBuf(const wxString
& s
, char *out
, size_t outsize
)
605 const wxWX2WCbuf buf
= s
.wc_str();
607 size_t len
= wxConvLibc
.FromWChar(out
, outsize
, buf
);
608 if ( len
!= wxCONV_FAILED
)
611 return wxConvLibc
.FromWChar(NULL
, 0, buf
);
613 #endif // !wxUSE_UTF8_LOCALE_ONLY
615 #if wxUSE_UNICODE_UTF8
616 int ConvertStringToBuf(const wxString
& s
, wchar_t *out
, size_t outsize
)
618 const wxWX2WCbuf
buf(s
.wc_str());
619 size_t len
= s
.length(); // same as buf length for wchar_t*
622 memcpy(out
, buf
, (len
+1) * sizeof(wchar_t));
624 else // not enough space
626 memcpy(out
, buf
, (outsize
-1) * sizeof(wchar_t));
631 #endif // wxUSE_UNICODE_UTF8
633 } // anonymous namespace
636 static size_t PrintfViaString(T
*out
, size_t outsize
,
637 const wxString
& format
, va_list argptr
)
640 s
.PrintfV(format
, argptr
);
642 return ConvertStringToBuf(s
, out
, outsize
);
644 #endif // wxUSE_UNICODE
646 int wxVsprintf(char *str
, const wxString
& format
, va_list argptr
)
648 #if wxUSE_UTF8_LOCALE_ONLY
649 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
651 #if wxUSE_UNICODE_UTF8
652 if ( wxLocaleIsUtf8
)
653 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
657 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
659 return wxCRT_VsprintfA(str
, format
.mb_str(), argptr
);
665 int wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
)
667 #if wxUSE_UNICODE_WCHAR
670 This fails with a bug similar to
671 http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=c++.beta&artnum=680
673 I don't see it being used in the wxWidgets sources at present (oct 2007) CE
675 #pragma message ( "warning ::::: wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) not yet implemented" )
676 wxFAIL_MSG( _T("TODO") );
680 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
682 #else // wxUSE_UNICODE_UTF8
683 #if !wxUSE_UTF8_LOCALE_ONLY
684 if ( !wxLocaleIsUtf8
)
685 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
688 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
689 #endif // wxUSE_UNICODE_UTF8
691 #endif // wxUSE_UNICODE
693 int wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
)
696 #if wxUSE_UTF8_LOCALE_ONLY
697 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
699 #if wxUSE_UNICODE_UTF8
700 if ( wxLocaleIsUtf8
)
701 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
706 // NB: if this code is called, then wxString::PrintV() would use the
707 // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
709 rv
= PrintfViaString(str
, size
, format
, argptr
);
712 rv
= wxCRT_VsnprintfA(str
, size
, format
.mb_str(), argptr
);
716 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
717 // doesn't nul terminate on truncation.
724 int wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
)
728 #if wxUSE_UNICODE_WCHAR
729 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
730 #else // wxUSE_UNICODE_UTF8
731 #if !wxUSE_UTF8_LOCALE_ONLY
732 if ( !wxLocaleIsUtf8
)
733 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
737 // NB: if this code is called, then wxString::PrintV() would use the
738 // char* version of wxVsnprintf(), so it's safe to use PrintV()
740 rv
= PrintfViaString(str
, size
, format
, argptr
);
742 #endif // wxUSE_UNICODE_UTF8
744 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
745 // doesn't nul terminate on truncation.
750 #endif // wxUSE_UNICODE
754 // ----------------------------------------------------------------------------
755 // ctype.h stuff (currently unused)
756 // ----------------------------------------------------------------------------
758 #ifdef wxNEED_WX_MBSTOWCS
760 WXDLLIMPEXP_BASE
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
770 const char* origin
= in
;
772 while (outlen
-- && *in
)
774 *out
++ = (wchar_t) *in
++;
782 WXDLLIMPEXP_BASE
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
792 const wchar_t* origin
= in
;
794 while (outlen
-- && *in
)
796 *out
++ = (char) *in
++;
804 #endif // wxNEED_WX_MBSTOWCS
806 #ifndef wxCRT_StrdupA
807 WXDLLIMPEXP_BASE
char *wxCRT_StrdupA(const char *s
)
809 return strcpy((char *)malloc(strlen(s
) + 1), s
);
811 #endif // wxCRT_StrdupA
813 #ifndef wxCRT_StrdupW
814 WXDLLIMPEXP_BASE
wchar_t * wxCRT_StrdupW(const wchar_t *pwz
)
816 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
817 wchar_t *ret
= (wchar_t *) malloc(size
);
818 memcpy(ret
, pwz
, size
);
821 #endif // wxCRT_StrdupW
823 #ifndef wxCRT_StricmpA
824 WXDLLIMPEXP_BASE
int wxCRT_StricmpA(const char *psz1
, const char *psz2
)
826 register char c1
, c2
;
828 c1
= wxTolower(*psz1
++);
829 c2
= wxTolower(*psz2
++);
830 } while ( c1
&& (c1
== c2
) );
833 #endif // !defined(wxCRT_StricmpA)
835 #ifndef wxCRT_StricmpW
836 WXDLLIMPEXP_BASE
int wxCRT_StricmpW(const wchar_t *psz1
, const wchar_t *psz2
)
838 register wchar_t c1
, c2
;
840 c1
= wxTolower(*psz1
++);
841 c2
= wxTolower(*psz2
++);
842 } while ( c1
&& (c1
== c2
) );
845 #endif // !defined(wxCRT_StricmpW)
847 #ifndef wxCRT_StrnicmpA
848 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpA(const char *s1
, const char *s2
, size_t n
)
850 // initialize the variables just to suppress stupid gcc warning
851 register char c1
= 0, c2
= 0;
852 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
854 if (c1
< c2
) return -1;
855 if (c1
> c2
) return 1;
859 #endif // !defined(wxCRT_StrnicmpA)
861 #ifndef wxCRT_StrnicmpW
862 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpW(const wchar_t *s1
, const wchar_t *s2
, size_t n
)
864 // initialize the variables just to suppress stupid gcc warning
865 register wchar_t c1
= 0, c2
= 0;
866 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
868 if (c1
< c2
) return -1;
869 if (c1
> c2
) return 1;
873 #endif // !defined(wxCRT_StrnicmpW)
875 // ----------------------------------------------------------------------------
876 // string.h functions
877 // ----------------------------------------------------------------------------
879 // this (and wxCRT_StrncmpW below) are extern "C" because they are needed
880 // by regex code, the rest isn't needed, so it's not declared as extern "C"
881 #ifndef wxCRT_StrlenW
882 extern "C" WXDLLIMPEXP_BASE
size_t wxCRT_StrlenW(const wchar_t *s
)
892 // ----------------------------------------------------------------------------
893 // stdlib.h functions
894 // ----------------------------------------------------------------------------
896 #ifndef wxCRT_GetenvW
897 WXDLLIMPEXP_BASE
wchar_t* wxCRT_GetenvW(const wchar_t *name
)
899 // NB: buffer returned by getenv() is allowed to be overwritten next
900 // time getenv() is called, so it is OK to use static string
901 // buffer to hold the data.
902 static wxWCharBuffer
value((wchar_t*)NULL
);
903 value
= wxConvLibc
.cMB2WC(getenv(wxConvLibc
.cWC2MB(name
)));
906 #endif // !wxCRT_GetenvW
908 #ifndef wxCRT_StrftimeW
909 WXDLLIMPEXP_BASE
size_t
910 wxCRT_StrftimeW(wchar_t *s
, size_t maxsize
, const wchar_t *fmt
, const struct tm
*tm
)
915 wxCharBuffer
buf(maxsize
);
917 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
921 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
925 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
929 wxCRT_StrncpyW(s
, wbuf
, maxsize
);
930 return wxCRT_StrlenW(s
);
932 #endif // !wxCRT_StrftimeW
934 #endif // wxUSE_WCHAR_T
939 wxCRT_StrtoullBase(const T
* nptr
, T
** endptr
, int base
, T
* sign
)
941 wxULongLong_t sum
= 0;
942 wxString
wxstr(nptr
);
943 wxString::const_iterator i
= wxstr
.begin();
944 wxString::const_iterator end
= wxstr
.end();
947 while ( i
!= end
&& wxIsspace(*i
) ) ++i
;
954 if ( c
== wxT('+') || c
== wxT('-') )
962 if ( i
!= end
&& *i
== wxT('0') )
967 if ( *i
== wxT('x') && (base
== 16 || base
== 0) )
987 for ( ; i
!= end
; ++i
)
997 n
= wxTolower(c
) - wxT('a') + 10;
1002 if ( n
>= (unsigned int)base
)
1003 // Invalid character (for this base)
1006 wxULongLong_t prevsum
= sum
;
1007 sum
= (sum
* base
) + n
;
1009 if ( sum
< prevsum
)
1011 wxSET_ERRNO(ERANGE
);
1018 *endptr
= (T
*)(nptr
+ (i
- wxstr
.begin()));
1024 template<typename T
>
1025 static wxULongLong_t
wxCRT_DoStrtoull(const T
* nptr
, T
** endptr
, int base
)
1028 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1030 if ( sign
== wxT('-') )
1032 wxSET_ERRNO(ERANGE
);
1039 template<typename T
>
1040 static wxLongLong_t
wxCRT_DoStrtoll(const T
* nptr
, T
** endptr
, int base
)
1043 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1044 wxLongLong_t val
= 0;
1046 if ( sign
== wxT('-') )
1048 if ( uval
<= wxULL(wxINT64_MAX
+1) )
1050 if ( uval
== wxULL(wxINT64_MAX
+1))
1051 val
= -((wxLongLong_t
)wxINT64_MAX
) - 1;
1053 val
= -((wxLongLong_t
)uval
);
1057 wxSET_ERRNO(ERANGE
);
1060 else if ( uval
<= wxINT64_MAX
)
1066 wxSET_ERRNO(ERANGE
);
1072 #ifndef wxCRT_StrtollA
1073 wxLongLong_t
wxCRT_StrtollA(const char* nptr
, char** endptr
, int base
)
1074 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1076 #ifndef wxCRT_StrtollW
1077 wxLongLong_t
wxCRT_StrtollW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1078 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1081 #ifndef wxCRT_StrtoullA
1082 wxULongLong_t
wxCRT_StrtoullA(const char* nptr
, char** endptr
, int base
)
1083 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1085 #ifndef wxCRT_StrtoullW
1086 wxULongLong_t
wxCRT_StrtoullW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1087 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1090 #endif // wxLongLong_t
1092 // ----------------------------------------------------------------------------
1093 // functions which we may need even if !wxUSE_WCHAR_T
1094 // ----------------------------------------------------------------------------
1096 template<typename T
>
1097 static T
*wxCRT_DoStrtok(T
*psz
, const T
*delim
, T
**save_ptr
)
1106 psz
+= wxStrspn(psz
, delim
);
1109 *save_ptr
= (T
*)NULL
;
1114 psz
= wxStrpbrk(psz
, delim
);
1117 *save_ptr
= (T
*)NULL
;
1122 *save_ptr
= psz
+ 1;
1128 #ifndef wxCRT_StrtokA
1129 char *wxCRT_StrtokA(char *psz
, const char *delim
, char **save_ptr
)
1130 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1132 #ifndef wxCRT_StrtokW
1133 wchar_t *wxCRT_StrtokW(wchar_t *psz
, const wchar_t *delim
, wchar_t **save_ptr
)
1134 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1137 // ----------------------------------------------------------------------------
1138 // missing C RTL functions
1139 // ----------------------------------------------------------------------------
1141 #ifdef wxNEED_STRDUP
1143 char *strdup(const char *s
)
1145 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1147 strcpy( dest
, s
) ;
1150 #endif // wxNEED_STRDUP
1152 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1154 void *calloc( size_t num
, size_t size
)
1156 void** ptr
= (void **)malloc(num
* size
);
1157 memset( ptr
, 0, num
* size
);
1161 #endif // __WXWINCE__ <= 211
1163 // ============================================================================
1165 // ============================================================================
1167 #if wxUSE_UNICODE_UTF8
1169 #if !wxUSE_UTF8_LOCALE_ONLY
1170 bool wxLocaleIsUtf8
= false; // the safer setting if not known
1173 static bool wxIsLocaleUtf8()
1175 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1176 // because a) it may be unavailable in some builds and b) has slightly
1177 // different semantics (default locale instead of current)
1179 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1180 // GNU libc provides current character set this way (this conforms to
1182 const char *charset
= nl_langinfo(CODESET
);
1185 // "UTF-8" is used by modern glibc versions, but test other variants
1186 // as well, just in case:
1187 if ( strcmp(charset
, "UTF-8") == 0 ||
1188 strcmp(charset
, "utf-8") == 0 ||
1189 strcmp(charset
, "UTF8") == 0 ||
1190 strcmp(charset
, "utf8") == 0 )
1195 #endif // HAVE_LANGINFO_H
1197 // check if we're running under the "C" locale: it is 7bit subset
1198 // of UTF-8, so it can be safely used with the UTF-8 build:
1199 const char *lc_ctype
= setlocale(LC_CTYPE
, NULL
);
1201 (strcmp(lc_ctype
, "C") == 0 || strcmp(lc_ctype
, "POSIX") == 0) )
1206 // we don't know what charset libc is using, so assume the worst
1211 void wxUpdateLocaleIsUtf8()
1213 #if wxUSE_UTF8_LOCALE_ONLY
1214 if ( !wxIsLocaleUtf8() )
1216 wxLogFatalError(_T("This program requires UTF-8 locale to run."));
1218 #else // !wxUSE_UTF8_LOCALE_ONLY
1219 wxLocaleIsUtf8
= wxIsLocaleUtf8();
1223 #endif // wxUSE_UNICODE_UTF8
1225 // ============================================================================
1226 // wx wrappers for CRT functions
1227 // ============================================================================
1229 #if wxUSE_UNICODE_WCHAR
1230 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW
1231 #elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
1232 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \
1233 return_kw wxLocaleIsUtf8 ? callA : callW
1234 #else // ANSI or UTF8 only
1235 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA
1238 int wxPuts(const wxString
& s
)
1240 CALL_ANSI_OR_UNICODE(return,
1241 wxCRT_PutsA(s
.mb_str()),
1242 wxCRT_PutsW(s
.wc_str()));
1245 int wxFputs(const wxString
& s
, FILE *stream
)
1247 CALL_ANSI_OR_UNICODE(return,
1248 wxCRT_FputsA(s
.mb_str(), stream
),
1249 wxCRT_FputsW(s
.wc_str(), stream
));
1252 int wxFputc(const wxUniChar
& c
, FILE *stream
)
1254 #if !wxUSE_UNICODE // FIXME-UTF8: temporary, remove this with ANSI build
1255 return wxCRT_FputcA((char)c
, stream
);
1257 CALL_ANSI_OR_UNICODE(return,
1258 wxCRT_FputsA(c
.AsUTF8(), stream
),
1259 wxCRT_FputcW((wchar_t)c
, stream
));
1263 #ifdef wxCRT_PerrorA
1265 void wxPerror(const wxString
& s
)
1267 #ifdef wxCRT_PerrorW
1268 CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE
,
1269 wxCRT_PerrorA(s
.mb_str()),
1270 wxCRT_PerrorW(s
.wc_str()));
1272 wxCRT_PerrorA(s
.mb_str());
1276 #endif // wxCRT_PerrorA
1278 wchar_t *wxFgets(wchar_t *s
, int size
, FILE *stream
)
1280 wxCHECK_MSG( s
, NULL
, "empty buffer passed to wxFgets()" );
1282 wxCharBuffer
buf(size
- 1);
1283 // FIXME: this reads too little data if wxConvLibc uses UTF-8 ('size' wide
1284 // characters may be encoded by up to 'size'*4 bytes), but what
1286 if ( wxFgets(buf
.data(), size
, stream
) == NULL
)
1289 if ( wxConvLibc
.ToWChar(s
, size
, buf
, wxNO_LEN
) == wxCONV_FAILED
)
1295 // ----------------------------------------------------------------------------
1296 // wxScanf() and friends
1297 // ----------------------------------------------------------------------------
1299 #ifndef HAVE_NO_VSSCANF // __VISUALC__ and __DMC__ see wx/crt.h
1300 int wxVsscanf(const char *str
, const char *format
, va_list ap
)
1301 { return wxCRT_VsscanfA(str
, format
, ap
); }
1302 int wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
)
1303 { return wxCRT_VsscanfW(str
, format
, ap
); }
1304 int wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
)
1305 { return wxCRT_VsscanfA(str
, format
, ap
); }
1306 int wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
)
1307 { return wxCRT_VsscanfW(str
, format
, ap
); }
1308 int wxVsscanf(const wxString
& str
, const char *format
, va_list ap
)
1309 { return wxCRT_VsscanfA(str
.mb_str(), format
, ap
); }
1310 int wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
)
1311 { return wxCRT_VsscanfW(str
.wc_str(), format
, ap
); }
1312 int wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
)
1313 { return wxCRT_VsscanfA(str
.AsCharBuf(), format
, ap
); }
1314 int wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
)
1315 { return wxCRT_VsscanfW(str
.AsWCharBuf(), format
, ap
); }
1316 #endif // HAVE_NO_VSSCANF