bump product version to 6.3.0.0.beta1
[LibreOffice.git] / connectivity / source / resource / sharedresources.cxx
blob87185d66b83a87c5ba139de4602ea38302c77250
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <memory>
21 #include <resource/sharedresources.hxx>
23 #include <com/sun/star/uno/XComponentContext.hpp>
25 #include <tools/diagnose_ex.h>
26 #include <unotools/resmgr.hxx>
27 #include <osl/diagnose.h>
28 #include <sal/log.hxx>
30 namespace connectivity
34 class SharedResources_Impl
36 private:
37 static SharedResources_Impl* s_pInstance;
38 static oslInterlockedCount s_nClients;
40 private:
41 std::locale m_aLocale;
43 public:
44 static void registerClient();
45 static void revokeClient();
47 static SharedResources_Impl&
48 getInstance();
50 OUString getResourceString(const char* pId);
52 private:
53 SharedResources_Impl();
55 static ::osl::Mutex& getMutex()
57 static ::osl::Mutex s_aMutex;
58 return s_aMutex;
62 SharedResources_Impl* SharedResources_Impl::s_pInstance( nullptr );
63 oslInterlockedCount SharedResources_Impl::s_nClients( 0 );
65 SharedResources_Impl::SharedResources_Impl()
66 : m_aLocale(Translate::Create("cnr"))
70 OUString SharedResources_Impl::getResourceString(const char* pId)
72 return Translate::get(pId, m_aLocale);
75 void SharedResources_Impl::registerClient()
77 osl_atomic_increment( &s_nClients );
80 void SharedResources_Impl::revokeClient()
82 ::osl::MutexGuard aGuard( getMutex() );
83 if ( 0 == osl_atomic_decrement( &s_nClients ) )
85 delete s_pInstance;
86 s_pInstance = nullptr;
91 SharedResources_Impl& SharedResources_Impl::getInstance()
93 ::osl::MutexGuard aGuard( getMutex() );
94 OSL_ENSURE( s_nClients > 0, "SharedResources_Impl::getInstance: no active clients!" );
96 if ( !s_pInstance )
97 s_pInstance = new SharedResources_Impl;
99 return *s_pInstance;
102 namespace
104 size_t lcl_substitute( OUString& _inout_rString,
105 const sal_Char* _pAsciiPattern, const OUString& _rReplace )
107 size_t nOccurrences = 0;
109 OUString sPattern( OUString::createFromAscii( _pAsciiPattern ) );
110 sal_Int32 nIndex = 0;
111 while ( ( nIndex = _inout_rString.indexOf( sPattern ) ) > -1 )
113 ++nOccurrences;
114 _inout_rString = _inout_rString.replaceAt( nIndex, sPattern.getLength(), _rReplace );
117 return nOccurrences;
121 SharedResources::SharedResources()
123 SharedResources_Impl::registerClient();
127 SharedResources::~SharedResources()
129 SharedResources_Impl::revokeClient();
133 OUString SharedResources::getResourceString(const char* pResId) const
135 return SharedResources_Impl::getInstance().getResourceString(pResId);
139 OUString SharedResources::getResourceStringWithSubstitution(const char* pResId,
140 const sal_Char* _pAsciiPatternToReplace, const OUString& _rStringToSubstitute ) const
142 OUString sString( SharedResources_Impl::getInstance().getResourceString(pResId) );
143 if ( !lcl_substitute( sString, _pAsciiPatternToReplace, _rStringToSubstitute ) )
144 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace << " with " << _rStringToSubstitute);
145 return sString;
149 OUString SharedResources::getResourceStringWithSubstitution(const char* pResId,
150 const sal_Char* _pAsciiPatternToReplace1, const OUString& _rStringToSubstitute1,
151 const sal_Char* _pAsciiPatternToReplace2, const OUString& _rStringToSubstitute2 ) const
153 OUString sString( SharedResources_Impl::getInstance().getResourceString(pResId) );
154 if( !lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) )
155 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace1 << " with " << _rStringToSubstitute1);
156 if( !lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) )
157 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace2 << " with " << _rStringToSubstitute2);
158 return sString;
162 OUString SharedResources::getResourceStringWithSubstitution(const char* pResId,
163 const sal_Char* _pAsciiPatternToReplace1, const OUString& _rStringToSubstitute1,
164 const sal_Char* _pAsciiPatternToReplace2, const OUString& _rStringToSubstitute2,
165 const sal_Char* _pAsciiPatternToReplace3, const OUString& _rStringToSubstitute3 ) const
167 OUString sString( SharedResources_Impl::getInstance().getResourceString(pResId) );
168 if( !lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) )
169 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace1 << " with " << _rStringToSubstitute1);
170 if( !lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) )
171 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace2 << " with " << _rStringToSubstitute2);
172 if( !lcl_substitute( sString, _pAsciiPatternToReplace3, _rStringToSubstitute3 ) )
173 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace3 << " with " << _rStringToSubstitute3);
174 return sString;
177 OUString SharedResources::getResourceStringWithSubstitution(const char* pResId,
178 const std::vector< std::pair<const sal_Char* , OUString > >& _rStringToSubstitutes) const
180 OUString sString( SharedResources_Impl::getInstance().getResourceString(pResId) );
181 for(const auto& [rPattern, rReplace] : _rStringToSubstitutes)
182 if( !lcl_substitute( sString, rPattern, rReplace ) )
183 SAL_WARN("connectivity.resource", "Unable to substitute " << rPattern << " with " << rReplace);
185 return sString;
189 } // namespace connectivity
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */