#include "stdafx.h" #include "List.h" namespace AVSDocFileFormat { List::List( short _numID ): numID(_numID), level(0) { } /*========================================================================================================*/ List::List( const list& _textItems, short _numID ): textItems(_textItems), numID(_numID), level(0) { this->SetLevel( this->level ); this->SetNumID( this->numID ); } /*========================================================================================================*/ List::List( const List& _list ): textItems(_list.textItems), numID(_list.numID), level(_list.level) { } /*========================================================================================================*/ void List::SetLevel( byte _level ) { for ( list::iterator iter = this->textItems.begin(); iter != this->textItems.end(); iter++ ) { if ( typeid(**iter) == typeid(Paragraph) ) { Paragraph* paragraph = dynamic_cast( iter->get() ); if ( paragraph != NULL ) { paragraph->RemoveProperty( (short)DocFileFormat::sprmPIlvl, (byte*)(&(this->level)) ); paragraph->AddProperty( (short)DocFileFormat::sprmPIlvl, (byte*)(&_level) ); } } else if ( typeid(**iter) == typeid(List) ) { List* subList = dynamic_cast( iter->get() ); if ( subList != NULL ) { subList->SetLevel( _level + 1 ); } } } this->level = _level; } /*========================================================================================================*/ void List::SetNumID( short _numID ) { for ( list::iterator iter = this->textItems.begin(); iter != this->textItems.end(); iter++ ) { if ( typeid(**iter) == typeid(Paragraph) ) { Paragraph* paragraph = dynamic_cast( iter->get() ); if ( paragraph != NULL ) { paragraph->RemoveProperty( (short)DocFileFormat::sprmPIlfo, (byte*)(&this->numID) ); paragraph->AddProperty( (short)DocFileFormat::sprmPIlfo, (byte*)(&_numID) ); } } else if ( typeid(**iter) == typeid(List) ) { List* subList = dynamic_cast( iter->get() ); if ( subList != NULL ) { subList->SetNumID( _numID ); } } } this->numID = _numID; } /*========================================================================================================*/ void List::AddParagraph( const Paragraph& _paragraph ) { TextItemPtr newTextItem( static_cast( _paragraph.Clone() ) ); this->textItems.push_back( newTextItem ); } /*========================================================================================================*/ void List::AddTextItem( const ITextItem& _textItem ) { TextItemPtr newTextItem( static_cast( _textItem.Clone() ) ); this->textItems.push_back( newTextItem ); this->SetLevel( this->level ); this->SetNumID( this->numID ); } /*========================================================================================================*/ wstring List::GetAllText() const { wstring allListText; for ( list::const_iterator iter = this->textItems.begin(); iter != this->textItems.end(); iter++ ) { allListText += (*iter)->GetAllText(); } return allListText; } /*========================================================================================================*/ List::operator wstring() const { wstring listText; for ( list::const_iterator iter = this->textItems.begin(); iter != this->textItems.end(); iter++ ) { listText += (wstring)(**iter); } return listText; } /*========================================================================================================*/ vector List::GetAllParagraphsCopy() const { vector allParagraphs; for ( list::const_iterator iter = this->textItems.begin(); iter != this->textItems.end(); iter++ ) { if ( typeid(**iter) == typeid(Paragraph) ) { allParagraphs.push_back( TextItemPtr( static_cast( (*iter)->Clone() ) ) ); } else { vector textItemParagraphs = (*iter)->GetAllParagraphsCopy(); for ( vector::const_iterator textItemIter = textItemParagraphs.begin(); textItemIter != textItemParagraphs.end(); textItemIter++ ) { allParagraphs.push_back( TextItemPtr( static_cast( (*textItemIter)->Clone() ) ) ); } } } return allParagraphs; } /*========================================================================================================*/ vector List::GetAllParagraphs() { vector allParagraphs; for ( list::iterator iter = this->textItems.begin(); iter != this->textItems.end(); iter++ ) { if ( typeid(**iter) == typeid(Paragraph) ) { allParagraphs.push_back( iter->get() ); } else { vector textItemParagraphs = (*iter)->GetAllParagraphs(); for ( vector::iterator textItemIter = textItemParagraphs.begin(); textItemIter != textItemParagraphs.end(); textItemIter++ ) { allParagraphs.push_back( *textItemIter ); } } } return allParagraphs; } /*========================================================================================================*/ vector List::GetAllRunsCopy( vector* allRunsOffsets ) const { vector allRuns; if ( allRunsOffsets != NULL ) { unsigned int runOffset = 0; for ( list::const_iterator iter = this->textItems.begin(); iter != this->textItems.end(); iter++ ) { vector allTextItemRunsOffsets; vector allTextItemRuns = (*iter)->GetAllRunsCopy( &allTextItemRunsOffsets ); for ( unsigned int i = 0; i < allTextItemRuns.size(); i++ ) { allRuns.push_back( IParagraphItemPtr( static_cast(allTextItemRuns[i]->Clone()) ) ); allRunsOffsets->push_back( runOffset + allTextItemRunsOffsets[i] ); } runOffset += ( sizeof(WCHAR) * (*iter)->GetAllText().size() ); } } return allRuns; } /*========================================================================================================*/ vector List::GetAllParagraphItemsCopy( vector* allParagraphItemsOffsets ) const { vector allParagraphItems; if ( allParagraphItemsOffsets != NULL ) { unsigned int paragraphItemOffset = 0; for ( list::const_iterator iter = this->textItems.begin(); iter != this->textItems.end(); iter++ ) { vector allTextItemParagraphItemsOffsets; vector allTextItemParagraphItems = (*iter)->GetAllParagraphItemsCopy( &allTextItemParagraphItemsOffsets ); for ( unsigned int i = 0; i < allTextItemParagraphItems.size(); i++ ) { allParagraphItems.push_back( IParagraphItemPtr( static_cast(allTextItemParagraphItems[i]->Clone()) ) ); allParagraphItemsOffsets->push_back( paragraphItemOffset + allTextItemParagraphItemsOffsets[i] ); } paragraphItemOffset += ( sizeof(WCHAR) * (*iter)->GetAllText().size() ); } } return allParagraphItems; } /*========================================================================================================*/ vector List::GetAllParagraphsProperties( vector* allParagraphsOffsets ) const { vector allParagraphsProperties; unsigned int paragraphOffset = 0; if ( allParagraphsOffsets != NULL ) { vector allParagraphs = this->GetAllParagraphsCopy(); for ( vector::const_iterator iter = allParagraphs.begin(); iter != allParagraphs.end(); iter++ ) { Paragraph* paragraph = dynamic_cast( iter->get() ); if ( paragraph != NULL ) { allParagraphsProperties.push_back( paragraph->GetProperties() ); allParagraphsOffsets->push_back( paragraphOffset ); paragraphOffset += ( sizeof(WCHAR) * paragraph->GetAllText().size() ); } } } return allParagraphsProperties; } /*========================================================================================================*/ vector List::GetAllRunProperties( vector* allRunsOffsets ) const { vector allRunProperties; unsigned int runOffset = 0; if ( allRunsOffsets != NULL ) { vector allParagraphs = this->GetAllParagraphsCopy(); for ( vector::const_iterator iter = allParagraphs.begin(); iter != allParagraphs.end(); iter++ ) { vector paragraphRunProperties; vector paragraphRunsOffsets; Paragraph* paragraph = dynamic_cast( iter->get() ); if ( paragraph != NULL ) { paragraphRunProperties = paragraph->GetAllRunProperties( ¶graphRunsOffsets ); for ( unsigned int i = 0; i < paragraphRunProperties.size(); i++ ) { allRunProperties.push_back( paragraphRunProperties[i] ); allRunsOffsets->push_back( runOffset + paragraphRunsOffsets[i] ); } runOffset += ( sizeof(WCHAR) * paragraph->GetAllText().size() ); } } } return allRunProperties; } /*========================================================================================================*/ List::~List() { } /*========================================================================================================*/ IVirtualConstructor* List::New() const { return new List(); } /*========================================================================================================*/ IVirtualConstructor* List::Clone() const { return new List( *this ); } }