~ubuntu-branches/ubuntu/raring/sunpinyin/raring

« back to all changes in this revision

Viewing changes to src/portability.cpp

  • Committer: Package Import Robot
  • Author(s): YunQiang Su
  • Date: 2012-03-30 15:31:55 UTC
  • mfrom: (1.1.3) (1.2.7 sid)
  • Revision ID: package-import@ubuntu.com-20120330153155-qgls77sogzgtg9zp
Tags: 2.0.3+git20120222-1
* Team upload: git snapshot 20120222.
   - fix breaks if LDFLAGS in environment contains
       multiple words (Closese #646001).
   - rm patches merged to upstream:
       append-os-environ-toenv.patch
       fix-ftbfs-on-sh.patch
       remove-10-candidate-words-limitation.patch
   - refresh disable-lm-dict-compile.patch.
* Bump stardard version to 3.9.3: no modify needed.
* add libsunpinyin3-dbg and python-sunpinyin packages.
* debian/compat to 9, multiarch it.
* rewrite debian/rules with dh 7 format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 
 * 
 
3
 *
4
4
 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
5
 
 * 
 
5
 *
6
6
 * The contents of this file are subject to the terms of either the GNU Lesser
7
7
 * General Public License Version 2.1 only ("LGPL") or the Common Development and
8
8
 * Distribution License ("CDDL")(collectively, the "License"). You may not use this
9
9
 * file except in compliance with the License. You can obtain a copy of the CDDL at
10
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 
 
11
 * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
12
12
 * specific language governing permissions and limitations under the License. When
13
13
 * distributing the software, include this License Header Notice in each file and
14
14
 * include the full text of the License in the License file as well as the
15
15
 * following notice:
16
 
 * 
 
16
 *
17
17
 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
18
18
 * (CDDL)
19
19
 * For Covered Software in this distribution, this License shall be governed by the
21
21
 * Any litigation relating to this License shall be subject to the jurisdiction of
22
22
 * the Federal Courts of the Northern District of California and the state courts
23
23
 * of the State of California, with venue lying in Santa Clara County, California.
24
 
 * 
 
24
 *
25
25
 * Contributor(s):
26
 
 * 
 
26
 *
27
27
 * If you wish your version of this file to be governed by only the CDDL or only
28
28
 * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
29
29
 * include this software in this distribution under the [CDDL or LGPL Version 2.1]
32
32
 * Version 2.1, or to extend the choice of license to its licensees as provided
33
33
 * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
34
34
 * Version 2 license, then the option applies only if the new code is made subject
35
 
 * to such option by the copyright holder. 
 
35
 * to such option by the copyright holder.
36
36
 */
37
37
 
38
38
#ifdef HAVE_CONFIG_H
61
61
}
62
62
 
63
63
TLongExpFloat
64
 
TLongExpFloat::operator* (const TLongExpFloat& b) const
 
64
TLongExpFloat::operator*(const TLongExpFloat& b) const
65
65
{
66
66
    double d = this->m_base * b.m_base;
67
67
    TLongExpFloat reda(d);
70
70
}
71
71
 
72
72
TLongExpFloat
73
 
TLongExpFloat::operator/ (const TLongExpFloat& b) const
 
73
TLongExpFloat::operator/(const TLongExpFloat& b) const
74
74
{
75
75
    double d = this->m_base / b.m_base;
76
76
    TLongExpFloat reda(d);
79
79
}
80
80
 
81
81
bool
82
 
TLongExpFloat::operator< (const TLongExpFloat& b) const
 
82
TLongExpFloat::operator<(const TLongExpFloat& b) const
83
83
{
84
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));
 
85
        return(m_exp < b.m_exp || (m_exp == b.m_exp && m_base < b.m_base));
86
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));
 
87
        return(m_exp > b.m_exp || (m_exp == b.m_exp && m_base < b.m_base));
88
88
    } else if (m_base < 0.0 && b.m_base >= 0.0)
89
89
        return true;
90
90
    else
95
95
TLongExpFloat::operator<=(const TLongExpFloat& b) const
96
96
{
97
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));
 
98
        return(m_exp < b.m_exp || (m_exp == b.m_exp && m_base <= b.m_base));
99
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));
 
100
        return(m_exp > b.m_exp || (m_exp == b.m_exp && m_base <= b.m_base));
101
101
    } else if (m_base < 0.0 && b.m_base >= 0.0)
102
102
        return true;
103
103
    else
107
107
bool
108
108
TLongExpFloat::operator==(const TLongExpFloat& b) const
109
109
{
110
 
    return (m_base == b.m_base && m_exp == b.m_exp);
 
110
    return(m_base == b.m_base && m_exp == b.m_exp);
111
111
}
112
112
 
113
113
void
122
122
#include <iconv.h>
123
123
 
124
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)
 
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)
131
132
{
132
133
#ifndef WORDS_BIGENDIAN
133
134
    static iconv_t ic = iconv_open("UCS-4LE", "UTF-8");
139
140
 
140
141
    // To eliminate the const char* and char* diffirence in differnt system
141
142
    TIConvSrcPtr src = (TIConvSrcPtr)s;
142
 
    size_t srclen = std::strlen(s)+1;
143
 
    char* dst = (char *)pwcs;
144
 
    size_t dstlen = n*sizeof(TWCHAR);
 
143
    size_t srclen = std::strlen(s) + 1;
 
144
    char* dst = (char*)pwcs;
 
145
    size_t dstlen = n * sizeof(TWCHAR);
145
146
 
146
147
    size_t res = iconv(ic, &src, &srclen, &dst, &dstlen);
147
148
 
148
149
    if (res != size_t(-1) && srclen == 0) {
149
 
        n -= dstlen/sizeof(TWCHAR);
150
 
        return (n > 0)?(n-1):0;
 
150
        n -= dstlen / sizeof(TWCHAR);
 
151
        return (n > 0) ? (n - 1) : 0;
151
152
    } else {
152
153
        return size_t(-1);
153
154
    }
154
155
}
155
156
 
156
157
/**
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)
 
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)
163
165
{
164
166
#ifndef WORDS_BIGENDIAN
165
167
    static iconv_t ic = iconv_open("UTF-8", "UCS-4LE");
170
172
    assert(ic != (iconv_t)-1);
171
173
 
172
174
    TIConvSrcPtr src = (TIConvSrcPtr)pwcs;
173
 
    size_t srclen = (WCSLEN(pwcs)+1)*sizeof(TWCHAR);
174
 
    char* dst = (char *)s;
 
175
    size_t srclen = (WCSLEN(pwcs) + 1) * sizeof(TWCHAR);
 
176
    char* dst = (char*)s;
175
177
    size_t dstlen = n;
176
178
 
177
179
    size_t res = iconv(ic, &src, &srclen, &dst, &dstlen);
178
180
 
179
181
    if (res != size_t(-1) && srclen == 0) {
180
182
        n -= dstlen;
181
 
        return (n > 0)?(n-1):0;
 
183
        return (n > 0) ? (n - 1) : 0;
182
184
    } else {
183
185
        return size_t(-1);
184
186
    }
186
188
 
187
189
#else // !HAVE_ICONV_H
188
190
 
189
 
size_t MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n)
 
191
size_t
 
192
MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n)
190
193
{
191
194
    const unsigned char *src = (const unsigned char*)s;
192
195
    TWCHAR* dst = pwcs;
193
196
 
194
197
    while (dst - pwcs < n) {
195
198
        if (*src < 0xc0 || *src >= 0xfe) {
196
 
            if(*src < 0x80) *dst++ = *src;
197
 
            if(*src++ == 0) break;
 
199
            if (*src < 0x80) *dst++ = *src;
 
200
            if (*src++ == 0) break;
198
201
            continue;
199
202
        }
200
203
 
201
204
        for (int bytes = 2; bytes <= 6; bytes++) {
202
 
            if ((*src & ~(0xff >> (bytes + 1))) != (((1 << bytes) - 1) << (8 - bytes))) continue;
 
205
            if ((*src & ~(0xff >> (bytes + 1))) !=
 
206
                (((1 << bytes) - 1) << (8 - bytes))) continue;
203
207
            if (bytes > 4) {
204
208
                src += bytes;
205
209
            } else {
206
 
                *dst = TWCHAR(*src++ & (0xff >> (bytes + 1))) << (6 * (bytes - 1));
207
 
                for (; bytes-- > 1; src++) *dst |= TWCHAR(*src & 0x3f) << (6 * (bytes - 1));
 
210
                *dst =
 
211
                    TWCHAR(*src++ & (0xff >> (bytes + 1))) << (6 * (bytes - 1));
 
212
                for (; bytes-- > 1; src++) *dst |=
 
213
                        TWCHAR(*src & 0x3f) << (6 * (bytes - 1));
208
214
                dst++;
209
215
            }
210
216
            break;
211
217
        }
212
218
    }
213
219
 
214
 
    return (dst - pwcs);
 
220
    return(dst - pwcs);
215
221
}
216
222
 
217
 
size_t WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n)
 
223
size_t
 
224
WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n)
218
225
{
219
226
    char* dst = s;
220
227
 
234
241
        char *tmp = dst - 1;
235
242
 
236
243
        for (; nbyte > 0; c >>= 6, nbyte--)
237
 
                *tmp-- = (nbyte == 1 ? (((1 << bytes) - 1) << (8 - bytes)) : 0x80) | (c & 0x3f);
 
244
            *tmp-- =
 
245
                (nbyte ==
 
246
                 1 ? (((1 << bytes) - 1) << (8 - bytes)) : 0x80) | (c & 0x3f);
238
247
    }
239
248
 
240
 
    return (dst - s);
 
249
    return(dst - s);
241
250
}
242
251
 
243
252
#endif // HAVE_ICONV_H
244
253
 
245
254
/**
246
 
* return the wide string len at pwcs, not count the end 0.
247
 
*/
248
 
size_t WCSLEN(const TWCHAR* pwcs)
 
255
 * return the wide string len at pwcs, not count the end 0.
 
256
 */
 
257
size_t
 
258
WCSLEN(const TWCHAR* pwcs)
249
259
{
250
260
    size_t sz = 0;
251
261
    if (pwcs) {
255
265
    return sz;
256
266
}
257
267
 
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
268