[gparted] Create and use general find_extended_partition() function



commit aa98107706405fe0a80d4fd1a1ddb0838518ea7f
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Sun Aug 21 11:56:51 2016 +0100

    Create and use general find_extended_partition() function
    
    The Operation class already provided find_index_extended() method and
     was used in the Operation and derived classes where required.  It
    returns the index to the extended partition in the PartitionVector
    object, or -1 when no extended partition exists.
    
    There were several cases of the same functionality being open coded in
    GParted_Core and Win_GParted.  Therefore move the implementation to
    find_extended_partition() in PartitionVector compilation unit and use
    this implementation everywhere.

 include/Operation.h        |    1 -
 include/PartitionVector.h  |    2 ++
 src/GParted_Core.cc        |   22 ++++------------------
 src/Operation.cc           |   13 ++-----------
 src/OperationDelete.cc     |    3 +--
 src/OperationResizeMove.cc |   13 +++++--------
 src/PartitionVector.cc     |    9 +++++++++
 src/Win_GParted.cc         |   20 ++++----------------
 8 files changed, 27 insertions(+), 56 deletions(-)
---
diff --git a/include/Operation.h b/include/Operation.h
index f84e133..caee6bb 100644
--- a/include/Operation.h
+++ b/include/Operation.h
@@ -66,7 +66,6 @@ public:
 protected:
        int find_index_original( const PartitionVector & partitions );
        int find_index_new( const PartitionVector & partitions );
-       int find_index_extended( const PartitionVector & partitions );
        void insert_unallocated( PartitionVector & partitions,
                                 Sector start, Sector end, Byte_Value sector_size, bool inside_extended );
        void substitute_new( PartitionVector & partitions );
diff --git a/include/PartitionVector.h b/include/PartitionVector.h
index 37054d6..8c1d255 100644
--- a/include/PartitionVector.h
+++ b/include/PartitionVector.h
@@ -77,6 +77,8 @@ private:
        std::vector<Partition *> v;
 };
 
+int find_extended_partition( const PartitionVector & partitions );
+
 } //GParted
 
 #endif /* GPARTED_PARTITIONVECTOR_H */
diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc
index c3c35a0..fe5f71b 100644
--- a/src/GParted_Core.cc
+++ b/src/GParted_Core.cc
@@ -380,17 +380,10 @@ bool GParted_Core::snap_to_mebibyte( const Device & device, Partition & partitio
                //  available for any following logical partition Extended Boot Record
                if ( partition .type == TYPE_EXTENDED )
                {
-                       //Locate the extended partition that contains the logical partitions.
-                       int index_extended = -1 ;
-                       for ( unsigned int t = 0 ; t < device .partitions .size() ; t++ )
-                       {
-                               if ( device .partitions[ t ] .type == TYPE_EXTENDED )
-                                       index_extended = t ;
-                       }
-
                        //If there is logical partition that starts less than 2 sectors
                        //  from the start of this partition, then reserve a mebibyte for the EBR.
-                       if ( index_extended != -1 )
+                       int index_extended = find_extended_partition( device.partitions );
+                       if ( index_extended >= 0 )
                        {
                                for ( unsigned int t = 0; t < device .partitions[ index_extended ] .logicals 
.size(); t++ )
                                {
@@ -420,17 +413,10 @@ bool GParted_Core::snap_to_mebibyte( const Device & device, Partition & partitio
        //  required for a following logical partition Extended Boot Record
        if ( partition .type == TYPE_LOGICAL )
        {
-               //Locate the extended partition that contains the logical partitions.
-               int index_extended = -1 ;
-               for ( unsigned int t = 0 ; t < device .partitions .size() ; t++ )
-               {
-                       if ( device .partitions[ t ] .type == TYPE_EXTENDED )
-                               index_extended = t ;
-               }
-
                //If there is a following logical partition that starts less than 2 sectors from
                //  the end of this partition, then reserve at least a mebibyte for the EBR.
-               if ( index_extended != -1 )
+               int index_extended = find_extended_partition( device.partitions );
+               if ( index_extended >= 0 )
                {
                        for ( unsigned int t = 0; t < device .partitions[ index_extended ] .logicals .size(); 
t++ )
                        {
diff --git a/src/Operation.cc b/src/Operation.cc
index feb5ff7..0661839 100644
--- a/src/Operation.cc
+++ b/src/Operation.cc
@@ -81,15 +81,6 @@ int Operation::find_index_new( const PartitionVector & partitions )
        return -1;
 }
 
-int Operation::find_index_extended( const PartitionVector & partitions )
-{
-       for ( unsigned int t = 0 ; t < partitions .size() ; t++ )
-               if ( partitions[ t ] .type == GParted::TYPE_EXTENDED )
-                       return t ;
-
-       return -1 ;
-}
-
 void Operation::insert_unallocated( PartitionVector & partitions,
                                     Sector start, Sector end, Byte_Value sector_size, bool inside_extended )
 {
@@ -110,7 +101,7 @@ void Operation::substitute_new( PartitionVector & partitions )
 
        if ( partition_original->inside_extended )
        {
-               index_extended = find_index_extended( partitions );
+               index_extended = find_extended_partition( partitions );
                if ( index_extended >= 0 )
                {
                        index = find_index_original( partitions[index_extended].logicals );
@@ -146,7 +137,7 @@ void Operation::insert_new( PartitionVector & partitions )
 
        if ( partition_new->inside_extended )
        {
-               index_extended = find_index_extended( partitions );
+               index_extended = find_extended_partition( partitions );
                if ( index_extended >= 0 )
                {
                        index = find_index_new( partitions[index_extended].logicals );
diff --git a/src/OperationDelete.cc b/src/OperationDelete.cc
index b33ed6b..cfeadc3 100644
--- a/src/OperationDelete.cc
+++ b/src/OperationDelete.cc
@@ -61,8 +61,7 @@ void OperationDelete::apply_to_visual( PartitionVector & partitions )
 
        if ( partition_original->inside_extended )
        {
-               index_extended = find_index_extended( partitions ) ;
-               
+               index_extended = find_extended_partition( partitions );
                if ( index_extended >= 0 )
                {
                        index = find_index_original( partitions[ index_extended ] .logicals ) ;
diff --git a/src/OperationResizeMove.cc b/src/OperationResizeMove.cc
index 7c27601..22f80cb 100644
--- a/src/OperationResizeMove.cc
+++ b/src/OperationResizeMove.cc
@@ -151,8 +151,7 @@ void OperationResizeMove::apply_normal_to_visual( PartitionVector & partitions )
 
        if ( partition_original->inside_extended )
        {
-               index_extended = find_index_extended( partitions ) ;
-                       
+               index_extended = find_extended_partition( partitions );
                if ( index_extended >= 0 )
                {
                        index = find_index_original( partitions[ index_extended ] .logicals ) ;
@@ -191,13 +190,12 @@ void OperationResizeMove::apply_extended_to_visual( PartitionVector & partitions
        int index_extended;
 
        //stuff OUTSIDE extended partition
-       index_extended = find_index_extended( partitions ) ;
-               
+       index_extended = find_extended_partition( partitions );
        if ( index_extended >= 0 )
        {
                remove_adjacent_unallocated( partitions, index_extended ) ;
-               
-               index_extended = find_index_extended( partitions ) ;
+
+               index_extended = find_extended_partition( partitions );
                if ( index_extended >= 0 )
                {
                        partitions[index_extended].sector_start = partition_new->sector_start;
@@ -208,8 +206,7 @@ void OperationResizeMove::apply_extended_to_visual( PartitionVector & partitions
        }
        
        //stuff INSIDE extended partition
-       index_extended = find_index_extended( partitions ) ;
-       
+       index_extended = find_extended_partition( partitions );
        if ( index_extended >= 0 )
        {
                if ( partitions[ index_extended ] .logicals .size() > 0 &&
diff --git a/src/PartitionVector.cc b/src/PartitionVector.cc
index edc172f..cad8007 100644
--- a/src/PartitionVector.cc
+++ b/src/PartitionVector.cc
@@ -90,4 +90,13 @@ void PartitionVector::replace_at( size_type n, const Partition * partition )
        v[n] = p;
 }
 
+// Return index of the extended partition or -1 when not found
+int find_extended_partition( const PartitionVector & partitions )
+{
+       for ( unsigned int i = 0 ; i < partitions.size() ; i ++ )
+               if ( partitions[i].type == TYPE_EXTENDED )
+                       return (int)i;
+       return -1;
+}
+
 } //GParted
diff --git a/src/Win_GParted.cc b/src/Win_GParted.cc
index 22ae41f..e0e901b 100644
--- a/src/Win_GParted.cc
+++ b/src/Win_GParted.cc
@@ -1783,13 +1783,9 @@ void Win_GParted::activate_resize()
        g_assert( valid_display_partition_ptr( selected_partition_ptr ) );  // Bug: Not pointing at a valid 
display partition object
 
        PartitionVector * display_partitions_ptr = &display_partitions;
-       if ( selected_partition_ptr->type == TYPE_LOGICAL )
-       {
-               unsigned int ext = 0 ;
-               while ( ext < display_partitions.size() && display_partitions[ext].type != TYPE_EXTENDED )
-                       ext++;
-               display_partitions_ptr = &display_partitions[ext].logicals;
-       }
+       int index_extended = find_extended_partition( display_partitions );
+       if ( index_extended >= 0 )
+               display_partitions_ptr = &display_partitions[index_extended].logicals;
 
        FS fs_cap = gparted_core.get_fs( selected_partition_ptr->get_filesystem_partition().filesystem );
        Partition * working_ptn;
@@ -2085,15 +2081,7 @@ void Win_GParted::activate_new()
                // Check if an extended partition already exist; so that the dialog can
                // decide whether to allow the creation of the only extended partition
                // type or not.
-               bool any_extended = false;
-               for ( unsigned int i = 0 ; i < display_partitions.size() ; i ++ )
-               {
-                       if ( display_partitions[i].type == TYPE_EXTENDED )
-                       {
-                               any_extended = true;
-                               break;
-                       }
-               }
+               bool any_extended = ( find_extended_partition( display_partitions ) >= 0 );
                Dialog_Partition_New dialog( devices[current_device],
                                             *selected_partition_ptr,
                                             any_extended,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]