1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxChar implementation
8 // Copyright: (c) wxWindows copyright
9 // Licence: wxWindows license
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)
51 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
55 if (n
) *buf
= wxT('\0');
58 return mbstowcs(buf
, psz
, n
);
61 // assume that we have mbsrtowcs() too if we have wcsrtombs()
64 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
66 return mbstowcs((wchar_t *) NULL
, psz
, 0);
70 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
74 // glibc2.1 chokes on null input
78 return wcstombs(buf
, pwz
, n
);
83 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
85 return wcstombs((char *) NULL
, pwz
, 0);
88 #endif // wxUSE_WCHAR_T
90 bool WXDLLEXPORT
wxOKlibc()
92 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__)
93 // glibc 2.0 uses UTF-8 even when it shouldn't
95 if ((MB_CUR_MAX
== 2) &&
96 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
98 // this is UTF-8 allright, check whether that's what we want
99 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
100 if ((strlen(cur_locale
) < 4) ||
101 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
102 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
103 // nope, don't use libc conversion
112 size_t WXDLLEXPORT
wcslen(const wchar_t *s
)
115 while (s
[len
]) len
++;
120 // ============================================================================
121 // printf() functions business
122 // ============================================================================
124 // special test mode: define all functions below even if we don't really need
125 // them to be able to test them
136 #define wxNEED_WPRINTF
138 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
);
141 // ----------------------------------------------------------------------------
142 // implement [v]snprintf() if the system doesn't provide a safe one
143 // ----------------------------------------------------------------------------
145 #if !defined(wxVsnprintf_)
146 int WXDLLEXPORT
wxVsnprintf_(wxChar
*buf
, size_t lenMax
,
147 const wxChar
*format
, va_list argptr
)
149 // buffer to avoid dynamic memory allocation each time for small strings
150 char szScratch
[1024];
152 // number of characters in the buffer so far, must be less than lenMax
155 for ( size_t n
= 0; ; n
++ )
157 const wxChar chCur
= format
[n
];
159 if ( chCur
== wxT('%') )
161 static char s_szFlags
[256] = "%";
163 bool adj_left
= FALSE
,
168 size_t min_width
= 0,
169 max_width
= wxSTRING_MAXLEN
;
174 if (in_prec && !prec_dot) \
176 s_szFlags[flagofs++] = '.'; \
180 #define APPEND_CH(ch) \
181 if ( lenCur == lenMax ) \
186 #define APPEND_STR(s) \
188 for ( const wxChar *p = s; *p; p++ ) \
195 const wxChar ch
= format
[++n
];
215 s_szFlags
[flagofs
++] = ch
;
221 s_szFlags
[flagofs
++] = ch
;
229 // dot will be auto-added to s_szFlags if non-negative
236 s_szFlags
[flagofs
++] = ch
;
242 s_szFlags
[flagofs
++] = ch
;
249 s_szFlags
[flagofs
++] = ch
;
255 s_szFlags
[flagofs
++] = ch
;
260 int len
= va_arg(argptr
, int);
271 adj_left
= !adj_left
;
272 s_szFlags
[flagofs
++] = '-';
277 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
281 case wxT('1'): case wxT('2'): case wxT('3'):
282 case wxT('4'): case wxT('5'): case wxT('6'):
283 case wxT('7'): case wxT('8'): case wxT('9'):
287 while ( (format
[n
] >= wxT('0')) &&
288 (format
[n
] <= wxT('9')) )
290 s_szFlags
[flagofs
++] = format
[n
];
291 len
= len
*10 + (format
[n
] - wxT('0'));
300 n
--; // the main loop pre-increments n again
311 s_szFlags
[flagofs
++] = ch
;
312 s_szFlags
[flagofs
] = '\0';
315 int val
= va_arg(argptr
, int);
316 ::sprintf(szScratch
, s_szFlags
, val
);
320 // NB: 'short int' value passed through '...'
321 // is promoted to 'int', so we have to get
322 // an int from stack even if we need a short
323 short int val
= (short int) va_arg(argptr
, int);
324 ::sprintf(szScratch
, s_szFlags
, val
);
328 long int val
= va_arg(argptr
, long int);
329 ::sprintf(szScratch
, s_szFlags
, val
);
334 long long int val
= va_arg(argptr
, long long int);
335 ::sprintf(szScratch
, s_szFlags
, val
);
337 long int val
= va_arg(argptr
, long int);
338 ::sprintf(szScratch
, s_szFlags
, val
);
339 #endif // long long/!long long
343 size_t val
= va_arg(argptr
, size_t);
344 ::sprintf(szScratch
, s_szFlags
, val
);
348 const wxMB2WXbuf tmp
=
349 wxConvLibc
.cMB2WX(szScratch
);
362 s_szFlags
[flagofs
++] = ch
;
363 s_szFlags
[flagofs
] = '\0';
366 long double val
= va_arg(argptr
, long double);
367 ::sprintf(szScratch
, s_szFlags
, val
);
371 double val
= va_arg(argptr
, double);
372 ::sprintf(szScratch
, s_szFlags
, val
);
376 const wxMB2WXbuf tmp
=
377 wxConvLibc
.cMB2WX(szScratch
);
386 void *val
= va_arg(argptr
, void *);
388 s_szFlags
[flagofs
++] = ch
;
389 s_szFlags
[flagofs
] = '\0';
390 ::sprintf(szScratch
, s_szFlags
, val
);
392 const wxMB2WXbuf tmp
=
393 wxConvLibc
.cMB2WX(szScratch
);
402 wxChar val
= va_arg(argptr
, int);
403 // we don't need to honor padding here, do we?
413 // wx extension: we'll let %hs mean non-Unicode
415 char *val
= va_arg(argptr
, char *);
417 // ASCII->Unicode constructor handles max_width
419 wxString
s(val
, wxConvLibc
, max_width
);
421 size_t len
= wxSTRING_MAXLEN
;
425 val
[len
] && (len
< max_width
);
431 wxString
s(val
, len
);
433 if (s
.Len() < min_width
)
434 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
440 wxChar
*val
= va_arg(argptr
, wxChar
*);
441 size_t len
= wxSTRING_MAXLEN
;
445 val
[len
] && (len
< max_width
);
452 wxString
s(val
, len
);
453 if (s
.Len() < min_width
)
454 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
464 int *val
= va_arg(argptr
, int *);
469 short int *val
= va_arg(argptr
, short int *);
474 long int *val
= va_arg(argptr
, long int *);
481 // bad format, leave unchanged
507 #endif // !wxVsnprintfA
509 #if !defined(wxSnprintf_)
510 int WXDLLEXPORT
wxSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
513 va_start(argptr
, format
);
515 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
521 #endif // wxSnprintf_
523 // ----------------------------------------------------------------------------
524 // implement the standard IO functions for wide char if libc doesn't have them
525 // ----------------------------------------------------------------------------
529 int wxFputs(const wchar_t *ws
, FILE *stream
)
531 // counting the number of wide characters written isn't worth the trouble,
532 // simply distinguish between ok and error
533 return fputs(wxConvLibc
.cWC2MB(ws
), stream
) == -1 ? -1 : 0;
536 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
538 wchar_t ws
[2] = { wc
, L
'\0' };
540 return wxFputs(ws
, stream
);
543 #endif // wxNEED_FPUTWC
545 // NB: we only implement va_list functions here, the ones taking ... are
546 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
547 // the definitions there to avoid duplicating them here
548 #ifdef wxNEED_WPRINTF
550 // TODO: implement the scanf() functions
551 int vwscanf(const wxChar
*format
, va_list argptr
)
553 wxFAIL_MSG( _T("TODO") );
558 int vswscanf(const wxChar
*ws
, const wxChar
*format
, va_list argptr
)
560 wxFAIL_MSG( _T("TODO") );
565 int vfwscanf(FILE *stream
, const wxChar
*format
, va_list argptr
)
567 wxFAIL_MSG( _T("TODO") );
572 #define vswprintf wxVsnprintf_
574 int vfwprintf(FILE *stream
, const wxChar
*format
, va_list argptr
)
577 int rc
= s
.PrintfV(format
, argptr
);
581 // we can't do much better without Unicode support in libc...
582 if ( fprintf(stream
, "%s", s
.mb_str()) == -1 )
589 int vwprintf(const wxChar
*format
, va_list argptr
)
591 return wxVfprintf(stdout
, format
, argptr
);
594 #endif // wxNEED_WPRINTF
596 #ifdef wxNEED_PRINTF_CONVERSION
598 // ----------------------------------------------------------------------------
599 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
600 // ----------------------------------------------------------------------------
603 Here are the gory details. We want to follow the Windows/MS conventions,
608 format specifier results in
609 -----------------------------------
615 format specifier results in
616 -----------------------------------
621 while on POSIX systems we have %C identical to %lc and %c always means char
622 (in any mode) while %lc always means wchar_t,
624 So to use native functions in order to get our semantics we must do the
625 following translations in Unicode mode (nothing to do in ANSI mode):
627 wxWindows specifier POSIX specifier
628 ----------------------------------------
634 And, of course, the same should be done for %s as well.
637 class wxFormatConverter
640 wxFormatConverter(const wxChar
*format
);
642 // notice that we only translated the string if m_fmtOrig == NULL (as set
643 // by CopyAllBefore()), otherwise we should simply use the original format
644 operator const wxChar
*() const
645 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
648 // copy another character to the translated format: this function does the
649 // copy if we are translating but doesn't do anything at all if we don't,
650 // so we don't create the translated format string at all unless we really
651 // need to (i.e. InsertFmtChar() is called)
652 wxChar
CopyFmtChar(wxChar ch
)
656 // we're translating, do copy
661 // simply increase the count which should be copied by
662 // CopyAllBefore() later if needed
669 // insert an extra character
670 void InsertFmtChar(wxChar ch
)
674 // so far we haven't translated anything yet
683 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
685 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
687 // we won't need it any longer
691 static bool IsFlagChar(wxChar ch
)
693 return ch
== _T('-') || ch
== _T('+') ||
694 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
697 void SkipDigits(const wxChar
**ppc
)
699 while ( **ppc
>= _T('0') && **ppc
<= _T('9') )
700 CopyFmtChar(*(*ppc
)++);
703 // the translated format
706 // the original format
707 const wxChar
*m_fmtOrig
;
709 // the number of characters already copied
713 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
720 if ( CopyFmtChar(*format
++) == _T('%') )
723 while ( IsFlagChar(*format
) )
724 CopyFmtChar(*format
++);
726 // and possible width
727 if ( *format
== _T('*') )
728 CopyFmtChar(*format
++);
733 if ( *format
== _T('.') )
738 // next we can have a size modifier
754 // "ll" has a different meaning!
755 if ( format
[1] != _T('l') )
767 // and finally we should have the type
772 // %C and %hC -> %c and %lC -> %lc
774 CopyFmtChar(_T('l'));
776 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
781 // %c -> %lc but %hc stays %hc and %lc is still %lc
785 InsertFmtChar(_T('l'));
789 CopyFmtChar(_T('h'));
798 // nothing special to do
799 CopyFmtChar(*format
++);
805 #else // !wxNEED_PRINTF_CONVERSION
806 // no conversion necessary
807 #define wxFormatConverter(x) (x)
808 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
810 // ----------------------------------------------------------------------------
811 // wxPrintf(), wxScanf() and relatives
812 // ----------------------------------------------------------------------------
814 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
816 int wxScanf( const wxChar
*format
, ... )
819 va_start(argptr
, format
);
821 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
828 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... )
831 va_start(argptr
, format
);
833 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
840 int wxFscanf( FILE *stream
, const wxChar
*format
, ... )
843 va_start(argptr
, format
);
845 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
852 int wxPrintf( const wxChar
*format
, ... )
855 va_start(argptr
, format
);
857 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
865 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... )
868 va_start(argptr
, format
);
870 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
878 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... )
881 va_start(argptr
, format
);
883 // callers of wxSprintf() deserve what they get
884 int ret
= vswprintf( str
, UINT_MAX
, wxFormatConverter(format
), argptr
);
891 int wxFprintf( FILE *stream
, const wxChar
*format
, ... )
894 va_start( argptr
, format
);
896 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
903 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
905 return vswscanf( str
, wxFormatConverter(format
), argptr
);
908 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
910 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
913 int wxVprintf( const wxChar
*format
, va_list argptr
)
915 return vwprintf( wxFormatConverter(format
), argptr
);
919 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
921 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
923 #endif // wxVsnprintf
925 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
927 // same as for wxSprintf()
928 return vswprintf(str
, UINT_MAX
, wxFormatConverter(format
), argptr
);
931 #endif // wxNEED_PRINTF_CONVERSION
933 // ----------------------------------------------------------------------------
934 // ctype.h stuff (currently unused)
935 // ----------------------------------------------------------------------------
937 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
938 inline WORD
wxMSW_ctype(wxChar ch
)
941 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
945 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
946 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
947 WXDLLEXPORT
int wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
948 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
949 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
950 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
951 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
952 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
953 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
954 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
955 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
956 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
957 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
961 WXDLLEXPORT wxChar
* wxStrdup(const wxChar
*psz
)
963 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
964 wxChar
*ret
= (wxChar
*) malloc(size
);
965 memcpy(ret
, psz
, size
);
971 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
973 register wxChar c1
, c2
;
975 c1
= wxTolower(*psz1
++);
976 c2
= wxTolower(*psz2
++);
977 } while ( c1
&& (c1
== c2
) );
983 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
985 register wxChar c1
, c2
;
986 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
988 if (c1
< c2
) return -1;
989 if (c1
> c2
) return 1;
996 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
1005 psz
+= wxStrspn(psz
, delim
);
1008 *save_ptr
= (wxChar
*)NULL
;
1009 return (wxChar
*)NULL
;
1013 psz
= wxStrpbrk(psz
, delim
);
1016 *save_ptr
= (wxChar
*)NULL
;
1021 *save_ptr
= psz
+ 1;
1029 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
1031 char *localeOld
= setlocale(category
, wxConvLocal
.cWX2MB(locale
));
1033 return wxWCharBuffer(wxConvLocal
.cMB2WC(localeOld
));
1037 // ----------------------------------------------------------------------------
1038 // string.h functions
1039 // ----------------------------------------------------------------------------
1041 #ifdef wxNEED_WX_STRING_H
1042 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
1045 while (*dest
) dest
++;
1046 while ((*dest
++ = *src
++));
1050 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
1052 // be careful here as the terminating NUL makes part of the string
1062 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
1064 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
1065 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1066 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1070 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
1073 while ((*dest
++ = *src
++));
1077 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
1080 while (*dest
) dest
++;
1081 while (n
&& (*dest
++ = *src
++)) n
--;
1085 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1087 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1089 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1090 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1095 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1098 while (n
&& (*dest
++ = *src
++)) n
--;
1099 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1103 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1105 while (*s
&& !wxStrchr(accept
, *s
))
1108 return *s
? s
: NULL
;
1111 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1113 const wxChar
*ret
= NULL
;
1125 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1128 while (wxStrchr(accept
, *s
++)) len
++;
1132 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1134 wxCHECK_RET( needle
, NULL
, _T("NULL argument in wxStrstr") );
1136 // VZ: this is not exactly the most efficient string search algorithm...
1138 const size_t len
= wxStrlen(needle
);
1140 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1142 if ( !wxStrncmp(fnd
, needle
, len
) )
1151 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1153 const wxChar
*start
= nptr
;
1155 // FIXME: only correct for C locale
1156 while (wxIsspace(*nptr
)) nptr
++;
1157 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1158 while (wxIsdigit(*nptr
)) nptr
++;
1159 if (*nptr
== wxT('.')) {
1161 while (wxIsdigit(*nptr
)) nptr
++;
1163 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1165 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1166 while (wxIsdigit(*nptr
)) nptr
++;
1169 wxString
data(nptr
, nptr
-start
);
1170 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1171 char *rdat
= wxMBSTRINGCAST dat
;
1172 double ret
= strtod(dat
, &rdat
);
1174 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1179 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1181 const wxChar
*start
= nptr
;
1183 // FIXME: only correct for C locale
1184 while (wxIsspace(*nptr
)) nptr
++;
1185 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1186 if (((base
== 0) || (base
== 16)) &&
1187 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1191 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1192 else if (base
== 0) base
= 10;
1194 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1195 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1197 wxString
data(nptr
, nptr
-start
);
1198 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1199 char *rdat
= wxMBSTRINGCAST dat
;
1200 long int ret
= strtol(dat
, &rdat
, base
);
1202 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1206 #endif // wxNEED_WX_STRING_H
1208 #ifdef wxNEED_WX_STDIO_H
1209 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1211 char mode_buffer
[10];
1212 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1213 mode_buffer
[i
] = (char) mode
[i
];
1215 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1218 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1220 char mode_buffer
[10];
1221 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1222 mode_buffer
[i
] = (char) mode
[i
];
1224 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1227 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1229 return remove( wxConvFile
.cWX2MB(path
) );
1232 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1234 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1239 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1241 return atof(wxConvLocal
.cWX2MB(psz
));
1245 #ifdef wxNEED_WX_STDLIB_H
1246 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1248 return atoi(wxConvLocal
.cWX2MB(psz
));
1251 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1253 return atol(wxConvLocal
.cWX2MB(psz
));
1256 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1258 static wxHashTable env
;
1260 // check if we already have stored the converted env var
1261 wxObject
*data
= env
.Get(name
);
1264 // nope, retrieve it,
1266 wxCharBuffer buffer
= wxConvLocal
.cWX2MB(name
);
1267 // printf( "buffer %s\n", (const char*) buffer );
1268 const char *val
= getenv( (const char *)buffer
);
1270 const char *val
= getenv( name
);
1273 if (!val
) return (wxChar
*)NULL
;
1274 // printf( "home %s\n", val );
1277 #ifdef wxUSE_UNICODE
1278 data
= (wxObject
*)new wxString(val
, wxConvLocal
);
1280 data
= (wxObject
*)new wxString(val
);
1284 env
.Put(name
, data
);
1286 // return converted env var
1287 return (wxChar
*)((wxString
*)data
)->c_str();
1290 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1292 return system(wxConvLocal
.cWX2MB(psz
));
1297 #ifdef wxNEED_WX_TIME_H
1298 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
1302 char *buf
= (char *)malloc(max
);
1303 size_t ret
= strftime(buf
, max
, wxConvLocal
.cWX2MB(fmt
), tm
);
1306 wxStrcpy(s
, wxConvLocal
.cMB2WX(buf
));