1449ab281SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3449ab281SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4449ab281SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5449ab281SAndrew Rist  * distributed with this work for additional information
6449ab281SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7449ab281SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8449ab281SAndrew Rist  * "License"); you may not use this file except in compliance
9449ab281SAndrew Rist  * with the License.  You may obtain a copy of the License at
10449ab281SAndrew Rist  *
11449ab281SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12449ab281SAndrew Rist  *
13449ab281SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14449ab281SAndrew Rist  * software distributed under the License is distributed on an
15449ab281SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16449ab281SAndrew Rist  * KIND, either express or implied.  See the License for the
17449ab281SAndrew Rist  * specific language governing permissions and limitations
18449ab281SAndrew Rist  * under the License.
19449ab281SAndrew Rist  *
20449ab281SAndrew Rist  *************************************************************/
21449ab281SAndrew Rist 
22449ab281SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_i18npool.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
28cdf0e10cSrcweir #include <indexentrysupplier_asian.hxx>
29cdf0e10cSrcweir #include <data/indexdata_alphanumeric.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using namespace ::com::sun::star::uno;
32cdf0e10cSrcweir using namespace ::com::sun::star::lang;
33cdf0e10cSrcweir using namespace ::rtl;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace com { namespace sun { namespace star { namespace i18n {
36cdf0e10cSrcweir 
thisModule()37cdf0e10cSrcweir extern "C" { static void SAL_CALL thisModule() {} }
38cdf0e10cSrcweir 
IndexEntrySupplier_asian(const Reference<XMultiServiceFactory> & rxMSF)39cdf0e10cSrcweir IndexEntrySupplier_asian::IndexEntrySupplier_asian(
40cdf0e10cSrcweir     const Reference < XMultiServiceFactory >& rxMSF ) : IndexEntrySupplier_Common(rxMSF)
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 	implementationName = "com.sun.star.i18n.IndexEntrySupplier_asian";
43cdf0e10cSrcweir #ifdef SAL_DLLPREFIX
44*6f51c329SHerbert Dürr     OUString lib=OUString::createFromAscii( SAL_DLLPREFIX "index_data" SAL_DLLEXTENSION);
45cdf0e10cSrcweir #else
46*6f51c329SHerbert Dürr     OUString lib=OUString::createFromAscii("index_data" SAL_DLLEXTENSION);
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir     hModule = osl_loadModuleRelative(
49cdf0e10cSrcweir         &thisModule, lib.pData, SAL_LOADMODULE_DEFAULT );
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
~IndexEntrySupplier_asian()52cdf0e10cSrcweir IndexEntrySupplier_asian::~IndexEntrySupplier_asian()
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     if (hModule) osl_unloadModule(hModule);
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
57cdf0e10cSrcweir OUString SAL_CALL
getIndexCharacter(const OUString & rIndexEntry,const Locale & rLocale,const OUString & rAlgorithm)58cdf0e10cSrcweir IndexEntrySupplier_asian::getIndexCharacter( const OUString& rIndexEntry,
59cdf0e10cSrcweir     const Locale& rLocale, const OUString& rAlgorithm ) throw (RuntimeException)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     sal_Int32 i=0;
62cdf0e10cSrcweir     sal_uInt32 ch = rIndexEntry.iterateCodePoints(&i, 0);
63cdf0e10cSrcweir     if (hModule) {
64cdf0e10cSrcweir         OUString get=OUString::createFromAscii("get_indexdata_");
65cdf0e10cSrcweir         sal_uInt16** (*func)(sal_Int16*)=NULL;
66cdf0e10cSrcweir         if (rLocale.Language.equalsAscii("zh") && OUString::createFromAscii("TW HK MO").indexOf(rLocale.Country) >= 0)
67cdf0e10cSrcweir             func=(sal_uInt16** (*)(sal_Int16*))osl_getFunctionSymbol(hModule, (get+rLocale.Language+OUString::createFromAscii("_TW_")+rAlgorithm).pData);
68cdf0e10cSrcweir         if (!func)
69cdf0e10cSrcweir             func=(sal_uInt16** (*)(sal_Int16*))osl_getFunctionSymbol(hModule, (get+rLocale.Language+OUString('_')+rAlgorithm).pData);
70cdf0e10cSrcweir         if (func) {
71cdf0e10cSrcweir             sal_Int16 max_index;
72cdf0e10cSrcweir             sal_uInt16** idx=func(&max_index);
73cdf0e10cSrcweir             if (((sal_Int16)(ch >> 8)) <= max_index) {
74cdf0e10cSrcweir                 sal_uInt16 address=idx[0][ch >> 8];
75cdf0e10cSrcweir                 if (address != 0xFFFF) {
76cdf0e10cSrcweir                     address=idx[1][address+(ch & 0xFF)];
77cdf0e10cSrcweir                     return idx[2] ? OUString(&idx[2][address]) : OUString(address);
78cdf0e10cSrcweir                 }
79cdf0e10cSrcweir             }
80cdf0e10cSrcweir         }
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir     // using alphanumeric index for non-define stirng
83cdf0e10cSrcweir     return OUString(&idxStr[(ch & 0xFFFFFF00) ? 0 : ch], 1);
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir OUString SAL_CALL
getIndexKey(const OUString & rIndexEntry,const OUString & rPhoneticEntry,const Locale & rLocale)87cdf0e10cSrcweir IndexEntrySupplier_asian::getIndexKey( const OUString& rIndexEntry,
88cdf0e10cSrcweir     const OUString& rPhoneticEntry, const Locale& rLocale) throw (RuntimeException)
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     return getIndexCharacter(getEntry(rIndexEntry, rPhoneticEntry, rLocale), rLocale, aAlgorithm);
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir sal_Int16 SAL_CALL
compareIndexEntry(const OUString & rIndexEntry1,const OUString & rPhoneticEntry1,const Locale & rLocale1,const OUString & rIndexEntry2,const OUString & rPhoneticEntry2,const Locale & rLocale2)94cdf0e10cSrcweir IndexEntrySupplier_asian::compareIndexEntry(
95cdf0e10cSrcweir 	const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1,
96cdf0e10cSrcweir 	const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 )
97cdf0e10cSrcweir 	throw (RuntimeException)
98cdf0e10cSrcweir {
99cdf0e10cSrcweir     sal_Int32 result = collator->compareString(getEntry(rIndexEntry1, rPhoneticEntry1, rLocale1),
100cdf0e10cSrcweir                                     getEntry(rIndexEntry2, rPhoneticEntry2, rLocale2));
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     // equivalent of phonetic entries does not mean equivalent of index entries.
103cdf0e10cSrcweir     // we have to continue comparing index entry here.
104cdf0e10cSrcweir     if (result == 0 && usePhonetic && rPhoneticEntry1.getLength() > 0 &&
105cdf0e10cSrcweir             rLocale1.Language == rLocale2.Language && rLocale1.Country == rLocale2.Country &&
106cdf0e10cSrcweir             rLocale1.Variant == rLocale2.Variant)
107cdf0e10cSrcweir         result = collator->compareString(rIndexEntry1, rIndexEntry2);
108cdf0e10cSrcweir     return sal::static_int_cast< sal_Int16 >(result); // result in { -1, 0, 1 }
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir OUString SAL_CALL
getPhoneticCandidate(const OUString & rIndexEntry,const Locale & rLocale)112cdf0e10cSrcweir IndexEntrySupplier_asian::getPhoneticCandidate( const OUString& rIndexEntry,
113cdf0e10cSrcweir         const Locale& rLocale ) throw (RuntimeException)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir     if (hModule) {
116cdf0e10cSrcweir         sal_uInt16 **(*func)(sal_Int16*)=NULL;
117cdf0e10cSrcweir         const sal_Char *func_name=NULL;
118cdf0e10cSrcweir         if (rLocale.Language.equalsAscii("zh"))
119cdf0e10cSrcweir             func_name=(OUString::createFromAscii("TW HK MO").indexOf(rLocale.Country) >= 0) ?  "get_zh_zhuyin" : "get_zh_pinyin";
120cdf0e10cSrcweir         else if (rLocale.Language.equalsAscii("ko"))
121cdf0e10cSrcweir             func_name="get_ko_phonetic";
122cdf0e10cSrcweir         if (func_name)
123cdf0e10cSrcweir             func=(sal_uInt16 **(*)(sal_Int16*))osl_getFunctionSymbol(hModule, OUString::createFromAscii(func_name).pData);
124cdf0e10cSrcweir         if (func) {
125cdf0e10cSrcweir             OUStringBuffer candidate;
126cdf0e10cSrcweir             sal_Int16 max_index;
127cdf0e10cSrcweir             sal_uInt16** idx=func(&max_index);
128cdf0e10cSrcweir             OUString aIndexEntry=rIndexEntry;
129cdf0e10cSrcweir             for (sal_Int32 i=0,j=0; i < rIndexEntry.getLength(); i=j) {
130cdf0e10cSrcweir                 sal_uInt32 ch = rIndexEntry.iterateCodePoints(&j, 1);
131cdf0e10cSrcweir                 if (((sal_Int16)(ch>>8)) <= max_index) {
132cdf0e10cSrcweir                     sal_uInt16 address = idx[0][ch>>8];
133cdf0e10cSrcweir                     if (address != 0xFFFF) {
134cdf0e10cSrcweir                         address = idx[1][address + (ch & 0xFF)];
135cdf0e10cSrcweir                         if (i > 0 && rLocale.Language.equalsAscii("zh"))
136cdf0e10cSrcweir                             candidate.appendAscii(" ");
137cdf0e10cSrcweir                         if (idx[2])
138cdf0e10cSrcweir                             candidate.append(&idx[2][address]);
139cdf0e10cSrcweir                         else
140cdf0e10cSrcweir                             candidate.append(address);
141cdf0e10cSrcweir                     } else
142cdf0e10cSrcweir                         candidate.appendAscii(" ");
143cdf0e10cSrcweir                 }
144cdf0e10cSrcweir             }
145cdf0e10cSrcweir             return candidate.makeStringAndClear();
146cdf0e10cSrcweir         }
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir     return OUString();
149cdf0e10cSrcweir }
150cdf0e10cSrcweir } } } }
151