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

Generated by: LCOV version 1.11