LCOV - code coverage report
Current view: top level - connectivity/source/resource - sharedresources.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 66 0.0 %
Date: 2014-04-14 Functions: 0 15 0.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             : #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           0 :     class SharedResources_Impl
      43             :     {
      44             :     private:
      45             :         static  SharedResources_Impl*   s_pInstance;
      46             :         static  oslInterlockedCount     s_nClients;
      47             : 
      48             :     private:
      49             :         ::std::auto_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           0 :         static ::osl::Mutex& getMutex()
      65             :         {
      66           0 :             static ::osl::Mutex s_aMutex;
      67           0 :             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           0 :     SharedResources_Impl::SharedResources_Impl()
      77             :     {
      78             :         try
      79             :         {
      80             :             Reference< XComponentContext > xContext(
      81           0 :                 comphelper::getProcessComponentContext() );
      82           0 :             m_pResourceBundle.reset( new ::comphelper::OfficeResourceBundle( xContext, "cnr" ) );
      83             :         }
      84           0 :         catch( const Exception& )
      85             :         {
      86             :             DBG_UNHANDLED_EXCEPTION();
      87             :         }
      88           0 :     }
      89             : 
      90             : 
      91           0 :     OUString SharedResources_Impl::getResourceString( ResourceId _nId )
      92             :     {
      93           0 :         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           0 :         return m_pResourceBundle->loadString( _nId );
      99             :     }
     100             : 
     101             : 
     102           0 :     void SharedResources_Impl::registerClient()
     103             :     {
     104           0 :         osl_atomic_increment( &s_nClients );
     105           0 :     }
     106             : 
     107             : 
     108           0 :     void SharedResources_Impl::revokeClient()
     109             :     {
     110           0 :         ::osl::MutexGuard aGuard( getMutex() );
     111           0 :         if ( 0 == osl_atomic_decrement( &s_nClients ) )
     112             :         {
     113           0 :             delete s_pInstance;
     114           0 :             s_pInstance = NULL;
     115           0 :         }
     116           0 :     }
     117             : 
     118             : 
     119           0 :     SharedResources_Impl& SharedResources_Impl::getInstance()
     120             :     {
     121           0 :         ::osl::MutexGuard aGuard( getMutex() );
     122             :         OSL_ENSURE( s_nClients > 0, "SharedResources_Impl::getInstance: no active clients!" );
     123             : 
     124           0 :         if ( !s_pInstance )
     125           0 :             s_pInstance = new SharedResources_Impl;
     126             : 
     127           0 :         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           0 :     SharedResources::SharedResources()
     157             :     {
     158           0 :         SharedResources_Impl::registerClient();
     159           0 :     }
     160             : 
     161             : 
     162           0 :     SharedResources::~SharedResources()
     163             :     {
     164           0 :         SharedResources_Impl::revokeClient();
     165           0 :     }
     166             : 
     167             : 
     168           0 :     OUString SharedResources::getResourceString( ResourceId _nResId ) const
     169             :     {
     170           0 :         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