]>
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/osx/core/cfref.h"
69 #include <CoreFoundation/CFLocale.h>
70 #include "wx/osx/core/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 char* wxSetlocale(int category
, const char *locale
)
139 // FIXME-CE: there is no setlocale() in CE CRT, use SetThreadLocale()?
140 wxUnusedVar(category
);
144 #else // !__WXWINCE__
147 if ( locale
!= NULL
&& locale
[0] == 0 )
149 // the attempt to use newlocale(LC_ALL_MASK, "", NULL);
150 // here in order to deduce the language along the environment vars rules
151 // lead to strange crashes later...
153 // we have to emulate the behaviour under OS X
154 wxCFRef
<CFLocaleRef
> userLocaleRef(CFLocaleCopyCurrent());
155 wxCFStringRef
str(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleLanguageCode
)));
156 wxString langFull
= str
.AsString()+"_";
157 str
.reset(wxCFRetain((CFStringRef
)CFLocaleGetValue(userLocaleRef
, kCFLocaleCountryCode
)));
158 langFull
+= str
.AsString();
159 rv
= setlocale(category
, langFull
.c_str());
162 rv
= setlocale(category
, locale
);
164 char *rv
= setlocale(category
, locale
);
166 if ( locale
!= NULL
/* setting locale, not querying */ &&
167 rv
/* call was successful */ )
169 wxUpdateLocaleIsUtf8();
172 #endif // __WXWINCE__/!__WXWINCE__
175 // ============================================================================
176 // printf() functions business
177 // ============================================================================
179 // special test mode: define all functions below even if we don't really need
180 // them to be able to test them
190 #define wxNEED_WPRINTF
192 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
);
196 /* Digital Mars adds count to _stprintf (C99) so convert */
197 int wxCRT_SprintfW (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
201 va_start( arglist
, format
);
202 int iLen
= swprintf ( s
, -1, format
, arglist
);
208 // ----------------------------------------------------------------------------
209 // implement the standard IO functions for wide char if libc doesn't have them
210 // ----------------------------------------------------------------------------
213 int wxCRT_FputsW(const wchar_t *ws
, FILE *stream
)
215 wxCharBuffer
buf(wxConvLibc
.cWC2MB(ws
));
219 // counting the number of wide characters written isn't worth the trouble,
220 // simply distinguish between ok and error
221 return wxCRT_FputsA(buf
, stream
) == -1 ? -1 : 0;
223 #endif // !wxCRT_FputsW
226 int wxCRT_PutsW(const wchar_t *ws
)
228 int rc
= wxCRT_FputsW(ws
, stdout
);
231 if ( wxCRT_FputsW(L
"\n", stdout
) == -1 )
239 #endif // !wxCRT_PutsW
242 int /* not wint_t */ wxCRT_FputcW(wchar_t wc
, FILE *stream
)
244 wchar_t ws
[2] = { wc
, L
'\0' };
246 return wxCRT_FputsW(ws
, stream
);
248 #endif // !wxCRT_FputcW
250 // NB: we only implement va_list functions here, the ones taking ... are
251 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
252 // the definitions there to avoid duplicating them here
253 #ifdef wxNEED_WPRINTF
255 // TODO: implement the scanf() functions
256 static int vwscanf(const wchar_t *format
, va_list argptr
)
258 wxFAIL_MSG( _T("TODO") );
263 static int vfwscanf(FILE *stream
, const wchar_t *format
, va_list argptr
)
265 wxFAIL_MSG( _T("TODO") );
270 #define vswprintf wxCRT_VsnprintfW
272 static int vfwprintf(FILE *stream
, const wchar_t *format
, va_list argptr
)
275 int rc
= s
.PrintfV(format
, argptr
);
279 // we can't do much better without Unicode support in libc...
280 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
287 static int vwprintf(const wchar_t *format
, va_list argptr
)
289 return wxCRT_VfprintfW(stdout
, format
, argptr
);
292 #endif // wxNEED_WPRINTF
294 #ifdef wxNEED_VSWSCANF
295 static int vswscanf(const wchar_t *ws
, const wchar_t *format
, va_list argptr
)
297 // The best we can do without proper Unicode support in glibc is to
298 // convert the strings into MB representation and run ANSI version
299 // of the function. This doesn't work with %c and %s because of difference
300 // in size of char and wchar_t, though.
302 wxCHECK_MSG( wxStrstr(format
, _T("%s")) == NULL
, -1,
303 _T("incomplete vswscanf implementation doesn't allow %s") );
304 wxCHECK_MSG( wxStrstr(format
, _T("%c")) == NULL
, -1,
305 _T("incomplete vswscanf implementation doesn't allow %c") );
307 return vsscanf(wx_static_cast(const char*, wxConvLibc
.cWX2MB(ws
)),
308 wxConvLibc
.cWX2MB(format
), argptr
);
312 // ----------------------------------------------------------------------------
313 // wxPrintf(), wxScanf() and relatives
314 // ----------------------------------------------------------------------------
316 // FIXME-UTF8: do format conversion using (modified) wxFormatConverter in
317 // template wrappers, not here; note that it will needed to
318 // translate all forms of string specifiers to %(l)s for wxPrintf(),
319 // but it only should do what it did in 2.8 for wxScanf()!
321 #ifndef wxCRT_PrintfW
322 int wxCRT_PrintfW( const wchar_t *format
, ... )
325 va_start(argptr
, format
);
327 int ret
= vwprintf( format
, argptr
);
335 #ifndef wxCRT_FprintfW
336 int wxCRT_FprintfW( FILE *stream
, const wchar_t *format
, ... )
339 va_start( argptr
, format
);
341 int ret
= vfwprintf( stream
, format
, argptr
);
349 #ifndef wxCRT_VfprintfW
350 int wxCRT_VfprintfW( FILE *stream
, const wchar_t *format
, va_list argptr
)
352 return vfwprintf( stream
, format
, argptr
);
356 #ifndef wxCRT_VprintfW
357 int wxCRT_VprintfW( const wchar_t *format
, va_list argptr
)
359 return vwprintf( format
, argptr
);
363 #ifndef wxCRT_VsprintfW
364 int wxCRT_VsprintfW( wchar_t *str
, const wchar_t *format
, va_list argptr
)
366 // same as for wxSprintf()
367 return vswprintf(str
, INT_MAX
/ 4, format
, argptr
);
372 int wxCRT_ScanfW(const wchar_t *format
, ...)
375 va_start(argptr
, format
);
378 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
379 int ret
= std::vwscanf(format
, argptr
);
381 int ret
= vwscanf(format
, argptr
);
384 int ret
= vwscanf(format
, argptr
);
393 #ifndef wxCRT_SscanfW
394 int wxCRT_SscanfW(const wchar_t *str
, const wchar_t *format
, ...)
397 va_start(argptr
, format
);
400 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
401 int ret
= std::vswscanf(str
, format
, argptr
);
403 int ret
= vswscanf(str
, format
, argptr
);
406 int ret
= vswscanf(str
, format
, argptr
);
415 #ifndef wxCRT_FscanfW
416 int wxCRT_FscanfW(FILE *stream
, const wchar_t *format
, ...)
419 va_start(argptr
, format
);
421 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
422 int ret
= std::vfwscanf(stream
, format
, argptr
);
424 int ret
= vfwscanf(stream
, format
, argptr
);
427 int ret
= vfwscanf(stream
, format
, argptr
);
436 #ifndef wxCRT_VsscanfW
437 int wxCRT_VsscanfW(const wchar_t *str
, const wchar_t *format
, va_list argptr
)
440 #if (__DECCXX_VER >= 70100000) && !defined(__STD_CFRONT) && !defined( __NONAMESPACE_STD )
441 return std::vswscanf(str
, format
, argptr
);
443 return vswscanf(str
, format
, argptr
);
446 return vswscanf(str
, format
, argptr
);
452 // ----------------------------------------------------------------------------
453 // wrappers to printf and scanf function families
454 // ----------------------------------------------------------------------------
456 #if !wxUSE_UTF8_LOCALE_ONLY
457 int wxDoSprintfWchar(char *str
, const wxChar
*format
, ...)
460 va_start(argptr
, format
);
462 int rv
= wxVsprintf(str
, format
, argptr
);
467 #endif // !wxUSE_UTF8_LOCALE_ONLY
469 #if wxUSE_UNICODE_UTF8
470 int wxDoSprintfUtf8(char *str
, const char *format
, ...)
473 va_start(argptr
, format
);
475 int rv
= wxVsprintf(str
, format
, argptr
);
480 #endif // wxUSE_UNICODE_UTF8
484 #if !wxUSE_UTF8_LOCALE_ONLY
485 int wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...)
488 va_start(argptr
, format
);
490 int rv
= wxVsprintf(str
, format
, argptr
);
495 #endif // !wxUSE_UTF8_LOCALE_ONLY
497 #if wxUSE_UNICODE_UTF8
498 int wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...)
501 va_start(argptr
, format
);
503 int rv
= wxVsprintf(str
, format
, argptr
);
508 #endif // wxUSE_UNICODE_UTF8
510 #endif // wxUSE_UNICODE
512 #if !wxUSE_UTF8_LOCALE_ONLY
513 int wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...)
516 va_start(argptr
, format
);
518 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
523 #endif // !wxUSE_UTF8_LOCALE_ONLY
525 #if wxUSE_UNICODE_UTF8
526 int wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...)
529 va_start(argptr
, format
);
531 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
536 #endif // wxUSE_UNICODE_UTF8
540 #if !wxUSE_UTF8_LOCALE_ONLY
541 int wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...)
544 va_start(argptr
, format
);
546 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
551 #endif // !wxUSE_UTF8_LOCALE_ONLY
553 #if wxUSE_UNICODE_UTF8
554 int wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...)
557 va_start(argptr
, format
);
559 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
564 #endif // wxUSE_UNICODE_UTF8
566 #endif // wxUSE_UNICODE
569 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
570 #define vsnprintf wx_fixed_vsnprintf
578 #if !wxUSE_UTF8_LOCALE_ONLY
579 int ConvertStringToBuf(const wxString
& s
, char *out
, size_t outsize
)
581 const wxWX2WCbuf buf
= s
.wc_str();
583 size_t len
= wxConvLibc
.FromWChar(out
, outsize
, buf
);
584 if ( len
!= wxCONV_FAILED
)
587 return wxConvLibc
.FromWChar(NULL
, 0, buf
);
589 #endif // !wxUSE_UTF8_LOCALE_ONLY
591 #if wxUSE_UNICODE_UTF8
592 int ConvertStringToBuf(const wxString
& s
, wchar_t *out
, size_t outsize
)
594 const wxWX2WCbuf
buf(s
.wc_str());
595 size_t len
= s
.length(); // same as buf length for wchar_t*
598 memcpy(out
, buf
, (len
+1) * sizeof(wchar_t));
600 else // not enough space
602 memcpy(out
, buf
, (outsize
-1) * sizeof(wchar_t));
607 #endif // wxUSE_UNICODE_UTF8
609 } // anonymous namespace
612 static size_t PrintfViaString(T
*out
, size_t outsize
,
613 const wxString
& format
, va_list argptr
)
616 s
.PrintfV(format
, argptr
);
618 return ConvertStringToBuf(s
, out
, outsize
);
620 #endif // wxUSE_UNICODE
622 int wxVsprintf(char *str
, const wxString
& format
, va_list argptr
)
624 #if wxUSE_UTF8_LOCALE_ONLY
625 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
627 #if wxUSE_UNICODE_UTF8
628 if ( wxLocaleIsUtf8
)
629 return wxCRT_VsprintfA(str
, format
.wx_str(), argptr
);
633 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
635 return wxCRT_VsprintfA(str
, format
.mb_str(), argptr
);
641 int wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
)
643 #if wxUSE_UNICODE_WCHAR
646 This fails with a bug similar to
647 http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=c++.beta&artnum=680
649 I don't see it being used in the wxWidgets sources at present (oct 2007) CE
651 #pragma message ( "warning ::::: wxVsprintf(wchar_t *str, const wxString& format, va_list argptr) not yet implemented" )
652 wxFAIL_MSG( _T("TODO") );
656 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
658 #else // wxUSE_UNICODE_UTF8
659 #if !wxUSE_UTF8_LOCALE_ONLY
660 if ( !wxLocaleIsUtf8
)
661 return wxCRT_VsprintfW(str
, format
.wc_str(), argptr
);
664 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
665 #endif // wxUSE_UNICODE_UTF8
667 #endif // wxUSE_UNICODE
669 int wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
)
672 #if wxUSE_UTF8_LOCALE_ONLY
673 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
675 #if wxUSE_UNICODE_UTF8
676 if ( wxLocaleIsUtf8
)
677 rv
= wxCRT_VsnprintfA(str
, size
, format
.wx_str(), argptr
);
682 // NB: if this code is called, then wxString::PrintV() would use the
683 // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
685 rv
= PrintfViaString(str
, size
, format
, argptr
);
688 rv
= wxCRT_VsnprintfA(str
, size
, format
.mb_str(), argptr
);
692 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
693 // doesn't nul terminate on truncation.
700 int wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
)
704 #if wxUSE_UNICODE_WCHAR
705 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
706 #else // wxUSE_UNICODE_UTF8
707 #if !wxUSE_UTF8_LOCALE_ONLY
708 if ( !wxLocaleIsUtf8
)
709 rv
= wxCRT_VsnprintfW(str
, size
, format
.wc_str(), argptr
);
713 // NB: if this code is called, then wxString::PrintV() would use the
714 // char* version of wxVsnprintf(), so it's safe to use PrintV()
716 rv
= PrintfViaString(str
, size
, format
, argptr
);
718 #endif // wxUSE_UNICODE_UTF8
720 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
721 // doesn't nul terminate on truncation.
726 #endif // wxUSE_UNICODE
730 // ----------------------------------------------------------------------------
731 // ctype.h stuff (currently unused)
732 // ----------------------------------------------------------------------------
734 #ifdef wxNEED_WX_MBSTOWCS
736 WXDLLIMPEXP_BASE
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
746 const char* origin
= in
;
748 while (outlen
-- && *in
)
750 *out
++ = (wchar_t) *in
++;
758 WXDLLIMPEXP_BASE
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
768 const wchar_t* origin
= in
;
770 while (outlen
-- && *in
)
772 *out
++ = (char) *in
++;
780 #endif // wxNEED_WX_MBSTOWCS
782 #ifndef wxCRT_StrdupA
783 WXDLLIMPEXP_BASE
char *wxCRT_StrdupA(const char *s
)
785 return strcpy((char *)malloc(strlen(s
) + 1), s
);
787 #endif // wxCRT_StrdupA
789 #ifndef wxCRT_StrdupW
790 WXDLLIMPEXP_BASE
wchar_t * wxCRT_StrdupW(const wchar_t *pwz
)
792 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
793 wchar_t *ret
= (wchar_t *) malloc(size
);
794 memcpy(ret
, pwz
, size
);
797 #endif // wxCRT_StrdupW
799 #ifndef wxCRT_StricmpA
800 WXDLLIMPEXP_BASE
int wxCRT_StricmpA(const char *psz1
, const char *psz2
)
802 register char c1
, c2
;
804 c1
= wxTolower(*psz1
++);
805 c2
= wxTolower(*psz2
++);
806 } while ( c1
&& (c1
== c2
) );
809 #endif // !defined(wxCRT_StricmpA)
811 #ifndef wxCRT_StricmpW
812 WXDLLIMPEXP_BASE
int wxCRT_StricmpW(const wchar_t *psz1
, const wchar_t *psz2
)
814 register wchar_t c1
, c2
;
816 c1
= wxTolower(*psz1
++);
817 c2
= wxTolower(*psz2
++);
818 } while ( c1
&& (c1
== c2
) );
821 #endif // !defined(wxCRT_StricmpW)
823 #ifndef wxCRT_StrnicmpA
824 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpA(const char *s1
, const char *s2
, size_t n
)
826 // initialize the variables just to suppress stupid gcc warning
827 register char c1
= 0, c2
= 0;
828 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
830 if (c1
< c2
) return -1;
831 if (c1
> c2
) return 1;
835 #endif // !defined(wxCRT_StrnicmpA)
837 #ifndef wxCRT_StrnicmpW
838 WXDLLIMPEXP_BASE
int wxCRT_StrnicmpW(const wchar_t *s1
, const wchar_t *s2
, size_t n
)
840 // initialize the variables just to suppress stupid gcc warning
841 register wchar_t c1
= 0, c2
= 0;
842 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
844 if (c1
< c2
) return -1;
845 if (c1
> c2
) return 1;
849 #endif // !defined(wxCRT_StrnicmpW)
851 // ----------------------------------------------------------------------------
852 // string.h functions
853 // ----------------------------------------------------------------------------
855 // this (and wxCRT_StrncmpW below) are extern "C" because they are needed
856 // by regex code, the rest isn't needed, so it's not declared as extern "C"
857 #ifndef wxCRT_StrlenW
858 extern "C" WXDLLIMPEXP_BASE
size_t wxCRT_StrlenW(const wchar_t *s
)
868 // ----------------------------------------------------------------------------
869 // stdlib.h functions
870 // ----------------------------------------------------------------------------
872 #ifndef wxCRT_GetenvW
873 WXDLLIMPEXP_BASE
wchar_t* wxCRT_GetenvW(const wchar_t *name
)
875 // NB: buffer returned by getenv() is allowed to be overwritten next
876 // time getenv() is called, so it is OK to use static string
877 // buffer to hold the data.
878 static wxWCharBuffer
value((wchar_t*)NULL
);
879 value
= wxConvLibc
.cMB2WC(getenv(wxConvLibc
.cWC2MB(name
)));
882 #endif // !wxCRT_GetenvW
884 #ifndef wxCRT_StrftimeW
885 WXDLLIMPEXP_BASE
size_t
886 wxCRT_StrftimeW(wchar_t *s
, size_t maxsize
, const wchar_t *fmt
, const struct tm
*tm
)
891 wxCharBuffer
buf(maxsize
);
893 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
897 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
901 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
905 wxCRT_StrncpyW(s
, wbuf
, maxsize
);
906 return wxCRT_StrlenW(s
);
908 #endif // !wxCRT_StrftimeW
910 #endif // wxUSE_WCHAR_T
915 wxCRT_StrtoullBase(const T
* nptr
, T
** endptr
, int base
, T
* sign
)
917 wxULongLong_t sum
= 0;
918 wxString
wxstr(nptr
);
919 wxString::const_iterator i
= wxstr
.begin();
920 wxString::const_iterator end
= wxstr
.end();
923 while ( i
!= end
&& wxIsspace(*i
) ) ++i
;
930 if ( c
== wxT('+') || c
== wxT('-') )
938 if ( i
!= end
&& *i
== wxT('0') )
943 if ( *i
== wxT('x') && (base
== 16 || base
== 0) )
963 for ( ; i
!= end
; ++i
)
973 n
= wxTolower(c
) - wxT('a') + 10;
978 if ( n
>= (unsigned int)base
)
979 // Invalid character (for this base)
982 wxULongLong_t prevsum
= sum
;
983 sum
= (sum
* base
) + n
;
994 *endptr
= (T
*)(nptr
+ (i
- wxstr
.begin()));
1000 template<typename T
>
1001 static wxULongLong_t
wxCRT_DoStrtoull(const T
* nptr
, T
** endptr
, int base
)
1004 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1006 if ( sign
== wxT('-') )
1008 wxSET_ERRNO(ERANGE
);
1015 template<typename T
>
1016 static wxLongLong_t
wxCRT_DoStrtoll(const T
* nptr
, T
** endptr
, int base
)
1019 wxULongLong_t uval
= ::wxCRT_StrtoullBase(nptr
, endptr
, base
, &sign
);
1020 wxLongLong_t val
= 0;
1022 if ( sign
== wxT('-') )
1024 if (uval
<= (wxULongLong_t
)wxINT64_MAX
+ 1)
1026 val
= -(wxLongLong_t
)uval
;
1030 wxSET_ERRNO(ERANGE
);
1033 else if ( uval
<= wxINT64_MAX
)
1039 wxSET_ERRNO(ERANGE
);
1045 #ifndef wxCRT_StrtollA
1046 wxLongLong_t
wxCRT_StrtollA(const char* nptr
, char** endptr
, int base
)
1047 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1049 #ifndef wxCRT_StrtollW
1050 wxLongLong_t
wxCRT_StrtollW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1051 { return wxCRT_DoStrtoll(nptr
, endptr
, base
); }
1054 #ifndef wxCRT_StrtoullA
1055 wxULongLong_t
wxCRT_StrtoullA(const char* nptr
, char** endptr
, int base
)
1056 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1058 #ifndef wxCRT_StrtoullW
1059 wxULongLong_t
wxCRT_StrtoullW(const wchar_t* nptr
, wchar_t** endptr
, int base
)
1060 { return wxCRT_DoStrtoull(nptr
, endptr
, base
); }
1063 #endif // wxLongLong_t
1065 // ----------------------------------------------------------------------------
1066 // functions which we may need even if !wxUSE_WCHAR_T
1067 // ----------------------------------------------------------------------------
1069 template<typename T
>
1070 static T
*wxCRT_DoStrtok(T
*psz
, const T
*delim
, T
**save_ptr
)
1079 psz
+= wxStrspn(psz
, delim
);
1082 *save_ptr
= (T
*)NULL
;
1087 psz
= wxStrpbrk(psz
, delim
);
1090 *save_ptr
= (T
*)NULL
;
1095 *save_ptr
= psz
+ 1;
1101 #ifndef wxCRT_StrtokA
1102 char *wxCRT_StrtokA(char *psz
, const char *delim
, char **save_ptr
)
1103 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1105 #ifndef wxCRT_StrtokW
1106 wchar_t *wxCRT_StrtokW(wchar_t *psz
, const wchar_t *delim
, wchar_t **save_ptr
)
1107 { return wxCRT_DoStrtok(psz
, delim
, save_ptr
); }
1110 // ----------------------------------------------------------------------------
1111 // missing C RTL functions
1112 // ----------------------------------------------------------------------------
1114 #ifdef wxNEED_STRDUP
1116 char *strdup(const char *s
)
1118 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1120 strcpy( dest
, s
) ;
1123 #endif // wxNEED_STRDUP
1125 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1127 void *calloc( size_t num
, size_t size
)
1129 void** ptr
= (void **)malloc(num
* size
);
1130 memset( ptr
, 0, num
* size
);
1134 #endif // __WXWINCE__ <= 211
1136 // ============================================================================
1138 // ============================================================================
1140 #if wxUSE_UNICODE_UTF8
1142 #if !wxUSE_UTF8_LOCALE_ONLY
1143 bool wxLocaleIsUtf8
= false; // the safer setting if not known
1146 static bool wxIsLocaleUtf8()
1148 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1149 // because a) it may be unavailable in some builds and b) has slightly
1150 // different semantics (default locale instead of current)
1152 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1153 // GNU libc provides current character set this way (this conforms to
1155 const char *charset
= nl_langinfo(CODESET
);
1158 // "UTF-8" is used by modern glibc versions, but test other variants
1159 // as well, just in case:
1160 if ( strcmp(charset
, "UTF-8") == 0 ||
1161 strcmp(charset
, "utf-8") == 0 ||
1162 strcmp(charset
, "UTF8") == 0 ||
1163 strcmp(charset
, "utf8") == 0 )
1168 #endif // HAVE_LANGINFO_H
1170 // check if we're running under the "C" locale: it is 7bit subset
1171 // of UTF-8, so it can be safely used with the UTF-8 build:
1172 const char *lc_ctype
= setlocale(LC_CTYPE
, NULL
);
1174 (strcmp(lc_ctype
, "C") == 0 || strcmp(lc_ctype
, "POSIX") == 0) )
1179 // we don't know what charset libc is using, so assume the worst
1184 void wxUpdateLocaleIsUtf8()
1186 #if wxUSE_UTF8_LOCALE_ONLY
1187 if ( !wxIsLocaleUtf8() )
1189 wxLogFatalError(_T("This program requires UTF-8 locale to run."));
1191 #else // !wxUSE_UTF8_LOCALE_ONLY
1192 wxLocaleIsUtf8
= wxIsLocaleUtf8();
1196 #endif // wxUSE_UNICODE_UTF8
1198 // ============================================================================
1199 // wx wrappers for CRT functions
1200 // ============================================================================
1202 #if wxUSE_UNICODE_WCHAR
1203 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callW
1204 #elif wxUSE_UNICODE_UTF8 && !wxUSE_UTF8_LOCALE_ONLY
1205 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) \
1206 return_kw wxLocaleIsUtf8 ? callA : callW
1207 #else // ANSI or UTF8 only
1208 #define CALL_ANSI_OR_UNICODE(return_kw, callA, callW) return_kw callA
1211 int wxPuts(const wxString
& s
)
1213 CALL_ANSI_OR_UNICODE(return,
1214 wxCRT_PutsA(s
.mb_str()),
1215 wxCRT_PutsW(s
.wc_str()));
1218 int wxFputs(const wxString
& s
, FILE *stream
)
1220 CALL_ANSI_OR_UNICODE(return,
1221 wxCRT_FputsA(s
.mb_str(), stream
),
1222 wxCRT_FputsW(s
.wc_str(), stream
));
1225 int wxFputc(const wxUniChar
& c
, FILE *stream
)
1227 #if !wxUSE_UNICODE // FIXME-UTF8: temporary, remove this with ANSI build
1228 return wxCRT_FputcA((char)c
, stream
);
1230 CALL_ANSI_OR_UNICODE(return,
1231 wxCRT_FputsA(c
.AsUTF8(), stream
),
1232 wxCRT_FputcW((wchar_t)c
, stream
));
1236 #ifdef wxCRT_PerrorA
1238 void wxPerror(const wxString
& s
)
1240 #ifdef wxCRT_PerrorW
1241 CALL_ANSI_OR_UNICODE(wxEMPTY_PARAMETER_VALUE
,
1242 wxCRT_PerrorA(s
.mb_str()),
1243 wxCRT_PerrorW(s
.wc_str()));
1245 wxCRT_PerrorA(s
.mb_str());
1249 #endif // wxCRT_PerrorA
1251 wchar_t *wxFgets(wchar_t *s
, int size
, FILE *stream
)
1253 wxCHECK_MSG( s
, NULL
, "empty buffer passed to wxFgets()" );
1255 wxCharBuffer
buf(size
- 1);
1256 // FIXME: this reads too little data if wxConvLibc uses UTF-8 ('size' wide
1257 // characters may be encoded by up to 'size'*4 bytes), but what
1259 if ( wxFgets(buf
.data(), size
, stream
) == NULL
)
1262 if ( wxConvLibc
.ToWChar(s
, size
, buf
, wxNO_LEN
) == wxCONV_FAILED
)
1268 // ----------------------------------------------------------------------------
1269 // wxScanf() and friends
1270 // ----------------------------------------------------------------------------
1272 #ifdef HAVE_VSSCANF // __VISUALC__ and __DMC__ see wx/crt.h
1273 int wxVsscanf(const char *str
, const char *format
, va_list ap
)
1274 { return wxCRT_VsscanfA(str
, format
, ap
); }
1275 int wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
)
1276 { return wxCRT_VsscanfW(str
, format
, ap
); }
1277 int wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
)
1278 { return wxCRT_VsscanfA(wx_static_cast(const char*, str
), format
, ap
); }
1279 int wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
)
1280 { return wxCRT_VsscanfW(str
, format
, ap
); }
1281 int wxVsscanf(const wxString
& str
, const char *format
, va_list ap
)
1282 { return wxCRT_VsscanfA(wx_static_cast(const char*, str
.mb_str()), format
, ap
); }
1283 int wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
)
1284 { return wxCRT_VsscanfW(str
.wc_str(), format
, ap
); }
1285 int wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
)
1286 { return wxCRT_VsscanfA(wx_static_cast(const char*, str
.AsCharBuf()), format
, ap
); }
1287 int wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
)
1288 { return wxCRT_VsscanfW(str
.AsWCharBuf(), format
, ap
); }
1289 #endif // HAVE_NO_VSSCANF