[gparted] Enhance format_size() for sector sizes > 512 bytes



commit 44e5176ee54b5f736a15227bcf1c9a042fab903e
Author: Curtis Gedak <gedakc gmail com>
Date:   Tue Mar 23 13:06:26 2010 -0600

    Enhance format_size() for sector sizes > 512 bytes

 include/Utils.h              |    2 +-
 src/Dialog_Partition_Info.cc |   10 +++++-----
 src/DrawingAreaVisualDisk.cc |    3 ++-
 src/GParted_Core.cc          |   38 +++++++++++++++++++-------------------
 src/OperationCopy.cc         |    3 ++-
 src/OperationCreate.cc       |    3 ++-
 src/OperationDelete.cc       |    3 ++-
 src/OperationResizeMove.cc   |    6 +++---
 src/TreeView_Detail.cc       |    8 ++++----
 src/Utils.cc                 |   20 ++++++++++----------
 src/Win_GParted.cc           |   12 ++++++------
 11 files changed, 56 insertions(+), 52 deletions(-)
---
diff --git a/include/Utils.h b/include/Utils.h
index 0e4a366..960fc7c 100644
--- a/include/Utils.h
+++ b/include/Utils.h
@@ -144,7 +144,7 @@ public:
 	static Glib::RefPtr<Gdk::Pixbuf> get_color_as_pixbuf( FILESYSTEM filesystem, int width, int height ) ;
 	static Glib::ustring get_filesystem_string( FILESYSTEM filesystem ) ;
 	static Glib::ustring get_filesystem_software( FILESYSTEM filesystem ) ;
-	static Glib::ustring format_size( Sector size ) ;
+	static Glib::ustring format_size( Sector sectors, Byte_Value sector_size ) ;
 	static Glib::ustring format_time( std::time_t seconds ) ;
 	static double sector_to_unit( Sector sectors, Byte_Value sector_size, SIZE_UNIT size_unit ) ;
 	static int execute_command( const Glib::ustring & command ) ;
diff --git a/src/Dialog_Partition_Info.cc b/src/Dialog_Partition_Info.cc
index c71fdfa..a99eb24 100644
--- a/src/Dialog_Partition_Info.cc
+++ b/src/Dialog_Partition_Info.cc
@@ -1,5 +1,5 @@
 /* Copyright (C) 2004 Bart
- * Copyright (C) 2008, 2009 Curtis Gedak
+ * Copyright (C) 2008, 2009, 2010 Curtis Gedak
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -136,7 +136,7 @@ void Dialog_Partition_Info::init_drawingarea()
 	
 	//set text of pangolayout
 	pango_layout = drawingarea .create_pango_layout( 
-		partition .get_path() + "\n" + Utils::format_size( partition .get_length() ) ) ;
+		partition .get_path() + "\n" + Utils::format_size( partition .get_length(), DEFAULT_SECTOR_SIZE ) ) ;
 }
 
 void Dialog_Partition_Info::Display_Info()
@@ -163,7 +163,7 @@ void Dialog_Partition_Info::Display_Info()
 			0, 1,
 			top, bottom,
 			Gtk::FILL) ;
-	table ->attach( * Utils::mk_label( Utils::format_size( partition .get_length() ), true, Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false, true ),
+	table ->attach( * Utils::mk_label( Utils::format_size( partition .get_length(), DEFAULT_SECTOR_SIZE ), true, Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false, true ),
 			1, 2,
 			top++, bottom++,
 			Gtk::FILL ) ;
@@ -179,7 +179,7 @@ void Dialog_Partition_Info::Display_Info()
 				0, 1,
 				top, bottom,
 				Gtk::FILL ) ;
-		table ->attach( * Utils::mk_label( Utils::format_size( partition .sectors_used ), true, Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false, true ),
+		table ->attach( * Utils::mk_label( Utils::format_size( partition .sectors_used, DEFAULT_SECTOR_SIZE ), true, Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false, true ),
 				1, 2,
 				top, bottom,
 				Gtk::FILL ) ;
@@ -193,7 +193,7 @@ void Dialog_Partition_Info::Display_Info()
 				0, 1,
 				top, bottom,
 				Gtk::FILL ) ;
-		table ->attach( * Utils::mk_label( Utils::format_size( partition .sectors_unused ), true, Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false, true ),
+		table ->attach( * Utils::mk_label( Utils::format_size( partition .sectors_unused, DEFAULT_SECTOR_SIZE ), true, Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false, true ),
 				1, 2,
 				top, bottom,
 				Gtk::FILL ) ;
diff --git a/src/DrawingAreaVisualDisk.cc b/src/DrawingAreaVisualDisk.cc
index 915f007..ea5f035 100644
--- a/src/DrawingAreaVisualDisk.cc
+++ b/src/DrawingAreaVisualDisk.cc
@@ -1,4 +1,5 @@
 /* Copyright (C) 2004 Bart
+ * Copyright (C) 2010 Curtis Gedak
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -107,7 +108,7 @@ void DrawingAreaVisualDisk::set_static_data( const std::vector<Partition> & part
 	   			         partitions[ t ] .get_length() ) ;
 		else
 			visual_partitions .back() .pango_layout = create_pango_layout( 
-				partitions[ t ] .get_path() + "\n" + Utils::format_size( partitions[ t ] .get_length() ) ) ;
+				partitions[ t ] .get_path() + "\n" + Utils::format_size( partitions[ t ] .get_length(), DEFAULT_SECTOR_SIZE ) ) ;
 	}
 }
 
diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc
index 89c5fdf..6fa3636 100644
--- a/src/GParted_Core.cc
+++ b/src/GParted_Core.cc
@@ -1388,8 +1388,8 @@ bool GParted_Core::create_partition( Partition & new_partition, OperationDetail
 						String::ucompose( _("start: %1"), new_partition .sector_start ) + "\n" +
 						String::ucompose( _("end: %1"), new_partition .sector_end ) + "\n" +
 						String::ucompose( _("size: %1 (%2)"),
-					  			  new_partition .get_length(),
-					  			  Utils::format_size( new_partition .get_length() ) ),
+								new_partition .get_length(),
+								Utils::format_size( new_partition .get_length(), DEFAULT_SECTOR_SIZE ) ),
 						STATUS_NONE,
 						FONT_ITALIC ) ) ;
 				}
@@ -1823,8 +1823,8 @@ bool GParted_Core::resize_move_partition( const Partition & partition_old,
 
 	if ( ! description .empty() && action != NONE && action != MOVE_LEFT && action != MOVE_RIGHT )
 		description = String::ucompose( description,
-						Utils::format_size( partition_old .get_length() ),
-						Utils::format_size( partition_new .get_length() ) ) ;
+						Utils::format_size( partition_old .get_length(), DEFAULT_SECTOR_SIZE ),
+						Utils::format_size( partition_new .get_length(), DEFAULT_SECTOR_SIZE ) ) ;
 
 	operationdetail .add_child( OperationDetail( description ) ) ;
 
@@ -1845,8 +1845,8 @@ bool GParted_Core::resize_move_partition( const Partition & partition_old,
 			String::ucompose( _("old start: %1"), partition_old .sector_start ) + "\n" +
 			String::ucompose( _("old end: %1"), partition_old .sector_end ) + "\n" +
 			String::ucompose( _("old size: %1 (%2)"),
-					  partition_old .get_length(),
-					  Utils::format_size( partition_old .get_length() ) ),
+					partition_old .get_length(),
+					Utils::format_size( partition_old .get_length(), DEFAULT_SECTOR_SIZE ) ),
 		STATUS_NONE, 
 		FONT_ITALIC ) ) ;
 	
@@ -1906,8 +1906,8 @@ bool GParted_Core::resize_move_partition( const Partition & partition_old,
 				String::ucompose( _("new start: %1"), new_start ) + "\n" +
 				String::ucompose( _("new end: %1"), new_end ) + "\n" +
 				String::ucompose( _("new size: %1 (%2)"),
-					 	  new_end - new_start + 1,
-					  	  Utils::format_size( new_end - new_start + 1 ) ),
+						new_end - new_start + 1,
+						Utils::format_size( new_end - new_start + 1, DEFAULT_SECTOR_SIZE ) ),
 			STATUS_NONE, 
 			FONT_ITALIC ) ) ;
 
@@ -2174,9 +2174,9 @@ bool GParted_Core::copy_filesystem( const Glib::ustring & src_device,
 	
 	if ( succes )
 		operationdetail .get_last_child() .add_child( OperationDetail( String::ucompose( _("optimal blocksize is %1 sectors (%2)"),
-					 							 optimal_blocksize,
-					 							 Utils::format_size( optimal_blocksize ) ),
-									       STATUS_NONE ) ) ;
+												optimal_blocksize,
+												Utils::format_size( optimal_blocksize, DEFAULT_SECTOR_SIZE ) ),
+												STATUS_NONE ) ) ;
 
 	if ( succes ) 
 	      succes = copy_blocks( src_device, 
@@ -2320,8 +2320,8 @@ void GParted_Core::set_progress_info( Sector total,
 
 	operationdetail .progress_text = 
 		String::ucompose( readonly ? _("%1 of %2 read (%3 remaining)") : _("%1 of %2 copied (%3 remaining)"),
-				  Utils::format_size( done ),
-				  Utils::format_size( total ),
+				  Utils::format_size( done, DEFAULT_SECTOR_SIZE ),
+				  Utils::format_size( total, DEFAULT_SECTOR_SIZE ),
 				  Utils::format_time( time_remaining) ) ; 
 			
 	operationdetail .set_description( 
@@ -2503,8 +2503,8 @@ bool GParted_Core::calibrate_partition( Partition & partition, OperationDetail &
 						String::ucompose( _("start: %1"), partition .sector_start ) + "\n" +
 						String::ucompose( _("end: %1"), partition .sector_end ) + "\n" +
 						String::ucompose( _("size: %1 (%2)"),
-								  partition .get_length(),
-								  Utils::format_size( partition .get_length() ) ),
+								partition .get_length(),
+								Utils::format_size( partition .get_length(), DEFAULT_SECTOR_SIZE ) ),
 					STATUS_NONE, 
 					FONT_ITALIC ) ) ;
 				succes = true ;
@@ -2532,8 +2532,8 @@ bool GParted_Core::calculate_exact_geom( const Partition & partition_old,
 			String::ucompose( _("requested start: %1"), partition_new .sector_start ) + "\n" +
 			String::ucompose( _("requested end: %1"), partition_new .sector_end ) + "\n" +
 			String::ucompose( _("requested size: %1 (%2)"),
-					  partition_new .get_length(),
-					  Utils::format_size( partition_new .get_length() ) ),
+					partition_new .get_length(),
+					Utils::format_size( partition_new .get_length(), DEFAULT_SECTOR_SIZE ) ),
 		STATUS_NONE,
 		FONT_ITALIC ) ) ;
 	
@@ -2583,8 +2583,8 @@ bool GParted_Core::calculate_exact_geom( const Partition & partition_old,
 				String::ucompose( _("new start: %1"), partition_new .sector_start ) + "\n" +
 				String::ucompose( _("new end: %1"), partition_new .sector_end ) + "\n" +
 				String::ucompose( _("new size: %1 (%2)"),
-					 	  partition_new .get_length(),
-					  	  Utils::format_size( partition_new .get_length() ) ),
+						partition_new .get_length(),
+						Utils::format_size( partition_new .get_length(), DEFAULT_SECTOR_SIZE ) ),
 			STATUS_NONE,
 			FONT_ITALIC ) ) ;
 
diff --git a/src/OperationCopy.cc b/src/OperationCopy.cc
index 75aeec6..b1684a9 100644
--- a/src/OperationCopy.cc
+++ b/src/OperationCopy.cc
@@ -1,4 +1,5 @@
 /* Copyright (C) 2004 Bart 'plors' Hakvoort
+ * Copyright (C) 2010 Curtis Gedak
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -78,7 +79,7 @@ void OperationCopy::create_description()
 		description = String::ucompose( _("Copy %1 to %2 (start at %3)"),
 					        partition_copied .get_path(),
 					        device .get_path(),
-					        Utils::format_size( partition_new .sector_start ) ) ;
+					        Utils::format_size( partition_new .sector_start, DEFAULT_SECTOR_SIZE ) ) ;
 	}
 	else
 	{
diff --git a/src/OperationCreate.cc b/src/OperationCreate.cc
index 47ba528..1bd378c 100644
--- a/src/OperationCreate.cc
+++ b/src/OperationCreate.cc
@@ -1,4 +1,5 @@
 /* Copyright (C) 2004 Bart 'plors' Hakvoort
+ * Copyright (C) 2010 Curtis Gedak
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -87,7 +88,7 @@ void OperationCreate::create_description()
 				 	description, 
 					partition_new .partition_number, 
 					Utils::get_filesystem_string( partition_new .filesystem ), 
-					Utils::format_size( partition_new .get_length() ),
+					Utils::format_size( partition_new .get_length(), DEFAULT_SECTOR_SIZE ),
 					device .get_path() ) ;
 }
 
diff --git a/src/OperationDelete.cc b/src/OperationDelete.cc
index e66012a..b74a657 100644
--- a/src/OperationDelete.cc
+++ b/src/OperationDelete.cc
@@ -1,4 +1,5 @@
 /* Copyright (C) 2004 Bart 'plors' Hakvoort
+ * Copyright (C) 2010 Curtis Gedak
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -80,7 +81,7 @@ void OperationDelete::create_description()
 	description = String::ucompose( _("Delete %1 (%2, %3) from %4"),
 					description,
 					Utils::get_filesystem_string( partition_original .filesystem ), 
-					Utils::format_size( partition_original .get_length() ),
+					Utils::format_size( partition_original .get_length(), DEFAULT_SECTOR_SIZE ),
 					partition_original .device_path ) ;
 }
 	
diff --git a/src/OperationResizeMove.cc b/src/OperationResizeMove.cc
index 2960cff..ffcdf17 100644
--- a/src/OperationResizeMove.cc
+++ b/src/OperationResizeMove.cc
@@ -1,5 +1,5 @@
 /* Copyright (C) 2004 Bart 'plors' Hakvoort
- * Copyright (C) 2008, 2009 Curtis Gedak
+ * Copyright (C) 2008, 2009, 2010 Curtis Gedak
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -117,8 +117,8 @@ void OperationResizeMove::create_description()
 	if ( ! description .empty() && action != NONE && action != MOVE_LEFT && action != MOVE_RIGHT )
 		description = String::ucompose( description,
 						partition_original .get_path(),
-						Utils::format_size( partition_original .get_length() ),
-						Utils::format_size( partition_new .get_length() ) ) ;
+						Utils::format_size( partition_original .get_length(), DEFAULT_SECTOR_SIZE ),
+						Utils::format_size( partition_new .get_length(), DEFAULT_SECTOR_SIZE ) ) ;
 }
 
 void OperationResizeMove::apply_normal_to_visual( std::vector<Partition> & partitions )
diff --git a/src/TreeView_Detail.cc b/src/TreeView_Detail.cc
index 83893aa..167a9de 100644
--- a/src/TreeView_Detail.cc
+++ b/src/TreeView_Detail.cc
@@ -1,5 +1,5 @@
 /* Copyright (C) 2004 Bart
- * Copyright (C) 2008 Curtis Gedak
+ * Copyright (C) 2008, 2009, 2010 Curtis Gedak
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -187,15 +187,15 @@ void TreeView_Detail::create_row( const Gtk::TreeRow & treerow, const Partition
 	treerow[ treeview_detail_columns .label ] = partition .label ;
 		
 	//size
-	treerow[ treeview_detail_columns .size ] = Utils::format_size( partition .get_length() ) ;
+	treerow[ treeview_detail_columns .size ] = Utils::format_size( partition .get_length(), DEFAULT_SECTOR_SIZE ) ;
 	
 	//used
 	treerow[ treeview_detail_columns .used ] =
-		partition .sectors_used == -1 ? "---" : Utils::format_size( partition .sectors_used ) ;
+		partition .sectors_used == -1 ? "---" : Utils::format_size( partition .sectors_used, DEFAULT_SECTOR_SIZE ) ;
 
 	//unused
 	treerow[ treeview_detail_columns .unused ] = 
-		partition .sectors_unused == -1 ? "---" : Utils::format_size( partition .sectors_unused ) ;
+		partition .sectors_unused == -1 ? "---" : Utils::format_size( partition .sectors_unused, DEFAULT_SECTOR_SIZE ) ;
 
 	//flags	
 	treerow[ treeview_detail_columns .flags ] = 
diff --git a/src/Utils.cc b/src/Utils.cc
index 52d4b03..24cc8c8 100644
--- a/src/Utils.cc
+++ b/src/Utils.cc
@@ -167,34 +167,34 @@ Glib::ustring Utils::get_filesystem_software( FILESYSTEM filesystem )
 	}
 }
 
-Glib::ustring Utils::format_size( Sector size )
+Glib::ustring Utils::format_size( Sector sectors, Byte_Value sector_size )
 {
 	std::stringstream ss ;
 	ss << std::setiosflags( std::ios::fixed ) << std::setprecision( 2 ) ;
 
-	if ( size < KIBIBYTE )
+	if ( (sectors * sector_size) < KIBI_FACTOR )
 	{
-		ss << sector_to_unit( size, DEFAULT_SECTOR_SIZE, UNIT_BYTE ) ;
+		ss << sector_to_unit( sectors, sector_size, UNIT_BYTE ) ;
 		return String::ucompose( _("%1 B"), ss .str() ) ;
 	}
-	else if ( size < MEBIBYTE )
+	else if ( (sectors * sector_size) < MEBI_FACTOR )
 	{
-		ss << sector_to_unit( size, DEFAULT_SECTOR_SIZE, UNIT_KIB ) ;
+		ss << sector_to_unit( sectors, sector_size, UNIT_KIB ) ;
 		return String::ucompose( _("%1 KiB"), ss .str() ) ;
 	}
-	else if ( size < GIBIBYTE )
+	else if ( (sectors * sector_size) < GIBI_FACTOR )
 	{
-		ss << sector_to_unit( size, DEFAULT_SECTOR_SIZE, UNIT_MIB ) ;
+		ss << sector_to_unit( sectors, sector_size, UNIT_MIB ) ;
 		return String::ucompose( _("%1 MiB"), ss .str() ) ;
 	}
-	else if ( size < TEBIBYTE )
+	else if ( (sectors * sector_size) < TEBI_FACTOR )
 	{
-		ss << sector_to_unit( size, DEFAULT_SECTOR_SIZE, UNIT_GIB ) ;
+		ss << sector_to_unit( sectors, sector_size, UNIT_GIB ) ;
 		return String::ucompose( _("%1 GiB"), ss .str() ) ;
 	}
 	else
 	{
-		ss << sector_to_unit( size, DEFAULT_SECTOR_SIZE, UNIT_TIB ) ;
+		ss << sector_to_unit( sectors, sector_size, UNIT_TIB ) ;
 		return String::ucompose( _("%1 TiB"), ss .str() ) ;
 	}
 }
diff --git a/src/Win_GParted.cc b/src/Win_GParted.cc
index 117b439..f53a1dc 100644
--- a/src/Win_GParted.cc
+++ b/src/Win_GParted.cc
@@ -565,12 +565,12 @@ void Win_GParted::refresh_combo_devices()
 		treerow[ treeview_devices_columns .icon ] =
 			render_icon( Gtk::Stock::HARDDISK, Gtk::ICON_SIZE_LARGE_TOOLBAR ) ;
 		treerow[ treeview_devices_columns .device ] = devices[ i ] .get_path() ;
-		treerow[ treeview_devices_columns .size ] = "(" + Utils::format_size( devices[ i ] .length ) + ")" ; 
+		treerow[ treeview_devices_columns .size ] = "(" + Utils::format_size( devices[ i ] .length, DEFAULT_SECTOR_SIZE ) + ")" ; 
 	
 		//devices submenu....
 		hbox = manage( new Gtk::HBox() ) ;
 		hbox ->pack_start( * Utils::mk_label( devices[ i ] .get_path() ), Gtk::PACK_SHRINK ) ;
-		hbox ->pack_start( * Utils::mk_label( "   (" + Utils::format_size( devices[ i ] .length ) + ")",
+		hbox ->pack_start( * Utils::mk_label( "   (" + Utils::format_size( devices[ i ] .length, DEFAULT_SECTOR_SIZE ) + ")",
 					              true,
 						      Gtk::ALIGN_RIGHT ),
 				   Gtk::PACK_EXPAND_WIDGET ) ;
@@ -645,7 +645,7 @@ void Win_GParted::Fill_Label_Device_Info( bool clear )
 		
 		//global info...
 		device_info[ t++ ] ->set_text( devices[ current_device ] .model ) ;
-		device_info[ t++ ] ->set_text( Utils::format_size( devices[ current_device ] .length ) ) ;
+		device_info[ t++ ] ->set_text( Utils::format_size( devices[ current_device ] .length, DEFAULT_SECTOR_SIZE ) ) ;
 		device_info[ t++ ] ->set_text( Glib::build_path( "\n", devices[ current_device ] .get_paths() ) ) ;
 		
 		//detailed info
@@ -1581,7 +1581,7 @@ void Win_GParted::activate_delete()
 		dialog .set_title( String::ucompose( _("Delete %1 (%2, %3)"), 
 						     selected_partition .get_path(), 
 						     Utils::get_filesystem_string( selected_partition .filesystem ),
-						     Utils::format_size( selected_partition .get_length() ) ) );
+						     Utils::format_size( selected_partition .get_length(), DEFAULT_SECTOR_SIZE ) ) );
 		dialog .add_button( Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL );
 		dialog .add_button( Gtk::Stock::DELETE, Gtk::RESPONSE_OK );
 	
@@ -1655,12 +1655,12 @@ void Win_GParted::activate_format( GParted::FILESYSTEM new_fs )
 			dialog .set_secondary_text( String::ucompose( 
 						_( "A %1 file system requires a partition of at least %2."),
 						Utils::get_filesystem_string( new_fs ),
-						Utils::format_size( (fs .MIN / DEFAULT_SECTOR_SIZE) ) ) );
+						Utils::format_size( (fs .MIN / DEFAULT_SECTOR_SIZE), DEFAULT_SECTOR_SIZE ) ) );
 		else
 			dialog .set_secondary_text( String::ucompose( 
 						_( "A partition with a %1 file system has a maximum size of %2."),
 						Utils::get_filesystem_string( new_fs ),
-						Utils::format_size( (fs .MAX / DEFAULT_SECTOR_SIZE) ) ) );
+						Utils::format_size( (fs .MAX / DEFAULT_SECTOR_SIZE), DEFAULT_SECTOR_SIZE ) ) );
 		
 		dialog .run() ;
 		return ;



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