[haiku-commits] r35935 - in haiku/trunk: headers/private/shared src/apps/drivesetup src/apps/installer src/kits/shared src/kits/tracker

  • From: superstippi@xxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 23 Mar 2010 17:49:49 +0100 (CET)

Author: stippi
Date: 2010-03-23 17:49:49 +0100 (Tue, 23 Mar 2010)
New Revision: 35935
Changeset: http://dev.haiku-os.org/changeset/35935/haiku
Ticket: http://dev.haiku-os.org/ticket/5378

Added:
   haiku/trunk/headers/private/shared/StringForSize.h
   haiku/trunk/src/kits/shared/StringForSize.cpp
Modified:
   haiku/trunk/src/apps/drivesetup/PartitionList.cpp
   haiku/trunk/src/apps/drivesetup/Support.cpp
   haiku/trunk/src/apps/drivesetup/Support.h
   haiku/trunk/src/apps/installer/InstallerWindow.cpp
   haiku/trunk/src/apps/installer/PackageViews.cpp
   haiku/trunk/src/apps/installer/PackageViews.h
   haiku/trunk/src/apps/installer/WorkerThread.cpp
   haiku/trunk/src/kits/shared/Jamfile
   haiku/trunk/src/kits/tracker/InfoWindow.cpp
   haiku/trunk/src/kits/tracker/Jamfile
   haiku/trunk/src/kits/tracker/StatusWindow.cpp
   haiku/trunk/src/kits/tracker/WidgetAttributeText.cpp
Log:
 * Moved the duplicate "string_for_size()" implementations into libshared.a.
 * Adapted libtracker.so, DriveSetup and Installer to use the shared version.
 * The new version uses the correct units (KiB instead of KB and so on).
 * Use the correct units in a few other prominent places, where
   string_for_size() could not be used.

Should resolve a major part of #5378.


Added: haiku/trunk/headers/private/shared/StringForSize.h
===================================================================
--- haiku/trunk/headers/private/shared/StringForSize.h                          
(rev 0)
+++ haiku/trunk/headers/private/shared/StringForSize.h  2010-03-23 16:49:49 UTC 
(rev 35935)
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2010 Haiku Inc. All rights reserved.
+ * Distributed under the terms of the MIT License.
+ */
+#ifndef STRING_FOR_SIZE_H
+#define STRING_FOR_SIZE_H
+
+#include <SupportDefs.h>
+
+
+namespace BPrivate {
+
+
+const char* string_for_size(double size, char* string, size_t stringSize);
+
+
+}      // namespace BPrivate
+
+
+using BPrivate::string_for_size;
+
+
+#endif // COLOR_QUANTIZER_H

Modified: haiku/trunk/src/apps/drivesetup/PartitionList.cpp
===================================================================
--- haiku/trunk/src/apps/drivesetup/PartitionList.cpp   2010-03-22 23:25:29 UTC 
(rev 35934)
+++ haiku/trunk/src/apps/drivesetup/PartitionList.cpp   2010-03-23 16:49:49 UTC 
(rev 35935)
@@ -227,8 +227,8 @@
        }
 
        char size[1024];
-       SetField(new BStringField(string_for_size(partition->Size(), size)),
-               kSizeColumn);
+       SetField(new BStringField(string_for_size(partition->Size(), size,
+               sizeof(size))), kSizeColumn);
 }
 
 
@@ -250,7 +250,8 @@
        SetField(new BStringField(kUnavailableString), kMountedAtColumn);
 
        char sizeString[1024];
-       SetField(new BStringField(string_for_size(size, sizeString)), 
kSizeColumn);
+       SetField(new BStringField(string_for_size(size, sizeString,
+               sizeof(sizeString))), kSizeColumn);
 }
 
 

Modified: haiku/trunk/src/apps/drivesetup/Support.cpp
===================================================================
--- haiku/trunk/src/apps/drivesetup/Support.cpp 2010-03-22 23:25:29 UTC (rev 
35934)
+++ haiku/trunk/src/apps/drivesetup/Support.cpp 2010-03-23 16:49:49 UTC (rev 
35935)
@@ -21,42 +21,15 @@
 #define TR_CONTEXT "Support"
 
 
-const char*
-string_for_size(off_t size, char *string)
-{
-       double kb = size / 1024.0;
-       if (kb < 1.0) {
-               sprintf(string, TR("%Ld B"), size);
-               return string;
-       }
-       float mb = kb / 1024.0;
-       if (mb < 1.0) {
-               sprintf(string, TR("%3.1f KB"), kb);
-               return string;
-       }
-       float gb = mb / 1024.0;
-       if (gb < 1.0) {
-               sprintf(string, TR("%3.1f MB"), mb);
-               return string;
-       }
-       float tb = gb / 1024.0;
-       if (tb < 1.0) {
-               sprintf(string, TR("%3.1f GB"), gb);
-               return string;
-       }
-       sprintf(string, TR("%.1f TB"), tb);
-       return string;
-}
-
-
 void
 dump_partition_info(const BPartition* partition)
 {
        char size[1024];
        printf("\tOffset(): %Ld\n", partition->Offset());
-       printf("\tSize(): %s\n", string_for_size(partition->Size(),size));
+       printf("\tSize(): %s\n", string_for_size(partition->Size(), size,
+               sizeof(size)));
        printf("\tContentSize(): %s\n", 
string_for_size(partition->ContentSize(),
-               size));
+               size, sizeof(size)));
        printf("\tBlockSize(): %ld\n", partition->BlockSize());
        printf("\tIndex(): %ld\n", partition->Index());
        printf("\tStatus(): %ld\n\n", partition->Status());
@@ -145,7 +118,9 @@
 const char*
 SizeSlider::UpdateText() const
 {
-       snprintf(fStatusLabel, sizeof(fStatusLabel), TR("%ld MB"),
+       // TODO: Perhaps replace with string_for_size, but it looks like
+       // Value() and fStartOffset are always in MiB.
+       snprintf(fStatusLabel, sizeof(fStatusLabel), TR("%ld MiB"),
                Value() - fStartOffset);
 
        return fStatusLabel;

Modified: haiku/trunk/src/apps/drivesetup/Support.h
===================================================================
--- haiku/trunk/src/apps/drivesetup/Support.h   2010-03-22 23:25:29 UTC (rev 
35934)
+++ haiku/trunk/src/apps/drivesetup/Support.h   2010-03-23 16:49:49 UTC (rev 
35935)
@@ -11,13 +11,12 @@
 #include <HashString.h>
 #include <Slider.h>
 #include <String.h>
+#include "StringForSize.h"
 
 
 class BPartition;
 
 
-const char* string_for_size(off_t size, char *string);
-
 void dump_partition_info(const BPartition* partition);
 
 bool is_valid_partitionable_space(size_t size);

Modified: haiku/trunk/src/apps/installer/InstallerWindow.cpp
===================================================================
--- haiku/trunk/src/apps/installer/InstallerWindow.cpp  2010-03-22 23:25:29 UTC 
(rev 35934)
+++ haiku/trunk/src/apps/installer/InstallerWindow.cpp  2010-03-23 16:49:49 UTC 
(rev 35935)
@@ -191,7 +191,7 @@
                fPackagesView, B_WILL_DRAW, false, true);
 
        const char* requiredDiskSpaceString
-               = TR("Additional disk space required: 0.0 KB");
+               = TR("Additional disk space required: 0.0 KiB");
        fSizeView = new BStringView("size_view", requiredDiskSpaceString);
        fSizeView->SetAlignment(B_ALIGN_RIGHT);
        fSizeView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 
B_SIZE_UNLIMITED));
@@ -378,9 +378,10 @@
                case PACKAGE_CHECKBOX:
                {
                        char buffer[15];
-                       fPackagesView->GetTotalSizeAsString(buffer);
-                       char string[255];
-                       sprintf(string, TR("Additional disk space required: 
%s"), buffer);
+                       fPackagesView->GetTotalSizeAsString(buffer, 
sizeof(buffer));
+                       char string[256];
+                       snprintf(string, sizeof(string),
+                               TR("Additional disk space required: %s"), 
buffer);
                        fSizeView->SetText(string);
                        break;
                }

Modified: haiku/trunk/src/apps/installer/PackageViews.cpp
===================================================================
--- haiku/trunk/src/apps/installer/PackageViews.cpp     2010-03-22 23:25:29 UTC 
(rev 35934)
+++ haiku/trunk/src/apps/installer/PackageViews.cpp     2010-03-23 16:49:49 UTC 
(rev 35935)
@@ -20,6 +20,7 @@
 #include <View.h>
 
 #include "InstallerWindow.h"
+#include "StringForSize.h"
 
 
 #undef TR_CONTEXT
@@ -27,33 +28,7 @@
 
 #define ICON_ATTRIBUTE "INSTALLER PACKAGE: ICON"
 
-void
-SizeAsString(off_t size, char *string)
-{
-       double kb = size / 1024.0;
-       if (kb < 1.0) {
-               sprintf(string, TR("%Ld B"), size);
-               return;
-       }
-       float mb = kb / 1024.0;
-       if (mb < 1.0) {
-               sprintf(string, TR("%3.1f KB"), kb);
-               return;
-       }
-       float gb = mb / 1024.0;
-       if (gb < 1.0) {
-               sprintf(string, TR("%3.1f MB"), mb);
-               return;
-       }
-       float tb = gb / 1024.0;
-       if (tb < 1.0) {
-               sprintf(string, TR("%3.1f GB"), gb);
-               return;
-       }
-       sprintf(string, TR("%.1f TB"), tb);
-}
 
-
 Package::Package(const char *folder)
        :
        Group(),
@@ -131,9 +106,9 @@
 
 
 void
-Package::GetSizeAsString(char *string)
+Package::GetSizeAsString(char* string, size_t stringSize)
 {
-       SizeAsString(fSize, string);
+       string_for_size(fSize, string, stringSize);
 }
 
 
@@ -166,7 +141,7 @@
 {
        BCheckBox::Draw(update);
        char string[15];
-       fPackage->GetSizeAsString(string);
+       fPackage->GetSizeAsString(string, sizeof(string));
        float width = StringWidth(string);
        DrawString(string, BPoint(Bounds().right - width - 8, 11));
 
@@ -281,7 +256,7 @@
 
 
 void
-PackagesView::GetTotalSizeAsString(char* string)
+PackagesView::GetTotalSizeAsString(char* string, size_t stringSize)
 {
        int32 count = CountChildren();
        int32 size = 0;
@@ -290,7 +265,7 @@
                if (cb && cb->Value())
                        size += cb->GetPackage()->Size();
        }
-       SizeAsString(size, string);
+       string_for_size(size, string, stringSize);
 }
 
 

Modified: haiku/trunk/src/apps/installer/PackageViews.h
===================================================================
--- haiku/trunk/src/apps/installer/PackageViews.h       2010-03-22 23:25:29 UTC 
(rev 35934)
+++ haiku/trunk/src/apps/installer/PackageViews.h       2010-03-23 16:49:49 UTC 
(rev 35935)
@@ -55,7 +55,8 @@
                                                                        { 
return fDescription; }
                        const int32                     Size() const
                                                                        { 
return fSize; }
-                       void                            GetSizeAsString(char* 
string);
+                       void                            GetSizeAsString(char* 
string,
+                                                                       size_t 
stringSize);
                        const BBitmap*          Icon() const
                                                                        { 
return fIcon; }
                        bool                            OnByDefault() const
@@ -110,7 +111,8 @@
 
                        void                            Clean();
                        void                            AddPackages(BList& 
list, BMessage* message);
-                       void                            
GetTotalSizeAsString(char* string);
+                       void                            
GetTotalSizeAsString(char* string,
+                                                                       size_t 
stringSize);
                        void                            
GetPackagesToInstall(BList* list, int32* size);
 
        virtual void                            FrameResized(float width, float 
height);

Modified: haiku/trunk/src/apps/installer/WorkerThread.cpp
===================================================================
--- haiku/trunk/src/apps/installer/WorkerThread.cpp     2010-03-22 23:25:29 UTC 
(rev 35934)
+++ haiku/trunk/src/apps/installer/WorkerThread.cpp     2010-03-23 16:49:49 UTC 
(rev 35935)
@@ -29,6 +29,7 @@
 #include "PackageViews.h"
 #include "PartitionMenuItem.h"
 #include "ProgressReporter.h"
+#include "StringForSize.h"
 #include "UnzipEngine.h"
 
 
@@ -48,9 +49,6 @@
 
 const char BOOT_PATH[] = "/boot";
 
-extern void SizeAsString(off_t size, char* string);
-
-
 const uint32 MSG_START_INSTALLING = 'eSRT';
 
 
@@ -519,7 +517,7 @@
        bool showContentType)
 {
        char size[15];
-       SizeAsString(partition->Size(), size);
+       string_for_size(partition->Size(), size, sizeof(size));
 
        BPath path;
        partition->GetPath(&path);

Modified: haiku/trunk/src/kits/shared/Jamfile
===================================================================
--- haiku/trunk/src/kits/shared/Jamfile 2010-03-22 23:25:29 UTC (rev 35934)
+++ haiku/trunk/src/kits/shared/Jamfile 2010-03-23 16:49:49 UTC (rev 35935)
@@ -20,6 +20,7 @@
        HashString.cpp
        RWLockManager.cpp
        ShakeTrackingFilter.cpp
+       StringForSize.cpp
        Variant.cpp
 ;
 

Added: haiku/trunk/src/kits/shared/StringForSize.cpp
===================================================================
--- haiku/trunk/src/kits/shared/StringForSize.cpp                               
(rev 0)
+++ haiku/trunk/src/kits/shared/StringForSize.cpp       2010-03-23 16:49:49 UTC 
(rev 35935)
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2010 Haiku Inc. All rights reserved.
+ * Distributed under the terms of the MIT License.
+ */
+
+#include "StringForSize.h"
+
+#include <stdio.h>
+
+
+namespace BPrivate {
+
+
+const char*
+string_for_size(double size, char* string, size_t stringSize)
+{
+       double kib = size / 1024.0;
+       if (kib < 1.0) {
+               snprintf(string, stringSize, "%d bytes", (int)size);
+               return string;
+       }
+       double mib = kib / 1024.0;
+       if (mib < 1.0) {
+               snprintf(string, stringSize, "%3.2f KiB", kib);
+               return string;
+       }
+       double gib = mib / 1024.0;
+       if (gib < 1.0) {
+               snprintf(string, stringSize, "%3.2f MiB", mib);
+               return string;
+       }
+       double tib = gib / 1024.0;
+       if (tib < 1.0) {
+               snprintf(string, stringSize, "%3.2f GiB", gib);
+               return string;
+       }
+       snprintf(string, stringSize, "%.2f TiB", tib);
+       return string;
+}
+
+
+}      // namespace BPrivate
+

Modified: haiku/trunk/src/kits/tracker/InfoWindow.cpp
===================================================================
--- haiku/trunk/src/kits/tracker/InfoWindow.cpp 2010-03-22 23:25:29 UTC (rev 
35934)
+++ haiku/trunk/src/kits/tracker/InfoWindow.cpp 2010-03-23 16:49:49 UTC (rev 
35935)
@@ -675,11 +675,11 @@
                bytes = numStr;
 
        if (size >= kGBSize)
-               PrintFloat(result, (float)size / kGBSize) << " GB";
+               PrintFloat(result, (float)size / kGBSize) << " GiB";
        else if (size >= kMBSize)
-               PrintFloat(result, (float)size / kMBSize) << " MB";
+               PrintFloat(result, (float)size / kMBSize) << " MiB";
        else if (size >= kKBSize)
-               result << (int64)(size + kHalfKBSize) / kKBSize << "K";
+               result << (int64)(size + kHalfKBSize) / kKBSize << "KiB";
        else
                result << size;
 

Modified: haiku/trunk/src/kits/tracker/Jamfile
===================================================================
--- haiku/trunk/src/kits/tracker/Jamfile        2010-03-22 23:25:29 UTC (rev 
35934)
+++ haiku/trunk/src/kits/tracker/Jamfile        2010-03-23 16:49:49 UTC (rev 
35935)
@@ -91,7 +91,7 @@
        VolumeWindow.cpp
        WidgetAttributeText.cpp
 
-       : be translation $(vector_icon_libs) $(TARGET_LIBSTDC++)
+       : be translation $(vector_icon_libs) $(TARGET_LIBSTDC++) libshared.a
 
        ;
 

Modified: haiku/trunk/src/kits/tracker/StatusWindow.cpp
===================================================================
--- haiku/trunk/src/kits/tracker/StatusWindow.cpp       2010-03-22 23:25:29 UTC 
(rev 35934)
+++ haiku/trunk/src/kits/tracker/StatusWindow.cpp       2010-03-23 16:49:49 UTC 
(rev 35935)
@@ -51,6 +51,7 @@
 #include "Bitmaps.h"
 #include "Commands.h"
 #include "StatusWindow.h"
+#include "StringForSize.h"
 #include "DeskWindow.h"
 
 
@@ -178,7 +179,7 @@
 };
 
 
-// #pragma mark -
+// #pragma mark - BStatusWindow
 
 
 BStatusWindow::BStatusWindow()
@@ -610,34 +611,6 @@
 }
 
 
-static const char*
-string_for_size(double size, char *string)
-{
-       double kb = size / 1024.0;
-       if (kb < 1.0) {
-               sprintf(string, "%d B", (int)size);
-               return string;
-       }
-       float mb = kb / 1024.0;
-       if (mb < 1.0) {
-               sprintf(string, "%3.1f KB", kb);
-               return string;
-       }
-       float gb = mb / 1024.0;
-       if (gb < 1.0) {
-               sprintf(string, "%3.1f MB", mb);
-               return string;
-       }
-       float tb = gb / 1024.0;
-       if (tb < 1.0) {
-               sprintf(string, "%3.1f GB", gb);
-               return string;
-       }
-       sprintf(string, "%.1f TB", tb);
-       return string;
-}
-
-
 void
 BStatusView::Draw(BRect updateRect)
 {
@@ -700,18 +673,22 @@
                        if (fBytesPerSecond != 0.0) {
                                char sizeBuffer[128];
                                buffer = "(";
-                               buffer << 
string_for_size((double)fSizeProcessed, sizeBuffer);
+                               buffer << 
string_for_size((double)fSizeProcessed, sizeBuffer,
+                                       sizeof(sizeBuffer));
                                buffer << " of ";
-                               buffer << string_for_size((double)fTotalSize, 
sizeBuffer);
+                               buffer << string_for_size((double)fTotalSize, 
sizeBuffer,
+                                       sizeof(sizeBuffer));
                                buffer << ", ";
-                               buffer << string_for_size(fBytesPerSecond, 
sizeBuffer);
+                               buffer << string_for_size(fBytesPerSecond, 
sizeBuffer,
+                                       sizeof(sizeBuffer));
                                buffer << "/s)";
                                tp.x = fStatusBar->Frame().right - 
StringWidth(buffer.String());
                                if (tp.x > rightDivider)
                                        DrawString(buffer.String(), tp);
                                else {
                                        // complete string too wide, try with 
shorter version
-                                       buffer << 
string_for_size(fBytesPerSecond, sizeBuffer);
+                                       buffer << 
string_for_size(fBytesPerSecond, sizeBuffer,
+                                               sizeof(sizeBuffer));
                                        buffer << "/s";
                                        tp.x = fStatusBar->Frame().right
                                                - StringWidth(buffer.String());

Modified: haiku/trunk/src/kits/tracker/WidgetAttributeText.cpp
===================================================================
--- haiku/trunk/src/kits/tracker/WidgetAttributeText.cpp        2010-03-22 
23:25:29 UTC (rev 35934)
+++ haiku/trunk/src/kits/tracker/WidgetAttributeText.cpp        2010-03-23 
16:49:49 UTC (rev 35935)
@@ -222,17 +222,17 @@
                const char *suffix;
                float floatValue;
                if (value >= kTBSize) {
-                       suffix = "TB";
+                       suffix = "TiB";
                        floatValue = (float)value / kTBSize;
                } else if (value >= kGBSize) {
-                       suffix = "GB";
+                       suffix = "GiB";
                        floatValue = (float)value / kGBSize;
                } else if (value >= kMBSize) {
-                       suffix = "MB";
+                       suffix = "MiB";
                        floatValue = (float)value / kMBSize;
                } else {
                        ASSERT(value >= kKBSize);
-                       suffix = "KB";
+                       suffix = "KiB";
                        floatValue = (float)value / kKBSize;
                }
 


Other related posts: