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 #include "wx/msgdlg.h"
45 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
53 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
57 if (n
) *buf
= wxT('\0');
60 return mbstowcs(buf
, psz
, n
);
63 // assume that we have mbsrtowcs() too if we have wcsrtombs()
66 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
68 return mbstowcs((wchar_t *) NULL
, psz
, 0);
72 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
76 // glibc2.1 chokes on null input
80 return wcstombs(buf
, pwz
, n
);
85 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
87 return wcstombs((char *) NULL
, pwz
, 0);
90 #endif // wxUSE_WCHAR_T
92 bool WXDLLEXPORT
wxOKlibc()
94 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__)
95 // glibc 2.0 uses UTF-8 even when it shouldn't
97 if ((MB_CUR_MAX
== 2) &&
98 (wxMB2WC(&res
, "\xdd\xa5", 1) == 1) &&
100 // this is UTF-8 allright, check whether that's what we want
101 char *cur_locale
= setlocale(LC_CTYPE
, NULL
);
102 if ((strlen(cur_locale
) < 4) ||
103 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8")) ||
104 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 5, "utf-8"))) {
105 // nope, don't use libc conversion
114 size_t WXDLLEXPORT
wcslen(const wchar_t *s
)
117 while (s
[len
]) len
++;
122 #ifdef wxNEED_PRINTF_CONVERSION
124 #define CONVERT_FORMAT_1 \
125 wxChar *new_format = (wxChar*) format; \
126 size_t old_len = wxStrlen( format ); \
129 for (i = 0; i < old_len; i++) \
131 if ( (format[i] == L'%') && \
132 ((i < old_len) && ((format[i+1] == L's') || (format[i+1] == L'c'))) && \
133 ((i == 0) || (format[i-1] != L'%')) ) \
141 new_format = new wxChar[old_len+n+1]; \
142 wxChar *s = new_format; \
144 for (i = 0; i < old_len+1; i++) \
146 if ( (format[i] == L'%') && \
147 ((i < old_len) && ((format[i+1] == L's') || (format[i+1] == L'c'))) && \
148 ((i == 0) || (format[i-1] != L'%')) ) \
163 #define CONVERT_FORMAT_2 \
165 delete [] new_format;
168 int wxScanf( const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_2
173 va_start(argptr
, format
);
175 int ret
= vwscanf( new_format
, argptr
);
184 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
189 va_start(argptr
, format
);
191 int ret
= vswscanf( str
, new_format
, argptr
);
200 int wxFscanf( FILE *stream
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
205 va_start(argptr
, format
);
207 int ret
= vfwscanf(stream
, new_format
, argptr
);
216 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list ap
)
220 int ret
= vswscanf( str
, new_format
, ap
);
227 int wxPrintf( const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_2
232 va_start(argptr
, format
);
234 int ret
= vwprintf( new_format
, argptr
);
243 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_4
248 va_start(argptr
, format
);
250 int ret
= vswprintf( str
, size
, new_format
, argptr
);
259 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
264 va_start(argptr
, format
);
267 int ret
= vswprintf( str
, 10000, new_format
, argptr
);
276 int wxFprintf( FILE *stream
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
281 va_start( argptr
, format
);
283 int ret
= vfwprintf( stream
, new_format
, argptr
);
292 int wxVfprint( FILE *stream
, const wxChar
*format
, va_list ap
)
296 int ret
= vfwprintf( stream
, new_format
, ap
);
303 int wxVprintf( const wxChar
*format
, va_list ap
)
307 int ret
= vwprintf( new_format
, ap
);
314 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list ap
)
318 int ret
= vswprintf( str
, size
, new_format
, ap
);
325 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list ap
)
330 int ret
= vswprintf(str
, 10000, new_format
, ap
);
338 #if !defined(wxVsnprintf) && !defined(wxHAS_VSNPRINTF)
339 int WXDLLEXPORT
wxVsnprintf(wxChar
*buf
, size_t len
,
340 const wxChar
*format
, va_list argptr
)
344 int iLen
= s
.PrintfV(format
, argptr
);
347 wxStrncpy(buf
, s
.c_str(), len
);
348 buf
[len
-1] = wxT('\0');
353 // vsnprintf() will not terminate the string with '\0' if there is not
354 // enough place, but we want the string to always be NUL terminated
355 int rc
= wxVsnprintfA(buf
, len
- 1, format
, argptr
);
362 #endif // Unicode/ANSI
366 #if !defined(wxSnprintf) && !defined(wxHAS_SNPRINTF)
367 int WXDLLEXPORT
wxSnprintf(wxChar
*buf
, size_t len
,
368 const wxChar
*format
, ...)
371 va_start(argptr
, format
);
373 int iLen
= wxVsnprintf(buf
, len
, format
, argptr
);
381 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
382 inline WORD
wxMSW_ctype(wxChar ch
)
385 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
389 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
390 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
391 WXDLLEXPORT
int wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
392 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
393 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
394 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
395 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
396 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
397 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
398 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
399 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
400 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
401 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
405 WXDLLEXPORT wxChar
* wxStrdup(const wxChar
*psz
)
407 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
408 wxChar
*ret
= (wxChar
*) malloc(size
);
409 memcpy(ret
, psz
, size
);
415 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
417 register wxChar c1
, c2
;
419 c1
= wxTolower(*psz1
++);
420 c2
= wxTolower(*psz2
++);
421 } while ( c1
&& (c1
== c2
) );
427 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
429 register wxChar c1
, c2
;
430 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
432 if (c1
< c2
) return -1;
433 if (c1
> c2
) return 1;
440 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
442 if (!psz
) psz
= *save_ptr
;
443 psz
+= wxStrspn(psz
, delim
);
445 *save_ptr
= (wxChar
*)NULL
;
446 return (wxChar
*)NULL
;
449 psz
= wxStrpbrk(psz
, delim
);
450 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
460 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
462 char *localeOld
= setlocale(category
, wxConvLocal
.cWX2MB(locale
));
464 return wxWCharBuffer(wxConvLocal
.cMB2WC(localeOld
));
468 #ifdef wxNEED_WX_STRING_H
469 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
472 while (*dest
) dest
++;
473 while ((*dest
++ = *src
++));
477 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
479 // be careful here as the terminating NUL makes part of the string
489 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
491 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
492 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
493 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
497 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
500 while ((*dest
++ = *src
++));
504 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
507 while (*dest
) dest
++;
508 while (n
&& (*dest
++ = *src
++)) n
--;
512 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
514 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
516 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
517 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
522 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
525 while (n
&& (*dest
++ = *src
++)) n
--;
526 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
530 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
532 while (*s
&& !wxStrchr(accept
, *s
))
535 return *s
? s
: NULL
;
538 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
540 const wxChar
*ret
= NULL
;
552 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
555 while (wxStrchr(accept
, *s
++)) len
++;
559 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
561 wxCHECK_RET( needle
, NULL
, _T("NULL argument in wxStrstr") );
563 // VZ: this is not exactly the most efficient string search algorithm...
565 const size_t len
= wxStrlen(needle
);
567 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
569 if ( !wxStrncmp(fnd
, needle
, len
) )
578 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
580 const wxChar
*start
= nptr
;
582 // FIXME: only correct for C locale
583 while (wxIsspace(*nptr
)) nptr
++;
584 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
585 while (wxIsdigit(*nptr
)) nptr
++;
586 if (*nptr
== wxT('.')) {
588 while (wxIsdigit(*nptr
)) nptr
++;
590 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
592 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
593 while (wxIsdigit(*nptr
)) nptr
++;
596 wxString
data(nptr
, nptr
-start
);
597 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
598 char *rdat
= wxMBSTRINGCAST dat
;
599 double ret
= strtod(dat
, &rdat
);
601 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
606 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
608 const wxChar
*start
= nptr
;
610 // FIXME: only correct for C locale
611 while (wxIsspace(*nptr
)) nptr
++;
612 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
613 if (((base
== 0) || (base
== 16)) &&
614 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
618 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
619 else if (base
== 0) base
= 10;
621 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
622 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
624 wxString
data(nptr
, nptr
-start
);
625 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
626 char *rdat
= wxMBSTRINGCAST dat
;
627 long int ret
= strtol(dat
, &rdat
, base
);
629 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
635 #ifdef wxNEED_WX_STDIO_H
636 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
638 char mode_buffer
[10];
639 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
640 mode_buffer
[i
] = (char) mode
[i
];
642 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
645 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
647 char mode_buffer
[10];
648 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
649 mode_buffer
[i
] = (char) mode
[i
];
651 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
654 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
656 return remove( wxConvFile
.cWX2MB(path
) );
659 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
661 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
666 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
668 return atof(wxConvLocal
.cWX2MB(psz
));
672 #ifdef wxNEED_WX_STDLIB_H
673 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
675 return atoi(wxConvLocal
.cWX2MB(psz
));
678 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
680 return atol(wxConvLocal
.cWX2MB(psz
));
683 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
685 static wxHashTable env
;
687 // check if we already have stored the converted env var
688 wxObject
*data
= env
.Get(name
);
691 // nope, retrieve it,
693 wxCharBuffer buffer
= wxConvLocal
.cWX2MB(name
);
694 // printf( "buffer %s\n", (const char*) buffer );
695 const char *val
= getenv( (const char *)buffer
);
697 const char *val
= getenv( name
);
700 if (!val
) return (wxChar
*)NULL
;
701 // printf( "home %s\n", val );
705 data
= (wxObject
*)new wxString(val
, wxConvLocal
);
707 data
= (wxObject
*)new wxString(val
);
713 // return converted env var
714 return (wxChar
*)((wxString
*)data
)->c_str();
717 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
719 return system(wxConvLocal
.cWX2MB(psz
));
724 #ifdef wxNEED_WX_TIME_H
725 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
729 char *buf
= (char *)malloc(max
);
730 size_t ret
= strftime(buf
, max
, wxConvLocal
.cWX2MB(fmt
), tm
);
733 wxStrcpy(s
, wxConvLocal
.cMB2WX(buf
));