b265161e86fc67aa7b038626955f13dff404a0ed
[public/netxms.git] / include / unicode.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2010 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU Lesser General Public License as published
7 ** by the Free Software Foundation; either version 3 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU Lesser General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: unicode.h
20 **
21 **/
22
23 #ifndef _unicode_h_
24 #define _unicode_h_
25
26 // Undef UNICODE_UCS2 and UNICODE_UCS4 if they are defined to 0
27 #if !UNICODE_UCS2
28 #undef UNICODE_UCS2
29 #endif
30 #if !UNICODE_UCS4
31 #undef UNICODE_UCS4
32 #endif
33
34 #if defined(_WIN32)
35
36 // Ensure that both UNICODE and _UNICODE are defined
37 #ifdef _UNICODE
38 #ifndef UNICODE
39 #define UNICODE
40 #endif
41 #endif
42
43 #ifdef UNICODE
44 #ifndef _UNICODE
45 #define _UNICODE
46 #endif
47 #endif
48
49 #include <tchar.h>
50
51 // Windows always use UCS-2
52 #define UNICODE_UCS2 1
53
54 #ifdef UNICODE
55
56 #define _tcstoll wcstoll
57 #define _tcstoull wcstoull
58
59 #define _ERR_error_tstring ERR_error_string_W
60
61 #else
62
63 #define _tcstoll strtoll
64 #define _tcstoull strtoull
65
66 #define _ERR_error_tstring ERR_error_string
67
68 #endif
69
70 #define ICONV_DEFAULT_CODEPAGE "ACP"
71
72 #define ucs2_strlen wcslen
73 #define ucs2_strdup wcsdup
74 #define ucs2_strncpy wcsncpy
75
76 #define ucs2_to_mb(wstr, wlen, mstr, mlen) WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wstr, wlen, mstr, mlen, NULL, NULL)
77 #define mb_to_ucs2(mstr, mlen, wstr, wlen) MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mstr, mlen, wstr, wlen)
78
79 #define UCS2CHAR WCHAR
80
81 #elif defined(__SYMBIAN32__)
82
83 // Ensure that both UNICODE and _UNICODE are defined
84 #ifdef _UNICODE
85 #ifndef UNICODE
86 #define UNICODE
87 #endif
88 #endif
89
90 #ifdef UNICODE
91 #ifndef _UNICODE
92 #define _UNICODE
93 #endif
94 #endif
95
96 // Symbian always use UCS-2
97 #define UNICODE_UCS2 1
98
99 #define TCHAR TText
100 #define WCHAR TText16
101 #define UCS2CHAR TText16
102
103 #ifdef UNICODE
104 #define _T(x) L##x
105 #else
106 #define _T(x) x
107 #endif
108
109 #else /* not _WIN32 and __SYMBIAN32__ */
110
111 #if defined(_NETWARE) && defined(__GNUC__) && defined(__cplusplus)
112 #define _WCHAR_T
113 #define _WCHAR_T_DEFINED
114 #define wchar_t unsigned short
115 #endif
116
117
118 #if HAVE_WCHAR_H
119 #include <wchar.h>
120 #endif
121
122 #if HAVE_STRING_H
123 #include <string.h>
124 #endif
125
126 #if HAVE_WCHAR_T
127
128 #define WCHAR wchar_t
129 #if UNICODE_UCS2
130 #define UCS2CHAR wchar_t
131 #else
132 #define UCS2CHAR unsigned short
133 #endif
134
135 #else /* wchar_t not presented */
136
137 #define WCHAR unsigned short
138 #define UCS2CHAR unsigned short
139 #undef UNICODE_UCS2
140 #undef UNICODE_UCS4
141 #define UNICODE_UCS2 1
142
143 #endif
144
145 // Use system wide character functions if system's wchar_t is 2 bytes long
146 #if UNICODE_UCS2
147
148 #if HAVE_WCSLEN
149 #define ucs2_strlen wcslen
150 #else
151 #define wcslen ucs2_strlen
152 #endif
153
154 #if HAVE_WCSDUP
155 #define ucs2_strdup wcsdup
156 #else
157 #define wcsdup ucs2_strdup
158 #endif
159
160 #if HAVE_WCSNCPY
161 #define ucs2_strncpy wcsncpy
162 #else
163 #define wcsncpy ucs2_strncpy
164 #endif
165
166 #endif
167
168 // On some old systems, ctype.h defines _T macro, so we include it
169 // before our definition and do an undef
170 #include <ctype.h>
171 #undef _T
172
173 #ifdef UNICODE
174
175 #define _T(x) L##x
176 #define TCHAR wchar_t
177 #define _TINT int
178
179 #define _tcscpy wcscpy
180 #define _tcsncpy wcsncpy
181 #define _tcslen wcslen
182 #define _tcschr wcschr
183 #define _tcsrchr wcsrchr
184 #define _tcscmp wcscmp
185 #define _tcsicmp wcsicmp
186 #define _tcsncmp wcsncmp
187 #define _tcsnicmp wcsnicmp
188 #define _tprintf nx_wprintf
189 #define _ftprintf nx_fwprintf
190 #define _sntprintf nx_swprintf
191 #define _vtprintf nx_vwprintf
192 #define _vftprintf nx_vfwprintf
193 #define _vsntprintf nx_vswprintf
194 #define _stscanf swscanf
195 #define _tfopen wfopen
196 #define _fgetts fgetws
197 #define _fputts fputws
198 #define _tcstol wcstol
199 #define _tcstoul wcstoul
200 #define _tcstoll wcstoll
201 #define _tcstoull wcstoull
202 #define _tcstod wcstod
203 #define _tcsdup wcsdup
204 #define _tcsupr wcsupr
205 #define _tcsspn wcsspn
206 #define _tcscspn wcscspn
207 #define _tcsstr wcsstr
208 #define _tcscat wcscat
209 #define _tcsncat wcsncat
210 #define _tcspbrk wcspbrk
211 #define _tcstok wcstok
212 #define _topen wopen
213 #define _taccess waccess
214 #define _tstat wstat
215 #define _tunlink wunlink
216 #define _trename wrename
217 #define _tremove wremove
218 #define _tcsftime wcsftime
219 #define _tctime wctime
220 #define _istspace iswspace
221 #define _istdigit iswdigit
222 #define _istxdigit iswxdigit
223 #define _istalpha iswalpha
224 #define _istupper iswupper
225 #define _tgetenv wgetenv
226 #define _tmkdir wmkdir
227 #define _tcserror wcserror
228 #define _tcserror_r wcserror_r
229
230 #else
231
232 #define _T(x) x
233 #define TCHAR char
234 #define _TINT int
235
236 #define _tcscpy strcpy
237 #define _tcsncpy strncpy
238 #define _tcslen strlen
239 #define _tcschr strchr
240 #define _tcsrchr strrchr
241 #define _tcscmp strcmp
242 #define _tcsicmp stricmp
243 #define _tcsncmp strncmp
244 #define _tcsnicmp strnicmp
245 #define _tprintf printf
246 #define _stprintf sprintf
247 #define _ftprintf fprintf
248 #define _sntprintf snprintf
249 #define _vtprintf vprintf
250 #define _vstprintf vsprintf
251 #define _vsntprintf vsnprintf
252 #define _stscanf sscanf
253 #define _tfopen fopen
254 #define _fgetts fgets
255 #define _fputts fputs
256 #define _tcstol strtol
257 #define _tcstoul strtoul
258 #define _tcstoll strtoll
259 #define _tcstoull strtoull
260 #define _tcstod strtod
261 #define _tcsdup strdup
262 #define _tcsupr strupr
263 #define _tcsspn strspn
264 #define _tcscspn strcspn
265 #define _tcsstr strstr
266 #define _tcscat strcat
267 #define _tcsncat strncat
268 #define _tcspbrk strpbrk
269 #define _tcstok strtok
270 #define _topen open
271 #define _taccess access
272 #define _tstat stat
273 #define _tunlink unlink
274 #define _trename rename
275 #define _tremove remove
276 #define _tcsftime strftime
277 #define _tctime ctime
278 #define _istspace isspace
279 #define _istdigit isdigit
280 #define _istxdigit isxdigit
281 #define _istalpha isalpha
282 #define _istupper isupper
283 #define _tgetenv getenv
284 #define _tmkdir mkdir
285 #define _tcserror strerror
286 #define _tcserror_r strerror_r
287
288 #define _ERR_error_tstring ERR_error_string
289
290 #endif
291
292 #define CP_ACP 0
293 #define CP_UTF8 65001
294 #define MB_PRECOMPOSED 0x00000001
295 #define WC_COMPOSITECHECK 0x00000002
296 #define WC_DEFAULTCHAR 0x00000004
297
298 // Default codepage for iconv()
299 #if HAVE_ICONV_ISO_8859_1
300 #define ICONV_DEFAULT_CODEPAGE "ISO-8859-1"
301 #elif HAVE_ICONV_ISO8859_1
302 #define ICONV_DEFAULT_CODEPAGE "ISO8859-1"
303 #elif HAVE_ICONV_ASCII
304 #define ICONV_DEFAULT_CODEPAGE "ASCII"
305 #else
306 #define ICONV_DEFAULT_CODEPAGE ""
307 #endif
308
309 #endif /* _WIN32 */
310
311 // Define UCS2-specific helper functions as define to wide string helper functions if possible
312 #if UNICODE_UCS2
313 #define UCS2StringFromMBString WideStringFromMBString
314 #define MBStringFromUCS2String MBStringFromWideString
315 #endif
316
317
318 #ifdef UNICODE
319 #define _t_inet_addr inet_addr_w
320 #else
321 #define _t_inet_addr inet_addr
322 #endif
323
324
325 // Check that either UNICODE_UCS2 or UNICODE_UCS4 are defined
326 #if !defined(UNICODE_UCS2) && !defined(UNICODE_UCS4)
327 #error Neither UNICODE_UCS2 nor UNICODE_UCS4 are defined
328 #endif
329
330 #endif /* _unicode_h_ */
331