]>
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 wxWX2WCbuf buf
= s
.wc_str();
591 size_t len
= wxConvLibc
.FromWChar(out
, outsize
, buf
);
592 if ( len
!= wxCONV_FAILED
)
595 return wxConvLibc
.FromWChar(NULL
, 0, buf
);
597 #endif // !wxUSE_UTF8_LOCALE_ONLY
599 #if wxUSE_UNICODE_UTF8
600 int ConvertStringToBuf(const wxString
& s
, wchar_t *out
, size_t outsize
)
602 const wxWX2WCbuf
buf(s
.wc_str());
603 size_t len
= s
.length(); // same as buf length for wchar_t*
606 memcpy(out
, buf
, (len
+1) * sizeof(wchar_t));
608 else // not enough space
610 memcpy(out
, buf
, (outsize
-1) * sizeof(wchar_t));
615 #endif // wxUSE_UNICODE_UTF8
617 } // anonymous namespace
620 static size_t PrintfViaString(T
*out
, size_t outsize
,
621 const wxString
& format
, va_list argptr
)
624 s
.PrintfV(format
, argptr
);
626 return ConvertStringToBuf(s
, out
, outsize
);
628 #endif // wxUSE_UNICODE
630 int wxVsprintf(char *str
, const wxString
& format
, va_list argptr
)
632 #if wxUSE_UTF8_LOCALE_ONLY
633 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
635 #if wxUSE_UNICODE_UTF8
636 if ( wxLocaleIsUtf8
)
637 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
641 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
643 return wxCRT_VsprintfA(str
, format
.mb_str(), argptr
);
649 int wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
)
651 #if wxUSE_UNICODE_WCHAR
654 This fails with a bug similar to
655 http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=c++.beta&artnum=680
657 I don't see it being used in the wxWidgets sources at present (oct 2007) CE
659 #pragma message ( "warning ::::: wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) not yet implemented" )
660 wxFAIL_MSG( wxT("TODO") );
664 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
666 #else // wxUSE_UNICODE_UTF8
667 #if !wxUSE_UTF8_LOCALE_ONLY
668 if ( !wxLocaleIsUtf8
)
669 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
672 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
673 #endif // wxUSE_UNICODE_UTF8
675 #endif // wxUSE_UNICODE
677 int wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
)
680 #if wxUSE_UTF8_LOCALE_ONLY
681 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
683 #if wxUSE_UNICODE_UTF8
684 if ( wxLocaleIsUtf8
)
685 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
690 // NB: if this code is called, then wxString::PrintV() would use the
691 // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
693 rv
= PrintfViaString(str
, size
, format
, argptr
);
696 rv
= wxCRT_VsnprintfA(str
, size
, format
.mb_str(), argptr
);
700 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
701 // doesn't nul terminate on truncation.
708 int wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
)
712 #if wxUSE_UNICODE_WCHAR
713 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
714 #else // wxUSE_UNICODE_UTF8
715 #if !wxUSE_UTF8_LOCALE_ONLY
716 if ( !wxLocaleIsUtf8
)
717 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
721 // NB: if this code is called, then wxString::PrintV() would use the
722 // char* version of wxVsnprintf(), so it's safe to use PrintV()
724 rv
= PrintfViaString(str
, size
, format
, argptr
);
726 #endif // wxUSE_UNICODE_UTF8
728 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
729 // doesn't nul terminate on truncation.
734 #endif // wxUSE_UNICODE
737 // ----------------------------------------------------------------------------
738 // ctype.h stuff (currently unused)
739 // ----------------------------------------------------------------------------
741 #ifdef wxNEED_WX_MBSTOWCS
743 WXDLLIMPEXP_BASE
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
753 const char* origin
= in
;
755 while (outlen
-- && *in
)
757 *out
++ = (wchar_t) *in
++;
765 WXDLLIMPEXP_BASE
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
775 const wchar_t* origin
= in
;
777 while (outlen
-- && *in
)
779 *out
++ = (char) *in
++;
787 #endif // wxNEED_WX_MBSTOWCS
789 #ifndef wxCRT_StrdupA
790 WXDLLIMPEXP_BASE
char *wxCRT_StrdupA(const char *s
)
792 return strcpy((char *)malloc(strlen(s
) + 1), s
);
794 #endif // wxCRT_StrdupA
796 #ifndef wxCRT_StrdupW
797 WXDLLIMPEXP_BASE
wchar_t * wxCRT_StrdupW(const wchar_t *pwz
)
799 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
800 wchar_t *ret
= (wchar_t *) malloc(size
);
801 memcpy(ret
, pwz
, size
);
804 #endif // wxCRT_StrdupW
806 #ifndef wxWCHAR_T_IS_WXCHAR16
807 size_t wxStrlen(const wxChar16
*s
)
811 while (*s
!=0) { ++i
; ++s
; };
815 wxChar16
* wxStrdup(const wxChar16
* s
)
817 size_t size
= (wxStrlen(s
) + 1) * sizeof(wxChar16
);
818 wxChar16
*ret
= (wxChar16
*) malloc(size
);
819 memcpy(ret
, s
, size
);
824 #ifndef wxWCHAR_T_IS_WXCHAR32
825 size_t wxStrlen(const wxChar32
*s
)
829 while (*s
!=0) { ++i
; ++s
; };
833 wxChar32
* wxStrdup(const wxChar32
* s
)
835 size_t size
= (wxStrlen(s
) + 1) * sizeof(wxChar32
);
836 wxChar32
*ret
= (wxChar32
*) malloc(size
);
837 memcpy(ret
, s
, size
);
842 #ifndef wxCRT_StricmpA
843 WXDLLIMPEXP_BASE
int wxCRT_StricmpA(const char *psz1
, const char *psz2
)
845 register char c1
, c2
;
847 c1
= wxTolower(*psz1
++);
848 c2
= wxTolower(*psz2
++);
849 } while ( c1
&& (c1
== c2
) );
852 #endif // !defined(wxCRT_StricmpA)
854 #ifndef wxCRT_StricmpW
855 WXDLLIMPEXP_BASE
int wxCRT_StricmpW(const wchar_t *psz1
, const wchar_t *psz2
)
857 register wchar_t c1
, c2
;
859 c1
= wxTolower(*psz1
++);
860 c2
= wxTolower(*psz2
++);
861 } while ( c1
&& (c1
== c2
) );
864 #endif // !defined(wxCRT_StricmpW)
866 #ifndef wxCRT_StrnicmpA
867 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpA(const char *s1
, const char *s2
, size_t n
)
869 // initialize the variables just to suppress stupid gcc warning
870 register char c1
= 0, c2
= 0;
871 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
873 if (c1
< c2
) return -1;
874 if (c1
> c2
) return 1;
878 #endif // !defined(wxCRT_StrnicmpA)
880 #ifndef wxCRT_StrnicmpW
881 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpW(const wchar_t *s1
, const wchar_t *s2
, size_t n
)
883 // initialize the variables just to suppress stupid gcc warning
884 register wchar_t c1
= 0, c2
= 0;
885 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
887 if (c1
< c2
) return -1;
888 if (c1
> c2
) return 1;
892 #endif // !defined(wxCRT_StrnicmpW)
894 // ----------------------------------------------------------------------------
895 // string.h functions
896 // ----------------------------------------------------------------------------
898 // this (and wxCRT_StrncmpW below) are extern "C" because they are needed
899 // by regex code, the rest isn't needed, so it's not declared as extern "C"
900 #ifndef wxCRT_StrlenW
901 extern "C" WXDLLIMPEXP_BASE
size_t wxCRT_StrlenW(const wchar_t *s
)
911 // ----------------------------------------------------------------------------
912 // stdlib.h functions
913 // ----------------------------------------------------------------------------
915 #ifndef wxCRT_GetenvW
916 WXDLLIMPEXP_BASE
wchar_t* wxCRT_GetenvW(const wchar_t *name
)
918 // NB: buffer returned by getenv() is allowed to be overwritten next
919 // time getenv() is called, so it is OK to use static string
920 // buffer to hold the data.
921 static wxWCharBuffer value
;
922 value
= wxConvLibc
.cMB2WC(getenv(wxConvLibc
.cWC2MB(name
)));
925 #endif // !wxCRT_GetenvW
927 #ifndef wxCRT_StrftimeW
928 WXDLLIMPEXP_BASE
size_t
929 wxCRT_StrftimeW(wchar_t *s
, size_t maxsize
, const wchar_t *fmt
, const struct tm
*tm
)
934 wxCharBuffer
buf(maxsize
);
936 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
940 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
944 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
948 wxCRT_StrncpyW(s
, wbuf
, maxsize
);
949 return wxCRT_StrlenW(s
);
951 #endif // !wxCRT_StrftimeW
956 wxCRT_StrtoullBase(const T
* nptr
, T
** endptr
, int base
, T
* sign
)
958 wxULongLong_t sum
= 0;
959 wxString
wxstr(nptr
);
960 wxString::const_iterator i
= wxstr
.begin();
961 wxString::const_iterator end
= wxstr
.end();
964 while ( i
!= end
&& wxIsspace(*i
) ) ++i
;
971 if ( c
== wxT('+') || c
== wxT('-') )
979 if ( i
!= end
&& *i
== wxT('0') )
984 if ( *i
== wxT('x') && (base
== 16 || base
== 0) )
1004 for ( ; i
!= end
; ++i
)
1014 n
= wxTolower(c
) - wxT('a') + 10;
1019 if ( n
>= (unsigned int)base
)
1020 // Invalid character (for this base)
1023 wxULongLong_t prevsum
= sum
;
1024 sum
= (sum
* base
) + n
;
1026 if ( sum
< prevsum
)
1028 wxSET_ERRNO(ERANGE
);
1035 *endptr
= (T
*)(nptr
+ (i
- wxstr
.begin()));
1041 template<typename T
>
1042 static wxULongLong_t
wxCRT_DoStrtoull(const T
* nptr
, T
** endptr
, int base
)
1045 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1047 if ( sign
== wxT('-') )
1049 wxSET_ERRNO(ERANGE
);
1056 template<typename T
>
1057 static wxLongLong_t
wxCRT_DoStrtoll(const T
* nptr
, T
** endptr
, int base
)
1060 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1061 wxLongLong_t val
= 0;
1063 if ( sign
== wxT('-') )
1065 if (uval
<= (wxULongLong_t
)wxINT64_MAX
+ 1)
1067 val
= -(wxLongLong_t
)uval
;
1071 wxSET_ERRNO(ERANGE
);
1074 else if ( uval
<= wxINT64_MAX
)
1080 wxSET_ERRNO(ERANGE
);
1086 #ifndef wxCRT_StrtollA
1087 wxLongLong_t
wxCRT_StrtollA(const char* nptr
, char** endptr
, int base
)
1088 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1090 #ifndef wxCRT_StrtollW
1091 wxLongLong_t
wxCRT_StrtollW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1092 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1095 #ifndef wxCRT_StrtoullA
1096 wxULongLong_t
wxCRT_StrtoullA(const char* nptr
, char** endptr
, int base
)
1097 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1099 #ifndef wxCRT_StrtoullW
1100 wxULongLong_t
wxCRT_StrtoullW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1101 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1104 #endif // wxLongLong_t
1106 // ----------------------------------------------------------------------------
1107 // strtok() functions
1108 // ----------------------------------------------------------------------------
1110 template<typename T
>
1111 static T
*wxCRT_DoStrtok(T
*psz
, const T
*delim
, T
**save_ptr
)
1120 psz
+= wxStrspn(psz
, delim
);
1128 psz
= wxStrpbrk(psz
, delim
);
1136 *save_ptr
= psz
+ 1;
1142 #ifndef wxCRT_StrtokA
1143 char *wxCRT_StrtokA(char *psz
, const char *delim
, char **save_ptr
)
1144 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1146 #ifndef wxCRT_StrtokW
1147 wchar_t *wxCRT_StrtokW(wchar_t *psz
, const wchar_t *delim
, wchar_t **save_ptr
)
1148 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1151 // ----------------------------------------------------------------------------
1152 // missing C RTL functions
1153 // ----------------------------------------------------------------------------
1155 #ifdef wxNEED_STRDUP
1157 char *strdup(const char *s
)
1159 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1161 strcpy( dest
, s
) ;
1164 #endif // wxNEED_STRDUP
1166 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1168 void *calloc( size_t num
, size_t size
)
1170 void** ptr
= (void **)malloc(num
* size
);
1171 memset( ptr
, 0, num
* size
);
1175 #endif // __WXWINCE__ <= 211
1177 // ============================================================================
1179 // ============================================================================
1181 #if wxUSE_UNICODE_UTF8
1183 #if !wxUSE_UTF8_LOCALE_ONLY
1184 bool wxLocaleIsUtf8
= false; // the safer setting if not known
1187 static bool wxIsLocaleUtf8()
1189 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1190 // because a) it may be unavailable in some builds and b) has slightly
1191 // different semantics (default locale instead of current)
1193 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1194 // GNU libc provides current character set this way (this conforms to
1196 const char *charset
= nl_langinfo(CODESET
);
1199 // "UTF-8" is used by modern glibc versions, but test other variants
1200 // as well, just in case:
1201 if ( strcmp(charset
, "UTF-8") == 0 ||
1202 strcmp(charset
, "utf-8") == 0 ||
1203 strcmp(charset
, "UTF8") == 0 ||
1204 strcmp(charset
, "utf8") == 0 )
1209 #endif // HAVE_LANGINFO_H
1211 // check if we're running under the "C" locale: it is 7bit subset
1212 // of UTF-8, so it can be safely used with the UTF-8 build:
1213 const char *lc_ctype
= setlocale(LC_CTYPE
, NULL
);
1215 (strcmp(lc_ctype
, "C") == 0 || strcmp(lc_ctype
, "POSIX") == 0) )
1220 // we don't know what charset libc is using, so assume the worst
1225 void wxUpdateLocaleIsUtf8()
1227 #if wxUSE_UTF8_LOCALE_ONLY
1228 if ( !wxIsLocaleUtf8() )
1230 wxLogFatalError(wxT("This program requires UTF-8 locale to run."));
1232 #else // !wxUSE_UTF8_LOCALE_ONLY
1233 wxLocaleIsUtf8
= wxIsLocaleUtf8();
1237 #endif // wxUSE_UNICODE_UTF8
1239 // ============================================================================
1240 // wx wrappers for CRT functions
1241 // ============================================================================
1243 #if wxUSE_UNICODE_WCHAR
1244 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW
1245 #elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
1246 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \
1247 return_kw wxLocaleIsUtf8 ? callA : callW
1248 #else // ANSI or UTF8 only
1249 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA
1252 int wxPuts(const wxString
& s
)
1254 // under IRIX putws() takes a non-const argument so use wchar_str() instead
1256 CALL_ANSI_OR_UNICODE(return,
1257 wxCRT_PutsA(s
.mb_str()),
1258 wxCRT_PutsW(s
.wchar_str()));
1261 int wxFputs(const wxString
& s
, FILE *stream
)
1263 CALL_ANSI_OR_UNICODE(return,
1264 wxCRT_FputsA(s
.mb_str(), stream
),
1265 wxCRT_FputsW(s
.wc_str(), stream
));
1268 int wxFputc(const wxUniChar
& c
, FILE *stream
)
1270 #if !wxUSE_UNICODE // FIXME-UTF8: temporary, remove this with ANSI build
1271 return wxCRT_FputcA((char)c
, stream
);
1273 CALL_ANSI_OR_UNICODE(return,
1274 wxCRT_FputsA(c
.AsUTF8(), stream
),
1275 wxCRT_FputcW((wchar_t)c
, stream
));
1279 #ifdef wxCRT_PerrorA
1281 void wxPerror(const wxString
& s
)
1283 #ifdef wxCRT_PerrorW
1284 CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE
,
1285 wxCRT_PerrorA(s
.mb_str()),
1286 wxCRT_PerrorW(s
.wc_str()));
1288 wxCRT_PerrorA(s
.mb_str());
1292 #endif // wxCRT_PerrorA
1294 wchar_t *wxFgets(wchar_t *s
, int size
, FILE *stream
)
1296 wxCHECK_MSG( s
, NULL
, "empty buffer passed to wxFgets()" );
1298 wxCharBuffer
buf(size
- 1);
1299 // FIXME: this reads too little data if wxConvLibc uses UTF-8 ('size' wide
1300 // characters may be encoded by up to 'size'*4 bytes), but what
1302 if ( wxFgets(buf
.data(), size
, stream
) == NULL
)
1305 if ( wxConvLibc
.ToWChar(s
, size
, buf
, wxNO_LEN
) == wxCONV_FAILED
)
1311 // ----------------------------------------------------------------------------
1312 // wxScanf() and friends
1313 // ----------------------------------------------------------------------------
1315 #ifdef HAVE_VSSCANF // __VISUALC__ and __DMC__ see wx/crt.h
1316 int wxVsscanf(const char *str
, const char *format
, va_list ap
)
1317 { return wxCRT_VsscanfA(str
, format
, ap
); }
1318 int wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
)
1319 { return wxCRT_VsscanfW(str
, format
, ap
); }
1320 int wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
)
1321 { return wxCRT_VsscanfA(static_cast<const char*>(str
), format
, ap
); }
1322 int wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
)
1323 { return wxCRT_VsscanfW(str
, format
, ap
); }
1324 int wxVsscanf(const wxString
& str
, const char *format
, va_list ap
)
1325 { return wxCRT_VsscanfA(static_cast<const char*>(str
.mb_str()), format
, ap
); }
1326 int wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
)
1327 { return wxCRT_VsscanfW(str
.wc_str(), format
, ap
); }
1328 int wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
)
1329 { return wxCRT_VsscanfA(static_cast<const char*>(str
.AsCharBuf()), format
, ap
); }
1330 int wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
)
1331 { return wxCRT_VsscanfW(str
.AsWCharBuf(), format
, ap
); }
1332 #endif // HAVE_NO_VSSCANF