1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxChar implementation
8 // Copyright: (c) wxWindows copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
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/wxchar.h"
39 #include "wx/string.h"
43 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
50 #if defined(__MWERKS__) && __MSL__ >= 0x6000
55 #include "wx/mac/private.h"
59 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
61 // assume that we have mbsrtowcs() too if we have wcsrtombs()
64 memset(&mbstate
, 0, sizeof(mbstate_t));
69 if (n
) *buf
= wxT('\0');
73 return mbsrtowcs(buf
, &psz
, n
, &mbstate
);
75 return mbstowcs(buf
, psz
, n
);
80 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
82 return mbstowcs((wchar_t *) NULL
, psz
, 0);
86 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
90 memset(&mbstate
, 0, sizeof(mbstate_t));
95 // glibc2.1 chokes on null input
100 return wcsrtombs(buf
, &pwz
, n
, &mbstate
);
102 return wcstombs(buf
, pwz
, n
);
107 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
109 return wcstombs((char *) NULL
, pwz
, 0);
112 #endif // wxUSE_WCHAR_T
114 bool WXDLLEXPORT
wxOKlibc()
116 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__) && !defined(__WINE__)
117 // glibc 2.0 uses UTF-8 even when it shouldn't
119 if ((MB_CUR_MAX
== 2) &&
120 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
122 // this is UTF-8 allright, check whether that's what we want
123 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
124 if ((strlen(cur_locale
) < 4) ||
125 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
126 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
127 // nope, don't use libc conversion
135 // ============================================================================
136 // printf() functions business
137 // ============================================================================
139 // special test mode: define all functions below even if we don't really need
140 // them to be able to test them
151 #define wxNEED_WPRINTF
153 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
);
156 // ----------------------------------------------------------------------------
157 // implement [v]snprintf() if the system doesn't provide a safe one
158 // ----------------------------------------------------------------------------
160 #if !defined(wxVsnprintf_)
161 int WXDLLEXPORT
wxVsnprintf_(wxChar
*buf
, size_t lenMax
,
162 const wxChar
*format
, va_list argptr
)
164 // buffer to avoid dynamic memory allocation each time for small strings
165 char szScratch
[1024];
167 // number of characters in the buffer so far, must be less than lenMax
170 for ( size_t n
= 0; ; n
++ )
172 const wxChar chCur
= format
[n
];
174 if ( chCur
== wxT('%') )
176 static char s_szFlags
[256] = "%";
178 bool adj_left
= FALSE
,
183 size_t min_width
= 0,
184 max_width
= wxSTRING_MAXLEN
;
189 if (in_prec && !prec_dot) \
191 s_szFlags[flagofs++] = '.'; \
195 #define APPEND_CH(ch) \
196 if ( lenCur == lenMax ) \
201 #define APPEND_STR(s) \
203 for ( const wxChar *p = s; *p; p++ ) \
210 const wxChar ch
= format
[++n
];
230 s_szFlags
[flagofs
++] = ch
;
236 s_szFlags
[flagofs
++] = ch
;
244 // dot will be auto-added to s_szFlags if non-negative
251 s_szFlags
[flagofs
++] = ch
;
257 s_szFlags
[flagofs
++] = ch
;
264 s_szFlags
[flagofs
++] = ch
;
270 s_szFlags
[flagofs
++] = ch
;
275 int len
= va_arg(argptr
, int);
286 adj_left
= !adj_left
;
287 s_szFlags
[flagofs
++] = '-';
292 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
296 case wxT('1'): case wxT('2'): case wxT('3'):
297 case wxT('4'): case wxT('5'): case wxT('6'):
298 case wxT('7'): case wxT('8'): case wxT('9'):
302 while ( (format
[n
] >= wxT('0')) &&
303 (format
[n
] <= wxT('9')) )
305 s_szFlags
[flagofs
++] = format
[n
];
306 len
= len
*10 + (format
[n
] - wxT('0'));
315 n
--; // the main loop pre-increments n again
326 s_szFlags
[flagofs
++] = ch
;
327 s_szFlags
[flagofs
] = '\0';
330 int val
= va_arg(argptr
, int);
331 ::sprintf(szScratch
, s_szFlags
, val
);
335 // NB: 'short int' value passed through '...'
336 // is promoted to 'int', so we have to get
337 // an int from stack even if we need a short
338 short int val
= (short int) va_arg(argptr
, int);
339 ::sprintf(szScratch
, s_szFlags
, val
);
343 long int val
= va_arg(argptr
, long int);
344 ::sprintf(szScratch
, s_szFlags
, val
);
349 long long int val
= va_arg(argptr
, long long int);
350 ::sprintf(szScratch
, s_szFlags
, val
);
352 long int val
= va_arg(argptr
, long int);
353 ::sprintf(szScratch
, s_szFlags
, val
);
354 #endif // long long/!long long
358 size_t val
= va_arg(argptr
, size_t);
359 ::sprintf(szScratch
, s_szFlags
, val
);
363 const wxMB2WXbuf tmp
=
364 wxConvLibc
.cMB2WX(szScratch
);
377 s_szFlags
[flagofs
++] = ch
;
378 s_szFlags
[flagofs
] = '\0';
381 long double val
= va_arg(argptr
, long double);
382 ::sprintf(szScratch
, s_szFlags
, val
);
386 double val
= va_arg(argptr
, double);
387 ::sprintf(szScratch
, s_szFlags
, val
);
391 const wxMB2WXbuf tmp
=
392 wxConvLibc
.cMB2WX(szScratch
);
401 void *val
= va_arg(argptr
, void *);
403 s_szFlags
[flagofs
++] = ch
;
404 s_szFlags
[flagofs
] = '\0';
405 ::sprintf(szScratch
, s_szFlags
, val
);
407 const wxMB2WXbuf tmp
=
408 wxConvLibc
.cMB2WX(szScratch
);
417 wxChar val
= va_arg(argptr
, int);
418 // we don't need to honor padding here, do we?
428 // wx extension: we'll let %hs mean non-Unicode
430 char *val
= va_arg(argptr
, char *);
432 // ASCII->Unicode constructor handles max_width
434 wxString
s(val
, wxConvLibc
, max_width
);
436 size_t len
= wxSTRING_MAXLEN
;
440 val
[len
] && (len
< max_width
);
446 wxString
s(val
, len
);
448 if (s
.Len() < min_width
)
449 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
455 wxChar
*val
= va_arg(argptr
, wxChar
*);
456 size_t len
= wxSTRING_MAXLEN
;
460 val
[len
] && (len
< max_width
);
467 wxString
s(val
, len
);
468 if (s
.Len() < min_width
)
469 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
479 int *val
= va_arg(argptr
, int *);
484 short int *val
= va_arg(argptr
, short int *);
489 long int *val
= va_arg(argptr
, long int *);
496 // bad format, leave unchanged
522 #endif // !wxVsnprintfA
524 #if !defined(wxSnprintf_)
525 int WXDLLEXPORT
wxSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
528 va_start(argptr
, format
);
530 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
536 #endif // wxSnprintf_
538 // ----------------------------------------------------------------------------
539 // implement the standard IO functions for wide char if libc doesn't have them
540 // ----------------------------------------------------------------------------
544 int wxFputs(const wchar_t *ws
, FILE *stream
)
546 // counting the number of wide characters written isn't worth the trouble,
547 // simply distinguish between ok and error
548 return fputs(wxConvLibc
.cWC2MB(ws
), stream
) == -1 ? -1 : 0;
551 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
553 wchar_t ws
[2] = { wc
, L
'\0' };
555 return wxFputs(ws
, stream
);
558 #endif // wxNEED_FPUTWC
560 // NB: we only implement va_list functions here, the ones taking ... are
561 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
562 // the definitions there to avoid duplicating them here
563 #ifdef wxNEED_WPRINTF
565 // TODO: implement the scanf() functions
566 int vwscanf(const wxChar
*format
, va_list argptr
)
568 wxFAIL_MSG( _T("TODO") );
573 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
575 wxFAIL_MSG( _T("TODO") );
580 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
582 wxFAIL_MSG( _T("TODO") );
587 #define vswprintf wxVsnprintf_
589 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
592 int rc
= s
.PrintfV(format
, argptr
);
596 // we can't do much better without Unicode support in libc...
597 if ( fprintf(stream
, "%s", s
.mb_str()) == -1 )
604 int vwprintf(const wxChar
*format
, va_list argptr
)
606 return wxVfprintf(stdout
, format
, argptr
);
609 #endif // wxNEED_WPRINTF
611 #ifdef wxNEED_PRINTF_CONVERSION
613 // ----------------------------------------------------------------------------
614 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
615 // ----------------------------------------------------------------------------
618 Here are the gory details. We want to follow the Windows/MS conventions,
623 format specifier results in
624 -----------------------------------
630 format specifier results in
631 -----------------------------------
636 while on POSIX systems we have %C identical to %lc and %c always means char
637 (in any mode) while %lc always means wchar_t,
639 So to use native functions in order to get our semantics we must do the
640 following translations in Unicode mode (nothing to do in ANSI mode):
642 wxWindows specifier POSIX specifier
643 ----------------------------------------
649 And, of course, the same should be done for %s as well.
652 class wxFormatConverter
655 wxFormatConverter(const wxChar
*format
);
657 // notice that we only translated the string if m_fmtOrig == NULL (as set
658 // by CopyAllBefore()), otherwise we should simply use the original format
659 operator const wxChar
*() const
660 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
663 // copy another character to the translated format: this function does the
664 // copy if we are translating but doesn't do anything at all if we don't,
665 // so we don't create the translated format string at all unless we really
666 // need to (i.e. InsertFmtChar() is called)
667 wxChar
CopyFmtChar(wxChar ch
)
671 // we're translating, do copy
676 // simply increase the count which should be copied by
677 // CopyAllBefore() later if needed
684 // insert an extra character
685 void InsertFmtChar(wxChar ch
)
689 // so far we haven't translated anything yet
698 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
700 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
702 // we won't need it any longer
706 static bool IsFlagChar(wxChar ch
)
708 return ch
== _T('-') || ch
== _T('+') ||
709 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
712 void SkipDigits(const wxChar
**ppc
)
714 while ( **ppc
>= _T('0') && **ppc
<= _T('9') )
715 CopyFmtChar(*(*ppc
)++);
718 // the translated format
721 // the original format
722 const wxChar
*m_fmtOrig
;
724 // the number of characters already copied
728 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
735 if ( CopyFmtChar(*format
++) == _T('%') )
738 while ( IsFlagChar(*format
) )
739 CopyFmtChar(*format
++);
741 // and possible width
742 if ( *format
== _T('*') )
743 CopyFmtChar(*format
++);
748 if ( *format
== _T('.') )
753 // next we can have a size modifier
769 // "ll" has a different meaning!
770 if ( format
[1] != _T('l') )
782 // and finally we should have the type
787 // %C and %hC -> %c and %lC -> %lc
789 CopyFmtChar(_T('l'));
791 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
796 // %c -> %lc but %hc stays %hc and %lc is still %lc
800 InsertFmtChar(_T('l'));
804 CopyFmtChar(_T('h'));
813 // nothing special to do
814 CopyFmtChar(*format
++);
820 #else // !wxNEED_PRINTF_CONVERSION
821 // no conversion necessary
822 #define wxFormatConverter(x) (x)
823 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
825 // ----------------------------------------------------------------------------
826 // wxPrintf(), wxScanf() and relatives
827 // ----------------------------------------------------------------------------
829 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
831 int wxScanf( const wxChar
*format
, ... )
834 va_start(argptr
, format
);
836 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
843 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... )
846 va_start(argptr
, format
);
848 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
855 int wxFscanf( FILE *stream
, const wxChar
*format
, ... )
858 va_start(argptr
, format
);
859 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
866 int wxPrintf( const wxChar
*format
, ... )
869 va_start(argptr
, format
);
871 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
879 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... )
882 va_start(argptr
, format
);
884 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
892 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... )
895 va_start(argptr
, format
);
897 // callers of wxSprintf() deserve what they get
898 int ret
= vswprintf( str
, UINT_MAX
, wxFormatConverter(format
), argptr
);
905 int wxFprintf( FILE *stream
, const wxChar
*format
, ... )
908 va_start( argptr
, format
);
910 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
917 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
919 return vswscanf( str
, wxFormatConverter(format
), argptr
);
922 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
924 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
927 int wxVprintf( const wxChar
*format
, va_list argptr
)
929 return vwprintf( wxFormatConverter(format
), argptr
);
933 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
935 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
937 #endif // wxVsnprintf
939 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
941 // same as for wxSprintf()
942 return vswprintf(str
, UINT_MAX
, wxFormatConverter(format
), argptr
);
945 #endif // wxNEED_PRINTF_CONVERSION
949 // ----------------------------------------------------------------------------
950 // ctype.h stuff (currently unused)
951 // ----------------------------------------------------------------------------
953 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
954 inline WORD
wxMSW_ctype(wxChar ch
)
957 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
961 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
962 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
963 WXDLLEXPORT
int wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
964 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
965 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
966 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
967 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
968 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
969 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
970 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
971 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
972 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
973 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
978 WXDLLEXPORT
char *wxStrdupA(const char *s
)
980 return strcpy((char *)malloc(strlen(s
) + 1), s
);
987 WXDLLEXPORT
wchar_t * wxStrdupW(const wchar_t *pwz
)
989 size_t size
= (wxWcslen(pwz
) + 1) * sizeof(wchar_t);
990 wchar_t *ret
= (wchar_t *) malloc(size
);
991 memcpy(ret
, pwz
, size
);
998 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
1000 register wxChar c1
, c2
;
1002 c1
= wxTolower(*psz1
++);
1003 c2
= wxTolower(*psz2
++);
1004 } while ( c1
&& (c1
== c2
) );
1010 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1012 register wxChar c1
, c2
;
1013 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
1015 if (c1
< c2
) return -1;
1016 if (c1
> c2
) return 1;
1023 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
1025 char *localeOld
= setlocale(category
, wxConvLocal
.cWX2MB(locale
));
1027 return wxWCharBuffer(wxConvLocal
.cMB2WC(localeOld
));
1031 // ----------------------------------------------------------------------------
1032 // string.h functions
1033 // ----------------------------------------------------------------------------
1035 #ifdef wxNEED_WX_STRING_H
1036 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
1039 while (*dest
) dest
++;
1040 while ((*dest
++ = *src
++));
1044 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
1046 // be careful here as the terminating NUL makes part of the string
1056 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
1058 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
1059 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1060 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1064 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
1067 while ((*dest
++ = *src
++));
1071 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
1074 while (*dest
) dest
++;
1075 while (n
&& (*dest
++ = *src
++)) n
--;
1079 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1081 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1083 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1084 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1089 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1092 while (n
&& (*dest
++ = *src
++)) n
--;
1093 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1097 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1099 while (*s
&& !wxStrchr(accept
, *s
))
1102 return *s
? s
: NULL
;
1105 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1107 const wxChar
*ret
= NULL
;
1119 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1122 while (wxStrchr(accept
, *s
++)) len
++;
1126 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1128 wxCHECK_RET( needle
, NULL
, _T("NULL argument in wxStrstr") );
1130 // VZ: this is not exactly the most efficient string search algorithm...
1132 const size_t len
= wxStrlen(needle
);
1134 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1136 if ( !wxStrncmp(fnd
, needle
, len
) )
1145 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1147 const wxChar
*start
= nptr
;
1149 // FIXME: only correct for C locale
1150 while (wxIsspace(*nptr
)) nptr
++;
1151 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1152 while (wxIsdigit(*nptr
)) nptr
++;
1153 if (*nptr
== wxT('.')) {
1155 while (wxIsdigit(*nptr
)) nptr
++;
1157 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1159 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1160 while (wxIsdigit(*nptr
)) nptr
++;
1163 wxString
data(nptr
, nptr
-start
);
1164 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1165 char *rdat
= wxMBSTRINGCAST dat
;
1166 double ret
= strtod(dat
, &rdat
);
1168 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1173 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1175 const wxChar
*start
= nptr
;
1177 // FIXME: only correct for C locale
1178 while (wxIsspace(*nptr
)) nptr
++;
1179 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1180 if (((base
== 0) || (base
== 16)) &&
1181 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1185 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1186 else if (base
== 0) base
= 10;
1188 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1189 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1191 wxString
data(nptr
, nptr
-start
);
1192 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1193 char *rdat
= wxMBSTRINGCAST dat
;
1194 long int ret
= strtol(dat
, &rdat
, base
);
1196 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1200 #endif // wxNEED_WX_STRING_H
1202 #if defined(__WXMAC__) && !defined(__DARWIN__)
1203 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1205 return fopen( wxMacStringToCString(path
), mode
);
1208 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1210 return freopen( wxMacStringToCString(path
), mode
, stream
);
1213 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1215 return remove( wxMacStringToCString(path
) );
1218 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1220 return rename( wxMacStringToCString(oldpath
), wxMacStringToCString(newpath
) );
1224 #ifdef wxNEED_WX_STDIO_H
1225 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1227 char mode_buffer
[10];
1228 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1229 mode_buffer
[i
] = (char) mode
[i
];
1231 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1234 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1236 char mode_buffer
[10];
1237 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1238 mode_buffer
[i
] = (char) mode
[i
];
1240 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1243 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1245 return remove( wxConvFile
.cWX2MB(path
) );
1248 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1250 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1255 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1257 return atof(wxConvLocal
.cWX2MB(psz
));
1261 #ifdef wxNEED_WX_STDLIB_H
1262 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1264 return atoi(wxConvLocal
.cWX2MB(psz
));
1267 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1269 return atol(wxConvLocal
.cWX2MB(psz
));
1272 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1274 static wxHashTable env
;
1276 // check if we already have stored the converted env var
1277 wxObject
*data
= env
.Get(name
);
1280 // nope, retrieve it,
1282 wxCharBuffer buffer
= wxConvLocal
.cWX2MB(name
);
1283 // printf( "buffer %s\n", (const char*) buffer );
1284 const char *val
= getenv( (const char *)buffer
);
1286 const char *val
= getenv( name
);
1289 if (!val
) return (wxChar
*)NULL
;
1290 // printf( "home %s\n", val );
1293 #ifdef wxUSE_UNICODE
1294 data
= (wxObject
*)new wxString(val
, wxConvLocal
);
1296 data
= (wxObject
*)new wxString(val
);
1300 env
.Put(name
, data
);
1302 // return converted env var
1303 return (wxChar
*)((wxString
*)data
)->c_str();
1306 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1308 return system(wxConvLocal
.cWX2MB(psz
));
1311 #endif // wxNEED_WX_STDLIB_H
1313 #ifdef wxNEED_WX_TIME_H
1314 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
1318 char *buf
= (char *)malloc(max
);
1319 size_t ret
= strftime(buf
, max
, wxConvLocal
.cWX2MB(fmt
), tm
);
1322 wxStrcpy(s
, wxConvLocal
.cMB2WX(buf
));
1333 #endif // wxNEED_WX_TIME_H
1335 #endif // wxUSE_WCHAR_T
1337 // ----------------------------------------------------------------------------
1338 // functions which we may need even if !wxUSE_WCHAR_T
1339 // ----------------------------------------------------------------------------
1343 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
1352 psz
+= wxStrspn(psz
, delim
);
1355 *save_ptr
= (wxChar
*)NULL
;
1356 return (wxChar
*)NULL
;
1360 psz
= wxStrpbrk(psz
, delim
);
1363 *save_ptr
= (wxChar
*)NULL
;
1368 *save_ptr
= psz
+ 1;
1376 // ----------------------------------------------------------------------------
1377 // missing C RTL functions
1378 // ----------------------------------------------------------------------------
1380 #if defined( __MWERKS__ ) && !defined(__MACH__)
1381 #if __MSL__ < 0x00008000
1382 char *strdup(const char *s
)
1384 return strcpy( (char*) malloc( strlen( s
) + 1 ) , s
) ;
1387 int isascii( int c
)
1389 return ( c
>= 0 && c
< 128 ) ;
1391 #endif // __MWERKS__