3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
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
18 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
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.
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.
39 #ifndef ___SUN_SLM_PORTABILITY_H___
40 #define ___SUN_SLM_PORTABILITY_H___
55 #endif //ifdef HAVE_UNISTD_H
57 #ifndef HOST_OS_GNUC_2
58 #if defined(DEBUG) && !defined(NDEBUG)
59 #define DEBUG_print(fmt, ...) fprintf(stderr, fmt, ...)
61 #define DEBUG_print(fmt, ...) (int(0))
63 #else // HOST_OS_GNUC_2
64 #if defined(DEBUG) && !defined(NDEBUG)
65 #define DEBUG_print(fmt, a ...) fprintf(stderr, fmt, a ...)
67 #define DEBUG_print(fmt, a ...) (int(0))
69 #endif // !HOST_OS_GNUC_2
72 inline double log2(double x) { return log(x) / M_LN2; }
75 #if defined(sun) // Solaris/HP-UX 's iconv is const char**
76 typedef const char* TIConvSrcPtr;
78 typedef char* TIConvSrcPtr;
81 union TDoubleAnatomy {
83 TDoubleAnatomy(double d) : m_value(d){
86 int getExp(void) const
87 { return anony.m_exp - 0x3FF; }
89 double getValue(void) const
93 { anony.m_exp = 0x3FF; }
98 #ifndef WORDS_BIGENDIAN
99 unsigned m_other2 : 32;
100 unsigned m_other1 : 20;
106 unsigned m_other1 : 20;
107 unsigned m_other2 : 32;
112 struct TLongExpFloat {
114 TLongExpFloat(const TLongExpFloat& b) : m_base(b.m_base), m_exp(b.m_exp) { }
116 TLongExpFloat(int exp = 0, double base = 0.0) : m_base(base), m_exp(exp) { }
118 TLongExpFloat(double d);
121 operator*(const TLongExpFloat& b) const;
124 operator/(const TLongExpFloat& b) const;
127 operator<(const TLongExpFloat& b) const;
130 operator<=(const TLongExpFloat& b) const;
133 operator==(const TLongExpFloat& b) const;
136 toString(std::string& str) const;
138 void toString(char* buf) const
139 { if (buf) sprintf(buf, "%10lf*2^%d", m_base, m_exp); }
141 double log2() const { return ::log2(m_base) + m_exp; }
149 * UCS4 wide character type, system dependent
150 * Multibytes string in this program is UTF-8 only
152 typedef unsigned int TWCHAR;
154 #if !defined(WORDS_BIGENDIAN)
155 #define TWCHAR_ICONV_NAME "UCS-4LE"
157 #define TWCHAR_ICONV_NAME "UCS-4BE"
160 typedef TWCHAR TSIMWordId;
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);
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 = '>';
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;
192 size_t MBSTOWCS(TWCHAR *pwcs, const char* s, size_t n);
194 size_t WCSTOMBS(char* s, const TWCHAR* pwcs, size_t n);
196 size_t WCSLEN(const TWCHAR* ws);
199 #ifdef HOST_OS_GNUC_2
200 struct string_char_traits<TWCHAR>
201 #else // !HOST_OS_GNUC_2
203 struct char_traits<TWCHAR>
204 #endif // HOST_OS_GNUC_2
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
214 static void assign(char_type& __c1, const char_type& __c2)
217 static bool eq(const char_type& __c1, const char_type& __c2)
218 { return __c1 == __c2; }
220 static bool lt(const char_type& __c1, const char_type& __c2)
221 { return __c1 < __c2; }
223 static int compare(const char_type* __s1, const char_type* __s2,
225 for (size_t i = 0; i < __n; ++i) {
228 else if (*__s1++ == *__s2++)
236 static size_t length(const char_type* __s)
237 { return WCSLEN(__s); }
239 static char_type*copy(char_type* __s1, const char_type* __s2,
241 return static_cast<char_type*>(memcpy(__s1, __s2, __n *
245 static char_type*move(char_type* __s1, const char_type* __s2,
247 return static_cast<char_type*>(memmove(__s1, __s2, __n *
251 #ifndef HOST_OS_GNUC_2
252 static const char_type*find(const char_type* __s,
254 const char_type& __a) {
262 static char_type to_char_type(const int_type& __c) { return char_type(__c); }
264 static int_type to_int_type(const char_type& __c) { return int_type(__c); }
266 static bool eq_int_type(const int_type& __c1, const int_type& __c2)
267 { return __c1 == __c2; }
269 static int_type eof() { return static_cast<int_type>(WEOF); }
271 static int_type not_eof(const int_type& __c)
272 { return eq_int_type(__c, eof()) ? 0 : __c; }
274 static char_type*assign(char_type* __s, size_t __n,
276 for (char_type *p = __s; __n--; )
280 #else // HOST_OS_GNUC_2
281 static bool ne(const char_type& __c1, const char_type& __c2)
282 { return __c1 != __c2; }
284 static char_type eos()
287 static bool is_del(char_type a)
288 { return a == WCH_SPACE; }
290 static char_type* set(char_type* __s, char_type __a, size_t __n){
291 for (char_type *p = __s; __n--; )
295 #endif // !HOST_OS_GNUC_2
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>
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,
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;
318 #endif // !HOST_OS_GNUC_2
321 template <class Iterator>
322 inline long distance(Iterator pos1, Iterator pos2){
324 distance(pos1, pos2, d);
329 #if !defined (HAVE_STRNDUP)
330 extern "C" char *strndup(const char *s, size_t n);
331 #endif //HAVE_STRNDUP