* 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

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

View File

@ -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);
}

View File

@ -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;

View File

@ -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);

View File

@ -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;
}

View File

@ -20,6 +20,7 @@
#include <View.h>
#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);
}

View File

@ -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);

View File

@ -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);

View File

@ -20,6 +20,7 @@ StaticLibrary libshared.a :
HashString.cpp
RWLockManager.cpp
ShakeTrackingFilter.cpp
StringForSize.cpp
Variant.cpp
;

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

View File

@ -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;

View File

@ -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
;

View File

@ -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());

View File

@ -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;
}