diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
commit | ed5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch) | |
tree | 7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /sc/source/filter/ftools | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream.tar.xz libreoffice-upstream.zip |
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sc/source/filter/ftools')
-rw-r--r-- | sc/source/filter/ftools/fapihelper.cxx | 369 | ||||
-rw-r--r-- | sc/source/filter/ftools/fprogressbar.cxx | 234 | ||||
-rw-r--r-- | sc/source/filter/ftools/ftools.cxx | 365 | ||||
-rw-r--r-- | sc/source/filter/ftools/sharedformulagroups.cxx | 40 |
4 files changed, 1008 insertions, 0 deletions
diff --git a/sc/source/filter/ftools/fapihelper.cxx b/sc/source/filter/ftools/fapihelper.cxx new file mode 100644 index 000000000..4abfc79be --- /dev/null +++ b/sc/source/filter/ftools/fapihelper.cxx @@ -0,0 +1,369 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <fapihelper.hxx> + +#include <algorithm> +#include <com/sun/star/frame/XModel.hpp> +#include <com/sun/star/lang/XServiceName.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/beans/NamedValue.hpp> +#include <com/sun/star/beans/XPropertyState.hpp> +#include <com/sun/star/beans/XPropertySetOption.hpp> +#include <com/sun/star/beans/XMultiPropertySet.hpp> +#include <comphelper/docpasswordhelper.hxx> +#include <comphelper/processfactory.hxx> +#include <sal/log.hxx> +#include <sfx2/objsh.hxx> +#include <sfx2/docfile.hxx> +#include <sfx2/frame.hxx> +#include <sfx2/sfxsids.hrc> +#include <svl/stritem.hxx> +#include <svl/itemset.hxx> +#include <miscuno.hxx> + +using ::com::sun::star::uno::Any; +using ::com::sun::star::uno::Reference; +using ::com::sun::star::uno::Sequence; +using ::com::sun::star::uno::Exception; +using ::com::sun::star::uno::UNO_QUERY; +using ::com::sun::star::uno::UNO_QUERY_THROW; +using ::com::sun::star::uno::XInterface; +using ::com::sun::star::beans::XPropertySet; +using ::com::sun::star::beans::XPropertyState; +using ::com::sun::star::lang::XServiceName; +using ::com::sun::star::lang::XMultiServiceFactory; + +using namespace ::com::sun::star; + +// Static helper functions ==================================================== + +OUString ScfApiHelper::GetServiceName( const Reference< XInterface >& xInt ) +{ + OUString aService; + Reference< XServiceName > xServiceName( xInt, UNO_QUERY ); + if( xServiceName.is() ) + aService = xServiceName->getServiceName(); + return aService; +} + +Reference< XMultiServiceFactory > ScfApiHelper::GetServiceFactory( const SfxObjectShell* pShell ) +{ + Reference< XMultiServiceFactory > xFactory; + if( pShell ) + xFactory.set( pShell->GetModel(), UNO_QUERY ); + return xFactory; +} + +Reference< XInterface > ScfApiHelper::CreateInstance( + const Reference< XMultiServiceFactory >& xFactory, const OUString& rServiceName ) +{ + Reference< XInterface > xInt; + if( xFactory.is() ) + { + try + { + xInt = xFactory->createInstance( rServiceName ); + } + catch( Exception& ) + { + OSL_FAIL( "ScfApiHelper::CreateInstance - cannot create instance" ); + } + } + return xInt; +} + +Reference< XInterface > ScfApiHelper::CreateInstance( const SfxObjectShell* pShell, const OUString& rServiceName ) +{ + return CreateInstance( GetServiceFactory( pShell ), rServiceName ); +} + +Reference< XInterface > ScfApiHelper::CreateInstance( const OUString& rServiceName ) +{ + return CreateInstance( ::comphelper::getProcessServiceFactory(), rServiceName ); +} + +uno::Sequence< beans::NamedValue > ScfApiHelper::QueryEncryptionDataForMedium( SfxMedium& rMedium, + ::comphelper::IDocPasswordVerifier& rVerifier, const ::std::vector< OUString >* pDefaultPasswords ) +{ + uno::Sequence< beans::NamedValue > aEncryptionData; + const SfxUnoAnyItem* pEncryptionDataItem = SfxItemSet::GetItem<SfxUnoAnyItem>(rMedium.GetItemSet(), SID_ENCRYPTIONDATA, false); + if ( pEncryptionDataItem ) + pEncryptionDataItem->GetValue() >>= aEncryptionData; + + OUString aPassword; + const SfxStringItem* pPasswordItem = SfxItemSet::GetItem<SfxStringItem>(rMedium.GetItemSet(), SID_PASSWORD, false); + if ( pPasswordItem ) + aPassword = pPasswordItem->GetValue(); + + bool bIsDefaultPassword = false; + aEncryptionData = ::comphelper::DocPasswordHelper::requestAndVerifyDocPassword( + rVerifier, aEncryptionData, aPassword, rMedium.GetInteractionHandler(), rMedium.GetOrigURL(), + ::comphelper::DocPasswordRequestType::MS, pDefaultPasswords, &bIsDefaultPassword ); + + rMedium.GetItemSet()->ClearItem( SID_PASSWORD ); + rMedium.GetItemSet()->ClearItem( SID_ENCRYPTIONDATA ); + + if( !bIsDefaultPassword && aEncryptionData.hasElements() ) + rMedium.GetItemSet()->Put( SfxUnoAnyItem( SID_ENCRYPTIONDATA, uno::Any( aEncryptionData ) ) ); + + return aEncryptionData; +} + +// Property sets ============================================================== + +ScfPropertySet::~ScfPropertySet() +{ + Reference<beans::XPropertySetOption> xPropSetOpt(mxPropSet, UNO_QUERY); + if (xPropSetOpt.is()) + // Turn the property value change notification back on when finished. + xPropSetOpt->enableChangeListenerNotification(true); +} + +void ScfPropertySet::Set( Reference< XPropertySet > const & xPropSet ) +{ + mxPropSet = xPropSet; + mxMultiPropSet.set( mxPropSet, UNO_QUERY ); + Reference<beans::XPropertySetOption> xPropSetOpt(mxPropSet, UNO_QUERY); + if (xPropSetOpt.is()) + // We don't want to broadcast property value changes during import to + // improve performance. + xPropSetOpt->enableChangeListenerNotification(false); +} + +OUString ScfPropertySet::GetServiceName() const +{ + return ScfApiHelper::GetServiceName( mxPropSet ); +} + +// Get properties ------------------------------------------------------------- + +bool ScfPropertySet::HasProperty( const OUString& rPropName ) const +{ + bool bHasProp = false; + try + { + Reference< XPropertyState > xPropState( mxPropSet, UNO_QUERY_THROW ); + bHasProp = xPropState->getPropertyState( rPropName ) == css::beans::PropertyState_DIRECT_VALUE; + } + catch( Exception& ) + { + } + return bHasProp; +} + +bool ScfPropertySet::GetAnyProperty( Any& rValue, const OUString& rPropName ) const +{ + bool bHasValue = false; + try + { + if( mxPropSet.is() ) + { + rValue = mxPropSet->getPropertyValue( rPropName ); + bHasValue = true; + } + } + catch( Exception& ) + { + } + return bHasValue; +} + +bool ScfPropertySet::GetBoolProperty( const OUString& rPropName ) const +{ + Any aAny; + return GetAnyProperty( aAny, rPropName ) && ScUnoHelpFunctions::GetBoolFromAny( aAny ); +} + +OUString ScfPropertySet::GetStringProperty( const OUString& rPropName ) const +{ + OUString aOUString; + GetProperty( aOUString, rPropName ); + return aOUString; +} + +bool ScfPropertySet::GetColorProperty( Color& rColor, const OUString& rPropName ) const +{ + sal_Int32 nApiColor = 0; + bool bRet = GetProperty( nApiColor, rPropName ); + rColor = Color( ColorTransparency, nApiColor ); + return bRet; +} + +void ScfPropertySet::GetProperties( Sequence< Any >& rValues, const Sequence< OUString >& rPropNames ) const +{ + try + { + OSL_ENSURE( mxMultiPropSet.is(), "ScfPropertySet::GetProperties - multi property set not available" ); + if( mxMultiPropSet.is() ) // first try the XMultiPropertySet + { + rValues = mxMultiPropSet->getPropertyValues( rPropNames ); + } + else if( mxPropSet.is() ) + { + sal_Int32 nLen = rPropNames.getLength(); + rValues.realloc( nLen ); + std::transform(rPropNames.begin(), rPropNames.end(), rValues.getArray(), + [this](const OUString& rPropName) -> Any { return mxPropSet->getPropertyValue(rPropName); }); + } + } + catch( Exception& ) + { + } +} + +// Set properties ------------------------------------------------------------- + +void ScfPropertySet::SetAnyProperty( const OUString& rPropName, const Any& rValue ) +{ + try + { + if( mxPropSet.is() ) + mxPropSet->setPropertyValue( rPropName, rValue ); + } + catch (const Exception&) + { + SAL_WARN("sc", "ScfPropertySet::SetAnyProperty - cannot set property \"" + rPropName + "\""); + } +} + +void ScfPropertySet::SetProperties( const Sequence< OUString >& rPropNames, const Sequence< Any >& rValues ) +{ + OSL_ENSURE( rPropNames.getLength() == rValues.getLength(), "ScfPropertySet::SetProperties - length of sequences different" ); + try + { + if( mxMultiPropSet.is() ) // first try the XMultiPropertySet + { + mxMultiPropSet->setPropertyValues( rPropNames, rValues ); + } + else if( mxPropSet.is() ) + { + OSL_FAIL( "ScfPropertySet::SetProperties - multi property set not available" ); + const OUString* pPropName = rPropNames.getConstArray(); + const OUString* pPropNameEnd = pPropName + rPropNames.getLength(); + const Any* pValue = rValues.getConstArray(); + for( ; pPropName != pPropNameEnd; ++pPropName, ++pValue ) + mxPropSet->setPropertyValue( *pPropName, *pValue ); + } + } + catch( Exception& ) + { + OSL_FAIL( "ScfPropertySet::SetAnyProperty - cannot set multiple properties" ); + } +} + +ScfPropSetHelper::ScfPropSetHelper( const char* const* ppcPropNames ) : + mnNextIdx( 0 ) +{ + OSL_ENSURE( ppcPropNames, "ScfPropSetHelper::ScfPropSetHelper - no strings found" ); + + // create OUStrings from ASCII property names + typedef ::std::pair< OUString, size_t > IndexedOUString; + std::vector<IndexedOUString> aPropNameVec; + for( size_t nVecIdx = 0; *ppcPropNames; ++ppcPropNames, ++nVecIdx ) + { + OUString aPropName = OUString::createFromAscii( *ppcPropNames ); + aPropNameVec.emplace_back( aPropName, nVecIdx ); + } + + // sorts the pairs, which will be sorted by first component (the property name) + ::std::sort( aPropNameVec.begin(), aPropNameVec.end() ); + + // resize member sequences + size_t nSize = aPropNameVec.size(); + maNameSeq.realloc( static_cast< sal_Int32 >( nSize ) ); + auto pNameSeq = maNameSeq.getArray(); + maValueSeq.realloc( static_cast< sal_Int32 >( nSize ) ); + maNameOrder.resize( nSize ); + + // fill the property name sequence and store original sort order + sal_Int32 nSeqIdx = 0; + for( auto& aPropName : aPropNameVec ) + { + pNameSeq[ nSeqIdx ] = aPropName.first; + maNameOrder[ aPropName.second ] = nSeqIdx; + ++nSeqIdx; + } +} + +// read properties ------------------------------------------------------------ + +void ScfPropSetHelper::ReadFromPropertySet( const ScfPropertySet& rPropSet ) +{ + rPropSet.GetProperties( maValueSeq, maNameSeq ); + mnNextIdx = 0; +} + +void ScfPropSetHelper::ReadValue( Any& rAny ) +{ + Any* pAny = GetNextAny(); + if( pAny ) + rAny = *pAny; +} + +void ScfPropSetHelper::ReadValue( Color& rColor ) +{ + sal_Int32 nApiColor(0); + ReadValue( nApiColor ); + rColor = Color( ColorTransparency, nApiColor ); +} + +void ScfPropSetHelper::ReadValue( bool& rbValue ) +{ + Any aAny; + ReadValue( aAny ); + rbValue = ScUnoHelpFunctions::GetBoolFromAny( aAny ); +} + +// write properties ----------------------------------------------------------- + +void ScfPropSetHelper::InitializeWrite() +{ + mnNextIdx = 0; +} + +void ScfPropSetHelper::WriteValue( const Any& rAny ) +{ + if( Any* pAny = GetNextAny() ) + *pAny = rAny; +} + +void ScfPropSetHelper::WriteValue( bool rbValue ) +{ + if( Any* pAny = GetNextAny() ) + *pAny <<= rbValue; +} + +void ScfPropSetHelper::WriteToPropertySet( ScfPropertySet& rPropSet ) const +{ + rPropSet.SetProperties( maNameSeq, maValueSeq ); +} + +// private -------------------------------------------------------------------- + +Any* ScfPropSetHelper::GetNextAny() +{ + OSL_ENSURE( mnNextIdx < maNameOrder.size(), "ScfPropSetHelper::GetNextAny - sequence overflow" ); + Any* pAny = nullptr; + if( mnNextIdx < maNameOrder.size() ) + pAny = &maValueSeq.getArray()[ maNameOrder[ mnNextIdx++ ] ]; + return pAny; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/filter/ftools/fprogressbar.cxx b/sc/source/filter/ftools/fprogressbar.cxx new file mode 100644 index 000000000..3b84e8492 --- /dev/null +++ b/sc/source/filter/ftools/fprogressbar.cxx @@ -0,0 +1,234 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <fprogressbar.hxx> +#include <globstr.hrc> +#include <scresid.hxx> +#include <progress.hxx> +#include <osl/diagnose.h> +#include <tools/stream.hxx> + +#include <limits> + +ScfProgressBar::ScfProgressSegment::ScfProgressSegment( std::size_t nSize ) : + mnSize( nSize ), + mnPos( 0 ) +{ +} + +ScfProgressBar::ScfProgressSegment::~ScfProgressSegment() +{ +} + +ScfProgressBar::ScfProgressBar( SfxObjectShell* pDocShell, const OUString& rText ) : + maText( rText ) +{ + Init( pDocShell ); +} + +ScfProgressBar::ScfProgressBar(SfxObjectShell* pDocShell, TranslateId pResId) + : maText(ScResId(pResId)) +{ + Init( pDocShell ); +} + +ScfProgressBar::ScfProgressBar( ScfProgressBar& rParProgress, ScfProgressSegment* pParSegment ) +{ + Init( rParProgress.mpDocShell ); + mpParentProgress = &rParProgress; + mpParentSegment = pParSegment; +} + +ScfProgressBar::~ScfProgressBar() +{ +} + +void ScfProgressBar::Init( SfxObjectShell* pDocShell ) +{ + mpDocShell = pDocShell; + mpParentProgress = nullptr; + mpParentSegment = mpCurrSegment = nullptr; + mnTotalSize = mnTotalPos = mnUnitSize = mnNextUnitPos = 0; + mnSysProgressScale = 1; // used to workaround the SfxProgress sal_uInt32 limit + mbInProgress = false; +} + +ScfProgressBar::ScfProgressSegment* ScfProgressBar::GetSegment( sal_Int32 nSegment ) +{ + if( nSegment < 0 ) + return nullptr; + return maSegments.at( nSegment ).get(); +} + +void ScfProgressBar::SetCurrSegment( ScfProgressSegment* pSegment ) +{ + if( mpCurrSegment == pSegment ) + return; + + mpCurrSegment = pSegment; + + if( mpParentProgress && mpParentSegment ) + { + mpParentProgress->SetCurrSegment( mpParentSegment ); + } + else if( !mxSysProgress && (mnTotalSize > 0) ) + { + // SfxProgress has a limit of sal_uInt32. + mnSysProgressScale = 1; + std::size_t nSysTotalSize = mnTotalSize; + while( nSysTotalSize > std::numeric_limits<sal_uInt32>::max() ) + { + nSysTotalSize /= 2; + mnSysProgressScale *= 2; + } + mxSysProgress.reset( new ScProgress( mpDocShell, maText, nSysTotalSize, true ) ); + } + + if( !mbInProgress && mpCurrSegment && (mnTotalSize > 0) ) + { + mnUnitSize = mnTotalSize / 256 + 1; // at most 256 calls of system progress + mnNextUnitPos = 0; + mbInProgress = true; + } +} + +void ScfProgressBar::IncreaseProgressBar( std::size_t nDelta ) +{ + std::size_t nNewPos = mnTotalPos + nDelta; + + // call back to parent progress bar + if( mpParentProgress && mpParentSegment ) + { + // calculate new position of parent progress bar + std::size_t nParentPos = static_cast< std::size_t >( + static_cast< double >( nNewPos ) * mpParentSegment->mnSize / mnTotalSize ); + mpParentProgress->ProgressAbs( nParentPos ); + } + // modify system progress bar + else if( mxSysProgress ) + { + if( nNewPos >= mnNextUnitPos ) + { + mnNextUnitPos = nNewPos + mnUnitSize; + mxSysProgress->SetState( static_cast< sal_uLong >( nNewPos / mnSysProgressScale ) ); + } + } + else + { + OSL_FAIL( "ScfProgressBar::IncreaseProgressBar - no progress bar found" ); + } + + mnTotalPos = nNewPos; +} + +sal_Int32 ScfProgressBar::AddSegment( std::size_t nSize ) +{ + OSL_ENSURE( !mbInProgress, "ScfProgressBar::AddSegment - already in progress mode" ); + if( nSize == 0 ) + return SCF_INV_SEGMENT; + + maSegments.push_back( std::make_unique<ScfProgressSegment>( nSize ) ); + mnTotalSize += nSize; + return static_cast< sal_Int32 >( maSegments.size() - 1 ); +} + +ScfProgressBar& ScfProgressBar::GetSegmentProgressBar( sal_Int32 nSegment ) +{ + ScfProgressSegment* pSegment = GetSegment( nSegment ); + OSL_ENSURE( !pSegment || (pSegment->mnPos == 0), "ScfProgressBar::GetSegmentProgressBar - segment already started" ); + if( pSegment && (pSegment->mnPos == 0) ) + { + if( !pSegment->mxProgress ) + pSegment->mxProgress.reset( new ScfProgressBar( *this, pSegment ) ); + return *pSegment->mxProgress; + } + return *this; +} + +bool ScfProgressBar::IsFull() const +{ + OSL_ENSURE( mbInProgress && mpCurrSegment, "ScfProgressBar::IsFull - no segment started" ); + return mpCurrSegment && (mpCurrSegment->mnPos >= mpCurrSegment->mnSize); +} + +void ScfProgressBar::ActivateSegment( sal_Int32 nSegment ) +{ + OSL_ENSURE( mnTotalSize > 0, "ScfProgressBar::ActivateSegment - progress range is zero" ); + if( mnTotalSize > 0 ) + SetCurrSegment( GetSegment( nSegment ) ); +} + +void ScfProgressBar::ProgressAbs( std::size_t nPos ) +{ + OSL_ENSURE( mbInProgress && mpCurrSegment, "ScfProgressBar::ProgressAbs - no segment started" ); + if( mpCurrSegment ) + { + OSL_ENSURE( mpCurrSegment->mnPos <= nPos, "ScfProgressBar::ProgressAbs - delta pos < 0" ); + OSL_ENSURE( nPos <= mpCurrSegment->mnSize, "ScfProgressBar::ProgressAbs - segment overflow" ); + if( (mpCurrSegment->mnPos < nPos) && (nPos <= mpCurrSegment->mnSize) ) + { + IncreaseProgressBar( nPos - mpCurrSegment->mnPos ); + mpCurrSegment->mnPos = nPos; + } + } +} + +void ScfProgressBar::Progress( std::size_t nDelta ) +{ + ProgressAbs( mpCurrSegment ? (mpCurrSegment->mnPos + nDelta) : 0 ); +} + +ScfSimpleProgressBar::ScfSimpleProgressBar( std::size_t nSize, SfxObjectShell* pDocShell, const OUString& rText ) : + maProgress( pDocShell, rText ) +{ + Init( nSize ); +} + +ScfSimpleProgressBar::ScfSimpleProgressBar(std::size_t nSize, SfxObjectShell* pDocShell, TranslateId pResId) + : maProgress(pDocShell, pResId) +{ + Init( nSize ); +} + +void ScfSimpleProgressBar::Init( std::size_t nSize ) +{ + sal_Int32 nSegment = maProgress.AddSegment( nSize ); + if( nSegment >= 0 ) + maProgress.ActivateSegment( nSegment ); +} + +ScfStreamProgressBar::ScfStreamProgressBar( SvStream& rStrm, SfxObjectShell* pDocShell ) : + mrStrm( rStrm ) +{ + Init( pDocShell, ScResId( STR_LOAD_DOC ) ); +} + +void ScfStreamProgressBar::Progress() +{ + mxProgress->ProgressAbs( mrStrm.Tell() ); +} + +void ScfStreamProgressBar::Init( SfxObjectShell* pDocShell, const OUString& rText ) +{ + sal_uInt64 const nSize = mrStrm.TellEnd(); + mxProgress.reset( new ScfSimpleProgressBar( nSize, pDocShell, rText ) ); + Progress(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/filter/ftools/ftools.cxx b/sc/source/filter/ftools/ftools.cxx new file mode 100644 index 000000000..5c219e3c3 --- /dev/null +++ b/sc/source/filter/ftools/ftools.cxx @@ -0,0 +1,365 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <ftools.hxx> +#include <osl/diagnose.h> +#include <osl/thread.h> +#include <tools/color.hxx> +#include <unotools/charclass.hxx> +#include <svl/itempool.hxx> +#include <svl/itemset.hxx> +#include <svl/poolitem.hxx> +#include <sot/storage.hxx> +#include <o3tl/string_view.hxx> + +#include <math.h> +#include <global.hxx> +#include <stlpool.hxx> +#include <stlsheet.hxx> +#include <compiler.hxx> + +#include <orcusfiltersimpl.hxx> + + +// ScFilterTools::ReadLongDouble() + +void ScfTools::ReadLongDouble(SvStream& rStrm, double& fResult) + +#ifdef __SIMPLE_FUNC // for <=VC 1.5 +{ + long double fRet; + bool bOk = 10 == rStrm.Read(&fRet, 10); + if (!bOk) + return; + fResult = static_cast<double>(fRet); +} +#undef __SIMPLE_FUNC + +#else // detailed for all others +{ + +/* +"Mapping - Guide" 10-Byte Intel + +77777777 77666666 66665555 55555544 44444444 33333333 33222222 22221111 11111100 00000000 x10 +98765432 10987654 32109876 54321098 76543210 98765432 10987654 32109876 54321098 76543210 Bit-# total +9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0 Byte-# +76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 Bit-# in Byte +SEEEEEEE EEEEEEEE IMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM Group +01111110 00000000 06665555 55555544 44444444 33333333 33222222 22221111 11111100 00000000 x10 +14321098 76543210 02109876 54321098 76543210 98765432 10987654 32109876 54321098 76543210 Bit in Group +*/ + + long double lfDouble; + long double lfFactor = 256.0; + sal_uInt8 pDouble10[ 10 ]; + + bool bOk = 10 == rStrm.ReadBytes(pDouble10, 10); // Intel-10 in pDouble10 + if (!bOk) + return; + + lfDouble = static_cast< long double >( pDouble10[ 7 ] ); // Byte 7 + lfDouble *= lfFactor; + lfDouble += static_cast< long double >( pDouble10[ 6 ] ); // Byte 6 + lfDouble *= lfFactor; + lfDouble += static_cast< long double >( pDouble10[ 5 ] ); // Byte 5 + lfDouble *= lfFactor; + lfDouble += static_cast< long double >( pDouble10[ 4 ] ); // Byte 4 + lfDouble *= lfFactor; + lfDouble += static_cast< long double >( pDouble10[ 3 ] ); // Byte 3 + lfDouble *= lfFactor; + lfDouble += static_cast< long double >( pDouble10[ 2 ] ); // Byte 2 + lfDouble *= lfFactor; + lfDouble += static_cast< long double >( pDouble10[ 1 ] ); // Byte 1 + lfDouble *= lfFactor; + lfDouble += static_cast< long double >( pDouble10[ 0 ] ); // Byte 0 + + // For value 0.0 all bits are zero; pow(2.0,-16446) does not work with CSet compilers + if( lfDouble != 0.0 ) + { + // exponent + sal_Int32 nExp; + nExp = pDouble10[ 9 ] & 0x7F; + nExp <<= 8; + nExp += pDouble10[ 8 ]; + nExp -= 16446; + + lfDouble *= pow( 2.0, static_cast< double >( nExp ) ); + } + + // sign + if( pDouble10[ 9 ] & 0x80 ) + lfDouble *= static_cast< long double >( -1.0 ); + + fResult = static_cast<double>(lfDouble); +} +#endif + +// *** common methods *** ----------------------------------------------------- + +rtl_TextEncoding ScfTools::GetSystemTextEncoding() +{ + return osl_getThreadTextEncoding(); +} + +OUString ScfTools::GetHexStr( sal_uInt16 nValue ) +{ + const char pHex[] = "0123456789ABCDEF"; + OUString aStr = OUStringChar( pHex[ nValue >> 12 ] ) + + OUStringChar( pHex[ (nValue >> 8) & 0x000F ] ) + + OUStringChar( pHex[ (nValue >> 4) & 0x000F ] ) + + OUStringChar( pHex[ nValue & 0x000F ] ); + return aStr; +} + +sal_uInt8 ScfTools::GetMixedColorComp( sal_uInt8 nFore, sal_uInt8 nBack, sal_uInt8 nTrans ) +{ + sal_Int32 nTemp = ((static_cast< sal_Int32 >( nBack ) - nFore) * nTrans) / 0x80 + nFore; + return static_cast< sal_uInt8 >( nTemp ); +} + +Color ScfTools::GetMixedColor( const Color& rFore, const Color& rBack, sal_uInt8 nTrans ) +{ + return Color( + GetMixedColorComp( rFore.GetRed(), rBack.GetRed(), nTrans ), + GetMixedColorComp( rFore.GetGreen(), rBack.GetGreen(), nTrans ), + GetMixedColorComp( rFore.GetBlue(), rBack.GetBlue(), nTrans ) ); +} + +// *** conversion of names *** ------------------------------------------------ + +/* XXX As in sc/source/core/tool/rangenam.cxx ScRangeData::IsValidName() */ + +OUString ScfTools::ConvertToScDefinedName(const OUString& rName ) +{ + //fdo#37872: we don't allow points in range names any more + OUString sName = rName.replace(u'.', + u'_'); + sal_Int32 nLen = sName.getLength(); + if( nLen && !ScCompiler::IsCharFlagAllConventions( sName, 0, ScCharFlags::CharName ) ) + sName = sName.replaceAt( 0, 1, u"_" ); + for( sal_Int32 nPos = 1; nPos < nLen; ++nPos ) + if( !ScCompiler::IsCharFlagAllConventions( sName, nPos, ScCharFlags::Name ) ) + sName = sName.replaceAt( nPos, 1, u"_" ); + return sName; +} + +// *** streams and storages *** ----------------------------------------------- + +tools::SvRef<SotStorage> ScfTools::OpenStorageRead( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrgName ) +{ + tools::SvRef<SotStorage> xSubStrg; + if( xStrg.is() && xStrg->IsContained( rStrgName ) ) + xSubStrg = xStrg->OpenSotStorage( rStrgName, StreamMode::STD_READ ); + return xSubStrg; +} + +tools::SvRef<SotStorage> ScfTools::OpenStorageWrite( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrgName ) +{ + tools::SvRef<SotStorage> xSubStrg; + if( xStrg.is() ) + xSubStrg = xStrg->OpenSotStorage( rStrgName, StreamMode::STD_WRITE ); + return xSubStrg; +} + +tools::SvRef<SotStorageStream> ScfTools::OpenStorageStreamRead( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrmName ) +{ + tools::SvRef<SotStorageStream> xStrm; + if( xStrg.is() && xStrg->IsContained( rStrmName ) && xStrg->IsStream( rStrmName ) ) + xStrm = xStrg->OpenSotStream( rStrmName, StreamMode::STD_READ ); + return xStrm; +} + +tools::SvRef<SotStorageStream> ScfTools::OpenStorageStreamWrite( tools::SvRef<SotStorage> const & xStrg, const OUString& rStrmName ) +{ + OSL_ENSURE( !xStrg.is() || !xStrg->IsContained( rStrmName ), "ScfTools::OpenStorageStreamWrite - stream exists already" ); + tools::SvRef<SotStorageStream> xStrm; + if( xStrg.is() ) + xStrm = xStrg->OpenSotStream( rStrmName, StreamMode::STD_WRITE | StreamMode::TRUNC ); + return xStrm; +} + +// *** item handling *** ------------------------------------------------------ + +bool ScfTools::CheckItem( const SfxItemSet& rItemSet, sal_uInt16 nWhichId, bool bDeep ) +{ + return rItemSet.GetItemState( nWhichId, bDeep ) == SfxItemState::SET; +} + +bool ScfTools::CheckItems( const SfxItemSet& rItemSet, const sal_uInt16* pnWhichIds, bool bDeep ) +{ + OSL_ENSURE( pnWhichIds, "ScfTools::CheckItems - no which id list" ); + for( const sal_uInt16* pnWhichId = pnWhichIds; *pnWhichId != 0; ++pnWhichId ) + if( CheckItem( rItemSet, *pnWhichId, bDeep ) ) + return true; + return false; +} + +void ScfTools::PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, sal_uInt16 nWhichId, bool bSkipPoolDef ) +{ + if( !bSkipPoolDef || (rItem != rItemSet.GetPool()->GetDefaultItem( nWhichId )) ) + { + rItemSet.Put( rItem.CloneSetWhich(nWhichId) ); + } +} + +void ScfTools::PutItem( SfxItemSet& rItemSet, const SfxPoolItem& rItem, bool bSkipPoolDef ) +{ + PutItem( rItemSet, rItem, rItem.Which(), bSkipPoolDef ); +} + +// *** style sheet handling *** ----------------------------------------------- + +namespace { + +ScStyleSheet& lclMakeStyleSheet( ScStyleSheetPool& rPool, const OUString& rStyleName, SfxStyleFamily eFamily, bool bForceName ) +{ + // find an unused name + OUString aNewName( rStyleName ); + sal_Int32 nIndex = 0; + SfxStyleSheetBase* pOldStyleSheet = nullptr; + while( SfxStyleSheetBase* pStyleSheet = rPool.Find( aNewName, eFamily ) ) + { + if( !pOldStyleSheet ) + pOldStyleSheet = pStyleSheet; + aNewName = rStyleName + " " + OUString::number( ++nIndex ); + } + + // rename existing style + if( pOldStyleSheet && bForceName ) + { + pOldStyleSheet->SetName( aNewName ); + aNewName = rStyleName; + } + + // create new style sheet + return static_cast< ScStyleSheet& >( rPool.Make( aNewName, eFamily, SfxStyleSearchBits::UserDefined ) ); +} + +} // namespace + +ScStyleSheet& ScfTools::MakeCellStyleSheet( ScStyleSheetPool& rPool, const OUString& rStyleName, bool bForceName ) +{ + return lclMakeStyleSheet( rPool, rStyleName, SfxStyleFamily::Para, bForceName ); +} + +ScStyleSheet& ScfTools::MakePageStyleSheet( ScStyleSheetPool& rPool, const OUString& rStyleName, bool bForceName ) +{ + return lclMakeStyleSheet( rPool, rStyleName, SfxStyleFamily::Page, bForceName ); +} + +// *** byte string import operations *** -------------------------------------- + +OString ScfTools::read_zeroTerminated_uInt8s_ToOString(SvStream& rStrm, sal_Int32& rnBytesLeft) +{ + OString aRet(::read_zeroTerminated_uInt8s_ToOString(rStrm)); + rnBytesLeft -= aRet.getLength(); //we read this number of bytes anyway + if (rStrm.good()) //if the stream is happy we read the null terminator as well + --rnBytesLeft; + return aRet; +} + +void ScfTools::AppendCString( SvStream& rStrm, OUString& rString, rtl_TextEncoding eTextEnc ) +{ + rString += ::read_zeroTerminated_uInt8s_ToOUString(rStrm, eTextEnc); +} + +// *** HTML table names <-> named range names *** ----------------------------- + +const OUString& ScfTools::GetHTMLDocName() +{ + static const OUString saHTMLDoc( "HTML_all" ); + return saHTMLDoc; +} + +const OUString& ScfTools::GetHTMLTablesName() +{ + static const OUString saHTMLTables( "HTML_tables" ); + return saHTMLTables; +} + +const OUString& ScfTools::GetHTMLIndexPrefix() +{ + static const OUString saHTMLIndexPrefix( "HTML_" ); + return saHTMLIndexPrefix; + +} + +const OUString& ScfTools::GetHTMLNamePrefix() +{ + static const OUString saHTMLNamePrefix( "HTML__" ); + return saHTMLNamePrefix; +} + +OUString ScfTools::GetNameFromHTMLIndex( sal_uInt32 nIndex ) +{ + OUString aName = GetHTMLIndexPrefix() + + OUString::number( static_cast< sal_Int32 >( nIndex ) ); + return aName; +} + +OUString ScfTools::GetNameFromHTMLName( std::u16string_view rTabName ) +{ + return GetHTMLNamePrefix() + rTabName; +} + +bool ScfTools::IsHTMLDocName( std::u16string_view rSource ) +{ + return o3tl::equalsIgnoreAsciiCase( rSource, GetHTMLDocName() ); +} + +bool ScfTools::IsHTMLTablesName( std::u16string_view rSource ) +{ + return o3tl::equalsIgnoreAsciiCase( rSource, GetHTMLTablesName() ); +} + +bool ScfTools::GetHTMLNameFromName( const OUString& rSource, OUString& rName ) +{ + rName.clear(); + if( rSource.startsWithIgnoreAsciiCase( GetHTMLNamePrefix() ) ) + { + rName = rSource.copy( GetHTMLNamePrefix().getLength() ); + ScGlobal::AddQuotes( rName, '"', false ); + } + else if( rSource.startsWithIgnoreAsciiCase( GetHTMLIndexPrefix() ) ) + { + OUString aIndex( rSource.copy( GetHTMLIndexPrefix().getLength() ) ); + if( CharClass::isAsciiNumeric( aIndex ) && (aIndex.toInt32() > 0) ) + rName = aIndex; + } + return !rName.isEmpty(); +} + +ScFormatFilterPluginImpl::ScFormatFilterPluginImpl() {} +ScFormatFilterPluginImpl::~ScFormatFilterPluginImpl() {} + +ScOrcusFilters* ScFormatFilterPluginImpl::GetOrcusFilters() +{ + static ScOrcusFiltersImpl aImpl; + return &aImpl; +} + +ScFormatFilterPlugin * ScFilterCreate() +{ + return new ScFormatFilterPluginImpl(); +} + +// implementation class inside the filters + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/filter/ftools/sharedformulagroups.cxx b/sc/source/filter/ftools/sharedformulagroups.cxx new file mode 100644 index 000000000..9b028d9eb --- /dev/null +++ b/sc/source/filter/ftools/sharedformulagroups.cxx @@ -0,0 +1,40 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <memory> +#include <sharedformulagroups.hxx> +#include <tokenarray.hxx> + +namespace sc { + +void SharedFormulaGroups::set( size_t nSharedId, std::unique_ptr<ScTokenArray> pArray ) +{ + m_Store.try_emplace(nSharedId, std::move(pArray), ScAddress(ScAddress::INITIALIZE_INVALID)); +} + +void SharedFormulaGroups::set( size_t nSharedId, std::unique_ptr<ScTokenArray> pArray, const ScAddress& rOrigin ) +{ + m_Store.try_emplace(nSharedId, std::move(pArray), rOrigin); +} + +const ScTokenArray* SharedFormulaGroups::get( size_t nSharedId ) const +{ + StoreType::const_iterator const it = m_Store.find(nSharedId); + return it == m_Store.end() ? nullptr : it->second.getTokenArray(); +} + +const SharedFormulaGroupEntry* SharedFormulaGroups::getEntry( size_t nSharedId ) const +{ + StoreType::const_iterator const it = m_Store.find(nSharedId); + return it == m_Store.end() ? nullptr : &(it->second); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |