LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/lingucomponent/source/lingutil - lingutil.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 79 94 84.0 %
Date: 2013-07-09 Functions: 3 3 100.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             : #if defined(WNT)
      22             : #include <windows.h>
      23             : #endif
      24             : 
      25             : #include <osl/thread.h>
      26             : #include <osl/file.hxx>
      27             : #include <tools/debug.hxx>
      28             : #include <tools/urlobj.hxx>
      29             : #include <i18nlangtag/languagetag.hxx>
      30             : #include <i18nlangtag/mslangid.hxx>
      31             : #include <unotools/lingucfg.hxx>
      32             : #include <unotools/pathoptions.hxx>
      33             : #include <rtl/ustring.hxx>
      34             : #include <rtl/string.hxx>
      35             : #include <rtl/tencinfo.h>
      36             : #include <linguistic/misc.hxx>
      37             : 
      38             : #include <set>
      39             : #include <vector>
      40             : #include <string.h>
      41             : 
      42             : #include <lingutil.hxx>
      43             : 
      44             : #include <sal/macros.h>
      45             : 
      46             : 
      47             : using ::com::sun::star::lang::Locale;
      48             : using namespace ::com::sun::star;
      49             : 
      50             : #if defined(WNT)
      51             : OString Win_GetShortPathName( const OUString &rLongPathName )
      52             : {
      53             :     OString aRes;
      54             : 
      55             :     sal_Unicode aShortBuffer[1024] = {0};
      56             :     sal_Int32   nShortBufSize = SAL_N_ELEMENTS( aShortBuffer );
      57             : 
      58             :     // use the version of 'GetShortPathName' that can deal with Unicode...
      59             :     sal_Int32 nShortLen = GetShortPathNameW(
      60             :             reinterpret_cast<LPCWSTR>( rLongPathName.getStr() ),
      61             :             reinterpret_cast<LPWSTR>( aShortBuffer ),
      62             :             nShortBufSize );
      63             : 
      64             :     if (nShortLen < nShortBufSize) // conversion successful?
      65             :         aRes = OString( OU2ENC( OUString( aShortBuffer, nShortLen ), osl_getThreadTextEncoding()) );
      66             :     else
      67             :         OSL_FAIL( "Win_GetShortPathName: buffer to short" );
      68             : 
      69             :     return aRes;
      70             : }
      71             : #endif //defined(WNT)
      72             : 
      73             : //////////////////////////////////////////////////////////////////////
      74             : 
      75             : // build list of old style diuctionaries (not as extensions) to use.
      76             : // User installed dictionaries (the ones residing in the user paths)
      77             : // will get precedence over system installed ones for the same language.
      78          90 : std::vector< SvtLinguConfigDictionaryEntry > GetOldStyleDics( const char *pDicType )
      79             : {
      80          90 :     std::vector< SvtLinguConfigDictionaryEntry > aRes;
      81             : 
      82          90 :     if (!pDicType)
      83           0 :         return aRes;
      84             : 
      85         180 :     OUString aFormatName;
      86         180 :     String aDicExtension;
      87             : #ifdef SYSTEM_DICTS
      88         180 :     OUString aSystemDir;
      89         180 :     OUString aSystemPrefix;
      90         180 :     OUString aSystemSuffix;
      91             : #endif
      92          90 :     if (strcmp( pDicType, "DICT" ) == 0)
      93             :     {
      94          22 :         aFormatName     = "DICT_SPELL";
      95          22 :         aDicExtension   = ".dic";
      96             : #ifdef SYSTEM_DICTS
      97          22 :         aSystemDir      = DICT_SYSTEM_DIR;
      98          22 :         aSystemSuffix   = aDicExtension;
      99             : #endif
     100             :     }
     101          68 :     else if (strcmp( pDicType, "HYPH" ) == 0)
     102             :     {
     103          22 :         aFormatName     = "DICT_HYPH";
     104          22 :         aDicExtension   = ".dic";
     105             : #ifdef SYSTEM_DICTS
     106          22 :         aSystemDir      = HYPH_SYSTEM_DIR;
     107          22 :         aSystemPrefix   = "hyph_";
     108          22 :         aSystemSuffix   = aDicExtension;
     109             : #endif
     110             :     }
     111          46 :     else if (strcmp( pDicType, "THES" ) == 0)
     112             :     {
     113          46 :         aFormatName     = "DICT_THES";
     114          46 :         aDicExtension   = ".dat";
     115             : #ifdef SYSTEM_DICTS
     116          46 :         aSystemDir      = THES_SYSTEM_DIR;
     117          46 :         aSystemPrefix   = "th_";
     118          46 :         aSystemSuffix   = "_v2.dat";
     119             : #endif
     120             :     }
     121             : 
     122             : 
     123          90 :     if (aFormatName.isEmpty() || aDicExtension.Len() == 0)
     124           0 :         return aRes;
     125             : 
     126             : #ifdef SYSTEM_DICTS
     127         180 :     osl::Directory aSystemDicts(aSystemDir);
     128          90 :     if (aSystemDicts.open() == osl::FileBase::E_None)
     129             :     {
     130             :         // set of languages to remember the language where it is already
     131             :         // decided to make use of the dictionary.
     132          90 :         std::set< OUString > aDicLangInUse;
     133             : 
     134         180 :         osl::DirectoryItem aItem;
     135         180 :         osl::FileStatus aFileStatus(osl_FileStatus_Mask_FileURL);
     136         730 :         while (aSystemDicts.getNextItem(aItem) == osl::FileBase::E_None)
     137             :         {
     138         550 :             aItem.getFileStatus(aFileStatus);
     139         550 :             OUString sPath = aFileStatus.getFileURL();
     140         550 :             if (sPath.lastIndexOf(aSystemSuffix) == sPath.getLength()-aSystemSuffix.getLength())
     141             :             {
     142         528 :                 sal_Int32 nStartIndex = sPath.lastIndexOf(sal_Unicode('/')) + 1;
     143         528 :                 if (!sPath.match(aSystemPrefix, nStartIndex))
     144           0 :                     continue;
     145         528 :                 OUString sChunk = sPath.copy(nStartIndex + aSystemPrefix.getLength(),
     146         528 :                     sPath.getLength() - aSystemSuffix.getLength() -
     147        1056 :                     nStartIndex - aSystemPrefix.getLength());
     148         528 :                 if (sChunk.isEmpty())
     149           0 :                     continue;
     150             :                 // We prefer (now) to use language tags.
     151             :                 // Avoid feeding in the older LANG_REGION scheme to the BCP47
     152             :                 // ctor as that triggers use of liblangtag and initializes its
     153             :                 // database which we do not want during startup. Convert
     154             :                 // instead.
     155             :                 sal_Int32 nPos;
     156         528 :                 if (sChunk.indexOf('-') < 0 && ((nPos = sChunk.indexOf('_')) > 0))
     157         528 :                     sChunk = sChunk.replaceAt( nPos, 1, OUString('-'));
     158        1056 :                 LanguageTag aLangTag(sChunk, true);
     159         528 :                 if (!aLangTag.isValidBcp47())
     160           0 :                     continue;
     161             : 
     162             :                 // Thus we first get the language of the dictionary
     163        1056 :                 OUString aLocaleName(aLangTag.getBcp47());
     164             : 
     165         528 :                 if (aDicLangInUse.count(aLocaleName) == 0)
     166             :                 {
     167             :                     // remember the new language in use
     168         528 :                     aDicLangInUse.insert(aLocaleName);
     169             : 
     170             :                     // add the dictionary to the resulting vector
     171         528 :                     SvtLinguConfigDictionaryEntry aDicEntry;
     172         528 :                     aDicEntry.aLocations.realloc(1);
     173         528 :                     aDicEntry.aLocaleNames.realloc(1);
     174         528 :                     aDicEntry.aLocations[0] = sPath;
     175         528 :                     aDicEntry.aFormatName = aFormatName;
     176         528 :                     aDicEntry.aLocaleNames[0] = aLocaleName;
     177         528 :                     aRes.push_back( aDicEntry );
     178         528 :                 }
     179             :             }
     180         640 :         }
     181             :     }
     182             : #endif
     183             : 
     184          90 :     return aRes;
     185             : }
     186             : 
     187             : 
     188          90 : void MergeNewStyleDicsAndOldStyleDics(
     189             :     std::list< SvtLinguConfigDictionaryEntry > &rNewStyleDics,
     190             :     const std::vector< SvtLinguConfigDictionaryEntry > &rOldStyleDics )
     191             : {
     192             :     // get list of languages supported by new style dictionaries
     193          90 :     std::set< LanguageType > aNewStyleLanguages;
     194          90 :     std::list< SvtLinguConfigDictionaryEntry >::const_iterator aIt;
     195          90 :     for (aIt = rNewStyleDics.begin() ;  aIt != rNewStyleDics.end();  ++aIt)
     196             :     {
     197           0 :         const uno::Sequence< OUString > aLocaleNames( aIt->aLocaleNames );
     198           0 :         sal_Int32 nLocaleNames = aLocaleNames.getLength();
     199           0 :         for (sal_Int32 k = 0;  k < nLocaleNames; ++k)
     200             :         {
     201           0 :             LanguageType nLang = LanguageTag( aLocaleNames[k] ).getLanguageType();
     202           0 :             aNewStyleLanguages.insert( nLang );
     203             :         }
     204           0 :     }
     205             : 
     206             :     // now check all old style dictionaries if they will add a not yet
     207             :     // added language. If so add them to the resulting vector
     208          90 :     std::vector< SvtLinguConfigDictionaryEntry >::const_iterator aIt2;
     209         618 :     for (aIt2 = rOldStyleDics.begin();  aIt2 != rOldStyleDics.end();  ++aIt2)
     210             :     {
     211         528 :         sal_Int32 nOldStyleDics = aIt2->aLocaleNames.getLength();
     212             : 
     213             :         // old style dics should only have one language listed...
     214             :         DBG_ASSERT( nOldStyleDics, "old style dictionary with more then one language found!");
     215         528 :         if (nOldStyleDics > 0)
     216             :         {
     217         528 :             LanguageType nLang = LanguageTag( aIt2->aLocaleNames[0] ).getLanguageType();
     218             : 
     219         528 :             if (nLang == LANGUAGE_DONTKNOW || linguistic::LinguIsUnspecified( nLang))
     220             :             {
     221             :                 OSL_FAIL( "old style dictionary with invalid language found!" );
     222           0 :                 continue;
     223             :             }
     224             : 
     225             :             // language not yet added?
     226         528 :             if (aNewStyleLanguages.count( nLang ) == 0)
     227         528 :                 rNewStyleDics.push_back( *aIt2 );
     228             :         }
     229             :         else
     230             :         {
     231             :             OSL_FAIL( "old style dictionary with no language found!" );
     232             :         }
     233          90 :     }
     234          90 : }
     235             : 
     236             : 
     237          26 : rtl_TextEncoding getTextEncodingFromCharset(const sal_Char* pCharset)
     238             : {
     239             :     // default result: used to indicate that we failed to get the proper encoding
     240          26 :     rtl_TextEncoding eRet = RTL_TEXTENCODING_DONTKNOW;
     241             : 
     242          26 :     if (pCharset)
     243             :     {
     244          26 :         eRet = rtl_getTextEncodingFromMimeCharset(pCharset);
     245          26 :         if (eRet == RTL_TEXTENCODING_DONTKNOW)
     246           0 :             eRet = rtl_getTextEncodingFromUnixCharset(pCharset);
     247          26 :         if (eRet == RTL_TEXTENCODING_DONTKNOW)
     248             :         {
     249           0 :             if (strcmp("ISCII-DEVANAGARI", pCharset) == 0)
     250           0 :                 eRet = RTL_TEXTENCODING_ISCII_DEVANAGARI;
     251             :         }
     252             :     }
     253          26 :     return eRet;
     254             : }
     255             : 
     256             : //////////////////////////////////////////////////////////////////////
     257             : 
     258             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10