ea3b4fbbc5f1121161858c75d980449506e7d7e7
[platform/core/uifw/ise-engine-sunpinyin.git] / src / portability.cpp
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * The contents of this file are subject to the terms of either the GNU Lesser
7  * General Public License Version 2.1 only ("LGPL") or the Common Development and
8  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
9  * file except in compliance with the License. You can obtain a copy of the CDDL at
10  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
11  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
12  * specific language governing permissions and limitations under the License. When
13  * distributing the software, include this License Header Notice in each file and
14  * include the full text of the License in the License file as well as the
15  * following notice:
16  *
17  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
18  * (CDDL)
19  * For Covered Software in this distribution, this License shall be governed by the
20  * laws of the State of California (excluding conflict-of-law provisions).
21  * Any litigation relating to this License shall be subject to the jurisdiction of
22  * the Federal Courts of the Northern District of California and the state courts
23  * of the State of California, with venue lying in Santa Clara County, California.
24  *
25  * Contributor(s):
26  *
27  * If you wish your version of this file to be governed by only the CDDL or only
28  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
29  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
30  * license." If you don't indicate a single choice of license, a recipient has the
31  * option to distribute your version of this file under either the CDDL or the LGPL
32  * Version 2.1, or to extend the choice of license to its licensees as provided
33  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
34  * Version 2 license, then the option applies only if the new code is made subject
35  * to such option by the copyright holder.
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #ifdef HAVE_ASSERT_H
43 #include <assert.h>
44 #endif
45
46 #include "portability.h"
47
48 #include <stdlib.h>
49
50 TLongExpFloat::TLongExpFloat(double d)
51 {
52     if (d != 0.0 && d != -0.0) {
53         TDoubleAnatomy da(d);
54         m_exp = da.getExp();
55         da.clearExp();
56         m_base = da.getValue();
57     } else {
58         m_base = d;
59         m_exp = 0;
60     }
61 }
62
63 TLongExpFloat
64 TLongExpFloat::operator*(const TLongExpFloat& b) const
65 {
66     double d = this->m_base * b.m_base;
67     TLongExpFloat reda(d);
68     reda.m_exp += this->m_exp + b.m_exp;
69     return reda;
70 }
71
72 TLongExpFloat
73 TLongExpFloat::operator/(const TLongExpFloat& b) const
74 {
75     double d = this->m_base / b.m_base;
76     TLongExpFloat reda(d);
77     reda.m_exp += (this->m_exp - b.m_exp);
78     return reda;
79 }
80
81 bool
82 TLongExpFloat::operator<(const TLongExpFloat& b) const
83 {
84     if (m_base >= 0.0 && b.m_base >= 0.0) {
85         return(m_exp < b.m_exp || (m_exp == b.m_exp && m_base < b.m_base));
86     } else if (m_base < 0.0 && b.m_base < 0.0) {
87         return(m_exp > b.m_exp || (m_exp == b.m_exp && m_base < b.m_base));
88     } else if (m_base < 0.0 && b.m_base >= 0.0)
89         return true;
90     else
91         return false;
92 }
93
94 bool
95 TLongExpFloat::operator<=(const TLongExpFloat& b) const
96 {
97     if (m_base >= 0.0 && b.m_base >= 0.0) {
98         return(m_exp < b.m_exp || (m_exp == b.m_exp && m_base <= b.m_base));
99     } else if (m_base < 0.0 && b.m_base < 0.0) {
100         return(m_exp > b.m_exp || (m_exp == b.m_exp && m_base <= b.m_base));
101     } else if (m_base < 0.0 && b.m_base >= 0.0)
102         return true;
103     else
104         return false;
105 }
106
107 bool
108 TLongExpFloat::operator==(const TLongExpFloat& b) const
109 {
110     return(m_base == b.m_base && m_exp == b.m_exp);
111 }
112
113 void
114 TLongExpFloat::toString(std::string& str) const
115 {
116     char buf[256];
117     toString(buf);
118     str = buf;
119 }
120
121 #ifdef HAVE_ICONV_H
122 #include <iconv.h>
123
124 /**
125  * convert UTF-8 string pointed by s into UCS-4 Wide String at pwcs.
126  * No more than n wide char could be converted into target buffer.
127  * return -1 means error.
128  * other means the converted wide char number do not count the end 0.
129  */
130 size_t
131 MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n)
132 {
133 #ifndef WORDS_BIGENDIAN
134     static iconv_t ic = iconv_open("UCS-4LE", "UTF-8");
135 #else
136     static iconv_t ic = iconv_open("UCS-4BE", "UTF-8");
137 #endif
138
139     assert(ic != (iconv_t)-1);
140
141     // To eliminate the const char* and char* diffirence in differnt system
142     TIConvSrcPtr src = (TIConvSrcPtr)s;
143     size_t srclen = std::strlen(s) + 1;
144     char* dst = (char*)pwcs;
145     size_t dstlen = n * sizeof(TWCHAR);
146
147     size_t res = iconv(ic, &src, &srclen, &dst, &dstlen);
148
149     if (res != size_t(-1) && srclen == 0) {
150         n -= dstlen / sizeof(TWCHAR);
151         return (n > 0) ? (n - 1) : 0;
152     } else {
153         return size_t(-1);
154     }
155 }
156
157 /**
158  * convert UCS-4 string pointed by pwcs into UTF-8 String at s.
159  * No more than n byte could be converted into target buffer.
160  * return -1 means error.
161  * Other means the converted byte number do not count the end 0.
162  */
163 size_t
164 WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n)
165 {
166 #ifndef WORDS_BIGENDIAN
167     static iconv_t ic = iconv_open("UTF-8", "UCS-4LE");
168 #else
169     static iconv_t ic = iconv_open("UTF-8", "UCS-4BE");
170 #endif
171
172     assert(ic != (iconv_t)-1);
173
174     TIConvSrcPtr src = (TIConvSrcPtr)pwcs;
175     size_t srclen = (WCSLEN(pwcs) + 1) * sizeof(TWCHAR);
176     char* dst = (char*)s;
177     size_t dstlen = n;
178
179     size_t res = iconv(ic, &src, &srclen, &dst, &dstlen);
180
181     if (res != size_t(-1) && srclen == 0) {
182         n -= dstlen;
183         return (n > 0) ? (n - 1) : 0;
184     } else {
185         return size_t(-1);
186     }
187 }
188
189 #else // !HAVE_ICONV_H
190
191 size_t
192 MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n)
193 {
194     const unsigned char *src = (const unsigned char*)s;
195     TWCHAR* dst = pwcs;
196
197     while (dst - pwcs < (ssize_t)n) {
198         if (*src < 0xc0 || *src >= 0xfe) {
199             if (*src < 0x80) *dst++ = *src;
200             if (*src++ == 0) break;
201             continue;
202         }
203
204         for (int bytes = 2; bytes <= 6; bytes++) {
205             if ((*src & ~(0xff >> (bytes + 1))) !=
206                 (((1 << bytes) - 1) << (8 - bytes))) continue;
207             if (bytes > 4) {
208                 src += bytes;
209             } else {
210                 *dst =
211                     TWCHAR(*src++ & (0xff >> (bytes + 1))) << (6 * (bytes - 1));
212                 for (; bytes-- > 1; src++) *dst |=
213                         TWCHAR(*src & 0x3f) << (6 * (bytes - 1));
214                 dst++;
215             }
216             break;
217         }
218     }
219
220     return(dst - pwcs);
221 }
222
223 size_t
224 WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n)
225 {
226     char* dst = s;
227
228     while (dst - s < n) {
229         if (*pwcs < 0x80 || *pwcs > 0x10ffff) {
230             if (*pwcs < 0x80) *dst++ = *pwcs;
231             if (*pwcs++ == 0) break;
232             continue;
233         }
234
235         int bytes = *pwcs < 0x800 ? 2 : (*pwcs < 0xffff ? 3 : 4);
236         dst += bytes;
237         if (dst - s > n) return -1;
238
239         TWCHAR c = *pwcs++;
240         int nbyte = bytes;
241         char *tmp = dst - 1;
242
243         for (; nbyte > 0; c >>= 6, nbyte--)
244             *tmp-- =
245                 (nbyte ==
246                  1 ? (((1 << bytes) - 1) << (8 - bytes)) : 0x80) | (c & 0x3f);
247     }
248
249     return(dst - s);
250 }
251
252 #endif // HAVE_ICONV_H
253
254 /**
255  * return the wide string len at pwcs, not count the end 0.
256  */
257 size_t
258 WCSLEN(const TWCHAR* pwcs)
259 {
260     size_t sz = 0;
261     if (pwcs) {
262         while (*pwcs++)
263             ++sz;
264     }
265     return sz;
266 }
267
268 #if !defined (HAVE_STRNDUP)
269 extern "C" char *
270 strndup(const char *s, size_t n)
271 {
272     size_t nMost;
273     char *p = NULL;
274
275     if (!s)
276         return NULL;
277
278 #ifdef __cplusplus
279     nMost = std::min(strlen(s) + 1, n + 1);
280 #else
281     nMost = min(strlen(s) + 1, n + 1);
282 #endif
283     p = (char*)malloc(nMost);
284     memcpy(p, s, nMost);
285     p[nMost - 1] = '\0';
286
287     return p;
288 }
289 #endif //HAVE_STRNDUP
290