1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/wxchar.cpp
3 // Purpose: wxChar implementation
5 // Modified by: Ron Lee
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 #define _ISOC9X_SOURCE 1 // to get vsscanf()
24 #define _BSD_SOURCE 1 // to still get strdup()
34 #include "wx/msw/wince/time.h"
39 #include "wx/wxchar.h"
40 #include "wx/string.h"
44 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
51 #if defined(__MWERKS__) && __MSL__ >= 0x6000
57 #include "wx/mac/private.h"
61 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
63 // assume that we have mbsrtowcs() too if we have wcsrtombs()
66 memset(&mbstate
, 0, sizeof(mbstate_t));
71 if (n
) *buf
= wxT('\0');
75 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
77 return wxMbstowcs(buf
, psz
, n
);
81 // note that we rely on common (and required by Unix98 but unfortunately not
82 // C99) extension which allows to call mbs(r)towcs() with NULL output pointer
83 // to just get the size of the needed buffer -- this is needed as otherwise
84 // we have no idea about how much space we need and if the CRT doesn't
85 // support it (the only currently known example being Metrowerks, see
86 // wx/wxchar.h) we don't use its mbstowcs() at all
88 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
90 return wxMbstowcs((wchar_t *) NULL
, psz
, 0);
94 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
98 memset(&mbstate
, 0, sizeof(mbstate_t));
103 // glibc2.1 chokes on null input
107 #ifdef HAVE_WCSRTOMBS
108 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
110 return wxWcstombs(buf
, pwz
, n
);
114 #ifdef HAVE_WCSRTOMBS
115 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
117 return wxWcstombs((char *) NULL
, pwz
, 0);
120 #endif // wxUSE_WCHAR_T
122 bool WXDLLEXPORT
wxOKlibc()
124 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
125 // glibc 2.0 uses UTF-8 even when it shouldn't
127 if ((MB_CUR_MAX
== 2) &&
128 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
130 // this is UTF-8 allright, check whether that's what we want
131 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
132 if ((strlen(cur_locale
) < 4) ||
133 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
134 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
135 // nope, don't use libc conversion
143 // ============================================================================
144 // printf() functions business
145 // ============================================================================
147 // special test mode: define all functions below even if we don't really need
148 // them to be able to test them
159 #define wxNEED_WPRINTF
161 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
);
164 // ----------------------------------------------------------------------------
165 // implement [v]snprintf() if the system doesn't provide a safe one
166 // ----------------------------------------------------------------------------
168 #if !defined(wxVsnprintf_)
169 int WXDLLEXPORT
wxVsnprintf_(wxChar
*buf
, size_t lenMax
,
170 const wxChar
*format
, va_list argptr
)
172 // buffer to avoid dynamic memory allocation each time for small strings
173 char szScratch
[1024];
175 // number of characters in the buffer so far, must be less than lenMax
178 for ( size_t n
= 0; ; n
++ )
180 const wxChar chCur
= format
[n
];
182 if ( chCur
== wxT('%') )
184 static char s_szFlags
[256] = "%";
186 bool adj_left
= false,
191 size_t min_width
= 0,
192 max_width
= wxSTRING_MAXLEN
;
197 if (in_prec && !prec_dot) \
199 s_szFlags[flagofs++] = '.'; \
203 #define APPEND_CH(ch) \
205 if ( lenCur == lenMax ) \
208 buf[lenCur++] = ch; \
211 #define APPEND_STR(s) \
213 for ( const wxChar *p = s; *p; p++ ) \
220 const wxChar ch
= format
[++n
];
240 s_szFlags
[flagofs
++] = ch
;
246 s_szFlags
[flagofs
++] = ch
;
254 // dot will be auto-added to s_szFlags if non-negative
261 s_szFlags
[flagofs
++] = ch
;
267 s_szFlags
[flagofs
++] = ch
;
274 s_szFlags
[flagofs
++] = ch
;
280 s_szFlags
[flagofs
++] = ch
;
285 int len
= va_arg(argptr
, int);
296 adj_left
= !adj_left
;
297 s_szFlags
[flagofs
++] = '-';
302 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
306 case wxT('1'): case wxT('2'): case wxT('3'):
307 case wxT('4'): case wxT('5'): case wxT('6'):
308 case wxT('7'): case wxT('8'): case wxT('9'):
312 while ( (format
[n
] >= wxT('0')) &&
313 (format
[n
] <= wxT('9')) )
315 s_szFlags
[flagofs
++] = format
[n
];
316 len
= len
*10 + (format
[n
] - wxT('0'));
325 n
--; // the main loop pre-increments n again
336 s_szFlags
[flagofs
++] = ch
;
337 s_szFlags
[flagofs
] = '\0';
340 int val
= va_arg(argptr
, int);
341 ::sprintf(szScratch
, s_szFlags
, val
);
345 // NB: 'short int' value passed through '...'
346 // is promoted to 'int', so we have to get
347 // an int from stack even if we need a short
348 short int val
= (short int) va_arg(argptr
, int);
349 ::sprintf(szScratch
, s_szFlags
, val
);
353 long int val
= va_arg(argptr
, long int);
354 ::sprintf(szScratch
, s_szFlags
, val
);
359 long long int val
= va_arg(argptr
, long long int);
360 ::sprintf(szScratch
, s_szFlags
, val
);
362 long int val
= va_arg(argptr
, long int);
363 ::sprintf(szScratch
, s_szFlags
, val
);
364 #endif // long long/!long long
368 size_t val
= va_arg(argptr
, size_t);
369 ::sprintf(szScratch
, s_szFlags
, val
);
373 const wxMB2WXbuf tmp
=
374 wxConvLibc
.cMB2WX(szScratch
);
387 s_szFlags
[flagofs
++] = ch
;
388 s_szFlags
[flagofs
] = '\0';
391 long double val
= va_arg(argptr
, long double);
392 ::sprintf(szScratch
, s_szFlags
, val
);
396 double val
= va_arg(argptr
, double);
397 ::sprintf(szScratch
, s_szFlags
, val
);
401 const wxMB2WXbuf tmp
=
402 wxConvLibc
.cMB2WX(szScratch
);
411 void *val
= va_arg(argptr
, void *);
413 s_szFlags
[flagofs
++] = ch
;
414 s_szFlags
[flagofs
] = '\0';
415 ::sprintf(szScratch
, s_szFlags
, val
);
417 const wxMB2WXbuf tmp
=
418 wxConvLibc
.cMB2WX(szScratch
);
427 int val
= va_arg(argptr
, int);
431 const char buf
[2] = { val
, 0 };
432 val
= wxString(buf
, wxConvLibc
)[0u];
437 const wchar_t buf
[2] = { val
, 0 };
438 val
= wxString(buf
, wxConvLibc
)[0u];
444 for (i
= 1; i
< min_width
; i
++)
450 for (i
= 1; i
< min_width
; i
++)
459 const wxChar
*val
= NULL
;
465 // wx extension: we'll let %hs mean non-Unicode
467 char *v
= va_arg(argptr
, char *);
470 val
= s
= wxString(v
, wxConvLibc
);
478 // %ls means Unicode strings
479 wchar_t *v
= va_arg(argptr
, wchar_t *);
482 val
= s
= wxString(v
, wxConvLibc
);
487 val
= va_arg(argptr
, wxChar
*);
495 val
[len
] && (len
< max_width
);
499 else if (max_width
>= 6)
513 for (i
= len
; i
< min_width
; i
++)
516 for (i
= 0; i
< len
; i
++)
520 for (i
= len
; i
< min_width
; i
++)
530 int *val
= va_arg(argptr
, int *);
535 short int *val
= va_arg(argptr
, short int *);
540 long int *val
= va_arg(argptr
, long int *);
547 // bad format, leave unchanged
573 #endif // !wxVsnprintfA
575 #if !defined(wxSnprintf_)
576 int WXDLLEXPORT
wxSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
579 va_start(argptr
, format
);
581 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
587 #endif // wxSnprintf_
590 /* Digital Mars adds count to _stprintf (C99) so convert */
592 int wxSprintf (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
596 va_start( arglist
, format
);
597 int iLen
= swprintf ( s
, -1, format
, arglist
);
602 #endif // wxUSE_UNICODE
606 // ----------------------------------------------------------------------------
607 // implement the standard IO functions for wide char if libc doesn't have them
608 // ----------------------------------------------------------------------------
611 int wxFputs(const wchar_t *ws
, FILE *stream
)
613 // counting the number of wide characters written isn't worth the trouble,
614 // simply distinguish between ok and error
615 return fputs(wxConvLibc
.cWC2MB(ws
), stream
) == -1 ? -1 : 0;
617 #endif // wxNEED_FPUTS
620 int wxPuts(const wxChar
*ws
)
622 int rc
= wxFputs(ws
, stdout
);
625 if ( wxFputs(L
"\n", stdout
) == -1 )
633 #endif // wxNEED_PUTS
636 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
638 wchar_t ws
[2] = { wc
, L
'\0' };
640 return wxFputs(ws
, stream
);
642 #endif // wxNEED_PUTC
644 // NB: we only implement va_list functions here, the ones taking ... are
645 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
646 // the definitions there to avoid duplicating them here
647 #ifdef wxNEED_WPRINTF
649 // TODO: implement the scanf() functions
650 int vwscanf(const wxChar
*format
, va_list argptr
)
652 wxFAIL_MSG( _T("TODO") );
657 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
659 // The best we can do without proper Unicode support in glibc is to
660 // convert the strings into MB representation and run ANSI version
661 // of the function. This doesn't work with %c and %s because of difference
662 // in size of char and wchar_t, though.
664 wxCHECK_MSG( wxStrstr(format
, _T("%s")) == NULL
, -1,
665 _T("incomplete vswscanf implementation doesn't allow %s") );
666 wxCHECK_MSG( wxStrstr(format
, _T("%c")) == NULL
, -1,
667 _T("incomplete vswscanf implementation doesn't allow %c") );
670 wxVaCopy(argcopy
, argptr
);
671 return vsscanf(wxConvLibc
.cWX2MB(ws
), wxConvLibc
.cWX2MB(format
), argcopy
);
674 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
676 wxFAIL_MSG( _T("TODO") );
681 #define vswprintf wxVsnprintf_
683 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
686 int rc
= s
.PrintfV(format
, argptr
);
690 // we can't do much better without Unicode support in libc...
691 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
698 int vwprintf(const wxChar
*format
, va_list argptr
)
700 return wxVfprintf(stdout
, format
, argptr
);
703 #endif // wxNEED_WPRINTF
705 #ifdef wxNEED_PRINTF_CONVERSION
707 // ----------------------------------------------------------------------------
708 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
709 // ----------------------------------------------------------------------------
712 Here are the gory details. We want to follow the Windows/MS conventions,
717 format specifier results in
718 -----------------------------------
724 format specifier results in
725 -----------------------------------
730 while on POSIX systems we have %C identical to %lc and %c always means char
731 (in any mode) while %lc always means wchar_t,
733 So to use native functions in order to get our semantics we must do the
734 following translations in Unicode mode (nothing to do in ANSI mode):
736 wxWidgets specifier POSIX specifier
737 ----------------------------------------
743 And, of course, the same should be done for %s as well.
746 class wxFormatConverter
749 wxFormatConverter(const wxChar
*format
);
751 // notice that we only translated the string if m_fmtOrig == NULL (as set
752 // by CopyAllBefore()), otherwise we should simply use the original format
753 operator const wxChar
*() const
754 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
757 // copy another character to the translated format: this function does the
758 // copy if we are translating but doesn't do anything at all if we don't,
759 // so we don't create the translated format string at all unless we really
760 // need to (i.e. InsertFmtChar() is called)
761 wxChar
CopyFmtChar(wxChar ch
)
765 // we're translating, do copy
770 // simply increase the count which should be copied by
771 // CopyAllBefore() later if needed
778 // insert an extra character
779 void InsertFmtChar(wxChar ch
)
783 // so far we haven't translated anything yet
792 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
794 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
796 // we won't need it any longer
800 static bool IsFlagChar(wxChar ch
)
802 return ch
== _T('-') || ch
== _T('+') ||
803 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
806 void SkipDigits(const wxChar
**ptpc
)
808 while ( **ptpc
>= _T('0') && **ptpc
<= _T('9') )
809 CopyFmtChar(*(*ptpc
)++);
812 // the translated format
815 // the original format
816 const wxChar
*m_fmtOrig
;
818 // the number of characters already copied
822 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
829 if ( CopyFmtChar(*format
++) == _T('%') )
832 while ( IsFlagChar(*format
) )
833 CopyFmtChar(*format
++);
835 // and possible width
836 if ( *format
== _T('*') )
837 CopyFmtChar(*format
++);
842 if ( *format
== _T('.') )
844 CopyFmtChar(*format
++);
845 if ( *format
== _T('*') )
846 CopyFmtChar(*format
++);
851 // next we can have a size modifier
867 // "ll" has a different meaning!
868 if ( format
[1] != _T('l') )
880 // and finally we should have the type
885 // %C and %hC -> %c and %lC -> %lc
887 CopyFmtChar(_T('l'));
889 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
894 // %c -> %lc but %hc stays %hc and %lc is still %lc
895 if ( size
== Default
)
896 InsertFmtChar(_T('l'));
900 // nothing special to do
901 if ( size
!= Default
)
902 CopyFmtChar(*(format
- 1));
903 CopyFmtChar(*format
++);
909 #else // !wxNEED_PRINTF_CONVERSION
910 // no conversion necessary
911 #define wxFormatConverter(x) (x)
912 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
915 // For testing the format converter
916 wxString
wxConvertFormat(const wxChar
*format
)
918 return wxString(wxFormatConverter(format
));
922 // ----------------------------------------------------------------------------
923 // wxPrintf(), wxScanf() and relatives
924 // ----------------------------------------------------------------------------
926 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
928 int wxScanf( const wxChar
*format
, ... )
931 va_start(argptr
, format
);
933 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
940 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... )
943 va_start(argptr
, format
);
945 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
952 int wxFscanf( FILE *stream
, const wxChar
*format
, ... )
955 va_start(argptr
, format
);
956 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
963 int wxPrintf( const wxChar
*format
, ... )
966 va_start(argptr
, format
);
968 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
976 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... )
979 va_start(argptr
, format
);
981 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
989 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... )
992 va_start(argptr
, format
);
994 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
995 // it's safe to implement this one in terms of it
996 wxString
s(wxString::FormatV(format
, argptr
));
1004 int wxFprintf( FILE *stream
, const wxChar
*format
, ... )
1007 va_start( argptr
, format
);
1009 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
1016 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
1018 return vswscanf( str
, wxFormatConverter(format
), argptr
);
1021 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
1023 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
1026 int wxVprintf( const wxChar
*format
, va_list argptr
)
1028 return vwprintf( wxFormatConverter(format
), argptr
);
1032 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
1034 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
1036 #endif // wxVsnprintf
1038 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
1040 // same as for wxSprintf()
1041 return vswprintf(str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
1044 #endif // wxNEED_PRINTF_CONVERSION
1048 // ----------------------------------------------------------------------------
1049 // ctype.h stuff (currently unused)
1050 // ----------------------------------------------------------------------------
1052 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
1053 inline WORD
wxMSW_ctype(wxChar ch
)
1056 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
1060 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
1061 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
1062 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
1063 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
1064 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
1065 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
1066 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
1067 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
1068 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
1069 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
1070 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
1071 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
1072 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
1075 #ifdef wxNEED_WX_MBSTOWCS
1077 WXDLLEXPORT
size_t wxMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
1087 const char* origin
= in
;
1089 while (outlen
-- && *in
)
1091 *out
++ = (wchar_t) *in
++;
1099 WXDLLEXPORT
size_t wxWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
1109 const wchar_t* origin
= in
;
1111 while (outlen
-- && *in
)
1113 *out
++ = (char) *in
++;
1121 #endif // wxNEED_WX_MBSTOWCS
1123 #if defined(wxNEED_WX_CTYPE_H)
1125 #include <CoreFoundation/CoreFoundation.h>
1127 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
1128 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
1129 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
1130 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
1131 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
1132 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
1133 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
1134 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
1135 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
1136 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
1138 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalnumset
, ch
); }
1139 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalphaset
, ch
); }
1140 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
1141 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfdigitset
, ch
); }
1142 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
) && ch
!= ' '; }
1143 WXDLLEXPORT
int wxIslower(wxChar ch
) { return CFCharacterSetIsCharacterMember(cflowerset
, ch
); }
1144 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
1145 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfpunctset
, ch
); }
1146 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfspaceset
, ch
); }
1147 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfupperset
, ch
); }
1148 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxIsdigit(ch
) || (ch
>='a' && ch
<='f') || (ch
>='A' && ch
<='F'); }
1149 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)tolower((char)(ch
)); }
1150 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)toupper((char)(ch
)); }
1152 #endif // wxNEED_WX_CTYPE_H
1156 WXDLLEXPORT
char *wxStrdupA(const char *s
)
1158 return strcpy((char *)malloc(strlen(s
) + 1), s
);
1165 WXDLLEXPORT
wchar_t * wxStrdupW(const wchar_t *pwz
)
1167 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
1168 wchar_t *ret
= (wchar_t *) malloc(size
);
1169 memcpy(ret
, pwz
, size
);
1176 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
1178 register wxChar c1
, c2
;
1180 c1
= wxTolower(*psz1
++);
1181 c2
= wxTolower(*psz2
++);
1182 } while ( c1
&& (c1
== c2
) );
1188 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1190 // initialize the variables just to suppress stupid gcc warning
1191 register wxChar c1
= 0, c2
= 0;
1192 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
1194 if (c1
< c2
) return -1;
1195 if (c1
> c2
) return 1;
1202 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
1204 char *localeOld
= setlocale(category
, wxConvLibc
.cWX2MB(locale
));
1206 return wxWCharBuffer(wxConvLibc
.cMB2WC(localeOld
));
1210 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
1211 WXDLLEXPORT
size_t wxWcslen(const wchar_t *s
)
1221 // ----------------------------------------------------------------------------
1222 // string.h functions
1223 // ----------------------------------------------------------------------------
1225 #ifdef wxNEED_WX_STRING_H
1227 // RN: These need to be c externed for the regex lib
1232 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
1235 while (*dest
) dest
++;
1236 while ((*dest
++ = *src
++));
1240 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
1242 // be careful here as the terminating NUL makes part of the string
1252 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
1254 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
1255 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1256 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1260 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
1263 while ((*dest
++ = *src
++));
1267 WXDLLEXPORT
size_t wxStrlen_(const wxChar
*s
)
1277 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
1280 while (*dest
) dest
++;
1281 while (n
&& (*dest
++ = *src
++)) n
--;
1285 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1287 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1289 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1290 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1295 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1298 while (n
&& (*dest
++ = *src
++)) n
--;
1299 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1303 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1305 while (*s
&& !wxStrchr(accept
, *s
))
1308 return *s
? s
: NULL
;
1311 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1313 const wxChar
*ret
= NULL
;
1325 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1328 while (wxStrchr(accept
, *s
++)) len
++;
1332 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1334 wxASSERT_MSG( needle
!= NULL
, _T("NULL argument in wxStrstr") );
1336 // VZ: this is not exactly the most efficient string search algorithm...
1338 const size_t len
= wxStrlen(needle
);
1340 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1342 if ( !wxStrncmp(fnd
, needle
, len
) )
1355 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1357 const wxChar
*start
= nptr
;
1359 // FIXME: only correct for C locale
1360 while (wxIsspace(*nptr
)) nptr
++;
1361 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1362 while (wxIsdigit(*nptr
)) nptr
++;
1363 if (*nptr
== wxT('.')) {
1365 while (wxIsdigit(*nptr
)) nptr
++;
1367 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1369 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1370 while (wxIsdigit(*nptr
)) nptr
++;
1373 wxString
data(nptr
, nptr
-start
);
1374 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1375 char *rdat
= wxMBSTRINGCAST dat
;
1376 double ret
= strtod(dat
, &rdat
);
1378 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1383 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1385 const wxChar
*start
= nptr
;
1387 // FIXME: only correct for C locale
1388 while (wxIsspace(*nptr
)) nptr
++;
1389 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1390 if (((base
== 0) || (base
== 16)) &&
1391 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1395 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1396 else if (base
== 0) base
= 10;
1398 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1399 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1401 wxString
data(start
, nptr
-start
);
1402 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
1403 char *rdat
= wxMBSTRINGCAST dat
;
1404 long int ret
= strtol(dat
, &rdat
, base
);
1406 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1411 WXDLLEXPORT
unsigned long int wxStrtoul(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1413 return (unsigned long int) wxStrtol(nptr
, endptr
, base
);
1416 #endif // wxNEED_WX_STRING_H
1418 #ifdef wxNEED_WX_STDIO_H
1419 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1421 char mode_buffer
[10];
1422 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1423 mode_buffer
[i
] = (char) mode
[i
];
1425 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1428 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1430 char mode_buffer
[10];
1431 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1432 mode_buffer
[i
] = (char) mode
[i
];
1434 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1437 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1439 return remove( wxConvFile
.cWX2MB(path
) );
1442 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1444 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1449 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1454 if (str
.ToDouble(& d
))
1459 return atof(wxConvLibc
.cWX2MB(psz
));
1464 #ifdef wxNEED_WX_STDLIB_H
1465 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1467 return atoi(wxConvLibc
.cWX2MB(psz
));
1470 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1472 return atol(wxConvLibc
.cWX2MB(psz
));
1475 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1478 // NB: buffer returned by getenv() is allowed to be overwritten next
1479 // time getenv() is called, so it is OK to use static string
1480 // buffer to hold the data.
1481 static wxWCharBuffer
value((wxChar
*)NULL
);
1482 value
= wxConvLibc
.cMB2WX(getenv(wxConvLibc
.cWX2MB(name
)));
1483 return value
.data();
1485 return getenv(name
);
1489 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1491 return system(wxConvLibc
.cWX2MB(psz
));
1494 #endif // wxNEED_WX_STDLIB_H
1496 #ifdef wxNEED_WX_TIME_H
1498 wxStrftime(wxChar
*s
, size_t maxsize
, const wxChar
*fmt
, const struct tm
*tm
)
1503 wxCharBuffer
buf(maxsize
);
1505 wxCharBuffer
bufFmt(wxConvLibc
.cWX2MB(fmt
));
1509 size_t ret
= strftime(buf
.data(), maxsize
, bufFmt
, tm
);
1513 wxWCharBuffer wbuf
= wxConvLibc
.cMB2WX(buf
);
1517 wxStrncpy(s
, wbuf
, maxsize
);
1520 #endif // wxNEED_WX_TIME_H
1523 WXDLLEXPORT wxChar
*wxCtime(const time_t *timep
)
1525 // normally the string is 26 chars but give one more in case some broken
1526 // DOS compiler decides to use "\r\n" instead of "\n" at the end
1527 static wxChar buf
[27];
1529 // ctime() is guaranteed to return a string containing only ASCII
1530 // characters, as its format is always the same for any locale
1531 wxStrncpy(buf
, wxString::FromAscii(ctime(timep
)), WXSIZEOF(buf
));
1532 buf
[WXSIZEOF(buf
) - 1] = _T('\0');
1538 #endif // wxUSE_WCHAR_T
1540 // ----------------------------------------------------------------------------
1541 // functions which we may need even if !wxUSE_WCHAR_T
1542 // ----------------------------------------------------------------------------
1546 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
1555 psz
+= wxStrspn(psz
, delim
);
1558 *save_ptr
= (wxChar
*)NULL
;
1559 return (wxChar
*)NULL
;
1563 psz
= wxStrpbrk(psz
, delim
);
1566 *save_ptr
= (wxChar
*)NULL
;
1571 *save_ptr
= psz
+ 1;
1579 // ----------------------------------------------------------------------------
1580 // missing C RTL functions
1581 // ----------------------------------------------------------------------------
1583 #ifdef wxNEED_STRDUP
1585 char *strdup(const char *s
)
1587 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1589 strcpy( dest
, s
) ;
1592 #endif // wxNEED_STRDUP
1594 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1596 void *calloc( size_t num
, size_t size
)
1598 void** ptr
= (void **)malloc(num
* size
);
1599 memset( ptr
, 0, num
* size
);
1603 #endif // __WXWINCE__ <= 211
1607 int wxRemove(const wxChar
*path
)
1609 return ::DeleteFile(path
) == 0;