Tizen 2.1 base
[platform/core/uifw/ise-engine-sunpinyin.git] / src / portability.h
1 // -*- mode: c++ -*-
2 /*
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4  *
5  * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
6  *
7  * The contents of this file are subject to the terms of either the GNU Lesser
8  * General Public License Version 2.1 only ("LGPL") or the Common Development and
9  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
10  * file except in compliance with the License. You can obtain a copy of the CDDL at
11  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
12  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
13  * specific language governing permissions and limitations under the License. When
14  * distributing the software, include this License Header Notice in each file and
15  * include the full text of the License in the License file as well as the
16  * following notice:
17  *
18  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
19  * (CDDL)
20  * For Covered Software in this distribution, this License shall be governed by the
21  * laws of the State of California (excluding conflict-of-law provisions).
22  * Any litigation relating to this License shall be subject to the jurisdiction of
23  * the Federal Courts of the Northern District of California and the state courts
24  * of the State of California, with venue lying in Santa Clara County, California.
25  *
26  * Contributor(s):
27  *
28  * If you wish your version of this file to be governed by only the CDDL or only
29  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
30  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
31  * license." If you don't indicate a single choice of license, a recipient has the
32  * option to distribute your version of this file under either the CDDL or the LGPL
33  * Version 2.1, or to extend the choice of license to its licensees as provided
34  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
35  * Version 2 license, then the option applies only if the new code is made subject
36  * to such option by the copyright holder.
37  */
38
39 #ifndef ___SUN_SLM_PORTABILITY_H___
40 #define ___SUN_SLM_PORTABILITY_H___
41
42 #include <stdio.h>
43 #include <math.h>
44 #include <stdint.h>
45 #include <string>
46 #include <cstring>
47
48 #ifdef HAVE_UNISTD_H
49 #include <unistd.h>
50 #ifdef __cplusplus
51 #include <algorithm>
52 #else
53 #include <sys/ddi.h>
54 #endif //__cpluscplus
55 #endif //ifdef HAVE_UNISTD_H
56
57 #ifndef HOST_OS_GNUC_2
58     #if defined(DEBUG) && !defined(NDEBUG)
59         #define DEBUG_print(fmt, ...)   fprintf(stderr, fmt, ...)
60     #else
61         #define DEBUG_print(fmt, ...)   (int(0))
62     #endif
63 #else // HOST_OS_GNUC_2
64     #if defined(DEBUG) && !defined(NDEBUG)
65         #define DEBUG_print(fmt, a ...)   fprintf(stderr, fmt, a ...)
66     #else
67         #define DEBUG_print(fmt, a ...)   (int(0))
68     #endif
69 #endif // !HOST_OS_GNUC_2
70
71 #ifndef HAVE_LOG2
72 inline double log2(double x) { return log(x) / M_LN2; }
73 #endif
74
75 #if defined(sun) // Solaris/HP-UX 's iconv is const char**
76 typedef const char* TIConvSrcPtr;
77 #else
78 typedef char* TIConvSrcPtr;
79 #endif
80
81 union TDoubleAnatomy {
82 public:
83     TDoubleAnatomy(double d) : m_value(d){
84     }
85
86     int getExp(void) const
87     { return anony.m_exp - 0x3FF; }
88
89     double getValue(void) const
90     { return m_value; }
91
92     void clearExp(void)
93     { anony.m_exp = 0x3FF; }
94
95 public:
96     double m_value;
97     struct TAnony {
98 #ifndef WORDS_BIGENDIAN
99         unsigned m_other2 : 32;
100         unsigned m_other1 : 20;
101         unsigned m_exp    : 11;
102         unsigned m_neg    : 1;
103 #else
104         unsigned m_neg    : 1;
105         unsigned m_exp    : 11;
106         unsigned m_other1 : 20;
107         unsigned m_other2 : 32;
108 #endif
109     } anony;
110 };
111
112 struct TLongExpFloat {
113 public:
114     TLongExpFloat(const TLongExpFloat& b) : m_base(b.m_base), m_exp(b.m_exp) { }
115
116     TLongExpFloat(int exp = 0, double base = 0.0) : m_base(base), m_exp(exp) { }
117
118     TLongExpFloat(double d);
119
120     TLongExpFloat
121     operator*(const TLongExpFloat& b) const;
122
123     TLongExpFloat
124     operator/(const TLongExpFloat& b) const;
125
126     bool
127     operator<(const TLongExpFloat& b) const;
128
129     bool
130     operator<=(const TLongExpFloat& b) const;
131
132     bool
133     operator==(const TLongExpFloat& b) const;
134
135     void
136     toString(std::string& str) const;
137
138     void toString(char* buf) const
139     { if (buf) sprintf(buf, "%10lf*2^%d", m_base, m_exp); }
140
141     double log2() const { return ::log2(m_base) + m_exp; }
142
143 private:
144     double m_base;
145     int m_exp;
146 };
147
148 /**
149  * UCS4 wide character type, system dependent
150  * Multibytes string in this program is UTF-8 only
151  */
152 typedef unsigned int TWCHAR;
153
154 #if !defined(WORDS_BIGENDIAN)
155     #define  TWCHAR_ICONV_NAME  "UCS-4LE"
156 #else
157     #define  TWCHAR_ICONV_NAME  "UCS-4BE"
158 #endif
159
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;
190
191
192 size_t MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n);
193
194 size_t WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n);
195
196 size_t WCSLEN(const TWCHAR* ws);
197
198 namespace std {
199 #ifdef HOST_OS_GNUC_2
200 struct string_char_traits<TWCHAR>
201 #else // !HOST_OS_GNUC_2
202 template<>
203 struct char_traits<TWCHAR>
204 #endif // HOST_OS_GNUC_2
205 {
206     typedef TWCHAR char_type;
207     typedef unsigned int int_type;
208 #ifndef HOST_OS_GNUC_2
209     typedef streamoff off_type;
210     typedef wstreampos pos_type;
211     typedef mbstate_t state_type;
212 #endif // !HOST_OS_GNUC_2
213
214     static void assign(char_type& __c1, const char_type& __c2)
215     { __c1 = __c2; }
216
217     static bool eq(const char_type& __c1, const char_type& __c2)
218     { return __c1 == __c2; }
219
220     static bool lt(const char_type& __c1, const char_type& __c2)
221     { return __c1 < __c2; }
222
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) {
226             if (*__s1 < *__s2)
227                 return -1;
228             else if (*__s1++ == *__s2++)
229                 continue;
230             else
231                 return 1;
232         }
233         return 0;
234     }
235
236     static size_t length(const char_type* __s)
237     { return WCSLEN(__s); }
238
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)));
243     }
244
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)));
249     }
250
251 #ifndef HOST_OS_GNUC_2
252     static const char_type*find(const char_type* __s,
253                                 size_t __n,
254                                 const char_type& __a)                       {
255         while (__n--) {
256             if (*__s++ == __a)
257                 return __s;
258         }
259         return NULL;
260     }
261
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)
267     { return __c1 == __c2; }
268
269     static int_type eof() { return static_cast<int_type>(WEOF); }
270
271     static int_type not_eof(const int_type& __c)
272     { return eq_int_type(__c, eof()) ? 0 : __c; }
273
274     static char_type*assign(char_type* __s, size_t __n,
275                             char_type __a)                 {
276         for (char_type *p = __s; __n--; )
277             *p++ = __a;
278         return __s;
279     }
280 #else // HOST_OS_GNUC_2
281     static bool ne(const char_type& __c1, const char_type& __c2)
282     { return __c1 != __c2; }
283
284     static char_type eos()
285     { return 0; }
286
287     static bool is_del(char_type a)
288     { return a == WCH_SPACE; }
289
290     static char_type* set(char_type* __s, char_type __a, size_t __n){
291         for (char_type *p = __s; __n--; )
292             *p++ = __a;
293         return __s;
294     }
295 #endif // !HOST_OS_GNUC_2
296 };
297 }; // namespace std
298
299 #ifndef HOST_OS_GNUC_2
300 typedef std::basic_string<TWCHAR>   wstring;
301 #else // HOST_OS_GNUC_2
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 };
318 #endif // !HOST_OS_GNUC_2
319
320 #ifdef _RW_STD_STL
321 template <class Iterator>
322 inline long distance(Iterator pos1, Iterator pos2){
323     long d = 0;
324     distance(pos1, pos2, d);
325     return d;
326 }
327 #endif
328
329 #if !defined (HAVE_STRNDUP)
330 extern "C" char *strndup(const char *s, size_t n);
331 #endif //HAVE_STRNDUP
332
333 #endif