]>
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()
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
= T('\0');
58 return mbstowcs(buf
, psz
, n
);
61 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
62 // honor the 3rd parameter, thus it will happily crash here).
64 // don't know if it's really needed (or if we can pass NULL), but better safe
67 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
69 return mbstowcs((wchar_t *) NULL
, psz
, 0);
73 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
77 // glibc2.1 chokes on null input
81 return wcstombs(buf
, pwz
, n
);
84 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
85 // honor the 3rd parameter, thus it will happily crash here).
87 // don't know if it's really needed (or if we can pass NULL), but better safe
90 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
92 return wcstombs((char *) NULL
, pwz
, 0);
97 bool WXDLLEXPORT
wxOKlibc()
99 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__)
100 // GNU libc uses UTF-8 even when it shouldn't
102 if ((MB_CUR_MAX
== 2) &&
103 (wxMB2WC(&res
, "\xdd\xa5", 1)>0) &&
105 // this is UTF-8 allright, check whether that's what we want
106 char *cur_locale
= setlocale(LC_ALL
, NULL
);
107 if ((strlen(cur_locale
) < 4) ||
108 (strcasecmp(cur_locale
+ strlen(cur_locale
) - 4, "utf8"))) {
109 // nope, don't use libc conversion
118 size_t WXDLLEXPORT
wcslen(const wchar_t *s
)
121 while (s
[len
]) len
++;
126 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
127 inline WORD
wxMSW_ctype(wxChar ch
)
130 GetStringTypeEx(LOCALE_USER_DEFAULT
, CT_CTYPE1
, &ch
, 1, &ret
);
134 WXDLLEXPORT
int wxIsalnum(wxChar ch
) { return IsCharAlphaNumeric(ch
); }
135 WXDLLEXPORT
int wxIsalpha(wxChar ch
) { return IsCharAlpha(ch
); }
136 WXDLLEXPORT
int wxIsctrl(wxChar ch
) { return wxMSW_ctype(ch
) & C1_CNTRL
; }
137 WXDLLEXPORT
int wxIsdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_DIGIT
; }
138 WXDLLEXPORT
int wxIsgraph(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_PUNCT
|C1_ALPHA
); }
139 WXDLLEXPORT
int wxIslower(wxChar ch
) { return IsCharLower(ch
); }
140 WXDLLEXPORT
int wxIsprint(wxChar ch
) { return wxMSW_ctype(ch
) & (C1_DIGIT
|C1_SPACE
|C1_PUNCT
|C1_ALPHA
); }
141 WXDLLEXPORT
int wxIspunct(wxChar ch
) { return wxMSW_ctype(ch
) & C1_PUNCT
; }
142 WXDLLEXPORT
int wxIsspace(wxChar ch
) { return wxMSW_ctype(ch
) & C1_SPACE
; }
143 WXDLLEXPORT
int wxIsupper(wxChar ch
) { return IsCharUpper(ch
); }
144 WXDLLEXPORT
int wxIsxdigit(wxChar ch
) { return wxMSW_ctype(ch
) & C1_XDIGIT
; }
145 WXDLLEXPORT
int wxTolower(wxChar ch
) { return (wxChar
)CharLower((LPTSTR
)(ch
)); }
146 WXDLLEXPORT
int wxToupper(wxChar ch
) { return (wxChar
)CharUpper((LPTSTR
)(ch
)); }
150 WXDLLEXPORT wxChar
* wxStrdup(const wxChar
*psz
)
152 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
153 wxChar
*ret
= (wxChar
*) malloc(size
);
154 memcpy(ret
, psz
, size
);
160 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
162 register wxChar c1
, c2
;
164 c1
= wxTolower(*psz1
++);
165 c2
= wxTolower(*psz2
++);
166 } while ( c1
&& (c1
== c2
) );
172 WXDLLEXPORT wxChar
* wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
174 if (!psz
) psz
= *save_ptr
;
175 psz
+= wxStrspn(psz
, delim
);
177 *save_ptr
= (wxChar
*)NULL
;
178 return (wxChar
*)NULL
;
181 psz
= wxStrpbrk(psz
, delim
);
182 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
192 WXDLLEXPORT wxChar
* wxSetlocale(int category
, const wxChar
*locale
)
194 setlocale(category
, wxConvLibc
.cWX2MB(locale
));
196 return (wxChar
*)NULL
;
200 #ifdef wxNEED_WX_STRING_H
201 WXDLLEXPORT wxChar
* wxStrcat(wxChar
*dest
, const wxChar
*src
)
204 while (*dest
) dest
++;
205 while ((*dest
++ = *src
++));
209 WXDLLEXPORT wxChar
* wxStrchr(const wxChar
*s
, wxChar c
)
211 while (*s
&& *s
!= c
) s
++;
212 return (*s
) ? (wxChar
*)s
: (wxChar
*)NULL
;
215 WXDLLEXPORT
int wxStrcmp(const wxChar
*s1
, const wxChar
*s2
)
217 while ((*s1
== *s2
) && *s1
) s1
++, s2
++;
218 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
219 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
223 WXDLLEXPORT wxChar
* wxStrcpy(wxChar
*dest
, const wxChar
*src
)
226 while ((*dest
++ = *src
++));
230 WXDLLEXPORT wxChar
* wxStrncat(wxChar
*dest
, const wxChar
*src
, size_t n
)
233 while (*dest
) dest
++;
234 while (n
&& (*dest
++ = *src
++)) n
--;
238 WXDLLEXPORT
int wxStrncmp(const wxChar
*s1
, const wxChar
*s2
, size_t n
)
240 while (n
&& (*s1
== *s2
) && *s1
) n
--, s1
++, s2
++;
242 if ((wxUChar
)*s1
< (wxUChar
)*s2
) return -1;
243 if ((wxUChar
)*s1
> (wxUChar
)*s2
) return 1;
248 WXDLLEXPORT wxChar
* wxStrncpy(wxChar
*dest
, const wxChar
*src
, size_t n
)
251 while (n
&& (*dest
++ = *src
++)) n
--;
252 while (n
) *dest
++=0, n
--; // the docs specify padding with zeroes
256 WXDLLEXPORT wxChar
* wxStrpbrk(const wxChar
*s
, const wxChar
*accept
)
258 while (*s
&& !wxStrchr(accept
, *s
)) s
++;
259 return (*s
) ? (wxChar
*)s
: (wxChar
*)NULL
;
262 WXDLLEXPORT wxChar
* wxStrrchr(const wxChar
*s
, wxChar c
)
264 wxChar
*ret
= (wxChar
*)NULL
;
266 if (*s
== c
) ret
= (wxChar
*)s
;
272 WXDLLEXPORT
size_t wxStrspn(const wxChar
*s
, const wxChar
*accept
)
275 while (wxStrchr(accept
, *s
++)) len
++;
279 WXDLLEXPORT wxChar
* wxStrstr(const wxChar
*haystack
, const wxChar
*needle
)
282 while ((fnd
= wxStrchr(haystack
, *needle
))) {
283 if (!wxStrcmp(fnd
, needle
)) return fnd
;
286 return (wxChar
*)NULL
;
289 WXDLLEXPORT
double wxStrtod(const wxChar
*nptr
, wxChar
**endptr
)
291 const wxChar
*start
= nptr
;
293 // FIXME: only correct for C locale
294 while (wxIsspace(*nptr
)) nptr
++;
295 if (*nptr
== T('+') || *nptr
== T('-')) nptr
++;
296 while (wxIsdigit(*nptr
)) nptr
++;
297 if (*nptr
== T('.')) {
299 while (wxIsdigit(*nptr
)) nptr
++;
301 if (*nptr
== T('E') || *nptr
== T('e')) {
303 if (*nptr
== T('+') || *nptr
== T('-')) nptr
++;
304 while (wxIsdigit(*nptr
)) nptr
++;
307 wxString
data(nptr
, nptr
-start
);
308 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
309 char *rdat
= wxMBSTRINGCAST dat
;
310 double ret
= strtod(dat
, &rdat
);
312 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
317 WXDLLEXPORT
long int wxStrtol(const wxChar
*nptr
, wxChar
**endptr
, int base
)
319 const wxChar
*start
= nptr
;
321 // FIXME: only correct for C locale
322 while (wxIsspace(*nptr
)) nptr
++;
323 if (*nptr
== T('+') || *nptr
== T('-')) nptr
++;
324 if (((base
== 0) || (base
== 16)) &&
325 (nptr
[0] == T('0') && nptr
[1] == T('x'))) {
329 else if ((base
== 0) && (nptr
[0] == T('0'))) base
= 8;
330 else if (base
== 0) base
= 10;
332 while ((wxIsdigit(*nptr
) && (*nptr
- T('0') < base
)) ||
333 (wxIsalpha(*nptr
) && (wxToupper(*nptr
) - T('A') + 10 < base
))) nptr
++;
335 wxString
data(nptr
, nptr
-start
);
336 wxWX2MBbuf dat
= data
.mb_str(wxConvLibc
);
337 char *rdat
= wxMBSTRINGCAST dat
;
338 long int ret
= strtol(dat
, &rdat
, base
);
340 if (endptr
) *endptr
= (wxChar
*)(start
+ (rdat
- (const char *)dat
));
346 #ifdef wxNEED_WX_STDIO_H
347 WXDLLEXPORT
FILE * wxFopen(const wxChar
*path
, const wxChar
*mode
)
349 return fopen(wxConvFile
.cWX2MB(path
), wxConvLibc
.cWX2MB(mode
));
352 WXDLLEXPORT
FILE * wxFreopen(const wxChar
*path
, const wxChar
*mode
, FILE *stream
)
354 return freopen(wxConvFile
.cWX2MB(path
), wxConvLibc
.cWX2MB(mode
), stream
);
357 int WXDLLEXPORT
wxPrintf(const wxChar
*fmt
, ...)
362 va_start(argptr
, fmt
);
363 ret
= wxVprintf(fmt
, argptr
);
368 int WXDLLEXPORT
wxVprintf(const wxChar
*fmt
, va_list argptr
)
371 str
.PrintfV(fmt
,argptr
);
372 printf("%s", (const char*)str
.mb_str());
376 int WXDLLEXPORT
wxFprintf(FILE *stream
, const wxChar
*fmt
, ...)
381 va_start(argptr
, fmt
);
382 ret
= wxVfprintf(stream
, fmt
, argptr
);
387 int WXDLLEXPORT
wxVfprintf(FILE *stream
, const wxChar
*fmt
, va_list argptr
)
390 str
.PrintfV(fmt
,argptr
);
391 fprintf(stream
, "%s", (const char*)str
.mb_str());
395 int WXDLLEXPORT
wxSprintf(wxChar
*buf
, const wxChar
*fmt
, ...)
400 va_start(argptr
, fmt
);
401 ret
= wxVsprintf(buf
, fmt
, argptr
);
406 int WXDLLEXPORT
wxVsprintf(wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
408 // this might be sort of inefficient, but it doesn't matter since
409 // we'd prefer people to use wxString::Printf directly instead anyway
411 str
.PrintfV(fmt
,argptr
);
412 wxStrcpy(buf
,str
.c_str());
416 int WXDLLEXPORT
wxSscanf(const wxChar
*buf
, const wxChar
*fmt
, ...)
421 va_start(argptr
, fmt
);
422 ret
= wxVsscanf(buf
, fmt
, argptr
);
427 int WXDLLEXPORT
wxVsscanf(const wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
430 // this will work only for numeric conversion! Strings will not be converted correctly
431 // hopefully this is all we'll need
432 ret
= vsscanf(wxConvLibc
.cWX2MB(buf
), wxConvLibc
.cWX2MB(fmt
), argptr
);
438 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
440 return atof(wxConvLibc
.cWX2MB(psz
));
444 #ifdef wxNEED_WX_STDLIB_H
445 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
447 return atoi(wxConvLibc
.cWX2MB(psz
));
450 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
452 return atol(wxConvLibc
.cWX2MB(psz
));
455 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
457 static wxHashTable env
;
458 // check if we already have stored the converted env var
459 wxObject
*data
= env
.Get(name
);
461 // nope, retrieve it,
462 const char *val
= getenv(wxConvLibc
.cWX2MB(name
));
463 if (!val
) return (wxChar
*)NULL
;
465 data
= (wxObject
*)new wxString(val
);
469 // return converted env var
470 return (wxChar
*)((wxString
*)data
)->c_str();
473 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
475 return system(wxConvLibc
.cWX2MB(psz
));
480 #ifdef wxNEED_WX_TIME_H
481 WXDLLEXPORT
size_t wxStrftime(wxChar
*s
, size_t max
, const wxChar
*fmt
, const struct tm
*tm
)
484 char *buf
= (char *)malloc(max
);
485 size_t ret
= strftime(buf
, max
, wxConvLibc
.cWX2MB(fmt
), tm
);
487 wxStrcpy(s
, wxConvLibc
.cMB2WX(buf
));