]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
Committing in .
[wxWidgets.git] / src / common / wxchar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxchar.cpp
3 // Purpose: wxChar implementation
4 // Author: Ove Kåven
5 // Modified by:
6 // Created: 09/04/99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows copyright
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "wxchar.h"
14 #endif
15
16 // ===========================================================================
17 // headers, declarations, constants
18 // ===========================================================================
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #define _ISOC9X_SOURCE 1 // to get vsscanf()
28 #define _BSD_SOURCE 1 // to still get strdup()
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <locale.h>
34 #include <time.h>
35
36 #ifndef WX_PRECOMP
37 #include "wx/defs.h"
38 #include "wx/wxchar.h"
39 #include "wx/string.h"
40 #include "wx/hash.h"
41 #endif
42
43 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
44 #include <windef.h>
45 #include <winbase.h>
46 #include <winnls.h>
47 #include <winnt.h>
48 #endif
49
50 #if wxUSE_WCHAR_T
51 size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n)
52 {
53 if (buf) {
54 if (!n || !*psz) {
55 if (n) *buf = wxT('\0');
56 return 0;
57 }
58 return mbstowcs(buf, psz, n);
59 }
60
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).
63 #if wxUSE_WCSRTOMBS
64 // don't know if it's really needed (or if we can pass NULL), but better safe
65 // than quick
66 mbstate_t mbstate;
67 return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
68 #else // !GNU libc
69 return mbstowcs((wchar_t *) NULL, psz, 0);
70 #endif // GNU
71 }
72
73 size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
74 {
75 if (buf) {
76 if (!n || !*pwz) {
77 // glibc2.1 chokes on null input
78 if (n) *buf = '\0';
79 return 0;
80 }
81 return wcstombs(buf, pwz, n);
82 }
83
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).
86 #if wxUSE_WCSRTOMBS
87 // don't know if it's really needed (or if we can pass NULL), but better safe
88 // than quick
89 mbstate_t mbstate;
90 return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
91 #else // !GNU libc
92 return wcstombs((char *) NULL, pwz, 0);
93 #endif // GNU
94 }
95 #endif
96
97 bool WXDLLEXPORT wxOKlibc()
98 {
99 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__)
100 // glibc 2.0 uses UTF-8 even when it shouldn't
101 wchar_t res = 0;
102 if ((MB_CUR_MAX == 2) &&
103 (wxMB2WC(&res, "\xdd\xa5", 1) == 1) &&
104 (res==0x765)) {
105 // this is UTF-8 allright, check whether that's what we want
106 char *cur_locale = setlocale(LC_CTYPE, NULL);
107 if ((strlen(cur_locale) < 4) ||
108 (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8")) ||
109 (strcasecmp(cur_locale + strlen(cur_locale) - 5, "utf-8"))) {
110 // nope, don't use libc conversion
111 return FALSE;
112 }
113 }
114 #endif
115 return TRUE;
116 }
117
118 #ifndef HAVE_WCSLEN
119 size_t WXDLLEXPORT wcslen(const wchar_t *s)
120 {
121 size_t len = 0;
122 while (s[len]) len++;
123 return len;
124 }
125 #endif
126
127 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
128 inline WORD wxMSW_ctype(wxChar ch)
129 {
130 WORD ret;
131 GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
132 return ret;
133 }
134
135 WXDLLEXPORT int wxIsalnum(wxChar ch) { return IsCharAlphaNumeric(ch); }
136 WXDLLEXPORT int wxIsalpha(wxChar ch) { return IsCharAlpha(ch); }
137 WXDLLEXPORT int wxIsctrl(wxChar ch) { return wxMSW_ctype(ch) & C1_CNTRL; }
138 WXDLLEXPORT int wxIsdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_DIGIT; }
139 WXDLLEXPORT int wxIsgraph(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_PUNCT|C1_ALPHA); }
140 WXDLLEXPORT int wxIslower(wxChar ch) { return IsCharLower(ch); }
141 WXDLLEXPORT int wxIsprint(wxChar ch) { return wxMSW_ctype(ch) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA); }
142 WXDLLEXPORT int wxIspunct(wxChar ch) { return wxMSW_ctype(ch) & C1_PUNCT; }
143 WXDLLEXPORT int wxIsspace(wxChar ch) { return wxMSW_ctype(ch) & C1_SPACE; }
144 WXDLLEXPORT int wxIsupper(wxChar ch) { return IsCharUpper(ch); }
145 WXDLLEXPORT int wxIsxdigit(wxChar ch) { return wxMSW_ctype(ch) & C1_XDIGIT; }
146 WXDLLEXPORT int wxTolower(wxChar ch) { return (wxChar)CharLower((LPTSTR)(ch)); }
147 WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
148 #endif
149
150 #ifndef wxStrdup
151 WXDLLEXPORT wxChar * wxStrdup(const wxChar *psz)
152 {
153 size_t size = (wxStrlen(psz) + 1) * sizeof(wxChar);
154 wxChar *ret = (wxChar *) malloc(size);
155 memcpy(ret, psz, size);
156 return ret;
157 }
158 #endif
159
160 #ifndef wxStricmp
161 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
162 {
163 register wxChar c1, c2;
164 do {
165 c1 = wxTolower(*psz1++);
166 c2 = wxTolower(*psz2++);
167 } while ( c1 && (c1 == c2) );
168 return c1 - c2;
169 }
170 #endif
171
172 #ifndef wxStricmp
173 int WXDLLEXPORT wxStrnicmp(const wxChar *s1, const wxChar *s2, size_t n)
174 {
175 register wxChar c1, c2;
176 while (n && ((c1 = wxTolower(*s1)) == (c2 = wxTolower(*s2)) ) && c1) n--, s1++, s2++;
177 if (n) {
178 if (c1 < c2) return -1;
179 if (c1 > c2) return 1;
180 }
181 return 0;
182 }
183 #endif
184
185 #ifndef wxStrtok
186 WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
187 {
188 if (!psz) psz = *save_ptr;
189 psz += wxStrspn(psz, delim);
190 if (!*psz) {
191 *save_ptr = (wxChar *)NULL;
192 return (wxChar *)NULL;
193 }
194 wxChar *ret = psz;
195 psz = wxStrpbrk(psz, delim);
196 if (!psz) *save_ptr = (wxChar*)NULL;
197 else {
198 *psz = wxT('\0');
199 *save_ptr = psz + 1;
200 }
201 return ret;
202 }
203 #endif
204
205 #ifndef wxSetlocale
206 WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale)
207 {
208 char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale));
209
210 return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld));
211 }
212 #endif
213
214 #ifdef wxNEED_WX_STRING_H
215 WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
216 {
217 wxChar *ret = dest;
218 while (*dest) dest++;
219 while ((*dest++ = *src++));
220 return ret;
221 }
222
223 WXDLLEXPORT wxChar * wxStrchr(const wxChar *s, wxChar c)
224 {
225 while (*s && *s != c) s++;
226 return (*s) ? (wxChar *)s : (wxChar *)NULL;
227 }
228
229 WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
230 {
231 while ((*s1 == *s2) && *s1) s1++, s2++;
232 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
233 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
234 return 0;
235 }
236
237 WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
238 {
239 wxChar *ret = dest;
240 while ((*dest++ = *src++));
241 return ret;
242 }
243
244 WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
245 {
246 wxChar *ret = dest;
247 while (*dest) dest++;
248 while (n && (*dest++ = *src++)) n--;
249 return ret;
250 }
251
252 WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
253 {
254 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
255 if (n) {
256 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
257 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
258 }
259 return 0;
260 }
261
262 WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
263 {
264 wxChar *ret = dest;
265 while (n && (*dest++ = *src++)) n--;
266 while (n) *dest++=0, n--; // the docs specify padding with zeroes
267 return ret;
268 }
269
270 WXDLLEXPORT wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
271 {
272 while (*s && !wxStrchr(accept, *s)) s++;
273 return (*s) ? (wxChar *)s : (wxChar *)NULL;
274 }
275
276 WXDLLEXPORT wxChar * wxStrrchr(const wxChar *s, wxChar c)
277 {
278 wxChar *ret = (wxChar *)NULL;
279 while (*s) {
280 if (*s == c) ret = (wxChar *)s;
281 s++;
282 }
283 return ret;
284 }
285
286 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
287 {
288 size_t len = 0;
289 while (wxStrchr(accept, *s++)) len++;
290 return len;
291 }
292
293 WXDLLEXPORT wxChar * wxStrstr(const wxChar *haystack, const wxChar *needle)
294 {
295 wxChar *fnd;
296 while ((fnd = wxStrchr(haystack, *needle))) {
297 if (!wxStrcmp(fnd, needle)) return fnd;
298 haystack = fnd + 1;
299 }
300 return (wxChar *)NULL;
301 }
302
303 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
304 {
305 const wxChar *start = nptr;
306
307 // FIXME: only correct for C locale
308 while (wxIsspace(*nptr)) nptr++;
309 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
310 while (wxIsdigit(*nptr)) nptr++;
311 if (*nptr == wxT('.')) {
312 nptr++;
313 while (wxIsdigit(*nptr)) nptr++;
314 }
315 if (*nptr == wxT('E') || *nptr == wxT('e')) {
316 nptr++;
317 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
318 while (wxIsdigit(*nptr)) nptr++;
319 }
320
321 wxString data(nptr, nptr-start);
322 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
323 char *rdat = wxMBSTRINGCAST dat;
324 double ret = strtod(dat, &rdat);
325
326 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
327
328 return ret;
329 }
330
331 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
332 {
333 const wxChar *start = nptr;
334
335 // FIXME: only correct for C locale
336 while (wxIsspace(*nptr)) nptr++;
337 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
338 if (((base == 0) || (base == 16)) &&
339 (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
340 nptr += 2;
341 base = 16;
342 }
343 else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
344 else if (base == 0) base = 10;
345
346 while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
347 (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
348
349 wxString data(nptr, nptr-start);
350 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
351 char *rdat = wxMBSTRINGCAST dat;
352 long int ret = strtol(dat, &rdat, base);
353
354 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
355
356 return ret;
357 }
358 #endif
359
360 #ifdef wxNEED_WX_STDIO_H
361 WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
362 {
363 return fopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode));
364 }
365
366 WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
367 {
368 return freopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode), stream);
369 }
370
371 WXDLLEXPORT int wxRemove(const wxChar *path)
372 {
373 return remove(wxConvFile.cWX2MB(path));
374 }
375
376 WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
377 {
378 return rename(wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath));
379 }
380
381 int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...)
382 {
383 va_list argptr;
384 int ret;
385
386 va_start(argptr, fmt);
387 ret = wxVprintf(fmt, argptr);
388 va_end(argptr);
389 return ret;
390 }
391
392 int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr)
393 {
394 wxString str;
395 str.PrintfV(fmt,argptr);
396 printf("%s", (const char*)str.mb_str());
397 return str.Len();
398 }
399
400 int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...)
401 {
402 va_list argptr;
403 int ret;
404
405 va_start(argptr, fmt);
406 ret = wxVfprintf(stream, fmt, argptr);
407 va_end(argptr);
408 return ret;
409 }
410
411 int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr)
412 {
413 wxString str;
414 str.PrintfV(fmt,argptr);
415 fprintf(stream, "%s", (const char*)str.mb_str());
416 return str.Len();
417 }
418
419 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...)
420 {
421 va_list argptr;
422 int ret;
423
424 va_start(argptr, fmt);
425 ret = wxVsprintf(buf, fmt, argptr);
426 va_end(argptr);
427 return ret;
428 }
429
430 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr)
431 {
432 // this might be sort of inefficient, but it doesn't matter since
433 // we'd prefer people to use wxString::Printf directly instead anyway
434 wxString str;
435 str.PrintfV(fmt,argptr);
436 wxStrcpy(buf,str.c_str());
437 return str.Len();
438 }
439
440 int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...)
441 {
442 va_list argptr;
443 int ret;
444
445 va_start(argptr, fmt);
446 ret = wxVsscanf(buf, fmt, argptr);
447 va_end(argptr);
448 return ret;
449 }
450
451 int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr)
452 {
453 int ret;
454 // this will work only for numeric conversion! Strings will not be converted correctly
455 // hopefully this is all we'll need
456 ret = vsscanf(wxConvLibc.cWX2MB(buf), wxConvLibc.cWX2MB(fmt), argptr);
457 return ret;
458 }
459 #endif
460
461 #ifndef wxAtof
462 double WXDLLEXPORT wxAtof(const wxChar *psz)
463 {
464 return atof(wxConvLibc.cWX2MB(psz));
465 }
466 #endif
467
468 #ifdef wxNEED_WX_STDLIB_H
469 int WXDLLEXPORT wxAtoi(const wxChar *psz)
470 {
471 return atoi(wxConvLibc.cWX2MB(psz));
472 }
473
474 long WXDLLEXPORT wxAtol(const wxChar *psz)
475 {
476 return atol(wxConvLibc.cWX2MB(psz));
477 }
478
479 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
480 {
481 static wxHashTable env;
482 // check if we already have stored the converted env var
483 wxObject *data = env.Get(name);
484 if (!data) {
485 // nope, retrieve it,
486 const char *val = getenv(wxConvLibc.cWX2MB(name));
487 if (!val) return (wxChar *)NULL;
488 // convert it,
489 data = (wxObject *)new wxString(val);
490 // and store it
491 env.Put(name, data);
492 }
493 // return converted env var
494 return (wxChar *)((wxString *)data)->c_str();
495 }
496
497 int WXDLLEXPORT wxSystem(const wxChar *psz)
498 {
499 return system(wxConvLibc.cWX2MB(psz));
500 }
501
502 #endif
503
504 #ifdef wxNEED_WX_TIME_H
505 WXDLLEXPORT size_t wxStrftime(wxChar *s, size_t max, const wxChar *fmt, const struct tm *tm)
506 {
507 if (!max) return 0;
508 char *buf = (char *)malloc(max);
509 size_t ret = strftime(buf, max, wxConvLibc.cWX2MB(fmt), tm);
510 if (ret) {
511 wxStrcpy(s, wxConvLibc.cMB2WX(buf));
512 free(buf);
513 return wxStrlen(s);
514 } else {
515 free(buf);
516 *s = 0;
517 return 0;
518 }
519 }
520 #endif