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

Generated by: LCOV version 1.10