~ubuntu-branches/ubuntu/trusty/sunpinyin/trusty-proposed

« back to all changes in this revision

Viewing changes to src/portability.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Zhengpeng Hou
  • Date: 2010-09-06 12:23:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100906122346-yamofztk2j5p85fs
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n)
 
131
{
 
132
#ifndef WORDS_BIGENDIAN
 
133
    static iconv_t ic = iconv_open("UCS-4LE", "UTF-8");
 
134
#else
 
135
    static iconv_t ic = iconv_open("UCS-4BE", "UTF-8");
 
136
#endif
 
137
 
 
138
    assert(ic != (iconv_t)-1);
 
139
 
 
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);
 
145
 
 
146
    size_t res = iconv(ic, &src, &srclen, &dst, &dstlen);
 
147
 
 
148
    if (res != size_t(-1) && srclen == 0) {
 
149
        n -= dstlen/sizeof(TWCHAR);
 
150
        return (n > 0)?(n-1):0;
 
151
    } else {
 
152
        return size_t(-1);
 
153
    }
 
154
}
 
155
 
 
156
/**
 
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.
 
161
*/
 
162
size_t WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n)
 
163
{
 
164
#ifndef WORDS_BIGENDIAN
 
165
    static iconv_t ic = iconv_open("UTF-8", "UCS-4LE");
 
166
#else
 
167
    static iconv_t ic = iconv_open("UTF-8", "UCS-4BE");
 
168
#endif
 
169
 
 
170
    assert(ic != (iconv_t)-1);
 
171
 
 
172
    TIConvSrcPtr src = (TIConvSrcPtr)pwcs;
 
173
    size_t srclen = (WCSLEN(pwcs)+1)*sizeof(TWCHAR);
 
174
    char* dst = (char *)s;
 
175
    size_t dstlen = n;
 
176
 
 
177
    size_t res = iconv(ic, &src, &srclen, &dst, &dstlen);
 
178
 
 
179
    if (res != size_t(-1) && srclen == 0) {
 
180
        n -= dstlen;
 
181
        return (n > 0)?(n-1):0;
 
182
    } else {
 
183
        return size_t(-1);
 
184
    }
 
185
}
 
186
 
 
187
#else // !HAVE_ICONV_H
 
188
 
 
189
size_t MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n)
 
190
{
 
191
    const unsigned char *src = (const unsigned char*)s;
 
192
    TWCHAR* dst = pwcs;
 
193
 
 
194
    while (dst - pwcs < n) {
 
195
        if (*src < 0xc0 || *src >= 0xfe) {
 
196
            if(*src < 0x80) *dst++ = *src;
 
197
            if(*src++ == 0) break;
 
198
            continue;
 
199
        }
 
200
 
 
201
        for (int bytes = 2; bytes <= 6; bytes++) {
 
202
            if ((*src & ~(0xff >> (bytes + 1))) != (((1 << bytes) - 1) << (8 - bytes))) continue;
 
203
            if (bytes > 4) {
 
204
                src += bytes;
 
205
            } else {
 
206
                *dst = TWCHAR(*src++ & (0xff >> (bytes + 1))) << (6 * (bytes - 1));
 
207
                for (; bytes-- > 1; src++) *dst |= TWCHAR(*src & 0x3f) << (6 * (bytes - 1));
 
208
                dst++;
 
209
            }
 
210
            break;
 
211
        }
 
212
    }
 
213
 
 
214
    return (dst - pwcs);
 
215
}
 
216
 
 
217
size_t WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n)
 
218
{
 
219
    char* dst = s;
 
220
 
 
221
    while (dst - s < n) {
 
222
        if (*pwcs < 0x80 || *pwcs > 0x10ffff) {
 
223
            if (*pwcs < 0x80) *dst++ = *pwcs;
 
224
            if (*pwcs++ == 0) break;
 
225
            continue;
 
226
        }
 
227
 
 
228
        int bytes = *pwcs < 0x800 ? 2 : (*pwcs < 0xffff ? 3 : 4);
 
229
        dst += bytes;
 
230
        if (dst - s > n) return -1;
 
231
 
 
232
        TWCHAR c = *pwcs++;
 
233
        int nbyte = bytes;
 
234
        char *tmp = dst - 1;
 
235
 
 
236
        for (; nbyte > 0; c >>= 6, nbyte--)
 
237
                *tmp-- = (nbyte == 1 ? (((1 << bytes) - 1) << (8 - bytes)) : 0x80) | (c & 0x3f);
 
238
    }
 
239
 
 
240
    return (dst - s);
 
241
}
 
242
 
 
243
#endif // HAVE_ICONV_H
 
244
 
 
245
/**
 
246
* return the wide string len at pwcs, not count the end 0.
 
247
*/
 
248
size_t WCSLEN(const TWCHAR* pwcs)
 
249
{
 
250
    size_t sz = 0;
 
251
    if (pwcs) {
 
252
        while (*pwcs++)
 
253
            ++sz;
 
254
    }
 
255
    return sz;
 
256
}
 
257
 
 
258
#if !defined (HAVE_STRNDUP)
 
259
extern "C" char *strndup( const char *s, size_t n )
 
260
{
 
261
    size_t nMost;
 
262
    char *p = NULL;                                                                          
 
263
                                                                  
 
264
    if ( !s )
 
265
        return NULL;
 
266
  
 
267
#ifdef __cplusplus                                                                                                                                       
 
268
    nMost = std::min( strlen(s) + 1, n + 1 );
 
269
#else
 
270
    nMost = min( strlen(s) + 1, n + 1 );
 
271
#endif
 
272
    p     = (char*)malloc( nMost );
 
273
    memcpy( p, s, nMost );
 
274
    p[nMost - 1] = '\0';
 
275
                                                                                                                                                
 
276
    return p;
 
277
}
 
278
#endif //HAVE_STRNDUP
 
279