Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / i18npool / source / indexentry / indexentrysupplier_asian.cxx
blob4df4d24c309f0f315355eaeae47498c892674f04
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 #include <rtl/ustrbuf.hxx>
22 #include <indexentrysupplier_asian.hxx>
23 #include <data/indexdata_alphanumeric.h>
25 using namespace ::com::sun::star::uno;
26 using namespace ::com::sun::star::lang;
27 using namespace ::rtl;
29 namespace com { namespace sun { namespace star { namespace i18n {
31 #ifndef DISABLE_DYNLOADING
33 extern "C" { static void SAL_CALL thisModule() {} }
35 #endif
37 IndexEntrySupplier_asian::IndexEntrySupplier_asian(
38 const Reference < XComponentContext >& rxContext ) : IndexEntrySupplier_Common(rxContext)
40 implementationName = "com.sun.star.i18n.IndexEntrySupplier_asian";
41 #ifndef DISABLE_DYNLOADING
42 #ifdef SAL_DLLPREFIX
43 OUString lib(SAL_DLLPREFIX"index_data" SAL_DLLEXTENSION);
44 #else
45 OUString lib("index_data" SAL_DLLEXTENSION);
46 #endif
47 hModule = osl_loadModuleRelative(
48 &thisModule, lib.pData, SAL_LOADMODULE_DEFAULT );
49 #endif
52 IndexEntrySupplier_asian::~IndexEntrySupplier_asian()
54 #ifndef DISABLE_DYNLOADING
55 if (hModule) osl_unloadModule(hModule);
56 #endif
59 #ifdef DISABLE_DYNLOADING
61 extern "C" {
63 sal_uInt16** get_indexdata_ko_dict(sal_Int16*);
64 sal_uInt16** get_indexdata_zh_TW_radical(sal_Int16*);
65 sal_uInt16** get_indexdata_zh_TW_stroke(sal_Int16*);
66 sal_uInt16** get_indexdata_zh_pinyin(sal_Int16*);
67 sal_uInt16** get_indexdata_zh_radical(sal_Int16*);
68 sal_uInt16** get_indexdata_zh_stroke(sal_Int16*);
69 sal_uInt16** get_indexdata_zh_zhuyin(sal_Int16*);
71 sal_uInt16** get_ko_phonetic(sal_Int16*);
72 sal_uInt16** get_zh_pinyin(sal_Int16*);
73 sal_uInt16** get_zh_zhuyin(sal_Int16*);
77 #endif
79 OUString SAL_CALL
80 IndexEntrySupplier_asian::getIndexCharacter( const OUString& rIndexEntry,
81 const Locale& rLocale, const OUString& rAlgorithm ) throw (RuntimeException)
83 sal_Int32 i=0;
84 sal_uInt32 ch = rIndexEntry.iterateCodePoints(&i, 0);
86 sal_uInt16** (*func)(sal_Int16*)=NULL;
87 #ifndef DISABLE_DYNLOADING
88 if (hModule) {
89 OUString get("get_indexdata_");
90 if ( rLocale.Language == "zh" && OUString( "TW HK MO" ).indexOf(rLocale.Country) >= 0 )
91 func=(sal_uInt16** (*)(sal_Int16*))osl_getFunctionSymbol(hModule, OUString(get+rLocale.Language+"_TW_"+rAlgorithm).pData);
92 if (!func)
93 func=(sal_uInt16** (*)(sal_Int16*))osl_getFunctionSymbol(hModule, OUString(get+rLocale.Language+OUString('_')+rAlgorithm).pData);
95 #else
96 if ( rLocale.Language == "zh" && OUString( "TW HK MO" ).indexOf(rLocale.Country) >= 0 ) {
97 if ( rAlgorithm == "radical" )
98 func = get_indexdata_zh_TW_radical;
99 else if ( rAlgorithm == "stroke" )
100 func = get_indexdata_zh_TW_stroke;
102 if (!func) {
103 if ( rLocale.Language == "ko" ) {
104 if ( rAlgorithm == "dict" )
105 func = get_indexdata_ko_dict;
106 } else if ( rLocale.Language == "zh" ) {
107 if ( rAlgorithm == "pinyin" )
108 func = get_indexdata_zh_pinyin;
109 else if ( rAlgorithm == "radical" )
110 func = get_indexdata_zh_radical;
111 else if ( rAlgorithm == "stroke" )
112 func = get_indexdata_zh_stroke;
113 else if ( rAlgorithm == "zhuyin" )
114 func = get_indexdata_zh_zhuyin;
117 #endif
118 if (func) {
119 sal_Int16 max_index;
120 sal_uInt16** idx=func(&max_index);
121 if (((sal_Int16)(ch >> 8)) <= max_index) {
122 sal_uInt16 address=idx[0][ch >> 8];
123 if (address != 0xFFFF) {
124 address=idx[1][address+(ch & 0xFF)];
125 return idx[2] ? OUString(&idx[2][address]) : OUString(address);
130 // using alphanumeric index for non-define stirng
131 return OUString(&idxStr[(ch & 0xFFFFFF00) ? 0 : ch], 1);
134 OUString SAL_CALL
135 IndexEntrySupplier_asian::getIndexKey( const OUString& rIndexEntry,
136 const OUString& rPhoneticEntry, const Locale& rLocale) throw (RuntimeException)
138 return getIndexCharacter(getEntry(rIndexEntry, rPhoneticEntry, rLocale), rLocale, aAlgorithm);
141 sal_Int16 SAL_CALL
142 IndexEntrySupplier_asian::compareIndexEntry(
143 const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1,
144 const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 )
145 throw (RuntimeException)
147 sal_Int32 result = collator->compareString(getEntry(rIndexEntry1, rPhoneticEntry1, rLocale1),
148 getEntry(rIndexEntry2, rPhoneticEntry2, rLocale2));
150 // equivalent of phonetic entries does not mean equivalent of index entries.
151 // we have to continue comparing index entry here.
152 if (result == 0 && usePhonetic && !rPhoneticEntry1.isEmpty() &&
153 rLocale1.Language == rLocale2.Language && rLocale1.Country == rLocale2.Country &&
154 rLocale1.Variant == rLocale2.Variant)
155 result = collator->compareString(rIndexEntry1, rIndexEntry2);
156 return sal::static_int_cast< sal_Int16 >(result); // result in { -1, 0, 1 }
159 OUString SAL_CALL
160 IndexEntrySupplier_asian::getPhoneticCandidate( const OUString& rIndexEntry,
161 const Locale& rLocale ) throw (RuntimeException)
163 sal_uInt16 **(*func)(sal_Int16*)=NULL;
164 #ifndef DISABLE_DYNLOADING
165 if (hModule) {
166 const sal_Char *func_name=NULL;
167 if ( rLocale.Language == "zh" )
168 func_name=(OUString("TW HK MO").indexOf(rLocale.Country) >= 0) ? "get_zh_zhuyin" : "get_zh_pinyin";
169 else if ( rLocale.Language == "ko" )
170 func_name="get_ko_phonetic";
171 if (func_name)
172 func=(sal_uInt16 **(*)(sal_Int16*))osl_getFunctionSymbol(hModule, OUString::createFromAscii(func_name).pData);
174 #else
175 if ( rLocale.Language == "zh" )
176 func = (OUString("TW HK MO").indexOf(rLocale.Country) >= 0) ? get_zh_zhuyin : get_zh_pinyin;
177 else if ( rLocale.Language == "ko" )
178 func = get_ko_phonetic;
180 #endif
181 if (func) {
182 OUStringBuffer candidate;
183 sal_Int16 max_index;
184 sal_uInt16** idx=func(&max_index);
185 for (sal_Int32 i=0,j=0; i < rIndexEntry.getLength(); i=j) {
186 sal_uInt32 ch = rIndexEntry.iterateCodePoints(&j, 1);
187 if (((sal_Int16)(ch>>8)) <= max_index) {
188 sal_uInt16 address = idx[0][ch>>8];
189 if (address != 0xFFFF) {
190 address = idx[1][address + (ch & 0xFF)];
191 if ( i > 0 && rLocale.Language == "zh" )
192 candidate.appendAscii(" ");
193 if (idx[2])
194 candidate.append(&idx[2][address]);
195 else
196 candidate.append(address);
197 } else
198 candidate.appendAscii(" ");
201 return candidate.makeStringAndClear();
203 return OUString();
205 } } } }
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */