From 38001018fa06f721cf87edae923b54ce9a5ab5a7 Mon Sep 17 00:00:00 2001 From: Arkadiy Illarionov Date: Sat, 10 Aug 2019 18:34:33 +0300 Subject: 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 Reviewed-by: Arkadiy Illarionov --- package/source/manifest/ManifestExport.cxx | 105 ++++---- package/source/xstor/ocompinstream.cxx | 109 ++++---- package/source/xstor/owriteablestream.cxx | 345 +++++++++++-------------- package/source/xstor/xfactory.cxx | 27 +- package/source/xstor/xstorage.cxx | 277 +++++++++----------- package/source/zippackage/ZipPackage.cxx | 114 ++++---- package/source/zippackage/ZipPackageStream.cxx | 6 +- package/source/zippackage/zipfileaccess.cxx | 4 +- 8 files changed, 441 insertions(+), 546 deletions(-) diff --git a/package/source/manifest/ManifestExport.cxx b/package/source/manifest/ManifestExport.cxx index f8c2d0399d4f..4b4d57ce9294 100644 --- a/package/source/manifest/ManifestExport.cxx +++ b/package/source/manifest/ManifestExport.cxx @@ -111,33 +111,30 @@ ManifestExport::ManifestExport( uno::Reference< xml::sax::XDocumentHandler > con const OUString sPGP_Name ( PGP_NAME ); ::comphelper::AttributeList * pRootAttrList = new ::comphelper::AttributeList; - const uno::Sequence < beans::PropertyValue > *pSequence = rManList.getConstArray(); - const sal_uInt32 nManLength = rManList.getLength(); // find the mediatype of the document if any OUString aDocMediaType; OUString aDocVersion; - sal_Int32 nRootFolderPropIndex=-1; - for (sal_uInt32 nInd = 0; nInd < nManLength ; nInd++ ) + const uno::Sequence* pRootFolderPropSeq = nullptr; + for (const uno::Sequence < beans::PropertyValue >& rSequence : rManList) { OUString aMediaType; OUString aPath; OUString aVersion; - const beans::PropertyValue *pValue = pSequence[nInd].getConstArray(); - for (sal_uInt32 j = 0, nNum = pSequence[nInd].getLength(); j < nNum; j++, pValue++) + for (const beans::PropertyValue& rValue : rSequence) { - if (pValue->Name == sMediaTypeProperty ) + if (rValue.Name == sMediaTypeProperty ) { - pValue->Value >>= aMediaType; + rValue.Value >>= aMediaType; } - else if (pValue->Name == sFullPathProperty ) + else if (rValue.Name == sFullPathProperty ) { - pValue->Value >>= aPath; + rValue.Value >>= aPath; } - else if (pValue->Name == sVersionProperty ) + else if (rValue.Name == sVersionProperty ) { - pValue->Value >>= aVersion; + rValue.Value >>= aVersion; } if ( !aPath.isEmpty() && !aMediaType.isEmpty() && !aVersion.isEmpty() ) @@ -148,7 +145,7 @@ ManifestExport::ManifestExport( uno::Reference< xml::sax::XDocumentHandler > con { aDocMediaType = aMediaType; aDocVersion = aVersion; - nRootFolderPropIndex = nInd; + pRootFolderPropSeq = &rSequence; break; } } @@ -217,15 +214,13 @@ ManifestExport::ManifestExport( uno::Reference< xml::sax::XDocumentHandler > con xHandler->startElement( sManifestElement, xRootAttrList ); const uno::Any *pKeyInfoProperty = nullptr; - if ( nRootFolderPropIndex >= 0 ) + if ( pRootFolderPropSeq ) { // do we have package-wide encryption info? - const beans::PropertyValue *pValue = - pSequence[nRootFolderPropIndex].getConstArray(); - for (sal_uInt32 j = 0, nNum = pSequence[nRootFolderPropIndex].getLength(); j < nNum; j++, pValue++) + for (const beans::PropertyValue& rValue : *pRootFolderPropSeq) { - if (pValue->Name == sKeyInfo ) - pKeyInfoProperty = &pValue->Value; + if (rValue.Name == sKeyInfo ) + pKeyInfoProperty = &rValue.Value; } if ( pKeyInfoProperty ) @@ -242,22 +237,19 @@ ManifestExport::ManifestExport( uno::Reference< xml::sax::XDocumentHandler > con uno::Sequence< uno::Sequence < beans::NamedValue > > aKeyInfoSequence; *pKeyInfoProperty >>= aKeyInfoSequence; - const uno::Sequence < beans::NamedValue > *pKeyInfoSequence = aKeyInfoSequence.getConstArray(); - const sal_uInt32 nKeyInfoLength = aKeyInfoSequence.getLength(); - for (sal_uInt32 nInd = 0; nInd < nKeyInfoLength ; nInd++ ) + for (const uno::Sequence& rKeyInfoSequence : std::as_const(aKeyInfoSequence)) { uno::Sequence < sal_Int8 > aPgpKeyID; uno::Sequence < sal_Int8 > aPgpKeyPacket; uno::Sequence < sal_Int8 > aCipherValue; - const beans::NamedValue *pNValue = pKeyInfoSequence[nInd].getConstArray(); - for (sal_uInt32 j = 0, nNum = pKeyInfoSequence[nInd].getLength(); j < nNum; j++, pNValue++) + for (const beans::NamedValue& rNValue : rKeyInfoSequence) { - if (pNValue->Name == sPgpKeyIDProperty ) - pNValue->Value >>= aPgpKeyID; - else if (pNValue->Name == sPgpKeyPacketProperty ) - pNValue->Value >>= aPgpKeyPacket; - else if (pNValue->Name == sCipherValueProperty ) - pNValue->Value >>= aCipherValue; + if (rNValue.Name == sPgpKeyIDProperty ) + rNValue.Value >>= aPgpKeyID; + else if (rNValue.Name == sPgpKeyPacketProperty ) + rNValue.Value >>= aPgpKeyPacket; + else if (rNValue.Name == sCipherValueProperty ) + rNValue.Value >>= aCipherValue; } if (aPgpKeyID.hasElements() && aCipherValue.hasElements() ) @@ -326,55 +318,54 @@ ManifestExport::ManifestExport( uno::Reference< xml::sax::XDocumentHandler > con } // now write individual file entries - for (sal_uInt32 i = 0 ; i < nManLength ; i++) + for (const uno::Sequence& rSequence : rManList) { ::comphelper::AttributeList *pAttrList = new ::comphelper::AttributeList; - const beans::PropertyValue *pValue = pSequence[i].getConstArray(); OUString aString; const uno::Any *pVector = nullptr, *pSalt = nullptr, *pIterationCount = nullptr, *pDigest = nullptr, *pDigestAlg = nullptr, *pEncryptAlg = nullptr, *pStartKeyAlg = nullptr, *pDerivedKeySize = nullptr; - for (sal_uInt32 j = 0, nNum = pSequence[i].getLength(); j < nNum; j++, pValue++) + for (const beans::PropertyValue& rValue : rSequence) { - if (pValue->Name == sMediaTypeProperty ) + if (rValue.Name == sMediaTypeProperty ) { - pValue->Value >>= aString; + rValue.Value >>= aString; pAttrList->AddAttribute ( sMediaTypeAttribute, sCdataAttribute, aString ); } - else if (pValue->Name == sVersionProperty ) + else if (rValue.Name == sVersionProperty ) { - pValue->Value >>= aString; + rValue.Value >>= aString; // the version is stored only if it is not empty if ( bAcceptNonemptyVersion && !aString.isEmpty() ) pAttrList->AddAttribute ( sVersionAttribute, sCdataAttribute, aString ); } - else if (pValue->Name == sFullPathProperty ) + else if (rValue.Name == sFullPathProperty ) { - pValue->Value >>= aString; + rValue.Value >>= aString; pAttrList->AddAttribute ( sFullPathAttribute, sCdataAttribute, aString ); } - else if (pValue->Name == sSizeProperty ) + else if (rValue.Name == sSizeProperty ) { sal_Int64 nSize = 0; - pValue->Value >>= nSize; + rValue.Value >>= nSize; OUStringBuffer aBuffer; aBuffer.append ( nSize ); pAttrList->AddAttribute ( sSizeAttribute, sCdataAttribute, aBuffer.makeStringAndClear() ); } - else if (pValue->Name == sInitialisationVectorProperty ) - pVector = &pValue->Value; - else if (pValue->Name == sSaltProperty ) - pSalt = &pValue->Value; - else if (pValue->Name == sIterationCountProperty ) - pIterationCount = &pValue->Value; - else if (pValue->Name == sDigestProperty ) - pDigest = &pValue->Value; - else if (pValue->Name == sDigestAlgProperty ) - pDigestAlg = &pValue->Value; - else if (pValue->Name == sEncryptionAlgProperty ) - pEncryptAlg = &pValue->Value; - else if (pValue->Name == sStartKeyAlgProperty ) - pStartKeyAlg = &pValue->Value; - else if (pValue->Name == sDerivedKeySizeProperty ) - pDerivedKeySize = &pValue->Value; + else if (rValue.Name == sInitialisationVectorProperty ) + pVector = &rValue.Value; + else if (rValue.Name == sSaltProperty ) + pSalt = &rValue.Value; + else if (rValue.Name == sIterationCountProperty ) + pIterationCount = &rValue.Value; + else if (rValue.Name == sDigestProperty ) + pDigest = &rValue.Value; + else if (rValue.Name == sDigestAlgProperty ) + pDigestAlg = &rValue.Value; + else if (rValue.Name == sEncryptionAlgProperty ) + pEncryptAlg = &rValue.Value; + else if (rValue.Name == sStartKeyAlgProperty ) + pStartKeyAlg = &rValue.Value; + else if (rValue.Name == sDerivedKeySizeProperty ) + pDerivedKeySize = &rValue.Value; } xHandler->ignorableWhitespace ( sWhiteSpace ); 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 #include #include +#include #include #include #include @@ -285,6 +286,17 @@ sal_Bool SAL_CALL OInputCompStream::hasByID( const OUString& sID ) return false; } +namespace +{ + +const beans::StringPair* lcl_findPairByName(const uno::Sequence& 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& 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 > aResult; + aResult.reserve(aSeq.getLength()); + + std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResult), + [&aTypeRel](const uno::Sequence& 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 #include #include +#include #include #include @@ -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& 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& 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 > aResult; + aResult.reserve(aSeq.getLength()); - return aResult; + std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResult), + [&aTypeRel](const uno::Sequence& 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 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(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 > aResultVec; + aResultVec.reserve(aSeq.getLength() + aEntries.getLength()); + + std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResultVec), + [&aIDTag, &aEntries, bReplace](const uno::Sequence& 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& 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& rEntry) -> uno::Sequence { + 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>(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& 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& 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& 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 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(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 > aResultVec; + aResultVec.reserve(aSeq.getLength() + aEntries.getLength()); + + std::copy_if(aSeq.begin(), aSeq.end(), std::back_inserter(aResultVec), + [&aIDTag, &aEntries, bReplace](const uno::Sequence& 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& 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& rEntry) -> uno::Sequence { + 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>(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; } diff --git a/package/source/zippackage/ZipPackage.cxx b/package/source/zippackage/ZipPackage.cxx index 574b7a434f5c..4ade3f66119c 100644 --- a/package/source/zippackage/ZipPackage.cxx +++ b/package/source/zippackage/ZipPackage.cxx @@ -200,46 +200,43 @@ void ZipPackage::parseManifest() const OUString sPropStartKeyAlgorithm ("StartKeyAlgorithm"); const OUString sKeyInfo ("KeyInfo"); - uno::Sequence < uno::Sequence < PropertyValue > > aManifestSequence = xReader->readManifestSequence ( xSink->getInputStream() ); - sal_Int32 nLength = aManifestSequence.getLength(); - const uno::Sequence < PropertyValue > *pSequence = aManifestSequence.getConstArray(); + const uno::Sequence < uno::Sequence < PropertyValue > > aManifestSequence = xReader->readManifestSequence ( xSink->getInputStream() ); ZipPackageStream *pStream = nullptr; ZipPackageFolder *pFolder = nullptr; const Any *pKeyInfo = nullptr; - for ( sal_Int32 i = 0; i < nLength ; i++, pSequence++ ) + for ( const uno::Sequence& rSequence : aManifestSequence ) { OUString sPath, sMediaType, sVersion; - const PropertyValue *pValue = pSequence->getConstArray(); const Any *pSalt = nullptr, *pVector = nullptr, *pCount = nullptr, *pSize = nullptr, *pDigest = nullptr, *pDigestAlg = nullptr, *pEncryptionAlg = nullptr, *pStartKeyAlg = nullptr, *pDerivedKeySize = nullptr; - for ( sal_Int32 j = 0, nNum = pSequence->getLength(); j < nNum; j++ ) + for ( const PropertyValue& rValue : rSequence ) { - if ( pValue[j].Name == sPropFullPath ) - pValue[j].Value >>= sPath; - else if ( pValue[j].Name == sPropVersion ) - pValue[j].Value >>= sVersion; - else if ( pValue[j].Name == sPropMediaType ) - pValue[j].Value >>= sMediaType; - else if ( pValue[j].Name == sPropSalt ) - pSalt = &( pValue[j].Value ); - else if ( pValue[j].Name == sPropInitialisationVector ) - pVector = &( pValue[j].Value ); - else if ( pValue[j].Name == sPropIterationCount ) - pCount = &( pValue[j].Value ); - else if ( pValue[j].Name == sPropSize ) - pSize = &( pValue[j].Value ); - else if ( pValue[j].Name == sPropDigest ) - pDigest = &( pValue[j].Value ); - else if ( pValue[j].Name == sPropDigestAlgorithm ) - pDigestAlg = &( pValue[j].Value ); - else if ( pValue[j].Name == sPropEncryptionAlgorithm ) - pEncryptionAlg = &( pValue[j].Value ); - else if ( pValue[j].Name == sPropStartKeyAlgorithm ) - pStartKeyAlg = &( pValue[j].Value ); - else if ( pValue[j].Name == sPropDerivedKeySize ) - pDerivedKeySize = &( pValue[j].Value ); - else if ( pValue[j].Name == sKeyInfo ) - pKeyInfo = &( pValue[j].Value ); + if ( rValue.Name == sPropFullPath ) + rValue.Value >>= sPath; + else if ( rValue.Name == sPropVersion ) + rValue.Value >>= sVersion; + else if ( rValue.Name == sPropMediaType ) + rValue.Value >>= sMediaType; + else if ( rValue.Name == sPropSalt ) + pSalt = &( rValue.Value ); + else if ( rValue.Name == sPropInitialisationVector ) + pVector = &( rValue.Value ); + else if ( rValue.Name == sPropIterationCount ) + pCount = &( rValue.Value ); + else if ( rValue.Name == sPropSize ) + pSize = &( rValue.Value ); + else if ( rValue.Name == sPropDigest ) + pDigest = &( rValue.Value ); + else if ( rValue.Name == sPropDigestAlgorithm ) + pDigestAlg = &( rValue.Value ); + else if ( rValue.Name == sPropEncryptionAlgorithm ) + pEncryptionAlg = &( rValue.Value ); + else if ( rValue.Name == sPropStartKeyAlgorithm ) + pStartKeyAlg = &( rValue.Value ); + else if ( rValue.Name == sPropDerivedKeySize ) + pDerivedKeySize = &( rValue.Value ); + else if ( rValue.Name == sKeyInfo ) + pKeyInfo = &( rValue.Value ); } if ( !sPath.isEmpty() && hasByHierarchicalName ( sPath ) ) @@ -469,26 +466,25 @@ void ZipPackage::parseContentType() uno::Reference< io::XInputStream > xInStream = xSink->getInputStream(); if ( xInStream.is() ) { - sal_Int32 nInd = 0; // here aContentTypeInfo[0] - Defaults, and aContentTypeInfo[1] - Overrides - uno::Sequence< uno::Sequence< beans::StringPair > > aContentTypeInfo = + const uno::Sequence< uno::Sequence< beans::StringPair > > aContentTypeInfo = ::comphelper::OFOPXMLHelper::ReadContentTypeSequence( xInStream, m_xContext ); if ( aContentTypeInfo.getLength() != 2 ) throw io::IOException(THROW_WHERE ); // set the implicit types first - for ( nInd = 0; nInd < aContentTypeInfo[0].getLength(); nInd++ ) - m_xRootFolder->setChildStreamsTypeByExtension( aContentTypeInfo[0][nInd] ); + for ( const auto& rPair : aContentTypeInfo[0] ) + m_xRootFolder->setChildStreamsTypeByExtension( rPair ); // now set the explicit types - for ( nInd = 0; nInd < aContentTypeInfo[1].getLength(); nInd++ ) + for ( const auto& rPair : aContentTypeInfo[1] ) { OUString aPath; - if ( aContentTypeInfo[1][nInd].First.toChar() == '/' ) - aPath = aContentTypeInfo[1][nInd].First.copy( 1 ); + if ( rPair.First.toChar() == '/' ) + aPath = rPair.First.copy( 1 ); else - aPath = aContentTypeInfo[1][nInd].First; + aPath = rPair.First; if ( !aPath.isEmpty() && hasByHierarchicalName( aPath ) ) { @@ -500,7 +496,7 @@ void ZipPackage::parseContentType() { // this is a package stream, in OFOPXML format only streams can have mediatype ZipPackageStream *pStream = reinterpret_cast < ZipPackageStream* > ( nTest ); - pStream->SetMediaType( aContentTypeInfo[1][nInd].Second ); + pStream->SetMediaType( rPair.Second ); } } } @@ -604,10 +600,10 @@ void SAL_CALL ZipPackage::initialize( const uno::Sequence< Any >& aArguments ) { bool bHaveZipFile = true; - for( int ind = 0; ind < aArguments.getLength(); ind++ ) + for( const auto& rArgument : aArguments ) { OUString aParamUrl; - if ( aArguments[ind] >>= aParamUrl ) + if ( rArgument >>= aParamUrl ) { m_eMode = e_IMode_URL; try @@ -670,17 +666,17 @@ void SAL_CALL ZipPackage::initialize( const uno::Sequence< Any >& aArguments ) bHaveZipFile = false; } } - else if ( aArguments[ind] >>= m_xStream ) + else if ( rArgument >>= m_xStream ) { // a writable stream can implement both XStream & XInputStream m_eMode = e_IMode_XStream; m_xContentStream = m_xStream->getInputStream(); } - else if ( aArguments[ind] >>= m_xContentStream ) + else if ( rArgument >>= m_xContentStream ) { m_eMode = e_IMode_XInputStream; } - else if ( aArguments[ind] >>= aNamedValue ) + else if ( rArgument >>= aNamedValue ) { if ( aNamedValue.Name == "RepairPackage" ) aNamedValue.Value >>= m_bForceRecovery; @@ -1642,9 +1638,9 @@ const uno::Sequence< sal_Int8 > ZipPackage::GetEncryptionKey() else throw uno::RuntimeException(THROW_WHERE "No expected key is provided!" ); - for ( sal_Int32 nInd = 0; nInd < m_aStorageEncryptionKeys.getLength(); nInd++ ) - if ( m_aStorageEncryptionKeys[nInd].Name == aNameToFind ) - m_aStorageEncryptionKeys[nInd].Value >>= aResult; + for ( const auto& rKey : std::as_const(m_aStorageEncryptionKeys) ) + if ( rKey.Name == aNameToFind ) + rKey.Value >>= aResult; // empty keys are not allowed here // so it is not important whether there is no key, or the key is empty, it is an error @@ -1759,11 +1755,11 @@ void SAL_CALL ZipPackage::setPropertyValue( const OUString& aPropertyName, const { bool bHasSHA256 = false; bool bHasSHA1 = false; - for ( sal_Int32 nInd = 0; nInd < aKeys.getLength(); nInd++ ) + for ( const auto& rKey : std::as_const(aKeys) ) { - if ( aKeys[nInd].Name == PACKAGE_ENCRYPTIONDATA_SHA256UTF8 ) + if ( rKey.Name == PACKAGE_ENCRYPTIONDATA_SHA256UTF8 ) bHasSHA256 = true; - if ( aKeys[nInd].Name == PACKAGE_ENCRYPTIONDATA_SHA1UTF8 ) + if ( rKey.Name == PACKAGE_ENCRYPTIONDATA_SHA1UTF8 ) bHasSHA1 = true; } @@ -1783,30 +1779,30 @@ void SAL_CALL ZipPackage::setPropertyValue( const OUString& aPropertyName, const throw IllegalArgumentException(THROW_WHERE "unexpected algorithms list is provided.", uno::Reference< uno::XInterface >(), 2 ); } - for ( sal_Int32 nInd = 0; nInd < aAlgorithms.getLength(); nInd++ ) + for ( const auto& rAlgorithm : std::as_const(aAlgorithms) ) { - if ( aAlgorithms[nInd].Name == "StartKeyGenerationAlgorithm" ) + if ( rAlgorithm.Name == "StartKeyGenerationAlgorithm" ) { sal_Int32 nID = 0; - if ( !( aAlgorithms[nInd].Value >>= nID ) + if ( !( rAlgorithm.Value >>= nID ) || ( nID != xml::crypto::DigestID::SHA256 && nID != xml::crypto::DigestID::SHA1 ) ) throw IllegalArgumentException(THROW_WHERE "Unexpected start key generation algorithm is provided!", uno::Reference< uno::XInterface >(), 2 ); m_nStartKeyGenerationID = nID; } - else if ( aAlgorithms[nInd].Name == "EncryptionAlgorithm" ) + else if ( rAlgorithm.Name == "EncryptionAlgorithm" ) { sal_Int32 nID = 0; - if ( !( aAlgorithms[nInd].Value >>= nID ) + if ( !( rAlgorithm.Value >>= nID ) || ( nID != xml::crypto::CipherID::AES_CBC_W3C_PADDING && nID != xml::crypto::CipherID::BLOWFISH_CFB_8 ) ) throw IllegalArgumentException(THROW_WHERE "Unexpected start key generation algorithm is provided!", uno::Reference< uno::XInterface >(), 2 ); m_nCommonEncryptionID = nID; } - else if ( aAlgorithms[nInd].Name == "ChecksumAlgorithm" ) + else if ( rAlgorithm.Name == "ChecksumAlgorithm" ) { sal_Int32 nID = 0; - if ( !( aAlgorithms[nInd].Value >>= nID ) + if ( !( rAlgorithm.Value >>= nID ) || ( nID != xml::crypto::DigestID::SHA1_1K && nID != xml::crypto::DigestID::SHA256_1K ) ) throw IllegalArgumentException(THROW_WHERE "Unexpected start key generation algorithm is provided!", uno::Reference< uno::XInterface >(), 2 ); diff --git a/package/source/zippackage/ZipPackageStream.cxx b/package/source/zippackage/ZipPackageStream.cxx index dc49729fc1fe..25c705036a6d 100644 --- a/package/source/zippackage/ZipPackageStream.cxx +++ b/package/source/zippackage/ZipPackageStream.cxx @@ -237,9 +237,9 @@ uno::Sequence ZipPackageStream::GetEncryptionKey(Bugs const bugs) else throw uno::RuntimeException(THROW_WHERE "No expected key is provided!" ); - for ( sal_Int32 nInd = 0; nInd < m_aStorageEncryptionKeys.getLength(); nInd++ ) - if ( m_aStorageEncryptionKeys[nInd].Name == aNameToFind ) - m_aStorageEncryptionKeys[nInd].Value >>= aResult; + for ( const auto& rKey : std::as_const(m_aStorageEncryptionKeys) ) + if ( rKey.Name == aNameToFind ) + rKey.Value >>= aResult; // empty keys are not allowed here // so it is not important whether there is no key, or the key is empty, it is an error diff --git a/package/source/zippackage/zipfileaccess.cxx b/package/source/zippackage/zipfileaccess.cxx index f5b0a4b434cf..fe584deaf7d9 100644 --- a/package/source/zippackage/zipfileaccess.cxx +++ b/package/source/zippackage/zipfileaccess.cxx @@ -214,10 +214,8 @@ void SAL_CALL OZipFileAccess::initialize( const uno::Sequence< uno::Any >& aArgu } else if (aArguments[0] >>= aArgs) { - for (sal_Int32 i = 0; i < aArgs.getLength(); ++i) + for (const beans::NamedValue& rArg : std::as_const(aArgs)) { - const beans::NamedValue& rArg = aArgs[i]; - if (rArg.Name == "URL") rArg.Value >>= aParamURL; } -- cgit v1.2.3