]>
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(__DARWIN__)
71 #include "wx/osx/core/cfref.h"
72 #include <CoreFoundation/CFLocale.h>
73 #include "wx/osx/core/cfstring.h"
77 WXDLLIMPEXP_BASE
size_t wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
79 // assume that we have mbsrtowcs() too if we have wcsrtombs()
82 memset(&mbstate
, 0, sizeof(mbstate_t));
87 if (n
) *buf
= wxT('\0');
91 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
93 return wxMbstowcs(buf
, psz
, n
);
97 // Note that we rely on common (and required by Unix98 but unfortunately not
98 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
99 // to just get the size of the needed buffer -- this is needed as otherwise
100 // we have no idea about how much space we need. Currently all supported
101 // compilers do provide it and if they don't, HAVE_WCSRTOMBS shouldn't be
103 #ifdef HAVE_WCSRTOMBS
104 return mbsrtowcs(NULL
, &psz
, 0, &mbstate
);
106 return wxMbstowcs(NULL
, psz
, 0);
110 WXDLLIMPEXP_BASE
size_t wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
112 #ifdef HAVE_WCSRTOMBS
114 memset(&mbstate
, 0, sizeof(mbstate_t));
119 // glibc2.1 chokes on null input
123 #ifdef HAVE_WCSRTOMBS
124 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
126 return wxWcstombs(buf
, pwz
, n
);
130 #ifdef HAVE_WCSRTOMBS
131 return wcsrtombs(NULL
, &pwz
, 0, &mbstate
);
133 return wxWcstombs(NULL
, pwz
, 0);
137 char* wxSetlocale(int category
, const char *locale
)
140 // FIXME-CE: there is no setlocale() in CE CRT, use SetThreadLocale()?
141 wxUnusedVar(category
);
145 #else // !__WXWINCE__
148 if ( locale
!= NULL
&& locale
[0] == 0 )
150 // the attempt to use newlocale(LC_ALL_MASK, "", NULL);
151 // here in order to deduce the language along the environment vars rules
152 // lead to strange crashes later...
154 // we have to emulate the behaviour under OS X
155 wxCFRef
<CFLocaleRef
> userLocaleRef(CFLocaleCopyCurrent());
156 wxCFStringRef
str(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleLanguageCode
)));
157 wxString langFull
= str
.AsString()+"_";
158 str
.reset(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleCountryCode
)));
159 langFull
+= str
.AsString();
160 rv
= setlocale(category
, langFull
.c_str());
163 rv
= setlocale(category
, locale
);
165 char *rv
= setlocale(category
, locale
);
167 if ( locale
!= NULL
/* setting locale, not querying */ &&
168 rv
/* call was successful */ )
170 wxUpdateLocaleIsUtf8();
173 #endif // __WXWINCE__/!__WXWINCE__
176 // ============================================================================
177 // printf() functions business
178 // ============================================================================
180 // special test mode: define all functions below even if we don't really need
181 // them to be able to test them
191 #define wxNEED_WPRINTF
193 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
);
197 /* Digital Mars adds count to _stprintf (C99) so convert */
198 int wxCRT_SprintfW (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
202 va_start( arglist
, format
);
203 int iLen
= swprintf ( s
, -1, format
, arglist
);
209 // ----------------------------------------------------------------------------
210 // implement the standard IO functions for wide char if libc doesn't have them
211 // ----------------------------------------------------------------------------
214 int wxCRT_FputsW(const wchar_t *ws
, FILE *stream
)
216 wxCharBuffer
buf(wxConvLibc
.cWC2MB(ws
));
220 // counting the number of wide characters written isn't worth the trouble,
221 // simply distinguish between ok and error
222 return wxCRT_FputsA(buf
, stream
) == -1 ? -1 : 0;
224 #endif // !wxCRT_FputsW
227 int wxCRT_PutsW(const wchar_t *ws
)
229 int rc
= wxCRT_FputsW(ws
, stdout
);
232 if ( wxCRT_FputsW(L
"\n", stdout
) == -1 )
240 #endif // !wxCRT_PutsW
243 int /* not wint_t */ wxCRT_FputcW(wchar_t wc
, FILE *stream
)
245 wchar_t ws
[2] = { wc
, L
'\0' };
247 return wxCRT_FputsW(ws
, stream
);
249 #endif // !wxCRT_FputcW
251 // NB: we only implement va_list functions here, the ones taking ... are
252 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
253 // the definitions there to avoid duplicating them here
254 #ifdef wxNEED_WPRINTF
256 // TODO: implement the scanf() functions
257 static int vwscanf(const wchar_t *format
, va_list argptr
)
259 wxFAIL_MSG( wxT("TODO") );
264 static int vfwscanf(FILE *stream
, const wchar_t *format
, va_list argptr
)
266 wxFAIL_MSG( wxT("TODO") );
271 #define vswprintf wxCRT_VsnprintfW
273 static int vfwprintf(FILE *stream
, const wchar_t *format
, va_list argptr
)
276 int rc
= s
.PrintfV(format
, argptr
);
280 // we can't do much better without Unicode support in libc...
281 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
288 static int vwprintf(const wchar_t *format
, va_list argptr
)
290 return wxCRT_VfprintfW(stdout
, format
, argptr
);
293 #endif // wxNEED_WPRINTF
295 #ifdef wxNEED_VSWSCANF
296 static int vswscanf(const wchar_t *ws
, const wchar_t *format
, va_list argptr
)
298 // The best we can do without proper Unicode support in glibc is to
299 // convert the strings into MB representation and run ANSI version
300 // of the function. This doesn't work with %c and %s because of difference
301 // in size of char and wchar_t, though.
303 wxCHECK_MSG( wxStrstr(format
, wxT("%s")) == NULL
, -1,
304 wxT("incomplete vswscanf implementation doesn't allow %s") );
305 wxCHECK_MSG( wxStrstr(format
, wxT("%c")) == NULL
, -1,
306 wxT("incomplete vswscanf implementation doesn't allow %c") );
308 return vsscanf(static_cast<const char*>(wxConvLibc
.cWX2MB(ws
)),
309 wxConvLibc
.cWX2MB(format
), argptr
);
313 // ----------------------------------------------------------------------------
314 // wxPrintf(), wxScanf() and relatives
315 // ----------------------------------------------------------------------------
317 // FIXME-UTF8: do format conversion using (modified) wxFormatConverter in
318 // template wrappers, not here; note that it will needed to
319 // translate all forms of string specifiers to %(l)s for wxPrintf(),
320 // but it only should do what it did in 2.8 for wxScanf()!
322 #ifndef wxCRT_PrintfW
323 int wxCRT_PrintfW( const wchar_t *format
, ... )
326 va_start(argptr
, format
);
328 int ret
= vwprintf( format
, argptr
);
336 #ifndef wxCRT_FprintfW
337 int wxCRT_FprintfW( FILE *stream
, const wchar_t *format
, ... )
340 va_start( argptr
, format
);
342 int ret
= vfwprintf( stream
, format
, argptr
);
350 #ifndef wxCRT_VfprintfW
351 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
)
353 return vfwprintf( stream
, format
, argptr
);
357 #ifndef wxCRT_VprintfW
358 int wxCRT_VprintfW( const wchar_t *format
, va_list argptr
)
360 return vwprintf( format
, argptr
);
364 #ifndef wxCRT_VsprintfW
365 int wxCRT_VsprintfW( wchar_t *str
, const wchar_t *format
, va_list argptr
)
367 // same as for wxSprintf()
368 return vswprintf(str
, INT_MAX
/ 4, format
, argptr
);
373 int wxCRT_ScanfW(const wchar_t *format
, ...)
376 va_start(argptr
, format
);
379 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
380 int ret
= std::vwscanf(format
, argptr
);
382 int ret
= vwscanf(format
, argptr
);
385 int ret
= vwscanf(format
, argptr
);
394 #ifndef wxCRT_SscanfW
395 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...)
398 va_start(argptr
, format
);
401 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
402 int ret
= std::vswscanf(str
, format
, argptr
);
404 int ret
= vswscanf(str
, format
, argptr
);
407 int ret
= vswscanf(str
, format
, argptr
);
416 #ifndef wxCRT_FscanfW
417 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...)
420 va_start(argptr
, format
);
422 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
423 int ret
= std::vfwscanf(stream
, format
, argptr
);
425 int ret
= vfwscanf(stream
, format
, argptr
);
428 int ret
= vfwscanf(stream
, format
, argptr
);
437 #ifndef wxCRT_VsscanfW
438 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list argptr
)
441 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
442 return std::vswscanf(str
, format
, argptr
);
444 return vswscanf(str
, format
, argptr
);
447 return vswscanf(str
, format
, argptr
);
453 // ----------------------------------------------------------------------------
454 // wrappers to printf and scanf function families
455 // ----------------------------------------------------------------------------
457 #if !wxUSE_UTF8_LOCALE_ONLY
458 int wxDoSprintfWchar(char *str
, const wxChar
*format
, ...)
461 va_start(argptr
, format
);
463 int rv
= wxVsprintf(str
, format
, argptr
);
468 #endif // !wxUSE_UTF8_LOCALE_ONLY
470 #if wxUSE_UNICODE_UTF8
471 int wxDoSprintfUtf8(char *str
, const char *format
, ...)
474 va_start(argptr
, format
);
476 int rv
= wxVsprintf(str
, format
, argptr
);
481 #endif // wxUSE_UNICODE_UTF8
485 #if !wxUSE_UTF8_LOCALE_ONLY
486 int wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...)
489 va_start(argptr
, format
);
491 int rv
= wxVsprintf(str
, format
, argptr
);
496 #endif // !wxUSE_UTF8_LOCALE_ONLY
498 #if wxUSE_UNICODE_UTF8
499 int wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...)
502 va_start(argptr
, format
);
504 int rv
= wxVsprintf(str
, format
, argptr
);
509 #endif // wxUSE_UNICODE_UTF8
511 #endif // wxUSE_UNICODE
513 #if !wxUSE_UTF8_LOCALE_ONLY
514 int wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...)
517 va_start(argptr
, format
);
519 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
524 #endif // !wxUSE_UTF8_LOCALE_ONLY
526 #if wxUSE_UNICODE_UTF8
527 int wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...)
530 va_start(argptr
, format
);
532 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
537 #endif // wxUSE_UNICODE_UTF8
541 #if !wxUSE_UTF8_LOCALE_ONLY
542 int wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...)
545 va_start(argptr
, format
);
547 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
552 #endif // !wxUSE_UTF8_LOCALE_ONLY
554 #if wxUSE_UNICODE_UTF8
555 int wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...)
558 va_start(argptr
, format
);
560 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
565 #endif // wxUSE_UNICODE_UTF8
567 #endif // wxUSE_UNICODE
570 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
571 #define vsnprintf wx_fixed_vsnprintf
579 #if !wxUSE_UTF8_LOCALE_ONLY
580 int ConvertStringToBuf(const wxString
& s
, char *out
, size_t outsize
)
582 const wxCharBuffer
buf(s
.mb_str());
584 const size_t len
= buf
.length();
587 memcpy(out
, buf
, (len
+1) * sizeof(char));
589 else // not enough space
591 memcpy(out
, buf
, (outsize
-1) * sizeof(char));
592 out
[outsize
-1] = '\0';
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