an attempt to fix glibc2.0 stupidness
[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 // Windows (VC++) has broad TCHAR support
25 #if defined(__VISUALC__) && defined(__WIN32__)
26
27 #include <tchar.h>
28 #if wxUSE_UNICODE // temporary - preserve binary compatibility
29 typedef _TCHAR wxChar;
30 typedef _TSCHAR wxSChar;
31 typedef _TUCHAR wxUChar;
32 #else
33 #define wxChar char
34 #define wxSChar signed char
35 #define wxUChar unsigned char
36 #endif
37
38 // ctype.h functions
39 #define wxIsalnum _istalnum
40 #define wxIsalpha _istalpha
41 #define wxIsctrl _istctrl
42 #define wxIsdigit _istdigit
43 #define wxIsgraph _istgraph
44 #define wxIslower _istlower
45 #define wxIsprint _istprint
46 #define wxIspunct _istpunct
47 #define wxIsspace _istspace
48 #define wxIsupper _istupper
49 #define wxIsxdigit _istxdigit
50 #define wxTolower _totlower
51 #define wxToupper _totupper
52
53 // locale.h functons
54 #define wxSetlocale _tsetlocale
55
56 // string.h functions
57 #define wxStrcat _tcscat
58 #define wxStrchr _tcschr
59 #define wxStrcmp _tcscmp
60 #define wxStrcoll _tcscoll
61 #define wxStrcpy _tcscpy
62 #define wxStrcspn _tcscspn
63 #define wxStrftime _tcsftime
64 #define wxStrncat _tcsncat
65 #define wxStrncmp _tcsncmp
66 #define wxStrncpy _tcsncpy
67 #define wxStrpbrk _tcspbrk
68 #define wxStrrchr _tcsrchr
69 #define wxStrspn _tcsspn
70 #define wxStrstr _tcsstr
71 #define wxStrtod _tcstod
72 // is there a _tcstok[_r] ?
73 #define wxStrtol _tcstol
74 #define wxStrtoul _tcstoul
75 #define wxStrxfrm _tcsxfrm
76
77 // stdio.h functions
78 #define wxFgetc _fgettc
79 #define wxFgetchar _fgettchar
80 #define wxFgets _fgetts
81 #define wxFopen _tfopen
82 #define wxFputc _fputtc
83 #define wxFputchar _fputtchar
84 #define wxFprintf _ftprintf
85 #define wxFreopen _tfreopen
86 #define wxFscanf _ftscanf
87 #define wxGetc _gettc
88 #define wxGetchar _gettchar
89 #define wxGets _getts
90 #define wxPerror _tperror
91 #define wxPrintf _tprintf
92 #define wxPutc _puttc
93 #define wxPutchar _puttchar
94 #define wxPuts _putts
95 #define wxRemove _tremove
96 #define wxRename _trename
97 #define wxScanf _tscanf
98 #define wxSprintf _stprintf
99 #define wxSscanf _stscanf
100 #define wxTmpnam _ttmpnam
101 #define wxUngetc _tungetc
102 #define wxVfprint _vftprintf
103 #define wxVprintf _vtprintf
104 #define wxVsprintf _vstprintf
105
106 // stdlib.h functions
107 #if !wxUSE_UNICODE
108 #define wxAtof atof
109 #endif
110 #define wxAtoi _ttoi
111 #define wxAtol _ttol
112 #define wxGetenv _tgetenv
113 #define wxSystem _tsystem
114
115 // time.h functions
116 #define wxAsctime _tasctime
117 #define wxCtime _tctime
118
119 // #elif defined(XXX)
120 // #include XXX-specific files here
121 // typeddef YYY wxChar;
122
123 // translate wxZZZ names
124
125 #else//!Windows (VC++)
126
127 // check whether we are doing Unicode
128 #if wxUSE_UNICODE
129
130 #include <wchar.h>
131 #include <wctype.h>
132
133 // this is probably glibc-specific
134 #if defined(__WCHAR_TYPE__)
135
136 typedef __WCHAR_TYPE__ wxChar;
137 typedef signed __WCHAR_TYPE__ wxSChar;
138 typedef unsigned __WCHAR_TYPE__ wxUChar;
139
140 #define _T(x) L##x
141
142 // ctype.h functions (wctype.h)
143 #define wxIsalnum iswalnum
144 #define wxIsalpha iswalpha
145 #define wxIsctrl iswcntrl
146 #define wxIsdigit iswdigit
147 #define wxIsgraph iswgraph
148 #define wxIslower iswlower
149 #define wxIsprint iswprint
150 #define wxIspunct iswpunct
151 #define wxIsspace iswspace
152 #define wxIsupper iswupper
153 #define wxIsxdigit iswxdigit
154
155 // VZ: the test is incorrect, should test for glibc2.0 somehow probably
156 #ifdef __GNUG__
157 // /usr/include/wctype.h incorrectly declares translations tables which
158 // provokes tons of compile-time warnings - try to correct this
159 #define wxTolower(wc) towctrans((wc), (wctrans_t)__ctype_tolower)
160 #define wxToupper(wc) towctrans((wc), (wctrans_t)__ctype_toupper)
161 #else
162 #define wxTolower towlower
163 #define wxToupper towupper
164 #endif // gcc/!gcc
165
166 // string.h functions (wchar.h)
167 #define wxStrcat wcscat
168 #define wxStrchr wcschr
169 #define wxStrcmp wcscmp
170 #define wxStrcoll wcscoll
171 #define wxStrcpy wcscpy
172 #define wxStrcspn wcscspn
173 #define wxStrncat wcsncat
174 #define wxStrncmp wcsncmp
175 #define wxStrncpy wcsncpy
176 #define wxStrpbrk wcspbrk
177 #define wxStrrchr wcsrchr
178 #define wxStrspn wcsspn
179 #define wxStrstr wcsstr
180 #define wxStrtod wcstod
181 #define wxStrtok wcstok
182 #define wxStrtol wcstol
183 #define wxStrtoul wcstoul
184 #define wxStrxfrm wcsxfrm
185
186 // glibc doesn't have wc equivalents of the other stuff
187 #define wxNEED_WX_STDIO_H
188 #define wxNEED_WX_STDLIB_H
189 #define wxNEED_WX_TIME_H
190
191 #else
192 #error "Please define your compiler's Unicode conventions in wxChar.h"
193 #endif
194 #else//!Unicode
195
196 #include <ctype.h>
197 #include <string.h>
198
199 #if 0 // temporary - preserve binary compatibilty
200 typedef char wxChar;
201 typedef signed char wxSChar;
202 typedef unsigned char wxUChar;
203 #else
204 #define wxChar char
205 #define wxSChar signed char
206 #define wxUChar unsigned char
207 #endif
208
209 #define _T(x) x
210
211 // ctype.h functions
212 #define wxIsalnum isalnum
213 #define wxIsalpha isalpha
214 #define wxIsctrl isctrl
215 #define wxIsdigit isdigit
216 #define wxIsgraph isgraph
217 #define wxIslower islower
218 #define wxIsprint isprint
219 #define wxIspunct ispunct
220 #define wxIsspace isspace
221 #define wxIsupper isupper
222 #define wxIsxdigit isxdigit
223 #define wxTolower tolower
224 #define wxToupper toupper
225
226 // locale.h functons
227 #define wxSetlocale setlocale
228
229 // string.h functions
230 #define wxStrcat strcat
231 #define wxStrchr strchr
232 #define wxStrcmp strcmp
233 #define wxStrcoll strcoll
234 #define wxStrcpy strcpy
235 #define wxStrcspn strcspn
236 #define wxStrdup strdup
237 #define wxStrncat strncat
238 #define wxStrncmp strncmp
239 #define wxStrncpy strncpy
240 #define wxStrpbrk strpbrk
241 #define wxStrrchr strrchr
242 #define wxStrspn strspn
243 #define wxStrstr strstr
244 #define wxStrtod strtod
245 // #define wxStrtok strtok_r // this needs a configure check
246 #define wxStrtol strtol
247 #define wxStrtoul strtoul
248 #define wxStrxfrm strxfrm
249
250 // stdio.h functions
251 #define wxFgetc fgetc
252 #define wxFgetchar fgetchar
253 #define wxFgets fgets
254 #define wxFopen fopen
255 #define wxFputc fputc
256 #define wxFputchar fputchar
257 #define wxFprintf fprintf
258 #define wxFreopen freopen
259 #define wxFscanf fscanf
260 #define wxGetc getc
261 #define wxGetchar getchar
262 #define wxGets gets
263 #define wxPerror perror
264 #define wxPrintf printf
265 #define wxPutc putc
266 #define wxPutchar putchar
267 #define wxPuts puts
268 #define wxRemove remove
269 #define wxRename rename
270 #define wxScanf scanf
271 #define wxSprintf sprintf
272 #define wxSscanf sscanf
273 #define wxTmpnam tmpnam
274 #define wxUngetc ungetc
275 #define wxVfprint vfprintf
276 #define wxVprintf vprintf
277 #define wxVsprintf vsprintf
278
279 // stdlib.h functions
280 #define wxAtof atof
281 #define wxAtoi atoi
282 #define wxAtol atol
283 #define wxGetenv getenv
284 #define wxSystem system
285
286 // time.h functions
287 #define wxAsctime asctime
288 #define wxCtime ctime
289 #define wxStrftime strftime
290
291 #endif//Unicode
292 #endif
293
294
295 /// checks whether the passed in pointer is NULL and if the string is empty
296 inline bool WXDLLEXPORT wxIsEmpty(const wxChar *p) { return !p || !*p; }
297
298 /// safe version of strlen() (returns 0 if passed NULL pointer)
299 inline size_t WXDLLEXPORT wxStrlen(const wxChar *psz)
300 #if defined(__VISUALC__)
301 { return psz ? _tcslen(psz) : 0; }
302 #elif wxUSE_UNICODE
303 { return psz ? wcslen(psz) : 0; }
304 #else
305 { return psz ? strlen(psz) : 0; }
306 #endif
307
308 /// portable strcasecmp/_stricmp
309 inline int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
310 #if defined(__VISUALC__)
311 { return _tcsicmp(psz1, psz2); }
312 #elif defined(__BORLANDC__) && !wxUSE_UNICODE
313 { return stricmp(psz1, psz2); }
314 #elif defined(__UNIX__) || defined(__GNUWIN32__)
315 #if !wxUSE_UNICODE
316 { return strcasecmp(psz1, psz2); }
317 #else // glibc doesn't seem to have wide char equivalent
318 {
319 register wxChar c1, c2;
320 do {
321 c1 = wxTolower(*psz1++);
322 c2 = wxTolower(*psz2++);
323 } while ( c1 && (c1 == c2) );
324
325 return c1 - c2;
326 }
327 #endif
328 #else
329 // almost all compilers/libraries provide this function (unfortunately under
330 // different names), that's why we don't implement our own which will surely
331 // be more efficient than this code (uncomment to use):
332 /*
333 register wxChar c1, c2;
334 do {
335 c1 = wxTolower(*psz1++);
336 c2 = wxTolower(*psz2++);
337 } while ( c1 && (c1 == c2) );
338
339 return c1 - c2;
340 */
341
342 #error "Please define string case-insensitive compare for your OS/compiler"
343 #endif // OS/compiler
344
345 // multibyte<->widechar conversion
346 size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n);
347 size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *psz, size_t n);
348 #if wxUSE_UNICODE
349 #define wxMB2WX wxMB2WC
350 #define wxWX2MB wxWC2MB
351 #define wxWC2WX wxStrncpy
352 #define wxWX2WC wxStrncpy
353 #else
354 #define wxMB2WX wxStrncpy
355 #define wxWX2MB wxStrncpy
356 #define wxWC2WX wxWC2MB
357 #define wxWX2WC wxMB2WC
358 #endif
359
360 // if libc versions are not available, use replacements defined in wxchar.cpp
361 #ifndef wxStrdup
362 wxChar * WXDLLEXPORT wxStrdup(const wxChar *psz);
363 #endif
364
365 #ifndef wxStrtok
366 wxChar * WXDLLEXPORT wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr);
367 #endif
368
369 #ifndef wxSetlocale
370 wxChar * WXDLLEXPORT wxSetlocale(int category, const wxChar *locale);
371 #endif
372
373 #ifdef wxNEED_WX_STDIO_H
374 #include <stdarg.h>
375 int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...);
376 int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr);
377 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...);
378 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr);
379 #endif
380
381 #ifndef wxAtof
382 double WXDLLEXPORT wxAtof(const wxChar *psz);
383 #endif
384
385 #ifdef wxNEED_WX_STDLIB_H
386 int WXDLLEXPORT wxAtoi(const wxChar *psz);
387 long WXDLLEXPORT wxAtol(const wxChar *psz);
388 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name);
389 int WXDLLEXPORT wxSystem(const wxChar *psz);
390 #endif
391
392 #endif
393 //_WX_WXCHAR_H_