Kaydet (Commit) 328cdfd4 authored tarafından Noel Grandin's avatar Noel Grandin

loplugin:useuniqueptr in PDFContainer

Change-Id: I25c2a5a078450ed921c7e981f4c9fac242aa7178
Reviewed-on: https://gerrit.libreoffice.org/44863Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst 01cc5c2b
...@@ -486,7 +486,7 @@ uno::Reference< io::XStream > getAdditionalStream( const OUString& ...@@ -486,7 +486,7 @@ uno::Reference< io::XStream > getAdditionalStream( const OUString&
unsigned int nElements = pPDFFile->m_aSubElements.size(); unsigned int nElements = pPDFFile->m_aSubElements.size();
while( nElements-- > 0 ) while( nElements-- > 0 )
{ {
pdfparse::PDFTrailer* pTrailer = dynamic_cast<pdfparse::PDFTrailer*>(pPDFFile->m_aSubElements[nElements]); pdfparse::PDFTrailer* pTrailer = dynamic_cast<pdfparse::PDFTrailer*>(pPDFFile->m_aSubElements[nElements].get());
if( pTrailer && pTrailer->m_pDict ) if( pTrailer && pTrailer->m_pDict )
{ {
// search document checksum entry // search document checksum entry
...@@ -527,8 +527,8 @@ uno::Reference< io::XStream > getAdditionalStream( const OUString& ...@@ -527,8 +527,8 @@ uno::Reference< io::XStream > getAdditionalStream( const OUString&
continue; continue;
// extract addstream and mimetype // extract addstream and mimetype
pdfparse::PDFName* pMimeType = dynamic_cast<pdfparse::PDFName*>(pStreams->m_aSubElements[0]); pdfparse::PDFName* pMimeType = dynamic_cast<pdfparse::PDFName*>(pStreams->m_aSubElements[0].get());
pdfparse::PDFObjectRef* pStreamRef = dynamic_cast<pdfparse::PDFObjectRef*>(pStreams->m_aSubElements[1]); pdfparse::PDFObjectRef* pStreamRef = dynamic_cast<pdfparse::PDFObjectRef*>(pStreams->m_aSubElements[1].get());
SAL_WARN_IF( !pMimeType, "sdext.pdfimport", "error: no mimetype element" ); SAL_WARN_IF( !pMimeType, "sdext.pdfimport", "error: no mimetype element" );
SAL_WARN_IF( !pStreamRef, "sdext.pdfimport", "error: no stream ref element" ); SAL_WARN_IF( !pStreamRef, "sdext.pdfimport", "error: no stream ref element" );
......
...@@ -157,14 +157,14 @@ struct PDFObject; ...@@ -157,14 +157,14 @@ struct PDFObject;
struct PDFContainer : public PDFEntry struct PDFContainer : public PDFEntry
{ {
sal_Int32 m_nOffset; sal_Int32 m_nOffset;
std::vector<PDFEntry*> m_aSubElements; std::vector<std::unique_ptr<PDFEntry>> m_aSubElements;
// this is an abstract base class for identifying // this is an abstract base class for identifying
// entries that can contain sub elements besides comments // entries that can contain sub elements besides comments
PDFContainer() : PDFEntry(), m_nOffset( 0 ) {} PDFContainer() : PDFEntry(), m_nOffset( 0 ) {}
virtual ~PDFContainer() override; virtual ~PDFContainer() override;
bool emitSubElements( EmitContext& rWriteContext ) const; bool emitSubElements( EmitContext& rWriteContext ) const;
void cloneSubElements( std::vector<PDFEntry*>& rNewSubElements ) const; void cloneSubElements( std::vector<std::unique_ptr<PDFEntry>>& rNewSubElements ) const;
PDFObject* findObject( unsigned int nNumber, unsigned int nGeneration ) const; PDFObject* findObject( unsigned int nNumber, unsigned int nGeneration ) const;
PDFObject* findObject( PDFObjectRef const * pRef ) const PDFObject* findObject( PDFObjectRef const * pRef ) const
......
...@@ -442,9 +442,6 @@ PDFEntry* PDFObjectRef::clone() const ...@@ -442,9 +442,6 @@ PDFEntry* PDFObjectRef::clone() const
PDFContainer::~PDFContainer() PDFContainer::~PDFContainer()
{ {
int nEle = m_aSubElements.size();
for( int i = 0; i < nEle; i++ )
delete m_aSubElements[i];
} }
bool PDFContainer::emitSubElements( EmitContext& rWriteContext ) const bool PDFContainer::emitSubElements( EmitContext& rWriteContext ) const
...@@ -454,7 +451,7 @@ bool PDFContainer::emitSubElements( EmitContext& rWriteContext ) const ...@@ -454,7 +451,7 @@ bool PDFContainer::emitSubElements( EmitContext& rWriteContext ) const
{ {
if( rWriteContext.m_bDecrypt ) if( rWriteContext.m_bDecrypt )
{ {
const PDFName* pName = dynamic_cast<PDFName*>(m_aSubElements[i]); const PDFName* pName = dynamic_cast<PDFName*>(m_aSubElements[i].get());
if (pName && pName->m_aName == "Encrypt") if (pName && pName->m_aName == "Encrypt")
{ {
i++; i++;
...@@ -467,11 +464,11 @@ bool PDFContainer::emitSubElements( EmitContext& rWriteContext ) const ...@@ -467,11 +464,11 @@ bool PDFContainer::emitSubElements( EmitContext& rWriteContext ) const
return true; return true;
} }
void PDFContainer::cloneSubElements( std::vector<PDFEntry*>& rNewSubElements ) const void PDFContainer::cloneSubElements( std::vector<std::unique_ptr<PDFEntry>>& rNewSubElements ) const
{ {
int nEle = m_aSubElements.size(); int nEle = m_aSubElements.size();
for( int i = 0; i < nEle; i++ ) for( int i = 0; i < nEle; i++ )
rNewSubElements.push_back( m_aSubElements[i]->clone() ); rNewSubElements.emplace_back( m_aSubElements[i]->clone() );
} }
PDFObject* PDFContainer::findObject( unsigned int nNumber, unsigned int nGeneration ) const PDFObject* PDFContainer::findObject( unsigned int nNumber, unsigned int nGeneration ) const
...@@ -479,7 +476,7 @@ PDFObject* PDFContainer::findObject( unsigned int nNumber, unsigned int nGenerat ...@@ -479,7 +476,7 @@ PDFObject* PDFContainer::findObject( unsigned int nNumber, unsigned int nGenerat
unsigned int nEle = m_aSubElements.size(); unsigned int nEle = m_aSubElements.size();
for( unsigned int i = 0; i < nEle; i++ ) for( unsigned int i = 0; i < nEle; i++ )
{ {
PDFObject* pObject = dynamic_cast<PDFObject*>(m_aSubElements[i]); PDFObject* pObject = dynamic_cast<PDFObject*>(m_aSubElements[i].get());
if( pObject && if( pObject &&
pObject->m_nNumber == nNumber && pObject->m_nNumber == nNumber &&
pObject->m_nGeneration == nGeneration ) pObject->m_nGeneration == nGeneration )
...@@ -532,16 +529,15 @@ void PDFDict::insertValue( const OString& rName, PDFEntry* pValue ) ...@@ -532,16 +529,15 @@ void PDFDict::insertValue( const OString& rName, PDFEntry* pValue )
if( it == m_aMap.end() ) if( it == m_aMap.end() )
{ {
// new name/value, pair, append it // new name/value, pair, append it
m_aSubElements.push_back( new PDFName( rName ) ); m_aSubElements.emplace_back( new PDFName( rName ) );
m_aSubElements.push_back( pValue ); m_aSubElements.emplace_back( pValue );
} }
else else
{ {
unsigned int nSub = m_aSubElements.size(); unsigned int nSub = m_aSubElements.size();
for( unsigned int i = 0; i < nSub; i++ ) for( unsigned int i = 0; i < nSub; i++ )
if( m_aSubElements[i] == it->second ) if( m_aSubElements[i].get() == it->second )
m_aSubElements[i] = pValue; m_aSubElements[i].reset(pValue);
delete it->second;
} }
m_aMap[ rName ] = pValue; m_aMap[ rName ] = pValue;
} }
...@@ -551,17 +547,14 @@ void PDFDict::eraseValue( const OString& rName ) ...@@ -551,17 +547,14 @@ void PDFDict::eraseValue( const OString& rName )
unsigned int nEle = m_aSubElements.size(); unsigned int nEle = m_aSubElements.size();
for( unsigned int i = 0; i < nEle; i++ ) for( unsigned int i = 0; i < nEle; i++ )
{ {
PDFName* pName = dynamic_cast<PDFName*>(m_aSubElements[i]); PDFName* pName = dynamic_cast<PDFName*>(m_aSubElements[i].get());
if( pName && pName->m_aName == rName ) if( pName && pName->m_aName == rName )
{ {
for( unsigned int j = i+1; j < nEle; j++ ) for( unsigned int j = i+1; j < nEle; j++ )
{ {
if( dynamic_cast<PDFComment*>(m_aSubElements[j]) == nullptr ) if( dynamic_cast<PDFComment*>(m_aSubElements[j].get()) == nullptr )
{ {
// free name and value // remove and free subelements from vector
delete m_aSubElements[j];
delete m_aSubElements[i];
// remove subelements from vector
m_aSubElements.erase( m_aSubElements.begin()+j ); m_aSubElements.erase( m_aSubElements.begin()+j );
m_aSubElements.erase( m_aSubElements.begin()+i ); m_aSubElements.erase( m_aSubElements.begin()+i );
buildMap(); buildMap();
...@@ -581,15 +574,15 @@ PDFEntry* PDFDict::buildMap() ...@@ -581,15 +574,15 @@ PDFEntry* PDFDict::buildMap()
PDFName* pName = nullptr; PDFName* pName = nullptr;
for( unsigned int i = 0; i < nEle; i++ ) for( unsigned int i = 0; i < nEle; i++ )
{ {
if( dynamic_cast<PDFComment*>(m_aSubElements[i]) == nullptr ) if( dynamic_cast<PDFComment*>(m_aSubElements[i].get()) == nullptr )
{ {
if( pName ) if( pName )
{ {
m_aMap[ pName->m_aName ] = m_aSubElements[i]; m_aMap[ pName->m_aName ] = m_aSubElements[i].get();
pName = nullptr; pName = nullptr;
} }
else if( (pName = dynamic_cast<PDFName*>(m_aSubElements[i])) == nullptr ) else if( (pName = dynamic_cast<PDFName*>(m_aSubElements[i].get())) == nullptr )
return m_aSubElements[i]; return m_aSubElements[i].get();
} }
} }
return pName; return pName;
...@@ -635,7 +628,7 @@ unsigned int PDFStream::getDictLength( const PDFContainer* pContainer ) const ...@@ -635,7 +628,7 @@ unsigned int PDFStream::getDictLength( const PDFContainer* pContainer ) const
int nEle = pContainer->m_aSubElements.size(); int nEle = pContainer->m_aSubElements.size();
for( int i = 0; i < nEle && ! pNum; i++ ) for( int i = 0; i < nEle && ! pNum; i++ )
{ {
PDFObject* pObj = dynamic_cast<PDFObject*>(pContainer->m_aSubElements[i]); PDFObject* pObj = dynamic_cast<PDFObject*>(pContainer->m_aSubElements[i].get());
if( pObj && if( pObj &&
pObj->m_nNumber == pRef->m_nNumber && pObj->m_nNumber == pRef->m_nNumber &&
pObj->m_nGeneration == pRef->m_nGeneration ) pObj->m_nGeneration == pRef->m_nGeneration )
...@@ -682,7 +675,7 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const ...@@ -682,7 +675,7 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const
PDFArray* pArray = dynamic_cast<PDFArray*>(it->second); PDFArray* pArray = dynamic_cast<PDFArray*>(it->second);
if( pArray && ! pArray->m_aSubElements.empty() ) if( pArray && ! pArray->m_aSubElements.empty() )
{ {
pFilter = dynamic_cast<PDFName*>(pArray->m_aSubElements.front()); pFilter = dynamic_cast<PDFName*>(pArray->m_aSubElements.front().get());
} }
} }
...@@ -850,7 +843,7 @@ bool PDFObject::emit( EmitContext& rWriteContext ) const ...@@ -850,7 +843,7 @@ bool PDFObject::emit( EmitContext& rWriteContext ) const
PDFArray* pArray = dynamic_cast<PDFArray*>(it->second); PDFArray* pArray = dynamic_cast<PDFArray*>(it->second);
if( pArray && ! pArray->m_aSubElements.empty() ) if( pArray && ! pArray->m_aSubElements.empty() )
{ {
pFilter = dynamic_cast<PDFName*>(pArray->m_aSubElements.front()); pFilter = dynamic_cast<PDFName*>(pArray->m_aSubElements.front().get());
if (pFilter && pFilter->m_aName == "FlateDecode") if (pFilter && pFilter->m_aName == "FlateDecode")
{ {
delete pFilter; delete pFilter;
...@@ -866,7 +859,7 @@ bool PDFObject::emit( EmitContext& rWriteContext ) const ...@@ -866,7 +859,7 @@ bool PDFObject::emit( EmitContext& rWriteContext ) const
unsigned int nEle = pClone->m_aSubElements.size(); unsigned int nEle = pClone->m_aSubElements.size();
for( unsigned int i = 0; i < nEle && bRet; i++ ) for( unsigned int i = 0; i < nEle && bRet; i++ )
{ {
if( pClone->m_aSubElements[i] != pClone->m_pStream ) if( pClone->m_aSubElements[i].get() != pClone->m_pStream )
bRet = pClone->m_aSubElements[i]->emit( rWriteContext ); bRet = pClone->m_aSubElements[i]->emit( rWriteContext );
} }
delete pClone; delete pClone;
...@@ -903,11 +896,11 @@ PDFEntry* PDFObject::clone() const ...@@ -903,11 +896,11 @@ PDFEntry* PDFObject::clone() const
unsigned int nEle = m_aSubElements.size(); unsigned int nEle = m_aSubElements.size();
for( unsigned int i = 0; i < nEle; i++ ) for( unsigned int i = 0; i < nEle; i++ )
{ {
if( m_aSubElements[i] == m_pObject ) if( m_aSubElements[i].get() == m_pObject )
pNewOb->m_pObject = pNewOb->m_aSubElements[i]; pNewOb->m_pObject = pNewOb->m_aSubElements[i].get();
else if( m_aSubElements[i] == m_pStream && pNewOb->m_pObject ) else if( m_aSubElements[i].get() == m_pStream && pNewOb->m_pObject )
{ {
pNewOb->m_pStream = dynamic_cast<PDFStream*>(pNewOb->m_aSubElements[i]); pNewOb->m_pStream = dynamic_cast<PDFStream*>(pNewOb->m_aSubElements[i].get());
PDFDict* pNewDict = dynamic_cast<PDFDict*>(pNewOb->m_pObject); PDFDict* pNewDict = dynamic_cast<PDFDict*>(pNewOb->m_pObject);
if (pNewDict && pNewOb->m_pStream) if (pNewDict && pNewOb->m_pStream)
pNewOb->m_pStream->m_pDict = pNewDict; pNewOb->m_pStream->m_pDict = pNewDict;
...@@ -995,9 +988,9 @@ PDFEntry* PDFTrailer::clone() const ...@@ -995,9 +988,9 @@ PDFEntry* PDFTrailer::clone() const
unsigned int nEle = m_aSubElements.size(); unsigned int nEle = m_aSubElements.size();
for( unsigned int i = 0; i < nEle; i++ ) for( unsigned int i = 0; i < nEle; i++ )
{ {
if( m_aSubElements[i] == m_pDict ) if( m_aSubElements[i].get() == m_pDict )
{ {
pNewTr->m_pDict = dynamic_cast<PDFDict*>(pNewTr->m_aSubElements[i]); pNewTr->m_pDict = dynamic_cast<PDFDict*>(pNewTr->m_aSubElements[i].get());
break; break;
} }
} }
...@@ -1273,7 +1266,7 @@ PDFFileImplData* PDFFile::impl_getData() const ...@@ -1273,7 +1266,7 @@ PDFFileImplData* PDFFile::impl_getData() const
unsigned int nElements = m_aSubElements.size(); unsigned int nElements = m_aSubElements.size();
while( nElements-- > 0 ) while( nElements-- > 0 )
{ {
PDFTrailer* pTrailer = dynamic_cast<PDFTrailer*>(m_aSubElements[nElements]); PDFTrailer* pTrailer = dynamic_cast<PDFTrailer*>(m_aSubElements[nElements].get());
if( pTrailer && pTrailer->m_pDict ) if( pTrailer && pTrailer->m_pDict )
{ {
// search doc id // search doc id
...@@ -1283,7 +1276,7 @@ PDFFileImplData* PDFFile::impl_getData() const ...@@ -1283,7 +1276,7 @@ PDFFileImplData* PDFFile::impl_getData() const
PDFArray* pArr = dynamic_cast<PDFArray*>(doc_id->second); PDFArray* pArr = dynamic_cast<PDFArray*>(doc_id->second);
if( pArr && pArr->m_aSubElements.size() > 0 ) if( pArr && pArr->m_aSubElements.size() > 0 )
{ {
PDFString* pStr = dynamic_cast<PDFString*>(pArr->m_aSubElements[0]); PDFString* pStr = dynamic_cast<PDFString*>(pArr->m_aSubElements[0].get());
if( pStr ) if( pStr )
m_pData->m_aDocID = pStr->getFilteredString(); m_pData->m_aDocID = pStr->getFilteredString();
#if OSL_DEBUG_LEVEL > 0 #if OSL_DEBUG_LEVEL > 0
......
...@@ -309,7 +309,7 @@ public: ...@@ -309,7 +309,7 @@ public:
PDFContainer* pContainer = dynamic_cast<PDFContainer*>(m_aObjectStack.back()); PDFContainer* pContainer = dynamic_cast<PDFContainer*>(m_aObjectStack.back());
if( pContainer == nullptr ) if( pContainer == nullptr )
parseError( "comment without container", first ); parseError( "comment without container", first );
pContainer->m_aSubElements.push_back( pComment ); pContainer->m_aSubElements.emplace_back( pComment );
} }
void insertNewValue( PDFEntry* pNewValue, iteratorT pPos ) void insertNewValue( PDFEntry* pNewValue, iteratorT pPos )
...@@ -351,7 +351,7 @@ public: ...@@ -351,7 +351,7 @@ public:
} }
} }
if( pContainer ) if( pContainer )
pContainer->m_aSubElements.push_back( pNewValue ); pContainer->m_aSubElements.emplace_back( pNewValue );
else else
{ {
if( ! pMsg ) if( ! pMsg )
...@@ -410,7 +410,7 @@ public: ...@@ -410,7 +410,7 @@ public:
( dynamic_cast<PDFFile*>(pContainer) || ( dynamic_cast<PDFFile*>(pContainer) ||
dynamic_cast<PDFPart*>(pContainer) ) ) dynamic_cast<PDFPart*>(pContainer) ) )
{ {
pContainer->m_aSubElements.push_back( pObj ); pContainer->m_aSubElements.emplace_back( pObj );
m_aObjectStack.push_back( pObj ); m_aObjectStack.push_back( pObj );
} }
else else
...@@ -502,7 +502,7 @@ public: ...@@ -502,7 +502,7 @@ public:
PDFStream* pStream = new PDFStream( first - m_aGlobalBegin, last - m_aGlobalBegin, pDict ); PDFStream* pStream = new PDFStream( first - m_aGlobalBegin, last - m_aGlobalBegin, pDict );
pObj->m_pStream = pStream; pObj->m_pStream = pStream;
pObj->m_aSubElements.push_back( pStream ); pObj->m_aSubElements.emplace_back( pStream );
} }
} }
else else
...@@ -522,7 +522,7 @@ public: ...@@ -522,7 +522,7 @@ public:
( dynamic_cast<PDFFile*>(pContainer) || ( dynamic_cast<PDFFile*>(pContainer) ||
dynamic_cast<PDFPart*>(pContainer) ) ) dynamic_cast<PDFPart*>(pContainer) ) )
{ {
pContainer->m_aSubElements.push_back( pTrailer ); pContainer->m_aSubElements.emplace_back( pTrailer );
m_aObjectStack.push_back( pTrailer ); m_aObjectStack.push_back( pTrailer );
} }
else else
......
...@@ -250,8 +250,8 @@ int write_addStreamArray( const char* pOutFile, PDFArray* pStreams, PDFFile* pPD ...@@ -250,8 +250,8 @@ int write_addStreamArray( const char* pOutFile, PDFArray* pStreams, PDFFile* pPD
unsigned int nArrayElements = pStreams->m_aSubElements.size(); unsigned int nArrayElements = pStreams->m_aSubElements.size();
for( unsigned int i = 0; i < nArrayElements-1 && nRet == 0; i++ ) for( unsigned int i = 0; i < nArrayElements-1 && nRet == 0; i++ )
{ {
PDFName* pMimeType = dynamic_cast<PDFName*>(pStreams->m_aSubElements[i]); PDFName* pMimeType = dynamic_cast<PDFName*>(pStreams->m_aSubElements[i].get());
PDFObjectRef* pStreamRef = dynamic_cast<PDFObjectRef*>(pStreams->m_aSubElements[i+1]); PDFObjectRef* pStreamRef = dynamic_cast<PDFObjectRef*>(pStreams->m_aSubElements[i+1].get());
if( ! pMimeType ) if( ! pMimeType )
fprintf( stderr, "error: no mimetype element\n" ); fprintf( stderr, "error: no mimetype element\n" );
if( ! pStreamRef ) if( ! pStreamRef )
...@@ -292,7 +292,7 @@ int write_addStreams( const char* pInFile, const char* pOutFile, PDFFile* pPDFFi ...@@ -292,7 +292,7 @@ int write_addStreams( const char* pInFile, const char* pOutFile, PDFFile* pPDFFi
unsigned int nElements = pPDFFile->m_aSubElements.size(); unsigned int nElements = pPDFFile->m_aSubElements.size();
for( unsigned i = 0; i < nElements && nRet == 0; i++ ) for( unsigned i = 0; i < nElements && nRet == 0; i++ )
{ {
PDFTrailer* pTrailer = dynamic_cast<PDFTrailer*>(pPDFFile->m_aSubElements[i]); PDFTrailer* pTrailer = dynamic_cast<PDFTrailer*>(pPDFFile->m_aSubElements[i].get());
if( pTrailer && pTrailer->m_pDict ) if( pTrailer && pTrailer->m_pDict )
{ {
// search for AdditionalStreams entry // search for AdditionalStreams entry
...@@ -316,7 +316,7 @@ int write_fonts( const char* i_pInFile, const char* i_pOutFile, PDFFile* i_pPDFF ...@@ -316,7 +316,7 @@ int write_fonts( const char* i_pInFile, const char* i_pOutFile, PDFFile* i_pPDFF
for( unsigned i = 0; i < nElements && nRet == 0; i++ ) for( unsigned i = 0; i < nElements && nRet == 0; i++ )
{ {
// search FontDescriptors // search FontDescriptors
PDFObject* pObj = dynamic_cast<PDFObject*>(i_pPDFFile->m_aSubElements[i]); PDFObject* pObj = dynamic_cast<PDFObject*>(i_pPDFFile->m_aSubElements[i].get());
if( ! pObj ) if( ! pObj )
continue; continue;
PDFDict* pDict = dynamic_cast<PDFDict*>(pObj->m_pObject); PDFDict* pDict = dynamic_cast<PDFDict*>(pObj->m_pObject);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment