* 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
This commit is contained in:
Stephan Aßmus
2010-03-23 16:49:49 +00:00
parent 3d019c5f63
commit 48d796576e
14 changed files with 113 additions and 118 deletions
+23
View File
@@ -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
+4 -3
View File
@@ -227,8 +227,8 @@ PartitionListRow::PartitionListRow(BPartition* partition)
} }
char size[1024]; char size[1024];
SetField(new BStringField(string_for_size(partition->Size(), size)), SetField(new BStringField(string_for_size(partition->Size(), size,
kSizeColumn); sizeof(size))), kSizeColumn);
} }
@@ -250,7 +250,8 @@ PartitionListRow::PartitionListRow(partition_id parentID, partition_id id,
SetField(new BStringField(kUnavailableString), kMountedAtColumn); SetField(new BStringField(kUnavailableString), kMountedAtColumn);
char sizeString[1024]; char sizeString[1024];
SetField(new BStringField(string_for_size(size, sizeString)), kSizeColumn); SetField(new BStringField(string_for_size(size, sizeString,
sizeof(sizeString))), kSizeColumn);
} }
+6 -31
View File
@@ -21,42 +21,15 @@
#define TR_CONTEXT "Support" #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 void
dump_partition_info(const BPartition* partition) dump_partition_info(const BPartition* partition)
{ {
char size[1024]; char size[1024];
printf("\tOffset(): %Ld\n", partition->Offset()); 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(), printf("\tContentSize(): %s\n", string_for_size(partition->ContentSize(),
size)); size, sizeof(size)));
printf("\tBlockSize(): %ld\n", partition->BlockSize()); printf("\tBlockSize(): %ld\n", partition->BlockSize());
printf("\tIndex(): %ld\n", partition->Index()); printf("\tIndex(): %ld\n", partition->Index());
printf("\tStatus(): %ld\n\n", partition->Status()); printf("\tStatus(): %ld\n\n", partition->Status());
@@ -145,7 +118,9 @@ SizeSlider::~SizeSlider()
const char* const char*
SizeSlider::UpdateText() const 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); Value() - fStartOffset);
return fStatusLabel; return fStatusLabel;
+1 -2
View File
@@ -11,13 +11,12 @@
#include <HashString.h> #include <HashString.h>
#include <Slider.h> #include <Slider.h>
#include <String.h> #include <String.h>
#include "StringForSize.h"
class BPartition; class BPartition;
const char* string_for_size(off_t size, char *string);
void dump_partition_info(const BPartition* partition); void dump_partition_info(const BPartition* partition);
bool is_valid_partitionable_space(size_t size); bool is_valid_partitionable_space(size_t size);
+5 -4
View File
@@ -191,7 +191,7 @@ InstallerWindow::InstallerWindow()
fPackagesView, B_WILL_DRAW, false, true); fPackagesView, B_WILL_DRAW, false, true);
const char* requiredDiskSpaceString 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 = new BStringView("size_view", requiredDiskSpaceString);
fSizeView->SetAlignment(B_ALIGN_RIGHT); fSizeView->SetAlignment(B_ALIGN_RIGHT);
fSizeView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED)); fSizeView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED));
@@ -378,9 +378,10 @@ InstallerWindow::MessageReceived(BMessage *msg)
case PACKAGE_CHECKBOX: case PACKAGE_CHECKBOX:
{ {
char buffer[15]; char buffer[15];
fPackagesView->GetTotalSizeAsString(buffer); fPackagesView->GetTotalSizeAsString(buffer, sizeof(buffer));
char string[255]; char string[256];
sprintf(string, TR("Additional disk space required: %s"), buffer); snprintf(string, sizeof(string),
TR("Additional disk space required: %s"), buffer);
fSizeView->SetText(string); fSizeView->SetText(string);
break; break;
} }
+6 -31
View File
@@ -20,6 +20,7 @@
#include <View.h> #include <View.h>
#include "InstallerWindow.h" #include "InstallerWindow.h"
#include "StringForSize.h"
#undef TR_CONTEXT #undef TR_CONTEXT
@@ -27,32 +28,6 @@
#define ICON_ATTRIBUTE "INSTALLER PACKAGE: ICON" #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) Package::Package(const char *folder)
: :
@@ -131,9 +106,9 @@ err:
void 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); BCheckBox::Draw(update);
char string[15]; char string[15];
fPackage->GetSizeAsString(string); fPackage->GetSizeAsString(string, sizeof(string));
float width = StringWidth(string); float width = StringWidth(string);
DrawString(string, BPoint(Bounds().right - width - 8, 11)); DrawString(string, BPoint(Bounds().right - width - 8, 11));
@@ -281,7 +256,7 @@ PackagesView::AddPackages(BList& packages, BMessage* msg)
void void
PackagesView::GetTotalSizeAsString(char* string) PackagesView::GetTotalSizeAsString(char* string, size_t stringSize)
{ {
int32 count = CountChildren(); int32 count = CountChildren();
int32 size = 0; int32 size = 0;
@@ -290,7 +265,7 @@ PackagesView::GetTotalSizeAsString(char* string)
if (cb && cb->Value()) if (cb && cb->Value())
size += cb->GetPackage()->Size(); size += cb->GetPackage()->Size();
} }
SizeAsString(size, string); string_for_size(size, string, stringSize);
} }
+4 -2
View File
@@ -55,7 +55,8 @@ public:
{ return fDescription; } { return fDescription; }
const int32 Size() const const int32 Size() const
{ return fSize; } { return fSize; }
void GetSizeAsString(char* string); void GetSizeAsString(char* string,
size_t stringSize);
const BBitmap* Icon() const const BBitmap* Icon() const
{ return fIcon; } { return fIcon; }
bool OnByDefault() const bool OnByDefault() const
@@ -110,7 +111,8 @@ public:
void Clean(); void Clean();
void AddPackages(BList& list, BMessage* message); void AddPackages(BList& list, BMessage* message);
void GetTotalSizeAsString(char* string); void GetTotalSizeAsString(char* string,
size_t stringSize);
void GetPackagesToInstall(BList* list, int32* size); void GetPackagesToInstall(BList* list, int32* size);
virtual void FrameResized(float width, float height); virtual void FrameResized(float width, float height);
+2 -4
View File
@@ -29,6 +29,7 @@
#include "PackageViews.h" #include "PackageViews.h"
#include "PartitionMenuItem.h" #include "PartitionMenuItem.h"
#include "ProgressReporter.h" #include "ProgressReporter.h"
#include "StringForSize.h"
#include "UnzipEngine.h" #include "UnzipEngine.h"
@@ -48,9 +49,6 @@
const char BOOT_PATH[] = "/boot"; const char BOOT_PATH[] = "/boot";
extern void SizeAsString(off_t size, char* string);
const uint32 MSG_START_INSTALLING = 'eSRT'; const uint32 MSG_START_INSTALLING = 'eSRT';
@@ -519,7 +517,7 @@ make_partition_label(BPartition* partition, char* label, char* menuLabel,
bool showContentType) bool showContentType)
{ {
char size[15]; char size[15];
SizeAsString(partition->Size(), size); string_for_size(partition->Size(), size, sizeof(size));
BPath path; BPath path;
partition->GetPath(&path); partition->GetPath(&path);
+1
View File
@@ -20,6 +20,7 @@ StaticLibrary libshared.a :
HashString.cpp HashString.cpp
RWLockManager.cpp RWLockManager.cpp
ShakeTrackingFilter.cpp ShakeTrackingFilter.cpp
StringForSize.cpp
Variant.cpp Variant.cpp
; ;
+43
View File
@@ -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
+3 -3
View File
@@ -675,11 +675,11 @@ BInfoWindow::GetSizeString(BString &result, off_t size, int32 fileCount)
bytes = numStr; bytes = numStr;
if (size >= kGBSize) if (size >= kGBSize)
PrintFloat(result, (float)size / kGBSize) << " GB"; PrintFloat(result, (float)size / kGBSize) << " GiB";
else if (size >= kMBSize) else if (size >= kMBSize)
PrintFloat(result, (float)size / kMBSize) << " MB"; PrintFloat(result, (float)size / kMBSize) << " MiB";
else if (size >= kKBSize) else if (size >= kKBSize)
result << (int64)(size + kHalfKBSize) / kKBSize << "K"; result << (int64)(size + kHalfKBSize) / kKBSize << "KiB";
else else
result << size; result << size;
+1 -1
View File
@@ -91,7 +91,7 @@ SharedLibrary libtracker.so :
VolumeWindow.cpp VolumeWindow.cpp
WidgetAttributeText.cpp WidgetAttributeText.cpp
: be translation $(vector_icon_libs) $(TARGET_LIBSTDC++) : be translation $(vector_icon_libs) $(TARGET_LIBSTDC++) libshared.a
; ;
+10 -33
View File
@@ -51,6 +51,7 @@ All rights reserved.
#include "Bitmaps.h" #include "Bitmaps.h"
#include "Commands.h" #include "Commands.h"
#include "StatusWindow.h" #include "StatusWindow.h"
#include "StringForSize.h"
#include "DeskWindow.h" #include "DeskWindow.h"
@@ -178,7 +179,7 @@ public:
}; };
// #pragma mark - // #pragma mark - BStatusWindow
BStatusWindow::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 void
BStatusView::Draw(BRect updateRect) BStatusView::Draw(BRect updateRect)
{ {
@@ -700,18 +673,22 @@ BStatusView::Draw(BRect updateRect)
if (fBytesPerSecond != 0.0) { if (fBytesPerSecond != 0.0) {
char sizeBuffer[128]; char sizeBuffer[128];
buffer = "("; buffer = "(";
buffer << string_for_size((double)fSizeProcessed, sizeBuffer); buffer << string_for_size((double)fSizeProcessed, sizeBuffer,
sizeof(sizeBuffer));
buffer << " of "; buffer << " of ";
buffer << string_for_size((double)fTotalSize, sizeBuffer); buffer << string_for_size((double)fTotalSize, sizeBuffer,
sizeof(sizeBuffer));
buffer << ", "; buffer << ", ";
buffer << string_for_size(fBytesPerSecond, sizeBuffer); buffer << string_for_size(fBytesPerSecond, sizeBuffer,
sizeof(sizeBuffer));
buffer << "/s)"; buffer << "/s)";
tp.x = fStatusBar->Frame().right - StringWidth(buffer.String()); tp.x = fStatusBar->Frame().right - StringWidth(buffer.String());
if (tp.x > rightDivider) if (tp.x > rightDivider)
DrawString(buffer.String(), tp); DrawString(buffer.String(), tp);
else { else {
// complete string too wide, try with shorter version // complete string too wide, try with shorter version
buffer << string_for_size(fBytesPerSecond, sizeBuffer); buffer << string_for_size(fBytesPerSecond, sizeBuffer,
sizeof(sizeBuffer));
buffer << "/s"; buffer << "/s";
tp.x = fStatusBar->Frame().right tp.x = fStatusBar->Frame().right
- StringWidth(buffer.String()); - StringWidth(buffer.String());
+4 -4
View File
@@ -222,17 +222,17 @@ TruncFileSizeBase(BString *result, int64 value, const View *view, float width)
const char *suffix; const char *suffix;
float floatValue; float floatValue;
if (value >= kTBSize) { if (value >= kTBSize) {
suffix = "TB"; suffix = "TiB";
floatValue = (float)value / kTBSize; floatValue = (float)value / kTBSize;
} else if (value >= kGBSize) { } else if (value >= kGBSize) {
suffix = "GB"; suffix = "GiB";
floatValue = (float)value / kGBSize; floatValue = (float)value / kGBSize;
} else if (value >= kMBSize) { } else if (value >= kMBSize) {
suffix = "MB"; suffix = "MiB";
floatValue = (float)value / kMBSize; floatValue = (float)value / kMBSize;
} else { } else {
ASSERT(value >= kKBSize); ASSERT(value >= kKBSize);
suffix = "KB"; suffix = "KiB";
floatValue = (float)value / kKBSize; floatValue = (float)value / kKBSize;
} }