LCOV - code coverage report
Current view: top level - connectivity/source/resource - sharedresources.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 34 66 51.5 %
Date: 2014-11-03 Functions: 10 15 66.7 %
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             : #include "resource/sharedresources.hxx"
      21             : 
      22             : #include <comphelper/processfactory.hxx>
      23             : #include <comphelper/officeresourcebundle.hxx>
      24             : 
      25             : #include <com/sun/star/uno/XComponentContext.hpp>
      26             : 
      27             : #include <tools/diagnose_ex.h>
      28             : #include <osl/diagnose.h>
      29             : 
      30             : 
      31             : namespace connectivity
      32             : {
      33             : 
      34             : 
      35             :     using ::com::sun::star::uno::Reference;
      36             :     using ::com::sun::star::uno::XComponentContext;
      37             :     using ::com::sun::star::uno::Exception;
      38             : 
      39             : 
      40             :     //= SharedResources_Impl
      41             : 
      42           5 :     class SharedResources_Impl
      43             :     {
      44             :     private:
      45             :         static  SharedResources_Impl*   s_pInstance;
      46             :         static  oslInterlockedCount     s_nClients;
      47             : 
      48             :     private:
      49             :         ::std::unique_ptr< ::comphelper::OfficeResourceBundle >
      50             :                                         m_pResourceBundle;
      51             : 
      52             :     public:
      53             :         static void     registerClient();
      54             :         static void     revokeClient();
      55             : 
      56             :         static SharedResources_Impl&
      57             :                         getInstance();
      58             : 
      59             :         OUString getResourceString( ResourceId _nId );
      60             : 
      61             :     private:
      62             :         SharedResources_Impl();
      63             : 
      64         118 :         static ::osl::Mutex& getMutex()
      65             :         {
      66         118 :             static ::osl::Mutex s_aMutex;
      67         118 :             return s_aMutex;
      68             :         }
      69             :     };
      70             : 
      71             : 
      72             :     SharedResources_Impl*   SharedResources_Impl::s_pInstance( NULL );
      73             :     oslInterlockedCount     SharedResources_Impl::s_nClients( 0 );
      74             : 
      75             : 
      76           5 :     SharedResources_Impl::SharedResources_Impl()
      77             :     {
      78             :         try
      79             :         {
      80             :             Reference< XComponentContext > xContext(
      81           5 :                 comphelper::getProcessComponentContext() );
      82           5 :             m_pResourceBundle.reset( new ::comphelper::OfficeResourceBundle( xContext, "cnr" ) );
      83             :         }
      84           0 :         catch( const Exception& )
      85             :         {
      86             :             DBG_UNHANDLED_EXCEPTION();
      87             :         }
      88           5 :     }
      89             : 
      90             : 
      91          46 :     OUString SharedResources_Impl::getResourceString( ResourceId _nId )
      92             :     {
      93          46 :         if ( m_pResourceBundle.get() == NULL )
      94             :             // this should never happen, but we gracefully ignore it. It has been reported
      95             :             // in the constructor in non-product builds.
      96           0 :             return OUString();
      97             : 
      98          46 :         return m_pResourceBundle->loadString( _nId );
      99             :     }
     100             : 
     101             : 
     102          74 :     void SharedResources_Impl::registerClient()
     103             :     {
     104          74 :         osl_atomic_increment( &s_nClients );
     105          74 :     }
     106             : 
     107             : 
     108          72 :     void SharedResources_Impl::revokeClient()
     109             :     {
     110          72 :         ::osl::MutexGuard aGuard( getMutex() );
     111          72 :         if ( 0 == osl_atomic_decrement( &s_nClients ) )
     112             :         {
     113          25 :             delete s_pInstance;
     114          25 :             s_pInstance = NULL;
     115          72 :         }
     116          72 :     }
     117             : 
     118             : 
     119          46 :     SharedResources_Impl& SharedResources_Impl::getInstance()
     120             :     {
     121          46 :         ::osl::MutexGuard aGuard( getMutex() );
     122             :         OSL_ENSURE( s_nClients > 0, "SharedResources_Impl::getInstance: no active clients!" );
     123             : 
     124          46 :         if ( !s_pInstance )
     125           5 :             s_pInstance = new SharedResources_Impl;
     126             : 
     127          46 :         return *s_pInstance;
     128             :     }
     129             : 
     130             : 
     131             :     //= helpers
     132             : 
     133             :     namespace
     134             :     {
     135           0 :         size_t lcl_substitute( OUString& _inout_rString,
     136             :             const sal_Char* _pAsciiPattern, const OUString& _rReplace )
     137             :         {
     138           0 :             size_t nOccurrences = 0;
     139             : 
     140           0 :             OUString sPattern( OUString::createFromAscii( _pAsciiPattern ) );
     141           0 :             sal_Int32 nIndex = 0;
     142           0 :             while ( ( nIndex = _inout_rString.indexOf( sPattern ) ) > -1 )
     143             :             {
     144           0 :                 ++nOccurrences;
     145           0 :                 _inout_rString = _inout_rString.replaceAt( nIndex, sPattern.getLength(), _rReplace );
     146             :             }
     147             : 
     148           0 :             return nOccurrences;
     149             :         }
     150             :     }
     151             : 
     152             : 
     153             :     //= SharedResources
     154             : 
     155             : 
     156          74 :     SharedResources::SharedResources()
     157             :     {
     158          74 :         SharedResources_Impl::registerClient();
     159          74 :     }
     160             : 
     161             : 
     162          72 :     SharedResources::~SharedResources()
     163             :     {
     164          72 :         SharedResources_Impl::revokeClient();
     165          72 :     }
     166             : 
     167             : 
     168          46 :     OUString SharedResources::getResourceString( ResourceId _nResId ) const
     169             :     {
     170          46 :         return SharedResources_Impl::getInstance().getResourceString( _nResId );
     171             :     }
     172             : 
     173             : 
     174           0 :     OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
     175             :                 const sal_Char* _pAsciiPatternToReplace, const OUString& _rStringToSubstitute ) const
     176             :     {
     177           0 :         OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
     178           0 :         OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace, _rStringToSubstitute ) );
     179           0 :         return sString;
     180             :     }
     181             : 
     182             : 
     183           0 :     OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
     184             :                 const sal_Char* _pAsciiPatternToReplace1, const OUString& _rStringToSubstitute1,
     185             :                 const sal_Char* _pAsciiPatternToReplace2, const OUString& _rStringToSubstitute2 ) const
     186             :     {
     187           0 :         OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
     188           0 :         OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) );
     189           0 :         OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) );
     190           0 :         return sString;
     191             :     }
     192             : 
     193             : 
     194           0 :     OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
     195             :                 const sal_Char* _pAsciiPatternToReplace1, const OUString& _rStringToSubstitute1,
     196             :                 const sal_Char* _pAsciiPatternToReplace2, const OUString& _rStringToSubstitute2,
     197             :                 const sal_Char* _pAsciiPatternToReplace3, const OUString& _rStringToSubstitute3 ) const
     198             :     {
     199           0 :         OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
     200           0 :         OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) );
     201           0 :         OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) );
     202           0 :         OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace3, _rStringToSubstitute3 ) );
     203           0 :         return sString;
     204             :     }
     205             : 
     206           0 :     OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
     207             :                     const ::std::list< ::std::pair<const sal_Char* , OUString > > _aStringToSubstitutes) const
     208             :     {
     209           0 :         OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
     210           0 :         ::std::list< ::std::pair<const sal_Char* , OUString > >::const_iterator aIter = _aStringToSubstitutes.begin();
     211           0 :         ::std::list< ::std::pair<const sal_Char* , OUString > >::const_iterator aEnd  = _aStringToSubstitutes.end();
     212           0 :         for(;aIter != aEnd; ++aIter)
     213           0 :             OSL_VERIFY( lcl_substitute( sString, aIter->first, aIter->second ) );
     214             : 
     215           0 :         return sString;
     216             :     }
     217             : 
     218             : 
     219             : } // namespace connectivity
     220             : 
     221             : 
     222             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10