~ubuntu-branches/ubuntu/quantal/sunpinyin/quantal

« back to all changes in this revision

Viewing changes to src/portability.h

  • 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
#ifndef ___SUN_SLM_PORTABILITY_H___
40
40
 
41
41
#include <stdio.h>
42
42
#include <math.h>
 
43
#include <stdint.h>
43
44
#include <string>
44
45
#include <cstring>
45
46
 
61
62
    #endif
62
63
#else // HOST_OS_GNUC_2
63
64
    #if defined(DEBUG) && !defined(NDEBUG)
64
 
        #define DEBUG_print(fmt, a...)   fprintf(stderr, fmt, a...)
 
65
        #define DEBUG_print(fmt, a ...)   fprintf(stderr, fmt, a ...)
65
66
    #else
66
 
        #define DEBUG_print(fmt, a...)   (int(0))
 
67
        #define DEBUG_print(fmt, a ...)   (int(0))
67
68
    #endif
68
69
#endif // !HOST_OS_GNUC_2
69
70
 
79
80
 
80
81
union TDoubleAnatomy {
81
82
public:
82
 
    TDoubleAnatomy(double d) : m_value(d)
83
 
    { }
 
83
    TDoubleAnatomy(double d) : m_value(d){
 
84
    }
84
85
 
85
 
    int
86
 
    getExp(void) const
 
86
    int getExp(void) const
87
87
    { return anony.m_exp - 0x3FF; }
88
88
 
89
 
    double
90
 
    getValue(void) const
 
89
    double getValue(void) const
91
90
    { return m_value; }
92
91
 
93
 
    void
94
 
    clearExp(void)
 
92
    void clearExp(void)
95
93
    { anony.m_exp = 0x3FF; }
96
94
 
97
95
public:
98
 
    double  m_value;
 
96
    double m_value;
99
97
    struct TAnony {
100
98
#ifndef WORDS_BIGENDIAN
101
 
        unsigned    m_other2 : 32;
102
 
        unsigned    m_other1 : 20;
103
 
        unsigned    m_exp    : 11;
104
 
        unsigned    m_neg    : 1;
 
99
        unsigned m_other2 : 32;
 
100
        unsigned m_other1 : 20;
 
101
        unsigned m_exp    : 11;
 
102
        unsigned m_neg    : 1;
105
103
#else
106
 
        unsigned    m_neg    : 1;
107
 
        unsigned    m_exp    : 11;
108
 
        unsigned    m_other1 : 20;
109
 
        unsigned    m_other2 : 32;
 
104
        unsigned m_neg    : 1;
 
105
        unsigned m_exp    : 11;
 
106
        unsigned m_other1 : 20;
 
107
        unsigned m_other2 : 32;
110
108
#endif
111
109
    } anony;
112
110
};
115
113
public:
116
114
    TLongExpFloat(const TLongExpFloat& b) : m_base(b.m_base), m_exp(b.m_exp) { }
117
115
 
118
 
    TLongExpFloat(int exp = 0, double base=0.0) : m_base(base), m_exp(exp) { }
 
116
    TLongExpFloat(int exp = 0, double base = 0.0) : m_base(base), m_exp(exp) { }
119
117
 
120
118
    TLongExpFloat(double d);
121
119
 
122
120
    TLongExpFloat
123
 
    operator* (const TLongExpFloat& b) const;
 
121
    operator*(const TLongExpFloat& b) const;
124
122
 
125
123
    TLongExpFloat
126
 
    operator/ (const TLongExpFloat& b) const;
 
124
    operator/(const TLongExpFloat& b) const;
127
125
 
128
126
    bool
129
 
    operator< (const TLongExpFloat& b) const;
 
127
    operator<(const TLongExpFloat& b) const;
130
128
 
131
129
    bool
132
130
    operator<=(const TLongExpFloat& b) const;
137
135
    void
138
136
    toString(std::string& str) const;
139
137
 
140
 
    void
141
 
    toString(char* buf) const
142
 
        {if (buf) sprintf(buf, "%10lf*2^%d", m_base, m_exp);}
 
138
    void toString(char* buf) const
 
139
    { if (buf) sprintf(buf, "%10lf*2^%d", m_base, m_exp); }
143
140
 
144
 
    double
145
 
    log2() const { return ::log2(m_base) + m_exp; }
 
141
    double log2() const { return ::log2(m_base) + m_exp; }
146
142
 
147
143
private:
148
 
    double   m_base;
149
 
    int      m_exp;
 
144
    double m_base;
 
145
    int m_exp;
150
146
};
151
147
 
152
148
/**
153
 
* UCS4 wide character type, system dependent
154
 
* Multibytes string in this program is UTF-8 only
155
 
*/
156
 
typedef unsigned int          TWCHAR;
 
149
 * UCS4 wide character type, system dependent
 
150
 * Multibytes string in this program is UTF-8 only
 
151
 */
 
152
typedef unsigned int TWCHAR;
157
153
 
158
154
#if !defined(WORDS_BIGENDIAN)
159
155
    #define  TWCHAR_ICONV_NAME  "UCS-4LE"
161
157
    #define  TWCHAR_ICONV_NAME  "UCS-4BE"
162
158
#endif
163
159
 
164
 
typedef TWCHAR                TSIMWordId;
165
 
 
166
 
const TSIMWordId SIM_ID_NOT_WORD        = (0x0);
167
 
const TSIMWordId SIM_ID_UNKNOWN_CN      = (2);
168
 
const TSIMWordId SIM_ID_SEN_TOKEN       = (10);
169
 
const TSIMWordId SIM_ID_DIGIT           = (20);
170
 
const TSIMWordId SIM_ID_SIMBOL          = (21);
171
 
const TSIMWordId SIM_ID_DUMMY_WORD      = (69);
172
 
const TSIMWordId SIM_ID_NONWORD         = (69);
173
 
const TSIMWordId SIM_ID_ALLWORD         = (69);
174
 
const TSIMWordId SIM_ID_REALWORD_START  = (70);
175
 
const TSIMWordId SIM_ID_CNWORD_START    = (100);
176
 
 
177
 
const TWCHAR WCH_NULL          =  0;
178
 
const TWCHAR WCH_RETURN        =  '\n';
179
 
const TWCHAR WCH_TAB           =  '\t';
180
 
const TWCHAR WCH_SPACE         =  ' ';
181
 
const TWCHAR WCH_LESSTHAN      =  '<';
182
 
const TWCHAR WCH_GREATERTHAN   =  '>';
183
 
 
184
 
const TWCHAR WCH_DOUBLESPACE   =  0x3000;
185
 
const TWCHAR WCH_JUHAO         =  0x3002;
186
 
const TWCHAR WCH_WENHAO        =  0xFF1F;
187
 
const TWCHAR WCH_TANHAO        =  0xFF01;
188
 
const TWCHAR WCH_FENHAO        =  0xFF1B;
189
 
const TWCHAR WCH_MAOHAO        =  0xFF1A;
190
 
const TWCHAR WCH_DOUHAO        =  0xFF0C;
191
 
const TWCHAR WCH_ZUOKUOHAO     =  0x201C;
192
 
const TWCHAR WCH_YOUKUOHAO     =  0x201D;
193
 
const TWCHAR WCH_SHENGLUEHAO   =  0x2026;
 
160
typedef TWCHAR TSIMWordId;
 
161
 
 
162
const TSIMWordId SIM_ID_NOT_WORD = (0x0);
 
163
const TSIMWordId SIM_ID_UNKNOWN_CN = (2);
 
164
const TSIMWordId SIM_ID_SEN_TOKEN = (10);
 
165
const TSIMWordId SIM_ID_DIGIT = (20);
 
166
const TSIMWordId SIM_ID_SIMBOL = (21);
 
167
const TSIMWordId SIM_ID_DUMMY_WORD = (69);
 
168
const TSIMWordId SIM_ID_NONWORD = (69);
 
169
const TSIMWordId SIM_ID_ALLWORD = (69);
 
170
const TSIMWordId SIM_ID_REALWORD_START = (70);
 
171
const TSIMWordId SIM_ID_CNWORD_START = (100);
 
172
 
 
173
const TWCHAR WCH_NULL = 0;
 
174
const TWCHAR WCH_RETURN = '\n';
 
175
const TWCHAR WCH_TAB = '\t';
 
176
const TWCHAR WCH_SPACE = ' ';
 
177
const TWCHAR WCH_LESSTHAN = '<';
 
178
const TWCHAR WCH_GREATERTHAN = '>';
 
179
 
 
180
const TWCHAR WCH_DOUBLESPACE = 0x3000;
 
181
const TWCHAR WCH_JUHAO = 0x3002;
 
182
const TWCHAR WCH_WENHAO = 0xFF1F;
 
183
const TWCHAR WCH_TANHAO = 0xFF01;
 
184
const TWCHAR WCH_FENHAO = 0xFF1B;
 
185
const TWCHAR WCH_MAOHAO = 0xFF1A;
 
186
const TWCHAR WCH_DOUHAO = 0xFF0C;
 
187
const TWCHAR WCH_ZUOKUOHAO = 0x201C;
 
188
const TWCHAR WCH_YOUKUOHAO = 0x201D;
 
189
const TWCHAR WCH_SHENGLUEHAO = 0x2026;
194
190
 
195
191
 
196
192
size_t MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n);
200
196
size_t WCSLEN(const TWCHAR* ws);
201
197
 
202
198
namespace std {
203
 
 
204
199
#ifdef HOST_OS_GNUC_2
205
200
struct string_char_traits<TWCHAR>
206
201
#else // !HOST_OS_GNUC_2
208
203
struct char_traits<TWCHAR>
209
204
#endif // HOST_OS_GNUC_2
210
205
{
211
 
    typedef TWCHAR            char_type;
212
 
    typedef unsigned int      int_type;
 
206
    typedef TWCHAR char_type;
 
207
    typedef unsigned int int_type;
213
208
#ifndef HOST_OS_GNUC_2
214
 
    typedef streamoff         off_type;
215
 
    typedef wstreampos        pos_type;
216
 
    typedef mbstate_t         state_type;
 
209
    typedef streamoff off_type;
 
210
    typedef wstreampos pos_type;
 
211
    typedef mbstate_t state_type;
217
212
#endif // !HOST_OS_GNUC_2
218
213
 
219
 
    static void
220
 
    assign(char_type& __c1, const char_type& __c2)
 
214
    static void assign(char_type& __c1, const char_type& __c2)
221
215
    { __c1 = __c2; }
222
216
 
223
 
    static bool
224
 
    eq(const char_type& __c1, const char_type& __c2)
 
217
    static bool eq(const char_type& __c1, const char_type& __c2)
225
218
    { return __c1 == __c2; }
226
219
 
227
 
    static bool
228
 
    lt(const char_type& __c1, const char_type& __c2)
 
220
    static bool lt(const char_type& __c1, const char_type& __c2)
229
221
    { return __c1 < __c2; }
230
222
 
231
 
    static int
232
 
    compare(const char_type* __s1, const char_type* __s2, size_t __n)
233
 
    {
234
 
        for (size_t i=0; i < __n; ++i) {
 
223
    static int compare(const char_type* __s1, const char_type* __s2,
 
224
                       size_t __n)           {
 
225
        for (size_t i = 0; i < __n; ++i) {
235
226
            if (*__s1 < *__s2)
236
227
                return -1;
237
228
            else if (*__s1++ == *__s2++)
242
233
        return 0;
243
234
    }
244
235
 
245
 
    static size_t
246
 
    length(const char_type* __s)
 
236
    static size_t length(const char_type* __s)
247
237
    { return WCSLEN(__s); }
248
238
 
249
 
    static char_type*
250
 
    copy(char_type* __s1, const char_type* __s2, size_t __n)
251
 
    {
252
 
        return static_cast<char_type*>(memcpy(__s1, __s2, __n*sizeof(char_type)));
 
239
    static char_type*copy(char_type* __s1, const char_type* __s2,
 
240
                          size_t __n)                 {
 
241
        return static_cast<char_type*>(memcpy(__s1, __s2, __n *
 
242
                                              sizeof(char_type)));
253
243
    }
254
244
 
255
 
    static char_type*
256
 
    move(char_type* __s1, const char_type* __s2, int_type __n)
257
 
    {
258
 
        return static_cast<char_type*>(memmove(__s1, __s2, __n*sizeof(char_type)));
 
245
    static char_type*move(char_type* __s1, const char_type* __s2,
 
246
                          int_type __n)                 {
 
247
        return static_cast<char_type*>(memmove(__s1, __s2, __n *
 
248
                                               sizeof(char_type)));
259
249
    }
260
250
 
261
251
#ifndef HOST_OS_GNUC_2
262
 
    static const char_type*
263
 
    find(const char_type* __s, size_t __n, const char_type& __a)
264
 
    {
 
252
    static const char_type*find(const char_type* __s,
 
253
                                size_t __n,
 
254
                                const char_type& __a)                       {
265
255
        while (__n--) {
266
256
            if (*__s++ == __a)
267
257
                return __s;
269
259
        return NULL;
270
260
    }
271
261
 
272
 
    static char_type
273
 
    to_char_type(const int_type& __c) { return char_type(__c); }
274
 
 
275
 
    static int_type
276
 
    to_int_type(const char_type& __c) { return int_type(__c); }
277
 
 
278
 
    static bool
279
 
    eq_int_type(const int_type& __c1, const int_type& __c2)
 
262
    static char_type to_char_type(const int_type& __c) { return char_type(__c); }
 
263
 
 
264
    static int_type to_int_type(const char_type& __c) { return int_type(__c); }
 
265
 
 
266
    static bool eq_int_type(const int_type& __c1, const int_type& __c2)
280
267
    { return __c1 == __c2; }
281
268
 
282
 
    static int_type
283
 
    eof() { return static_cast<int_type>(WEOF); }
 
269
    static int_type eof() { return static_cast<int_type>(WEOF); }
284
270
 
285
 
    static int_type
286
 
    not_eof(const int_type& __c)
 
271
    static int_type not_eof(const int_type& __c)
287
272
    { return eq_int_type(__c, eof()) ? 0 : __c; }
288
273
 
289
 
    static char_type*
290
 
    assign(char_type* __s, size_t __n, char_type __a)
291
 
    {
292
 
        for (char_type *p = __s; __n--;)
 
274
    static char_type*assign(char_type* __s, size_t __n,
 
275
                            char_type __a)                 {
 
276
        for (char_type *p = __s; __n--; )
293
277
            *p++ = __a;
294
278
        return __s;
295
279
    }
303
287
    static bool is_del(char_type a)
304
288
    { return a == WCH_SPACE; }
305
289
 
306
 
    static char_type* set(char_type* __s, char_type __a, size_t __n)
307
 
    {
308
 
        for (char_type *p = __s; __n--;)
 
290
    static char_type* set(char_type* __s, char_type __a, size_t __n){
 
291
        for (char_type *p = __s; __n--; )
309
292
            *p++ = __a;
310
293
        return __s;
311
294
    }
312
295
#endif // !HOST_OS_GNUC_2
313
296
};
314
 
 
315
297
}; // namespace std
316
298
 
317
299
#ifndef HOST_OS_GNUC_2
318
 
    typedef std::basic_string<TWCHAR>   wstring;
 
300
typedef std::basic_string<TWCHAR>   wstring;
319
301
#else // HOST_OS_GNUC_2
320
 
    class wstring : public std::basic_string<TWCHAR>
321
 
    {
322
 
        public:
323
 
            inline wstring() : std::basic_string<TWCHAR>((TWCHAR)0) {}
324
 
            inline wstring(const TWCHAR* c) : std::basic_string<TWCHAR>(c) {}
325
 
            inline wstring(const TWCHAR* c, size_t n) : std::basic_string<TWCHAR>(c, n) {}
326
 
            inline void push_back(TWCHAR c) { this->append(1, c); }
327
 
            inline void clear(void) { this->resize(0); }
328
 
            inline const TWCHAR* c_str(void) const
329
 
            {
330
 
                static TWCHAR null_s = 0;
331
 
                if (this->length() == 0) return &null_s;
332
 
                *(const_cast<TWCHAR*>(this->data()) + this->length()) = 0;
333
 
                return this->data();
334
 
            }
335
 
    };
 
302
class wstring : public std::basic_string<TWCHAR>
 
303
{
 
304
public:
 
305
    inline wstring() : std::basic_string<TWCHAR>((TWCHAR) 0) {}
 
306
    inline wstring(const TWCHAR* c) : std::basic_string<TWCHAR>(c) {}
 
307
    inline wstring(const TWCHAR* c, size_t n) : std::basic_string<TWCHAR>(c,
 
308
                                                                          n) {}
 
309
    inline void push_back(TWCHAR c) { this->append(1, c); }
 
310
    inline void clear(void) { this->resize(0); }
 
311
    inline const TWCHAR* c_str(void) const {
 
312
        static TWCHAR null_s = 0;
 
313
        if (this->length() == 0) return &null_s;
 
314
        *(const_cast<TWCHAR*>(this->data()) + this->length()) = 0;
 
315
        return this->data();
 
316
    }
 
317
};
336
318
#endif // !HOST_OS_GNUC_2
337
319
 
338
320
#ifdef _RW_STD_STL
339
321
template <class Iterator>
340
 
inline long distance (Iterator pos1, Iterator pos2)
341
 
{
342
 
    long d = 0;                                                                            
343
 
    distance (pos1, pos2, d);
344
 
    return d;                                                                              
345
 
}                                                                                        
 
322
inline long distance(Iterator pos1, Iterator pos2){
 
323
    long d = 0;
 
324
    distance(pos1, pos2, d);
 
325
    return d;
 
326
}
346
327
#endif
347
328
 
348
 
#if !defined (HAVE_STRNDUP)
349
 
extern "C" char *strndup( const char *s, size_t n );
350
 
#endif //HAVE_STRNDUP
351
 
 
352
329
#endif