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