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 // ----------------------------------------------------------------------------
125 // implement [v]snprintf() if the system doesn't provide a safe one
126 // ----------------------------------------------------------------------------
128 #if !defined(wxVsnprintf_)
129 int WXDLLEXPORT
wxVsnprintf_(wxChar
*buf
, size_t lenMax
,
130 const wxChar
*format
, va_list argptr
)
132 // buffer to avoid dynamic memory allocation each time for small strings
133 char szScratch
[1024];
135 // number of characters in the buffer so far, must be less than lenMax
138 for (size_t n
= 0; format
[n
]; n
++)
140 if (format
[n
] == wxT('%')) {
141 static char s_szFlags
[256] = "%";
143 bool adj_left
= FALSE
,
148 size_t min_width
= 0,
149 max_width
= wxSTRING_MAXLEN
;
153 if (in_prec && !prec_dot) \
155 s_szFlags[flagofs++] = '.'; \
159 #define APPEND_CH(ch) \
160 if ( lenCur == lenMax ) \
165 #define APPEND_STR(s) \
166 for ( const char *p = s; *p; p++ ) \
168 APPEND_CH((wchar_t)(*p)); \
171 #define APPEND_WSTR(s) \
172 for ( const wchar_t *p = s; *p; p++ ) \
177 switch (format
[++n
]) {
195 s_szFlags
[flagofs
++] = format
[n
];
201 s_szFlags
[flagofs
++] = format
[n
];
209 // dot will be auto-added to s_szFlags if non-negative number follows
215 s_szFlags
[flagofs
++] = format
[n
];
221 s_szFlags
[flagofs
++] = format
[n
];
228 s_szFlags
[flagofs
++] = format
[n
];
234 s_szFlags
[flagofs
++] = format
[n
];
239 int len
= va_arg(argptr
, int);
246 adj_left
= !adj_left
;
247 s_szFlags
[flagofs
++] = '-';
252 flagofs
+= ::sprintf(s_szFlags
+flagofs
,"%d",len
);
256 case wxT('1'): case wxT('2'): case wxT('3'):
257 case wxT('4'): case wxT('5'): case wxT('6'):
258 case wxT('7'): case wxT('8'): case wxT('9'):
262 while ((format
[n
]>=wxT('0')) && (format
[n
]<=wxT('9'))) {
263 s_szFlags
[flagofs
++] = format
[n
];
264 len
= len
*10 + (format
[n
] - wxT('0'));
267 if (in_prec
) max_width
= len
;
268 else min_width
= len
;
269 n
--; // the main loop pre-increments n again
280 s_szFlags
[flagofs
++] = format
[n
];
281 s_szFlags
[flagofs
] = '\0';
283 int val
= va_arg(argptr
, int);
284 ::sprintf(szScratch
, s_szFlags
, val
);
286 else if (ilen
== -1) {
287 short int val
= va_arg(argptr
, short int);
288 ::sprintf(szScratch
, s_szFlags
, val
);
290 else if (ilen
== 1) {
291 long int val
= va_arg(argptr
, long int);
292 ::sprintf(szScratch
, s_szFlags
, val
);
294 else if (ilen
== 2) {
296 long long int val
= va_arg(argptr
, long long int);
297 ::sprintf(szScratch
, s_szFlags
, val
);
299 long int val
= va_arg(argptr
, long int);
300 ::sprintf(szScratch
, s_szFlags
, val
);
303 else if (ilen
== 3) {
304 size_t val
= va_arg(argptr
, size_t);
305 ::sprintf(szScratch
, s_szFlags
, val
);
308 APPEND_STR(szScratch
);
319 s_szFlags
[flagofs
++] = format
[n
];
320 s_szFlags
[flagofs
] = '\0';
322 long double val
= va_arg(argptr
, long double);
323 ::sprintf(szScratch
, s_szFlags
, val
);
325 double val
= va_arg(argptr
, double);
326 ::sprintf(szScratch
, s_szFlags
, val
);
329 APPEND_STR(szScratch
);
336 void *val
= va_arg(argptr
, void *);
338 s_szFlags
[flagofs
++] = format
[n
];
339 s_szFlags
[flagofs
] = '\0';
340 ::sprintf(szScratch
, s_szFlags
, val
);
342 APPEND_STR(szScratch
);
350 wxChar val
= va_arg(argptr
, int);
351 // we don't need to honor padding here, do we?
360 // wx extension: we'll let %hs mean non-Unicode strings
361 char *val
= va_arg(argptr
, char *);
363 // ASCII->Unicode constructor handles max_width right
364 wxString
s(val
, wxConvLibc
, max_width
);
366 size_t len
= wxSTRING_MAXLEN
;
368 for (len
= 0; val
[len
] && (len
<max_width
); len
++);
369 } else val
= wxT("(null)");
370 wxString
s(val
, len
);
372 if (s
.Len() < min_width
)
373 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
377 wxChar
*val
= va_arg(argptr
, wxChar
*);
378 size_t len
= wxSTRING_MAXLEN
;
380 for (len
= 0; val
[len
] && (len
<max_width
); len
++);
381 } else val
= wxT("(null)");
382 wxString
s(val
, len
);
383 if (s
.Len() < min_width
)
384 s
.Pad(min_width
- s
.Len(), wxT(' '), adj_left
);
393 int *val
= va_arg(argptr
, int *);
396 else if (ilen
== -1) {
397 short int *val
= va_arg(argptr
, short int *);
400 else if (ilen
>= 1) {
401 long int *val
= va_arg(argptr
, long int *);
408 if (wxIsalpha(format
[n
]))
409 // probably some flag not taken care of here yet
410 s_szFlags
[flagofs
++] = format
[n
];
413 APPEND_CH(_T('%')); // just to pass the glibc tst-printf.c
423 APPEND_CH(format
[n
]);
434 #endif // !wxVsnprintfA
436 #if !defined(wxSnprintf_)
437 int WXDLLEXPORT
wxSnprintf_(wxChar
*buf
, size_t len
, const wxChar
*format
, ...)
440 va_start(argptr
, format
);
442 int iLen
= wxVsnprintf_(buf
, len
, format
, argptr
);
448 #endif // wxSnprintf_
450 // ----------------------------------------------------------------------------
451 // implement the standard IO functions for wide char if libc doesn't have them
452 // ----------------------------------------------------------------------------
456 int wxFputs(const wchar_t *ws
, FILE *stream
)
458 // counting the number of wide characters written isn't worth the trouble,
459 // simply distinguish between ok and error
460 return fputs(wxConvLibc
.cWC2MB(ws
), stream
) == -1 ? -1 : 0;
463 int /* not wint_t */ wxPutc(wchar_t wc
, FILE *stream
)
465 wchar_t ws
[2] = { wc
, L
'\0' };
467 return wxFputs(ws
, stream
);
470 #endif // HAVE_FPUTWC
472 // NB: we only implement va_list functions here, the ones taking ... are
473 // defined below for wxNEED_PRINTF_CONVERSION case anyhow and we reuse
474 // the definitions there to avoid duplicating them here
475 #ifdef wxNEED_WPRINTF
477 // TODO: implement the scanf() functions
478 int vwscanf(const wchar_t *format
, va_list argptr
)
480 wxFAIL_MSG( _T("TODO") );
485 int vswscanf(const wchar_t *ws
, const wchar_t *format
, va_list argptr
)
487 wxFAIL_MSG( _T("TODO") );
492 int vfwscanf(FILE *stream
, const wchar_t *format
, va_list argptr
)
494 wxFAIL_MSG( _T("TODO") );
499 #define vswprintf wxVsnprintf_
501 int vfwprintf(FILE *stream
, const wchar_t *format
, va_list argptr
)
504 int rc
= s
.PrintfV(format
, argptr
);
508 // we can't do much better without Unicode support in libc...
509 if ( fprintf(stream
, s
.mb_str()) == -1 )
516 int vwprintf(const wchar_t *format
, va_list argptr
)
518 return wxVfprintf(stdout
, format
, argptr
);
521 #endif // wxNEED_WPRINTF
523 #ifdef wxNEED_PRINTF_CONVERSION
525 // ----------------------------------------------------------------------------
526 // wxFormatConverter: class doing the "%s" -> "%ls" conversion
527 // ----------------------------------------------------------------------------
530 Here are the gory details. We want to follow the Windows/MS conventions,
535 format specifier results in
536 -----------------------------------
542 format specifier results in
543 -----------------------------------
548 while on POSIX systems we have %C identical to %lc and %c always means char
549 (in any mode) while %lc always means wchar_t,
551 So to use native functions in order to get our semantics we must do the
552 following translations in Unicode mode (nothing to do in ANSI mode):
554 wxWindows specifier POSIX specifier
555 ----------------------------------------
561 And, of course, the same should be done for %s as well.
564 class wxFormatConverter
567 wxFormatConverter(const wxChar
*format
);
569 // notice that we only translated the string if m_fmtOrig == NULL (as set
570 // by CopyAllBefore()), otherwise we should simply use the original format
571 operator const wxChar
*() const
572 { return m_fmtOrig
? m_fmtOrig
: m_fmt
.c_str(); }
575 // copy another character to the translated format: this function does the
576 // copy if we are translating but doesn't do anything at all if we don't,
577 // so we don't create the translated format string at all unless we really
578 // need to (i.e. InsertFmtChar() is called)
579 wxChar
CopyFmtChar(wxChar ch
)
583 // we're translating, do copy
588 // simply increase the count which should be copied by
589 // CopyAllBefore() later if needed
596 // insert an extra character
597 void InsertFmtChar(wxChar ch
)
601 // so far we haven't translated anything yet
610 wxASSERT_MSG( m_fmtOrig
&& m_fmt
.empty(), _T("logic error") );
612 m_fmt
= wxString(m_fmtOrig
, m_nCopied
);
614 // we won't need it any longer
618 static bool IsFlagChar(wxChar ch
)
620 return ch
== _T('-') || ch
== _T('+') ||
621 ch
== _T('0') || ch
== _T(' ') || ch
== _T('#');
624 void SkipDigits(const wxChar
**ppc
)
626 while ( **ppc
>= _T('0') && **ppc
<= _T('9') )
627 CopyFmtChar(*(*ppc
)++);
630 // the translated format
633 // the original format
634 const wxChar
*m_fmtOrig
;
636 // the number of characters already copied
640 wxFormatConverter::wxFormatConverter(const wxChar
*format
)
647 if ( CopyFmtChar(*format
++) == _T('%') )
650 while ( IsFlagChar(*format
) )
651 CopyFmtChar(*format
++);
653 // and possible width
654 if ( *format
== _T('*') )
655 CopyFmtChar(*format
++);
660 if ( *format
== _T('.') )
665 // next we can have a size modifier
681 // "ll" has a different meaning!
682 if ( format
[1] != _T('l') )
694 // and finally we should have the type
699 // %C and %hC -> %c and %lC -> %lc
701 CopyFmtChar(_T('l'));
703 InsertFmtChar(*format
++ == _T('C') ? _T('c') : _T('s'));
708 // %c -> %lc but %hc stays %hc and %lc is still %lc
712 InsertFmtChar(_T('l'));
716 CopyFmtChar(_T('h'));
725 // nothing special to do
726 CopyFmtChar(*format
++);
732 #else // !wxNEED_PRINTF_CONVERSION
733 // no conversion necessary
734 #define wxFormatConverter(x) (x)
735 #endif // wxNEED_PRINTF_CONVERSION/!wxNEED_PRINTF_CONVERSION
737 // ----------------------------------------------------------------------------
738 // wxPrintf(), wxScanf() and relatives
739 // ----------------------------------------------------------------------------
741 #if defined(wxNEED_PRINTF_CONVERSION) || defined(wxNEED_WPRINTF)
743 int wxScanf( const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_2
746 va_start(argptr
, format
);
748 int ret
= vwscanf(wxFormatConverter(format
), argptr
);
755 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
758 va_start(argptr
, format
);
760 int ret
= vswscanf( str
, wxFormatConverter(format
), argptr
);
767 int wxFscanf( FILE *stream
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
770 va_start(argptr
, format
);
772 int ret
= vfwscanf(stream
, wxFormatConverter(format
), argptr
);
779 int wxPrintf( const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_2
782 va_start(argptr
, format
);
784 int ret
= vwprintf( wxFormatConverter(format
), argptr
);
792 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_4
795 va_start(argptr
, format
);
797 int ret
= vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
805 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
808 va_start(argptr
, format
);
810 // callers of wxSprintf() deserve what they get
811 int ret
= vswprintf( str
, UINT_MAX
, wxFormatConverter(format
), argptr
);
818 int wxFprintf( FILE *stream
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
821 va_start( argptr
, format
);
823 int ret
= vfwprintf( stream
, wxFormatConverter(format
), argptr
);
830 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list argptr
)
832 return vswscanf( str
, wxFormatConverter(format
), argptr
);
835 int wxVfprintf( FILE *stream
, const wxChar
*format
, va_list argptr
)
837 return vfwprintf( stream
, wxFormatConverter(format
), argptr
);
840 int wxVprintf( const wxChar
*format
, va_list argptr
)
842 return vwprintf( wxFormatConverter(format
), argptr
);
846 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list argptr
)
848 return vswprintf( str
, size
, wxFormatConverter(format
), argptr
);
850 #endif // wxVsnprintf
852 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list argptr
)
854 // same as for wxSprintf()
855 return vswprintf(str
, UINT_MAX
, wxFormatConverter(format
), argptr
);
858 #endif // wxNEED_PRINTF_CONVERSION
860 // ----------------------------------------------------------------------------
861 // ctype.h stuff (currently unused)
862 // ----------------------------------------------------------------------------
864 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
865 inline WORD
wxMSW_ctype(wxChar ch
)
868 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
872 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
873 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
874 WXDLLEXPORT
int wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
875 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
876 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
877 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
878 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
879 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
880 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
881 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
882 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
883 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
884 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
888 WXDLLEXPORT wxChar
* wxStrdup(const wxChar
*psz
)
890 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
891 wxChar
*ret
= (wxChar
*) malloc(size
);
892 memcpy(ret
, psz
, size
);
898 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
900 register wxChar c1
, c2
;
902 c1
= wxTolower(*psz1
++);
903 c2
= wxTolower(*psz2
++);
904 } while ( c1
&& (c1
== c2
) );
910 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
912 register wxChar c1
, c2
;
913 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
915 if (c1
< c2
) return -1;
916 if (c1
> c2
) return 1;
923 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
925 if (!psz
) psz
= *save_ptr
;
926 psz
+= wxStrspn(psz
, delim
);
928 *save_ptr
= (wxChar
*)NULL
;
929 return (wxChar
*)NULL
;
932 psz
= wxStrpbrk(psz
, delim
);
933 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
943 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
945 char *localeOld
= setlocale(category
, wxConvLocal
.cWX2MB(locale
));
947 return wxWCharBuffer(wxConvLocal
.cMB2WC(localeOld
));
951 // ----------------------------------------------------------------------------
952 // string.h functions
953 // ----------------------------------------------------------------------------
955 #ifdef wxNEED_WX_STRING_H
956 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
959 while (*dest
) dest
++;
960 while ((*dest
++ = *src
++));
964 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
966 // be careful here as the terminating NUL makes part of the string
976 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
978 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
979 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
980 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
984 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
987 while ((*dest
++ = *src
++));
991 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
994 while (*dest
) dest
++;
995 while (n
&& (*dest
++ = *src
++)) n
--;
999 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
1001 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
1003 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
1004 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
1009 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
1012 while (n
&& (*dest
++ = *src
++)) n
--;
1013 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
1017 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
1019 while (*s
&& !wxStrchr(accept
, *s
))
1022 return *s
? s
: NULL
;
1025 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
1027 const wxChar
*ret
= NULL
;
1039 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
1042 while (wxStrchr(accept
, *s
++)) len
++;
1046 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
1048 wxCHECK_RET( needle
, NULL
, _T("NULL argument in wxStrstr") );
1050 // VZ: this is not exactly the most efficient string search algorithm...
1052 const size_t len
= wxStrlen(needle
);
1054 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
1056 if ( !wxStrncmp(fnd
, needle
, len
) )
1065 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
1067 const wxChar
*start
= nptr
;
1069 // FIXME: only correct for C locale
1070 while (wxIsspace(*nptr
)) nptr
++;
1071 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1072 while (wxIsdigit(*nptr
)) nptr
++;
1073 if (*nptr
== wxT('.')) {
1075 while (wxIsdigit(*nptr
)) nptr
++;
1077 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
1079 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1080 while (wxIsdigit(*nptr
)) nptr
++;
1083 wxString
data(nptr
, nptr
-start
);
1084 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1085 char *rdat
= wxMBSTRINGCAST dat
;
1086 double ret
= strtod(dat
, &rdat
);
1088 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1093 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
1095 const wxChar
*start
= nptr
;
1097 // FIXME: only correct for C locale
1098 while (wxIsspace(*nptr
)) nptr
++;
1099 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
1100 if (((base
== 0) || (base
== 16)) &&
1101 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
1105 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
1106 else if (base
== 0) base
= 10;
1108 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
1109 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
1111 wxString
data(nptr
, nptr
-start
);
1112 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
1113 char *rdat
= wxMBSTRINGCAST dat
;
1114 long int ret
= strtol(dat
, &rdat
, base
);
1116 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
1120 #endif // wxNEED_WX_STRING_H
1122 #ifdef wxNEED_WX_STDIO_H
1123 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
1125 char mode_buffer
[10];
1126 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1127 mode_buffer
[i
] = (char) mode
[i
];
1129 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
1132 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
1134 char mode_buffer
[10];
1135 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
1136 mode_buffer
[i
] = (char) mode
[i
];
1138 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
1141 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
1143 return remove( wxConvFile
.cWX2MB(path
) );
1146 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
1148 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
1153 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
1155 return atof(wxConvLocal
.cWX2MB(psz
));
1159 #ifdef wxNEED_WX_STDLIB_H
1160 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
1162 return atoi(wxConvLocal
.cWX2MB(psz
));
1165 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
1167 return atol(wxConvLocal
.cWX2MB(psz
));
1170 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
1172 static wxHashTable env
;
1174 // check if we already have stored the converted env var
1175 wxObject
*data
= env
.Get(name
);
1178 // nope, retrieve it,
1180 wxCharBuffer buffer
= wxConvLocal
.cWX2MB(name
);
1181 // printf( "buffer %s\n", (const char*) buffer );
1182 const char *val
= getenv( (const char *)buffer
);
1184 const char *val
= getenv( name
);
1187 if (!val
) return (wxChar
*)NULL
;
1188 // printf( "home %s\n", val );
1191 #ifdef wxUSE_UNICODE
1192 data
= (wxObject
*)new wxString(val
, wxConvLocal
);
1194 data
= (wxObject
*)new wxString(val
);
1198 env
.Put(name
, data
);
1200 // return converted env var
1201 return (wxChar
*)((wxString
*)data
)->c_str();
1204 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
1206 return system(wxConvLocal
.cWX2MB(psz
));
1211 #ifdef wxNEED_WX_TIME_H
1212 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
1216 char *buf
= (char *)malloc(max
);
1217 size_t ret
= strftime(buf
, max
, wxConvLocal
.cWX2MB(fmt
), tm
);
1220 wxStrcpy(s
, wxConvLocal
.cMB2WX(buf
));