LCOV - code coverage report
Current view: top level - libreoffice/i18npool/source/collator - collator_unicode.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 29 71 40.8 %
Date: 2012-12-27 Functions: 5 10 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : 
      21             : // generated list of languages
      22             : #include "lrl_include.hxx"
      23             : 
      24             : #include <rtl/ustrbuf.hxx>
      25             : #include <collator_unicode.hxx>
      26             : #include <localedata.hxx>
      27             : #include <com/sun/star/i18n/CollatorOptions.hpp>
      28             : 
      29             : using namespace ::com::sun::star;
      30             : using namespace ::com::sun::star::lang;
      31             : using namespace ::com::sun::star::uno;
      32             : using namespace ::rtl;
      33             : 
      34             : namespace com { namespace sun { namespace star { namespace i18n {
      35             : 
      36           8 : Collator_Unicode::Collator_Unicode()
      37             : {
      38           8 :     implementationName = "com.sun.star.i18n.Collator_Unicode";
      39           8 :     collator = NULL;
      40           8 :     uca_base = NULL;
      41             : #ifndef DISABLE_DYNLOADING
      42           8 :     hModule = NULL;
      43             : #endif
      44           8 : }
      45             : 
      46           9 : Collator_Unicode::~Collator_Unicode()
      47             : {
      48           3 :     if (collator) delete collator;
      49           3 :     if (uca_base) delete uca_base;
      50             : #ifndef DISABLE_DYNLOADING
      51           3 :     if (hModule) osl_unloadModule(hModule);
      52             : #endif
      53           6 : }
      54             : 
      55             : #ifdef DISABLE_DYNLOADING
      56             : 
      57             : extern "C" {
      58             : 
      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.
      63             : 
      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();
      83             : 
      84             : }
      85             : 
      86             : #endif
      87             : 
      88             : sal_Int32 SAL_CALL
      89           0 : Collator_Unicode::compareSubstring( const OUString& str1, sal_Int32 off1, sal_Int32 len1,
      90             :     const OUString& str2, sal_Int32 off2, sal_Int32 len2) throw(RuntimeException)
      91             : {
      92           0 :     return collator->compare(reinterpret_cast<const UChar *>(str1.getStr()) + off1, len1, reinterpret_cast<const UChar *>(str2.getStr()) + off2, len2); // UChar != sal_Unicode in MinGW
      93             : }
      94             : 
      95             : sal_Int32 SAL_CALL
      96        3339 : Collator_Unicode::compareString( const OUString& str1, const OUString& str2) throw(RuntimeException)
      97             : {
      98        3339 :     return collator->compare(reinterpret_cast<const UChar *>(str1.getStr()), reinterpret_cast<const UChar *>(str2.getStr()));   // UChar != sal_Unicode in MinGW
      99             : }
     100             : 
     101             : #ifndef DISABLE_DYNLOADING
     102             : 
     103           0 : extern "C" { static void SAL_CALL thisModule() {} }
     104             : 
     105             : #endif
     106             : 
     107             : sal_Int32 SAL_CALL
     108           8 : Collator_Unicode::loadCollatorAlgorithm(const OUString& rAlgorithm, const lang::Locale& rLocale, sal_Int32 options)
     109             :     throw(RuntimeException)
     110             : {
     111           8 :     if (!collator) {
     112           8 :         UErrorCode status = U_ZERO_ERROR;
     113           8 :         OUString rule = LocaleData().getCollatorRuleByAlgorithm(rLocale, rAlgorithm);
     114           8 :         if (!rule.isEmpty()) {
     115           0 :             collator = new RuleBasedCollator(reinterpret_cast<const UChar *>(rule.getStr()), status);   // UChar != sal_Unicode in MinGW
     116           0 :             if (! U_SUCCESS(status)) throw RuntimeException();
     117             :         }
     118           8 :         if (!collator && OUString::createFromAscii(LOCAL_RULE_LANGS).indexOf(rLocale.Language) >= 0) {
     119           0 :             const sal_uInt8* (*func)() = NULL;
     120             : 
     121             : #ifndef DISABLE_DYNLOADING
     122           0 :             OUStringBuffer aBuf;
     123             : #ifdef SAL_DLLPREFIX
     124           0 :             aBuf.appendAscii(SAL_DLLPREFIX);
     125             : #endif
     126           0 :             aBuf.appendAscii( "collator_data" ).appendAscii( SAL_DLLEXTENSION );
     127           0 :             hModule = osl_loadModuleRelative( &thisModule, aBuf.makeStringAndClear().pData, SAL_LOADMODULE_DEFAULT );
     128           0 :             if (hModule) {
     129           0 :                 aBuf.appendAscii("get_").append(rLocale.Language).appendAscii("_");
     130           0 :                 if ( rLocale.Language == "zh" ) {
     131           0 :                     OUString func_base = aBuf.makeStringAndClear();
     132           0 :                     if (OUString("TW HK MO").indexOf(rLocale.Country) >= 0)
     133             :                         func=(const sal_uInt8* (*)()) osl_getFunctionSymbol(hModule,
     134           0 :                                     OUString(func_base + "TW_" + rAlgorithm).pData);
     135           0 :                     if (!func)
     136           0 :                         func=(const sal_uInt8* (*)()) osl_getFunctionSymbol(hModule, OUString(func_base + rAlgorithm).pData);
     137             :                 } else {
     138           0 :                     if ( rLocale.Language == "ja" ) {
     139             :                         // replace algrithm name to implementation name.
     140           0 :                         if (rAlgorithm.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("phonetic (alphanumeric first)")) )
     141           0 :                             aBuf.appendAscii("phonetic_alphanumeric_first");
     142           0 :                         else if (rAlgorithm.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("phonetic (alphanumeric last)")))
     143           0 :                             aBuf.appendAscii("phonetic_alphanumeric_last");
     144             :                         else
     145           0 :                             aBuf.append(rAlgorithm);
     146             :                     } else {
     147           0 :                         aBuf.append(rAlgorithm);
     148             :                     }
     149           0 :                     func=(const sal_uInt8* (*)()) osl_getFunctionSymbol(hModule, aBuf.makeStringAndClear().pData);
     150             :                 }
     151             :             }
     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;
     202             :             }
     203             : #endif
     204           0 :             if (func) {
     205           0 :                 const sal_uInt8* ruleImage=func();
     206           0 :                 uca_base = new RuleBasedCollator(static_cast<UChar*>(NULL), status);
     207           0 :                 if (! U_SUCCESS(status)) throw RuntimeException();
     208           0 :                 collator = new RuleBasedCollator(reinterpret_cast<const uint8_t*>(ruleImage), -1, uca_base, status);
     209           0 :                 if (! U_SUCCESS(status)) throw RuntimeException();
     210           0 :             }
     211             :         }
     212           8 :         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.
     218             :             */
     219             :             icu::Locale icuLocale(
     220             :                    OUStringToOString(rLocale.Language, RTL_TEXTENCODING_ASCII_US).getStr(),
     221             :                    OUStringToOString(rLocale.Country, RTL_TEXTENCODING_ASCII_US).getStr(),
     222           8 :                    OUStringToOString(rAlgorithm, RTL_TEXTENCODING_ASCII_US).getStr());
     223             :             // load ICU collator
     224           8 :             collator = (RuleBasedCollator*) icu::Collator::createInstance(icuLocale, status);
     225           8 :             if (! U_SUCCESS(status)) throw RuntimeException();
     226           8 :         }
     227             :     }
     228             : 
     229           8 :     if (options & CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT)
     230           0 :         collator->setStrength(Collator::PRIMARY);
     231           8 :     else if (options & CollatorOptions::CollatorOptions_IGNORE_CASE)
     232           2 :         collator->setStrength(Collator::SECONDARY);
     233             :     else
     234           6 :         collator->setStrength(Collator::TERTIARY);
     235             : 
     236           8 :     return(0);
     237             : }
     238             : 
     239             : 
     240             : OUString SAL_CALL
     241           0 : Collator_Unicode::getImplementationName() throw( RuntimeException )
     242             : {
     243           0 :     return OUString::createFromAscii(implementationName);
     244             : }
     245             : 
     246             : sal_Bool SAL_CALL
     247           0 : Collator_Unicode::supportsService(const rtl::OUString& rServiceName) throw( RuntimeException )
     248             : {
     249           0 :     return !rServiceName.compareToAscii(implementationName);
     250             : }
     251             : 
     252             : Sequence< OUString > SAL_CALL
     253           0 : Collator_Unicode::getSupportedServiceNames() throw( RuntimeException )
     254             : {
     255           0 :     Sequence< OUString > aRet(1);
     256           0 :     aRet[0] = OUString::createFromAscii(implementationName);
     257           0 :     return aRet;
     258             : }
     259             : 
     260             : } } } }
     261             : 
     262             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10