Bladeren bron

Moved size string formatting method to a separate class

Tal Aloni 8 jaren geleden
bovenliggende
commit
0db4e2aa2c
3 gewijzigde bestanden met toevoegingen van 46 en 59 verwijderingen
  1. 40 0
      ISCSIConsole/FormattingHelper.cs
  2. 1 0
      ISCSIConsole/ISCSIConsole.csproj
  3. 5 59
      ISCSIConsole/Program.ListCommand.cs

+ 40 - 0
ISCSIConsole/FormattingHelper.cs

@@ -0,0 +1,40 @@
+/* Copyright (C) 2012-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+ * 
+ * You can redistribute this program and/or modify it under the terms of
+ * the GNU Lesser Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ */
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace ISCSIConsole
+{
+    public class FormattingHelper
+    {
+        public static string GetStandardSizeString(long value)
+        {
+            string[] suffixes = { " B", "KB", "MB", "GB", "TB", "PB", "EB" };
+            int suffixIndex = 0;
+            while (value > 9999)
+            {
+                value = value / 1024;
+                suffixIndex++;
+            }
+
+            if (suffixIndex < suffixes.Length)
+            {
+                string FourCharacterValue = value.ToString();
+                while (FourCharacterValue.Length < 4)
+                {
+                    FourCharacterValue = " " + FourCharacterValue;
+                }
+                return String.Format("{0} {1}", FourCharacterValue, suffixes[suffixIndex]);
+            }
+            else
+            {
+                return "Too Big";
+            }
+        }
+    }
+}

+ 1 - 0
ISCSIConsole/ISCSIConsole.csproj

@@ -33,6 +33,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Disks\VolumeDisk.cs" />
+    <Compile Include="FormattingHelper.cs" />
     <Compile Include="ISCSINameHelper.cs" />
     <Compile Include="OfflineCommand.cs" />
     <Compile Include="Program.AttachCommand.cs" />

+ 5 - 59
ISCSIConsole/Program.ListCommand.cs

@@ -80,7 +80,7 @@ namespace ISCSIConsole
                 }
 
                 diskID = diskID.PadLeft(6);
-                Console.WriteLine("Disk {0}  {1}  {2}  {3}  {4}  {5}", diskNumber, GetStandardSizeString(disk.Size), isGPTStr, isDynStr, diskID, diskGroupName);
+                Console.WriteLine("Disk {0}  {1}  {2}  {3}  {4}  {5}", diskNumber, FormattingHelper.GetStandardSizeString(disk.Size), isGPTStr, isDynStr, diskID, diskGroupName);
             }
         }
 
@@ -107,7 +107,7 @@ namespace ISCSIConsole
                     }
                     partitionType = partitionType.PadRight(16);
                     string startSector = partition.FirstSector.ToString().PadLeft(12);
-                    Console.WriteLine("Partition {0}  {1}  {2}  {3}  {4}", index.ToString(), partitionType, GetStandardSizeString(size), GetStandardSizeString(offset), startSector);
+                    Console.WriteLine("Partition {0}  {1}  {2}  {3}  {4}", index.ToString(), partitionType, FormattingHelper.GetStandardSizeString(size), FormattingHelper.GetStandardSizeString(offset), startSector);
                 }
             }
             else
@@ -192,7 +192,7 @@ namespace ISCSIConsole
                 }
                 volumeIDString = volumeIDString.PadRight(4);
 
-                Console.WriteLine("Volume {0}  {1}  {2}  {3}  {4}  {5}", volumeNumber, volumeIDString, name, type, GetStandardSizeString(volume.Size), status);
+                Console.WriteLine("Volume {0}  {1}  {2}  {3}  {4}  {5}", volumeNumber, volumeIDString, name, type, FormattingHelper.GetStandardSizeString(volume.Size), status);
             }
         }
 
@@ -232,7 +232,7 @@ namespace ISCSIConsole
                     if (extent.Disk != null)
                     {
                         long offset = extent.FirstSector * extent.Disk.BytesPerSector;
-                        offsetString = GetStandardSizeString(offset);
+                        offsetString = FormattingHelper.GetStandardSizeString(offset);
                     }
                     else
                     {
@@ -258,7 +258,7 @@ namespace ISCSIConsole
 
                     string startSector = extent.FirstSector.ToString().PadLeft(12);
 
-                    Console.WriteLine("Extent {0}  {1}  {2}  {3}  {4}  {5}  {6}", extentNumber, extentIDString, name, GetStandardSizeString(size), diskIDString, offsetString, startSector);
+                    Console.WriteLine("Extent {0}  {1}  {2}  {3}  {4}  {5}  {6}", extentNumber, extentIDString, name, FormattingHelper.GetStandardSizeString(size), diskIDString, offsetString, startSector);
                 }
             }
             else
@@ -266,59 +266,5 @@ namespace ISCSIConsole
                 Console.WriteLine("No volume has been selected");
             }
         }
-
-        public static long ParseStandardSizeString(string value)
-        {
-            if (value.ToUpper().EndsWith("TB"))
-            {
-                return (long)1024 * 1024 * 1024 * 1024 * Conversion.ToInt64(value.Substring(0, value.Length - 2), -1);
-            }
-            else if (value.ToUpper().EndsWith("GB"))
-            {
-                return 1024 * 1024 * 1024 * Conversion.ToInt64(value.Substring(0, value.Length - 2), -1);
-            }
-            else if (value.ToUpper().EndsWith("MB"))
-            {
-                return 1024 * 1024 * Conversion.ToInt64(value.Substring(0, value.Length - 2), -1);
-            }
-            else if (value.ToUpper().EndsWith("KB"))
-            {
-                return 1024 * Conversion.ToInt64(value.Substring(0, value.Length - 2), -1);
-            }
-            if (value.ToUpper().EndsWith("B"))
-            {
-                return Conversion.ToInt64(value.Substring(0, value.Length - 1), -1);
-            }
-            else
-            {
-                return Conversion.ToInt64(value, -1);
-            }
-        }
-
-        public static string GetStandardSizeString(long value)
-        {
-            string[] suffixes = { " B", "KB", "MB", "GB", "TB", "PB", "EB" };
-            int suffixIndex = 0;
-            while (value > 9999)
-            {
-                value = value / 1024;
-                suffixIndex++;
-            }
-
-            if (suffixIndex < suffixes.Length)
-            {
-                string FourCharacterValue = value.ToString();
-                while (FourCharacterValue.Length < 4)
-                {
-                    FourCharacterValue = " " + FourCharacterValue;
-                }
-                return String.Format("{0} {1}", FourCharacterValue, suffixes[suffixIndex]);
-            }
-            else
-            {
-                return "Too Big";
-            }
-        }
-
     }
 }