Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / i18npool / source / indexentry / indexentrysupplier_default.cxx
blobeba5beed2c33876020421fb372d09f5ee0eaf59f
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 .
20 #include <indexentrysupplier_default.hxx>
21 #include <localedata.hxx>
22 #include <i18nutil/unicode.hxx>
23 #include <com/sun/star/i18n/CollatorOptions.hpp>
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 IndexEntrySupplier_Unicode::IndexEntrySupplier_Unicode(
32 const com::sun::star::uno::Reference < com::sun::star::uno::XComponentContext >& rxContext ) :
33 IndexEntrySupplier_Common(rxContext)
35 implementationName = "com.sun.star.i18n.IndexEntrySupplier_Unicode";
36 index = new Index(rxContext);
39 IndexEntrySupplier_Unicode::~IndexEntrySupplier_Unicode()
41 delete index;
44 sal_Bool SAL_CALL IndexEntrySupplier_Unicode::loadAlgorithm( const lang::Locale& rLocale,
45 const OUString& rAlgorithm, sal_Int32 collatorOptions ) throw (RuntimeException)
47 index->init(rLocale, rAlgorithm);
48 return IndexEntrySupplier_Common::loadAlgorithm(rLocale, rAlgorithm, collatorOptions);
51 OUString SAL_CALL IndexEntrySupplier_Unicode::getIndexKey( const OUString& rIndexEntry,
52 const OUString& rPhoneticEntry, const lang::Locale& rLocale ) throw (RuntimeException)
54 return index->getIndexDescription(getEntry(rIndexEntry, rPhoneticEntry, rLocale));
57 sal_Int16 SAL_CALL IndexEntrySupplier_Unicode::compareIndexEntry(
58 const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const lang::Locale& rLocale1,
59 const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const lang::Locale& rLocale2 )
60 throw (RuntimeException)
62 sal_Int16 result =
63 index->getIndexWeight(getEntry(rIndexEntry1, rPhoneticEntry1, rLocale1)) -
64 index->getIndexWeight(getEntry(rIndexEntry2, rPhoneticEntry2, rLocale2));
65 if (result == 0)
66 return IndexEntrySupplier_Common::compareIndexEntry(
67 rIndexEntry1, rPhoneticEntry1, rLocale1,
68 rIndexEntry2, rPhoneticEntry2, rLocale2);
69 return result > 0 ? 1 : -1;
72 OUString SAL_CALL IndexEntrySupplier_Unicode::getIndexCharacter( const OUString& rIndexEntry,
73 const lang::Locale& rLocale, const OUString& rAlgorithm ) throw (RuntimeException) {
75 if (loadAlgorithm( rLocale, rAlgorithm, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT))
76 return index->getIndexDescription(rIndexEntry);
77 else
78 return IndexEntrySupplier_Common::getIndexCharacter(rIndexEntry, rLocale, rAlgorithm);
81 IndexTable::IndexTable()
83 table = NULL;
86 IndexTable::~IndexTable()
88 if (table) free(table);
91 void IndexTable::init(sal_Unicode start_, sal_Unicode end_, IndexKey *keys, sal_Int16 key_count, Index *index)
93 start=start_;
94 end=end_;
95 table = (sal_uInt8*) malloc((end-start+1)*sizeof(sal_uInt8));
96 for (sal_Unicode i = start; i <= end; i++) {
97 sal_Int16 j;
98 for (j = 0; j < key_count; j++) {
99 if (keys[j].key > 0 && (i == keys[j].key || index->compare(i, keys[j].key) == 0)) {
100 table[i-start] = sal::static_int_cast<sal_uInt8>(j);
101 break;
104 if (j == key_count)
105 table[i-start] = 0xFF;
109 Index::Index(const com::sun::star::uno::Reference < com::sun::star::uno::XComponentContext >& rxContext)
111 collator = new CollatorImpl(rxContext);
114 Index::~Index()
116 delete collator;
119 sal_Int16 Index::compare(sal_Unicode c1, sal_Unicode c2)
121 return sal::static_int_cast<sal_Int16>( collator->compareString(OUString(&c1, 1), OUString(&c2, 1)) );
124 sal_Int16 Index::getIndexWeight(const OUString& rIndexEntry)
126 sal_Int32 startPos=0;
127 if (!skipping_chars.isEmpty())
128 while (skipping_chars.indexOf(rIndexEntry[startPos]) >= 0)
129 startPos++;
130 if (mkey_count > 0) {
131 for (sal_Int16 i = 0; i < mkey_count; i++) {
132 sal_Int32 len = keys[mkeys[i]].mkey.getLength();
133 if (collator->compareSubstring(rIndexEntry, startPos, len,
134 keys[mkeys[i]].mkey, 0, len) == 0)
135 return mkeys[i];
138 sal_Unicode code = rIndexEntry[startPos];
139 for (sal_Int16 i = 0; i < table_count; i++) {
140 if (tables[i].start <= code && code <= tables[i].end)
141 return tables[i].table[code-tables[i].start];
143 return 0xFF;
146 OUString Index::getIndexDescription(const OUString& rIndexEntry)
148 sal_Int16 wgt = getIndexWeight(rIndexEntry);
149 if (wgt < MAX_KEYS) {
150 if (!keys[wgt].desc.isEmpty())
151 return keys[wgt].desc;
152 else if (keys[wgt].key > 0)
153 return OUString(&keys[wgt].key, 1);
154 else
155 return keys[wgt].mkey;
157 sal_Int32 nPos=0;
158 sal_uInt32 indexChar=rIndexEntry.iterateCodePoints(&nPos, 0);
159 return OUString(&indexChar, 1);
162 #define LOCALE_EN lang::Locale(OUString("en"), OUString(), OUString())
164 void Index::makeIndexKeys(const lang::Locale &rLocale, const OUString &algorithm) throw (RuntimeException)
166 OUString keyStr = LocaleData().getIndexKeysByAlgorithm(rLocale, algorithm);
168 if (keyStr.isEmpty()) {
169 keyStr = LocaleData().getIndexKeysByAlgorithm(LOCALE_EN,
170 LocaleData().getDefaultIndexAlgorithm(LOCALE_EN));
171 if (keyStr.isEmpty())
172 throw RuntimeException();
175 sal_Int16 len = sal::static_int_cast<sal_Int16>( keyStr.getLength() );
176 mkey_count=key_count=0;
177 skipping_chars=OUString();
178 sal_Int16 i, j;
180 for (i = 0; i < len && key_count < MAX_KEYS; i++)
182 sal_Unicode curr = keyStr[i];
183 sal_Unicode close = sal_Unicode(')');
185 if (unicode::isWhiteSpace(curr))
186 continue;
188 switch(curr) {
189 case sal_Unicode('-'):
190 if (key_count > 0 && i + 1 < len ) {
191 for (curr = keyStr[++i]; key_count < MAX_KEYS && keys[key_count-1].key < curr; key_count++) {
192 keys[key_count].key = keys[key_count-1].key+1;
193 keys[key_count].desc = OUString();
195 } else
196 throw RuntimeException();
197 break;
198 case sal_Unicode('['):
199 for (i++; i < len && keyStr[i] != sal_Unicode(']'); i++) {
200 if (unicode::isWhiteSpace(keyStr[i])) {
201 continue;
202 } else if (keyStr[i] == sal_Unicode('_')) {
203 for (curr=keyStr[i-1]+1; curr <= keyStr[i+1]; curr++)
204 skipping_chars+=OUString(curr);
205 i+=2;
206 } else {
207 skipping_chars+=OUString(keyStr[i]);
210 break;
211 case sal_Unicode('{'):
212 close = sal_Unicode('}');
213 case sal_Unicode('('):
214 if (key_count > 0) {
215 sal_Int16 end = i+1;
216 for (end=i+1; end < len && keyStr[end] != close; end++) ;
218 if (end >= len) // no found
219 throw RuntimeException();
220 if (close == sal_Unicode(')'))
221 keys[key_count-1].desc = keyStr.copy(i+1, end-i-1);
222 else {
223 mkeys[mkey_count++]=key_count;
224 keys[key_count].key = 0;
225 keys[key_count].mkey = keyStr.copy(i+1, end-i-1);
226 keys[key_count++].desc=OUString();
228 i=end+1;
229 } else
230 throw RuntimeException();
231 break;
232 default:
233 keys[key_count].key = curr;
234 keys[key_count++].desc = OUString();
235 break;
238 for (i = 0; i < mkey_count; i++) {
239 for (j=i+1; j < mkey_count; j++) {
240 if (keys[mkeys[i]].mkey.getLength() < keys[mkeys[j]].mkey.getLength()) {
241 sal_Int16 k = mkeys[i];
242 mkeys[i] = mkeys[j];
243 mkeys[j] = k;
249 void Index::init(const lang::Locale &rLocale, const OUString& algorithm) throw (RuntimeException)
251 makeIndexKeys(rLocale, algorithm);
253 Sequence< UnicodeScript > scriptList = LocaleData().getUnicodeScripts( rLocale );
255 if (scriptList.getLength() == 0) {
256 scriptList = LocaleData().getUnicodeScripts(LOCALE_EN);
257 if (scriptList.getLength() == 0)
258 throw RuntimeException();
261 table_count = sal::static_int_cast<sal_Int16>( scriptList.getLength() );
262 if (table_count > MAX_TABLES)
263 throw RuntimeException();
265 collator->loadCollatorAlgorithm(algorithm, rLocale, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT);
266 sal_Int16 j=0;
267 sal_Unicode start = unicode::getUnicodeScriptStart((UnicodeScript)0);
268 sal_Unicode end = unicode::getUnicodeScriptEnd((UnicodeScript)0);
269 for (sal_Int16 i= (scriptList[0] == (UnicodeScript)0) ? 1 : 0; i< scriptList.getLength(); i++) {
270 if (unicode::getUnicodeScriptStart(scriptList[i]) != end+1) {
271 tables[j++].init(start, end, keys, key_count, this);
272 start = unicode::getUnicodeScriptStart(scriptList[i]);
274 end = unicode::getUnicodeScriptEnd(scriptList[i]);
276 tables[j++].init(start, end, keys, key_count, this);
277 table_count = j;
280 } } } }
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */