fixed Windows build errors
[public/netxms.git] / src / libnetxms / cc_ucs2.cpp
CommitLineData
7768852d
VK
1/*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2016 Raden Solutions
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: cc_ucs2.cpp
20 **
21 **/
22
23#include "libnetxms.h"
24#include "unicode_cc.h"
25
26/**
27 * Convert UCS-2 to UCS-4 - internal dumb method
28 */
29static int __internal_ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, UCS4CHAR *dst, int dstLen)
30{
31 int i, len;
32
33 len = (int) ((srcLen == -1) ? ucs2_strlen(src) : srcLen);
34 if (len > (int) dstLen - 1)
35 len = (int) dstLen - 1;
36 for(i = 0; i < len; i++)
37 dst[i] = (UCS4CHAR)src[i];
38 dst[i] = 0;
39 return len;
40}
41
42/**
43 * Convert UCS-2 to UCS-4
44 */
45int LIBNETXMS_EXPORTABLE ucs2_to_ucs4(const UCS2CHAR *src, int srcLen, UCS4CHAR *dst, int dstLen)
46{
669fcb92 47#if !defined(__DISABLE_ICONV) && !defined(_WIN32)
7768852d
VK
48 iconv_t cd;
49 const char *inbuf;
50 char *outbuf;
51 size_t count, inbytes, outbytes;
52
53 cd = IconvOpen(UCS4_CODEPAGE_NAME, UCS2_CODEPAGE_NAME);
54 if (cd == (iconv_t) (-1))
55 {
56 return __internal_ucs2_to_ucs4(src, srcLen, dst, dstLen);
57 }
58
59 inbuf = (const char *) src;
60 inbytes = ((srcLen == -1) ? ucs2_strlen(src) + 1 : (size_t) srcLen) * sizeof(UCS2CHAR);
61 outbuf = (char *) dst;
62 outbytes = (size_t) dstLen * sizeof(UCS4CHAR);
63 count = iconv(cd, (ICONV_CONST char **) &inbuf, &inbytes, &outbuf, &outbytes);
64 IconvClose(cd);
65
66 if (count == (size_t) - 1)
67 {
68 if (errno == EILSEQ)
69 {
70 count = (dstLen * sizeof(UCS4CHAR) - outbytes) / sizeof(UCS4CHAR);
71 }
72 else
73 {
74 count = 0;
75 }
76 }
77 else
78 {
79 count = (dstLen * sizeof(UCS4CHAR) - outbytes) / sizeof(UCS4CHAR);
80 }
81 if ((srcLen == -1) && (outbytes >= sizeof(UCS4CHAR)))
82 {
83 *((UCS4CHAR *) outbuf) = 0;
84 }
85
86 return (int)count;
87#else
88 return __internal_ucs2_to_ucs4(src, srcLen, dst, dstLen);
89#endif
90}
91
92#if !defined(_WIN32) && !defined(UNICODE_UCS2)
93
94/**
95 * Convert UCS-2 to UTF-8 using stub (no actual conversion for character codes above 0x007F)
96 */
97static int __internal_ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen)
98{
99 const UCS2CHAR *psrc;
100 char *pdst;
101 int pos, size;
102
103 size = (srcLen == -1) ? ucs2_strlen(src) : srcLen;
104 if (size >= dstLen)
105 size = dstLen - 1;
106 for(psrc = src, pos = 0, pdst = dst; pos < size; pos++, psrc++, pdst++)
107 *pdst = (*psrc < 128) ? (char) (*psrc) : '?';
108 *pdst = 0;
109 return size;
110}
111
112/**
113 * Convert UCS-2 to UTF-8
114 */
115int LIBNETXMS_EXPORTABLE ucs2_to_utf8(const UCS2CHAR *src, int srcLen, char *dst, int dstLen)
116{
117#if HAVE_ICONV && !defined(__DISABLE_ICONV)
118 iconv_t cd;
119 const char *inbuf;
120 char *outbuf;
121 size_t count, inbytes, outbytes;
122
123 cd = IconvOpen("UTF-8", UCS2_CODEPAGE_NAME);
124 if (cd == (iconv_t) (-1))
125 {
126 return __internal_ucs2_to_utf8(src, srcLen, dst, dstLen);
127 }
128
129 inbuf = (const char *) src;
130 inbytes = ((srcLen == -1) ? ucs2_strlen(src) + 1 : (size_t) srcLen) * sizeof(UCS2CHAR);
131 outbuf = (char *) dst;
132 outbytes = (size_t) dstLen;
133 count = iconv(cd, (ICONV_CONST char **) &inbuf, &inbytes, &outbuf, &outbytes);
134 IconvClose(cd);
135
136 if (count == (size_t) - 1)
137 {
138 if (errno == EILSEQ)
139 {
140 count = (dstLen * sizeof(char) - outbytes) / sizeof(char);
141 }
142 else
143 {
144 count = 0;
145 }
146 }
147 else
148 {
149 count = dstLen - outbytes;
150 }
151 if ((srcLen == -1) && (outbytes >= sizeof(char)))
152 {
153 *((char *) outbuf) = 0;
154 }
155
156 return (int)count;
157#else
158 return __internal_ucs2_to_utf8(src, srcLen, dst, dstLen);
159#endif
160}
161
162/**
163 * Convert UCS-2 to multibyte using stub (no actual conversion for character codes above 0x007F)
164 */
165static int __internal_ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen)
166{
167 const UCS2CHAR *psrc;
168 char *pdst;
169 int pos, size;
170
171 size = (srcLen == -1) ? (int) ucs2_strlen(src) : srcLen;
172 if (size >= dstLen)
173 size = dstLen - 1;
174
175 for(psrc = src, pos = 0, pdst = dst; pos < size; pos++, psrc++, pdst++)
176 *pdst = (*psrc < 256) ? (char) (*psrc) : '?';
177 *pdst = 0;
178
179 return size;
180}
181
182/**
183 * Convert UCS-2 to multibyte
184 */
185int LIBNETXMS_EXPORTABLE ucs2_to_mb(const UCS2CHAR *src, int srcLen, char *dst, int dstLen)
186{
187#if HAVE_ICONV && !defined(__DISABLE_ICONV)
188 iconv_t cd;
189 const char *inbuf;
190 char *outbuf;
191 size_t count, inbytes, outbytes;
192
193 cd = IconvOpen(g_cpDefault, UCS2_CODEPAGE_NAME);
194 if (cd == (iconv_t) (-1))
195 {
196 return __internal_ucs2_to_mb(src, srcLen, dst, dstLen);
197 }
198
199 inbuf = (const char *) src;
200 inbytes = ((srcLen == -1) ? ucs2_strlen(src) + 1 : (size_t) srcLen) * sizeof(UCS2CHAR);
201 outbuf = (char *) dst;
202 outbytes = (size_t) dstLen;
203 count = iconv(cd, (ICONV_CONST char **) &inbuf, &inbytes, &outbuf, &outbytes);
204 IconvClose(cd);
205
206 if (count == (size_t) - 1)
207 {
208 if (errno == EILSEQ)
209 {
210 count = (dstLen * sizeof(char) - outbytes) / sizeof(char);
211 }
212 else
213 {
214 count = 0;
215 }
216 }
217 if ((srcLen == -1) && (outbytes >= sizeof(char)))
218 {
219 *((char *) outbuf) = 0;
220 }
221
222 return (int)count;
223#else
224 return __internal_ucs2_to_mb(src, srcLen, dst, dstLen);
225#endif
226}
227
228#endif