]>
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"
24 #include "wx/strconv.h" // wxMBConv::cWC2MB()
26 #define _ISOC9X_SOURCE 1 // to get vsscanf()
27 #define _BSD_SOURCE 1 // to still get strdup()
35 // wide character functions are declared in std namespace under IRIX
38 // and this one is only declared if __c99 is defined which is not the case
39 // for C++ builds, so declare it ourselves
40 extern "C" int vswscanf(const wchar_t *, const wchar_t *, va_list);
48 #include "wx/msw/wince/time.h"
50 #endif // !__WXPALMOS5__
53 #include "wx/string.h"
55 #include "wx/utils.h" // for wxMin and wxMax
59 #ifdef HAVE_LANGINFO_H
64 // there is no errno.h under CE apparently
65 #define wxSET_ERRNO(value)
69 #define wxSET_ERRNO(value) errno = value
72 #if defined(__MWERKS__) && __MSL__ >= 0x6000
77 #if defined(__DARWIN__)
78 #include "wx/osx/core/cfref.h"
79 #include <CoreFoundation/CFLocale.h>
80 #include "wx/osx/core/cfstring.h"
84 WXDLLIMPEXP_BASE
size_t wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
86 // assume that we have mbsrtowcs() too if we have wcsrtombs()
89 memset(&mbstate
, 0, sizeof(mbstate_t));
94 if (n
) *buf
= wxT('\0');
98 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
100 return wxMbstowcs(buf
, psz
, n
);
104 // note that we rely on common (and required by Unix98 but unfortunately not
105 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
106 // to just get the size of the needed buffer -- this is needed as otherwise
107 // we have no idea about how much space we need and if the CRT doesn't
108 // support it (the only currently known example being Metrowerks, see
109 // wx/crt.h) we don't use its mbstowcs() at all
110 #ifdef HAVE_WCSRTOMBS
111 return mbsrtowcs(NULL
, &psz
, 0, &mbstate
);
113 return wxMbstowcs(NULL
, psz
, 0);
117 WXDLLIMPEXP_BASE
size_t wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
119 #ifdef HAVE_WCSRTOMBS
121 memset(&mbstate
, 0, sizeof(mbstate_t));
126 // glibc2.1 chokes on null input
130 #ifdef HAVE_WCSRTOMBS
131 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
133 return wxWcstombs(buf
, pwz
, n
);
137 #ifdef HAVE_WCSRTOMBS
138 return wcsrtombs(NULL
, &pwz
, 0, &mbstate
);
140 return wxWcstombs(NULL
, pwz
, 0);
144 char* wxSetlocale(int category
, const char *locale
)
147 // FIXME-CE: there is no setlocale() in CE CRT, use SetThreadLocale()?
148 wxUnusedVar(category
);
152 #else // !__WXWINCE__
155 if ( locale
!= NULL
&& locale
[0] == 0 )
157 // the attempt to use newlocale(LC_ALL_MASK, "", NULL);
158 // here in order to deduce the language along the environment vars rules
159 // lead to strange crashes later...
161 // we have to emulate the behaviour under OS X
162 wxCFRef
<CFLocaleRef
> userLocaleRef(CFLocaleCopyCurrent());
163 wxCFStringRef
str(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleLanguageCode
)));
164 wxString langFull
= str
.AsString()+"_";
165 str
.reset(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleCountryCode
)));
166 langFull
+= str
.AsString();
167 rv
= setlocale(category
, langFull
.c_str());
170 rv
= setlocale(category
, locale
);
172 char *rv
= setlocale(category
, locale
);
174 if ( locale
!= NULL
/* setting locale, not querying */ &&
175 rv
/* call was successful */ )
177 wxUpdateLocaleIsUtf8();
180 #endif // __WXWINCE__/!__WXWINCE__
183 // ============================================================================
184 // printf() functions business
185 // ============================================================================
187 // special test mode: define all functions below even if we don't really need
188 // them to be able to test them
198 #define wxNEED_WPRINTF
200 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
);
204 /* Digital Mars adds count to _stprintf (C99) so convert */
205 int wxCRT_SprintfW (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
209 va_start( arglist
, format
);
210 int iLen
= swprintf ( s
, -1, format
, arglist
);
216 // ----------------------------------------------------------------------------
217 // implement the standard IO functions for wide char if libc doesn't have them
218 // ----------------------------------------------------------------------------
221 int wxCRT_FputsW(const wchar_t *ws
, FILE *stream
)
223 wxCharBuffer
buf(wxConvLibc
.cWC2MB(ws
));
227 // counting the number of wide characters written isn't worth the trouble,
228 // simply distinguish between ok and error
229 return wxCRT_FputsA(buf
, stream
) == -1 ? -1 : 0;
231 #endif // !wxCRT_FputsW
234 int wxCRT_PutsW(const wchar_t *ws
)
236 int rc
= wxCRT_FputsW(ws
, stdout
);
239 if ( wxCRT_FputsW(L
"\n", stdout
) == -1 )
247 #endif // !wxCRT_PutsW
250 int /* not wint_t */ wxCRT_FputcW(wchar_t wc
, FILE *stream
)
252 wchar_t ws
[2] = { wc
, L
'\0' };
254 return wxCRT_FputsW(ws
, stream
);
256 #endif // !wxCRT_FputcW
258 // NB: we only implement va_list functions here, the ones taking ... are
259 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
260 // the definitions there to avoid duplicating them here
261 #ifdef wxNEED_WPRINTF
263 // TODO: implement the scanf() functions
264 static int vwscanf(const wchar_t *format
, va_list argptr
)
266 wxFAIL_MSG( wxT("TODO") );
271 static int vfwscanf(FILE *stream
, const wchar_t *format
, va_list argptr
)
273 wxFAIL_MSG( wxT("TODO") );
278 #define vswprintf wxCRT_VsnprintfW
280 static int vfwprintf(FILE *stream
, const wchar_t *format
, va_list argptr
)
283 int rc
= s
.PrintfV(format
, argptr
);
287 // we can't do much better without Unicode support in libc...
288 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
295 static int vwprintf(const wchar_t *format
, va_list argptr
)
297 return wxCRT_VfprintfW(stdout
, format
, argptr
);
300 #endif // wxNEED_WPRINTF
302 #ifdef wxNEED_VSWSCANF
303 static int vswscanf(const wchar_t *ws
, const wchar_t *format
, va_list argptr
)
305 // The best we can do without proper Unicode support in glibc is to
306 // convert the strings into MB representation and run ANSI version
307 // of the function. This doesn't work with %c and %s because of difference
308 // in size of char and wchar_t, though.
310 wxCHECK_MSG( wxStrstr(format
, wxT("%s")) == NULL
, -1,
311 wxT("incomplete vswscanf implementation doesn't allow %s") );
312 wxCHECK_MSG( wxStrstr(format
, wxT("%c")) == NULL
, -1,
313 wxT("incomplete vswscanf implementation doesn't allow %c") );
315 return vsscanf(static_cast<const char*>(wxConvLibc
.cWX2MB(ws
)),
316 wxConvLibc
.cWX2MB(format
), argptr
);
320 // ----------------------------------------------------------------------------
321 // wxPrintf(), wxScanf() and relatives
322 // ----------------------------------------------------------------------------
324 // FIXME-UTF8: do format conversion using (modified) wxFormatConverter in
325 // template wrappers, not here; note that it will needed to
326 // translate all forms of string specifiers to %(l)s for wxPrintf(),
327 // but it only should do what it did in 2.8 for wxScanf()!
329 #ifndef wxCRT_PrintfW
330 int wxCRT_PrintfW( const wchar_t *format
, ... )
333 va_start(argptr
, format
);
335 int ret
= vwprintf( format
, argptr
);
343 #ifndef wxCRT_FprintfW
344 int wxCRT_FprintfW( FILE *stream
, const wchar_t *format
, ... )
347 va_start( argptr
, format
);
349 int ret
= vfwprintf( stream
, format
, argptr
);
357 #ifndef wxCRT_VfprintfW
358 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
)
360 return vfwprintf( stream
, format
, argptr
);
364 #ifndef wxCRT_VprintfW
365 int wxCRT_VprintfW( const wchar_t *format
, va_list argptr
)
367 return vwprintf( format
, argptr
);
371 #ifndef wxCRT_VsprintfW
372 int wxCRT_VsprintfW( wchar_t *str
, const wchar_t *format
, va_list argptr
)
374 // same as for wxSprintf()
375 return vswprintf(str
, INT_MAX
/ 4, format
, argptr
);
380 int wxCRT_ScanfW(const wchar_t *format
, ...)
383 va_start(argptr
, format
);
386 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
387 int ret
= std::vwscanf(format
, argptr
);
389 int ret
= vwscanf(format
, argptr
);
392 int ret
= vwscanf(format
, argptr
);
401 #ifndef wxCRT_SscanfW
402 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...)
405 va_start(argptr
, format
);
408 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
409 int ret
= std::vswscanf(str
, format
, argptr
);
411 int ret
= vswscanf(str
, format
, argptr
);
414 int ret
= vswscanf(str
, format
, argptr
);
423 #ifndef wxCRT_FscanfW
424 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...)
427 va_start(argptr
, format
);
429 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
430 int ret
= std::vfwscanf(stream
, format
, argptr
);
432 int ret
= vfwscanf(stream
, format
, argptr
);
435 int ret
= vfwscanf(stream
, format
, argptr
);
444 #ifndef wxCRT_VsscanfW
445 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list argptr
)
448 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
449 return std::vswscanf(str
, format
, argptr
);
451 return vswscanf(str
, format
, argptr
);
454 return vswscanf(str
, format
, argptr
);
460 // ----------------------------------------------------------------------------
461 // wrappers to printf and scanf function families
462 // ----------------------------------------------------------------------------
464 #if !wxUSE_UTF8_LOCALE_ONLY
465 int wxDoSprintfWchar(char *str
, const wxChar
*format
, ...)
468 va_start(argptr
, format
);
470 int rv
= wxVsprintf(str
, format
, argptr
);
475 #endif // !wxUSE_UTF8_LOCALE_ONLY
477 #if wxUSE_UNICODE_UTF8
478 int wxDoSprintfUtf8(char *str
, const char *format
, ...)
481 va_start(argptr
, format
);
483 int rv
= wxVsprintf(str
, format
, argptr
);
488 #endif // wxUSE_UNICODE_UTF8
492 #if !wxUSE_UTF8_LOCALE_ONLY
493 int wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...)
496 va_start(argptr
, format
);
498 int rv
= wxVsprintf(str
, format
, argptr
);
503 #endif // !wxUSE_UTF8_LOCALE_ONLY
505 #if wxUSE_UNICODE_UTF8
506 int wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...)
509 va_start(argptr
, format
);
511 int rv
= wxVsprintf(str
, format
, argptr
);
516 #endif // wxUSE_UNICODE_UTF8
518 #endif // wxUSE_UNICODE
520 #if !wxUSE_UTF8_LOCALE_ONLY
521 int wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...)
524 va_start(argptr
, format
);
526 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
531 #endif // !wxUSE_UTF8_LOCALE_ONLY
533 #if wxUSE_UNICODE_UTF8
534 int wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...)
537 va_start(argptr
, format
);
539 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
544 #endif // wxUSE_UNICODE_UTF8
548 #if !wxUSE_UTF8_LOCALE_ONLY
549 int wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...)
552 va_start(argptr
, format
);
554 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
559 #endif // !wxUSE_UTF8_LOCALE_ONLY
561 #if wxUSE_UNICODE_UTF8
562 int wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...)
565 va_start(argptr
, format
);
567 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
572 #endif // wxUSE_UNICODE_UTF8
574 #endif // wxUSE_UNICODE
577 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
578 #define vsnprintf wx_fixed_vsnprintf
586 #if !wxUSE_UTF8_LOCALE_ONLY
587 int ConvertStringToBuf(const wxString
& s
, char *out
, size_t outsize
)
589 const wxCharBuffer
buf(s
.mb_str());
591 const size_t len
= buf
.length();
594 memcpy(out
, buf
, (len
+1) * sizeof(char));
596 else // not enough space
598 memcpy(out
, buf
, (outsize
-1) * sizeof(char));
599 out
[outsize
-1] = '\0';
604 #endif // !wxUSE_UTF8_LOCALE_ONLY
606 #if wxUSE_UNICODE_UTF8
607 int ConvertStringToBuf(const wxString
& s
, wchar_t *out
, size_t outsize
)
609 const wxWX2WCbuf
buf(s
.wc_str());
610 size_t len
= s
.length(); // same as buf length for wchar_t*
613 memcpy(out
, buf
, (len
+1) * sizeof(wchar_t));
615 else // not enough space
617 memcpy(out
, buf
, (outsize
-1) * sizeof(wchar_t));
622 #endif // wxUSE_UNICODE_UTF8
624 } // anonymous namespace
627 static size_t PrintfViaString(T
*out
, size_t outsize
,
628 const wxString
& format
, va_list argptr
)
631 s
.PrintfV(format
, argptr
);
633 return ConvertStringToBuf(s
, out
, outsize
);
635 #endif // wxUSE_UNICODE
637 int wxVsprintf(char *str
, const wxString
& format
, va_list argptr
)
639 #if wxUSE_UTF8_LOCALE_ONLY
640 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
642 #if wxUSE_UNICODE_UTF8
643 if ( wxLocaleIsUtf8
)
644 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
648 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
650 return wxCRT_VsprintfA(str
, format
.mb_str(), argptr
);
656 int wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
)
658 #if wxUSE_UNICODE_WCHAR
661 This fails with a bug similar to
662 http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=c++.beta&artnum=680
664 I don't see it being used in the wxWidgets sources at present (oct 2007) CE
666 #pragma message ( "warning ::::: wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) not yet implemented" )
667 wxFAIL_MSG( wxT("TODO") );
671 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
673 #else // wxUSE_UNICODE_UTF8
674 #if !wxUSE_UTF8_LOCALE_ONLY
675 if ( !wxLocaleIsUtf8
)
676 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
679 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
680 #endif // wxUSE_UNICODE_UTF8
682 #endif // wxUSE_UNICODE
684 int wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
)
687 #if wxUSE_UTF8_LOCALE_ONLY
688 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
690 #if wxUSE_UNICODE_UTF8
691 if ( wxLocaleIsUtf8
)
692 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
697 // NB: if this code is called, then wxString::PrintV() would use the
698 // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
700 rv
= PrintfViaString(str
, size
, format
, argptr
);
703 rv
= wxCRT_VsnprintfA(str
, size
, format
.mb_str(), argptr
);
707 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
708 // doesn't nul terminate on truncation.
715 int wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
)
719 #if wxUSE_UNICODE_WCHAR
720 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
721 #else // wxUSE_UNICODE_UTF8
722 #if !wxUSE_UTF8_LOCALE_ONLY
723 if ( !wxLocaleIsUtf8
)
724 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
728 // NB: if this code is called, then wxString::PrintV() would use the
729 // char* version of wxVsnprintf(), so it's safe to use PrintV()
731 rv
= PrintfViaString(str
, size
, format
, argptr
);
733 #endif // wxUSE_UNICODE_UTF8
735 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
736 // doesn't nul terminate on truncation.
741 #endif // wxUSE_UNICODE
744 // ----------------------------------------------------------------------------
745 // ctype.h stuff (currently unused)
746 // ----------------------------------------------------------------------------
748 #ifdef wxNEED_WX_MBSTOWCS
750 WXDLLIMPEXP_BASE
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
760 const char* origin
= in
;
762 while (outlen
-- && *in
)
764 *out
++ = (wchar_t) *in
++;
772 WXDLLIMPEXP_BASE
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
782 const wchar_t* origin
= in
;
784 while (outlen
-- && *in
)
786 *out
++ = (char) *in
++;
794 #endif // wxNEED_WX_MBSTOWCS
796 #ifndef wxCRT_StrdupA
797 WXDLLIMPEXP_BASE
char *wxCRT_StrdupA(const char *s
)
799 return strcpy((char *)malloc(strlen(s
) + 1), s
);
801 #endif // wxCRT_StrdupA
803 #ifndef wxCRT_StrdupW
804 WXDLLIMPEXP_BASE
wchar_t * wxCRT_StrdupW(const wchar_t *pwz
)
806 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
807 wchar_t *ret
= (wchar_t *) malloc(size
);
808 memcpy(ret
, pwz
, size
);
811 #endif // wxCRT_StrdupW
813 #ifndef wxWCHAR_T_IS_WXCHAR16
814 size_t wxStrlen(const wxChar16
*s
)
818 while (*s
!=0) { ++i
; ++s
; };
822 wxChar16
* wxStrdup(const wxChar16
* s
)
824 size_t size
= (wxStrlen(s
) + 1) * sizeof(wxChar16
);
825 wxChar16
*ret
= (wxChar16
*) malloc(size
);
826 memcpy(ret
, s
, size
);
831 #ifndef wxWCHAR_T_IS_WXCHAR32
832 size_t wxStrlen(const wxChar32
*s
)
836 while (*s
!=0) { ++i
; ++s
; };
840 wxChar32
* wxStrdup(const wxChar32
* s
)
842 size_t size
= (wxStrlen(s
) + 1) * sizeof(wxChar32
);
843 wxChar32
*ret
= (wxChar32
*) malloc(size
);
844 memcpy(ret
, s
, size
);
849 #ifndef wxCRT_StricmpA
850 WXDLLIMPEXP_BASE
int wxCRT_StricmpA(const char *psz1
, const char *psz2
)
852 register char c1
, c2
;
854 c1
= wxTolower(*psz1
++);
855 c2
= wxTolower(*psz2
++);
856 } while ( c1
&& (c1
== c2
) );
859 #endif // !defined(wxCRT_StricmpA)
861 #ifndef wxCRT_StricmpW
862 WXDLLIMPEXP_BASE
int wxCRT_StricmpW(const wchar_t *psz1
, const wchar_t *psz2
)
864 register wchar_t c1
, c2
;
866 c1
= wxTolower(*psz1
++);
867 c2
= wxTolower(*psz2
++);
868 } while ( c1
&& (c1
== c2
) );
871 #endif // !defined(wxCRT_StricmpW)
873 #ifndef wxCRT_StrnicmpA
874 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpA(const char *s1
, const char *s2
, size_t n
)
876 // initialize the variables just to suppress stupid gcc warning
877 register char c1
= 0, c2
= 0;
878 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
880 if (c1
< c2
) return -1;
881 if (c1
> c2
) return 1;
885 #endif // !defined(wxCRT_StrnicmpA)
887 #ifndef wxCRT_StrnicmpW
888 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpW(const wchar_t *s1
, const wchar_t *s2
, size_t n
)
890 // initialize the variables just to suppress stupid gcc warning
891 register wchar_t c1
= 0, c2
= 0;
892 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
894 if (c1
< c2
) return -1;
895 if (c1
> c2
) return 1;
899 #endif // !defined(wxCRT_StrnicmpW)
901 // ----------------------------------------------------------------------------
902 // string.h functions
903 // ----------------------------------------------------------------------------
905 // this (and wxCRT_StrncmpW below) are extern "C" because they are needed
906 // by regex code, the rest isn't needed, so it's not declared as extern "C"
907 #ifndef wxCRT_StrlenW
908 extern "C" WXDLLIMPEXP_BASE
size_t wxCRT_StrlenW(const wchar_t *s
)
918 // ----------------------------------------------------------------------------
919 // stdlib.h functions
920 // ----------------------------------------------------------------------------
922 #ifndef wxCRT_GetenvW
923 WXDLLIMPEXP_BASE
wchar_t* wxCRT_GetenvW(const wchar_t *name
)
925 // NB: buffer returned by getenv() is allowed to be overwritten next
926 // time getenv() is called, so it is OK to use static string
927 // buffer to hold the data.
928 static wxWCharBuffer value
;
929 value
= wxConvLibc
.cMB2WC(getenv(wxConvLibc
.cWC2MB(name
)));
932 #endif // !wxCRT_GetenvW
934 #ifndef wxCRT_StrftimeW
935 WXDLLIMPEXP_BASE
size_t
936 wxCRT_StrftimeW(wchar_t *s
, size_t maxsize
, const wchar_t *fmt
, const struct tm
*tm
)
941 wxCharBuffer
buf(maxsize
);
943 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
947 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
951 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
955 wxCRT_StrncpyW(s
, wbuf
, maxsize
);
956 return wxCRT_StrlenW(s
);
958 #endif // !wxCRT_StrftimeW
963 wxCRT_StrtoullBase(const T
* nptr
, T
** endptr
, int base
, T
* sign
)
965 wxULongLong_t sum
= 0;
966 wxString
wxstr(nptr
);
967 wxString::const_iterator i
= wxstr
.begin();
968 wxString::const_iterator end
= wxstr
.end();
971 while ( i
!= end
&& wxIsspace(*i
) ) ++i
;
978 if ( c
== wxT('+') || c
== wxT('-') )
986 if ( i
!= end
&& *i
== wxT('0') )
991 if ( *i
== wxT('x') && (base
== 16 || base
== 0) )
1000 wxSET_ERRNO(EINVAL
);
1011 for ( ; i
!= end
; ++i
)
1021 n
= wxTolower(c
) - wxT('a') + 10;
1026 if ( n
>= (unsigned int)base
)
1027 // Invalid character (for this base)
1030 wxULongLong_t prevsum
= sum
;
1031 sum
= (sum
* base
) + n
;
1033 if ( sum
< prevsum
)
1035 wxSET_ERRNO(ERANGE
);
1042 *endptr
= (T
*)(nptr
+ (i
- wxstr
.begin()));
1048 template<typename T
>
1049 static wxULongLong_t
wxCRT_DoStrtoull(const T
* nptr
, T
** endptr
, int base
)
1052 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1054 if ( sign
== wxT('-') )
1056 wxSET_ERRNO(ERANGE
);
1063 template<typename T
>
1064 static wxLongLong_t
wxCRT_DoStrtoll(const T
* nptr
, T
** endptr
, int base
)
1067 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1068 wxLongLong_t val
= 0;
1070 if ( sign
== wxT('-') )
1072 if (uval
<= (wxULongLong_t
)wxINT64_MAX
+ 1)
1074 val
= -(wxLongLong_t
)uval
;
1078 wxSET_ERRNO(ERANGE
);
1081 else if ( uval
<= wxINT64_MAX
)
1087 wxSET_ERRNO(ERANGE
);
1093 #ifndef wxCRT_StrtollA
1094 wxLongLong_t
wxCRT_StrtollA(const char* nptr
, char** endptr
, int base
)
1095 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1097 #ifndef wxCRT_StrtollW
1098 wxLongLong_t
wxCRT_StrtollW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1099 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1102 #ifndef wxCRT_StrtoullA
1103 wxULongLong_t
wxCRT_StrtoullA(const char* nptr
, char** endptr
, int base
)
1104 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1106 #ifndef wxCRT_StrtoullW
1107 wxULongLong_t
wxCRT_StrtoullW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1108 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1111 #endif // wxLongLong_t
1113 // ----------------------------------------------------------------------------
1114 // strtok() functions
1115 // ----------------------------------------------------------------------------
1117 template<typename T
>
1118 static T
*wxCRT_DoStrtok(T
*psz
, const T
*delim
, T
**save_ptr
)
1127 psz
+= wxStrspn(psz
, delim
);
1135 psz
= wxStrpbrk(psz
, delim
);
1143 *save_ptr
= psz
+ 1;
1149 #ifndef wxCRT_StrtokA
1150 char *wxCRT_StrtokA(char *psz
, const char *delim
, char **save_ptr
)
1151 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1153 #ifndef wxCRT_StrtokW
1154 wchar_t *wxCRT_StrtokW(wchar_t *psz
, const wchar_t *delim
, wchar_t **save_ptr
)
1155 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1158 // ----------------------------------------------------------------------------
1159 // missing C RTL functions
1160 // ----------------------------------------------------------------------------
1162 #ifdef wxNEED_STRDUP
1164 char *strdup(const char *s
)
1166 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1168 strcpy( dest
, s
) ;
1171 #endif // wxNEED_STRDUP
1173 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1175 void *calloc( size_t num
, size_t size
)
1177 void** ptr
= (void **)malloc(num
* size
);
1178 memset( ptr
, 0, num
* size
);
1182 #endif // __WXWINCE__ <= 211
1184 // ============================================================================
1186 // ============================================================================
1188 #if wxUSE_UNICODE_UTF8
1190 #if !wxUSE_UTF8_LOCALE_ONLY
1191 bool wxLocaleIsUtf8
= false; // the safer setting if not known
1194 static bool wxIsLocaleUtf8()
1196 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1197 // because a) it may be unavailable in some builds and b) has slightly
1198 // different semantics (default locale instead of current)
1200 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1201 // GNU libc provides current character set this way (this conforms to
1203 const char *charset
= nl_langinfo(CODESET
);
1206 // "UTF-8" is used by modern glibc versions, but test other variants
1207 // as well, just in case:
1208 if ( strcmp(charset
, "UTF-8") == 0 ||
1209 strcmp(charset
, "utf-8") == 0 ||
1210 strcmp(charset
, "UTF8") == 0 ||
1211 strcmp(charset
, "utf8") == 0 )
1216 #endif // HAVE_LANGINFO_H
1218 // check if we're running under the "C" locale: it is 7bit subset
1219 // of UTF-8, so it can be safely used with the UTF-8 build:
1220 const char *lc_ctype
= setlocale(LC_CTYPE
, NULL
);
1222 (strcmp(lc_ctype
, "C") == 0 || strcmp(lc_ctype
, "POSIX") == 0) )
1227 // we don't know what charset libc is using, so assume the worst
1232 void wxUpdateLocaleIsUtf8()
1234 #if wxUSE_UTF8_LOCALE_ONLY
1235 if ( !wxIsLocaleUtf8() )
1237 wxLogFatalError(wxT("This program requires UTF-8 locale to run."));
1239 #else // !wxUSE_UTF8_LOCALE_ONLY
1240 wxLocaleIsUtf8
= wxIsLocaleUtf8();
1244 #endif // wxUSE_UNICODE_UTF8
1246 // ============================================================================
1247 // wx wrappers for CRT functions
1248 // ============================================================================
1250 #if wxUSE_UNICODE_WCHAR
1251 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW
1252 #elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
1253 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \
1254 return_kw wxLocaleIsUtf8 ? callA : callW
1255 #else // ANSI or UTF8 only
1256 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA
1259 int wxPuts(const wxString
& s
)
1261 // under IRIX putws() takes a non-const argument so use wchar_str() instead
1263 CALL_ANSI_OR_UNICODE(return,
1264 wxCRT_PutsA(s
.mb_str()),
1265 wxCRT_PutsW(s
.wchar_str()));
1268 int wxFputs(const wxString
& s
, FILE *stream
)
1270 CALL_ANSI_OR_UNICODE(return,
1271 wxCRT_FputsA(s
.mb_str(), stream
),
1272 wxCRT_FputsW(s
.wc_str(), stream
));
1275 int wxFputc(const wxUniChar
& c
, FILE *stream
)
1277 #if !wxUSE_UNICODE // FIXME-UTF8: temporary, remove this with ANSI build
1278 return wxCRT_FputcA((char)c
, stream
);
1280 CALL_ANSI_OR_UNICODE(return,
1281 wxCRT_FputsA(c
.AsUTF8(), stream
),
1282 wxCRT_FputcW((wchar_t)c
, stream
));
1286 #ifdef wxCRT_PerrorA
1288 void wxPerror(const wxString
& s
)
1290 #ifdef wxCRT_PerrorW
1291 CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE
,
1292 wxCRT_PerrorA(s
.mb_str()),
1293 wxCRT_PerrorW(s
.wc_str()));
1295 wxCRT_PerrorA(s
.mb_str());
1299 #endif // wxCRT_PerrorA
1301 wchar_t *wxFgets(wchar_t *s
, int size
, FILE *stream
)
1303 wxCHECK_MSG( s
, NULL
, "empty buffer passed to wxFgets()" );
1305 wxCharBuffer
buf(size
- 1);
1306 // FIXME: this reads too little data if wxConvLibc uses UTF-8 ('size' wide
1307 // characters may be encoded by up to 'size'*4 bytes), but what
1309 if ( wxFgets(buf
.data(), size
, stream
) == NULL
)
1312 if ( wxConvLibc
.ToWChar(s
, size
, buf
, wxNO_LEN
) == wxCONV_FAILED
)
1318 // ----------------------------------------------------------------------------
1319 // wxScanf() and friends
1320 // ----------------------------------------------------------------------------
1322 #ifdef HAVE_VSSCANF // __VISUALC__ and __DMC__ see wx/crt.h
1323 int wxVsscanf(const char *str
, const char *format
, va_list ap
)
1324 { return wxCRT_VsscanfA(str
, format
, ap
); }
1325 int wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
)
1326 { return wxCRT_VsscanfW(str
, format
, ap
); }
1327 int wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
)
1328 { return wxCRT_VsscanfA(static_cast<const char*>(str
), format
, ap
); }
1329 int wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
)
1330 { return wxCRT_VsscanfW(str
, format
, ap
); }
1331 int wxVsscanf(const wxString
& str
, const char *format
, va_list ap
)
1332 { return wxCRT_VsscanfA(static_cast<const char*>(str
.mb_str()), format
, ap
); }
1333 int wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
)
1334 { return wxCRT_VsscanfW(str
.wc_str(), format
, ap
); }
1335 int wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
)
1336 { return wxCRT_VsscanfA(static_cast<const char*>(str
.AsCharBuf()), format
, ap
); }
1337 int wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
)
1338 { return wxCRT_VsscanfW(str
.AsWCharBuf(), format
, ap
); }
1339 #endif // HAVE_NO_VSSCANF