From 48d796576e9b4b9610f78f4b96131eb28a362f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Tue, 23 Mar 2010 16:49:49 +0000 Subject: [PATCH] * 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. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35935 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/shared/StringForSize.h | 23 +++++++++++++ src/apps/drivesetup/PartitionList.cpp | 7 ++-- src/apps/drivesetup/Support.cpp | 37 ++++---------------- src/apps/drivesetup/Support.h | 3 +- src/apps/installer/InstallerWindow.cpp | 9 ++--- src/apps/installer/PackageViews.cpp | 37 ++++---------------- src/apps/installer/PackageViews.h | 6 ++-- src/apps/installer/WorkerThread.cpp | 6 ++-- src/kits/shared/Jamfile | 1 + src/kits/shared/StringForSize.cpp | 43 ++++++++++++++++++++++++ src/kits/tracker/InfoWindow.cpp | 6 ++-- src/kits/tracker/Jamfile | 2 +- src/kits/tracker/StatusWindow.cpp | 43 ++++++------------------ src/kits/tracker/WidgetAttributeText.cpp | 8 ++--- 14 files changed, 113 insertions(+), 118 deletions(-) create mode 100644 headers/private/shared/StringForSize.h create mode 100644 src/kits/shared/StringForSize.cpp diff --git a/headers/private/shared/StringForSize.h b/headers/private/shared/StringForSize.h new file mode 100644 index 0000000000..fa3e13b115 --- /dev/null +++ b/headers/private/shared/StringForSize.h @@ -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 + + +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 diff --git a/src/apps/drivesetup/PartitionList.cpp b/src/apps/drivesetup/PartitionList.cpp index 59d17eda41..64ede2804f 100644 --- a/src/apps/drivesetup/PartitionList.cpp +++ b/src/apps/drivesetup/PartitionList.cpp @@ -227,8 +227,8 @@ PartitionListRow::PartitionListRow(BPartition* partition) } 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 @@ PartitionListRow::PartitionListRow(partition_id parentID, partition_id id, 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); } diff --git a/src/apps/drivesetup/Support.cpp b/src/apps/drivesetup/Support.cpp index 51ae8573e0..fad363099b 100644 --- a/src/apps/drivesetup/Support.cpp +++ b/src/apps/drivesetup/Support.cpp @@ -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 @@ SizeSlider::~SizeSlider() 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; diff --git a/src/apps/drivesetup/Support.h b/src/apps/drivesetup/Support.h index 6e64a19aac..736ce4c011 100644 --- a/src/apps/drivesetup/Support.h +++ b/src/apps/drivesetup/Support.h @@ -11,13 +11,12 @@ #include #include #include +#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); diff --git a/src/apps/installer/InstallerWindow.cpp b/src/apps/installer/InstallerWindow.cpp index b0308b0303..0a0b21ed98 100644 --- a/src/apps/installer/InstallerWindow.cpp +++ b/src/apps/installer/InstallerWindow.cpp @@ -191,7 +191,7 @@ InstallerWindow::InstallerWindow() 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 @@ InstallerWindow::MessageReceived(BMessage *msg) 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; } diff --git a/src/apps/installer/PackageViews.cpp b/src/apps/installer/PackageViews.cpp index 4a1e687adf..1888758da1 100644 --- a/src/apps/installer/PackageViews.cpp +++ b/src/apps/installer/PackageViews.cpp @@ -20,6 +20,7 @@ #include #include "InstallerWindow.h" +#include "StringForSize.h" #undef TR_CONTEXT @@ -27,32 +28,6 @@ #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) : @@ -131,9 +106,9 @@ err: 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 @@ PackageCheckBox::Draw(BRect update) { 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 @@ PackagesView::AddPackages(BList& packages, BMessage* msg) void -PackagesView::GetTotalSizeAsString(char* string) +PackagesView::GetTotalSizeAsString(char* string, size_t stringSize) { int32 count = CountChildren(); int32 size = 0; @@ -290,7 +265,7 @@ PackagesView::GetTotalSizeAsString(char* string) if (cb && cb->Value()) size += cb->GetPackage()->Size(); } - SizeAsString(size, string); + string_for_size(size, string, stringSize); } diff --git a/src/apps/installer/PackageViews.h b/src/apps/installer/PackageViews.h index 8cc5cfdfa0..d676b6b91d 100644 --- a/src/apps/installer/PackageViews.h +++ b/src/apps/installer/PackageViews.h @@ -55,7 +55,8 @@ public: { 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 @@ public: 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); diff --git a/src/apps/installer/WorkerThread.cpp b/src/apps/installer/WorkerThread.cpp index 908277d622..f944bf5895 100644 --- a/src/apps/installer/WorkerThread.cpp +++ b/src/apps/installer/WorkerThread.cpp @@ -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 @@ make_partition_label(BPartition* partition, char* label, char* menuLabel, bool showContentType) { char size[15]; - SizeAsString(partition->Size(), size); + string_for_size(partition->Size(), size, sizeof(size)); BPath path; partition->GetPath(&path); diff --git a/src/kits/shared/Jamfile b/src/kits/shared/Jamfile index 950414f800..001ee6f446 100644 --- a/src/kits/shared/Jamfile +++ b/src/kits/shared/Jamfile @@ -20,6 +20,7 @@ StaticLibrary libshared.a : HashString.cpp RWLockManager.cpp ShakeTrackingFilter.cpp + StringForSize.cpp Variant.cpp ; diff --git a/src/kits/shared/StringForSize.cpp b/src/kits/shared/StringForSize.cpp new file mode 100644 index 0000000000..ae7831e397 --- /dev/null +++ b/src/kits/shared/StringForSize.cpp @@ -0,0 +1,43 @@ +/* + * Copyright 2010 Haiku Inc. All rights reserved. + * Distributed under the terms of the MIT License. + */ + +#include "StringForSize.h" + +#include + + +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 + diff --git a/src/kits/tracker/InfoWindow.cpp b/src/kits/tracker/InfoWindow.cpp index 79ba39bb19..6400cbd599 100644 --- a/src/kits/tracker/InfoWindow.cpp +++ b/src/kits/tracker/InfoWindow.cpp @@ -675,11 +675,11 @@ BInfoWindow::GetSizeString(BString &result, off_t size, int32 fileCount) 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; diff --git a/src/kits/tracker/Jamfile b/src/kits/tracker/Jamfile index c23846746a..2502779fd3 100644 --- a/src/kits/tracker/Jamfile +++ b/src/kits/tracker/Jamfile @@ -91,7 +91,7 @@ SharedLibrary libtracker.so : VolumeWindow.cpp WidgetAttributeText.cpp - : be translation $(vector_icon_libs) $(TARGET_LIBSTDC++) + : be translation $(vector_icon_libs) $(TARGET_LIBSTDC++) libshared.a ; diff --git a/src/kits/tracker/StatusWindow.cpp b/src/kits/tracker/StatusWindow.cpp index f1245b47ea..fe1ba74d83 100644 --- a/src/kits/tracker/StatusWindow.cpp +++ b/src/kits/tracker/StatusWindow.cpp @@ -51,6 +51,7 @@ All rights reserved. #include "Bitmaps.h" #include "Commands.h" #include "StatusWindow.h" +#include "StringForSize.h" #include "DeskWindow.h" @@ -178,7 +179,7 @@ public: }; -// #pragma mark - +// #pragma mark - BStatusWindow BStatusWindow::BStatusWindow() @@ -610,34 +611,6 @@ BStatusView::InitStatus(int32 totalItems, off_t totalSize, } -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 @@ BStatusView::Draw(BRect updateRect) 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()); diff --git a/src/kits/tracker/WidgetAttributeText.cpp b/src/kits/tracker/WidgetAttributeText.cpp index 663d37c973..a579fa9a3f 100644 --- a/src/kits/tracker/WidgetAttributeText.cpp +++ b/src/kits/tracker/WidgetAttributeText.cpp @@ -222,17 +222,17 @@ TruncFileSizeBase(BString *result, int64 value, const View *view, float width) 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; }