]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
wxControl and wxDialog coded and supporting module def file.
[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 // nope, don't use libc conversion
110 return FALSE;
111 }
112 }
113 #endif
114 return TRUE;
115 }
116
117 #ifndef HAVE_WCSLEN
118 size_t WXDLLEXPORT wcslen(const wchar_t *s)
119 {
120 size_t len = 0;
121 while (s[len]) len++;
122 return len;
123 }
124 #endif
125
126 #if defined(__WIN32__) && defined(wxNEED_WX_CTYPE_H)
127 inline WORD wxMSW_ctype(wxChar ch)
128 {
129 WORD ret;
130 GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
131 return ret;
132 }
133
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)); }
147 #endif
148
149 #ifndef wxStrdup
150 WXDLLEXPORT wxChar * wxStrdup(const wxChar *psz)
151 {
152 size_t size = (wxStrlen(psz) + 1) * sizeof(wxChar);
153 wxChar *ret = (wxChar *) malloc(size);
154 memcpy(ret, psz, size);
155 return ret;
156 }
157 #endif
158
159 #ifndef wxStricmp
160 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
161 {
162 register wxChar c1, c2;
163 do {
164 c1 = wxTolower(*psz1++);
165 c2 = wxTolower(*psz2++);
166 } while ( c1 && (c1 == c2) );
167 return c1 - c2;
168 }
169 #endif
170
171 #ifndef wxStrtok
172 WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
173 {
174 if (!psz) psz = *save_ptr;
175 psz += wxStrspn(psz, delim);
176 if (!*psz) {
177 *save_ptr = (wxChar *)NULL;
178 return (wxChar *)NULL;
179 }
180 wxChar *ret = psz;
181 psz = wxStrpbrk(psz, delim);
182 if (!psz) *save_ptr = (wxChar*)NULL;
183 else {
184 *psz = wxT('\0');
185 *save_ptr = psz + 1;
186 }
187 return ret;
188 }
189 #endif
190
191 #ifndef wxSetlocale
192 WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale)
193 {
194 char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale));
195
196 return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld));
197 }
198 #endif
199
200 #ifdef wxNEED_WX_STRING_H
201 WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
202 {
203 wxChar *ret = dest;
204 while (*dest) dest++;
205 while ((*dest++ = *src++));
206 return ret;
207 }
208
209 WXDLLEXPORT wxChar * wxStrchr(const wxChar *s, wxChar c)
210 {
211 while (*s && *s != c) s++;
212 return (*s) ? (wxChar *)s : (wxChar *)NULL;
213 }
214
215 WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
216 {
217 while ((*s1 == *s2) && *s1) s1++, s2++;
218 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
219 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
220 return 0;
221 }
222
223 WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
224 {
225 wxChar *ret = dest;
226 while ((*dest++ = *src++));
227 return ret;
228 }
229
230 WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
231 {
232 wxChar *ret = dest;
233 while (*dest) dest++;
234 while (n && (*dest++ = *src++)) n--;
235 return ret;
236 }
237
238 WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
239 {
240 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
241 if (n) {
242 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
243 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
244 }
245 return 0;
246 }
247
248 WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
249 {
250 wxChar *ret = dest;
251 while (n && (*dest++ = *src++)) n--;
252 while (n) *dest++=0, n--; // the docs specify padding with zeroes
253 return ret;
254 }
255
256 WXDLLEXPORT wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
257 {
258 while (*s && !wxStrchr(accept, *s)) s++;
259 return (*s) ? (wxChar *)s : (wxChar *)NULL;
260 }
261
262 WXDLLEXPORT wxChar * wxStrrchr(const wxChar *s, wxChar c)
263 {
264 wxChar *ret = (wxChar *)NULL;
265 while (*s) {
266 if (*s == c) ret = (wxChar *)s;
267 s++;
268 }
269 return ret;
270 }
271
272 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
273 {
274 size_t len = 0;
275 while (wxStrchr(accept, *s++)) len++;
276 return len;
277 }
278
279 WXDLLEXPORT wxChar * wxStrstr(const wxChar *haystack, const wxChar *needle)
280 {
281 wxChar *fnd;
282 while ((fnd = wxStrchr(haystack, *needle))) {
283 if (!wxStrcmp(fnd, needle)) return fnd;
284 haystack = fnd + 1;
285 }
286 return (wxChar *)NULL;
287 }
288
289 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
290 {
291 const wxChar *start = nptr;
292
293 // FIXME: only correct for C locale
294 while (wxIsspace(*nptr)) nptr++;
295 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
296 while (wxIsdigit(*nptr)) nptr++;
297 if (*nptr == wxT('.')) {
298 nptr++;
299 while (wxIsdigit(*nptr)) nptr++;
300 }
301 if (*nptr == wxT('E') || *nptr == wxT('e')) {
302 nptr++;
303 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
304 while (wxIsdigit(*nptr)) nptr++;
305 }
306
307 wxString data(nptr, nptr-start);
308 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
309 char *rdat = wxMBSTRINGCAST dat;
310 double ret = strtod(dat, &rdat);
311
312 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
313
314 return ret;
315 }
316
317 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
318 {
319 const wxChar *start = nptr;
320
321 // FIXME: only correct for C locale
322 while (wxIsspace(*nptr)) nptr++;
323 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
324 if (((base == 0) || (base == 16)) &&
325 (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
326 nptr += 2;
327 base = 16;
328 }
329 else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
330 else if (base == 0) base = 10;
331
332 while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
333 (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
334
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);
339
340 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
341
342 return ret;
343 }
344 #endif
345
346 #ifdef wxNEED_WX_STDIO_H
347 WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
348 {
349 return fopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode));
350 }
351
352 WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
353 {
354 return freopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode), stream);
355 }
356
357 WXDLLEXPORT int wxRemove(const wxChar *path)
358 {
359 return remove(wxConvFile.cWX2MB(path));
360 }
361
362 WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
363 {
364 return rename(wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath));
365 }
366
367 int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...)
368 {
369 va_list argptr;
370 int ret;
371
372 va_start(argptr, fmt);
373 ret = wxVprintf(fmt, argptr);
374 va_end(argptr);
375 return ret;
376 }
377
378 int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr)
379 {
380 wxString str;
381 str.PrintfV(fmt,argptr);
382 printf("%s", (const char*)str.mb_str());
383 return str.Len();
384 }
385
386 int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...)
387 {
388 va_list argptr;
389 int ret;
390
391 va_start(argptr, fmt);
392 ret = wxVfprintf(stream, fmt, argptr);
393 va_end(argptr);
394 return ret;
395 }
396
397 int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr)
398 {
399 wxString str;
400 str.PrintfV(fmt,argptr);
401 fprintf(stream, "%s", (const char*)str.mb_str());
402 return str.Len();
403 }
404
405 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...)
406 {
407 va_list argptr;
408 int ret;
409
410 va_start(argptr, fmt);
411 ret = wxVsprintf(buf, fmt, argptr);
412 va_end(argptr);
413 return ret;
414 }
415
416 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr)
417 {
418 // this might be sort of inefficient, but it doesn't matter since
419 // we'd prefer people to use wxString::Printf directly instead anyway
420 wxString str;
421 str.PrintfV(fmt,argptr);
422 wxStrcpy(buf,str.c_str());
423 return str.Len();
424 }
425
426 int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...)
427 {
428 va_list argptr;
429 int ret;
430
431 va_start(argptr, fmt);
432 ret = wxVsscanf(buf, fmt, argptr);
433 va_end(argptr);
434 return ret;
435 }
436
437 int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr)
438 {
439 int ret;
440 // this will work only for numeric conversion! Strings will not be converted correctly
441 // hopefully this is all we'll need
442 ret = vsscanf(wxConvLibc.cWX2MB(buf), wxConvLibc.cWX2MB(fmt), argptr);
443 return ret;
444 }
445 #endif
446
447 #ifndef wxAtof
448 double WXDLLEXPORT wxAtof(const wxChar *psz)
449 {
450 return atof(wxConvLibc.cWX2MB(psz));
451 }
452 #endif
453
454 #ifdef wxNEED_WX_STDLIB_H
455 int WXDLLEXPORT wxAtoi(const wxChar *psz)
456 {
457 return atoi(wxConvLibc.cWX2MB(psz));
458 }
459
460 long WXDLLEXPORT wxAtol(const wxChar *psz)
461 {
462 return atol(wxConvLibc.cWX2MB(psz));
463 }
464
465 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
466 {
467 static wxHashTable env;
468 // check if we already have stored the converted env var
469 wxObject *data = env.Get(name);
470 if (!data) {
471 // nope, retrieve it,
472 const char *val = getenv(wxConvLibc.cWX2MB(name));
473 if (!val) return (wxChar *)NULL;
474 // convert it,
475 data = (wxObject *)new wxString(val);
476 // and store it
477 env.Put(name, data);
478 }
479 // return converted env var
480 return (wxChar *)((wxString *)data)->c_str();
481 }
482
483 int WXDLLEXPORT wxSystem(const wxChar *psz)
484 {
485 return system(wxConvLibc.cWX2MB(psz));
486 }
487
488 #endif
489
490 #ifdef wxNEED_WX_TIME_H
491 WXDLLEXPORT size_t wxStrftime(wxChar *s, size_t max, const wxChar *fmt, const struct tm *tm)
492 {
493 if (!max) return 0;
494 char *buf = (char *)malloc(max);
495 size_t ret = strftime(buf, max, wxConvLibc.cWX2MB(fmt), tm);
496 if (ret) {
497 wxStrcpy(s, wxConvLibc.cMB2WX(buf));
498 free(buf);
499 return wxStrlen(s);
500 } else {
501 free(buf);
502 *s = 0;
503 return 0;
504 }
505 }
506 #endif