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.
130
size_t MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n)
132
#ifndef WORDS_BIGENDIAN
133
static iconv_t ic = iconv_open("UCS-4LE", "UTF-8");
135
static iconv_t ic = iconv_open("UCS-4BE", "UTF-8");
138
assert(ic != (iconv_t)-1);
140
// To eliminate the const char* and char* diffirence in differnt system
141
TIConvSrcPtr src = (TIConvSrcPtr)s;
142
size_t srclen = std::strlen(s)+1;
143
char* dst = (char *)pwcs;
144
size_t dstlen = n*sizeof(TWCHAR);
146
size_t res = iconv(ic, &src, &srclen, &dst, &dstlen);
148
if (res != size_t(-1) && srclen == 0) {
149
n -= dstlen/sizeof(TWCHAR);
150
return (n > 0)?(n-1):0;
157
* convert UCS-4 string pointed by pwcs into UTF-8 String at s.
158
* No more than n byte could be converted into target buffer.
159
* return -1 means error.
160
* Other means the converted byte number do not count the end 0.
162
size_t WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n)
164
#ifndef WORDS_BIGENDIAN
165
static iconv_t ic = iconv_open("UTF-8", "UCS-4LE");
167
static iconv_t ic = iconv_open("UTF-8", "UCS-4BE");
170
assert(ic != (iconv_t)-1);
172
TIConvSrcPtr src = (TIConvSrcPtr)pwcs;
173
size_t srclen = (WCSLEN(pwcs)+1)*sizeof(TWCHAR);
174
char* dst = (char *)s;
177
size_t res = iconv(ic, &src, &srclen, &dst, &dstlen);
179
if (res != size_t(-1) && srclen == 0) {
181
return (n > 0)?(n-1):0;
187
#else // !HAVE_ICONV_H
189
size_t MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n)
191
const unsigned char *src = (const unsigned char*)s;
194
while (dst - pwcs < n) {
195
if (*src < 0xc0 || *src >= 0xfe) {
196
if(*src < 0x80) *dst++ = *src;
197
if(*src++ == 0) break;
201
for (int bytes = 2; bytes <= 6; bytes++) {
202
if ((*src & ~(0xff >> (bytes + 1))) != (((1 << bytes) - 1) << (8 - bytes))) continue;
206
*dst = TWCHAR(*src++ & (0xff >> (bytes + 1))) << (6 * (bytes - 1));
207
for (; bytes-- > 1; src++) *dst |= TWCHAR(*src & 0x3f) << (6 * (bytes - 1));
217
size_t WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n)
221
while (dst - s < n) {
222
if (*pwcs < 0x80 || *pwcs > 0x10ffff) {
223
if (*pwcs < 0x80) *dst++ = *pwcs;
224
if (*pwcs++ == 0) break;
228
int bytes = *pwcs < 0x800 ? 2 : (*pwcs < 0xffff ? 3 : 4);
230
if (dst - s > n) return -1;
236
for (; nbyte > 0; c >>= 6, nbyte--)
237
*tmp-- = (nbyte == 1 ? (((1 << bytes) - 1) << (8 - bytes)) : 0x80) | (c & 0x3f);
243
#endif // HAVE_ICONV_H
246
* return the wide string len at pwcs, not count the end 0.
248
size_t WCSLEN(const TWCHAR* pwcs)
258
#if !defined (HAVE_STRNDUP)
259
extern "C" char *strndup( const char *s, size_t n )
268
nMost = std::min( strlen(s) + 1, n + 1 );
270
nMost = min( strlen(s) + 1, n + 1 );
272
p = (char*)malloc( nMost );
273
memcpy( p, s, nMost );
278
#endif //HAVE_STRNDUP