]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
compile fix
[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 // GNU libc uses UTF-8 even when it shouldn't
101 wchar_t res;
102 if ((MB_CUR_MAX == 2) &&
103 (wxMB2WC(&res, "\xdd\xa5", 1)>0) &&
104 (res==0x765)) {
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
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 wxChar * wxSetlocale(int category, const wxChar *locale)
193 {
194 #ifdef wxUSE_THREADS
195 wxASSERT_MSG( wxThread::IsMain(), _T("wxSetlocale() is not MT-safe") );
196 #endif
197
198 static wxWCharBuffer s_wzLocale;
199
200 char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale));
201 s_wzLocale = wxConvLibc.cMB2WC(localeOld);
202
203 return s_wzLocale;
204 }
205 #endif
206
207 #ifdef wxNEED_WX_STRING_H
208 WXDLLEXPORT wxChar * wxStrcat(wxChar *dest, const wxChar *src)
209 {
210 wxChar *ret = dest;
211 while (*dest) dest++;
212 while ((*dest++ = *src++));
213 return ret;
214 }
215
216 WXDLLEXPORT wxChar * wxStrchr(const wxChar *s, wxChar c)
217 {
218 while (*s && *s != c) s++;
219 return (*s) ? (wxChar *)s : (wxChar *)NULL;
220 }
221
222 WXDLLEXPORT int wxStrcmp(const wxChar *s1, const wxChar *s2)
223 {
224 while ((*s1 == *s2) && *s1) s1++, s2++;
225 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
226 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
227 return 0;
228 }
229
230 WXDLLEXPORT wxChar * wxStrcpy(wxChar *dest, const wxChar *src)
231 {
232 wxChar *ret = dest;
233 while ((*dest++ = *src++));
234 return ret;
235 }
236
237 WXDLLEXPORT wxChar * wxStrncat(wxChar *dest, const wxChar *src, size_t n)
238 {
239 wxChar *ret = dest;
240 while (*dest) dest++;
241 while (n && (*dest++ = *src++)) n--;
242 return ret;
243 }
244
245 WXDLLEXPORT int wxStrncmp(const wxChar *s1, const wxChar *s2, size_t n)
246 {
247 while (n && (*s1 == *s2) && *s1) n--, s1++, s2++;
248 if (n) {
249 if ((wxUChar)*s1 < (wxUChar)*s2) return -1;
250 if ((wxUChar)*s1 > (wxUChar)*s2) return 1;
251 }
252 return 0;
253 }
254
255 WXDLLEXPORT wxChar * wxStrncpy(wxChar *dest, const wxChar *src, size_t n)
256 {
257 wxChar *ret = dest;
258 while (n && (*dest++ = *src++)) n--;
259 while (n) *dest++=0, n--; // the docs specify padding with zeroes
260 return ret;
261 }
262
263 WXDLLEXPORT wxChar * wxStrpbrk(const wxChar *s, const wxChar *accept)
264 {
265 while (*s && !wxStrchr(accept, *s)) s++;
266 return (*s) ? (wxChar *)s : (wxChar *)NULL;
267 }
268
269 WXDLLEXPORT wxChar * wxStrrchr(const wxChar *s, wxChar c)
270 {
271 wxChar *ret = (wxChar *)NULL;
272 while (*s) {
273 if (*s == c) ret = (wxChar *)s;
274 s++;
275 }
276 return ret;
277 }
278
279 WXDLLEXPORT size_t wxStrspn(const wxChar *s, const wxChar *accept)
280 {
281 size_t len = 0;
282 while (wxStrchr(accept, *s++)) len++;
283 return len;
284 }
285
286 WXDLLEXPORT wxChar * wxStrstr(const wxChar *haystack, const wxChar *needle)
287 {
288 wxChar *fnd;
289 while ((fnd = wxStrchr(haystack, *needle))) {
290 if (!wxStrcmp(fnd, needle)) return fnd;
291 haystack = fnd + 1;
292 }
293 return (wxChar *)NULL;
294 }
295
296 WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
297 {
298 const wxChar *start = nptr;
299
300 // FIXME: only correct for C locale
301 while (wxIsspace(*nptr)) nptr++;
302 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
303 while (wxIsdigit(*nptr)) nptr++;
304 if (*nptr == wxT('.')) {
305 nptr++;
306 while (wxIsdigit(*nptr)) nptr++;
307 }
308 if (*nptr == wxT('E') || *nptr == wxT('e')) {
309 nptr++;
310 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
311 while (wxIsdigit(*nptr)) nptr++;
312 }
313
314 wxString data(nptr, nptr-start);
315 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
316 char *rdat = wxMBSTRINGCAST dat;
317 double ret = strtod(dat, &rdat);
318
319 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
320
321 return ret;
322 }
323
324 WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base)
325 {
326 const wxChar *start = nptr;
327
328 // FIXME: only correct for C locale
329 while (wxIsspace(*nptr)) nptr++;
330 if (*nptr == wxT('+') || *nptr == wxT('-')) nptr++;
331 if (((base == 0) || (base == 16)) &&
332 (nptr[0] == wxT('0') && nptr[1] == wxT('x'))) {
333 nptr += 2;
334 base = 16;
335 }
336 else if ((base == 0) && (nptr[0] == wxT('0'))) base = 8;
337 else if (base == 0) base = 10;
338
339 while ((wxIsdigit(*nptr) && (*nptr - wxT('0') < base)) ||
340 (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
341
342 wxString data(nptr, nptr-start);
343 wxWX2MBbuf dat = data.mb_str(wxConvLibc);
344 char *rdat = wxMBSTRINGCAST dat;
345 long int ret = strtol(dat, &rdat, base);
346
347 if (endptr) *endptr = (wxChar *)(start + (rdat - (const char *)dat));
348
349 return ret;
350 }
351 #endif
352
353 #ifdef wxNEED_WX_STDIO_H
354 WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode)
355 {
356 return fopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode));
357 }
358
359 WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream)
360 {
361 return freopen(wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode), stream);
362 }
363
364 int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...)
365 {
366 va_list argptr;
367 int ret;
368
369 va_start(argptr, fmt);
370 ret = wxVprintf(fmt, argptr);
371 va_end(argptr);
372 return ret;
373 }
374
375 int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr)
376 {
377 wxString str;
378 str.PrintfV(fmt,argptr);
379 printf("%s", (const char*)str.mb_str());
380 return str.Len();
381 }
382
383 int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...)
384 {
385 va_list argptr;
386 int ret;
387
388 va_start(argptr, fmt);
389 ret = wxVfprintf(stream, fmt, argptr);
390 va_end(argptr);
391 return ret;
392 }
393
394 int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr)
395 {
396 wxString str;
397 str.PrintfV(fmt,argptr);
398 fprintf(stream, "%s", (const char*)str.mb_str());
399 return str.Len();
400 }
401
402 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...)
403 {
404 va_list argptr;
405 int ret;
406
407 va_start(argptr, fmt);
408 ret = wxVsprintf(buf, fmt, argptr);
409 va_end(argptr);
410 return ret;
411 }
412
413 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr)
414 {
415 // this might be sort of inefficient, but it doesn't matter since
416 // we'd prefer people to use wxString::Printf directly instead anyway
417 wxString str;
418 str.PrintfV(fmt,argptr);
419 wxStrcpy(buf,str.c_str());
420 return str.Len();
421 }
422
423 int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...)
424 {
425 va_list argptr;
426 int ret;
427
428 va_start(argptr, fmt);
429 ret = wxVsscanf(buf, fmt, argptr);
430 va_end(argptr);
431 return ret;
432 }
433
434 int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr)
435 {
436 int ret;
437 // this will work only for numeric conversion! Strings will not be converted correctly
438 // hopefully this is all we'll need
439 ret = vsscanf(wxConvLibc.cWX2MB(buf), wxConvLibc.cWX2MB(fmt), argptr);
440 return ret;
441 }
442 #endif
443
444 #ifndef wxAtof
445 double WXDLLEXPORT wxAtof(const wxChar *psz)
446 {
447 return atof(wxConvLibc.cWX2MB(psz));
448 }
449 #endif
450
451 #ifdef wxNEED_WX_STDLIB_H
452 int WXDLLEXPORT wxAtoi(const wxChar *psz)
453 {
454 return atoi(wxConvLibc.cWX2MB(psz));
455 }
456
457 long WXDLLEXPORT wxAtol(const wxChar *psz)
458 {
459 return atol(wxConvLibc.cWX2MB(psz));
460 }
461
462 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
463 {
464 static wxHashTable env;
465 // check if we already have stored the converted env var
466 wxObject *data = env.Get(name);
467 if (!data) {
468 // nope, retrieve it,
469 const char *val = getenv(wxConvLibc.cWX2MB(name));
470 if (!val) return (wxChar *)NULL;
471 // convert it,
472 data = (wxObject *)new wxString(val);
473 // and store it
474 env.Put(name, data);
475 }
476 // return converted env var
477 return (wxChar *)((wxString *)data)->c_str();
478 }
479
480 int WXDLLEXPORT wxSystem(const wxChar *psz)
481 {
482 return system(wxConvLibc.cWX2MB(psz));
483 }
484
485 #endif
486
487 #ifdef wxNEED_WX_TIME_H
488 WXDLLEXPORT size_t wxStrftime(wxChar *s, size_t max, const wxChar *fmt, const struct tm *tm)
489 {
490 if (!max) return 0;
491 char *buf = (char *)malloc(max);
492 size_t ret = strftime(buf, max, wxConvLibc.cWX2MB(fmt), tm);
493 if (ret) {
494 wxStrcpy(s, wxConvLibc.cMB2WX(buf));
495 free(buf);
496 return wxStrlen(s);
497 } else {
498 free(buf);
499 *s = 0;
500 return 0;
501 }
502 }
503 #endif