]>
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);
47 #include "wx/msw/wince/time.h"
51 #include "wx/string.h"
53 #include "wx/utils.h" // for wxMin and wxMax
57 #ifdef HAVE_LANGINFO_H
62 // there is no errno.h under CE apparently
63 #define wxSET_ERRNO(value)
67 #define wxSET_ERRNO(value) errno = value
70 #if defined(__MWERKS__) && __MSL__ >= 0x6000
75 #if defined(__DARWIN__)
76 #include "wx/osx/core/cfref.h"
77 #include <CoreFoundation/CFLocale.h>
78 #include "wx/osx/core/cfstring.h"
82 WXDLLIMPEXP_BASE
size_t wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
84 // assume that we have mbsrtowcs() too if we have wcsrtombs()
87 memset(&mbstate
, 0, sizeof(mbstate_t));
92 if (n
) *buf
= wxT('\0');
96 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
98 return wxMbstowcs(buf
, psz
, n
);
102 // note that we rely on common (and required by Unix98 but unfortunately not
103 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
104 // to just get the size of the needed buffer -- this is needed as otherwise
105 // we have no idea about how much space we need and if the CRT doesn't
106 // support it (the only currently known example being Metrowerks, see
107 // wx/crt.h) we don't use its mbstowcs() at all
108 #ifdef HAVE_WCSRTOMBS
109 return mbsrtowcs(NULL
, &psz
, 0, &mbstate
);
111 return wxMbstowcs(NULL
, psz
, 0);
115 WXDLLIMPEXP_BASE
size_t wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
117 #ifdef HAVE_WCSRTOMBS
119 memset(&mbstate
, 0, sizeof(mbstate_t));
124 // glibc2.1 chokes on null input
128 #ifdef HAVE_WCSRTOMBS
129 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
131 return wxWcstombs(buf
, pwz
, n
);
135 #ifdef HAVE_WCSRTOMBS
136 return wcsrtombs(NULL
, &pwz
, 0, &mbstate
);
138 return wxWcstombs(NULL
, pwz
, 0);
142 char* wxSetlocale(int category
, const char *locale
)
145 // FIXME-CE: there is no setlocale() in CE CRT, use SetThreadLocale()?
146 wxUnusedVar(category
);
150 #else // !__WXWINCE__
153 if ( locale
!= NULL
&& locale
[0] == 0 )
155 // the attempt to use newlocale(LC_ALL_MASK, "", NULL);
156 // here in order to deduce the language along the environment vars rules
157 // lead to strange crashes later...
159 // we have to emulate the behaviour under OS X
160 wxCFRef
<CFLocaleRef
> userLocaleRef(CFLocaleCopyCurrent());
161 wxCFStringRef
str(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleLanguageCode
)));
162 wxString langFull
= str
.AsString()+"_";
163 str
.reset(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleCountryCode
)));
164 langFull
+= str
.AsString();
165 rv
= setlocale(category
, langFull
.c_str());
168 rv
= setlocale(category
, locale
);
170 char *rv
= setlocale(category
, locale
);
172 if ( locale
!= NULL
/* setting locale, not querying */ &&
173 rv
/* call was successful */ )
175 wxUpdateLocaleIsUtf8();
178 #endif // __WXWINCE__/!__WXWINCE__
181 // ============================================================================
182 // printf() functions business
183 // ============================================================================
185 // special test mode: define all functions below even if we don't really need
186 // them to be able to test them
196 #define wxNEED_WPRINTF
198 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
);
202 /* Digital Mars adds count to _stprintf (C99) so convert */
203 int wxCRT_SprintfW (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
207 va_start( arglist
, format
);
208 int iLen
= swprintf ( s
, -1, format
, arglist
);
214 // ----------------------------------------------------------------------------
215 // implement the standard IO functions for wide char if libc doesn't have them
216 // ----------------------------------------------------------------------------
219 int wxCRT_FputsW(const wchar_t *ws
, FILE *stream
)
221 wxCharBuffer
buf(wxConvLibc
.cWC2MB(ws
));
225 // counting the number of wide characters written isn't worth the trouble,
226 // simply distinguish between ok and error
227 return wxCRT_FputsA(buf
, stream
) == -1 ? -1 : 0;
229 #endif // !wxCRT_FputsW
232 int wxCRT_PutsW(const wchar_t *ws
)
234 int rc
= wxCRT_FputsW(ws
, stdout
);
237 if ( wxCRT_FputsW(L
"\n", stdout
) == -1 )
245 #endif // !wxCRT_PutsW
248 int /* not wint_t */ wxCRT_FputcW(wchar_t wc
, FILE *stream
)
250 wchar_t ws
[2] = { wc
, L
'\0' };
252 return wxCRT_FputsW(ws
, stream
);
254 #endif // !wxCRT_FputcW
256 // NB: we only implement va_list functions here, the ones taking ... are
257 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
258 // the definitions there to avoid duplicating them here
259 #ifdef wxNEED_WPRINTF
261 // TODO: implement the scanf() functions
262 static int vwscanf(const wchar_t *format
, va_list argptr
)
264 wxFAIL_MSG( wxT("TODO") );
269 static int vfwscanf(FILE *stream
, const wchar_t *format
, va_list argptr
)
271 wxFAIL_MSG( wxT("TODO") );
276 #define vswprintf wxCRT_VsnprintfW
278 static int vfwprintf(FILE *stream
, const wchar_t *format
, va_list argptr
)
281 int rc
= s
.PrintfV(format
, argptr
);
285 // we can't do much better without Unicode support in libc...
286 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
293 static int vwprintf(const wchar_t *format
, va_list argptr
)
295 return wxCRT_VfprintfW(stdout
, format
, argptr
);
298 #endif // wxNEED_WPRINTF
300 #ifdef wxNEED_VSWSCANF
301 static int vswscanf(const wchar_t *ws
, const wchar_t *format
, va_list argptr
)
303 // The best we can do without proper Unicode support in glibc is to
304 // convert the strings into MB representation and run ANSI version
305 // of the function. This doesn't work with %c and %s because of difference
306 // in size of char and wchar_t, though.
308 wxCHECK_MSG( wxStrstr(format
, wxT("%s")) == NULL
, -1,
309 wxT("incomplete vswscanf implementation doesn't allow %s") );
310 wxCHECK_MSG( wxStrstr(format
, wxT("%c")) == NULL
, -1,
311 wxT("incomplete vswscanf implementation doesn't allow %c") );
313 return vsscanf(static_cast<const char*>(wxConvLibc
.cWX2MB(ws
)),
314 wxConvLibc
.cWX2MB(format
), argptr
);
318 // ----------------------------------------------------------------------------
319 // wxPrintf(), wxScanf() and relatives
320 // ----------------------------------------------------------------------------
322 // FIXME-UTF8: do format conversion using (modified) wxFormatConverter in
323 // template wrappers, not here; note that it will needed to
324 // translate all forms of string specifiers to %(l)s for wxPrintf(),
325 // but it only should do what it did in 2.8 for wxScanf()!
327 #ifndef wxCRT_PrintfW
328 int wxCRT_PrintfW( const wchar_t *format
, ... )
331 va_start(argptr
, format
);
333 int ret
= vwprintf( format
, argptr
);
341 #ifndef wxCRT_FprintfW
342 int wxCRT_FprintfW( FILE *stream
, const wchar_t *format
, ... )
345 va_start( argptr
, format
);
347 int ret
= vfwprintf( stream
, format
, argptr
);
355 #ifndef wxCRT_VfprintfW
356 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
)
358 return vfwprintf( stream
, format
, argptr
);
362 #ifndef wxCRT_VprintfW
363 int wxCRT_VprintfW( const wchar_t *format
, va_list argptr
)
365 return vwprintf( format
, argptr
);
369 #ifndef wxCRT_VsprintfW
370 int wxCRT_VsprintfW( wchar_t *str
, const wchar_t *format
, va_list argptr
)
372 // same as for wxSprintf()
373 return vswprintf(str
, INT_MAX
/ 4, format
, argptr
);
378 int wxCRT_ScanfW(const wchar_t *format
, ...)
381 va_start(argptr
, format
);
384 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
385 int ret
= std::vwscanf(format
, argptr
);
387 int ret
= vwscanf(format
, argptr
);
390 int ret
= vwscanf(format
, argptr
);
399 #ifndef wxCRT_SscanfW
400 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...)
403 va_start(argptr
, format
);
406 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
407 int ret
= std::vswscanf(str
, format
, argptr
);
409 int ret
= vswscanf(str
, format
, argptr
);
412 int ret
= vswscanf(str
, format
, argptr
);
421 #ifndef wxCRT_FscanfW
422 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...)
425 va_start(argptr
, format
);
427 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
428 int ret
= std::vfwscanf(stream
, format
, argptr
);
430 int ret
= vfwscanf(stream
, format
, argptr
);
433 int ret
= vfwscanf(stream
, format
, argptr
);
442 #ifndef wxCRT_VsscanfW
443 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list argptr
)
446 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
447 return std::vswscanf(str
, format
, argptr
);
449 return vswscanf(str
, format
, argptr
);
452 return vswscanf(str
, format
, argptr
);
458 // ----------------------------------------------------------------------------
459 // wrappers to printf and scanf function families
460 // ----------------------------------------------------------------------------
462 #if !wxUSE_UTF8_LOCALE_ONLY
463 int wxDoSprintfWchar(char *str
, const wxChar
*format
, ...)
466 va_start(argptr
, format
);
468 int rv
= wxVsprintf(str
, format
, argptr
);
473 #endif // !wxUSE_UTF8_LOCALE_ONLY
475 #if wxUSE_UNICODE_UTF8
476 int wxDoSprintfUtf8(char *str
, const char *format
, ...)
479 va_start(argptr
, format
);
481 int rv
= wxVsprintf(str
, format
, argptr
);
486 #endif // wxUSE_UNICODE_UTF8
490 #if !wxUSE_UTF8_LOCALE_ONLY
491 int wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...)
494 va_start(argptr
, format
);
496 int rv
= wxVsprintf(str
, format
, argptr
);
501 #endif // !wxUSE_UTF8_LOCALE_ONLY
503 #if wxUSE_UNICODE_UTF8
504 int wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...)
507 va_start(argptr
, format
);
509 int rv
= wxVsprintf(str
, format
, argptr
);
514 #endif // wxUSE_UNICODE_UTF8
516 #endif // wxUSE_UNICODE
518 #if !wxUSE_UTF8_LOCALE_ONLY
519 int wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...)
522 va_start(argptr
, format
);
524 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
529 #endif // !wxUSE_UTF8_LOCALE_ONLY
531 #if wxUSE_UNICODE_UTF8
532 int wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...)
535 va_start(argptr
, format
);
537 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
542 #endif // wxUSE_UNICODE_UTF8
546 #if !wxUSE_UTF8_LOCALE_ONLY
547 int wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...)
550 va_start(argptr
, format
);
552 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
557 #endif // !wxUSE_UTF8_LOCALE_ONLY
559 #if wxUSE_UNICODE_UTF8
560 int wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...)
563 va_start(argptr
, format
);
565 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
570 #endif // wxUSE_UNICODE_UTF8
572 #endif // wxUSE_UNICODE
575 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
576 #define vsnprintf wx_fixed_vsnprintf
584 #if !wxUSE_UTF8_LOCALE_ONLY
585 int ConvertStringToBuf(const wxString
& s
, char *out
, size_t outsize
)
587 const wxCharBuffer
buf(s
.mb_str());
589 const size_t len
= buf
.length();
592 memcpy(out
, buf
, (len
+1) * sizeof(char));
594 else // not enough space
596 memcpy(out
, buf
, (outsize
-1) * sizeof(char));
597 out
[outsize
-1] = '\0';
602 #endif // !wxUSE_UTF8_LOCALE_ONLY
604 #if wxUSE_UNICODE_UTF8
605 int ConvertStringToBuf(const wxString
& s
, wchar_t *out
, size_t outsize
)
607 const wxWX2WCbuf
buf(s
.wc_str());
608 size_t len
= s
.length(); // same as buf length for wchar_t*
611 memcpy(out
, buf
, (len
+1) * sizeof(wchar_t));
613 else // not enough space
615 memcpy(out
, buf
, (outsize
-1) * sizeof(wchar_t));
620 #endif // wxUSE_UNICODE_UTF8
622 } // anonymous namespace
625 static size_t PrintfViaString(T
*out
, size_t outsize
,
626 const wxString
& format
, va_list argptr
)
629 s
.PrintfV(format
, argptr
);
631 return ConvertStringToBuf(s
, out
, outsize
);
633 #endif // wxUSE_UNICODE
635 int wxVsprintf(char *str
, const wxString
& format
, va_list argptr
)
637 #if wxUSE_UTF8_LOCALE_ONLY
638 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
640 #if wxUSE_UNICODE_UTF8
641 if ( wxLocaleIsUtf8
)
642 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
646 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
648 return wxCRT_VsprintfA(str
, format
.mb_str(), argptr
);
654 int wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
)
656 #if wxUSE_UNICODE_WCHAR
659 This fails with a bug similar to
660 http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=c++.beta&artnum=680
662 I don't see it being used in the wxWidgets sources at present (oct 2007) CE
664 #pragma message ( "warning ::::: wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) not yet implemented" )
665 wxFAIL_MSG( wxT("TODO") );
669 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
671 #else // wxUSE_UNICODE_UTF8
672 #if !wxUSE_UTF8_LOCALE_ONLY
673 if ( !wxLocaleIsUtf8
)
674 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
677 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
678 #endif // wxUSE_UNICODE_UTF8
680 #endif // wxUSE_UNICODE
682 int wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
)
685 #if wxUSE_UTF8_LOCALE_ONLY
686 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
688 #if wxUSE_UNICODE_UTF8
689 if ( wxLocaleIsUtf8
)
690 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
695 // NB: if this code is called, then wxString::PrintV() would use the
696 // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
698 rv
= PrintfViaString(str
, size
, format
, argptr
);
701 rv
= wxCRT_VsnprintfA(str
, size
, format
.mb_str(), argptr
);
705 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
706 // doesn't nul terminate on truncation.
713 int wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
)
717 #if wxUSE_UNICODE_WCHAR
718 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
719 #else // wxUSE_UNICODE_UTF8
720 #if !wxUSE_UTF8_LOCALE_ONLY
721 if ( !wxLocaleIsUtf8
)
722 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
726 // NB: if this code is called, then wxString::PrintV() would use the
727 // char* version of wxVsnprintf(), so it's safe to use PrintV()
729 rv
= PrintfViaString(str
, size
, format
, argptr
);
731 #endif // wxUSE_UNICODE_UTF8
733 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
734 // doesn't nul terminate on truncation.
739 #endif // wxUSE_UNICODE
742 // ----------------------------------------------------------------------------
743 // ctype.h stuff (currently unused)
744 // ----------------------------------------------------------------------------
746 #ifdef wxNEED_WX_MBSTOWCS
748 WXDLLIMPEXP_BASE
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
758 const char* origin
= in
;
760 while (outlen
-- && *in
)
762 *out
++ = (wchar_t) *in
++;
770 WXDLLIMPEXP_BASE
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
780 const wchar_t* origin
= in
;
782 while (outlen
-- && *in
)
784 *out
++ = (char) *in
++;
792 #endif // wxNEED_WX_MBSTOWCS
794 #ifndef wxCRT_StrdupA
795 WXDLLIMPEXP_BASE
char *wxCRT_StrdupA(const char *s
)
797 return strcpy((char *)malloc(strlen(s
) + 1), s
);
799 #endif // wxCRT_StrdupA
801 #ifndef wxCRT_StrdupW
802 WXDLLIMPEXP_BASE
wchar_t * wxCRT_StrdupW(const wchar_t *pwz
)
804 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
805 wchar_t *ret
= (wchar_t *) malloc(size
);
806 memcpy(ret
, pwz
, size
);
809 #endif // wxCRT_StrdupW
811 #ifndef wxWCHAR_T_IS_WXCHAR16
812 size_t wxStrlen(const wxChar16
*s
)
816 while (*s
!=0) { ++i
; ++s
; };
820 wxChar16
* wxStrdup(const wxChar16
* s
)
822 size_t size
= (wxStrlen(s
) + 1) * sizeof(wxChar16
);
823 wxChar16
*ret
= (wxChar16
*) malloc(size
);
824 memcpy(ret
, s
, size
);
829 #ifndef wxWCHAR_T_IS_WXCHAR32
830 size_t wxStrlen(const wxChar32
*s
)
834 while (*s
!=0) { ++i
; ++s
; };
838 wxChar32
* wxStrdup(const wxChar32
* s
)
840 size_t size
= (wxStrlen(s
) + 1) * sizeof(wxChar32
);
841 wxChar32
*ret
= (wxChar32
*) malloc(size
);
842 memcpy(ret
, s
, size
);
847 #ifndef wxCRT_StricmpA
848 WXDLLIMPEXP_BASE
int wxCRT_StricmpA(const char *psz1
, const char *psz2
)
850 register char c1
, c2
;
852 c1
= wxTolower(*psz1
++);
853 c2
= wxTolower(*psz2
++);
854 } while ( c1
&& (c1
== c2
) );
857 #endif // !defined(wxCRT_StricmpA)
859 #ifndef wxCRT_StricmpW
860 WXDLLIMPEXP_BASE
int wxCRT_StricmpW(const wchar_t *psz1
, const wchar_t *psz2
)
862 register wchar_t c1
, c2
;
864 c1
= wxTolower(*psz1
++);
865 c2
= wxTolower(*psz2
++);
866 } while ( c1
&& (c1
== c2
) );
869 #endif // !defined(wxCRT_StricmpW)
871 #ifndef wxCRT_StrnicmpA
872 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpA(const char *s1
, const char *s2
, size_t n
)
874 // initialize the variables just to suppress stupid gcc warning
875 register char c1
= 0, c2
= 0;
876 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
878 if (c1
< c2
) return -1;
879 if (c1
> c2
) return 1;
883 #endif // !defined(wxCRT_StrnicmpA)
885 #ifndef wxCRT_StrnicmpW
886 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpW(const wchar_t *s1
, const wchar_t *s2
, size_t n
)
888 // initialize the variables just to suppress stupid gcc warning
889 register wchar_t c1
= 0, c2
= 0;
890 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
892 if (c1
< c2
) return -1;
893 if (c1
> c2
) return 1;
897 #endif // !defined(wxCRT_StrnicmpW)
899 // ----------------------------------------------------------------------------
900 // string.h functions
901 // ----------------------------------------------------------------------------
903 // this (and wxCRT_StrncmpW below) are extern "C" because they are needed
904 // by regex code, the rest isn't needed, so it's not declared as extern "C"
905 #ifndef wxCRT_StrlenW
906 extern "C" WXDLLIMPEXP_BASE
size_t wxCRT_StrlenW(const wchar_t *s
)
916 // ----------------------------------------------------------------------------
917 // stdlib.h functions
918 // ----------------------------------------------------------------------------
920 #ifndef wxCRT_GetenvW
921 WXDLLIMPEXP_BASE
wchar_t* wxCRT_GetenvW(const wchar_t *name
)
923 // NB: buffer returned by getenv() is allowed to be overwritten next
924 // time getenv() is called, so it is OK to use static string
925 // buffer to hold the data.
926 static wxWCharBuffer value
;
927 value
= wxConvLibc
.cMB2WC(getenv(wxConvLibc
.cWC2MB(name
)));
930 #endif // !wxCRT_GetenvW
932 #ifndef wxCRT_StrftimeW
933 WXDLLIMPEXP_BASE
size_t
934 wxCRT_StrftimeW(wchar_t *s
, size_t maxsize
, const wchar_t *fmt
, const struct tm
*tm
)
939 wxCharBuffer
buf(maxsize
);
941 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
945 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
949 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
953 wxCRT_StrncpyW(s
, wbuf
, maxsize
);
954 return wxCRT_StrlenW(s
);
956 #endif // !wxCRT_StrftimeW
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 // strtok() functions
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