1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxChar implementation
5 // Modified by: Ron Lee
8 // Copyright: (c) wxWidgets copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "wxchar.h"
16 // ===========================================================================
17 // headers, declarations, constants
18 // ===========================================================================
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #define _ISOC9X_SOURCE 1 // to get vsscanf()
28 #define _BSD_SOURCE 1 // to still get strdup()
38 #include "wx/msw/wince/time.h"
43 #include "wx/wxchar.h"
44 #include "wx/string.h"
48 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
55 #if defined(__MWERKS__) && __MSL__ >= 0x6000
60 #include "wx/mac/private.h"
64 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
66 // assume that we have mbsrtowcs() too if we have wcsrtombs()
69 memset(&mbstate
, 0, sizeof(mbstate_t));
74 if (n
) *buf
= wxT('\0');
78 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
80 return mbstowcs(buf
, psz
, n
);
85 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
87 return mbstowcs((wchar_t *) NULL
, psz
, 0);
91 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
95 memset(&mbstate
, 0, sizeof(mbstate_t));
100 // glibc2.1 chokes on null input
105 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
107 return wcstombs(buf
, pwz
, n
);
112 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
114 return wcstombs((char *) NULL
, pwz
, 0);
117 #endif // wxUSE_WCHAR_T
119 bool WXDLLEXPORT
wxOKlibc()
121 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
122 // glibc 2.0 uses UTF-8 even when it shouldn't
124 if ((MB_CUR_MAX
== 2) &&
125 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
127 // this is UTF-8 allright, check whether that's what we want
128 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
129 if ((strlen(cur_locale
) < 4) ||
130 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
131 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
132 // nope, don't use libc conversion
140 // ============================================================================
141 // printf() functions business
142 // ============================================================================
144 // special test mode: define all functions below even if we don't really need
145 // them to be able to test them
156 #define wxNEED_WPRINTF
158 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
);
161 // ----------------------------------------------------------------------------
162 // implement [v]snprintf() if the system doesn't provide a safe one
163 // ----------------------------------------------------------------------------
165 #if !defined(wxVsnprintf_)
166 int WXDLLEXPORT
wxVsnprintf_(wxChar
*buf
, size_t lenMax
,
167 const wxChar
*format
, va_list argptr
)
169 // buffer to avoid dynamic memory allocation each time for small strings
170 char szScratch
[1024];
172 // number of characters in the buffer so far, must be less than lenMax
175 for ( size_t n
= 0; ; n
++ )
177 const wxChar chCur
= format
[n
];
179 if ( chCur
== wxT('%') )
181 static char s_szFlags
[256] = "%";
183 bool adj_left
= false,
188 size_t min_width
= 0,
189 max_width
= wxSTRING_MAXLEN
;
194 if (in_prec && !prec_dot) \
196 s_szFlags[flagofs++] = '.'; \
200 #define APPEND_CH(ch) \
202 if ( lenCur == lenMax ) \
205 buf[lenCur++] = ch; \
208 #define APPEND_STR(s) \
210 for ( const wxChar *p = s; *p; p++ ) \
217 const wxChar ch
= format
[++n
];
237 s_szFlags
[flagofs
++] = ch
;
243 s_szFlags
[flagofs
++] = ch
;
251 // dot will be auto-added to s_szFlags if non-negative
258 s_szFlags
[flagofs
++] = ch
;
264 s_szFlags
[flagofs
++] = ch
;
271 s_szFlags
[flagofs
++] = ch
;
277 s_szFlags
[flagofs
++] = ch
;
282 int len
= va_arg(argptr
, int);
293 adj_left
= !adj_left
;
294 s_szFlags
[flagofs
++] = '-';
299 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
303 case wxT('1'): case wxT('2'): case wxT('3'):
304 case wxT('4'): case wxT('5'): case wxT('6'):
305 case wxT('7'): case wxT('8'): case wxT('9'):
309 while ( (format
[n
] >= wxT('0')) &&
310 (format
[n
] <= wxT('9')) )
312 s_szFlags
[flagofs
++] = format
[n
];
313 len
= len
*10 + (format
[n
] - wxT('0'));
322 n
--; // the main loop pre-increments n again
333 s_szFlags
[flagofs
++] = ch
;
334 s_szFlags
[flagofs
] = '\0';
337 int val
= va_arg(argptr
, int);
338 ::sprintf(szScratch
, s_szFlags
, val
);
342 // NB: 'short int' value passed through '...'
343 // is promoted to 'int', so we have to get
344 // an int from stack even if we need a short
345 short int val
= (short int) va_arg(argptr
, int);
346 ::sprintf(szScratch
, s_szFlags
, val
);
350 long int val
= va_arg(argptr
, long int);
351 ::sprintf(szScratch
, s_szFlags
, val
);
356 long long int val
= va_arg(argptr
, long long int);
357 ::sprintf(szScratch
, s_szFlags
, val
);
359 long int val
= va_arg(argptr
, long int);
360 ::sprintf(szScratch
, s_szFlags
, val
);
361 #endif // long long/!long long
365 size_t val
= va_arg(argptr
, size_t);
366 ::sprintf(szScratch
, s_szFlags
, val
);
370 const wxMB2WXbuf tmp
=
371 wxConvLibc
.cMB2WX(szScratch
);
384 s_szFlags
[flagofs
++] = ch
;
385 s_szFlags
[flagofs
] = '\0';
388 long double val
= va_arg(argptr
, long double);
389 ::sprintf(szScratch
, s_szFlags
, val
);
393 double val
= va_arg(argptr
, double);
394 ::sprintf(szScratch
, s_szFlags
, val
);
398 const wxMB2WXbuf tmp
=
399 wxConvLibc
.cMB2WX(szScratch
);
408 void *val
= va_arg(argptr
, void *);
410 s_szFlags
[flagofs
++] = ch
;
411 s_szFlags
[flagofs
] = '\0';
412 ::sprintf(szScratch
, s_szFlags
, val
);
414 const wxMB2WXbuf tmp
=
415 wxConvLibc
.cMB2WX(szScratch
);
424 int val
= va_arg(argptr
, int);
428 const char buf
[2] = { val
, 0 };
429 val
= wxString(buf
, wxConvLibc
)[0u];
434 const wchar_t buf
[2] = { val
, 0 };
435 val
= wxString(buf
, wxConvLibc
)[0u];
441 for (i
= 1; i
< min_width
; i
++)
447 for (i
= 1; i
< min_width
; i
++)
456 const wxChar
*val
= NULL
;
462 // wx extension: we'll let %hs mean non-Unicode
464 char *v
= va_arg(argptr
, char *);
467 val
= s
= wxString(v
, wxConvLibc
);
475 // %ls means Unicode strings
476 wchar_t *v
= va_arg(argptr
, wchar_t *);
479 val
= s
= wxString(v
, wxConvLibc
);
484 val
= va_arg(argptr
, wxChar
*);
492 val
[len
] && (len
< max_width
);
496 else if (max_width
>= 6)
510 for (i
= len
; i
< min_width
; i
++)
513 for (i
= 0; i
< len
; i
++)
517 for (i
= len
; i
< min_width
; i
++)
527 int *val
= va_arg(argptr
, int *);
532 short int *val
= va_arg(argptr
, short int *);
537 long int *val
= va_arg(argptr
, long int *);
544 // bad format, leave unchanged
570 #endif // !wxVsnprintfA
572 #if !defined(wxSnprintf_)
573 int WXDLLEXPORT
wxSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
576 va_start(argptr
, format
);
578 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
584 #endif // wxSnprintf_
587 /* Digital Mars adds count to _stprintf (C99) so convert */
589 int wxSprintf (wchar_t * __RESTRICT s
, const wchar_t * __RESTRICT format
, ... )
593 va_start( arglist
, format
);
594 int iLen
= swprintf ( s
, -1, format
, arglist
);
599 #endif // wxUSE_UNICODE
603 // ----------------------------------------------------------------------------
604 // implement the standard IO functions for wide char if libc doesn't have them
605 // ----------------------------------------------------------------------------
608 int wxFputs(const wchar_t *ws
, FILE *stream
)
610 // counting the number of wide characters written isn't worth the trouble,
611 // simply distinguish between ok and error
612 return fputs(wxConvLibc
.cWC2MB(ws
), stream
) == -1 ? -1 : 0;
614 #endif // wxNEED_FPUTS
617 int wxPuts(const wxChar
*ws
)
619 int rc
= wxFputs(ws
, stdout
);
622 if ( wxFputs(L
"\n", stdout
) == -1 )
630 #endif // wxNEED_PUTS
633 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
635 wchar_t ws
[2] = { wc
, L
'\0' };
637 return wxFputs(ws
, stream
);
639 #endif // wxNEED_PUTC
641 // NB: we only implement va_list functions here, the ones taking ... are
642 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
643 // the definitions there to avoid duplicating them here
644 #ifdef wxNEED_WPRINTF
646 // TODO: implement the scanf() functions
647 int vwscanf(const wxChar
*format
, va_list argptr
)
649 wxFAIL_MSG( _T("TODO") );
654 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
656 wxFAIL_MSG( _T("TODO") );
661 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
663 wxFAIL_MSG( _T("TODO") );
668 #define vswprintf wxVsnprintf_
670 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
673 int rc
= s
.PrintfV(format
, argptr
);
677 // we can't do much better without Unicode support in libc...
678 if ( fprintf(stream
, "%s", (const char*)s
.mb_str() ) == -1 )
685 int vwprintf(const wxChar
*format
, va_list argptr
)
687 return wxVfprintf(stdout
, format
, argptr
);
690 #endif // wxNEED_WPRINTF
692 #ifdef wxNEED_PRINTF_CONVERSION
694 // ----------------------------------------------------------------------------
695 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
696 // ----------------------------------------------------------------------------
699 Here are the gory details. We want to follow the Windows/MS conventions,
704 format specifier results in
705 -----------------------------------
711 format specifier results in
712 -----------------------------------
717 while on POSIX systems we have %C identical to %lc and %c always means char
718 (in any mode) while %lc always means wchar_t,
720 So to use native functions in order to get our semantics we must do the
721 following translations in Unicode mode (nothing to do in ANSI mode):
723 wxWidgets specifier POSIX specifier
724 ----------------------------------------
730 And, of course, the same should be done for %s as well.
733 class wxFormatConverter
736 wxFormatConverter(const wxChar
*format
);
738 // notice that we only translated the string if m_fmtOrig == NULL (as set
739 // by CopyAllBefore()), otherwise we should simply use the original format
740 operator const wxChar
*() const
741 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
744 // copy another character to the translated format: this function does the
745 // copy if we are translating but doesn't do anything at all if we don't,
746 // so we don't create the translated format string at all unless we really
747 // need to (i.e. InsertFmtChar() is called)
748 wxChar
CopyFmtChar(wxChar ch
)
752 // we're translating, do copy
757 // simply increase the count which should be copied by
758 // CopyAllBefore() later if needed
765 // insert an extra character
766 void InsertFmtChar(wxChar ch
)
770 // so far we haven't translated anything yet
779 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
781 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
783 // we won't need it any longer
787 static bool IsFlagChar(wxChar ch
)
789 return ch
== _T('-') || ch
== _T('+') ||
790 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
793 void SkipDigits(const wxChar
**ptpc
)
795 while ( **ptpc
>= _T('0') && **ptpc
<= _T('9') )
796 CopyFmtChar(*(*ptpc
)++);
799 // the translated format
802 // the original format
803 const wxChar
*m_fmtOrig
;
805 // the number of characters already copied
809 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
816 if ( CopyFmtChar(*format
++) == _T('%') )
819 while ( IsFlagChar(*format
) )
820 CopyFmtChar(*format
++);
822 // and possible width
823 if ( *format
== _T('*') )
824 CopyFmtChar(*format
++);
829 if ( *format
== _T('.') )
831 CopyFmtChar(*format
++);
832 if ( *format
== _T('*') )
833 CopyFmtChar(*format
++);
838 // next we can have a size modifier
854 // "ll" has a different meaning!
855 if ( format
[1] != _T('l') )
867 // and finally we should have the type
872 // %C and %hC -> %c and %lC -> %lc
874 CopyFmtChar(_T('l'));
876 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
881 // %c -> %lc but %hc stays %hc and %lc is still %lc
882 if ( size
== Default
)
883 InsertFmtChar(_T('l'));
887 // nothing special to do
888 if ( size
!= Default
)
889 CopyFmtChar(*(format
- 1));
890 CopyFmtChar(*format
++);
896 #else // !wxNEED_PRINTF_CONVERSION
897 // no conversion necessary
898 #define wxFormatConverter(x) (x)
899 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
902 // For testing the format converter
903 wxString
wxConvertFormat(const wxChar
*format
)
905 return wxString(wxFormatConverter(format
));
909 // ----------------------------------------------------------------------------
910 // wxPrintf(), wxScanf() and relatives
911 // ----------------------------------------------------------------------------
913 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
915 int wxScanf( const wxChar
*format
, ... )
918 va_start(argptr
, format
);
920 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
927 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... )
930 va_start(argptr
, format
);
932 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
939 int wxFscanf( FILE *stream
, const wxChar
*format
, ... )
942 va_start(argptr
, format
);
943 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
950 int wxPrintf( const wxChar
*format
, ... )
953 va_start(argptr
, format
);
955 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
963 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... )
966 va_start(argptr
, format
);
968 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
976 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... )
979 va_start(argptr
, format
);
981 // note that wxString::FormatV() uses wxVsnprintf(), not wxSprintf(), so
982 // it's safe to implement this one in terms of it
983 wxString
s(wxString::FormatV(format
, argptr
));
991 int wxFprintf( FILE *stream
, const wxChar
*format
, ... )
994 va_start( argptr
, format
);
996 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
1003 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
1005 return vswscanf( str
, wxFormatConverter(format
), argptr
);
1008 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
1010 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
1013 int wxVprintf( const wxChar
*format
, va_list argptr
)
1015 return vwprintf( wxFormatConverter(format
), argptr
);
1019 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
1021 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
1023 #endif // wxVsnprintf
1025 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
1027 // same as for wxSprintf()
1028 return vswprintf(str
, INT_MAX
/ 4, wxFormatConverter(format
), argptr
);
1031 #endif // wxNEED_PRINTF_CONVERSION
1035 // ----------------------------------------------------------------------------
1036 // ctype.h stuff (currently unused)
1037 // ----------------------------------------------------------------------------
1039 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
1040 inline WORD
wxMSW_ctype(wxChar ch
)
1043 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
1047 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
1048 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
1049 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
1050 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
1051 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
1052 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
1053 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
1054 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
1055 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
1056 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
1057 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
1058 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
1059 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
1062 #if defined(__DARWIN__) && ( MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_2 )
1064 WXDLLEXPORT
size_t wxInternalMbstowcs (wchar_t * out
, const char * in
, size_t outlen
)
1074 const char* origin
= in
;
1076 while (outlen
-- && *in
)
1078 *out
++ = (wchar_t) *in
++;
1086 WXDLLEXPORT
size_t wxInternalWcstombs (char * out
, const wchar_t * in
, size_t outlen
)
1096 const wchar_t* origin
= in
;
1098 while (outlen
-- && *in
)
1100 *out
++ = (char) *in
++;
1108 #if defined(wxNEED_WX_CTYPE_H)
1110 #include <CoreFoundation/CoreFoundation.h>
1112 #define cfalnumset CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric)
1113 #define cfalphaset CFCharacterSetGetPredefined(kCFCharacterSetLetter)
1114 #define cfcntrlset CFCharacterSetGetPredefined(kCFCharacterSetControl)
1115 #define cfdigitset CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit)
1116 //CFCharacterSetRef cfgraphset = kCFCharacterSetControl && !' '
1117 #define cflowerset CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter)
1118 //CFCharacterSetRef cfprintset = !kCFCharacterSetControl
1119 #define cfpunctset CFCharacterSetGetPredefined(kCFCharacterSetPunctuation)
1120 #define cfspaceset CFCharacterSetGetPredefined(kCFCharacterSetWhitespaceAndNewline)
1121 #define cfupperset CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter)
1123 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalnumset
, ch
); }
1124 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfalphaset
, ch
); }
1125 WXDLLEXPORT
int wxIscntrl(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
1126 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfdigitset
, ch
); }
1127 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
) && ch
!= ' '; }
1128 WXDLLEXPORT
int wxIslower(wxChar ch
) { return CFCharacterSetIsCharacterMember(cflowerset
, ch
); }
1129 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return !CFCharacterSetIsCharacterMember(cfcntrlset
, ch
); }
1130 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfpunctset
, ch
); }
1131 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfspaceset
, ch
); }
1132 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return CFCharacterSetIsCharacterMember(cfupperset
, ch
); }
1133 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxIsdigit(ch
) || (ch
>='a' && ch
<='f') || (ch
>='A' && ch
<='F'); }
1134 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)tolower((char)(ch
)); }
1135 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)toupper((char)(ch
)); }
1137 #endif // wxNEED_WX_CTYPE_H
1139 #endif // defined(__DARWIN__) and OSX <= 10.2
1143 WXDLLEXPORT
char *wxStrdupA(const char *s
)
1145 return strcpy((char *)malloc(strlen(s
) + 1), s
);
1152 WXDLLEXPORT
wchar_t * wxStrdupW(const wchar_t *pwz
)
1154 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
1155 wchar_t *ret
= (wchar_t *) malloc(size
);
1156 memcpy(ret
, pwz
, size
);
1163 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
1165 register wxChar c1
, c2
;
1167 c1
= wxTolower(*psz1
++);
1168 c2
= wxTolower(*psz2
++);
1169 } while ( c1
&& (c1
== c2
) );
1175 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1177 // initialize the variables just to suppress stupid gcc warning
1178 register wxChar c1
= 0, c2
= 0;
1179 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
1181 if (c1
< c2
) return -1;
1182 if (c1
> c2
) return 1;
1189 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
1191 char *localeOld
= setlocale(category
, wxConvLocal
.cWX2MB(locale
));
1193 return wxWCharBuffer(wxConvLocal
.cMB2WC(localeOld
));
1197 #if wxUSE_WCHAR_T && !defined(HAVE_WCSLEN)
1198 WXDLLEXPORT
size_t wxWcslen(const wchar_t *s
)
1208 // ----------------------------------------------------------------------------
1209 // string.h functions
1210 // ----------------------------------------------------------------------------
1212 #ifdef wxNEED_WX_STRING_H
1214 // RN: These need to be c externed for the regex lib
1219 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
1222 while (*dest
) dest
++;
1223 while ((*dest
++ = *src
++));
1227 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
1229 // be careful here as the terminating NUL makes part of the string
1239 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
1241 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
1242 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1243 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1247 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
1250 while ((*dest
++ = *src
++));
1254 WXDLLEXPORT
size_t wxStrlen_(const wxChar
*s
)
1264 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
1267 while (*dest
) dest
++;
1268 while (n
&& (*dest
++ = *src
++)) n
--;
1272 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1274 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1276 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1277 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1282 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1285 while (n
&& (*dest
++ = *src
++)) n
--;
1286 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1290 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1292 while (*s
&& !wxStrchr(accept
, *s
))
1295 return *s
? s
: NULL
;
1298 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1300 const wxChar
*ret
= NULL
;
1312 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1315 while (wxStrchr(accept
, *s
++)) len
++;
1319 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1321 wxASSERT_MSG( needle
!= NULL
, _T("NULL argument in wxStrstr") );
1323 // VZ: this is not exactly the most efficient string search algorithm...
1325 const size_t len
= wxStrlen(needle
);
1327 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1329 if ( !wxStrncmp(fnd
, needle
, len
) )
1342 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1344 const wxChar
*start
= nptr
;
1346 // FIXME: only correct for C locale
1347 while (wxIsspace(*nptr
)) nptr
++;
1348 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1349 while (wxIsdigit(*nptr
)) nptr
++;
1350 if (*nptr
== wxT('.')) {
1352 while (wxIsdigit(*nptr
)) nptr
++;
1354 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1356 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1357 while (wxIsdigit(*nptr
)) nptr
++;
1360 wxString
data(nptr
, nptr
-start
);
1361 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1362 char *rdat
= wxMBSTRINGCAST dat
;
1363 double ret
= strtod(dat
, &rdat
);
1365 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1370 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1372 const wxChar
*start
= nptr
;
1374 // FIXME: only correct for C locale
1375 while (wxIsspace(*nptr
)) nptr
++;
1376 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1377 if (((base
== 0) || (base
== 16)) &&
1378 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1382 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1383 else if (base
== 0) base
= 10;
1385 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1386 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1388 wxString
data(start
, nptr
-start
);
1389 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1390 char *rdat
= wxMBSTRINGCAST dat
;
1391 long int ret
= strtol(dat
, &rdat
, base
);
1393 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1398 WXDLLEXPORT
unsigned long int wxStrtoul(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1400 return (unsigned long int) wxStrtol(nptr
, endptr
, base
);
1403 #endif // wxNEED_WX_STRING_H
1405 #ifdef wxNEED_WX_STDIO_H
1406 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1408 char mode_buffer
[10];
1409 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1410 mode_buffer
[i
] = (char) mode
[i
];
1412 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1415 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1417 char mode_buffer
[10];
1418 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1419 mode_buffer
[i
] = (char) mode
[i
];
1421 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1424 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1426 return remove( wxConvFile
.cWX2MB(path
) );
1429 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1431 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1436 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1441 if (str
.ToDouble(& d
))
1446 return atof(wxConvLocal
.cWX2MB(psz
));
1451 #ifdef wxNEED_WX_STDLIB_H
1452 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1454 return atoi(wxConvLocal
.cWX2MB(psz
));
1457 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1459 return atol(wxConvLocal
.cWX2MB(psz
));
1462 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1465 // NB: buffer returned by getenv() is allowed to be overwritten next
1466 // time getenv() is called, so it is OK to use static string
1467 // buffer to hold the data.
1468 static wxWCharBuffer
value((wxChar
*)NULL
);
1469 value
= wxConvLocal
.cMB2WX(getenv(wxConvLocal
.cWX2MB(name
)));
1470 return value
.data();
1472 return getenv(name
);
1476 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1478 return system(wxConvLocal
.cWX2MB(psz
));
1481 #endif // wxNEED_WX_STDLIB_H
1483 #ifdef wxNEED_WX_TIME_H
1484 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
1488 char *buf
= (char *)malloc(max
);
1489 size_t ret
= strftime(buf
, max
, wxConvLocal
.cWX2MB(fmt
), tm
);
1492 wxStrcpy(s
, wxConvLocal
.cMB2WX(buf
));
1503 #endif // wxNEED_WX_TIME_H
1506 WXDLLEXPORT wxChar
*wxCtime(const time_t *timep
)
1508 static wxChar buf
[128];
1510 wxStrncpy( buf
, wxConvertMB2WX( ctime( timep
) ), sizeof( buf
) );
1511 buf
[ sizeof( buf
) - 1 ] = _T('\0');
1517 #endif // wxUSE_WCHAR_T
1519 // ----------------------------------------------------------------------------
1520 // functions which we may need even if !wxUSE_WCHAR_T
1521 // ----------------------------------------------------------------------------
1525 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
1534 psz
+= wxStrspn(psz
, delim
);
1537 *save_ptr
= (wxChar
*)NULL
;
1538 return (wxChar
*)NULL
;
1542 psz
= wxStrpbrk(psz
, delim
);
1545 *save_ptr
= (wxChar
*)NULL
;
1550 *save_ptr
= psz
+ 1;
1558 // ----------------------------------------------------------------------------
1559 // missing C RTL functions
1560 // ----------------------------------------------------------------------------
1564 char *strdup(const char *s
)
1566 char *dest
= (char*) malloc( strlen( s
) + 1 ) ;
1568 strcpy( dest
, s
) ;
1571 #endif // wxNEED_STRDUP
1573 #if defined(__WXWINCE__) && (_WIN32_WCE <= 211)
1575 void *calloc( size_t num
, size_t size
)
1577 void** ptr
= (void **)malloc(num
* size
);
1578 memset( ptr
, 0, num
* size
);
1582 #endif // __WXWINCE__ <= 211