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 // we want to find out if the current platform supports vsnprintf()-like
339 // function: for Unix this is done with configure, for Windows we test the
340 // compiler explicitly.
342 // FIXME currently, this is only for ANSI (!Unicode) strings, so we call this
343 // function wxVsnprintfA (A for ANSI), should also find one for Unicode
344 // strings in Unicode build
346 #if defined(__VISUALC__) || (defined(__MINGW32__) && wxUSE_NORLANDER_HEADERS)
347 #define wxVsnprintfA _vsnprintf
349 #elif defined(__WXMAC__)
350 #define wxVsnprintfA vsnprintf
352 #ifdef HAVE_VSNPRINTF
353 #define wxVsnprintfA vsnprintf
355 #endif // Windows/!Windows
358 // in this case we'll use vsprintf() (which is ANSI and thus should be
359 // always available), but it's unsafe because it doesn't check for buffer
360 // size - so give a warning
361 #define wxVsnprintfA(buf, len, format, arg) vsprintf(buf, format, arg)
363 #if defined(__VISUALC__)
364 #pragma message("Using sprintf() because no snprintf()-like function defined")
365 #elif defined(__GNUG__)
366 #warning "Using sprintf() because no snprintf()-like function defined"
368 #endif // no vsnprintf
371 // AIX has vsnprintf, but there's no prototype in the system headers.
372 extern "C" int vsnprintf(char* str
, size_t n
, const char* format
, va_list ap
);
375 #if !defined(wxVsnprintf) && !defined(wxHAS_VSNPRINTF)
376 int WXDLLEXPORT
wxVsnprintf(wxChar
*buf
, size_t len
,
377 const wxChar
*format
, va_list argptr
)
381 int iLen
= s
.PrintfV(format
, argptr
);
384 wxStrncpy(buf
, s
.c_str(), len
);
385 buf
[len
-1] = wxT('\0');
390 // vsnprintf() will not terminate the string with '\0' if there is not
391 // enough place, but we want the string to always be NUL terminated
392 int rc
= wxVsnprintfA(buf
, len
- 1, format
, argptr
);
399 #endif // Unicode/ANSI
403 #if !defined(wxSnprintf) && !defined(wxHAS_SNPRINTF)
404 int WXDLLEXPORT
wxSnprintf(wxChar
*buf
, size_t len
,
405 const wxChar
*format
, ...)
408 va_start(argptr
, format
);
410 int iLen
= wxVsnprintf(buf
, len
, format
, argptr
);
418 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
419 inline WORD
wxMSW_ctype(wxChar ch
)
422 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
426 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
427 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
428 WXDLLEXPORT
int wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
429 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
430 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
431 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
432 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
433 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
434 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
435 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
436 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
437 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
438 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
442 WXDLLEXPORT wxChar
* wxStrdup(const wxChar
*psz
)
444 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
445 wxChar
*ret
= (wxChar
*) malloc(size
);
446 memcpy(ret
, psz
, size
);
452 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
454 register wxChar c1
, c2
;
456 c1
= wxTolower(*psz1
++);
457 c2
= wxTolower(*psz2
++);
458 } while ( c1
&& (c1
== c2
) );
464 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
466 register wxChar c1
, c2
;
467 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
469 if (c1
< c2
) return -1;
470 if (c1
> c2
) return 1;
477 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
479 if (!psz
) psz
= *save_ptr
;
480 psz
+= wxStrspn(psz
, delim
);
482 *save_ptr
= (wxChar
*)NULL
;
483 return (wxChar
*)NULL
;
486 psz
= wxStrpbrk(psz
, delim
);
487 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
497 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
499 char *localeOld
= setlocale(category
, wxConvLocal
.cWX2MB(locale
));
501 return wxWCharBuffer(wxConvLocal
.cMB2WC(localeOld
));
505 #ifdef wxNEED_WX_STRING_H
506 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
509 while (*dest
) dest
++;
510 while ((*dest
++ = *src
++));
514 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
516 // be careful here as the terminating NUL makes part of the string
526 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
528 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
529 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
530 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
534 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
537 while ((*dest
++ = *src
++));
541 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
544 while (*dest
) dest
++;
545 while (n
&& (*dest
++ = *src
++)) n
--;
549 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
551 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
553 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
554 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
559 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
562 while (n
&& (*dest
++ = *src
++)) n
--;
563 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
567 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
569 while (*s
&& !wxStrchr(accept
, *s
))
572 return *s
? s
: NULL
;
575 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
577 const wxChar
*ret
= NULL
;
589 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
592 while (wxStrchr(accept
, *s
++)) len
++;
596 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
598 wxCHECK_RET( needle
, NULL
, _T("NULL argument in wxStrstr") );
600 // VZ: this is not exactly the most efficient string search algorithm...
602 const size_t len
= wxStrlen(needle
);
604 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
606 if ( !wxStrncmp(fnd
, needle
, len
) )
615 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
617 const wxChar
*start
= nptr
;
619 // FIXME: only correct for C locale
620 while (wxIsspace(*nptr
)) nptr
++;
621 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
622 while (wxIsdigit(*nptr
)) nptr
++;
623 if (*nptr
== wxT('.')) {
625 while (wxIsdigit(*nptr
)) nptr
++;
627 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
629 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
630 while (wxIsdigit(*nptr
)) nptr
++;
633 wxString
data(nptr
, nptr
-start
);
634 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
635 char *rdat
= wxMBSTRINGCAST dat
;
636 double ret
= strtod(dat
, &rdat
);
638 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
643 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
645 const wxChar
*start
= nptr
;
647 // FIXME: only correct for C locale
648 while (wxIsspace(*nptr
)) nptr
++;
649 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
650 if (((base
== 0) || (base
== 16)) &&
651 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
655 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
656 else if (base
== 0) base
= 10;
658 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
659 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
661 wxString
data(nptr
, nptr
-start
);
662 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
663 char *rdat
= wxMBSTRINGCAST dat
;
664 long int ret
= strtol(dat
, &rdat
, base
);
666 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
672 #ifdef wxNEED_WX_STDIO_H
673 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
675 char mode_buffer
[10];
676 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
677 mode_buffer
[i
] = (char) mode
[i
];
679 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
682 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
684 char mode_buffer
[10];
685 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
686 mode_buffer
[i
] = (char) mode
[i
];
688 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
691 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
693 return remove( wxConvFile
.cWX2MB(path
) );
696 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
698 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
703 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
705 return atof(wxConvLocal
.cWX2MB(psz
));
709 #ifdef wxNEED_WX_STDLIB_H
710 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
712 return atoi(wxConvLocal
.cWX2MB(psz
));
715 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
717 return atol(wxConvLocal
.cWX2MB(psz
));
720 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
722 static wxHashTable env
;
724 // check if we already have stored the converted env var
725 wxObject
*data
= env
.Get(name
);
728 // nope, retrieve it,
730 wxCharBuffer buffer
= wxConvLocal
.cWX2MB(name
);
731 // printf( "buffer %s\n", (const char*) buffer );
732 const char *val
= getenv( (const char *)buffer
);
734 const char *val
= getenv( name
);
737 if (!val
) return (wxChar
*)NULL
;
738 // printf( "home %s\n", val );
742 data
= (wxObject
*)new wxString(val
, wxConvLocal
);
744 data
= (wxObject
*)new wxString(val
);
750 // return converted env var
751 return (wxChar
*)((wxString
*)data
)->c_str();
754 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
756 return system(wxConvLocal
.cWX2MB(psz
));
761 #ifdef wxNEED_WX_TIME_H
762 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
766 char *buf
= (char *)malloc(max
);
767 size_t ret
= strftime(buf
, max
, wxConvLocal
.cWX2MB(fmt
), tm
);
770 wxStrcpy(s
, wxConvLocal
.cMB2WX(buf
));