]>
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"
23 #include "wx/wxchar.h"
25 #define _ISOC9X_SOURCE 1 // to get vsscanf()
26 #define _BSD_SOURCE 1 // to still get strdup()
36 #include "wx/msw/wince/time.h"
40 #include "wx/string.h"
42 #include "wx/utils.h" // for wxMin and wxMax
46 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
55 // there is no errno.h under CE apparently
56 #define wxSET_ERRNO(value)
60 #define wxSET_ERRNO(value) errno = value
64 #if defined(__MWERKS__) && __MSL__ >= 0x6000
70 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
72 // assume that we have mbsrtowcs() too if we have wcsrtombs()
75 memset(&mbstate
, 0, sizeof(mbstate_t));
80 if (n
) *buf
= wxT('\0');
84 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
86 return wxMbstowcs(buf
, psz
, n
);
90 // note that we rely on common (and required by Unix98 but unfortunately not
91 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
92 // to just get the size of the needed buffer -- this is needed as otherwise
93 // we have no idea about how much space we need and if the CRT doesn't
94 // support it (the only currently known example being Metrowerks, see
95 // wx/wxchar.h) we don't use its mbstowcs() at all
97 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
99 return wxMbstowcs((wchar_t *) NULL
, psz
, 0);
103 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
105 #ifdef HAVE_WCSRTOMBS
107 memset(&mbstate
, 0, sizeof(mbstate_t));
112 // glibc2.1 chokes on null input
116 #ifdef HAVE_WCSRTOMBS
117 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
119 return wxWcstombs(buf
, pwz
, n
);
123 #ifdef HAVE_WCSRTOMBS
124 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
126 return wxWcstombs((char *) NULL
, pwz
, 0);
129 #endif // wxUSE_WCHAR_T
131 bool WXDLLEXPORT
wxOKlibc()
133 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
134 // glibc 2.0 uses UTF-8 even when it shouldn't
136 if ((MB_CUR_MAX
== 2) &&
137 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
139 // this is UTF-8 allright, check whether that's what we want
140 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
141 if ((strlen(cur_locale
) < 4) ||
142 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
143 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
144 // nope, don't use libc conversion
152 // ============================================================================
153 // printf() functions business
154 // ============================================================================
156 // special test mode: define all functions below even if we don't really need
157 // them to be able to test them
167 #define wxNEED_WPRINTF
169 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
);
173 /* Digital Mars adds count to _stprintf (C99) so convert */
175 int wxSprintf (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
179 va_start( arglist
, format
);
180 int iLen
= swprintf ( s
, -1, format
, arglist
);
185 #endif // wxUSE_UNICODE
189 // ----------------------------------------------------------------------------
190 // implement the standard IO functions for wide char if libc doesn't have them
191 // ----------------------------------------------------------------------------
194 int wxFputs(const wchar_t *ws
, FILE *stream
)
196 wxCharBuffer
buf(wxConvLibc
.cWC2MB(ws
));
200 // counting the number of wide characters written isn't worth the trouble,
201 // simply distinguish between ok and error
202 return fputs(buf
, stream
) == -1 ? -1 : 0;
204 #endif // wxNEED_FPUTS
207 int wxPuts(const wxChar
*ws
)
209 int rc
= wxFputs(ws
, stdout
);
212 if ( wxFputs(L
"\n", stdout
) == -1 )
220 #endif // wxNEED_PUTS
223 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
225 wchar_t ws
[2] = { wc
, L
'\0' };
227 return wxFputs(ws
, stream
);
229 #endif // wxNEED_PUTC
231 // NB: we only implement va_list functions here, the ones taking ... are
232 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
233 // the definitions there to avoid duplicating them here
234 #ifdef wxNEED_WPRINTF
236 // TODO: implement the scanf() functions
237 int vwscanf(const wxChar
*format
, va_list argptr
)
239 wxFAIL_MSG( _T("TODO") );
244 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
246 // The best we can do without proper Unicode support in glibc is to
247 // convert the strings into MB representation and run ANSI version
248 // of the function. This doesn't work with %c and %s because of difference
249 // in size of char and wchar_t, though.
251 wxCHECK_MSG( wxStrstr(format
, _T("%s")) == NULL
, -1,
252 _T("incomplete vswscanf implementation doesn't allow %s") );
253 wxCHECK_MSG( wxStrstr(format
, _T("%c")) == NULL
, -1,
254 _T("incomplete vswscanf implementation doesn't allow %c") );
256 return vsscanf(wxConvLibc
.cWX2MB(ws
), wxConvLibc
.cWX2MB(format
), argptr
);
259 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
261 wxFAIL_MSG( _T("TODO") );
266 #define vswprintf wxVsnprintf_
268 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
271 int rc
= s
.PrintfV(format
, argptr
);
275 // we can't do much better without Unicode support in libc...
276 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
283 int vwprintf(const wxChar
*format
, va_list argptr
)
285 return wxVfprintf(stdout
, format
, argptr
);
288 #endif // wxNEED_WPRINTF
290 #ifdef wxNEED_PRINTF_CONVERSION
292 // ----------------------------------------------------------------------------
293 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
294 // ----------------------------------------------------------------------------
297 Here are the gory details. We want to follow the Windows/MS conventions,
302 format specifier results in
303 -----------------------------------
309 format specifier results in
310 -----------------------------------
315 while on POSIX systems we have %C identical to %lc and %c always means char
316 (in any mode) while %lc always means wchar_t,
318 So to use native functions in order to get our semantics we must do the
319 following translations in Unicode mode (nothing to do in ANSI mode):
321 wxWidgets specifier POSIX specifier
322 ----------------------------------------
328 And, of course, the same should be done for %s as well.
331 class wxFormatConverter
334 wxFormatConverter(const wxChar
*format
);
336 // notice that we only translated the string if m_fmtOrig == NULL (as set
337 // by CopyAllBefore()), otherwise we should simply use the original format
338 operator const wxChar
*() const
339 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
342 // copy another character to the translated format: this function does the
343 // copy if we are translating but doesn't do anything at all if we don't,
344 // so we don't create the translated format string at all unless we really
345 // need to (i.e. InsertFmtChar() is called)
346 wxChar
CopyFmtChar(wxChar ch
)
350 // we're translating, do copy
355 // simply increase the count which should be copied by
356 // CopyAllBefore() later if needed
363 // insert an extra character
364 void InsertFmtChar(wxChar ch
)
368 // so far we haven't translated anything yet
377 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
379 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
381 // we won't need it any longer
385 static bool IsFlagChar(wxChar ch
)
387 return ch
== _T('-') || ch
== _T('+') ||
388 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
391 void SkipDigits(const wxChar
**ptpc
)
393 while ( **ptpc
>= _T('0') && **ptpc
<= _T('9') )
394 CopyFmtChar(*(*ptpc
)++);
397 // the translated format
400 // the original format
401 const wxChar
*m_fmtOrig
;
403 // the number of characters already copied
407 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
414 if ( CopyFmtChar(*format
++) == _T('%') )
417 while ( IsFlagChar(*format
) )
418 CopyFmtChar(*format
++);
420 // and possible width
421 if ( *format
== _T('*') )
422 CopyFmtChar(*format
++);
427 if ( *format
== _T('.') )
429 CopyFmtChar(*format
++);
430 if ( *format
== _T('*') )
431 CopyFmtChar(*format
++);
436 // next we can have a size modifier
452 // "ll" has a different meaning!
453 if ( format
[1] != _T('l') )
465 // and finally we should have the type
470 // %C and %hC -> %c and %lC -> %lc
472 CopyFmtChar(_T('l'));
474 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
479 // %c -> %lc but %hc stays %hc and %lc is still %lc
480 if ( size
== Default
)
481 InsertFmtChar(_T('l'));
485 // nothing special to do
486 if ( size
!= Default
)
487 CopyFmtChar(*(format
- 1));
488 CopyFmtChar(*format
++);
494 #else // !wxNEED_PRINTF_CONVERSION
495 // no conversion necessary
496 #define wxFormatConverter(x) (x)
497 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
500 // For testing the format converter
501 wxString
wxConvertFormat(const wxChar
*format
)
503 return wxString(wxFormatConverter(format
));
507 // ----------------------------------------------------------------------------
508 // wxPrintf(), wxScanf() and relatives
509 // ----------------------------------------------------------------------------
511 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
513 int wxCRT_Printf( const wxChar
*format
, ... )
516 va_start(argptr
, format
);
518 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
525 int wxCRT_Fprintf( FILE *stream
, const wxChar
*format
, ... )
528 va_start( argptr
, format
);
530 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
537 int wxCRT_Vfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
539 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
542 int wxCRT_Vprintf( const wxChar
*format
, va_list argptr
)
544 return vwprintf( wxFormatConverter(format
), argptr
);
547 #ifndef wxCRT_Vsnprintf
548 int wxCRT_Vsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
550 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
552 #endif // wxCRT_Vsnprintf
554 int wxCRT_Vsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
556 // same as for wxSprintf()
557 return vswprintf(str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
560 #endif // wxNEED_PRINTF_CONVERSION
563 // ----------------------------------------------------------------------------
564 // wrappers to printf and scanf function families
565 // ----------------------------------------------------------------------------
567 #if !wxUSE_UTF8_LOCALE_ONLY
568 int wxDoSprintfWchar(char *str
, const wxChar
*format
, ...)
571 va_start(argptr
, format
);
573 int rv
= wxVsprintf(str
, format
, argptr
);
578 #endif // !wxUSE_UTF8_LOCALE_ONLY
580 #if wxUSE_UNICODE_UTF8
581 int wxDoSprintfUtf8(char *str
, const char *format
, ...)
584 va_start(argptr
, format
);
586 int rv
= wxVsprintf(str
, format
, argptr
);
591 #endif // wxUSE_UNICODE_UTF8
595 #if !wxUSE_UTF8_LOCALE_ONLY
596 int wxDoSprintfWchar(wchar_t *str
, const wxChar
*format
, ...)
599 va_start(argptr
, format
);
601 int rv
= wxVsprintf(str
, format
, argptr
);
606 #endif // !wxUSE_UTF8_LOCALE_ONLY
608 #if wxUSE_UNICODE_UTF8
609 int wxDoSprintfUtf8(wchar_t *str
, const char *format
, ...)
612 va_start(argptr
, format
);
614 int rv
= wxVsprintf(str
, format
, argptr
);
619 #endif // wxUSE_UNICODE_UTF8
621 #endif // wxUSE_UNICODE
623 #if !wxUSE_UTF8_LOCALE_ONLY
624 int wxDoSnprintfWchar(char *str
, size_t size
, const wxChar
*format
, ...)
627 va_start(argptr
, format
);
629 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
634 #endif // !wxUSE_UTF8_LOCALE_ONLY
636 #if wxUSE_UNICODE_UTF8
637 int wxDoSnprintfUtf8(char *str
, size_t size
, const char *format
, ...)
640 va_start(argptr
, format
);
642 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
647 #endif // wxUSE_UNICODE_UTF8
651 #if !wxUSE_UTF8_LOCALE_ONLY
652 int wxDoSnprintfWchar(wchar_t *str
, size_t size
, const wxChar
*format
, ...)
655 va_start(argptr
, format
);
657 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
662 #endif // !wxUSE_UTF8_LOCALE_ONLY
664 #if wxUSE_UNICODE_UTF8
665 int wxDoSnprintfUtf8(wchar_t *str
, size_t size
, const char *format
, ...)
668 va_start(argptr
, format
);
670 int rv
= wxVsnprintf(str
, size
, format
, argptr
);
675 #endif // wxUSE_UNICODE_UTF8
677 #endif // wxUSE_UNICODE
680 #ifdef HAVE_BROKEN_VSNPRINTF_DECL
681 #define vsnprintf wx_fixed_vsnprintf
686 #if !wxUSE_UTF8_LOCALE_ONLY
687 static int ConvertStringToBuf(const wxString
& s
, char *out
, size_t outsize
)
689 const wxWX2WCbuf buf
= s
.wc_str();
691 size_t len
= wxConvLibc
.FromWChar(out
, outsize
, buf
);
692 if ( len
!= wxCONV_FAILED
)
695 return wxConvLibc
.FromWChar(NULL
, 0, buf
);
697 #endif // !wxUSE_UTF8_LOCALE_ONLY
699 #if wxUSE_UNICODE_UTF8
700 static int ConvertStringToBuf(const wxString
& s
, wchar_t *out
, size_t outsize
)
702 const wxWX2WCbuf
buf(s
.wc_str());
703 size_t len
= wxWcslen(buf
);
705 memcpy(out
, buf
, (len
+1) * sizeof(wchar_t));
706 // else: not enough space
709 #endif // wxUSE_UNICODE_UTF8
712 static size_t PrintfViaString(T
*out
, size_t outsize
,
713 const wxString
& format
, va_list argptr
)
716 s
.PrintfV(format
, argptr
);
718 return ConvertStringToBuf(s
, out
, outsize
);
720 #endif // wxUSE_UNICODE
722 int wxVsprintf(char *str
, const wxString
& format
, va_list argptr
)
724 #if wxUSE_UTF8_LOCALE_ONLY
725 return vsprintf(str
, format
.wx_str(), argptr
);
727 #if wxUSE_UNICODE_UTF8
728 if ( wxLocaleIsUtf8
)
729 return vsprintf(str
, format
.wx_str(), argptr
);
733 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
735 return wxCRT_Vsprintf(str
, format
, argptr
);
741 int wxVsprintf(wchar_t *str
, const wxString
& format
, va_list argptr
)
743 #if wxUSE_UNICODE_WCHAR
744 return wxCRT_Vsprintf(str
, format
, argptr
);
745 #else // wxUSE_UNICODE_UTF8
746 #if !wxUSE_UTF8_LOCALE_ONLY
747 if ( !wxLocaleIsUtf8
)
748 return wxCRT_Vsprintf(str
, format
, argptr
);
751 return PrintfViaString(str
, wxNO_LEN
, format
, argptr
);
752 #endif // wxUSE_UNICODE_UTF8
754 #endif // wxUSE_UNICODE
756 int wxVsnprintf(char *str
, size_t size
, const wxString
& format
, va_list argptr
)
759 #if wxUSE_UTF8_LOCALE_ONLY
760 rv
= vsnprintf(str
, size
, format
.wx_str(), argptr
);
762 #if wxUSE_UNICODE_UTF8
763 if ( wxLocaleIsUtf8
)
764 rv
= vsnprintf(str
, size
, format
.wx_str(), argptr
);
769 // NB: if this code is called, then wxString::PrintV() would use the
770 // wchar_t* version of wxVsnprintf(), so it's safe to use PrintV()
772 rv
= PrintfViaString(str
, size
, format
, argptr
);
775 rv
= wxCRT_Vsnprintf(str
, size
, format
, argptr
);
779 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
780 // doesn't nul terminate on truncation.
787 int wxVsnprintf(wchar_t *str
, size_t size
, const wxString
& format
, va_list argptr
)
791 #if wxUSE_UNICODE_WCHAR
792 rv
= wxCRT_Vsnprintf(str
, size
, format
, argptr
);
793 #else // wxUSE_UNICODE_UTF8
794 #if !wxUSE_UTF8_LOCALE_ONLY
795 if ( !wxLocaleIsUtf8
)
796 rv
= wxCRT_Vsnprintf(str
, size
, format
, argptr
);
800 // NB: if this code is called, then wxString::PrintV() would use the
801 // char* version of wxVsnprintf(), so it's safe to use PrintV()
803 rv
= PrintfViaString(str
, size
, format
, argptr
);
805 #endif // wxUSE_UNICODE_UTF8
807 // VsnprintfTestCase reveals that glibc's implementation of vswprintf
808 // doesn't nul terminate on truncation.
813 #endif // wxUSE_UNICODE
817 // ----------------------------------------------------------------------------
818 // ctype.h stuff (currently unused)
819 // ----------------------------------------------------------------------------
821 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
822 inline WORD
wxMSW_ctype(wxChar ch
)
825 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
829 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
830 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
831 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
832 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
833 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
834 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
835 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
836 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
837 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
838 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
839 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
840 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
841 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
844 #ifdef wxNEED_WX_MBSTOWCS
846 WXDLLEXPORT
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
856 const char* origin
= in
;
858 while (outlen
-- && *in
)
860 *out
++ = (wchar_t) *in
++;
868 WXDLLEXPORT
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
878 const wchar_t* origin
= in
;
880 while (outlen
-- && *in
)
882 *out
++ = (char) *in
++;
890 #endif // wxNEED_WX_MBSTOWCS
892 #if defined(wxNEED_WX_CTYPE_H)
894 #include <CoreFoundation/CoreFoundation.h>
896 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
897 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
898 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
899 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
900 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
901 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
902 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
903 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
904 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
905 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
907 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalnumset
, ch
); }
908 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalphaset
, ch
); }
909 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
910 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfdigitset
, ch
); }
911 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
) && ch
!= ' '; }
912 WXDLLEXPORT
int wxIslower(wxChar ch
) { return CFCharacterSetIsCharacterMember(cflowerset
, ch
); }
913 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
914 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfpunctset
, ch
); }
915 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfspaceset
, ch
); }
916 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfupperset
, ch
); }
917 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxIsdigit(ch
) || (ch
>='a' && ch
<='f') || (ch
>='A' && ch
<='F'); }
918 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)tolower((char)(ch
)); }
919 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)toupper((char)(ch
)); }
921 #endif // wxNEED_WX_CTYPE_H
925 WXDLLEXPORT
char *wxStrdupA(const char *s
)
927 return strcpy((char *)malloc(strlen(s
) + 1), s
);
934 WXDLLEXPORT
wchar_t * wxStrdupW(const wchar_t *pwz
)
936 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
937 wchar_t *ret
= (wchar_t *) malloc(size
);
938 memcpy(ret
, pwz
, size
);
945 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
947 register wxChar c1
, c2
;
949 c1
= wxTolower(*psz1
++);
950 c2
= wxTolower(*psz2
++);
951 } while ( c1
&& (c1
== c2
) );
957 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
959 // initialize the variables just to suppress stupid gcc warning
960 register wxChar c1
= 0, c2
= 0;
961 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
963 if (c1
< c2
) return -1;
964 if (c1
> c2
) return 1;
971 wxWCharBuffer
wxSetlocale_(int category
, const wxChar
*locale
)
973 char *localeOld
= setlocale(category
, wxConvLibc
.cWX2MB(locale
));
975 return wxWCharBuffer(wxConvLibc
.cMB2WC(localeOld
));
978 wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
980 wxWCharBuffer rv
= wxSetlocale_(category
, locale
);
982 wxUpdateLocaleIsUtf8();
985 #else // defined(wxSetlocale_)
986 wxChar
*wxSetlocale(int category
, const wxChar
*locale
)
988 wxChar
*rv
= wxSetlocale_(category
, locale
);
990 wxUpdateLocaleIsUtf8();
993 #endif // wxSetlocale_ defined or not
995 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
996 WXDLLEXPORT
size_t wxWcslen(const wchar_t *s
)
1006 // ----------------------------------------------------------------------------
1007 // string.h functions
1008 // ----------------------------------------------------------------------------
1010 #ifdef wxNEED_WX_STRING_H
1012 // RN: These need to be c externed for the regex lib
1017 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
1020 while (*dest
) dest
++;
1021 while ((*dest
++ = *src
++));
1025 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
1027 // be careful here as the terminating NUL makes part of the string
1037 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
1039 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
1040 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1041 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1045 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
1048 while ((*dest
++ = *src
++));
1052 WXDLLEXPORT
size_t wxStrlen_(const wxChar
*s
)
1062 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
1065 while (*dest
) dest
++;
1066 while (n
&& (*dest
++ = *src
++)) n
--;
1070 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1072 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1074 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1075 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1080 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1083 while (n
&& (*dest
++ = *src
++)) n
--;
1084 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1088 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1090 while (*s
&& !wxStrchr(accept
, *s
))
1093 return *s
? s
: NULL
;
1096 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1098 const wxChar
*ret
= NULL
;
1110 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1113 while (wxStrchr(accept
, *s
++)) len
++;
1117 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1119 wxASSERT_MSG( needle
!= NULL
, _T("NULL argument in wxStrstr") );
1121 // VZ: this is not exactly the most efficient string search algorithm...
1123 const size_t len
= wxStrlen(needle
);
1125 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1127 if ( !wxStrncmp(fnd
, needle
, len
) )
1140 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1142 const wxChar
*start
= nptr
;
1144 // FIXME: only correct for C locale
1145 while (wxIsspace(*nptr
)) nptr
++;
1146 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1147 while (wxIsdigit(*nptr
)) nptr
++;
1148 if (*nptr
== wxT('.')) {
1150 while (wxIsdigit(*nptr
)) nptr
++;
1152 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1154 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1155 while (wxIsdigit(*nptr
)) nptr
++;
1158 wxString
data(nptr
, nptr
-start
);
1159 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1160 char *rdat
= wxMBSTRINGCAST dat
;
1161 double ret
= strtod(dat
, &rdat
);
1163 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1168 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1170 const wxChar
*start
= nptr
;
1172 // FIXME: only correct for C locale
1173 while (wxIsspace(*nptr
)) nptr
++;
1174 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1175 if (((base
== 0) || (base
== 16)) &&
1176 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1180 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1181 else if (base
== 0) base
= 10;
1183 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1184 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1186 wxString
data(start
, nptr
-start
);
1187 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1188 char *rdat
= wxMBSTRINGCAST dat
;
1189 long int ret
= strtol(dat
, &rdat
, base
);
1191 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1196 WXDLLEXPORT
unsigned long int wxStrtoul(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1198 return (unsigned long int) wxStrtol(nptr
, endptr
, base
);
1201 #endif // wxNEED_WX_STRING_H
1203 #ifdef wxNEED_WX_STDIO_H
1204 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1206 char mode_buffer
[10];
1207 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1208 mode_buffer
[i
] = (char) mode
[i
];
1210 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1213 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1215 char mode_buffer
[10];
1216 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1217 mode_buffer
[i
] = (char) mode
[i
];
1219 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1222 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1224 return remove( wxConvFile
.cWX2MB(path
) );
1227 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1229 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1234 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1239 if (str
.ToDouble(& d
))
1244 return atof(wxConvLibc
.cWX2MB(psz
));
1249 #ifdef wxNEED_WX_STDLIB_H
1250 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1252 return atoi(wxConvLibc
.cWX2MB(psz
));
1255 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1257 return atol(wxConvLibc
.cWX2MB(psz
));
1260 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1263 // NB: buffer returned by getenv() is allowed to be overwritten next
1264 // time getenv() is called, so it is OK to use static string
1265 // buffer to hold the data.
1266 static wxWCharBuffer
value((wxChar
*)NULL
);
1267 value
= wxConvLibc
.cMB2WX(getenv(wxConvLibc
.cWX2MB(name
)));
1268 return value
.data();
1270 return getenv(name
);
1274 #endif // wxNEED_WX_STDLIB_H
1276 #ifdef wxNEED_WXSYSTEM
1277 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1279 return system(wxConvLibc
.cWX2MB(psz
));
1281 #endif // wxNEED_WXSYSTEM
1283 #ifdef wxNEED_WX_TIME_H
1285 wxStrftime(wxChar
*s
, size_t maxsize
, const wxChar
*fmt
, const struct tm
*tm
)
1290 wxCharBuffer
buf(maxsize
);
1292 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
1296 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
1300 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
1304 wxStrncpy(s
, wbuf
, maxsize
);
1307 #endif // wxNEED_WX_TIME_H
1310 WXDLLEXPORT wxChar
*wxCtime(const time_t *timep
)
1312 // normally the string is 26 chars but give one more in case some broken
1313 // DOS compiler decides to use "\r\n" instead of "\n" at the end
1314 static wxChar buf
[27];
1316 // ctime() is guaranteed to return a string containing only ASCII
1317 // characters, as its format is always the same for any locale
1318 wxStrncpy(buf
, wxString::FromAscii(ctime(timep
)), WXSIZEOF(buf
));
1319 buf
[WXSIZEOF(buf
) - 1] = _T('\0');
1325 #endif // wxUSE_WCHAR_T
1328 static wxULongLong_t
wxStrtoullBase(const wxChar
* nptr
, wxChar
** endptr
, int base
, wxChar
* sign
)
1330 wxULongLong_t sum
= 0;
1331 wxString
wxstr(nptr
);
1332 wxString::const_iterator i
= wxstr
.begin();
1333 wxString::const_iterator end
= wxstr
.end();
1336 while ( i
!= end
&& wxIsspace(*i
) ) i
++;
1338 // Starts with sign?
1343 if ( c
== wxT('+') || c
== wxT('-') )
1351 if ( i
!= end
&& *i
== wxT('0') )
1356 if ( *i
== wxT('x') && (base
== 16 || base
== 0) )
1364 *endptr
= (wxChar
*) nptr
;
1365 wxSET_ERRNO(EINVAL
);
1376 for ( ; i
!= end
; i
++ )
1381 if ( c
>= wxT('0') )
1383 if ( c
<= wxT('9') )
1386 n
= wxTolower(c
) - wxT('a') + 10;
1391 if ( n
>= (unsigned int)base
)
1392 // Invalid character (for this base)
1395 wxULongLong_t prevsum
= sum
;
1396 sum
= (sum
* base
) + n
;
1398 if ( sum
< prevsum
)
1400 wxSET_ERRNO(ERANGE
);
1407 *endptr
= (wxChar
*)(nptr
+ (i
- wxstr
.begin()));
1413 wxULongLong_t
wxStrtoull(const wxChar
* nptr
, wxChar
** endptr
, int base
)
1416 wxULongLong_t uval
= wxStrtoullBase(nptr
, endptr
, base
, &sign
);
1418 if ( sign
== wxT('-') )
1420 wxSET_ERRNO(ERANGE
);
1427 wxLongLong_t
wxStrtoll(const wxChar
* nptr
, wxChar
** endptr
, int base
)
1430 wxULongLong_t uval
= wxStrtoullBase(nptr
, endptr
, base
, &sign
);
1431 wxLongLong_t val
= 0;
1433 if ( sign
== wxT('-') )
1435 if ( uval
<= wxULL(wxINT64_MAX
+1) )
1437 if ( uval
== wxULL(wxINT64_MAX
+1))
1438 val
= -((wxLongLong_t
)wxINT64_MAX
) - 1;
1440 val
= -((wxLongLong_t
)uval
);
1444 wxSET_ERRNO(ERANGE
);
1447 else if ( uval
<= wxINT64_MAX
)
1453 wxSET_ERRNO(ERANGE
);
1460 // ----------------------------------------------------------------------------
1461 // functions which we may need even if !wxUSE_WCHAR_T
1462 // ----------------------------------------------------------------------------
1466 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
1475 psz
+= wxStrspn(psz
, delim
);
1478 *save_ptr
= (wxChar
*)NULL
;
1479 return (wxChar
*)NULL
;
1483 psz
= wxStrpbrk(psz
, delim
);
1486 *save_ptr
= (wxChar
*)NULL
;
1491 *save_ptr
= psz
+ 1;
1499 // ----------------------------------------------------------------------------
1500 // missing C RTL functions
1501 // ----------------------------------------------------------------------------
1503 #ifdef wxNEED_STRDUP
1505 char *strdup(const char *s
)
1507 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1509 strcpy( dest
, s
) ;
1512 #endif // wxNEED_STRDUP
1514 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1516 void *calloc( size_t num
, size_t size
)
1518 void** ptr
= (void **)malloc(num
* size
);
1519 memset( ptr
, 0, num
* size
);
1523 #endif // __WXWINCE__ <= 211
1527 int wxRemove(const wxChar
*path
)
1529 return ::DeleteFile(path
) == 0;
1535 // ----------------------------------------------------------------------------
1537 // ----------------------------------------------------------------------------
1539 #if wxUSE_UNICODE_UTF8
1541 #if !wxUSE_UTF8_LOCALE_ONLY
1542 bool wxLocaleIsUtf8
= false; // the safer setting if not known
1545 static bool wxIsLocaleUtf8()
1547 // NB: we intentionally don't use wxLocale::GetSystemEncodingName(),
1548 // because a) it may be unavailable in some builds and b) has slightly
1549 // different semantics (default locale instead of current)
1551 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
1552 // GNU libc provides current character set this way (this conforms to
1554 const char *charset
= nl_langinfo(CODESET
);
1557 // "UTF-8" is used by modern glibc versions, but test other variants
1558 // as well, just in case:
1559 if ( strcmp(charset
, "UTF-8") == 0 ||
1560 strcmp(charset
, "utf-8") == 0 ||
1561 strcmp(charset
, "UTF8") == 0 ||
1562 strcmp(charset
, "utf8") == 0 )
1569 // check if we're running under the "C" locale: it is 7bit subset
1570 // of UTF-8, so it can be safely used with the UTF-8 build:
1571 const char *lc_ctype
= setlocale(LC_CTYPE
, NULL
);
1573 (strcmp(lc_ctype
, "C") == 0 || strcmp(lc_ctype
, "POSIX") == 0) )
1578 // we don't know what charset libc is using, so assume the worst
1583 void wxUpdateLocaleIsUtf8()
1585 #if wxUSE_UTF8_LOCALE_ONLY
1586 if ( !wxIsLocaleUtf8() )
1588 wxLogFatalError(_T("This program requires UTF-8 locale to run."));
1590 #else // !wxUSE_UTF8_LOCALE_ONLY
1591 wxLocaleIsUtf8
= wxIsLocaleUtf8();
1595 #endif // wxUSE_UNICODE_UTF8
1597 // ============================================================================
1598 // wx wrappers for CRT functions
1599 // ============================================================================
1602 // ----------------------------------------------------------------------------
1603 // wxScanf() and friends
1604 // ----------------------------------------------------------------------------
1606 // implement vararg function by calling a vfoo function that takes va_list
1607 // argument; use "ap" for the va_list argument in "call" expression
1608 #define IMPL_SCANFUNC(call) \
1610 va_start(ap, format); \
1611 int retval = call; \
1615 int wxScanf(const char *format
, ...)
1616 { IMPL_SCANFUNC( wxCRT_VscanfA(format
, ap
) ); }
1617 int wxScanf(const wchar_t *format
, ...)
1618 { IMPL_SCANFUNC( wxCRT_VscanfW(wxFormatConverter(format
), ap
) ); }
1620 int wxFscanf(FILE *stream
, const char *format
, ...)
1621 { IMPL_SCANFUNC( wxCRT_VfscanfA(stream
, format
, ap
) ); }
1622 int wxFscanf(FILE *stream
, const wchar_t *format
, ...)
1623 { IMPL_SCANFUNC( wxCRT_VfscanfW(stream
, wxFormatConverter(format
), ap
) ); }
1625 int wxSscanf(const char *str
, const char *format
, ...)
1626 { IMPL_SCANFUNC( wxCRT_VsscanfA(str
, format
, ap
) ); }
1627 int wxSscanf(const wchar_t *str
, const wchar_t *format
, ...)
1628 { IMPL_SCANFUNC( wxCRT_VsscanfW(str
, wxFormatConverter(format
), ap
) ); }
1629 int wxSscanf(const wxCharBuffer
& str
, const char *format
, ...)
1630 { IMPL_SCANFUNC( wxCRT_VsscanfA(str
, format
, ap
) ); }
1631 int wxSscanf(const wxWCharBuffer
& str
, const wchar_t *format
, ...)
1632 { IMPL_SCANFUNC( wxCRT_VsscanfW(str
, wxFormatConverter(format
), ap
) ); }
1633 int wxSscanf(const wxString
& str
, const char *format
, ...)
1634 { IMPL_SCANFUNC( wxCRT_VsscanfA(str
.mb_str(), format
, ap
) ); }
1635 int wxSscanf(const wxString
& str
, const wchar_t *format
, ...)
1636 { IMPL_SCANFUNC( wxCRT_VsscanfW(str
.wc_str(), wxFormatConverter(format
), ap
) ); }
1637 int wxSscanf(const wxCStrData
& str
, const char *format
, ...)
1638 { IMPL_SCANFUNC( wxCRT_VsscanfA(str
.AsCharBuf(), format
, ap
) ); }
1639 int wxSscanf(const wxCStrData
& str
, const wchar_t *format
, ...)
1640 { IMPL_SCANFUNC( wxCRT_VsscanfW(str
.AsWCharBuf(), wxFormatConverter(format
), ap
) ); }
1642 int wxVsscanf(const char *str
, const char *format
, va_list ap
)
1643 { return wxCRT_VsscanfA(str
, format
, ap
); }
1644 int wxVsscanf(const wchar_t *str
, const wchar_t *format
, va_list ap
)
1645 { return wxCRT_VsscanfW(str
, wxFormatConverter(format
), ap
); }
1646 int wxVsscanf(const wxCharBuffer
& str
, const char *format
, va_list ap
)
1647 { return wxCRT_VsscanfA(str
, format
, ap
); }
1648 int wxVsscanf(const wxWCharBuffer
& str
, const wchar_t *format
, va_list ap
)
1649 { return wxCRT_VsscanfW(str
, wxFormatConverter(format
), ap
); }
1650 int wxVsscanf(const wxString
& str
, const char *format
, va_list ap
)
1651 { return wxCRT_VsscanfA(str
.mb_str(), format
, ap
); }
1652 int wxVsscanf(const wxString
& str
, const wchar_t *format
, va_list ap
)
1653 { return wxCRT_VsscanfW(str
.wc_str(), wxFormatConverter(format
), ap
); }
1654 int wxVsscanf(const wxCStrData
& str
, const char *format
, va_list ap
)
1655 { return wxCRT_VsscanfA(str
.AsCharBuf(), format
, ap
); }
1656 int wxVsscanf(const wxCStrData
& str
, const wchar_t *format
, va_list ap
)
1657 { return wxCRT_VsscanfW(str
.AsWCharBuf(), wxFormatConverter(format
), ap
); }