Preparation for UNICODE support on UNIX
[public/netxms.git] / include / unicode.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003, 2004, 2005, 2006 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 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 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 #ifdef _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 #ifdef UNICODE
52
53 // Windows always use UCS-2
54 #define UNICODE_UCS2 1
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 HAVE_WFOPEN 1
80 #define HAVE_WOPEN 1
81
82 #else /* not _WIN32 */
83
84 #if HAVE_WCHAR_H
85 #include <wchar.h>
86 #endif
87
88 #if HAVE_STRING_H
89 #include <string.h>
90 #endif
91
92 /*#ifdef _NETWARE
93 #define WCHAR wchar_t
94 #else
95 #define WCHAR unsigned short
96 #endif*/
97
98 #define WCHAR wchar_t
99 #if UNICODE_UCS2
100 #define UCS2CHAR wchar_t
101 #else
102 #define UCS2CHAR unsigned short
103 #endif
104
105 // Use system wide character functions if system's wchar_t is 2 bytes long
106 #if UNICODE_UCS2
107
108 #define ucs2_strlen wcslen
109 #define ucs2_strdup wcsdup
110 #define ucs2_strncpy wcsncpy
111
112 #endif
113
114 // On some old systems, ctype.h defines _T macro, so we include it
115 // before our definition and do an undef
116 #include <ctype.h>
117 #undef _T
118
119 #ifdef UNICODE
120
121 #define _T(x) L##x
122 #define TCHAR wchar_t
123 #define _TINT int
124
125 #define _tcscpy wcscpy
126 #define _tcsncpy wcsncpy
127 #define _tcslen wcslen
128 #define _tcschr wcschr
129 #define _tcsrchr wcsrchr
130 #define _tcscmp wcscmp
131 #define _tcsicmp wcsicmp
132 #define _tcsncmp wcsncmp
133 #define _tcsnicmp wcsnicmp
134 #define _tprintf wprintf
135 //#define _stprintf swprintf
136 #define _ftprintf fwprintf
137 #define _sntprintf swprintf
138 #define _vtprintf vwprintf
139 //#define _vstprintf vswprintf
140 #define _vsntprintf vswprintf
141 #define _tfopen wfopen
142 #define _fgetts fgetws
143 #define _fputts fputws
144 #define _tcstol wcstol
145 #define _tcstoul wcstoul
146 #define _tcstoll wcstoll
147 #define _tcstoull wcstoull
148 #define _tcstod wcstod
149 #define _tcsdup wcsdup
150 #define _tcsupr wcsupr
151 #define _tcsspn wcsspn
152 #define _tcscspn wcscspn
153 #define _tcsstr wcsstr
154 #define _tcscat wcscat
155 #define _topen wopen
156 #define _taccess waccess
157 #define _tstat wstat
158 #define _tunlink wunlink
159 #define _tcsftime wcsftime
160 #define _tctime wctime
161 #define _istspace iswspace
162 #define _istdigit iswdigit
163 #define _istalpha iswalpha
164 #define _istupper iswupper
165 #define _tgetenv wgetenv
166
167 #else
168
169 #define _T(x) x
170 #define TCHAR char
171 #define _TINT int
172
173 #define _tcscpy strcpy
174 #define _tcsncpy strncpy
175 #define _tcslen strlen
176 #define _tcschr strchr
177 #define _tcsrchr strrchr
178 #define _tcscmp strcmp
179 #define _tcsicmp stricmp
180 #define _tcsncmp strncmp
181 #define _tcsnicmp strnicmp
182 #define _tprintf printf
183 #define _stprintf sprintf
184 #define _ftprintf fprintf
185 #define _sntprintf snprintf
186 #define _vtprintf vprintf
187 #define _vstprintf vsprintf
188 #define _vsntprintf vsnprintf
189 #define _tfopen fopen
190 #define _fgetts fgets
191 #define _fputts fputs
192 #define _tcstol strtol
193 #define _tcstoul strtoul
194 #define _tcstoll strtoll
195 #define _tcstoull strtoull
196 #define _tcstod strtod
197 #define _tcsdup strdup
198 #define _tcsupr strupr
199 #define _tcsspn strspn
200 #define _tcscspn strcspn
201 #define _tcsstr strstr
202 #define _tcscat strcat
203 #define _topen open
204 #define _taccess access
205 #define _tstat stat
206 #define _tunlink unlink
207 #define _tcsftime strftime
208 #define _tctime ctime
209 #define _istspace isspace
210 #define _istdigit isdigit
211 #define _istalpha isalpha
212 #define _istupper isupper
213 #define _tgetenv getenv
214
215 #define _ERR_error_tstring ERR_error_string
216
217 #endif
218
219 #define CP_ACP 0
220 #define CP_UTF8 65001
221 #define MB_PRECOMPOSED 0x00000001
222 #define WC_COMPOSITECHECK 0x00000002
223 #define WC_DEFAULTCHAR 0x00000004
224
225 // Default codepage for iconv()
226 #if HAVE_ICONV_ISO_8859_1
227 #define ICONV_DEFAULT_CODEPAGE "ISO-8859-1"
228 #elif HAVE_ICONV_ISO8859_1
229 #define ICONV_DEFAULT_CODEPAGE "ISO8859-1"
230 #elif HAVE_ICONV_ASCII
231 #define ICONV_DEFAULT_CODEPAGE "ASCII"
232 #else
233 #define ICONV_DEFAULT_CODEPAGE ""
234 #endif
235
236 #endif /* _WIN32 */
237
238
239 #ifdef UNICODE
240 #define _t_inet_addr inet_addr_w
241 #else
242 #define _t_inet_addr inet_addr
243 #endif
244
245
246 // Check that either UNICODE_UCS2 or UNICODE_UCS4 are defined
247 #ifdef UNICODE
248 #if !defined(UNICODE_UCS2) && !defined(UNICODE_UCS4)
249 #error Neither UNICODE_UCS2 nor UNICODE_UCS4 are defined
250 #endif
251 #endif
252
253 #endif /* _unicode_h_ */
254