diff options
author | Arkadiy Illarionov <qarkai@gmail.com> | 2019-08-10 18:34:33 +0300 |
---|---|---|
committer | Arkadiy Illarionov <qarkai@gmail.com> | 2019-08-22 18:03:16 +0200 |
commit | 38001018fa06f721cf87edae923b54ce9a5ab5a7 (patch) | |
tree | 1872cc7cbedcc093a4ea3f0acaf75ac90b37fba9 /package/source/xstor | |
parent | 3a2eeafc30b6b6cf3dafc6d3b94144ba54a8b48b (diff) |
Simplify Sequence iterations in package
Use range-based loops, STL and comphelper functions
Change-Id: Ibd836b9b2df2f30b42f2d7a621188d78f5b53196
Reviewed-on: https://gerrit.libreoffice.org/77246
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Reviewed-by: Arkadiy Illarionov <qarkai@gmail.com>
Diffstat (limited to 'package/source/xstor')
-rw-r--r-- | package/source/xstor/ocompinstream.cxx | 109 | ||||
-rw-r--r-- | package/source/xstor/owriteablestream.cxx | 345 | ||||
-rw-r--r-- | package/source/xstor/xfactory.cxx | 27 | ||||
-rw-r--r-- | package/source/xstor/xstorage.cxx | 277 |
4 files changed, 334 insertions, 424 deletions
diff --git a/package/source/xstor/ocompinstream.cxx b/package/source/xstor/ocompinstream.cxx index 52565b7933cf..ac4e3f101a07 100644 --- a/package/source/xstor/ocompinstream.cxx +++ b/package/source/xstor/ocompinstream.cxx @@ -21,6 +21,7 @@ #include <com/sun/star/embed/StorageFormats.hpp> #include <com/sun/star/io/IOException.hpp> #include <com/sun/star/lang/DisposedException.hpp> +#include <comphelper/sequence.hxx> #include <cppuhelper/queryinterface.hxx> #include <osl/diagnose.h> #include <sal/log.hxx> @@ -285,6 +286,17 @@ sal_Bool SAL_CALL OInputCompStream::hasByID( const OUString& sID ) return false; } +namespace +{ + +const beans::StringPair* lcl_findPairByName(const uno::Sequence<beans::StringPair>& rSeq, const OUString& rName) +{ + return std::find_if(rSeq.begin(), rSeq.end(), + [&rName](const beans::StringPair& rPair) { return rPair.First == rName; }); +} + +} + OUString SAL_CALL OInputCompStream::getTargetByID( const OUString& sID ) { ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ); @@ -298,10 +310,10 @@ OUString SAL_CALL OInputCompStream::getTargetByID( const OUString& sID ) if ( m_nStorageType != embed::StorageFormats::OFOPXML ) throw uno::RuntimeException(); - uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID ); - for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ ) - if ( aSeq[nInd].First == "Target" ) - return aSeq[nInd].Second; + const uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID ); + auto pRel = lcl_findPairByName(aSeq, "Target"); + if (pRel != aSeq.end()) + return pRel->Second; return OUString(); } @@ -319,10 +331,10 @@ OUString SAL_CALL OInputCompStream::getTypeByID( const OUString& sID ) if ( m_nStorageType != embed::StorageFormats::OFOPXML ) throw uno::RuntimeException(); - uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID ); - for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ ) - if ( aSeq[nInd].First == "Type" ) - return aSeq[nInd].Second; + const uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID ); + auto pRel = lcl_findPairByName(aSeq, "Type"); + if (pRel != aSeq.end()) + return pRel->Second; return OUString(); } @@ -341,15 +353,13 @@ uno::Sequence< beans::StringPair > SAL_CALL OInputCompStream::getRelationshipByI throw uno::RuntimeException(); // TODO/LATER: in future the unification of the ID could be checked - uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); - for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ ) - for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ ) - if ( aSeq[nInd1][nInd2].First == "Id" ) - { - if ( aSeq[nInd1][nInd2].Second == sID ) - return aSeq[nInd1]; - break; - } + const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); + const beans::StringPair aIDRel("Id", sID); + auto pRel = std::find_if(aSeq.begin(), aSeq.end(), + [&aIDRel](const uno::Sequence<beans::StringPair>& rRel){ + return std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end(); }); + if (pRel != aSeq.end()) + return *pRel; throw container::NoSuchElementException(); } @@ -367,24 +377,17 @@ uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OInputCompStream::g if ( m_nStorageType != embed::StorageFormats::OFOPXML ) throw uno::RuntimeException(); - uno::Sequence< uno::Sequence< beans::StringPair > > aResult; - sal_Int32 nEntriesNum = 0; - // TODO/LATER: in future the unification of the ID could be checked - uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); - for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ ) - for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ ) - if ( aSeq[nInd1][nInd2].First == "Type" ) - { - if ( aSeq[nInd1][nInd2].Second == sType ) - { - aResult.realloc( nEntriesNum ); - aResult[nEntriesNum-1] = aSeq[nInd1]; - } - break; - } - - return aResult; + const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); + const beans::StringPair aTypeRel("Type", sType); + std::vector< uno::Sequence<beans::StringPair> > aResult; + aResult.reserve(aSeq.getLength()); + + std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResult), + [&aTypeRel](const uno::Sequence<beans::StringPair>& rRel) { + return std::find(rRel.begin(), rRel.end(), aTypeRel) != rRel.end(); }); + + return comphelper::containerToSequence(aResult); } uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OInputCompStream::getAllRelationships() @@ -401,15 +404,14 @@ uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OInputCompStream::g throw uno::RuntimeException(); // TODO/LATER: in future the information could be taken directly from m_pImpl when possible - uno::Sequence< uno::Sequence< beans::StringPair > > aResult; - for ( sal_Int32 aInd = 0; aInd < m_aProperties.getLength(); aInd++ ) - if ( m_aProperties[aInd].Name == "RelationsInfo" ) - { - if ( m_aProperties[aInd].Value >>= aResult ) - return aResult; - - break; - } + auto pProp = std::find_if(std::cbegin(m_aProperties), std::cend(m_aProperties), + [](const beans::PropertyValue& rProp) { return rProp.Name == "RelationsInfo"; }); + if (pProp != std::cend(m_aProperties)) + { + uno::Sequence< uno::Sequence< beans::StringPair > > aResult; + if (pProp->Value >>= aResult) + return aResult; + } throw io::IOException(); // the relations info could not be read } @@ -503,13 +505,9 @@ void SAL_CALL OInputCompStream::setPropertyValue( const OUString& aPropertyName, } // all the provided properties are accessible - for ( sal_Int32 aInd = 0; aInd < m_aProperties.getLength(); aInd++ ) - { - if ( m_aProperties[aInd].Name == aPropertyName ) - { - throw beans::PropertyVetoException(); // TODO - } - } + if (std::any_of(std::cbegin(m_aProperties), std::cend(m_aProperties), + [&aPropertyName](const beans::PropertyValue& rProp) { return rProp.Name == aPropertyName; })) + throw beans::PropertyVetoException(); // TODO throw beans::UnknownPropertyException(); // TODO } @@ -534,13 +532,10 @@ uno::Any SAL_CALL OInputCompStream::getPropertyValue( const OUString& aProp ) throw beans::UnknownPropertyException(); // TODO // all the provided properties are accessible - for ( sal_Int32 aInd = 0; aInd < m_aProperties.getLength(); aInd++ ) - { - if ( m_aProperties[aInd].Name == aPropertyName ) - { - return m_aProperties[aInd].Value; - } - } + auto pProp = std::find_if(std::cbegin(m_aProperties), std::cend(m_aProperties), + [&aPropertyName](const beans::PropertyValue& rProp) { return rProp.Name == aPropertyName; }); + if (pProp != std::cend(m_aProperties)) + return pProp->Value; throw beans::UnknownPropertyException(); // TODO } diff --git a/package/source/xstor/owriteablestream.cxx b/package/source/xstor/owriteablestream.cxx index 1c9332e9e845..27a3d4daa5b7 100644 --- a/package/source/xstor/owriteablestream.cxx +++ b/package/source/xstor/owriteablestream.cxx @@ -45,6 +45,7 @@ #include <comphelper/storagehelper.hxx> #include <comphelper/ofopxmlhelper.hxx> #include <comphelper/refcountedmutex.hxx> +#include <comphelper/sequence.hxx> #include <rtl/digest.h> #include <rtl/instance.hxx> @@ -92,10 +93,9 @@ bool PackageEncryptionDatasEqual( const ::comphelper::SequenceAsHashMap& aHash1, bResult = ( ( aIter->second >>= aKey1 ) && aKey1.hasElements() ); if ( bResult ) { - uno::Sequence< sal_Int8 > aKey2 = aHash2.getUnpackedValueOrDefault( aIter->first, uno::Sequence< sal_Int8 >() ); - bResult = ( aKey1.getLength() == aKey2.getLength() ); - for ( sal_Int32 nInd = 0; bResult && nInd < aKey1.getLength(); nInd++ ) - bResult = ( aKey1[nInd] == aKey2[nInd] ); + const uno::Sequence< sal_Int8 > aKey2 = aHash2.getUnpackedValueOrDefault( aIter->first, uno::Sequence< sal_Int8 >() ); + bResult = aKey1.getLength() == aKey2.getLength() + && std::equal(std::cbegin(aKey1), std::cend(aKey1), aKey2.begin(), aKey2.end()); } } @@ -142,14 +142,8 @@ uno::Any GetEncryptionKeyProperty_Impl( const uno::Reference< beans::XPropertySe bool SequencesEqual( const uno::Sequence< sal_Int8 >& aSequence1, const uno::Sequence< sal_Int8 >& aSequence2 ) { - if ( aSequence1.getLength() != aSequence2.getLength() ) - return false; - - for ( sal_Int32 nInd = 0; nInd < aSequence1.getLength(); nInd++ ) - if ( aSequence1[nInd] != aSequence2[nInd] ) - return false; - - return true; + return aSequence1.getLength() == aSequence2.getLength() + && std::equal(aSequence1.begin(), aSequence1.end(), aSequence2.begin(), aSequence2.end()); } bool SequencesEqual( const uno::Sequence< beans::NamedValue >& aSequence1, const uno::Sequence< beans::NamedValue >& aSequence2 ) @@ -157,35 +151,35 @@ bool SequencesEqual( const uno::Sequence< beans::NamedValue >& aSequence1, const if ( aSequence1.getLength() != aSequence2.getLength() ) return false; - for ( sal_Int32 nInd = 0; nInd < aSequence1.getLength(); nInd++ ) + for ( const auto& rProp1 : aSequence1 ) { bool bHasMember = false; uno::Sequence< sal_Int8 > aMember1; sal_Int32 nMember1 = 0; - if ( aSequence1[nInd].Value >>= aMember1 ) + if ( rProp1.Value >>= aMember1 ) { - for ( sal_Int32 nInd2 = 0; nInd2 < aSequence2.getLength(); nInd2++ ) + for ( const auto& rProp2 : aSequence2 ) { - if ( aSequence1[nInd].Name == aSequence2[nInd2].Name ) + if ( rProp1.Name == rProp2.Name ) { bHasMember = true; uno::Sequence< sal_Int8 > aMember2; - if ( !( aSequence2[nInd2].Value >>= aMember2 ) || !SequencesEqual( aMember1, aMember2 ) ) + if ( !( rProp2.Value >>= aMember2 ) || !SequencesEqual( aMember1, aMember2 ) ) return false; } } } - else if ( aSequence1[nInd].Value >>= nMember1 ) + else if ( rProp1.Value >>= nMember1 ) { - for ( sal_Int32 nInd2 = 0; nInd2 < aSequence2.getLength(); nInd2++ ) + for ( const auto& rProp2 : aSequence2 ) { - if ( aSequence1[nInd].Name == aSequence2[nInd2].Name ) + if ( rProp1.Name == rProp2.Name ) { bHasMember = true; sal_Int32 nMember2 = 0; - if ( !( aSequence2[nInd2].Value >>= nMember2 ) || nMember1 != nMember2 ) + if ( !( rProp2.Value >>= nMember2 ) || nMember1 != nMember2 ) return false; } } @@ -253,6 +247,12 @@ uno::Reference< io::XStream > CreateMemoryStream( const uno::Reference< uno::XCo uno::UNO_QUERY_THROW); } +const beans::StringPair* lcl_findPairByName(const uno::Sequence<beans::StringPair>& rSeq, const OUString& rName) +{ + return std::find_if(rSeq.begin(), rSeq.end(), + [&rName](const beans::StringPair& rPair) { return rPair.First == rName; }); +} + } // anonymous namespace OWriteStream_Impl::OWriteStream_Impl( OStorage_Impl* pParent, @@ -371,11 +371,11 @@ bool OWriteStream_Impl::IsEncrypted() } bool bToBeEncr = false; - for ( sal_Int32 nInd = 0; nInd < m_aProps.getLength(); nInd++ ) + for ( const auto& rProp : std::as_const(m_aProps) ) { - if ( m_aProps[nInd].Name == "Encrypted" ) + if ( rProp.Name == "Encrypted" ) { - if ( !( m_aProps[nInd].Value >>= bToBeEncr ) ) + if ( !( rProp.Value >>= bToBeEncr ) ) { SAL_WARN( "package.xstor", "The property has wrong type!" ); } @@ -421,10 +421,10 @@ void OWriteStream_Impl::SetDecrypted() m_bHasCachedEncryptionData = false; m_aEncryptionData.clear(); - for ( sal_Int32 nInd = 0; nInd < m_aProps.getLength(); nInd++ ) + for ( auto& rProp : m_aProps ) { - if ( m_aProps[nInd].Name == "Encrypted" ) - m_aProps[nInd].Value <<= false; + if ( rProp.Name == "Encrypted" ) + rProp.Value <<= false; } } @@ -444,10 +444,10 @@ void OWriteStream_Impl::SetEncrypted( const ::comphelper::SequenceAsHashMap& aEn m_bHasDataToFlush = true; // introduce encryption info - for ( sal_Int32 nInd = 0; nInd < m_aProps.getLength(); nInd++ ) + for ( auto& rProp : m_aProps ) { - if ( m_aProps[nInd].Name == "Encrypted" ) - m_aProps[nInd].Value <<= true; + if ( rProp.Name == "Encrypted" ) + rProp.Value <<= true; } m_bUseCommonEncryption = false; // very important to set it to false @@ -714,29 +714,29 @@ void OWriteStream_Impl::InsertStreamDirectly( const uno::Reference< io::XInputSt bool bCompressed = false; OUString aComprPropName( "Compressed" ); OUString aMedTypePropName( "MediaType" ); - for ( sal_Int32 nInd = 0; nInd < aProps.getLength(); nInd++ ) + for ( const auto& rProp : aProps ) { - if ( aProps[nInd].Name == aComprPropName ) + if ( rProp.Name == aComprPropName ) { bCompressedIsSet = true; - aProps[nInd].Value >>= bCompressed; + rProp.Value >>= bCompressed; } else if ( ( m_nStorageType == embed::StorageFormats::OFOPXML || m_nStorageType == embed::StorageFormats::PACKAGE ) - && aProps[nInd].Name == aMedTypePropName ) + && rProp.Name == aMedTypePropName ) { - xPropertySet->setPropertyValue( aProps[nInd].Name, aProps[nInd].Value ); + xPropertySet->setPropertyValue( rProp.Name, rProp.Value ); } - else if ( m_nStorageType == embed::StorageFormats::PACKAGE && aProps[nInd].Name == "UseCommonStoragePasswordEncryption" ) - aProps[nInd].Value >>= m_bUseCommonEncryption; + else if ( m_nStorageType == embed::StorageFormats::PACKAGE && rProp.Name == "UseCommonStoragePasswordEncryption" ) + rProp.Value >>= m_bUseCommonEncryption; else throw lang::IllegalArgumentException(); // if there are cached properties update them - if ( aProps[nInd].Name == aMedTypePropName || aProps[nInd].Name == aComprPropName ) - for ( sal_Int32 nMemInd = 0; nMemInd < m_aProps.getLength(); nMemInd++ ) + if ( rProp.Name == aMedTypePropName || rProp.Name == aComprPropName ) + for ( auto& rMemProp : m_aProps ) { - if ( aProps[nInd].Name == m_aProps[nMemInd].Name ) - m_aProps[nMemInd].Value = aProps[nInd].Value; + if ( rProp.Name == rMemProp.Name ) + rMemProp.Value = rProp.Value; } } @@ -823,18 +823,18 @@ void OWriteStream_Impl::Commit() // copy properties to the package stream uno::Reference< beans::XPropertySet > xPropertySet( xNewPackageStream, uno::UNO_QUERY_THROW ); - for ( sal_Int32 nInd = 0; nInd < m_aProps.getLength(); nInd++ ) + for ( auto& rProp : m_aProps ) { - if ( m_aProps[nInd].Name == "Size" ) + if ( rProp.Name == "Size" ) { if ( m_pAntiImpl && !m_bHasInsertedStreamOptimization && m_pAntiImpl->m_xSeekable.is() ) { - m_aProps[nInd].Value <<= m_pAntiImpl->m_xSeekable->getLength(); - xPropertySet->setPropertyValue( m_aProps[nInd].Name, m_aProps[nInd].Value ); + rProp.Value <<= m_pAntiImpl->m_xSeekable->getLength(); + xPropertySet->setPropertyValue( rProp.Name, rProp.Value ); } } else - xPropertySet->setPropertyValue( m_aProps[nInd].Name, m_aProps[nInd].Value ); + xPropertySet->setPropertyValue( rProp.Name, rProp.Value ); } if ( m_bUseCommonEncryption ) @@ -1051,10 +1051,10 @@ uno::Sequence< beans::PropertyValue > OWriteStream_Impl::ReadPackageStreamProper // TODO: may be also raw stream should be marked uno::Reference< beans::XPropertySet > xPropSet( m_xPackageStream, uno::UNO_QUERY_THROW ); - for ( sal_Int32 nInd = 0; nInd < aResult.getLength(); nInd++ ) + for ( auto& rProp : aResult ) { try { - aResult[nInd].Value = xPropSet->getPropertyValue( aResult[nInd].Name ); + rProp.Value = xPropSet->getPropertyValue( rProp.Name ); } catch( const uno::Exception& ) { @@ -2186,10 +2186,10 @@ void OWriteStream::CloseOutput_Impl() if ( !m_xSeekable.is() ) throw uno::RuntimeException(); - for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ ) + for ( auto& rProp : m_pImpl->m_aProps ) { - if ( m_pImpl->m_aProps[nInd].Name == "Size" ) - m_pImpl->m_aProps[nInd].Value <<= m_xSeekable->getLength(); + if ( rProp.Name == "Size" ) + rProp.Value <<= m_xSeekable->getLength(); } } } @@ -2511,10 +2511,10 @@ OUString SAL_CALL OWriteStream::getTargetByID( const OUString& sID ) if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML ) throw uno::RuntimeException(); - uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID ); - for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ ) - if ( aSeq[nInd].First == "Target" ) - return aSeq[nInd].Second; + const uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID ); + auto pRel = lcl_findPairByName(aSeq, "Target"); + if (pRel != aSeq.end()) + return pRel->Second; return OUString(); } @@ -2532,10 +2532,10 @@ OUString SAL_CALL OWriteStream::getTypeByID( const OUString& sID ) if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML ) throw uno::RuntimeException(); - uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID ); - for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ ) - if ( aSeq[nInd].First == "Type" ) - return aSeq[nInd].Second; + const uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID ); + auto pRel = lcl_findPairByName(aSeq, "Type"); + if (pRel != aSeq.end()) + return pRel->Second; return OUString(); } @@ -2554,15 +2554,13 @@ uno::Sequence< beans::StringPair > SAL_CALL OWriteStream::getRelationshipByID( throw uno::RuntimeException(); // TODO/LATER: in future the unification of the ID could be checked - uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); - for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ ) - for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ ) - if ( aSeq[nInd1][nInd2].First == "Id" ) - { - if ( aSeq[nInd1][nInd2].Second == sID ) - return aSeq[nInd1]; - break; - } + const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); + const beans::StringPair aIDRel("Id", sID); + auto pRel = std::find_if(aSeq.begin(), aSeq.end(), + [&aIDRel](const uno::Sequence<beans::StringPair>& rRel) { + return std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end(); }); + if (pRel != aSeq.end()) + return *pRel; throw container::NoSuchElementException(); } @@ -2580,24 +2578,17 @@ uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OWriteStream::getRe if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML ) throw uno::RuntimeException(); - uno::Sequence< uno::Sequence< beans::StringPair > > aResult; - sal_Int32 nEntriesNum = 0; - // TODO/LATER: in future the unification of the ID could be checked - uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); - for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ ) - for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ ) - if ( aSeq[nInd1][nInd2].First == "Type" ) - { - if ( aSeq[nInd1][nInd2].Second == sType ) - { - aResult.realloc( nEntriesNum ); - aResult[nEntriesNum-1] = aSeq[nInd1]; - } - break; - } + const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); + const beans::StringPair aTypeRel("Type", sType); + std::vector< uno::Sequence<beans::StringPair> > aResult; + aResult.reserve(aSeq.getLength()); - return aResult; + std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResult), + [&aTypeRel](const uno::Sequence<beans::StringPair>& rRel) { + return std::find(rRel.begin(), rRel.end(), aTypeRel) != rRel.end(); }); + + return comphelper::containerToSequence(aResult); } uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OWriteStream::getAllRelationships() @@ -2629,21 +2620,18 @@ void SAL_CALL OWriteStream::insertRelationshipByID( const OUString& sID, const if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML ) throw uno::RuntimeException(); - OUString aIDTag( "Id" ); + const beans::StringPair aIDRel("Id", sID); sal_Int32 nIDInd = -1; // TODO/LATER: in future the unification of the ID could be checked uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); - for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ ) - for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ ) - if ( aSeq[nInd1][nInd2].First == aIDTag ) - { - if ( aSeq[nInd1][nInd2].Second == sID ) - nIDInd = nInd1; - - break; - } + for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ ) + { + const auto& rRel = aSeq[nInd]; + if (std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end()) + nIDInd = nInd; + } if ( nIDInd != -1 && !bReplace ) throw container::ElementExistException(); // TODO @@ -2654,20 +2642,14 @@ void SAL_CALL OWriteStream::insertRelationshipByID( const OUString& sID, const aSeq.realloc( nIDInd + 1 ); } - aSeq[nIDInd].realloc( aEntry.getLength() + 1 ); + std::vector<beans::StringPair> aResult; + aResult.reserve(aEntry.getLength() + 1); - aSeq[nIDInd][0].First = aIDTag; - aSeq[nIDInd][0].Second = sID; - sal_Int32 nIndTarget = 1; - for ( sal_Int32 nIndOrig = 0; - nIndOrig < aEntry.getLength(); - nIndOrig++ ) - { - if ( aEntry[nIndOrig].First != aIDTag ) - aSeq[nIDInd][nIndTarget++] = aEntry[nIndOrig]; - } + aResult.push_back(aIDRel); + std::copy_if(aEntry.begin(), aEntry.end(), std::back_inserter(aResult), + [](const beans::StringPair& rRel) { return rRel.First != "Id"; }); - aSeq[nIDInd].realloc( nIndTarget ); + aSeq[nIDInd] = comphelper::containerToSequence(aResult); m_pImpl->m_aNewRelInfo = aSeq; m_pImpl->m_xNewRelInfoStream.clear(); @@ -2688,26 +2670,22 @@ void SAL_CALL OWriteStream::removeRelationshipByID( const OUString& sID ) throw uno::RuntimeException(); uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); - for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ ) - for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ ) - if ( aSeq[nInd1][nInd2].First == "Id" ) - { - if ( aSeq[nInd1][nInd2].Second == sID ) - { - sal_Int32 nLength = aSeq.getLength(); - aSeq[nInd1] = aSeq[nLength-1]; - aSeq.realloc( nLength - 1 ); - - m_pImpl->m_aNewRelInfo = aSeq; - m_pImpl->m_xNewRelInfoStream.clear(); - m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED; + const beans::StringPair aIDRel("Id", sID); + auto pRel = std::find_if(std::cbegin(aSeq), std::cend(aSeq), + [&aIDRel](const uno::Sequence< beans::StringPair >& rRel) { + return std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end(); }); + if (pRel != std::cend(aSeq)) + { + auto nInd = static_cast<sal_Int32>(std::distance(std::cbegin(aSeq), pRel)); + comphelper::removeElementAt(aSeq, nInd); - // TODO/LATER: in future the unification of the ID could be checked - return; - } + m_pImpl->m_aNewRelInfo = aSeq; + m_pImpl->m_xNewRelInfoStream.clear(); + m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED; - break; - } + // TODO/LATER: in future the unification of the ID could be checked + return; + } throw container::NoSuchElementException(); } @@ -2726,67 +2704,41 @@ void SAL_CALL OWriteStream::insertRelationships( const uno::Sequence< uno::Sequ throw uno::RuntimeException(); OUString aIDTag( "Id" ); - uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); - uno::Sequence< uno::Sequence< beans::StringPair > > aResultSeq( aSeq.getLength() + aEntries.getLength() ); - sal_Int32 nResultInd = 0; - - for ( sal_Int32 nIndTarget1 = 0; nIndTarget1 < aSeq.getLength(); nIndTarget1++ ) - for ( sal_Int32 nIndTarget2 = 0; nIndTarget2 < aSeq[nIndTarget1].getLength(); nIndTarget2++ ) - if ( aSeq[nIndTarget1][nIndTarget2].First == aIDTag ) - { - sal_Int32 nIndSourceSame = -1; - - for ( sal_Int32 nIndSource1 = 0; nIndSource1 < aEntries.getLength(); nIndSource1++ ) - for ( sal_Int32 nIndSource2 = 0; nIndSource2 < aEntries[nIndSource1].getLength(); nIndSource2++ ) - { - if ( aEntries[nIndSource1][nIndSource2].First == aIDTag ) - { - if ( aEntries[nIndSource1][nIndSource2].Second == aSeq[nIndTarget1][nIndTarget2].Second ) - { - if ( !bReplace ) - throw container::ElementExistException(); - - nIndSourceSame = nIndSource1; - } - - break; - } - } - - if ( nIndSourceSame == -1 ) - { - // no such element in the provided sequence - aResultSeq[nResultInd++] = aSeq[nIndTarget1]; - } - - break; - } - - for ( sal_Int32 nIndSource1 = 0; nIndSource1 < aEntries.getLength(); nIndSource1++ ) - { - aResultSeq[nResultInd].realloc( aEntries[nIndSource1].getLength() ); - bool bHasID = false; - sal_Int32 nResInd2 = 1; - - for ( sal_Int32 nIndSource2 = 0; nIndSource2 < aEntries[nIndSource1].getLength(); nIndSource2++ ) - if ( aEntries[nIndSource1][nIndSource2].First == aIDTag ) - { - aResultSeq[nResultInd][0] = aEntries[nIndSource1][nIndSource2]; - bHasID = true; - } - else if ( nResInd2 < aResultSeq[nResultInd].getLength() ) - aResultSeq[nResultInd][nResInd2++] = aEntries[nIndSource1][nIndSource2]; - else + const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); + std::vector< uno::Sequence<beans::StringPair> > aResultVec; + aResultVec.reserve(aSeq.getLength() + aEntries.getLength()); + + std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResultVec), + [&aIDTag, &aEntries, bReplace](const uno::Sequence<beans::StringPair>& rTargetRel) { + auto pTargetPair = lcl_findPairByName(rTargetRel, aIDTag); + if (pTargetPair == rTargetRel.end()) + return false; + + bool bIsSourceSame = std::any_of(aEntries.begin(), aEntries.end(), + [&pTargetPair](const uno::Sequence<beans::StringPair>& rSourceEntry) { + return std::find(rSourceEntry.begin(), rSourceEntry.end(), *pTargetPair) != rSourceEntry.end(); }); + + if ( bIsSourceSame && !bReplace ) + throw container::ElementExistException(); + + // if no such element in the provided sequence + return !bIsSourceSame; + }); + + std::transform(aEntries.begin(), aEntries.end(), std::back_inserter(aResultVec), + [&aIDTag](const uno::Sequence<beans::StringPair>& rEntry) -> uno::Sequence<beans::StringPair> { + auto pPair = lcl_findPairByName(rEntry, aIDTag); + if (pPair == rEntry.end()) throw io::IOException(); // TODO: illegal relation ( no ID ) - if ( !bHasID ) - throw io::IOException(); // TODO: illegal relations + auto aResult = comphelper::sequenceToContainer<std::vector<beans::StringPair>>(rEntry); + auto nIDInd = std::distance(rEntry.begin(), pPair); + std::rotate(aResult.begin(), std::next(aResult.begin(), nIDInd), std::next(aResult.begin(), nIDInd + 1)); - nResultInd++; - } + return comphelper::containerToSequence(aResult); + }); - aResultSeq.realloc( nResultInd ); - m_pImpl->m_aNewRelInfo = aResultSeq; + m_pImpl->m_aNewRelInfo = comphelper::containerToSequence(aResultVec); m_pImpl->m_xNewRelInfoStream.clear(); m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED; } @@ -2843,22 +2795,22 @@ void SAL_CALL OWriteStream::setPropertyValue( const OUString& aPropertyName, con bCompressedValueFromType = false; } - for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ ) + for ( auto& rProp : m_pImpl->m_aProps ) { - if ( aPropertyName == m_pImpl->m_aProps[nInd].Name ) - m_pImpl->m_aProps[nInd].Value = aValue; - else if ( !m_pImpl->m_bCompressedSetExplicit && aCompressedString == m_pImpl->m_aProps[nInd].Name ) - m_pImpl->m_aProps[nInd].Value <<= bCompressedValueFromType; + if ( aPropertyName == rProp.Name ) + rProp.Value = aValue; + else if ( !m_pImpl->m_bCompressedSetExplicit && aCompressedString == rProp.Name ) + rProp.Value <<= bCompressedValueFromType; } } else if ( aPropertyName == aCompressedString ) { // if the "Compressed" property is not set explicitly, the MediaType can change the default value m_pImpl->m_bCompressedSetExplicit = true; - for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ ) + for ( auto& rProp : m_pImpl->m_aProps ) { - if ( aPropertyName == m_pImpl->m_aProps[nInd].Name ) - m_pImpl->m_aProps[nInd].Value = aValue; + if ( aPropertyName == rProp.Name ) + rProp.Value = aValue; } } else if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE @@ -2886,10 +2838,10 @@ void SAL_CALL OWriteStream::setPropertyValue( const OUString& aPropertyName, con } else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName == aMediaTypeString ) { - for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ ) + for ( auto& rProp : m_pImpl->m_aProps ) { - if ( aPropertyName == m_pImpl->m_aProps[nInd].Name ) - m_pImpl->m_aProps[nInd].Value = aValue; + if ( aPropertyName == rProp.Name ) + rProp.Value = aValue; } } else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName == "RelationsInfoStream" ) @@ -2960,11 +2912,10 @@ uno::Any SAL_CALL OWriteStream::getPropertyValue( const OUString& aProp ) { m_pImpl->GetStreamProperties(); - for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ ) - { - if ( aPropertyName == m_pImpl->m_aProps[nInd].Name ) - return m_pImpl->m_aProps[nInd].Value; - } + auto pProp = std::find_if(std::cbegin(m_pImpl->m_aProps), std::cend(m_pImpl->m_aProps), + [&aPropertyName](const css::beans::PropertyValue& rProp){ return aPropertyName == rProp.Name; }); + if (pProp != std::cend(m_pImpl->m_aProps)) + return pProp->Value; } else if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE && aPropertyName == "UseCommonStoragePasswordEncryption" ) diff --git a/package/source/xstor/xfactory.cxx b/package/source/xstor/xfactory.cxx index 2b28fdc81cd6..dcb8dd23a880 100644 --- a/package/source/xstor/xfactory.cxx +++ b/package/source/xstor/xfactory.cxx @@ -182,22 +182,23 @@ uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstanceWithAr aPropsToSet[0].Value <<= aURL; } - for ( sal_Int32 nInd = 0, nNumArgs = 1; nInd < aDescr.getLength(); nInd++ ) + sal_Int32 nNumArgs = 1; + for ( const auto& rProp : std::as_const(aDescr) ) { - if ( aDescr[nInd].Name == "InteractionHandler" - || aDescr[nInd].Name == "Password" - || aDescr[nInd].Name == "RepairPackage" - || aDescr[nInd].Name == "StatusIndicator" ) + if ( rProp.Name == "InteractionHandler" + || rProp.Name == "Password" + || rProp.Name == "RepairPackage" + || rProp.Name == "StatusIndicator" ) { aPropsToSet.realloc( ++nNumArgs ); - aPropsToSet[nNumArgs-1].Name = aDescr[nInd].Name; - aPropsToSet[nNumArgs-1].Value = aDescr[nInd].Value; + aPropsToSet[nNumArgs-1].Name = rProp.Name; + aPropsToSet[nNumArgs-1].Value = rProp.Value; } - else if ( aDescr[nInd].Name == "StorageFormat" ) + else if ( rProp.Name == "StorageFormat" ) { OUString aFormatName; sal_Int32 nFormatID = 0; - if ( aDescr[nInd].Value >>= aFormatName ) + if ( rProp.Value >>= aFormatName ) { if ( aFormatName == PACKAGE_STORAGE_FORMAT_STRING ) nStorageType = embed::StorageFormats::PACKAGE; @@ -208,7 +209,7 @@ uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstanceWithAr else throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 1 ); } - else if ( aDescr[nInd].Value >>= nFormatID ) + else if ( rProp.Value >>= nFormatID ) { if ( nFormatID != embed::StorageFormats::PACKAGE && nFormatID != embed::StorageFormats::ZIP @@ -220,12 +221,12 @@ uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstanceWithAr else throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 1 ); } - else if (aDescr[nInd].Name == "NoFileSync") + else if (rProp.Name == "NoFileSync") { // Forward NoFileSync to the storage. aPropsToSet.realloc(++nNumArgs); - aPropsToSet[nNumArgs - 1].Name = aDescr[nInd].Name; - aPropsToSet[nNumArgs - 1].Value = aDescr[nInd].Value; + aPropsToSet[nNumArgs - 1].Name = rProp.Name; + aPropsToSet[nNumArgs - 1].Value = rProp.Value; } else OSL_FAIL( "Unacceptable property, will be ignored!" ); diff --git a/package/source/xstor/xstorage.cxx b/package/source/xstor/xstorage.cxx index 16ae4a9331e0..f87927339152 100644 --- a/package/source/xstor/xstorage.cxx +++ b/package/source/xstor/xstorage.cxx @@ -141,8 +141,8 @@ void OStorage_Impl::completeStorageStreamCopy_Impl( aPropNames[1] = "MediaType"; } - for ( int ind = 0; ind < aPropNames.getLength(); ind++ ) - xDestProps->setPropertyValue( aPropNames[ind], xSourceProps->getPropertyValue( aPropNames[ind] ) ); + for ( const auto& rPropName : std::as_const(aPropNames) ) + xDestProps->setPropertyValue( rPropName, xSourceProps->getPropertyValue( rPropName ) ); } static uno::Reference< io::XInputStream > GetSeekableTempCopy( const uno::Reference< io::XInputStream >& xInStream, @@ -340,9 +340,9 @@ OStorage_Impl::~OStorage_Impl() m_xPackage.clear(); OUString aPropertyName = "URL"; - for ( sal_Int32 aInd = 0; aInd < m_xProperties.getLength(); ++aInd ) + for ( const auto& rProp : std::as_const(m_xProperties) ) { - if ( m_xProperties[aInd].Name == aPropertyName ) + if ( rProp.Name == aPropertyName ) { // the storage is URL based so all the streams are opened by factory and should be closed try @@ -429,19 +429,18 @@ void OStorage_Impl::OpenOwnPackage() uno::makeAny( false ) ); sal_Int32 nArgNum = 2; - for ( sal_Int32 aInd = 0; aInd < m_xProperties.getLength(); aInd++ ) + for ( const auto& rProp : std::as_const(m_xProperties) ) { - if ( m_xProperties[aInd].Name == "RepairPackage" - || m_xProperties[aInd].Name == "ProgressHandler" - || m_xProperties[aInd].Name == "NoFileSync" ) + if ( rProp.Name == "RepairPackage" + || rProp.Name == "ProgressHandler" + || rProp.Name == "NoFileSync" ) { // Forward these to the package. - beans::NamedValue aNamedValue( m_xProperties[aInd].Name, - m_xProperties[aInd].Value ); + beans::NamedValue aNamedValue( rProp.Name, rProp.Value ); aArguments.realloc( ++nArgNum ); aArguments[nArgNum-1] <<= aNamedValue; } - else if ( m_xProperties[aInd].Name == "Password" ) + else if ( rProp.Name == "Password" ) { // TODO: implement password setting for documents // the password entry must be removed after setting @@ -771,15 +770,15 @@ void OStorage_Impl::CopyStorageElement( SotElement_Impl* pElement, { // fill in the properties for the stream uno::Sequence< beans::PropertyValue > aStrProps(0); - uno::Sequence< beans::PropertyValue > aSrcPkgProps = pElement->m_xStream->GetStreamProperties(); + const uno::Sequence< beans::PropertyValue > aSrcPkgProps = pElement->m_xStream->GetStreamProperties(); sal_Int32 nNum = 0; - for ( int ind = 0; ind < aSrcPkgProps.getLength(); ind++ ) + for ( const auto& rSrcPkgProp : aSrcPkgProps ) { - if ( aSrcPkgProps[ind].Name == "MediaType" || aSrcPkgProps[ind].Name == "Compressed" ) + if ( rSrcPkgProp.Name == "MediaType" || rSrcPkgProp.Name == "Compressed" ) { aStrProps.realloc( ++nNum ); - aStrProps[nNum-1].Name = aSrcPkgProps[ind].Name; - aStrProps[nNum-1].Value = aSrcPkgProps[ind].Value; + aStrProps[nNum-1].Name = rSrcPkgProp.Name; + aStrProps[nNum-1].Value = rSrcPkgProp.Value; } } @@ -1684,7 +1683,7 @@ void OStorage_Impl::CommitRelInfo( const uno::Reference< container::XNameContain if (m_nRelInfoStatus == RELINFO_CHANGED) { - if (m_aRelInfo.getLength()) + if (m_aRelInfo.hasElements()) { CreateRelStorage(); @@ -3152,7 +3151,7 @@ uno::Reference< io::XStream > SAL_CALL OStorage::openEncryptedStream( if ( ( nOpenMode & embed::ElementModes::WRITE ) && m_pData->m_bReadOnlyWrap ) throw io::IOException( THROW_WHERE ); // TODO: access denied - if ( !aEncryptionData.getLength() ) + if ( !aEncryptionData.hasElements() ) throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 3 ); uno::Reference< io::XStream > xResult; @@ -3235,7 +3234,7 @@ uno::Reference< io::XStream > SAL_CALL OStorage::cloneEncryptedStream( throw lang::DisposedException( THROW_WHERE ); } - if ( !aEncryptionData.getLength() ) + if ( !aEncryptionData.hasElements() ) throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 2 ); try @@ -4102,7 +4101,7 @@ void SAL_CALL OStorage::setEncryptionData( const uno::Sequence< beans::NamedValu if ( m_pData->m_nStorageType != embed::StorageFormats::PACKAGE ) throw uno::RuntimeException( THROW_WHERE ); // the interface must be visible only for package storage - if ( !aEncryptionData.getLength() ) + if ( !aEncryptionData.hasElements() ) throw uno::RuntimeException( THROW_WHERE "Unexpected empty encryption data!" ); SAL_WARN_IF( !m_pData->m_bIsRoot, "package.xstor", "setEncryptionData() method is not available for nonroot storages!" ); @@ -4167,7 +4166,7 @@ void SAL_CALL OStorage::setEncryptionAlgorithms( const uno::Sequence< beans::Nam if ( m_pData->m_nStorageType != embed::StorageFormats::PACKAGE ) throw uno::RuntimeException( THROW_WHERE ); // the interface must be visible only for package storage - if ( !aAlgorithms.getLength() ) + if ( !aAlgorithms.hasElements() ) throw uno::RuntimeException( THROW_WHERE "Unexpected empty encryption algorithms list!" ); SAL_WARN_IF( !m_pData->m_bIsRoot, "package.xstor", "setEncryptionAlgorithms() method is not available for nonroot storages!" ); @@ -4227,7 +4226,7 @@ void SAL_CALL OStorage::setGpgProperties( const uno::Sequence< uno::Sequence< be if ( m_pData->m_nStorageType != embed::StorageFormats::PACKAGE ) throw uno::RuntimeException( THROW_WHERE ); // the interface must be visible only for package storage - if ( !aProps.getLength() ) + if ( !aProps.hasElements() ) throw uno::RuntimeException( THROW_WHERE "Unexpected empty encryption algorithms list!" ); SAL_WARN_IF( !m_pData->m_bIsRoot, "package.xstor", "setGpgProperties() method is not available for nonroot storages!" ); @@ -4501,11 +4500,10 @@ uno::Any SAL_CALL OStorage::getPropertyValue( const OUString& aPropertyName ) if ( aPropertyName == "URL" || aPropertyName == "RepairPackage" ) { - for ( sal_Int32 aInd = 0; aInd < m_pImpl->m_xProperties.getLength(); aInd++ ) - { - if ( m_pImpl->m_xProperties[aInd].Name == aPropertyName ) - return m_pImpl->m_xProperties[aInd].Value; - } + auto pProp = std::find_if(std::cbegin(m_pImpl->m_xProperties), std::cend(m_pImpl->m_xProperties), + [&aPropertyName](const css::beans::PropertyValue& rProp) { return rProp.Name == aPropertyName; }); + if (pProp != std::cend(m_pImpl->m_xProperties)) + return pProp->Value; if ( aPropertyName == "URL" ) return uno::makeAny( OUString() ); @@ -4633,6 +4631,16 @@ sal_Bool SAL_CALL OStorage::hasByID( const OUString& sID ) return false; } +namespace +{ + +const beans::StringPair* lcl_findPairByName(const uno::Sequence<beans::StringPair>& rSeq, const OUString& rName) +{ + return std::find_if(rSeq.begin(), rSeq.end(), [&rName](const beans::StringPair& rPair) { return rPair.First == rName; }); +} + +} + OUString SAL_CALL OStorage::getTargetByID( const OUString& sID ) { ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() ); @@ -4646,10 +4654,10 @@ OUString SAL_CALL OStorage::getTargetByID( const OUString& sID ) if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML ) throw uno::RuntimeException( THROW_WHERE ); - uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID ); - for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ ) - if ( aSeq[nInd].First == "Target" ) - return aSeq[nInd].Second; + const uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID ); + auto pRel = lcl_findPairByName(aSeq, "Target"); + if (pRel != aSeq.end()) + return pRel->Second; return OUString(); } @@ -4667,10 +4675,10 @@ OUString SAL_CALL OStorage::getTypeByID( const OUString& sID ) if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML ) throw uno::RuntimeException( THROW_WHERE ); - uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID ); - for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ ) - if ( aSeq[nInd].First == "Type" ) - return aSeq[nInd].Second; + const uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID ); + auto pRel = lcl_findPairByName(aSeq, "Type"); + if (pRel != aSeq.end()) + return pRel->Second; return OUString(); } @@ -4689,15 +4697,14 @@ uno::Sequence< beans::StringPair > SAL_CALL OStorage::getRelationshipByID( cons throw uno::RuntimeException( THROW_WHERE ); // TODO/LATER: in future the unification of the ID could be checked - uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); - for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ ) - for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ ) - if ( aSeq[nInd1][nInd2].First == "Id" ) - { - if ( aSeq[nInd1][nInd2].Second == sID ) - return aSeq[nInd1]; - break; - } + const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); + const beans::StringPair aIDRel("Id", sID); + + auto pRel = std::find_if(aSeq.begin(), aSeq.end(), + [&aIDRel](const uno::Sequence<beans::StringPair>& rRel) { + return std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end(); }); + if (pRel != aSeq.end()) + return *pRel; throw container::NoSuchElementException( THROW_WHERE ); } @@ -4715,25 +4722,20 @@ uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OStorage::getRelati if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML ) throw uno::RuntimeException( THROW_WHERE ); - uno::Sequence< uno::Sequence< beans::StringPair > > aResult; - sal_Int32 nEntriesNum = 0; - // TODO/LATER: in future the unification of the ID could be checked - uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); - for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ ) - for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ ) - if ( aSeq[nInd1][nInd2].First == "Type" ) - { + const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); + std::vector< uno::Sequence< beans::StringPair > > aResult; + aResult.reserve(aSeq.getLength()); + + std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResult), + [&sType](const uno::Sequence<beans::StringPair>& rRel) { + auto pRel = lcl_findPairByName(rRel, "Type"); + return pRel != rRel.end() // the type is usually a URL, so the check should be case insensitive - if ( aSeq[nInd1][nInd2].Second.equalsIgnoreAsciiCase( sType ) ) - { - aResult.realloc( ++nEntriesNum ); - aResult[nEntriesNum-1] = aSeq[nInd1]; - } - break; - } + && pRel->Second.equalsIgnoreAsciiCase( sType ); + }); - return aResult; + return comphelper::containerToSequence(aResult); } uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OStorage::getAllRelationships() @@ -4786,21 +4788,18 @@ void SAL_CALL OStorage::insertRelationshipByID( const OUString& sID, const uno: if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML ) throw uno::RuntimeException( THROW_WHERE ); - OUString aIDTag( "Id" ); + const beans::StringPair aIDRel("Id", sID); sal_Int32 nIDInd = -1; // TODO/LATER: in future the unification of the ID could be checked uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); - for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ ) - for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ ) - if ( aSeq[nInd1][nInd2].First == aIDTag ) - { - if ( aSeq[nInd1][nInd2].Second == sID ) - nIDInd = nInd1; - - break; - } + for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ ) + { + const auto& rRel = aSeq[nInd]; + if (std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end()) + nIDInd = nInd; + } if ( nIDInd != -1 && !bReplace ) throw container::ElementExistException( THROW_WHERE ); @@ -4811,20 +4810,14 @@ void SAL_CALL OStorage::insertRelationshipByID( const OUString& sID, const uno: aSeq.realloc( nIDInd + 1 ); } - aSeq[nIDInd].realloc( aEntry.getLength() + 1 ); + std::vector<beans::StringPair> aResult; + aResult.reserve(aEntry.getLength() + 1); - aSeq[nIDInd][0].First = aIDTag; - aSeq[nIDInd][0].Second = sID; - sal_Int32 nIndTarget = 1; - for ( sal_Int32 nIndOrig = 0; - nIndOrig < aEntry.getLength(); - nIndOrig++ ) - { - if ( aEntry[nIndOrig].First != aIDTag ) - aSeq[nIDInd][nIndTarget++] = aEntry[nIndOrig]; - } + aResult.push_back(aIDRel); + std::copy_if(aEntry.begin(), aEntry.end(), std::back_inserter(aResult), + [](const beans::StringPair& rPair) { return rPair.First != "Id"; }); - aSeq[nIDInd].realloc( nIndTarget ); + aSeq[nIDInd] = comphelper::containerToSequence(aResult); m_pImpl->m_aRelInfo = aSeq; m_pImpl->m_xNewRelInfoStream.clear(); @@ -4845,26 +4838,22 @@ void SAL_CALL OStorage::removeRelationshipByID( const OUString& sID ) throw uno::RuntimeException( THROW_WHERE ); uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); - for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ ) - for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ ) - if ( aSeq[nInd1][nInd2].First == "Id" ) - { - if ( aSeq[nInd1][nInd2].Second == sID ) - { - sal_Int32 nLength = aSeq.getLength(); - aSeq[nInd1] = aSeq[nLength-1]; - aSeq.realloc( nLength - 1 ); - - m_pImpl->m_aRelInfo = aSeq; - m_pImpl->m_xNewRelInfoStream.clear(); - m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED; + const beans::StringPair aIDRel("Id", sID); + auto pRel = std::find_if(std::cbegin(aSeq), std::cend(aSeq), + [&aIDRel](const uno::Sequence< beans::StringPair >& rRel) { + return std::find(rRel.begin(), rRel.end(), aIDRel) != rRel.end(); }); + if (pRel != std::cend(aSeq)) + { + auto nInd = static_cast<sal_Int32>(std::distance(std::cbegin(aSeq), pRel)); + comphelper::removeElementAt(aSeq, nInd); - // TODO/LATER: in future the unification of the ID could be checked - return; - } + m_pImpl->m_aRelInfo = aSeq; + m_pImpl->m_xNewRelInfoStream.clear(); + m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED; - break; - } + // TODO/LATER: in future the unification of the ID could be checked + return; + } throw container::NoSuchElementException( THROW_WHERE ); } @@ -4883,67 +4872,41 @@ void SAL_CALL OStorage::insertRelationships( const uno::Sequence< uno::Sequence throw uno::RuntimeException( THROW_WHERE ); OUString aIDTag( "Id" ); - uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); - uno::Sequence< uno::Sequence< beans::StringPair > > aResultSeq( aSeq.getLength() + aEntries.getLength() ); - sal_Int32 nResultInd = 0; - - for ( sal_Int32 nIndTarget1 = 0; nIndTarget1 < aSeq.getLength(); nIndTarget1++ ) - for ( sal_Int32 nIndTarget2 = 0; nIndTarget2 < aSeq[nIndTarget1].getLength(); nIndTarget2++ ) - if ( aSeq[nIndTarget1][nIndTarget2].First == aIDTag ) - { - sal_Int32 nIndSourceSame = -1; - - for ( sal_Int32 nIndSource1 = 0; nIndSource1 < aEntries.getLength(); nIndSource1++ ) - for ( sal_Int32 nIndSource2 = 0; nIndSource2 < aEntries[nIndSource1].getLength(); nIndSource2++ ) - { - if ( aEntries[nIndSource1][nIndSource2].First == aIDTag ) - { - if ( aEntries[nIndSource1][nIndSource2].Second == aSeq[nIndTarget1][nIndTarget2].Second ) - { - if ( !bReplace ) - throw container::ElementExistException( THROW_WHERE ); - - nIndSourceSame = nIndSource1; - } - - break; - } - } - - if ( nIndSourceSame == -1 ) - { - // no such element in the provided sequence - aResultSeq[nResultInd++] = aSeq[nIndTarget1]; - } - - break; - } - - for ( sal_Int32 nIndSource1 = 0; nIndSource1 < aEntries.getLength(); nIndSource1++ ) - { - aResultSeq[nResultInd].realloc( aEntries[nIndSource1].getLength() ); - bool bHasID = false; - sal_Int32 nResInd2 = 1; - - for ( sal_Int32 nIndSource2 = 0; nIndSource2 < aEntries[nIndSource1].getLength(); nIndSource2++ ) - if ( aEntries[nIndSource1][nIndSource2].First == aIDTag ) - { - aResultSeq[nResultInd][0] = aEntries[nIndSource1][nIndSource2]; - bHasID = true; - } - else if ( nResInd2 < aResultSeq[nResultInd].getLength() ) - aResultSeq[nResultInd][nResInd2++] = aEntries[nIndSource1][nIndSource2]; - else + const uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships(); + std::vector< uno::Sequence<beans::StringPair> > aResultVec; + aResultVec.reserve(aSeq.getLength() + aEntries.getLength()); + + std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResultVec), + [&aIDTag, &aEntries, bReplace](const uno::Sequence<beans::StringPair>& rTargetRel) { + auto pTargetPair = lcl_findPairByName(rTargetRel, aIDTag); + if (pTargetPair == rTargetRel.end()) + return false; + + bool bIsSourceSame = std::any_of(aEntries.begin(), aEntries.end(), + [&pTargetPair](const uno::Sequence<beans::StringPair>& rSourceEntry) { + return std::find(rSourceEntry.begin(), rSourceEntry.end(), *pTargetPair) != rSourceEntry.end(); }); + + if ( bIsSourceSame && !bReplace ) + throw container::ElementExistException( THROW_WHERE ); + + // if no such element in the provided sequence + return !bIsSourceSame; + }); + + std::transform(aEntries.begin(), aEntries.end(), std::back_inserter(aResultVec), + [&aIDTag](const uno::Sequence<beans::StringPair>& rEntry) -> uno::Sequence<beans::StringPair> { + auto pPair = lcl_findPairByName(rEntry, aIDTag); + if (pPair == rEntry.end()) throw io::IOException( THROW_WHERE ); // TODO: illegal relation ( no ID ) - if ( !bHasID ) - throw io::IOException( THROW_WHERE ); // TODO: illegal relations + auto aResult = comphelper::sequenceToContainer<std::vector<beans::StringPair>>(rEntry); + auto nIDInd = std::distance(rEntry.begin(), pPair); + std::rotate(aResult.begin(), std::next(aResult.begin(), nIDInd), std::next(aResult.begin(), nIDInd + 1)); - nResultInd++; - } + return comphelper::containerToSequence(aResult); + }); - aResultSeq.realloc( nResultInd ); - m_pImpl->m_aRelInfo = aResultSeq; + m_pImpl->m_aRelInfo = comphelper::containerToSequence(aResultVec); m_pImpl->m_xNewRelInfoStream.clear(); m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED; } |