]>
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
);
838 memcpy(ret
, s
, size
);
843 #ifndef wxCRT_StricmpA
844 WXDLLIMPEXP_BASE
int wxCRT_StricmpA(const char *psz1
, const char *psz2
)
846 register char c1
, c2
;
848 c1
= wxTolower(*psz1
++);
849 c2
= wxTolower(*psz2
++);
850 } while ( c1
&& (c1
== c2
) );
853 #endif // !defined(wxCRT_StricmpA)
855 #ifndef wxCRT_StricmpW
856 WXDLLIMPEXP_BASE
int wxCRT_StricmpW(const wchar_t *psz1
, const wchar_t *psz2
)
858 register wchar_t c1
, c2
;
860 c1
= wxTolower(*psz1
++);
861 c2
= wxTolower(*psz2
++);
862 } while ( c1
&& (c1
== c2
) );
865 #endif // !defined(wxCRT_StricmpW)
867 #ifndef wxCRT_StrnicmpA
868 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpA(const char *s1
, const char *s2
, size_t n
)
870 // initialize the variables just to suppress stupid gcc warning
871 register char c1
= 0, c2
= 0;
872 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
874 if (c1
< c2
) return -1;
875 if (c1
> c2
) return 1;
879 #endif // !defined(wxCRT_StrnicmpA)
881 #ifndef wxCRT_StrnicmpW
882 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpW(const wchar_t *s1
, const wchar_t *s2
, size_t n
)
884 // initialize the variables just to suppress stupid gcc warning
885 register wchar_t c1
= 0, c2
= 0;
886 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
888 if (c1
< c2
) return -1;
889 if (c1
> c2
) return 1;
893 #endif // !defined(wxCRT_StrnicmpW)
895 // ----------------------------------------------------------------------------
896 // string.h functions
897 // ----------------------------------------------------------------------------
899 // this (and wxCRT_StrncmpW below) are extern "C" because they are needed
900 // by regex code, the rest isn't needed, so it's not declared as extern "C"
901 #ifndef wxCRT_StrlenW
902 extern "C" WXDLLIMPEXP_BASE
size_t wxCRT_StrlenW(const wchar_t *s
)
912 // ----------------------------------------------------------------------------
913 // stdlib.h functions
914 // ----------------------------------------------------------------------------
916 #ifndef wxCRT_GetenvW
917 WXDLLIMPEXP_BASE
wchar_t* wxCRT_GetenvW(const wchar_t *name
)
919 // NB: buffer returned by getenv() is allowed to be overwritten next
920 // time getenv() is called, so it is OK to use static string
921 // buffer to hold the data.
922 static wxWCharBuffer value
;
923 value
= wxConvLibc
.cMB2WC(getenv(wxConvLibc
.cWC2MB(name
)));
926 #endif // !wxCRT_GetenvW
928 #ifndef wxCRT_StrftimeW
929 WXDLLIMPEXP_BASE
size_t
930 wxCRT_StrftimeW(wchar_t *s
, size_t maxsize
, const wchar_t *fmt
, const struct tm
*tm
)
935 wxCharBuffer
buf(maxsize
);
937 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
941 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
945 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
949 wxCRT_StrncpyW(s
, wbuf
, maxsize
);
950 return wxCRT_StrlenW(s
);
952 #endif // !wxCRT_StrftimeW
957 wxCRT_StrtoullBase(const T
* nptr
, T
** endptr
, int base
, T
* sign
)
959 wxULongLong_t sum
= 0;
960 wxString
wxstr(nptr
);
961 wxString::const_iterator i
= wxstr
.begin();
962 wxString::const_iterator end
= wxstr
.end();
965 while ( i
!= end
&& wxIsspace(*i
) ) ++i
;
972 if ( c
== wxT('+') || c
== wxT('-') )
979 // Starts with octal or hexadecimal prefix?
980 if ( i
!= end
&& *i
== wxT('0') )
985 if ( (*i
== wxT('x')) || (*i
== wxT('X')) )
987 // Hexadecimal prefix: use base 16 if auto-detecting.
991 // If we do use base 16, just skip "x" as well.
996 else // Not using base 16
998 // Then it's an error.
1000 *endptr
= (T
*) nptr
;
1001 wxSET_ERRNO(EINVAL
);
1005 else if ( base
== 0 )
1017 for ( ; i
!= end
; ++i
)
1027 n
= wxTolower(c
) - wxT('a') + 10;
1032 if ( n
>= (unsigned int)base
)
1033 // Invalid character (for this base)
1036 wxULongLong_t prevsum
= sum
;
1037 sum
= (sum
* base
) + n
;
1039 if ( sum
< prevsum
)
1041 wxSET_ERRNO(ERANGE
);
1048 *endptr
= (T
*)(nptr
+ (i
- wxstr
.begin()));
1054 template<typename T
>
1055 static wxULongLong_t
wxCRT_DoStrtoull(const T
* nptr
, T
** endptr
, int base
)
1058 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1060 if ( sign
== wxT('-') )
1062 wxSET_ERRNO(ERANGE
);
1069 template<typename T
>
1070 static wxLongLong_t
wxCRT_DoStrtoll(const T
* nptr
, T
** endptr
, int base
)
1073 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1074 wxLongLong_t val
= 0;
1076 if ( sign
== wxT('-') )
1078 if (uval
<= (wxULongLong_t
)wxINT64_MAX
+ 1)
1080 val
= -(wxLongLong_t
)uval
;
1084 wxSET_ERRNO(ERANGE
);
1087 else if ( uval
<= wxINT64_MAX
)
1093 wxSET_ERRNO(ERANGE
);
1099 #ifndef wxCRT_StrtollA
1100 wxLongLong_t
wxCRT_StrtollA(const char* nptr
, char** endptr
, int base
)
1101 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1103 #ifndef wxCRT_StrtollW
1104 wxLongLong_t
wxCRT_StrtollW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1105 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1108 #ifndef wxCRT_StrtoullA
1109 wxULongLong_t
wxCRT_StrtoullA(const char* nptr
, char** endptr
, int base
)
1110 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1112 #ifndef wxCRT_StrtoullW
1113 wxULongLong_t
wxCRT_StrtoullW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1114 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1117 #endif // wxLongLong_t
1119 // ----------------------------------------------------------------------------
1120 // strtok() functions
1121 // ----------------------------------------------------------------------------
1123 template<typename T
>
1124 static T
*wxCRT_DoStrtok(T
*psz
, const T
*delim
, T
**save_ptr
)
1133 psz
+= wxStrspn(psz
, delim
);
1141 psz
= wxStrpbrk(psz
, delim
);
1149 *save_ptr
= psz
+ 1;
1155 #ifndef wxCRT_StrtokA
1156 char *wxCRT_StrtokA(char *psz
, const char *delim
, char **save_ptr
)
1157 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1159 #ifndef wxCRT_StrtokW
1160 wchar_t *wxCRT_StrtokW(wchar_t *psz
, const wchar_t *delim
, wchar_t **save_ptr
)
1161 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1164 // ----------------------------------------------------------------------------
1165 // missing C RTL functions
1166 // ----------------------------------------------------------------------------
1168 #ifdef wxNEED_STRDUP
1170 char *strdup(const char *s
)
1172 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1174 strcpy( dest
, s
) ;
1177 #endif // wxNEED_STRDUP
1179 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1181 void *calloc( size_t num
, size_t size
)
1183 void** ptr
= (void **)malloc(num
* size
);
1184 memset( ptr
, 0, num
* size
);
1188 #endif // __WXWINCE__ <= 211
1190 // ============================================================================
1192 // ============================================================================
1194 #if wxUSE_UNICODE_UTF8
1196 #if !wxUSE_UTF8_LOCALE_ONLY
1197 bool wxLocaleIsUtf8
= false; // the safer setting if not known
1200 static bool wxIsLocaleUtf8()
1202 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1203 // because a) it may be unavailable in some builds and b) has slightly
1204 // different semantics (default locale instead of current)
1206 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1207 // GNU libc provides current character set this way (this conforms to
1209 const char *charset
= nl_langinfo(CODESET
);
1212 // "UTF-8" is used by modern glibc versions, but test other variants
1213 // as well, just in case:
1214 if ( strcmp(charset
, "UTF-8") == 0 ||
1215 strcmp(charset
, "utf-8") == 0 ||
1216 strcmp(charset
, "UTF8") == 0 ||
1217 strcmp(charset
, "utf8") == 0 )
1222 #endif // HAVE_LANGINFO_H
1224 // check if we're running under the "C" locale: it is 7bit subset
1225 // of UTF-8, so it can be safely used with the UTF-8 build:
1226 const char *lc_ctype
= setlocale(LC_CTYPE
, NULL
);
1228 (strcmp(lc_ctype
, "C") == 0 || strcmp(lc_ctype
, "POSIX") == 0) )
1233 // we don't know what charset libc is using, so assume the worst
1238 void wxUpdateLocaleIsUtf8()
1240 #if wxUSE_UTF8_LOCALE_ONLY
1241 if ( !wxIsLocaleUtf8() )
1243 wxLogFatalError(wxT("This program requires UTF-8 locale to run."));
1245 #else // !wxUSE_UTF8_LOCALE_ONLY
1246 wxLocaleIsUtf8
= wxIsLocaleUtf8();
1250 #endif // wxUSE_UNICODE_UTF8
1252 // ============================================================================
1253 // wx wrappers for CRT functions
1254 // ============================================================================
1256 #if wxUSE_UNICODE_WCHAR
1257 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW
1258 #elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
1259 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \
1260 return_kw wxLocaleIsUtf8 ? callA : callW
1261 #else // ANSI or UTF8 only
1262 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA
1265 int wxPuts(const wxString
& s
)
1267 // under IRIX putws() takes a non-const argument so use wchar_str() instead
1269 CALL_ANSI_OR_UNICODE(return,
1270 wxCRT_PutsA(s
.mb_str()),
1271 wxCRT_PutsW(s
.wchar_str()));
1274 int wxFputs(const wxString
& s
, FILE *stream
)
1276 CALL_ANSI_OR_UNICODE(return,
1277 wxCRT_FputsA(s
.mb_str(), stream
),
1278 wxCRT_FputsW(s
.wc_str(), stream
));
1281 int wxFputc(const wxUniChar
& c
, FILE *stream
)
1283 #if !wxUSE_UNICODE // FIXME-UTF8: temporary, remove this with ANSI build
1284 return wxCRT_FputcA((char)c
, stream
);
1286 CALL_ANSI_OR_UNICODE(return,
1287 wxCRT_FputsA(c
.AsUTF8(), stream
),
1288 wxCRT_FputcW((wchar_t)c
, stream
));
1292 #ifdef wxCRT_PerrorA
1294 void wxPerror(const wxString
& s
)
1296 #ifdef wxCRT_PerrorW
1297 CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE
,
1298 wxCRT_PerrorA(s
.mb_str()),
1299 wxCRT_PerrorW(s
.wc_str()));
1301 wxCRT_PerrorA(s
.mb_str());
1305 #endif // wxCRT_PerrorA
1307 wchar_t *wxFgets(wchar_t *s
, int size
, FILE *stream
)
1309 wxCHECK_MSG( s
, NULL
, "empty buffer passed to wxFgets()" );
1311 wxCharBuffer
buf(size
- 1);
1312 // FIXME: this reads too little data if wxConvLibc uses UTF-8 ('size' wide
1313 // characters may be encoded by up to 'size'*4 bytes), but what
1315 if ( wxFgets(buf
.data(), size
, stream
) == NULL
)
1318 if ( wxConvLibc
.ToWChar(s
, size
, buf
, wxNO_LEN
) == wxCONV_FAILED
)
1324 // ----------------------------------------------------------------------------
1325 // wxScanf() and friends
1326 // ----------------------------------------------------------------------------
1328 #ifdef HAVE_VSSCANF // __VISUALC__ and __DMC__ see wx/crt.h
1329 int wxVsscanf(const char *str
, const char *format
, va_list ap
)
1330 { return wxCRT_VsscanfA(str
, format
, ap
); }
1331 int wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
)
1332 { return wxCRT_VsscanfW(str
, format
, ap
); }
1333 int wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
)
1334 { return wxCRT_VsscanfA(static_cast<const char*>(str
), format
, ap
); }
1335 int wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
)
1336 { return wxCRT_VsscanfW(str
, format
, ap
); }
1337 int wxVsscanf(const wxString
& str
, const char *format
, va_list ap
)
1338 { return wxCRT_VsscanfA(static_cast<const char*>(str
.mb_str()), format
, ap
); }
1339 int wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
)
1340 { return wxCRT_VsscanfW(str
.wc_str(), format
, ap
); }
1341 int wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
)
1342 { return wxCRT_VsscanfA(static_cast<const char*>(str
.AsCharBuf()), format
, ap
); }
1343 int wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
)
1344 { return wxCRT_VsscanfW(str
.AsWCharBuf(), format
, ap
); }
1345 #endif // HAVE_NO_VSSCANF