]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
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()
37 #include "wx/wxchar.h"
38 #include "wx/string.h"
42 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
50 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
54 if (n
) *buf
= _T('\0');
57 return mbstowcs(buf
, psz
, n
);
60 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
61 // honor the 3rd parameter, thus it will happily crash here).
63 // don't know if it's really needed (or if we can pass NULL), but better safe
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
);
83 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
84 // honor the 3rd parameter, thus it will happily crash here).
86 // don't know if it's really needed (or if we can pass NULL), but better safe
89 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
91 return wcstombs((char *) NULL
, pwz
, 0);
96 bool WXDLLEXPORT
wxOKlibc()
98 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__)
99 // GNU libc uses UTF-8 even when it shouldn't
101 if ((MB_CUR_MAX
== 2) &&
102 (wxMB2WC(&res
, "\xdd\xa5", 1)>0) &&
104 // this is UTF-8 allright, check whether that's what we want
105 char *cur_locale
= setlocale(LC_ALL
, NULL
);
106 if ((strlen(cur_locale
) < 4) ||
107 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8"))) {
108 // nope, don't use libc conversion
117 size_t WXDLLEXPORT
wcslen(const wchar_t *s
)
120 while (s
[len
]) len
++;
125 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
126 inline WORD
wxMSW_ctype(wxChar ch
)
129 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
133 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
134 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
135 WXDLLEXPORT
int wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
136 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
137 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
138 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
139 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
140 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
141 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
142 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
143 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
144 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
145 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
149 WXDLLEXPORT wxChar
* wxStrdup(const wxChar
*psz
)
151 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
152 wxChar
*ret
= (wxChar
*) malloc(size
);
153 memcpy(ret
, psz
, size
);
159 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
161 register wxChar c1
, c2
;
163 c1
= wxTolower(*psz1
++);
164 c2
= wxTolower(*psz2
++);
165 } while ( c1
&& (c1
== c2
) );
171 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
173 if (!psz
) psz
= *save_ptr
;
174 psz
+= wxStrspn(psz
, delim
);
176 *save_ptr
= (wxChar
*)NULL
;
177 return (wxChar
*)NULL
;
180 psz
= wxStrpbrk(psz
, delim
);
181 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
191 WXDLLEXPORT wxChar
* wxSetlocale(int category
, const wxChar
*locale
)
193 setlocale(category
, wxConvLibc
.cWX2MB(locale
));
195 return (wxChar
*)NULL
;
199 #ifdef wxNEED_WX_STRING_H
200 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
203 while (*dest
) dest
++;
204 while ((*dest
++ = *src
++));
208 WXDLLEXPORT wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
210 while (*s
&& *s
!= c
) s
++;
211 return (*s
) ? (wxChar
*)s
: (wxChar
*)NULL
;
214 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
216 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
217 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
218 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
222 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
225 while ((*dest
++ = *src
++));
229 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
232 while (*dest
) dest
++;
233 while (n
&& (*dest
++ = *src
++)) n
--;
237 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
239 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
241 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
242 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
247 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
250 while (n
&& (*dest
++ = *src
++)) n
--;
251 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
255 WXDLLEXPORT wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
257 while (*s
&& !wxStrchr(accept
, *s
)) s
++;
258 return (*s
) ? (wxChar
*)s
: (wxChar
*)NULL
;
261 WXDLLEXPORT wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
263 wxChar
*ret
= (wxChar
*)NULL
;
265 if (*s
== c
) ret
= (wxChar
*)s
;
271 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
274 while (wxStrchr(accept
, *s
++)) len
++;
278 WXDLLEXPORT wxChar
* wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
281 while ((fnd
= wxStrchr(haystack
, *needle
))) {
282 if (!wxStrcmp(fnd
, needle
)) return fnd
;
285 return (wxChar
*)NULL
;
288 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
290 const wxChar
*start
= nptr
;
292 // FIXME: only correct for C locale
293 while (wxIsspace(*nptr
)) nptr
++;
294 if (*nptr
== _T('+') || *nptr
== _T('-')) nptr
++;
295 while (wxIsdigit(*nptr
)) nptr
++;
296 if (*nptr
== _T('.')) {
298 while (wxIsdigit(*nptr
)) nptr
++;
300 if (*nptr
== _T('E') || *nptr
== _T('e')) {
302 if (*nptr
== _T('+') || *nptr
== _T('-')) nptr
++;
303 while (wxIsdigit(*nptr
)) nptr
++;
306 wxString
data(nptr
, nptr
-start
);
307 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
308 char *rdat
= MBSTRINGCAST dat
;
309 double ret
= strtod(dat
, &rdat
);
311 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
316 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
318 const wxChar
*start
= nptr
;
320 // FIXME: only correct for C locale
321 while (wxIsspace(*nptr
)) nptr
++;
322 if (*nptr
== _T('+') || *nptr
== _T('-')) nptr
++;
323 if (((base
== 0) || (base
== 16)) &&
324 (nptr
[0] == _T('0') && nptr
[1] == _T('x'))) {
328 else if ((base
== 0) && (nptr
[0] == _T('0'))) base
= 8;
329 else if (base
== 0) base
= 10;
331 while ((wxIsdigit(*nptr
) && (*nptr
- _T('0') < base
)) ||
332 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - _T('A') + 10 < base
))) nptr
++;
334 wxString
data(nptr
, nptr
-start
);
335 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
336 char *rdat
= MBSTRINGCAST dat
;
337 long int ret
= strtol(dat
, &rdat
, base
);
339 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
345 #ifdef wxNEED_WX_STDIO_H
346 int WXDLLEXPORT
wxPrintf(const wxChar
*fmt
, ...)
351 va_start(argptr
, fmt
);
352 ret
= wxVprintf(fmt
, argptr
);
357 int WXDLLEXPORT
wxVprintf(const wxChar
*fmt
, va_list argptr
)
360 str
.PrintfV(fmt
,argptr
);
361 printf("%s", (const char*)str
.mb_str());
365 int WXDLLEXPORT
wxFprintf(FILE *stream
, const wxChar
*fmt
, ...)
370 va_start(argptr
, fmt
);
371 ret
= wxVfprintf(stream
, fmt
, argptr
);
376 int WXDLLEXPORT
wxVfprintf(FILE *stream
, const wxChar
*fmt
, va_list argptr
)
379 str
.PrintfV(fmt
,argptr
);
380 fprintf(stream
, "%s", (const char*)str
.mb_str());
384 int WXDLLEXPORT
wxSprintf(wxChar
*buf
, const wxChar
*fmt
, ...)
389 va_start(argptr
, fmt
);
390 ret
= wxVsprintf(buf
, fmt
, argptr
);
395 int WXDLLEXPORT
wxVsprintf(wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
397 // this might be sort of inefficient, but it doesn't matter since
398 // we'd prefer people to use wxString::Printf directly instead anyway
400 str
.PrintfV(fmt
,argptr
);
401 wxStrcpy(buf
,str
.c_str());
405 int WXDLLEXPORT
wxSscanf(const wxChar
*buf
, const wxChar
*fmt
, ...)
410 va_start(argptr
, fmt
);
411 ret
= wxVsscanf(buf
, fmt
, argptr
);
416 int WXDLLEXPORT
wxVsscanf(const wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
419 // this will work only for numeric conversion! Strings will not be converted correctly
420 // hopefully this is all we'll need
421 ret
= vsscanf(wxConvLibc
.cWX2MB(buf
), wxConvLibc
.cWX2MB(fmt
), argptr
);
427 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
429 return atof(wxConvLibc
.cWX2MB(psz
));
433 #ifdef wxNEED_WX_STDLIB_H
434 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
436 return atoi(wxConvLibc
.cWX2MB(psz
));
439 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
441 return atol(wxConvLibc
.cWX2MB(psz
));
444 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
446 static wxHashTable env
;
447 // check if we already have stored the converted env var
448 wxObject
*data
= env
.Get(name
);
450 // nope, retrieve it,
451 const char *val
= getenv(wxConvLibc
.cWX2MB(name
));
452 if (!val
) return (wxChar
*)NULL
;
454 data
= (wxObject
*)new wxString(val
);
458 // return converted env var
459 return (wxChar
*)((wxString
*)data
)->c_str();
462 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
464 return system(wxConvLibc
.cWX2MB(psz
));