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 #ifdef wxNEED_PRINTF_CONVERSION
122 #define CONVERT_FORMAT_1 \
123 wxChar *new_format = (wxChar*) format; \
124 size_t old_len = wxStrlen( format ); \
127 for (i = 0; i < old_len; i++) \
129 if ( (format[i] == L'%') && \
130 ((i < old_len) && ((format[i+1] == L's') || (format[i+1] == L'c'))) && \
131 ((i == 0) || (format[i-1] != L'%')) ) \
139 new_format = new wxChar[old_len+n+1]; \
140 wxChar *s = new_format; \
142 for (i = 0; i < old_len+1; i++) \
144 if ( (format[i] == L'%') && \
145 ((i < old_len) && ((format[i+1] == L's') || (format[i+1] == L'c'))) && \
146 ((i == 0) || (format[i-1] != L'%')) ) \
161 #define CONVERT_FORMAT_2 \
163 delete [] new_format;
166 int wxScanf( const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_2
171 va_start(argptr
, format
);
173 int ret
= vwscanf( new_format
, argptr
);
182 int wxSscanf( const wxChar
*str
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
187 va_start(argptr
, format
);
189 int ret
= vswscanf( str
, new_format
, argptr
);
198 int wxFscanf( FILE *stream
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
203 va_start(argptr
, format
);
205 int ret
= vfwscanf(stream
, new_format
, argptr
);
214 int wxVsscanf( const wxChar
*str
, const wxChar
*format
, va_list ap
)
218 int ret
= vswscanf( str
, new_format
, ap
);
225 int wxPrintf( const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_2
230 va_start(argptr
, format
);
232 int ret
= vwprintf( new_format
, argptr
);
241 int wxSnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_4
246 va_start(argptr
, format
);
248 int ret
= vswprintf( str
, size
, new_format
, argptr
);
257 int wxSprintf( wxChar
*str
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
262 va_start(argptr
, format
);
265 int ret
= vswprintf( str
, 10000, new_format
, argptr
);
274 int wxFprintf( FILE *stream
, const wxChar
*format
, ... ) ATTRIBUTE_PRINTF_3
279 va_start( argptr
, format
);
281 int ret
= vfwprintf( stream
, new_format
, argptr
);
290 int wxVfprint( FILE *stream
, const wxChar
*format
, va_list ap
)
294 int ret
= vfwprintf( stream
, new_format
, ap
);
301 int wxVprintf( const wxChar
*format
, va_list ap
)
305 int ret
= vwprintf( new_format
, ap
);
312 int wxVsnprintf( wxChar
*str
, size_t size
, const wxChar
*format
, va_list ap
)
316 int ret
= vswprintf( str
, size
, new_format
, ap
);
323 int wxVsprintf( wxChar
*str
, const wxChar
*format
, va_list ap
)
328 int ret
= vswprintf(str
, 10000, new_format
, ap
);
336 // we want to find out if the current platform supports vsnprintf()-like
337 // function: for Unix this is done with configure, for Windows we test the
338 // compiler explicitly.
340 // FIXME currently, this is only for ANSI (!Unicode) strings, so we call this
341 // function wxVsnprintfA (A for ANSI), should also find one for Unicode
342 // strings in Unicode build
344 #if defined(__VISUALC__) || (defined(__MINGW32__) && wxUSE_NORLANDER_HEADERS)
345 #define wxVsnprintfA _vsnprintf
347 #elif defined(__WXMAC__)
348 #define wxVsnprintfA vsnprintf
350 #ifdef HAVE_VSNPRINTF
351 #define wxVsnprintfA vsnprintf
353 #endif // Windows/!Windows
356 // in this case we'll use vsprintf() (which is ANSI and thus should be
357 // always available), but it's unsafe because it doesn't check for buffer
358 // size - so give a warning
359 #define wxVsnprintfA(buf, len, format, arg) vsprintf(buf, format, arg)
361 #if defined(__VISUALC__)
362 #pragma message("Using sprintf() because no snprintf()-like function defined")
363 #elif defined(__GNUG__)
364 #warning "Using sprintf() because no snprintf()-like function defined"
366 #endif // no vsnprintf
369 // AIX has vsnprintf, but there's no prototype in the system headers.
370 extern "C" int vsnprintf(char* str
, size_t n
, const char* format
, va_list ap
);
373 #if !defined(wxVsnprintf) && !defined(wxHAS_VSNPRINTF)
374 int WXDLLEXPORT
wxVsnprintf(wxChar
*buf
, size_t len
,
375 const wxChar
*format
, va_list argptr
)
379 int iLen
= s
.PrintfV(format
, argptr
);
382 wxStrncpy(buf
, s
.c_str(), len
);
383 buf
[len
-1] = wxT('\0');
388 // vsnprintf() will not terminate the string with '\0' if there is not
389 // enough place, but we want the string to always be NUL terminated
390 int rc
= wxVsnprintfA(buf
, len
- 1, format
, argptr
);
397 #endif // Unicode/ANSI
401 #if !defined(wxSnprintf) && !defined(wxHAS_SNPRINTF)
402 int WXDLLEXPORT
wxSnprintf(wxChar
*buf
, size_t len
,
403 const wxChar
*format
, ...)
406 va_start(argptr
, format
);
408 int iLen
= wxVsnprintf(buf
, len
, format
, argptr
);
416 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
417 inline WORD
wxMSW_ctype(wxChar ch
)
420 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
424 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
425 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
426 WXDLLEXPORT
int wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
427 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
428 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
429 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
430 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
431 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
432 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
433 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
434 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
435 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
436 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
440 WXDLLEXPORT wxChar
* wxStrdup(const wxChar
*psz
)
442 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
443 wxChar
*ret
= (wxChar
*) malloc(size
);
444 memcpy(ret
, psz
, size
);
450 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
452 register wxChar c1
, c2
;
454 c1
= wxTolower(*psz1
++);
455 c2
= wxTolower(*psz2
++);
456 } while ( c1
&& (c1
== c2
) );
462 int WXDLLEXPORT
wxStrnicmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
464 register wxChar c1
, c2
;
465 while (n
&& ((c1
= wxTolower(*s1
)) == (c2
= wxTolower(*s2
)) ) && c1
) n
--, s1
++, s2
++;
467 if (c1
< c2
) return -1;
468 if (c1
> c2
) return 1;
475 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
477 if (!psz
) psz
= *save_ptr
;
478 psz
+= wxStrspn(psz
, delim
);
480 *save_ptr
= (wxChar
*)NULL
;
481 return (wxChar
*)NULL
;
484 psz
= wxStrpbrk(psz
, delim
);
485 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
495 WXDLLEXPORT wxWCharBuffer
wxSetlocale(int category
, const wxChar
*locale
)
497 char *localeOld
= setlocale(category
, wxConvLocal
.cWX2MB(locale
));
499 return wxWCharBuffer(wxConvLocal
.cMB2WC(localeOld
));
503 #ifdef wxNEED_WX_STRING_H
504 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
507 while (*dest
) dest
++;
508 while ((*dest
++ = *src
++));
512 WXDLLEXPORT
const wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
514 // be careful here as the terminating NUL makes part of the string
524 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
526 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
527 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
528 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
532 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
535 while ((*dest
++ = *src
++));
539 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
542 while (*dest
) dest
++;
543 while (n
&& (*dest
++ = *src
++)) n
--;
547 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
549 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
551 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
552 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
557 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
560 while (n
&& (*dest
++ = *src
++)) n
--;
561 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
565 WXDLLEXPORT
const wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
567 while (*s
&& !wxStrchr(accept
, *s
))
570 return *s
? s
: NULL
;
573 WXDLLEXPORT
const wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
575 const wxChar
*ret
= NULL
;
587 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
590 while (wxStrchr(accept
, *s
++)) len
++;
594 WXDLLEXPORT
const wxChar
*wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
596 wxCHECK_RET( needle
, NULL
, _T("NULL argument in wxStrstr") );
598 // VZ: this is not exactly the most efficient string search algorithm...
600 const size_t len
= wxStrlen(needle
);
602 while ( const wxChar
*fnd
= wxStrchr(haystack
, *needle
) )
604 if ( !wxStrncmp(fnd
, needle
, len
) )
613 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
615 const wxChar
*start
= nptr
;
617 // FIXME: only correct for C locale
618 while (wxIsspace(*nptr
)) nptr
++;
619 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
620 while (wxIsdigit(*nptr
)) nptr
++;
621 if (*nptr
== wxT('.')) {
623 while (wxIsdigit(*nptr
)) nptr
++;
625 if (*nptr
== wxT('E') || *nptr
== wxT('e')) {
627 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
628 while (wxIsdigit(*nptr
)) nptr
++;
631 wxString
data(nptr
, nptr
-start
);
632 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
633 char *rdat
= wxMBSTRINGCAST dat
;
634 double ret
= strtod(dat
, &rdat
);
636 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
641 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
643 const wxChar
*start
= nptr
;
645 // FIXME: only correct for C locale
646 while (wxIsspace(*nptr
)) nptr
++;
647 if (*nptr
== wxT('+') || *nptr
== wxT('-')) nptr
++;
648 if (((base
== 0) || (base
== 16)) &&
649 (nptr
[0] == wxT('0') && nptr
[1] == wxT('x'))) {
653 else if ((base
== 0) && (nptr
[0] == wxT('0'))) base
= 8;
654 else if (base
== 0) base
= 10;
656 while ((wxIsdigit(*nptr
) && (*nptr
- wxT('0') < base
)) ||
657 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - wxT('A') + 10 < base
))) nptr
++;
659 wxString
data(nptr
, nptr
-start
);
660 wxWX2MBbuf dat
= data
.mb_str(wxConvLocal
);
661 char *rdat
= wxMBSTRINGCAST dat
;
662 long int ret
= strtol(dat
, &rdat
, base
);
664 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
670 #ifdef wxNEED_WX_STDIO_H
671 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
673 char mode_buffer
[10];
674 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
675 mode_buffer
[i
] = (char) mode
[i
];
677 return fopen( wxConvFile
.cWX2MB(path
), mode_buffer
);
680 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
682 char mode_buffer
[10];
683 for (size_t i
= 0; i
< wxStrlen(mode
)+1; i
++)
684 mode_buffer
[i
] = (char) mode
[i
];
686 return freopen( wxConvFile
.cWX2MB(path
), mode_buffer
, stream
);
689 WXDLLEXPORT
int wxRemove(const wxChar
*path
)
691 return remove( wxConvFile
.cWX2MB(path
) );
694 WXDLLEXPORT
int wxRename(const wxChar
*oldpath
, const wxChar
*newpath
)
696 return rename( wxConvFile
.cWX2MB(oldpath
), wxConvFile
.cWX2MB(newpath
) );
701 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
703 return atof(wxConvLocal
.cWX2MB(psz
));
707 #ifdef wxNEED_WX_STDLIB_H
708 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
710 return atoi(wxConvLocal
.cWX2MB(psz
));
713 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
715 return atol(wxConvLocal
.cWX2MB(psz
));
718 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
720 static wxHashTable env
;
722 // check if we already have stored the converted env var
723 wxObject
*data
= env
.Get(name
);
726 // nope, retrieve it,
728 wxCharBuffer buffer
= wxConvLocal
.cWX2MB(name
);
729 // printf( "buffer %s\n", (const char*) buffer );
730 const char *val
= getenv( (const char *)buffer
);
732 const char *val
= getenv( name
);
735 if (!val
) return (wxChar
*)NULL
;
736 // printf( "home %s\n", val );
740 data
= (wxObject
*)new wxString(val
, wxConvLocal
);
742 data
= (wxObject
*)new wxString(val
);
748 // return converted env var
749 return (wxChar
*)((wxString
*)data
)->c_str();
752 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
754 return system(wxConvLocal
.cWX2MB(psz
));
759 #ifdef wxNEED_WX_TIME_H
760 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
764 char *buf
= (char *)malloc(max
);
765 size_t ret
= strftime(buf
, max
, wxConvLocal
.cWX2MB(fmt
), tm
);
768 wxStrcpy(s
, wxConvLocal
.cMB2WX(buf
));