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::toString(std::string& str) const
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.
131 MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n)
133 #ifndef WORDS_BIGENDIAN
134 static iconv_t ic = iconv_open("UCS-4LE", "UTF-8");
136 static iconv_t ic = iconv_open("UCS-4BE", "UTF-8");
139 assert(ic != (iconv_t)-1);
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);
147 size_t res = iconv(ic, &src, &srclen, &dst, &dstlen);
149 if (res != size_t(-1) && srclen == 0) {
150 n -= dstlen / sizeof(TWCHAR);
151 return (n > 0) ? (n - 1) : 0;
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.
164 WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n)
166 #ifndef WORDS_BIGENDIAN
167 static iconv_t ic = iconv_open("UTF-8", "UCS-4LE");
169 static iconv_t ic = iconv_open("UTF-8", "UCS-4BE");
172 assert(ic != (iconv_t)-1);
174 TIConvSrcPtr src = (TIConvSrcPtr)pwcs;
175 size_t srclen = (WCSLEN(pwcs) + 1) * sizeof(TWCHAR);
176 char* dst = (char*)s;
179 size_t res = iconv(ic, &src, &srclen, &dst, &dstlen);
181 if (res != size_t(-1) && srclen == 0) {
183 return (n > 0) ? (n - 1) : 0;
189 #else // !HAVE_ICONV_H
192 MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n)
194 const unsigned char *src = (const unsigned char*)s;
197 while (dst - pwcs < n) {
198 if (*src < 0xc0 || *src >= 0xfe) {
199 if (*src < 0x80) *dst++ = *src;
200 if (*src++ == 0) break;
204 for (int bytes = 2; bytes <= 6; bytes++) {
205 if ((*src & ~(0xff >> (bytes + 1))) !=
206 (((1 << bytes) - 1) << (8 - bytes))) continue;
211 TWCHAR(*src++ & (0xff >> (bytes + 1))) << (6 * (bytes - 1));
212 for (; bytes-- > 1; src++) *dst |=
213 TWCHAR(*src & 0x3f) << (6 * (bytes - 1));
224 WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n)
228 while (dst - s < n) {
229 if (*pwcs < 0x80 || *pwcs > 0x10ffff) {
230 if (*pwcs < 0x80) *dst++ = *pwcs;
231 if (*pwcs++ == 0) break;
235 int bytes = *pwcs < 0x800 ? 2 : (*pwcs < 0xffff ? 3 : 4);
237 if (dst - s > n) return -1;
243 for (; nbyte > 0; c >>= 6, nbyte--)
246 1 ? (((1 << bytes) - 1) << (8 - bytes)) : 0x80) | (c & 0x3f);
252 #endif // HAVE_ICONV_H
255 * return the wide string len at pwcs, not count the end 0.
258 WCSLEN(const TWCHAR* pwcs)
268 #if !defined (HAVE_STRNDUP)
270 strndup(const char *s, size_t n)
279 nMost = std::min(strlen(s) + 1, n + 1);
281 nMost = min(strlen(s) + 1, n + 1);
283 p = (char*)malloc(nMost);
289 #endif //HAVE_STRNDUP