2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
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
17 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
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.
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.
46 #include "portability.h"
50 TLongExpFloat::TLongExpFloat(double d)
52 if (d != 0.0 && d != -0.0) {
56 m_base = da.getValue();
64 TLongExpFloat::operator*(const TLongExpFloat& b) const
66 double d = this->m_base * b.m_base;
67 TLongExpFloat reda(d);
68 reda.m_exp += this->m_exp + b.m_exp;
73 TLongExpFloat::operator/(const TLongExpFloat& b) const
75 double d = this->m_base / b.m_base;
76 TLongExpFloat reda(d);
77 reda.m_exp += (this->m_exp - b.m_exp);
82 TLongExpFloat::operator<(const TLongExpFloat& b) const
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)
95 TLongExpFloat::operator<=(const TLongExpFloat& b) const
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)
108 TLongExpFloat::operator==(const TLongExpFloat& b) const
110 return(m_base == b.m_base && m_exp == b.m_exp);
114 TLongExpFloat::operator=(const TLongExpFloat& b)
116 if (m_base == b.m_base && m_exp == b.m_exp)
126 TLongExpFloat::toString(std::string& str) const
137 * convert UTF-8 string pointed by s into UCS-4 Wide String at pwcs.
138 * No more than n wide char could be converted into target buffer.
139 * return -1 means error.
140 * other means the converted wide char number do not count the end 0.
143 MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n)
145 #ifndef WORDS_BIGENDIAN
146 static iconv_t ic = iconv_open("UCS-4LE", "UTF-8");
148 static iconv_t ic = iconv_open("UCS-4BE", "UTF-8");
151 assert(ic != (iconv_t)-1);
153 // To eliminate the const char* and char* diffirence in differnt system
154 TIConvSrcPtr src = (TIConvSrcPtr)s;
155 size_t srclen = std::strlen(s) + 1;
156 char* dst = (char*)pwcs;
157 size_t dstlen = n * sizeof(TWCHAR);
159 size_t res = iconv(ic, &src, &srclen, &dst, &dstlen);
161 if (res != size_t(-1) && srclen == 0) {
162 n -= dstlen / sizeof(TWCHAR);
163 return (n > 0) ? (n - 1) : 0;
170 * convert UCS-4 string pointed by pwcs into UTF-8 String at s.
171 * No more than n byte could be converted into target buffer.
172 * return -1 means error.
173 * Other means the converted byte number do not count the end 0.
176 WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n)
178 #ifndef WORDS_BIGENDIAN
179 static iconv_t ic = iconv_open("UTF-8", "UCS-4LE");
181 static iconv_t ic = iconv_open("UTF-8", "UCS-4BE");
184 assert(ic != (iconv_t)-1);
186 TIConvSrcPtr src = (TIConvSrcPtr)pwcs;
187 size_t srclen = (WCSLEN(pwcs) + 1) * sizeof(TWCHAR);
188 char* dst = (char*)s;
191 size_t res = iconv(ic, &src, &srclen, &dst, &dstlen);
193 if (res != size_t(-1) && srclen == 0) {
195 return (n > 0) ? (n - 1) : 0;
201 #else // !HAVE_ICONV_H
204 MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n)
206 const unsigned char *src = (const unsigned char*)s;
209 while (dst - pwcs < (ssize_t)n) {
210 if (*src < 0xc0 || *src >= 0xfe) {
211 if (*src < 0x80) *dst++ = *src;
212 if (*src++ == 0) break;
216 for (int bytes = 2; bytes <= 6; bytes++) {
217 if ((*src & ~(0xff >> (bytes + 1))) !=
218 (((1 << bytes) - 1) << (8 - bytes))) continue;
223 TWCHAR(*src++ & (0xff >> (bytes + 1))) << (6 * (bytes - 1));
224 for (; bytes-- > 1; src++) *dst |=
225 TWCHAR(*src & 0x3f) << (6 * (bytes - 1));
236 WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n)
240 while (dst - s < n) {
241 if (*pwcs < 0x80 || *pwcs > 0x10ffff) {
242 if (*pwcs < 0x80) *dst++ = *pwcs;
243 if (*pwcs++ == 0) break;
247 int bytes = *pwcs < 0x800 ? 2 : (*pwcs < 0xffff ? 3 : 4);
249 if (dst - s > n) return -1;
255 for (; nbyte > 0; c >>= 6, nbyte--)
258 1 ? (((1 << bytes) - 1) << (8 - bytes)) : 0x80) | (c & 0x3f);
264 #endif // HAVE_ICONV_H
267 * return the wide string len at pwcs, not count the end 0.
270 WCSLEN(const TWCHAR* pwcs)
280 #if !defined (HAVE_STRNDUP)
282 strndup(const char *s, size_t n)
291 nMost = std::min(strlen(s) + 1, n + 1);
293 nMost = min(strlen(s) + 1, n + 1);
295 p = (char*)malloc(nMost);
301 #endif //HAVE_STRNDUP