Add copy constructor
[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     TLongExpFloat&
136     operator=(const TLongExpFloat& b);
137
138     void
139     toString(std::string& str) const;
140
141     void toString(char* buf) const
142     { if (buf) sprintf(buf, "%10lf*2^%d", m_base, m_exp); }
143
144     double log2() const { return ::log2(m_base) + m_exp; }
145
146 private:
147     double m_base;
148     int m_exp;
149 };
150
151 /**
152  * UCS4 wide character type, system dependent
153  * Multibytes string in this program is UTF-8 only
154  */
155 typedef unsigned int TWCHAR;
156
157 #if !defined(WORDS_BIGENDIAN)
158     #define  TWCHAR_ICONV_NAME  "UCS-4LE"
159 #else
160     #define  TWCHAR_ICONV_NAME  "UCS-4BE"
161 #endif
162
163 typedef TWCHAR TSIMWordId;
164
165 const TSIMWordId SIM_ID_NOT_WORD = (0x0);
166 const TSIMWordId SIM_ID_UNKNOWN_CN = (2);
167 const TSIMWordId SIM_ID_SEN_TOKEN = (10);
168 const TSIMWordId SIM_ID_DIGIT = (20);
169 const TSIMWordId SIM_ID_SIMBOL = (21);
170 const TSIMWordId SIM_ID_DUMMY_WORD = (69);
171 const TSIMWordId SIM_ID_NONWORD = (69);
172 const TSIMWordId SIM_ID_ALLWORD = (69);
173 const TSIMWordId SIM_ID_REALWORD_START = (70);
174 const TSIMWordId SIM_ID_CNWORD_START = (100);
175
176 const TWCHAR WCH_NULL = 0;
177 const TWCHAR WCH_RETURN = '\n';
178 const TWCHAR WCH_TAB = '\t';
179 const TWCHAR WCH_SPACE = ' ';
180 const TWCHAR WCH_LESSTHAN = '<';
181 const TWCHAR WCH_GREATERTHAN = '>';
182
183 const TWCHAR WCH_DOUBLESPACE = 0x3000;
184 const TWCHAR WCH_JUHAO = 0x3002;
185 const TWCHAR WCH_WENHAO = 0xFF1F;
186 const TWCHAR WCH_TANHAO = 0xFF01;
187 const TWCHAR WCH_FENHAO = 0xFF1B;
188 const TWCHAR WCH_MAOHAO = 0xFF1A;
189 const TWCHAR WCH_DOUHAO = 0xFF0C;
190 const TWCHAR WCH_ZUOKUOHAO = 0x201C;
191 const TWCHAR WCH_YOUKUOHAO = 0x201D;
192 const TWCHAR WCH_SHENGLUEHAO = 0x2026;
193
194
195 size_t MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n);
196
197 size_t WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n);
198
199 EXPORTED size_t WCSLEN(const TWCHAR* ws);
200
201 namespace std {
202 #ifdef HOST_OS_GNUC_2
203 struct string_char_traits<TWCHAR>
204 #else // !HOST_OS_GNUC_2
205 template<>
206 struct char_traits<TWCHAR>
207 #endif // HOST_OS_GNUC_2
208 {
209     typedef TWCHAR char_type;
210     typedef unsigned int int_type;
211 #ifndef HOST_OS_GNUC_2
212     typedef streamoff off_type;
213     typedef wstreampos pos_type;
214     typedef mbstate_t state_type;
215 #endif // !HOST_OS_GNUC_2
216
217     static void assign(char_type& __c1, const char_type& __c2)
218     { __c1 = __c2; }
219
220     static bool eq(const char_type& __c1, const char_type& __c2)
221     { return __c1 == __c2; }
222
223     static bool lt(const char_type& __c1, const char_type& __c2)
224     { return __c1 < __c2; }
225
226     static int compare(const char_type* __s1, const char_type* __s2,
227                        size_t __n)           {
228         for (size_t i = 0; i < __n; ++i) {
229             if (*__s1 < *__s2)
230                 return -1;
231             else if (*__s1++ == *__s2++)
232                 continue;
233             else
234                 return 1;
235         }
236         return 0;
237     }
238
239     static size_t length(const char_type* __s)
240     { return WCSLEN(__s); }
241
242     static char_type*copy(char_type* __s1, const char_type* __s2,
243                           size_t __n)                 {
244         return static_cast<char_type*>(memcpy(__s1, __s2, __n *
245                                               sizeof(char_type)));
246     }
247
248     static char_type*move(char_type* __s1, const char_type* __s2,
249                           int_type __n)                 {
250         return static_cast<char_type*>(memmove(__s1, __s2, __n *
251                                                sizeof(char_type)));
252     }
253
254 #ifndef HOST_OS_GNUC_2
255     static const char_type*find(const char_type* __s,
256                                 size_t __n,
257                                 const char_type& __a)                       {
258         while (__n--) {
259             if (*__s++ == __a)
260                 return __s;
261         }
262         return NULL;
263     }
264
265     static char_type to_char_type(const int_type& __c) { return char_type(__c); }
266
267     static int_type to_int_type(const char_type& __c) { return int_type(__c); }
268
269     static bool eq_int_type(const int_type& __c1, const int_type& __c2)
270     { return __c1 == __c2; }
271
272     static int_type eof() { return static_cast<int_type>(WEOF); }
273
274     static int_type not_eof(const int_type& __c)
275     { return eq_int_type(__c, eof()) ? 0 : __c; }
276
277     static char_type*assign(char_type* __s, size_t __n,
278                             char_type __a)                 {
279         for (char_type *p = __s; __n--; )
280             *p++ = __a;
281         return __s;
282     }
283 #else // HOST_OS_GNUC_2
284     static bool ne(const char_type& __c1, const char_type& __c2)
285     { return __c1 != __c2; }
286
287     static char_type eos()
288     { return 0; }
289
290     static bool is_del(char_type a)
291     { return a == WCH_SPACE; }
292
293     static char_type* set(char_type* __s, char_type __a, size_t __n){
294         for (char_type *p = __s; __n--; )
295             *p++ = __a;
296         return __s;
297     }
298 #endif // !HOST_OS_GNUC_2
299 };
300 }; // namespace std
301
302 #ifndef HOST_OS_GNUC_2
303 typedef std::basic_string<TWCHAR>   wstring;
304 #else // HOST_OS_GNUC_2
305 class wstring : public std::basic_string<TWCHAR>
306 {
307 public:
308     inline wstring() : std::basic_string<TWCHAR>((TWCHAR) 0) {}
309     inline wstring(const TWCHAR* c) : std::basic_string<TWCHAR>(c) {}
310     inline wstring(const TWCHAR* c, size_t n) : std::basic_string<TWCHAR>(c,
311                                                                           n) {}
312     inline void push_back(TWCHAR c) { this->append(1, c); }
313     inline void clear(void) { this->resize(0); }
314     inline const TWCHAR* c_str(void) const {
315         static TWCHAR null_s = 0;
316         if (this->length() == 0) return &null_s;
317         *(const_cast<TWCHAR*>(this->data()) + this->length()) = 0;
318         return this->data();
319     }
320 };
321 #endif // !HOST_OS_GNUC_2
322
323 #ifdef _RW_STD_STL
324 template <class Iterator>
325 inline long distance(Iterator pos1, Iterator pos2){
326     long d = 0;
327     distance(pos1, pos2, d);
328     return d;
329 }
330 #endif
331
332 #if !defined (HAVE_STRNDUP)
333 extern "C" char *strndup(const char *s, size_t n);
334 #endif //HAVE_STRNDUP
335
336 #endif