Must include wchar.h in non-Unicode build too, since buffer.h depends on it
[wxWidgets.git] / include / wx / wxchar.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxchar.h
3 // Purpose: Declarations common to wx char/wchar_t usage (wide chars)
4 // Author: Joel Farley
5 // Modified by:
6 // Created: 1998/06/12
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows copyright
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_WXCHAR_H_
13 #define _WX_WXCHAR_H_
14
15 #ifdef __GNUG__
16 #pragma interface "wxchar.h"
17 #endif
18
19 // only do SBCS or _UNICODE
20 #if defined (_MBCS )
21 #error "MBCS is not supported by wxChar"
22 #endif
23
24 // set wxUSE_UNICODE to 1 if UNICODE or _UNICODE is defined
25 #if defined(_UNICODE) || defined(UNICODE)
26 #undef wxUSE_UNICODE
27 #define wxUSE_UNICODE 1
28 #else
29 #ifndef wxUSE_UNICODE
30 #define wxUSE_UNICODE 0
31 #endif
32 #endif
33
34 // and vice versa: define UNICODE and _UNICODE if wxUSE_UNICODE is 1...
35 #if wxUSE_UNICODE
36 #ifndef _UNICODE
37 #define _UNICODE
38 #endif
39 #ifndef UNICODE
40 #define UNICODE
41 #endif
42 #endif
43
44 // Windows (VC++) has broad TCHAR support
45 #if defined(__VISUALC__) && defined(__WIN32__)
46
47 #include <tchar.h>
48 #if wxUSE_UNICODE // temporary - preserve binary compatibility
49 typedef _TCHAR wxChar;
50 typedef _TSCHAR wxSChar;
51 typedef _TUCHAR wxUChar;
52 #else
53 #define wxChar char
54 #define wxSChar signed char
55 #define wxUChar unsigned char
56 #endif
57
58 // ctype.h functions
59 #define wxIsalnum _istalnum
60 #define wxIsalpha _istalpha
61 #define wxIsctrl _istctrl
62 #define wxIsdigit _istdigit
63 #define wxIsgraph _istgraph
64 #define wxIslower _istlower
65 #define wxIsprint _istprint
66 #define wxIspunct _istpunct
67 #define wxIsspace _istspace
68 #define wxIsupper _istupper
69 #define wxIsxdigit _istxdigit
70 #define wxTolower _totlower
71 #define wxToupper _totupper
72
73 // locale.h functons
74 #define wxSetlocale _tsetlocale
75
76 // string.h functions
77 #define wxStrcat _tcscat
78 #define wxStrchr _tcschr
79 #define wxStrcmp _tcscmp
80 #define wxStrcoll _tcscoll
81 #define wxStrcpy _tcscpy
82 #define wxStrcspn _tcscspn
83 #define wxStrftime _tcsftime
84 #define wxStricmp _tcsicmp
85 #define wxStrlen_ _tcslen // used in wxStrlen inline function
86 #define wxStrncat _tcsncat
87 #define wxStrncmp _tcsncmp
88 #define wxStrncpy _tcsncpy
89 #define wxStrpbrk _tcspbrk
90 #define wxStrrchr _tcsrchr
91 #define wxStrspn _tcsspn
92 #define wxStrstr _tcsstr
93 #define wxStrtod _tcstod
94 // is there a _tcstok[_r] ?
95 #define wxStrtol _tcstol
96 #define wxStrtoul _tcstoul
97 #define wxStrxfrm _tcsxfrm
98
99 // stdio.h functions
100 #define wxFgetc _fgettc
101 #define wxFgetchar _fgettchar
102 #define wxFgets _fgetts
103 #define wxFopen _tfopen
104 #define wxFputc _fputtc
105 #define wxFputchar _fputtchar
106 #define wxFprintf _ftprintf
107 #define wxFreopen _tfreopen
108 #define wxFscanf _ftscanf
109 #define wxGetc _gettc
110 #define wxGetchar _gettchar
111 #define wxGets _getts
112 #define wxPerror _tperror
113 #define wxPrintf _tprintf
114 #define wxPutc _puttc
115 #define wxPutchar _puttchar
116 #define wxPuts _putts
117 #define wxRemove _tremove
118 #define wxRename _trename
119 #define wxScanf _tscanf
120 #define wxSprintf _stprintf
121 #define wxSscanf _stscanf
122 #define wxTmpnam _ttmpnam
123 #define wxUngetc _tungetc
124 #define wxVfprint _vftprintf
125 #define wxVprintf _vtprintf
126 #define wxVsscanf _vstscanf
127 #define wxVsprintf _vstprintf
128
129 // stdlib.h functions
130 #if !wxUSE_UNICODE
131 #define wxAtof atof
132 #endif
133 #define wxAtoi _ttoi
134 #define wxAtol _ttol
135 #define wxGetenv _tgetenv
136 #define wxSystem _tsystem
137
138 // time.h functions
139 #define wxAsctime _tasctime
140 #define wxCtime _tctime
141
142 // #elif defined(XXX)
143 // #include XXX-specific files here
144 // typeddef YYY wxChar;
145
146 // translate wxZZZ names
147
148 #elif defined(__BORLANDC__) && defined(__WIN32__)
149
150 // Borland C++ 4.52 doesn't have much tchar support
151 // maybe Borland C++ 5.02 has, can't check right now
152 // but I'll use the Win32 API instead here
153
154 #include <tchar.h>
155 #if wxUSE_UNICODE // temporary - preserve binary compatibility
156 typedef _TCHAR wxChar;
157 typedef _TSCHAR wxSChar;
158 typedef _TUCHAR wxUChar;
159 #else
160 #define wxChar char
161 #define wxSChar signed char
162 #define wxUChar unsigned char
163 #endif
164
165 #include <windef.h>
166 #include <winbase.h>
167 #include <winnls.h>
168 #include <winnt.h>
169
170 // ctype.h functions
171 inline WORD __wxMSW_ctype(wxChar ch)
172 {
173 WORD ret;
174 GetStringTypeEx(LOCALE_USER_DEFAULT, CT_CTYPE1, &ch, 1, &ret);
175 return ret;
176 }
177 #define wxIsalnum(x) IsCharAlphaNumeric
178 #define wxIsalpha IsCharAlpha
179 #define wxIsctrl(x) (__wxMSW_ctype(x) & C1_CNTRL)
180 #define wxIsdigit(x) (__wxMSW_ctype(x) & C1_DIGIT)
181 #define wxIsgraph(x) (__wxMSW_ctype(x) & (C1_DIGIT|C1_PUNCT|C1_ALPHA))
182 #define wxIslower(x) IsCharLower
183 #define wxIsprint(x) (__wxMSW_ctype(x) & (C1_DIGIT|C1_SPACE|C1_PUNCT|C1_ALPHA))
184 #define wxIspunct(x) (__wxMSW_ctype(x) & C1_PUNCT)
185 #define wxIsspace(x) (__wxMSW_ctype(x) & C1_SPACE)
186 #define wxIsupper(x) IsCharUpper
187 #define wxIsxdigit(x) (__wxMSW_ctype(x) & C1_XDIGIT)
188 #define wxTolower(x) (wxChar)CharLower((LPTSTR)(x))
189 #define wxToupper(x) (wxChar)CharUpper((LPTSTR)(x))
190
191 // #define wxStrtok strtok_r // Borland C++ 4.52 doesn't have strtok_r
192 #define wxNEED_WX_STRING_H
193 #define wxNEED_WX_STDIO_H
194 #define wxNEED_WX_STDLIB_H
195 #define wxNEED_WX_TIME_H
196 #define wxNEED_WCSLEN
197
198 #else//!Windows
199
200 // wchar_t will be used in buffer.h, so include it rather unconditionally
201 #include <wchar.h>
202
203 // check whether we are doing Unicode
204 #if wxUSE_UNICODE
205
206 #include <wctype.h>
207
208 // this is probably glibc-specific
209 #if defined(__WCHAR_TYPE__)
210
211 typedef __WCHAR_TYPE__ wxChar;
212 typedef signed __WCHAR_TYPE__ wxSChar;
213 typedef unsigned __WCHAR_TYPE__ wxUChar;
214
215 #define _T(x) L##x
216
217 // ctype.h functions (wctype.h)
218 #define wxIsalnum iswalnum
219 #define wxIsalpha iswalpha
220 #define wxIsctrl iswcntrl
221 #define wxIsdigit iswdigit
222 #define wxIsgraph iswgraph
223 #define wxIslower iswlower
224 #define wxIsprint iswprint
225 #define wxIspunct iswpunct
226 #define wxIsspace iswspace
227 #define wxIsupper iswupper
228 #define wxIsxdigit iswxdigit
229
230 #if defined(__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0)
231 // /usr/include/wctype.h incorrectly declares translations tables which
232 // provokes tons of compile-time warnings - try to correct this
233 #define wxTolower(wc) towctrans((wc), (wctrans_t)__ctype_tolower)
234 #define wxToupper(wc) towctrans((wc), (wctrans_t)__ctype_toupper)
235 #else
236 #define wxTolower towlower
237 #define wxToupper towupper
238 #endif // gcc/!gcc
239
240 // string.h functions (wchar.h)
241 #define wxStrcat wcscat
242 #define wxStrchr wcschr
243 #define wxStrcmp wcscmp
244 #define wxStrcoll wcscoll
245 #define wxStrcpy wcscpy
246 #define wxStrcspn wcscspn
247 #define wxStrlen_ wcslen // used in wxStrlen inline function
248 #define wxStrncat wcsncat
249 #define wxStrncmp wcsncmp
250 #define wxStrncpy wcsncpy
251 #define wxStrpbrk wcspbrk
252 #define wxStrrchr wcsrchr
253 #define wxStrspn wcsspn
254 #define wxStrstr wcsstr
255 #define wxStrtod wcstod
256 #define wxStrtok wcstok
257 #define wxStrtol wcstol
258 #define wxStrtoul wcstoul
259 #define wxStrxfrm wcsxfrm
260
261 // glibc doesn't have wc equivalents of the other stuff
262 #define wxNEED_WX_STDIO_H
263 #define wxNEED_WX_STDLIB_H
264 #define wxNEED_WX_TIME_H
265
266 #else//!glibc
267 #error "Please define your compiler's Unicode conventions in wxChar.h"
268 #endif
269 #else//!Unicode
270
271 #include <ctype.h>
272 #include <string.h>
273
274 #if 0 // temporary - preserve binary compatibilty
275 typedef char wxChar;
276 typedef signed char wxSChar;
277 typedef unsigned char wxUChar;
278 #else
279 #define wxChar char
280 #define wxSChar signed char
281 #define wxUChar unsigned char
282 #endif
283
284 #define _T(x) x
285
286 // ctype.h functions
287 #define wxIsalnum isalnum
288 #define wxIsalpha isalpha
289 #define wxIsctrl isctrl
290 #define wxIsdigit isdigit
291 #define wxIsgraph isgraph
292 #define wxIslower islower
293 #define wxIsprint isprint
294 #define wxIspunct ispunct
295 #define wxIsspace isspace
296 #define wxIsupper isupper
297 #define wxIsxdigit isxdigit
298 #define wxTolower tolower
299 #define wxToupper toupper
300
301 // locale.h functons
302 #define wxSetlocale setlocale
303
304 // string.h functions
305 #define wxStricmp strcasecmp
306 // #define wxStrtok strtok_r // this needs a configure check
307
308 // leave the rest to defaults below
309 #define wxNEED_WX_STRING_H
310 #define wxNEED_WX_STDIO_H
311 #define wxNEED_WX_STDLIB_H
312 #define wxNEED_WX_TIME_H
313
314 #endif//Unicode
315 #endif//TCHAR-aware compilers
316
317 // define wxStricmp for various compilers without Unicode possibilities
318 #if !defined(wxStricmp) && !wxUSE_UNICODE
319 #if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__SALFORDC__)
320 #define wxStricmp stricmp
321 #elif defined(__SC__) || defined(__VISUALC__) || (defined(__MWERKS) && defined(__INTEL__))
322 #define wxStricmp _stricmp
323 #elif defined(__UNIX__) || defined(__GNUWIN32__)
324 #define wxStricmp strcasecmp
325 #elif defined(__MWERKS__) && !defined(__INTEL__)
326 // use wxWindows' implementation
327 #else
328 // if you leave wxStricmp undefined, wxWindows' implementation will be used
329 #error "Please define string case-insensitive compare for your OS/compiler"
330 #endif
331 #endif
332
333 // if we need to define for standard headers, and we're not using Unicode,
334 // just define to standard C library routines
335 #if !wxUSE_UNICODE
336 #ifdef wxNEED_WX_STRING_H
337 #define wxStrcat strcat
338 #define wxStrchr strchr
339 #define wxStrcmp strcmp
340 #define wxStrcoll strcoll
341 #define wxStrcpy strcpy
342 #define wxStrcspn strcspn
343 #define wxStrdup strdup
344 #define wxStrlen_ strlen // used in wxStrlen inline function
345 #define wxStrncat strncat
346 #define wxStrncmp strncmp
347 #define wxStrncpy strncpy
348 #define wxStrpbrk strpbrk
349 #define wxStrrchr strrchr
350 #define wxStrspn strspn
351 #define wxStrstr strstr
352 #define wxStrtod strtod
353 #define wxStrtol strtol
354 #define wxStrtoul strtoul
355 #define wxStrxfrm strxfrm
356 #undef wxNEED_WX_STRING_H
357 #endif
358
359 #ifdef wxNEED_WX_STDIO_H
360 #define wxFgetc fgetc
361 #define wxFgetchar fgetchar
362 #define wxFgets fgets
363 #define wxFopen fopen
364 #define wxFputc fputc
365 #define wxFputchar fputchar
366 #define wxFprintf fprintf
367 #define wxFreopen freopen
368 #define wxFscanf fscanf
369 #define wxGetc getc
370 #define wxGetchar getchar
371 #define wxGets gets
372 #define wxPerror perror
373 #define wxPrintf printf
374 #define wxPutc putc
375 #define wxPutchar putchar
376 #define wxPuts puts
377 #define wxRemove remove
378 #define wxRename rename
379 #define wxScanf scanf
380 #define wxSprintf sprintf
381 #define wxSscanf sscanf
382 #define wxTmpnam tmpnam
383 #define wxUngetc ungetc
384 #define wxVfprint vfprintf
385 #define wxVprintf vprintf
386 #define wxVsscanf vsscanf
387 #define wxVsprintf vsprintf
388 #undef wxNEED_WX_STDIO_H
389 #endif
390
391 #ifdef wxNEED_WX_STDLIB_H
392 #define wxAtof atof
393 #define wxAtoi atoi
394 #define wxAtol atol
395 #define wxGetenv getenv
396 #define wxSystem system
397 #undef wxNEED_WX_STDLIB_H
398 #endif
399
400 #ifdef wxNEED_WX_TIME_H
401 #define wxAsctime asctime
402 #define wxCtime ctime
403 #define wxStrftime strftime
404 #undef wxNEED_WX_TIME_H
405 #endif
406 #endif //!Unicode
407
408 #if defined(wxNEED_WCSLEN) && wxUSE_UNICODE
409 #define wcslen wxStrlen
410 #undef wxNEED_WCSLEN
411 #endif
412
413 /// checks whether the passed in pointer is NULL and if the string is empty
414 inline bool WXDLLEXPORT wxIsEmpty(const wxChar *p) { return !p || !*p; }
415
416 #ifndef wxNEED_WX_STRING_H
417 /// safe version of strlen() (returns 0 if passed NULL pointer)
418 inline size_t WXDLLEXPORT wxStrlen(const wxChar *psz)
419 { return psz ? wxStrlen_(psz) : 0; }
420 #endif
421
422 // multibyte<->widechar conversion
423 size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n);
424 size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *psz, size_t n);
425 #if wxUSE_UNICODE
426 #define wxMB2WX wxMB2WC
427 #define wxWX2MB wxWC2MB
428 #define wxWC2WX wxStrncpy
429 #define wxWX2WC wxStrncpy
430 #else
431 #define wxMB2WX wxStrncpy
432 #define wxWX2MB wxStrncpy
433 #define wxWC2WX wxWC2MB
434 #define wxWX2WC wxMB2WC
435 #endif
436
437 // if libc versions are not available, use replacements defined in wxchar.cpp
438 #ifndef wxStrdup
439 wxChar * WXDLLEXPORT wxStrdup(const wxChar *psz);
440 #endif
441
442 #ifndef wxStricmp
443 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2);
444 #endif
445
446 #ifndef wxStrtok
447 wxChar * WXDLLEXPORT wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr);
448 #endif
449
450 #ifndef wxSetlocale
451 wxChar * WXDLLEXPORT wxSetlocale(int category, const wxChar *locale);
452 #endif
453
454 #ifdef wxNEED_WCSLEN // for use in buffer.h
455 size_t WXDLLEXPORT wcslen(const wchar_t *s);
456 #endif
457
458 #ifdef wxNEED_WX_STRING_H
459 int WXDLLEXPORT wxStrcmp(const wxChar *psz1, const wxChar *psz2);
460 size_t WXDLLEXPORT wxStrlen(const wxChar *s);
461 #endif
462
463 #ifdef wxNEED_WX_STDIO_H
464 #include <stdio.h>
465 #include <stdarg.h>
466 int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...);
467 int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr);
468 int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...);
469 int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr);
470 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...);
471 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr);
472 int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...);
473 int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr);
474 #endif
475
476 #ifndef wxAtof
477 double WXDLLEXPORT wxAtof(const wxChar *psz);
478 #endif
479
480 #ifdef wxNEED_WX_STDLIB_H
481 int WXDLLEXPORT wxAtoi(const wxChar *psz);
482 long WXDLLEXPORT wxAtol(const wxChar *psz);
483 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name);
484 int WXDLLEXPORT wxSystem(const wxChar *psz);
485 #endif
486
487 #endif
488 //_WX_WXCHAR_H_