]>
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"
85 WXDLLIMPEXP_BASE
size_t wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
87 // assume that we have mbsrtowcs() too if we have wcsrtombs()
90 memset(&mbstate
, 0, sizeof(mbstate_t));
95 if (n
) *buf
= wxT('\0');
99 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
101 return wxMbstowcs(buf
, psz
, n
);
105 // note that we rely on common (and required by Unix98 but unfortunately not
106 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
107 // to just get the size of the needed buffer -- this is needed as otherwise
108 // we have no idea about how much space we need and if the CRT doesn't
109 // support it (the only currently known example being Metrowerks, see
110 // wx/crt.h) we don't use its mbstowcs() at all
111 #ifdef HAVE_WCSRTOMBS
112 return mbsrtowcs(NULL
, &psz
, 0, &mbstate
);
114 return wxMbstowcs(NULL
, psz
, 0);
118 WXDLLIMPEXP_BASE
size_t wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
120 #ifdef HAVE_WCSRTOMBS
122 memset(&mbstate
, 0, sizeof(mbstate_t));
127 // glibc2.1 chokes on null input
131 #ifdef HAVE_WCSRTOMBS
132 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
134 return wxWcstombs(buf
, pwz
, n
);
138 #ifdef HAVE_WCSRTOMBS
139 return wcsrtombs(NULL
, &pwz
, 0, &mbstate
);
141 return wxWcstombs(NULL
, pwz
, 0);
144 #endif // wxUSE_WCHAR_T
146 char* wxSetlocale(int category
, const char *locale
)
149 // FIXME-CE: there is no setlocale() in CE CRT, use SetThreadLocale()?
150 wxUnusedVar(category
);
154 #else // !__WXWINCE__
157 if ( locale
!= NULL
&& locale
[0] == 0 )
159 // the attempt to use newlocale(LC_ALL_MASK, "", NULL);
160 // here in order to deduce the language along the environment vars rules
161 // lead to strange crashes later...
163 // we have to emulate the behaviour under OS X
164 wxCFRef
<CFLocaleRef
> userLocaleRef(CFLocaleCopyCurrent());
165 wxCFStringRef
str(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleLanguageCode
)));
166 wxString langFull
= str
.AsString()+"_";
167 str
.reset(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleCountryCode
)));
168 langFull
+= str
.AsString();
169 rv
= setlocale(category
, langFull
.c_str());
172 rv
= setlocale(category
, locale
);
174 char *rv
= setlocale(category
, locale
);
176 if ( locale
!= NULL
/* setting locale, not querying */ &&
177 rv
/* call was successful */ )
179 wxUpdateLocaleIsUtf8();
182 #endif // __WXWINCE__/!__WXWINCE__
185 // ============================================================================
186 // printf() functions business
187 // ============================================================================
189 // special test mode: define all functions below even if we don't really need
190 // them to be able to test them
200 #define wxNEED_WPRINTF
202 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
);
206 /* Digital Mars adds count to _stprintf (C99) so convert */
207 int wxCRT_SprintfW (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
211 va_start( arglist
, format
);
212 int iLen
= swprintf ( s
, -1, format
, arglist
);
218 // ----------------------------------------------------------------------------
219 // implement the standard IO functions for wide char if libc doesn't have them
220 // ----------------------------------------------------------------------------
223 int wxCRT_FputsW(const wchar_t *ws
, FILE *stream
)
225 wxCharBuffer
buf(wxConvLibc
.cWC2MB(ws
));
229 // counting the number of wide characters written isn't worth the trouble,
230 // simply distinguish between ok and error
231 return wxCRT_FputsA(buf
, stream
) == -1 ? -1 : 0;
233 #endif // !wxCRT_FputsW
236 int wxCRT_PutsW(const wchar_t *ws
)
238 int rc
= wxCRT_FputsW(ws
, stdout
);
241 if ( wxCRT_FputsW(L
"\n", stdout
) == -1 )
249 #endif // !wxCRT_PutsW
252 int /* not wint_t */ wxCRT_FputcW(wchar_t wc
, FILE *stream
)
254 wchar_t ws
[2] = { wc
, L
'\0' };
256 return wxCRT_FputsW(ws
, stream
);
258 #endif // !wxCRT_FputcW
260 // NB: we only implement va_list functions here, the ones taking ... are
261 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
262 // the definitions there to avoid duplicating them here
263 #ifdef wxNEED_WPRINTF
265 // TODO: implement the scanf() functions
266 static int vwscanf(const wchar_t *format
, va_list argptr
)
268 wxFAIL_MSG( wxT("TODO") );
273 static int vfwscanf(FILE *stream
, const wchar_t *format
, va_list argptr
)
275 wxFAIL_MSG( wxT("TODO") );
280 #define vswprintf wxCRT_VsnprintfW
282 static int vfwprintf(FILE *stream
, const wchar_t *format
, va_list argptr
)
285 int rc
= s
.PrintfV(format
, argptr
);
289 // we can't do much better without Unicode support in libc...
290 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
297 static int vwprintf(const wchar_t *format
, va_list argptr
)
299 return wxCRT_VfprintfW(stdout
, format
, argptr
);
302 #endif // wxNEED_WPRINTF
304 #ifdef wxNEED_VSWSCANF
305 static int vswscanf(const wchar_t *ws
, const wchar_t *format
, va_list argptr
)
307 // The best we can do without proper Unicode support in glibc is to
308 // convert the strings into MB representation and run ANSI version
309 // of the function. This doesn't work with %c and %s because of difference
310 // in size of char and wchar_t, though.
312 wxCHECK_MSG( wxStrstr(format
, wxT("%s")) == NULL
, -1,
313 wxT("incomplete vswscanf implementation doesn't allow %s") );
314 wxCHECK_MSG( wxStrstr(format
, wxT("%c")) == NULL
, -1,
315 wxT("incomplete vswscanf implementation doesn't allow %c") );
317 return vsscanf(static_cast<const char*>(wxConvLibc
.cWX2MB(ws
)),
318 wxConvLibc
.cWX2MB(format
), argptr
);
322 // ----------------------------------------------------------------------------
323 // wxPrintf(), wxScanf() and relatives
324 // ----------------------------------------------------------------------------
326 // FIXME-UTF8: do format conversion using (modified) wxFormatConverter in
327 // template wrappers, not here; note that it will needed to
328 // translate all forms of string specifiers to %(l)s for wxPrintf(),
329 // but it only should do what it did in 2.8 for wxScanf()!
331 #ifndef wxCRT_PrintfW
332 int wxCRT_PrintfW( const wchar_t *format
, ... )
335 va_start(argptr
, format
);
337 int ret
= vwprintf( format
, argptr
);
345 #ifndef wxCRT_FprintfW
346 int wxCRT_FprintfW( FILE *stream
, const wchar_t *format
, ... )
349 va_start( argptr
, format
);
351 int ret
= vfwprintf( stream
, format
, argptr
);
359 #ifndef wxCRT_VfprintfW
360 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
)
362 return vfwprintf( stream
, format
, argptr
);
366 #ifndef wxCRT_VprintfW
367 int wxCRT_VprintfW( const wchar_t *format
, va_list argptr
)
369 return vwprintf( format
, argptr
);
373 #ifndef wxCRT_VsprintfW
374 int wxCRT_VsprintfW( wchar_t *str
, const wchar_t *format
, va_list argptr
)
376 // same as for wxSprintf()
377 return vswprintf(str
, INT_MAX
/ 4, format
, argptr
);
382 int wxCRT_ScanfW(const wchar_t *format
, ...)
385 va_start(argptr
, format
);
388 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
389 int ret
= std::vwscanf(format
, argptr
);
391 int ret
= vwscanf(format
, argptr
);
394 int ret
= vwscanf(format
, argptr
);
403 #ifndef wxCRT_SscanfW
404 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...)
407 va_start(argptr
, format
);
410 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
411 int ret
= std::vswscanf(str
, format
, argptr
);
413 int ret
= vswscanf(str
, format
, argptr
);
416 int ret
= vswscanf(str
, format
, argptr
);
425 #ifndef wxCRT_FscanfW
426 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...)
429 va_start(argptr
, format
);
431 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
432 int ret
= std::vfwscanf(stream
, format
, argptr
);
434 int ret
= vfwscanf(stream
, format
, argptr
);
437 int ret
= vfwscanf(stream
, format
, argptr
);
446 #ifndef wxCRT_VsscanfW
447 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list argptr
)
450 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
451 return std::vswscanf(str
, format
, argptr
);
453 return vswscanf(str
, format
, argptr
);
456 return vswscanf(str
, format
, argptr
);
462 // ----------------------------------------------------------------------------
463 // wrappers to printf and scanf function families
464 // ----------------------------------------------------------------------------
466 #if !wxUSE_UTF8_LOCALE_ONLY
467 int wxDoSprintfWchar(char *str
, const wxChar
*format
, ...)
470 va_start(argptr
, format
);
472 int rv
= wxVsprintf(str
, format
, argptr
);
477 #endif // !wxUSE_UTF8_LOCALE_ONLY
479 #if wxUSE_UNICODE_UTF8
480 int wxDoSprintfUtf8(char *str
, const char *format
, ...)
483 va_start(argptr
, format
);
485 int rv
= wxVsprintf(str
, format
, argptr
);
490 #endif // wxUSE_UNICODE_UTF8
494 #if !wxUSE_UTF8_LOCALE_ONLY
495 int wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...)
498 va_start(argptr
, format
);
500 int rv
= wxVsprintf(str
, format
, argptr
);
505 #endif // !wxUSE_UTF8_LOCALE_ONLY
507 #if wxUSE_UNICODE_UTF8
508 int wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...)
511 va_start(argptr
, format
);
513 int rv
= wxVsprintf(str
, format
, argptr
);
518 #endif // wxUSE_UNICODE_UTF8
520 #endif // wxUSE_UNICODE
522 #if !wxUSE_UTF8_LOCALE_ONLY
523 int wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...)
526 va_start(argptr
, format
);
528 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
533 #endif // !wxUSE_UTF8_LOCALE_ONLY
535 #if wxUSE_UNICODE_UTF8
536 int wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...)
539 va_start(argptr
, format
);
541 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
546 #endif // wxUSE_UNICODE_UTF8
550 #if !wxUSE_UTF8_LOCALE_ONLY
551 int wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...)
554 va_start(argptr
, format
);
556 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
561 #endif // !wxUSE_UTF8_LOCALE_ONLY
563 #if wxUSE_UNICODE_UTF8
564 int wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...)
567 va_start(argptr
, format
);
569 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
574 #endif // wxUSE_UNICODE_UTF8
576 #endif // wxUSE_UNICODE
579 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
580 #define vsnprintf wx_fixed_vsnprintf
588 #if !wxUSE_UTF8_LOCALE_ONLY
589 int ConvertStringToBuf(const wxString
& s
, char *out
, size_t outsize
)
591 const wxWX2WCbuf buf
= s
.wc_str();
593 size_t len
= wxConvLibc
.FromWChar(out
, outsize
, buf
);
594 if ( len
!= wxCONV_FAILED
)
597 return wxConvLibc
.FromWChar(NULL
, 0, buf
);
599 #endif // !wxUSE_UTF8_LOCALE_ONLY
601 #if wxUSE_UNICODE_UTF8
602 int ConvertStringToBuf(const wxString
& s
, wchar_t *out
, size_t outsize
)
604 const wxWX2WCbuf
buf(s
.wc_str());
605 size_t len
= s
.length(); // same as buf length for wchar_t*
608 memcpy(out
, buf
, (len
+1) * sizeof(wchar_t));
610 else // not enough space
612 memcpy(out
, buf
, (outsize
-1) * sizeof(wchar_t));
617 #endif // wxUSE_UNICODE_UTF8
619 } // anonymous namespace
622 static size_t PrintfViaString(T
*out
, size_t outsize
,
623 const wxString
& format
, va_list argptr
)
626 s
.PrintfV(format
, argptr
);
628 return ConvertStringToBuf(s
, out
, outsize
);
630 #endif // wxUSE_UNICODE
632 int wxVsprintf(char *str
, const wxString
& format
, va_list argptr
)
634 #if wxUSE_UTF8_LOCALE_ONLY
635 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
637 #if wxUSE_UNICODE_UTF8
638 if ( wxLocaleIsUtf8
)
639 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
643 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
645 return wxCRT_VsprintfA(str
, format
.mb_str(), argptr
);
651 int wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
)
653 #if wxUSE_UNICODE_WCHAR
656 This fails with a bug similar to
657 http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=c++.beta&artnum=680
659 I don't see it being used in the wxWidgets sources at present (oct 2007) CE
661 #pragma message ( "warning ::::: wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) not yet implemented" )
662 wxFAIL_MSG( wxT("TODO") );
666 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
668 #else // wxUSE_UNICODE_UTF8
669 #if !wxUSE_UTF8_LOCALE_ONLY
670 if ( !wxLocaleIsUtf8
)
671 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
674 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
675 #endif // wxUSE_UNICODE_UTF8
677 #endif // wxUSE_UNICODE
679 int wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
)
682 #if wxUSE_UTF8_LOCALE_ONLY
683 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
685 #if wxUSE_UNICODE_UTF8
686 if ( wxLocaleIsUtf8
)
687 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
692 // NB: if this code is called, then wxString::PrintV() would use the
693 // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
695 rv
= PrintfViaString(str
, size
, format
, argptr
);
698 rv
= wxCRT_VsnprintfA(str
, size
, format
.mb_str(), argptr
);
702 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
703 // doesn't nul terminate on truncation.
710 int wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
)
714 #if wxUSE_UNICODE_WCHAR
715 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
716 #else // wxUSE_UNICODE_UTF8
717 #if !wxUSE_UTF8_LOCALE_ONLY
718 if ( !wxLocaleIsUtf8
)
719 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
723 // NB: if this code is called, then wxString::PrintV() would use the
724 // char* version of wxVsnprintf(), so it's safe to use PrintV()
726 rv
= PrintfViaString(str
, size
, format
, argptr
);
728 #endif // wxUSE_UNICODE_UTF8
730 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
731 // doesn't nul terminate on truncation.
736 #endif // wxUSE_UNICODE
740 // ----------------------------------------------------------------------------
741 // ctype.h stuff (currently unused)
742 // ----------------------------------------------------------------------------
744 #ifdef wxNEED_WX_MBSTOWCS
746 WXDLLIMPEXP_BASE
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
756 const char* origin
= in
;
758 while (outlen
-- && *in
)
760 *out
++ = (wchar_t) *in
++;
768 WXDLLIMPEXP_BASE
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
778 const wchar_t* origin
= in
;
780 while (outlen
-- && *in
)
782 *out
++ = (char) *in
++;
790 #endif // wxNEED_WX_MBSTOWCS
792 #ifndef wxCRT_StrdupA
793 WXDLLIMPEXP_BASE
char *wxCRT_StrdupA(const char *s
)
795 return strcpy((char *)malloc(strlen(s
) + 1), s
);
797 #endif // wxCRT_StrdupA
799 #ifndef wxCRT_StrdupW
800 WXDLLIMPEXP_BASE
wchar_t * wxCRT_StrdupW(const wchar_t *pwz
)
802 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
803 wchar_t *ret
= (wchar_t *) malloc(size
);
804 memcpy(ret
, pwz
, size
);
807 #endif // wxCRT_StrdupW
809 #ifndef wxWCHAR_T_IS_WXCHAR16
810 size_t wxStrlen(const wxChar16
*s
)
814 while (*s
!=0) { ++i
; ++s
; };
818 wxChar16
* wxStrdup(const wxChar16
* s
)
820 size_t size
= (wxStrlen(s
) + 1) * sizeof(wxChar16
);
821 wxChar16
*ret
= (wxChar16
*) malloc(size
);
822 memcpy(ret
, s
, size
);
827 #ifndef wxWCHAR_T_IS_WXCHAR32
828 size_t wxStrlen(const wxChar32
*s
)
832 while (*s
!=0) { ++i
; ++s
; };
836 wxChar32
* wxStrdup(const wxChar32
* s
)
838 size_t size
= (wxStrlen(s
) + 1) * sizeof(wxChar32
);
839 wxChar32
*ret
= (wxChar32
*) malloc(size
);
840 memcpy(ret
, s
, size
);
845 #ifndef wxCRT_StricmpA
846 WXDLLIMPEXP_BASE
int wxCRT_StricmpA(const char *psz1
, const char *psz2
)
848 register char c1
, c2
;
850 c1
= wxTolower(*psz1
++);
851 c2
= wxTolower(*psz2
++);
852 } while ( c1
&& (c1
== c2
) );
855 #endif // !defined(wxCRT_StricmpA)
857 #ifndef wxCRT_StricmpW
858 WXDLLIMPEXP_BASE
int wxCRT_StricmpW(const wchar_t *psz1
, const wchar_t *psz2
)
860 register wchar_t c1
, c2
;
862 c1
= wxTolower(*psz1
++);
863 c2
= wxTolower(*psz2
++);
864 } while ( c1
&& (c1
== c2
) );
867 #endif // !defined(wxCRT_StricmpW)
869 #ifndef wxCRT_StrnicmpA
870 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpA(const char *s1
, const char *s2
, size_t n
)
872 // initialize the variables just to suppress stupid gcc warning
873 register char c1
= 0, c2
= 0;
874 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
876 if (c1
< c2
) return -1;
877 if (c1
> c2
) return 1;
881 #endif // !defined(wxCRT_StrnicmpA)
883 #ifndef wxCRT_StrnicmpW
884 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpW(const wchar_t *s1
, const wchar_t *s2
, size_t n
)
886 // initialize the variables just to suppress stupid gcc warning
887 register wchar_t c1
= 0, c2
= 0;
888 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
890 if (c1
< c2
) return -1;
891 if (c1
> c2
) return 1;
895 #endif // !defined(wxCRT_StrnicmpW)
897 // ----------------------------------------------------------------------------
898 // string.h functions
899 // ----------------------------------------------------------------------------
901 // this (and wxCRT_StrncmpW below) are extern "C" because they are needed
902 // by regex code, the rest isn't needed, so it's not declared as extern "C"
903 #ifndef wxCRT_StrlenW
904 extern "C" WXDLLIMPEXP_BASE
size_t wxCRT_StrlenW(const wchar_t *s
)
914 // ----------------------------------------------------------------------------
915 // stdlib.h functions
916 // ----------------------------------------------------------------------------
918 #ifndef wxCRT_GetenvW
919 WXDLLIMPEXP_BASE
wchar_t* wxCRT_GetenvW(const wchar_t *name
)
921 // NB: buffer returned by getenv() is allowed to be overwritten next
922 // time getenv() is called, so it is OK to use static string
923 // buffer to hold the data.
924 static wxWCharBuffer value
;
925 value
= wxConvLibc
.cMB2WC(getenv(wxConvLibc
.cWC2MB(name
)));
928 #endif // !wxCRT_GetenvW
930 #ifndef wxCRT_StrftimeW
931 WXDLLIMPEXP_BASE
size_t
932 wxCRT_StrftimeW(wchar_t *s
, size_t maxsize
, const wchar_t *fmt
, const struct tm
*tm
)
937 wxCharBuffer
buf(maxsize
);
939 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
943 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
947 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
951 wxCRT_StrncpyW(s
, wbuf
, maxsize
);
952 return wxCRT_StrlenW(s
);
954 #endif // !wxCRT_StrftimeW
956 #endif // wxUSE_WCHAR_T
961 wxCRT_StrtoullBase(const T
* nptr
, T
** endptr
, int base
, T
* sign
)
963 wxULongLong_t sum
= 0;
964 wxString
wxstr(nptr
);
965 wxString::const_iterator i
= wxstr
.begin();
966 wxString::const_iterator end
= wxstr
.end();
969 while ( i
!= end
&& wxIsspace(*i
) ) ++i
;
976 if ( c
== wxT('+') || c
== wxT('-') )
984 if ( i
!= end
&& *i
== wxT('0') )
989 if ( *i
== wxT('x') && (base
== 16 || base
== 0) )
1009 for ( ; i
!= end
; ++i
)
1019 n
= wxTolower(c
) - wxT('a') + 10;
1024 if ( n
>= (unsigned int)base
)
1025 // Invalid character (for this base)
1028 wxULongLong_t prevsum
= sum
;
1029 sum
= (sum
* base
) + n
;
1031 if ( sum
< prevsum
)
1033 wxSET_ERRNO(ERANGE
);
1040 *endptr
= (T
*)(nptr
+ (i
- wxstr
.begin()));
1046 template<typename T
>
1047 static wxULongLong_t
wxCRT_DoStrtoull(const T
* nptr
, T
** endptr
, int base
)
1050 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1052 if ( sign
== wxT('-') )
1054 wxSET_ERRNO(ERANGE
);
1061 template<typename T
>
1062 static wxLongLong_t
wxCRT_DoStrtoll(const T
* nptr
, T
** endptr
, int base
)
1065 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1066 wxLongLong_t val
= 0;
1068 if ( sign
== wxT('-') )
1070 if (uval
<= (wxULongLong_t
)wxINT64_MAX
+ 1)
1072 val
= -(wxLongLong_t
)uval
;
1076 wxSET_ERRNO(ERANGE
);
1079 else if ( uval
<= wxINT64_MAX
)
1085 wxSET_ERRNO(ERANGE
);
1091 #ifndef wxCRT_StrtollA
1092 wxLongLong_t
wxCRT_StrtollA(const char* nptr
, char** endptr
, int base
)
1093 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1095 #ifndef wxCRT_StrtollW
1096 wxLongLong_t
wxCRT_StrtollW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1097 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1100 #ifndef wxCRT_StrtoullA
1101 wxULongLong_t
wxCRT_StrtoullA(const char* nptr
, char** endptr
, int base
)
1102 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1104 #ifndef wxCRT_StrtoullW
1105 wxULongLong_t
wxCRT_StrtoullW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1106 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1109 #endif // wxLongLong_t
1111 // ----------------------------------------------------------------------------
1112 // functions which we may need even if !wxUSE_WCHAR_T
1113 // ----------------------------------------------------------------------------
1115 template<typename T
>
1116 static T
*wxCRT_DoStrtok(T
*psz
, const T
*delim
, T
**save_ptr
)
1125 psz
+= wxStrspn(psz
, delim
);
1133 psz
= wxStrpbrk(psz
, delim
);
1141 *save_ptr
= psz
+ 1;
1147 #ifndef wxCRT_StrtokA
1148 char *wxCRT_StrtokA(char *psz
, const char *delim
, char **save_ptr
)
1149 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1151 #ifndef wxCRT_StrtokW
1152 wchar_t *wxCRT_StrtokW(wchar_t *psz
, const wchar_t *delim
, wchar_t **save_ptr
)
1153 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1156 // ----------------------------------------------------------------------------
1157 // missing C RTL functions
1158 // ----------------------------------------------------------------------------
1160 #ifdef wxNEED_STRDUP
1162 char *strdup(const char *s
)
1164 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1166 strcpy( dest
, s
) ;
1169 #endif // wxNEED_STRDUP
1171 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1173 void *calloc( size_t num
, size_t size
)
1175 void** ptr
= (void **)malloc(num
* size
);
1176 memset( ptr
, 0, num
* size
);
1180 #endif // __WXWINCE__ <= 211
1182 // ============================================================================
1184 // ============================================================================
1186 #if wxUSE_UNICODE_UTF8
1188 #if !wxUSE_UTF8_LOCALE_ONLY
1189 bool wxLocaleIsUtf8
= false; // the safer setting if not known
1192 static bool wxIsLocaleUtf8()
1194 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1195 // because a) it may be unavailable in some builds and b) has slightly
1196 // different semantics (default locale instead of current)
1198 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1199 // GNU libc provides current character set this way (this conforms to
1201 const char *charset
= nl_langinfo(CODESET
);
1204 // "UTF-8" is used by modern glibc versions, but test other variants
1205 // as well, just in case:
1206 if ( strcmp(charset
, "UTF-8") == 0 ||
1207 strcmp(charset
, "utf-8") == 0 ||
1208 strcmp(charset
, "UTF8") == 0 ||
1209 strcmp(charset
, "utf8") == 0 )
1214 #endif // HAVE_LANGINFO_H
1216 // check if we're running under the "C" locale: it is 7bit subset
1217 // of UTF-8, so it can be safely used with the UTF-8 build:
1218 const char *lc_ctype
= setlocale(LC_CTYPE
, NULL
);
1220 (strcmp(lc_ctype
, "C") == 0 || strcmp(lc_ctype
, "POSIX") == 0) )
1225 // we don't know what charset libc is using, so assume the worst
1230 void wxUpdateLocaleIsUtf8()
1232 #if wxUSE_UTF8_LOCALE_ONLY
1233 if ( !wxIsLocaleUtf8() )
1235 wxLogFatalError(wxT("This program requires UTF-8 locale to run."));
1237 #else // !wxUSE_UTF8_LOCALE_ONLY
1238 wxLocaleIsUtf8
= wxIsLocaleUtf8();
1242 #endif // wxUSE_UNICODE_UTF8
1244 // ============================================================================
1245 // wx wrappers for CRT functions
1246 // ============================================================================
1248 #if wxUSE_UNICODE_WCHAR
1249 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW
1250 #elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
1251 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \
1252 return_kw wxLocaleIsUtf8 ? callA : callW
1253 #else // ANSI or UTF8 only
1254 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA
1257 int wxPuts(const wxString
& s
)
1259 // under IRIX putws() takes a non-const argument so use wchar_str() instead
1261 CALL_ANSI_OR_UNICODE(return,
1262 wxCRT_PutsA(s
.mb_str()),
1263 wxCRT_PutsW(s
.wchar_str()));
1266 int wxFputs(const wxString
& s
, FILE *stream
)
1268 CALL_ANSI_OR_UNICODE(return,
1269 wxCRT_FputsA(s
.mb_str(), stream
),
1270 wxCRT_FputsW(s
.wc_str(), stream
));
1273 int wxFputc(const wxUniChar
& c
, FILE *stream
)
1275 #if !wxUSE_UNICODE // FIXME-UTF8: temporary, remove this with ANSI build
1276 return wxCRT_FputcA((char)c
, stream
);
1278 CALL_ANSI_OR_UNICODE(return,
1279 wxCRT_FputsA(c
.AsUTF8(), stream
),
1280 wxCRT_FputcW((wchar_t)c
, stream
));
1284 #ifdef wxCRT_PerrorA
1286 void wxPerror(const wxString
& s
)
1288 #ifdef wxCRT_PerrorW
1289 CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE
,
1290 wxCRT_PerrorA(s
.mb_str()),
1291 wxCRT_PerrorW(s
.wc_str()));
1293 wxCRT_PerrorA(s
.mb_str());
1297 #endif // wxCRT_PerrorA
1299 wchar_t *wxFgets(wchar_t *s
, int size
, FILE *stream
)
1301 wxCHECK_MSG( s
, NULL
, "empty buffer passed to wxFgets()" );
1303 wxCharBuffer
buf(size
- 1);
1304 // FIXME: this reads too little data if wxConvLibc uses UTF-8 ('size' wide
1305 // characters may be encoded by up to 'size'*4 bytes), but what
1307 if ( wxFgets(buf
.data(), size
, stream
) == NULL
)
1310 if ( wxConvLibc
.ToWChar(s
, size
, buf
, wxNO_LEN
) == wxCONV_FAILED
)
1316 // ----------------------------------------------------------------------------
1317 // wxScanf() and friends
1318 // ----------------------------------------------------------------------------
1320 #ifdef HAVE_VSSCANF // __VISUALC__ and __DMC__ see wx/crt.h
1321 int wxVsscanf(const char *str
, const char *format
, va_list ap
)
1322 { return wxCRT_VsscanfA(str
, format
, ap
); }
1323 int wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
)
1324 { return wxCRT_VsscanfW(str
, format
, ap
); }
1325 int wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
)
1326 { return wxCRT_VsscanfA(static_cast<const char*>(str
), format
, ap
); }
1327 int wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
)
1328 { return wxCRT_VsscanfW(str
, format
, ap
); }
1329 int wxVsscanf(const wxString
& str
, const char *format
, va_list ap
)
1330 { return wxCRT_VsscanfA(static_cast<const char*>(str
.mb_str()), format
, ap
); }
1331 int wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
)
1332 { return wxCRT_VsscanfW(str
.wc_str(), format
, ap
); }
1333 int wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
)
1334 { return wxCRT_VsscanfA(static_cast<const char*>(str
.AsCharBuf()), format
, ap
); }
1335 int wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
)
1336 { return wxCRT_VsscanfW(str
.AsWCharBuf(), format
, ap
); }
1337 #endif // HAVE_NO_VSSCANF