Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / i18npool / source / collator / collator_unicode.cxx
blob2ba089a43418936b4490a322f64eb728289c0ef8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 // generated list of languages
22 #include "lrl_include.hxx"
24 #include <rtl/ustrbuf.hxx>
25 #include <collator_unicode.hxx>
26 #include <localedata.hxx>
27 #include <com/sun/star/i18n/CollatorOptions.hpp>
29 using namespace ::com::sun::star;
30 using namespace ::com::sun::star::lang;
31 using namespace ::com::sun::star::uno;
32 using namespace ::rtl;
34 namespace com { namespace sun { namespace star { namespace i18n {
36 Collator_Unicode::Collator_Unicode()
38 implementationName = "com.sun.star.i18n.Collator_Unicode";
39 collator = NULL;
40 uca_base = NULL;
41 #ifndef DISABLE_DYNLOADING
42 hModule = NULL;
43 #endif
46 Collator_Unicode::~Collator_Unicode()
48 if (collator) delete collator;
49 if (uca_base) delete uca_base;
50 #ifndef DISABLE_DYNLOADING
51 if (hModule) osl_unloadModule(hModule);
52 #endif
55 #ifdef DISABLE_DYNLOADING
57 extern "C" {
59 // For DISABLE_DYNLOADING the generated functions have names that
60 // start with get_collator_data_ to avoid clashing with a few
61 // functions in the generated libindex_data that are called just
62 // get_zh_pinyin for instance.
64 const sal_uInt8* get_collator_data_ca_charset();
65 const sal_uInt8* get_collator_data_dz_charset();
66 const sal_uInt8* get_collator_data_hu_charset();
67 const sal_uInt8* get_collator_data_ja_charset();
68 const sal_uInt8* get_collator_data_ja_phonetic_alphanumeric_first();
69 const sal_uInt8* get_collator_data_ja_phonetic_alphanumeric_last();
70 const sal_uInt8* get_collator_data_ko_charset();
71 const sal_uInt8* get_collator_data_ku_alphanumeric();
72 const sal_uInt8* get_collator_data_ln_charset();
73 const sal_uInt8* get_collator_data_my_dictionary();
74 const sal_uInt8* get_collator_data_ne_charset();
75 const sal_uInt8* get_collator_data_zh_TW_charset();
76 const sal_uInt8* get_collator_data_zh_TW_radical();
77 const sal_uInt8* get_collator_data_zh_TW_stroke();
78 const sal_uInt8* get_collator_data_zh_charset();
79 const sal_uInt8* get_collator_data_zh_pinyin();
80 const sal_uInt8* get_collator_data_zh_radical();
81 const sal_uInt8* get_collator_data_zh_stroke();
82 const sal_uInt8* get_collator_data_zh_zhuyin();
86 #endif
88 sal_Int32 SAL_CALL
89 Collator_Unicode::compareSubstring( const OUString& str1, sal_Int32 off1, sal_Int32 len1,
90 const OUString& str2, sal_Int32 off2, sal_Int32 len2) throw(RuntimeException)
92 return collator->compare(reinterpret_cast<const UChar *>(str1.getStr()) + off1, len1, reinterpret_cast<const UChar *>(str2.getStr()) + off2, len2); // UChar != sal_Unicode in MinGW
95 sal_Int32 SAL_CALL
96 Collator_Unicode::compareString( const OUString& str1, const OUString& str2) throw(RuntimeException)
98 return collator->compare(reinterpret_cast<const UChar *>(str1.getStr()), reinterpret_cast<const UChar *>(str2.getStr())); // UChar != sal_Unicode in MinGW
101 #ifndef DISABLE_DYNLOADING
103 extern "C" { static void SAL_CALL thisModule() {} }
105 #endif
107 sal_Int32 SAL_CALL
108 Collator_Unicode::loadCollatorAlgorithm(const OUString& rAlgorithm, const lang::Locale& rLocale, sal_Int32 options)
109 throw(RuntimeException)
111 if (!collator) {
112 UErrorCode status = U_ZERO_ERROR;
113 OUString rule = LocaleData().getCollatorRuleByAlgorithm(rLocale, rAlgorithm);
114 if (!rule.isEmpty()) {
115 collator = new RuleBasedCollator(reinterpret_cast<const UChar *>(rule.getStr()), status); // UChar != sal_Unicode in MinGW
116 if (! U_SUCCESS(status)) throw RuntimeException();
118 if (!collator && OUString::createFromAscii(LOCAL_RULE_LANGS).indexOf(rLocale.Language) >= 0) {
119 const sal_uInt8* (*func)() = NULL;
121 #ifndef DISABLE_DYNLOADING
122 OUStringBuffer aBuf;
123 #ifdef SAL_DLLPREFIX
124 aBuf.appendAscii(SAL_DLLPREFIX);
125 #endif
126 aBuf.appendAscii( "collator_data" ).appendAscii( SAL_DLLEXTENSION );
127 hModule = osl_loadModuleRelative( &thisModule, aBuf.makeStringAndClear().pData, SAL_LOADMODULE_DEFAULT );
128 if (hModule) {
129 aBuf.appendAscii("get_").append(rLocale.Language).appendAscii("_");
130 if ( rLocale.Language == "zh" ) {
131 OUString func_base = aBuf.makeStringAndClear();
132 if (OUString("TW HK MO").indexOf(rLocale.Country) >= 0)
133 func=(const sal_uInt8* (*)()) osl_getFunctionSymbol(hModule,
134 OUString(func_base + "TW_" + rAlgorithm).pData);
135 if (!func)
136 func=(const sal_uInt8* (*)()) osl_getFunctionSymbol(hModule, OUString(func_base + rAlgorithm).pData);
137 } else {
138 if ( rLocale.Language == "ja" ) {
139 // replace algrithm name to implementation name.
140 if (rAlgorithm.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("phonetic (alphanumeric first)")) )
141 aBuf.appendAscii("phonetic_alphanumeric_first");
142 else if (rAlgorithm.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("phonetic (alphanumeric last)")))
143 aBuf.appendAscii("phonetic_alphanumeric_last");
144 else
145 aBuf.append(rAlgorithm);
146 } else {
147 aBuf.append(rAlgorithm);
149 func=(const sal_uInt8* (*)()) osl_getFunctionSymbol(hModule, aBuf.makeStringAndClear().pData);
152 #else
153 if ( rLocale.Language == "ca" ) {
154 if ( rAlgorithm == "charset" )
155 func = get_collator_data_ca_charset;
156 } else if ( rLocale.Language == "dz" ) {
157 if ( rAlgorithm == "charset" )
158 func = get_collator_data_dz_charset;
159 } else if ( rLocale.Language == "hu" ) {
160 if ( rAlgorithm == "charset" )
161 func = get_collator_data_hu_charset;
162 } else if ( rLocale.Language == "ja" ) {
163 if ( rAlgorithm == "charset" )
164 func = get_collator_data_hu_charset;
165 else if ( rAlgorithm == "phonetic (alphanumeric first)" )
166 func = get_collator_data_ja_phonetic_alphanumeric_first;
167 else if ( rAlgorithm == "phonetic (alphanumeric last)" )
168 func = get_collator_data_ja_phonetic_alphanumeric_last;
169 } else if ( rLocale.Language == "ko" ) {
170 if ( rAlgorithm == "charset" )
171 func = get_collator_data_ko_charset;
172 } else if ( rLocale.Language == "ku" ) {
173 if ( rAlgorithm == "alphanumeric" )
174 func = get_collator_data_ku_alphanumeric;
175 } else if ( rLocale.Language == "ln" ) {
176 if ( rAlgorithm == "charset" )
177 func = get_collator_data_ln_charset;
178 } else if ( rLocale.Language == "my" ) {
179 if ( rAlgorithm == "dictionary" )
180 func = get_collator_data_my_dictionary;
181 } else if ( rLocale.Language == "ne" ) {
182 if ( rAlgorithm == "charset" )
183 func = get_collator_data_ne_charset;
184 } else if ( rLocale.Language == "zh" && (rLocale.Country == "TW" || rLocale.Country == "HK" || rLocale.Country == "MO") ) {
185 if ( rAlgorithm == "charset" )
186 func = get_collator_data_zh_TW_charset;
187 else if ( rAlgorithm == "radical" )
188 func = get_collator_data_zh_TW_radical;
189 else if ( rAlgorithm == "stroke" )
190 func = get_collator_data_zh_TW_stroke;
191 } else if ( rLocale.Language == "zh" ) {
192 if ( rAlgorithm == "charset" )
193 func = get_collator_data_zh_charset;
194 else if ( rAlgorithm == "pinyin" )
195 func = get_collator_data_zh_pinyin;
196 else if ( rAlgorithm == "radical" )
197 func = get_collator_data_zh_radical;
198 else if ( rAlgorithm == "stroke" )
199 func = get_collator_data_zh_stroke;
200 else if ( rAlgorithm == "zhuyin" )
201 func = get_collator_data_zh_zhuyin;
203 #endif
204 if (func) {
205 const sal_uInt8* ruleImage=func();
206 uca_base = new RuleBasedCollator(static_cast<UChar*>(NULL), status);
207 if (! U_SUCCESS(status)) throw RuntimeException();
208 collator = new RuleBasedCollator(reinterpret_cast<const uint8_t*>(ruleImage), -1, uca_base, status);
209 if (! U_SUCCESS(status)) throw RuntimeException();
212 if (!collator) {
213 /** ICU collators are loaded using a locale only.
214 ICU uses Variant as collation algorithm name (like de__PHONEBOOK
215 locale), note the empty territory (Country) designator in this special
216 case here. The icu::Locale contructor changes the algorithm name to
217 uppercase itself, so we don't have to bother with that.
219 icu::Locale icuLocale(
220 OUStringToOString(rLocale.Language, RTL_TEXTENCODING_ASCII_US).getStr(),
221 OUStringToOString(rLocale.Country, RTL_TEXTENCODING_ASCII_US).getStr(),
222 OUStringToOString(rAlgorithm, RTL_TEXTENCODING_ASCII_US).getStr());
223 // load ICU collator
224 collator = (RuleBasedCollator*) icu::Collator::createInstance(icuLocale, status);
225 if (! U_SUCCESS(status)) throw RuntimeException();
229 if (options & CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT)
230 collator->setStrength(Collator::PRIMARY);
231 else if (options & CollatorOptions::CollatorOptions_IGNORE_CASE)
232 collator->setStrength(Collator::SECONDARY);
233 else
234 collator->setStrength(Collator::TERTIARY);
236 return(0);
240 OUString SAL_CALL
241 Collator_Unicode::getImplementationName() throw( RuntimeException )
243 return OUString::createFromAscii(implementationName);
246 sal_Bool SAL_CALL
247 Collator_Unicode::supportsService(const rtl::OUString& rServiceName) throw( RuntimeException )
249 return !rServiceName.compareToAscii(implementationName);
252 Sequence< OUString > SAL_CALL
253 Collator_Unicode::getSupportedServiceNames() throw( RuntimeException )
255 Sequence< OUString > aRet(1);
256 aRet[0] = OUString::createFromAscii(implementationName);
257 return aRet;
260 } } } }
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */