[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: how to enable telling KLocale::formatByteSize which format it
From:       Marcel Partap <mpartap () gmx ! net>
Date:       2009-02-28 19:50:56
Message-ID: 49A995A0.8070300 () gmx ! net
[Download RAW message or body]

> Make a new method, and add an enum parameter to that method.
> Then port the existing method to call that new method (but leave its
> declaration in the header file unchanged).
ok thx david actually that's what i did have in mind.. so now here's 
the first patch version, works but still needs a few lines of 
commenting.. Any better suggestions for the function name?
regards marcel ;)


-- 
  "Obstacles are those frightful things you see when you take
   your eyes off your goal."         -- Henry Ford (1863-1947)

   Change the world! Vote: http://hfopi.org/vote-future

["klocale-format-byte-size-with-specific-prefix.patch" (text/plain)]

Index: kdecore/localization/klocale.h
===================================================================
--- kdecore/localization/klocale.h	(revision 933044)
+++ kdecore/localization/klocale.h	(working copy)
@@ -405,6 +405,29 @@
    */
   QString formatLong(long num) const;
 
+  enum ByteSizePrefix {
+    Byte,      //   B          1    1 byte
+    KiloByte,  //  KB     1000^1    1000 bytes
+    KibiByte,  // KiB     1024^1    1024 bytes
+    MegaByte,  //  MB     1000^2    1 million bytes
+    MebiByte,  // MiB     1024^2    1,048,576 bytes
+    GigaByte,  //  GB     1000^3    1 billion bytes
+    GibiByte,  // GiB     1024^3    1,073,741,824 bytes
+    TeraByte,  //  TB     1000^4    1 trillion bytes
+    TebiByte,  // TiB     1024^4    1,099,511,627,776 bytes
+    PetaByte,  //  PB     1000^5    1 quadrillion bytes
+    PebiByte,  // PiB     1024^5    1,125,899,906,842,624 bytes
+    ExaByte,   //  EB     1000^6    1 quintillion bytes
+    ExbiByte,  // EiB     1024^6    1,152,921,504,606,846,976 bytes
+    ZettaByte, //  ZB     1000^7    1 sextillion bytes
+    ZebiByte,  // ZiB     1024^7    1,180,591,620,717,411,303,424 bytes
+    YottaByte, //  YB     1000^8    1 septillion bytes
+    YobiByte   // YiB     1024^8    1,208,925,819,614,629,174,706,176 bytes
+  };
+
+  QString formatByteSizeSpecificPrefix(double size, ByteSizePrefix prefix,
+                                       bool longprefix, int precision) const;
+
   /**
    * Converts @p size from bytes to the string representation using the
    * IEC 60027-2 standard
Index: kdecore/localization/klocale.cpp
===================================================================
--- kdecore/localization/klocale.cpp	(revision 933044)
+++ kdecore/localization/klocale.cpp	(working copy)
@@ -1192,49 +1192,97 @@
 // a hidden kconfig option and getting the code from #57240 into the same
 // method, so that all KDE apps use the same unit, instead of letting each app decide.
 
-QString KLocale::formatByteSize( double size ) const
+QString KLocale::formatByteSizeSpecificPrefix(double size, ByteSizePrefix prefix,
+                                              bool longprefix, int precision) const
 {
-    // Per IEC 60027-2
-
-    // Binary prefixes
-    //Tebi-byte             TiB             2^40    1,099,511,627,776 bytes
-    //Gibi-byte             GiB             2^30    1,073,741,824 bytes
-    //Mebi-byte             MiB             2^20    1,048,576 bytes
-    //Kibi-byte             KiB             2^10    1,024 bytes
-
-    QString s;
-    // Gibi-byte
-    if ( size >= 1073741824.0 )
-    {
-        size /= 1073741824.0;
-        if ( size > 1024 ) // Tebi-byte
-            s = i18n( "%1 TiB", formatNumber(size / 1024.0, 1));
-        else
-            s = i18n( "%1 GiB", formatNumber(size, 1));
+    if ( size <1 ) {
+        return i18n(longprefix ? "0 Byte" : "0 B");
     }
-    // Mebi-byte
-    else if ( size >= 1048576.0 )
+    switch ( prefix )
     {
-        size /= 1048576.0;
-        s = i18n( "%1 MiB", formatNumber(size, 1));
+        case Byte:
+            return i18n("%1 %2", formatNumber(size, precision),
+                        longprefix ? "Bytes" : "B");
+        case KiloByte:
+            return i18n("%1 %2", formatNumber(size / 1000.0, precision),
+                        longprefix ? "Kilobytes" : "KB");
+        case KibiByte:
+            return i18n("%1 %2", formatNumber(size / 1024.0, precision),
+                        longprefix ? "Kibibytes" : "KiB");
+        case MegaByte:
+            return i18n("%1 %2", formatNumber(size / 1000000.0, precision),
+                        longprefix ? "Megabytes" : "MB");
+        case MebiByte:
+            return i18n("%1 %2", formatNumber(size / 1048576.0, precision),
+                        longprefix ? "Mebibytes" : "MiB");
+        case GigaByte:
+            return i18n("%1 %2", formatNumber(size / 1000000000.0, precision),
+                        longprefix ? "Gigabytes" : "GB");
+        case GibiByte:
+            return i18n("%1 %2", formatNumber(size / 1073741824.0, precision),
+                        longprefix ? "Gibibytes" : "GiB");
+        case TeraByte:
+            return i18n("%1 %2", formatNumber(size / 1000000000000.0, precision),
+                        longprefix ? "Terabytes" : "TB");
+        case TebiByte:
+            return i18n("%1 %2", formatNumber(size / 1099511627776.0, precision),
+                        longprefix ? "Tebibytes" : "TiB");
+        case PetaByte:
+            return i18n("%1 %2", formatNumber(size / 1000000000000000.0, precision),
+                        longprefix ? "Petabytes" : "PB");
+        case PebiByte:
+            return i18n("%1 %2", formatNumber(size / 1125899906842624.0, precision),
+                        longprefix ? "Pebibytes" : "PiB");
+        case ExaByte:
+            return i18n("%1 %2", formatNumber(size / 1000000000000000000.0, precision),
+                        longprefix ? "Exabytes" : "EB");
+        case ExbiByte:
+            return i18n("%1 %2", formatNumber(size / 1152921504606846976.0, precision),
+                        longprefix ? "Exbibytes" : "EiB");
+        case ZettaByte:
+            return i18n("%1 %2", formatNumber(size / 1000000000000000000000.0, precision),
+                        longprefix ? "Zettabytes" : "ZB");
+        case ZebiByte:
+            return i18n("%1 %2", formatNumber(size / 1180591620717411303424.0, precision),
+                        longprefix ? "Zebibytes" : "ZiB");
+        case YottaByte:
+            return i18n("%1 %2", formatNumber(size / 1000000000000000000000000.0, precision),
+                        longprefix ? "Yottabytes" : "YB");
+        case YobiByte:
+            return i18n("%1 %2", formatNumber(size / 1208925819614629174706176.0, precision),
+                        longprefix ? "YobiBytes" : "YiB");
     }
-    // Kibi-byte
-    else if ( size >= 1024.0 )
-    {
-        size /= 1024.0;
-        s = i18n( "%1 KiB", formatNumber(size, 1));
+}
+
+QString KLocale::formatByteSize( double size ) const
+{
+    if (size < 1024.0) {
+        return formatByteSizeSpecificPrefix(size, Byte, false, 1);
     }
-    // Just byte
-    else if ( size > 0 )
-    {
-        s = i18n( "%1 B", formatNumber(size, 0));
+    else if (size < 1048576.0) {
+        return formatByteSizeSpecificPrefix(size, KibiByte, false, 1);
     }
-    // Nothing
-    else
-    {
-        s = i18n( "0 B" );
+    else if (size < 1073741824.0) {
+        return formatByteSizeSpecificPrefix(size, MebiByte, false, 1);
     }
-    return s;
+    else if (size < 1099511627776.0) {
+        return formatByteSizeSpecificPrefix(size, GibiByte, false, 1);
+    }
+    else if (size < 1125899906842624.0) {
+        return formatByteSizeSpecificPrefix(size, TebiByte, false, 1);
+    }
+    else if (size < 1152921504606846976.0) {
+        return formatByteSizeSpecificPrefix(size, PebiByte, false, 1);
+    }
+    else if (size < 1180591620717411303424.0) {
+        return formatByteSizeSpecificPrefix(size, ExbiByte, false, 1);
+    }
+    else if (size < 1208925819614629174706176.0) {
+        return formatByteSizeSpecificPrefix(size, ZebiByte, false, 1);
+    }
+    else {
+        return formatByteSizeSpecificPrefix(size, YobiByte, false, 1);
+    }
 }
 
 QString KLocale::formatDuration( unsigned long mSec) const


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic