]>
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()
38 #include "wx/msw/wince/time.h"
40 #endif // !__WXPALMOS5__
43 #include "wx/string.h"
45 #include "wx/utils.h" // for wxMin and wxMax
49 #ifdef HAVE_LANGINFO_H
54 // there is no errno.h under CE apparently
55 #define wxSET_ERRNO(value)
59 #define wxSET_ERRNO(value) errno = value
62 #if defined(__MWERKS__) && __MSL__ >= 0x6000
67 #if defined(__DARWIN__)
68 #include "wx/mac/corefoundation/cfref.h"
69 #include <CoreFoundation/CFLocale.h>
70 #include "wx/mac/corefoundation/cfstring.h"
75 WXDLLIMPEXP_BASE
size_t wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
77 // assume that we have mbsrtowcs() too if we have wcsrtombs()
80 memset(&mbstate
, 0, sizeof(mbstate_t));
85 if (n
) *buf
= wxT('\0');
89 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
91 return wxMbstowcs(buf
, psz
, n
);
95 // note that we rely on common (and required by Unix98 but unfortunately not
96 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
97 // to just get the size of the needed buffer -- this is needed as otherwise
98 // we have no idea about how much space we need and if the CRT doesn't
99 // support it (the only currently known example being Metrowerks, see
100 // wx/crt.h) we don't use its mbstowcs() at all
101 #ifdef HAVE_WCSRTOMBS
102 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
104 return wxMbstowcs((wchar_t *) NULL
, psz
, 0);
108 WXDLLIMPEXP_BASE
size_t wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
110 #ifdef HAVE_WCSRTOMBS
112 memset(&mbstate
, 0, sizeof(mbstate_t));
117 // glibc2.1 chokes on null input
121 #ifdef HAVE_WCSRTOMBS
122 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
124 return wxWcstombs(buf
, pwz
, n
);
128 #ifdef HAVE_WCSRTOMBS
129 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
131 return wxWcstombs((char *) NULL
, pwz
, 0);
134 #endif // wxUSE_WCHAR_T
136 WXDLLIMPEXP_BASE
bool wxOKlibc()
138 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
139 // glibc 2.0 uses UTF-8 even when it shouldn't
141 if ((MB_CUR_MAX
== 2) &&
142 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
144 // this is UTF-8 allright, check whether that's what we want
145 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
146 if ((strlen(cur_locale
) < 4) ||
147 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
148 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
149 // nope, don't use libc conversion
157 char* wxSetlocale(int category
, const char *locale
)
160 // FIXME-CE: there is no setlocale() in CE CRT, use SetThreadLocale()?
161 wxUnusedVar(category
);
165 #else // !__WXWINCE__
168 if ( locale
!= NULL
&& locale
[0] == 0 )
170 locale_t lt
= newlocale(LC_ALL_MASK
, "", NULL
);
173 rv
= (char*) querylocale( LC_ALL_MASK
, lt
);
176 if ( rv
== NULL
|| rv
[0] == 0 || strcmp( rv
, "C" ) == 0 || strcmp( rv
, "POSIX" ) == 0 )
178 // we have to emulate the behaviour under OS X
179 wxCFRef
<CFLocaleRef
> userLocaleRef(CFLocaleCopyCurrent());
180 wxCFStringRef
str(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleLanguageCode
)));
181 wxString langFull
= str
.AsString()+"_";
182 str
.reset(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleCountryCode
)));
183 langFull
+= str
.AsString();
184 rv
= setlocale(category
, langFull
.c_str());
188 rv
= setlocale(category
, rv
);
192 rv
= setlocale(category
, locale
);
194 char *rv
= setlocale(category
, locale
);
196 if ( locale
!= NULL
/* setting locale, not querying */ &&
197 rv
/* call was successful */ )
199 wxUpdateLocaleIsUtf8();
202 #endif // __WXWINCE__/!__WXWINCE__
205 // ============================================================================
206 // printf() functions business
207 // ============================================================================
209 // special test mode: define all functions below even if we don't really need
210 // them to be able to test them
220 #define wxNEED_WPRINTF
222 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
);
226 /* Digital Mars adds count to _stprintf (C99) so convert */
227 int wxCRT_SprintfW (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
231 va_start( arglist
, format
);
232 int iLen
= swprintf ( s
, -1, format
, arglist
);
238 // ----------------------------------------------------------------------------
239 // implement the standard IO functions for wide char if libc doesn't have them
240 // ----------------------------------------------------------------------------
243 int wxCRT_FputsW(const wchar_t *ws
, FILE *stream
)
245 wxCharBuffer
buf(wxConvLibc
.cWC2MB(ws
));
249 // counting the number of wide characters written isn't worth the trouble,
250 // simply distinguish between ok and error
251 return wxCRT_FputsA(buf
, stream
) == -1 ? -1 : 0;
253 #endif // !wxCRT_FputsW
256 int wxCRT_PutsW(const wchar_t *ws
)
258 int rc
= wxCRT_FputsW(ws
, stdout
);
261 if ( wxCRT_FputsW(L
"\n", stdout
) == -1 )
269 #endif // !wxCRT_PutsW
272 int /* not wint_t */ wxCRT_FputcW(wchar_t wc
, FILE *stream
)
274 wchar_t ws
[2] = { wc
, L
'\0' };
276 return wxCRT_FputsW(ws
, stream
);
278 #endif // !wxCRT_FputcW
280 // NB: we only implement va_list functions here, the ones taking ... are
281 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
282 // the definitions there to avoid duplicating them here
283 #ifdef wxNEED_WPRINTF
285 // TODO: implement the scanf() functions
286 static int vwscanf(const wchar_t *format
, va_list argptr
)
288 wxFAIL_MSG( _T("TODO") );
293 static int vswscanf(const wchar_t *ws
, const wchar_t *format
, va_list argptr
)
295 // The best we can do without proper Unicode support in glibc is to
296 // convert the strings into MB representation and run ANSI version
297 // of the function. This doesn't work with %c and %s because of difference
298 // in size of char and wchar_t, though.
300 wxCHECK_MSG( wxStrstr(format
, _T("%s")) == NULL
, -1,
301 _T("incomplete vswscanf implementation doesn't allow %s") );
302 wxCHECK_MSG( wxStrstr(format
, _T("%c")) == NULL
, -1,
303 _T("incomplete vswscanf implementation doesn't allow %c") );
305 return vsscanf(wxConvLibc
.cWX2MB(ws
), wxConvLibc
.cWX2MB(format
), argptr
);
308 static int vfwscanf(FILE *stream
, const wchar_t *format
, va_list argptr
)
310 wxFAIL_MSG( _T("TODO") );
315 #define vswprintf wxCRT_VsnprintfW
317 static int vfwprintf(FILE *stream
, const wchar_t *format
, va_list argptr
)
320 int rc
= s
.PrintfV(format
, argptr
);
324 // we can't do much better without Unicode support in libc...
325 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
332 static int vwprintf(const wchar_t *format
, va_list argptr
)
334 return wxCRT_VfprintfW(stdout
, format
, argptr
);
337 #endif // wxNEED_WPRINTF
339 // ----------------------------------------------------------------------------
340 // wxPrintf(), wxScanf() and relatives
341 // ----------------------------------------------------------------------------
343 // FIXME-UTF8: do format conversion using (modified) wxFormatConverter in
344 // template wrappers, not here; note that it will needed to
345 // translate all forms of string specifiers to %(l)s for wxPrintf(),
346 // but it only should do what it did in 2.8 for wxScanf()!
348 #ifndef wxCRT_PrintfW
349 int wxCRT_PrintfW( const wchar_t *format
, ... )
352 va_start(argptr
, format
);
354 int ret
= vwprintf( format
, argptr
);
362 #ifndef wxCRT_FprintfW
363 int wxCRT_FprintfW( FILE *stream
, const wchar_t *format
, ... )
366 va_start( argptr
, format
);
368 int ret
= vfwprintf( stream
, format
, argptr
);
376 #ifndef wxCRT_VfprintfW
377 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
)
379 return vfwprintf( stream
, format
, argptr
);
383 #ifndef wxCRT_VprintfW
384 int wxCRT_VprintfW( const wchar_t *format
, va_list argptr
)
386 return vwprintf( format
, argptr
);
390 #ifndef wxCRT_VsprintfW
391 int wxCRT_VsprintfW( wchar_t *str
, const wchar_t *format
, va_list argptr
)
393 // same as for wxSprintf()
394 return vswprintf(str
, INT_MAX
/ 4, format
, argptr
);
399 int wxCRT_ScanfW(const wchar_t *format
, ...)
402 va_start(argptr
, format
);
405 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
406 int ret
= std::vwscanf(format
, argptr
);
408 int ret
= vwscanf(format
, argptr
);
411 int ret
= vwscanf(format
, argptr
);
420 #ifndef wxCRT_SscanfW
421 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...)
424 va_start(argptr
, format
);
427 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
428 int ret
= std::vswscanf(str
, format
, argptr
);
430 int ret
= vswscanf(str
, format
, argptr
);
433 int ret
= vswscanf(str
, format
, argptr
);
442 #ifndef wxCRT_FscanfW
443 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...)
446 va_start(argptr
, format
);
448 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
449 int ret
= std::vfwscanf(stream
, format
, argptr
);
451 int ret
= vfwscanf(stream
, format
, argptr
);
454 int ret
= vfwscanf(stream
, format
, argptr
);
463 #ifndef wxCRT_VsscanfW
464 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list argptr
)
467 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
468 return std::vswscanf(str
, format
, argptr
);
470 return vswscanf(str
, format
, argptr
);
473 return vswscanf(str
, format
, argptr
);
479 // ----------------------------------------------------------------------------
480 // wrappers to printf and scanf function families
481 // ----------------------------------------------------------------------------
483 #if !wxUSE_UTF8_LOCALE_ONLY
484 int wxDoSprintfWchar(char *str
, const wxChar
*format
, ...)
487 va_start(argptr
, format
);
489 int rv
= wxVsprintf(str
, format
, argptr
);
494 #endif // !wxUSE_UTF8_LOCALE_ONLY
496 #if wxUSE_UNICODE_UTF8
497 int wxDoSprintfUtf8(char *str
, const char *format
, ...)
500 va_start(argptr
, format
);
502 int rv
= wxVsprintf(str
, format
, argptr
);
507 #endif // wxUSE_UNICODE_UTF8
511 #if !wxUSE_UTF8_LOCALE_ONLY
512 int wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...)
515 va_start(argptr
, format
);
517 int rv
= wxVsprintf(str
, format
, argptr
);
522 #endif // !wxUSE_UTF8_LOCALE_ONLY
524 #if wxUSE_UNICODE_UTF8
525 int wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...)
528 va_start(argptr
, format
);
530 int rv
= wxVsprintf(str
, format
, argptr
);
535 #endif // wxUSE_UNICODE_UTF8
537 #endif // wxUSE_UNICODE
539 #if !wxUSE_UTF8_LOCALE_ONLY
540 int wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...)
543 va_start(argptr
, format
);
545 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
550 #endif // !wxUSE_UTF8_LOCALE_ONLY
552 #if wxUSE_UNICODE_UTF8
553 int wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...)
556 va_start(argptr
, format
);
558 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
563 #endif // wxUSE_UNICODE_UTF8
567 #if !wxUSE_UTF8_LOCALE_ONLY
568 int wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...)
571 va_start(argptr
, format
);
573 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
578 #endif // !wxUSE_UTF8_LOCALE_ONLY
580 #if wxUSE_UNICODE_UTF8
581 int wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...)
584 va_start(argptr
, format
);
586 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
591 #endif // wxUSE_UNICODE_UTF8
593 #endif // wxUSE_UNICODE
596 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
597 #define vsnprintf wx_fixed_vsnprintf
605 #if !wxUSE_UTF8_LOCALE_ONLY
606 int ConvertStringToBuf(const wxString
& s
, char *out
, size_t outsize
)
608 const wxWX2WCbuf buf
= s
.wc_str();
610 size_t len
= wxConvLibc
.FromWChar(out
, outsize
, buf
);
611 if ( len
!= wxCONV_FAILED
)
614 return wxConvLibc
.FromWChar(NULL
, 0, buf
);
616 #endif // !wxUSE_UTF8_LOCALE_ONLY
618 #if wxUSE_UNICODE_UTF8
619 int ConvertStringToBuf(const wxString
& s
, wchar_t *out
, size_t outsize
)
621 const wxWX2WCbuf
buf(s
.wc_str());
622 size_t len
= s
.length(); // same as buf length for wchar_t*
625 memcpy(out
, buf
, (len
+1) * sizeof(wchar_t));
627 else // not enough space
629 memcpy(out
, buf
, (outsize
-1) * sizeof(wchar_t));
634 #endif // wxUSE_UNICODE_UTF8
636 } // anonymous namespace
639 static size_t PrintfViaString(T
*out
, size_t outsize
,
640 const wxString
& format
, va_list argptr
)
643 s
.PrintfV(format
, argptr
);
645 return ConvertStringToBuf(s
, out
, outsize
);
647 #endif // wxUSE_UNICODE
649 int wxVsprintf(char *str
, const wxString
& format
, va_list argptr
)
651 #if wxUSE_UTF8_LOCALE_ONLY
652 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
654 #if wxUSE_UNICODE_UTF8
655 if ( wxLocaleIsUtf8
)
656 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
660 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
662 return wxCRT_VsprintfA(str
, format
.mb_str(), argptr
);
668 int wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
)
670 #if wxUSE_UNICODE_WCHAR
673 This fails with a bug similar to
674 http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=c++.beta&artnum=680
676 I don't see it being used in the wxWidgets sources at present (oct 2007) CE
678 #pragma message ( "warning ::::: wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) not yet implemented" )
679 wxFAIL_MSG( _T("TODO") );
683 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
685 #else // wxUSE_UNICODE_UTF8
686 #if !wxUSE_UTF8_LOCALE_ONLY
687 if ( !wxLocaleIsUtf8
)
688 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
691 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
692 #endif // wxUSE_UNICODE_UTF8
694 #endif // wxUSE_UNICODE
696 int wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
)
699 #if wxUSE_UTF8_LOCALE_ONLY
700 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
702 #if wxUSE_UNICODE_UTF8
703 if ( wxLocaleIsUtf8
)
704 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
709 // NB: if this code is called, then wxString::PrintV() would use the
710 // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
712 rv
= PrintfViaString(str
, size
, format
, argptr
);
715 rv
= wxCRT_VsnprintfA(str
, size
, format
.mb_str(), argptr
);
719 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
720 // doesn't nul terminate on truncation.
727 int wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
)
731 #if wxUSE_UNICODE_WCHAR
732 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
733 #else // wxUSE_UNICODE_UTF8
734 #if !wxUSE_UTF8_LOCALE_ONLY
735 if ( !wxLocaleIsUtf8
)
736 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
740 // NB: if this code is called, then wxString::PrintV() would use the
741 // char* version of wxVsnprintf(), so it's safe to use PrintV()
743 rv
= PrintfViaString(str
, size
, format
, argptr
);
745 #endif // wxUSE_UNICODE_UTF8
747 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
748 // doesn't nul terminate on truncation.
753 #endif // wxUSE_UNICODE
757 // ----------------------------------------------------------------------------
758 // ctype.h stuff (currently unused)
759 // ----------------------------------------------------------------------------
761 #ifdef wxNEED_WX_MBSTOWCS
763 WXDLLIMPEXP_BASE
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
773 const char* origin
= in
;
775 while (outlen
-- && *in
)
777 *out
++ = (wchar_t) *in
++;
785 WXDLLIMPEXP_BASE
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
795 const wchar_t* origin
= in
;
797 while (outlen
-- && *in
)
799 *out
++ = (char) *in
++;
807 #endif // wxNEED_WX_MBSTOWCS
809 #ifndef wxCRT_StrdupA
810 WXDLLIMPEXP_BASE
char *wxCRT_StrdupA(const char *s
)
812 return strcpy((char *)malloc(strlen(s
) + 1), s
);
814 #endif // wxCRT_StrdupA
816 #ifndef wxCRT_StrdupW
817 WXDLLIMPEXP_BASE
wchar_t * wxCRT_StrdupW(const wchar_t *pwz
)
819 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
820 wchar_t *ret
= (wchar_t *) malloc(size
);
821 memcpy(ret
, pwz
, size
);
824 #endif // wxCRT_StrdupW
826 #ifndef wxCRT_StricmpA
827 WXDLLIMPEXP_BASE
int wxCRT_StricmpA(const char *psz1
, const char *psz2
)
829 register char c1
, c2
;
831 c1
= wxTolower(*psz1
++);
832 c2
= wxTolower(*psz2
++);
833 } while ( c1
&& (c1
== c2
) );
836 #endif // !defined(wxCRT_StricmpA)
838 #ifndef wxCRT_StricmpW
839 WXDLLIMPEXP_BASE
int wxCRT_StricmpW(const wchar_t *psz1
, const wchar_t *psz2
)
841 register wchar_t c1
, c2
;
843 c1
= wxTolower(*psz1
++);
844 c2
= wxTolower(*psz2
++);
845 } while ( c1
&& (c1
== c2
) );
848 #endif // !defined(wxCRT_StricmpW)
850 #ifndef wxCRT_StrnicmpA
851 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpA(const char *s1
, const char *s2
, size_t n
)
853 // initialize the variables just to suppress stupid gcc warning
854 register char c1
= 0, c2
= 0;
855 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
857 if (c1
< c2
) return -1;
858 if (c1
> c2
) return 1;
862 #endif // !defined(wxCRT_StrnicmpA)
864 #ifndef wxCRT_StrnicmpW
865 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpW(const wchar_t *s1
, const wchar_t *s2
, size_t n
)
867 // initialize the variables just to suppress stupid gcc warning
868 register wchar_t c1
= 0, c2
= 0;
869 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
871 if (c1
< c2
) return -1;
872 if (c1
> c2
) return 1;
876 #endif // !defined(wxCRT_StrnicmpW)
878 // ----------------------------------------------------------------------------
879 // string.h functions
880 // ----------------------------------------------------------------------------
882 // this (and wxCRT_StrncmpW below) are extern "C" because they are needed
883 // by regex code, the rest isn't needed, so it's not declared as extern "C"
884 #ifndef wxCRT_StrlenW
885 extern "C" WXDLLIMPEXP_BASE
size_t wxCRT_StrlenW(const wchar_t *s
)
895 // ----------------------------------------------------------------------------
896 // stdlib.h functions
897 // ----------------------------------------------------------------------------
899 #ifndef wxCRT_GetenvW
900 WXDLLIMPEXP_BASE
wchar_t* wxCRT_GetenvW(const wchar_t *name
)
902 // NB: buffer returned by getenv() is allowed to be overwritten next
903 // time getenv() is called, so it is OK to use static string
904 // buffer to hold the data.
905 static wxWCharBuffer
value((wchar_t*)NULL
);
906 value
= wxConvLibc
.cMB2WC(getenv(wxConvLibc
.cWC2MB(name
)));
909 #endif // !wxCRT_GetenvW
911 #ifndef wxCRT_StrftimeW
912 WXDLLIMPEXP_BASE
size_t
913 wxCRT_StrftimeW(wchar_t *s
, size_t maxsize
, const wchar_t *fmt
, const struct tm
*tm
)
918 wxCharBuffer
buf(maxsize
);
920 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
924 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
928 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
932 wxCRT_StrncpyW(s
, wbuf
, maxsize
);
933 return wxCRT_StrlenW(s
);
935 #endif // !wxCRT_StrftimeW
937 #endif // wxUSE_WCHAR_T
942 wxCRT_StrtoullBase(const T
* nptr
, T
** endptr
, int base
, T
* sign
)
944 wxULongLong_t sum
= 0;
945 wxString
wxstr(nptr
);
946 wxString::const_iterator i
= wxstr
.begin();
947 wxString::const_iterator end
= wxstr
.end();
950 while ( i
!= end
&& wxIsspace(*i
) ) ++i
;
957 if ( c
== wxT('+') || c
== wxT('-') )
965 if ( i
!= end
&& *i
== wxT('0') )
970 if ( *i
== wxT('x') && (base
== 16 || base
== 0) )
990 for ( ; i
!= end
; ++i
)
1000 n
= wxTolower(c
) - wxT('a') + 10;
1005 if ( n
>= (unsigned int)base
)
1006 // Invalid character (for this base)
1009 wxULongLong_t prevsum
= sum
;
1010 sum
= (sum
* base
) + n
;
1012 if ( sum
< prevsum
)
1014 wxSET_ERRNO(ERANGE
);
1021 *endptr
= (T
*)(nptr
+ (i
- wxstr
.begin()));
1027 template<typename T
>
1028 static wxULongLong_t
wxCRT_DoStrtoull(const T
* nptr
, T
** endptr
, int base
)
1031 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1033 if ( sign
== wxT('-') )
1035 wxSET_ERRNO(ERANGE
);
1042 template<typename T
>
1043 static wxLongLong_t
wxCRT_DoStrtoll(const T
* nptr
, T
** endptr
, int base
)
1046 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1047 wxLongLong_t val
= 0;
1049 if ( sign
== wxT('-') )
1051 if ( uval
<= wxULL(wxINT64_MAX
+1) )
1053 if ( uval
== wxULL(wxINT64_MAX
+1))
1054 val
= -((wxLongLong_t
)wxINT64_MAX
) - 1;
1056 val
= -((wxLongLong_t
)uval
);
1060 wxSET_ERRNO(ERANGE
);
1063 else if ( uval
<= wxINT64_MAX
)
1069 wxSET_ERRNO(ERANGE
);
1075 #ifndef wxCRT_StrtollA
1076 wxLongLong_t
wxCRT_StrtollA(const char* nptr
, char** endptr
, int base
)
1077 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1079 #ifndef wxCRT_StrtollW
1080 wxLongLong_t
wxCRT_StrtollW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1081 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1084 #ifndef wxCRT_StrtoullA
1085 wxULongLong_t
wxCRT_StrtoullA(const char* nptr
, char** endptr
, int base
)
1086 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1088 #ifndef wxCRT_StrtoullW
1089 wxULongLong_t
wxCRT_StrtoullW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1090 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1093 #endif // wxLongLong_t
1095 // ----------------------------------------------------------------------------
1096 // functions which we may need even if !wxUSE_WCHAR_T
1097 // ----------------------------------------------------------------------------
1099 template<typename T
>
1100 static T
*wxCRT_DoStrtok(T
*psz
, const T
*delim
, T
**save_ptr
)
1109 psz
+= wxStrspn(psz
, delim
);
1112 *save_ptr
= (T
*)NULL
;
1117 psz
= wxStrpbrk(psz
, delim
);
1120 *save_ptr
= (T
*)NULL
;
1125 *save_ptr
= psz
+ 1;
1131 #ifndef wxCRT_StrtokA
1132 char *wxCRT_StrtokA(char *psz
, const char *delim
, char **save_ptr
)
1133 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1135 #ifndef wxCRT_StrtokW
1136 wchar_t *wxCRT_StrtokW(wchar_t *psz
, const wchar_t *delim
, wchar_t **save_ptr
)
1137 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1140 // ----------------------------------------------------------------------------
1141 // missing C RTL functions
1142 // ----------------------------------------------------------------------------
1144 #ifdef wxNEED_STRDUP
1146 char *strdup(const char *s
)
1148 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1150 strcpy( dest
, s
) ;
1153 #endif // wxNEED_STRDUP
1155 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1157 void *calloc( size_t num
, size_t size
)
1159 void** ptr
= (void **)malloc(num
* size
);
1160 memset( ptr
, 0, num
* size
);
1164 #endif // __WXWINCE__ <= 211
1166 // ============================================================================
1168 // ============================================================================
1170 #if wxUSE_UNICODE_UTF8
1172 #if !wxUSE_UTF8_LOCALE_ONLY
1173 bool wxLocaleIsUtf8
= false; // the safer setting if not known
1176 static bool wxIsLocaleUtf8()
1178 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1179 // because a) it may be unavailable in some builds and b) has slightly
1180 // different semantics (default locale instead of current)
1182 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1183 // GNU libc provides current character set this way (this conforms to
1185 const char *charset
= nl_langinfo(CODESET
);
1188 // "UTF-8" is used by modern glibc versions, but test other variants
1189 // as well, just in case:
1190 if ( strcmp(charset
, "UTF-8") == 0 ||
1191 strcmp(charset
, "utf-8") == 0 ||
1192 strcmp(charset
, "UTF8") == 0 ||
1193 strcmp(charset
, "utf8") == 0 )
1198 #endif // HAVE_LANGINFO_H
1200 // check if we're running under the "C" locale: it is 7bit subset
1201 // of UTF-8, so it can be safely used with the UTF-8 build:
1202 const char *lc_ctype
= setlocale(LC_CTYPE
, NULL
);
1204 (strcmp(lc_ctype
, "C") == 0 || strcmp(lc_ctype
, "POSIX") == 0) )
1209 // we don't know what charset libc is using, so assume the worst
1214 void wxUpdateLocaleIsUtf8()
1216 #if wxUSE_UTF8_LOCALE_ONLY
1217 if ( !wxIsLocaleUtf8() )
1219 wxLogFatalError(_T("This program requires UTF-8 locale to run."));
1221 #else // !wxUSE_UTF8_LOCALE_ONLY
1222 wxLocaleIsUtf8
= wxIsLocaleUtf8();
1226 #endif // wxUSE_UNICODE_UTF8
1228 // ============================================================================
1229 // wx wrappers for CRT functions
1230 // ============================================================================
1232 #if wxUSE_UNICODE_WCHAR
1233 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW
1234 #elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
1235 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \
1236 return_kw wxLocaleIsUtf8 ? callA : callW
1237 #else // ANSI or UTF8 only
1238 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA
1241 int wxPuts(const wxString
& s
)
1243 CALL_ANSI_OR_UNICODE(return,
1244 wxCRT_PutsA(s
.mb_str()),
1245 wxCRT_PutsW(s
.wc_str()));
1248 int wxFputs(const wxString
& s
, FILE *stream
)
1250 CALL_ANSI_OR_UNICODE(return,
1251 wxCRT_FputsA(s
.mb_str(), stream
),
1252 wxCRT_FputsW(s
.wc_str(), stream
));
1255 int wxFputc(const wxUniChar
& c
, FILE *stream
)
1257 #if !wxUSE_UNICODE // FIXME-UTF8: temporary, remove this with ANSI build
1258 return wxCRT_FputcA((char)c
, stream
);
1260 CALL_ANSI_OR_UNICODE(return,
1261 wxCRT_FputsA(c
.AsUTF8(), stream
),
1262 wxCRT_FputcW((wchar_t)c
, stream
));
1266 #ifdef wxCRT_PerrorA
1268 void wxPerror(const wxString
& s
)
1270 #ifdef wxCRT_PerrorW
1271 CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE
,
1272 wxCRT_PerrorA(s
.mb_str()),
1273 wxCRT_PerrorW(s
.wc_str()));
1275 wxCRT_PerrorA(s
.mb_str());
1279 #endif // wxCRT_PerrorA
1281 wchar_t *wxFgets(wchar_t *s
, int size
, FILE *stream
)
1283 wxCHECK_MSG( s
, NULL
, "empty buffer passed to wxFgets()" );
1285 wxCharBuffer
buf(size
- 1);
1286 // FIXME: this reads too little data if wxConvLibc uses UTF-8 ('size' wide
1287 // characters may be encoded by up to 'size'*4 bytes), but what
1289 if ( wxFgets(buf
.data(), size
, stream
) == NULL
)
1292 if ( wxConvLibc
.ToWChar(s
, size
, buf
, wxNO_LEN
) == wxCONV_FAILED
)
1298 // ----------------------------------------------------------------------------
1299 // wxScanf() and friends
1300 // ----------------------------------------------------------------------------
1302 #ifndef HAVE_NO_VSSCANF // __VISUALC__ and __DMC__ see wx/crt.h
1303 int wxVsscanf(const char *str
, const char *format
, va_list ap
)
1304 { return wxCRT_VsscanfA(str
, format
, ap
); }
1305 int wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
)
1306 { return wxCRT_VsscanfW(str
, format
, ap
); }
1307 int wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
)
1308 { return wxCRT_VsscanfA(str
, format
, ap
); }
1309 int wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
)
1310 { return wxCRT_VsscanfW(str
, format
, ap
); }
1311 int wxVsscanf(const wxString
& str
, const char *format
, va_list ap
)
1312 { return wxCRT_VsscanfA(str
.mb_str(), format
, ap
); }
1313 int wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
)
1314 { return wxCRT_VsscanfW(str
.wc_str(), format
, ap
); }
1315 int wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
)
1316 { return wxCRT_VsscanfA(str
.AsCharBuf(), format
, ap
); }
1317 int wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
)
1318 { return wxCRT_VsscanfW(str
.AsWCharBuf(), format
, ap
); }
1319 #endif // HAVE_NO_VSSCANF