diff --git a/src/modules/partition/CMakeLists.txt b/src/modules/partition/CMakeLists.txt index 01fd705a0..47574fb16 100644 --- a/src/modules/partition/CMakeLists.txt +++ b/src/modules/partition/CMakeLists.txt @@ -31,6 +31,7 @@ calamares_add_plugin( partition FormatPartitionJob.cpp PartitionCoreModule.cpp PartitionInfo.cpp + PartitionIterator.cpp PartitionJob.cpp PartitionModel.cpp PartitionPage.cpp diff --git a/src/modules/partition/PartitionCoreModule.cpp b/src/modules/partition/PartitionCoreModule.cpp index 09eec3a18..4e7d34b62 100644 --- a/src/modules/partition/PartitionCoreModule.cpp +++ b/src/modules/partition/PartitionCoreModule.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -39,86 +40,6 @@ // Qt #include -class PartitionIterator -{ -public: - Partition* operator*() const - { - return m_current; - } - - void operator++() - { - if ( !m_current ) - return; - if ( m_current->hasChildren() ) - { - // Go to the first child - m_current = static_cast< Partition* >( m_current->children().first() ); - return; - } - PartitionNode* parent = m_current->parent(); - Partition* successor = parent->successor( *m_current ); - if ( successor ) - { - // Go to the next sibling - m_current = successor; - return; - } - if ( parent->isRoot() ) - { - // We reached the end - m_current = nullptr; - return; - } - // Try to go to the next sibling of our parent - - PartitionNode* grandParent = parent->parent(); - Q_ASSERT( grandParent ); - // If parent is not root, then it's not a PartitionTable but a - // Partition, we can static_cast it. - m_current = grandParent->successor( *static_cast< Partition* >( parent ) ); - } - - bool operator==( const PartitionIterator& other ) const - { - return m_device == other.m_device && m_current == other.m_current; - } - - bool operator!=( const PartitionIterator& other ) const - { - return ! ( *this == other ); - } - - static PartitionIterator begin( Device* device ) - { - auto it = PartitionIterator( device ); - PartitionTable* table = device->partitionTable(); - if ( !table ) - return it; - QList< Partition* > children = table->children(); - // Does not usually happen, but it did happen on a 10MB disk with an MBR - // partition table. - if ( children.isEmpty() ) - return it; - it.m_current = children.first(); - return it; - } - - static PartitionIterator end( Device* device ) - { - return PartitionIterator( device ); - } - -private: - PartitionIterator( Device* device ) - : m_device( device ) - {} - - Device* m_device; - Partition* m_current = nullptr; -}; - //- DeviceInfo --------------------------------------------- PartitionCoreModule::DeviceInfo::DeviceInfo( Device* _device ) : device( _device ) diff --git a/src/modules/partition/PartitionIterator.cpp b/src/modules/partition/PartitionIterator.cpp new file mode 100644 index 000000000..f31e53866 --- /dev/null +++ b/src/modules/partition/PartitionIterator.cpp @@ -0,0 +1,102 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014, Aurélien Gâteau + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include + +// CalaPM +#include +#include +#include + +PartitionIterator::PartitionIterator( Device* device ) + : m_device( device ) +{} + +Partition* +PartitionIterator::operator*() const +{ + return m_current; +} + +void +PartitionIterator::operator++() +{ + if ( !m_current ) + return; + if ( m_current->hasChildren() ) + { + // Go to the first child + m_current = static_cast< Partition* >( m_current->children().first() ); + return; + } + PartitionNode* parent = m_current->parent(); + Partition* successor = parent->successor( *m_current ); + if ( successor ) + { + // Go to the next sibling + m_current = successor; + return; + } + if ( parent->isRoot() ) + { + // We reached the end + m_current = nullptr; + return; + } + // Try to go to the next sibling of our parent + + PartitionNode* grandParent = parent->parent(); + Q_ASSERT( grandParent ); + // If parent is not root, then it's not a PartitionTable but a + // Partition, we can static_cast it. + m_current = grandParent->successor( *static_cast< Partition* >( parent ) ); +} + +bool +PartitionIterator::operator==( const PartitionIterator& other ) const +{ + return m_device == other.m_device && m_current == other.m_current; +} + +bool +PartitionIterator::operator!=( const PartitionIterator& other ) const +{ + return ! ( *this == other ); +} + +PartitionIterator +PartitionIterator::begin( Device* device ) +{ + auto it = PartitionIterator( device ); + PartitionTable* table = device->partitionTable(); + if ( !table ) + return it; + QList< Partition* > children = table->children(); + // Does not usually happen, but it did happen on a 10MB disk with an MBR + // partition table. + if ( children.isEmpty() ) + return it; + it.m_current = children.first(); + return it; +} + +PartitionIterator +PartitionIterator::end( Device* device ) +{ + return PartitionIterator( device ); +} diff --git a/src/modules/partition/PartitionIterator.h b/src/modules/partition/PartitionIterator.h new file mode 100644 index 000000000..7b527b241 --- /dev/null +++ b/src/modules/partition/PartitionIterator.h @@ -0,0 +1,49 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014, Aurélien Gâteau + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef PARTITIONITERATOR_H +#define PARTITIONITERATOR_H + +class Device; +class Partition; + +/** + * A forward-only iterator to go through the partitions of a device, + * independently of whether they are primary, logical or extended. + */ +class PartitionIterator +{ +public: + Partition* operator*() const; + + void operator++(); + + bool operator==( const PartitionIterator& other ) const; + bool operator!=( const PartitionIterator& other ) const; + + static PartitionIterator begin( Device* device ); + static PartitionIterator end( Device* device ); + +private: + PartitionIterator( Device* device ); + + Device* m_device; + Partition* m_current = nullptr; +}; + +#endif /* PARTITIONITERATOR_H */