From 3c3b25a8894259a606757f60dedd6b5c18cc1397 Mon Sep 17 00:00:00 2001 From: Michael Pfeiffer Date: Mon, 31 Mar 2008 18:40:20 +0000 Subject: [PATCH] Replace partition size pretty printing with a version by Ingo copied from file Partitioner.cpp. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24708 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/bootman/PartitionsPage.cpp | 42 ++++++++++++++--------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/apps/bootman/PartitionsPage.cpp b/src/apps/bootman/PartitionsPage.cpp index 8112384730..5e60f010f3 100644 --- a/src/apps/bootman/PartitionsPage.cpp +++ b/src/apps/bootman/PartitionsPage.cpp @@ -1,6 +1,10 @@ /* - * Copyright 2008, Michael Pfeiffer, laplace@users.sourceforge.net. All rights reserved. + * Copyright 2008, Haiku, Inc. All rights reserved. * Distributed under the terms of the MIT License. + * + * Authors: + * Michael Pfeiffer, laplace@users.sourceforge.net + * Ingo Weinhold */ @@ -14,6 +18,7 @@ #include #include +#include #include #include @@ -301,30 +306,23 @@ static const char kPrefix[] = " KMGTPE"; void -PartitionsPage::_CreateSizeText(int64 size, BString* text) +PartitionsPage::_CreateSizeText(int64 _size, BString* text) { - if (size < 0) { - (*text) << "Error"; - return; - } - + const char* suffixes[] = { + "", "K", "M", "G", "T", NULL + }; + + double size = _size; int index = 0; - int n = 7; - int64 remainder = 0; - for (; size >= 1000 && index <= n; index ++) { - remainder = size % 1000; - size /= 1000; + while (size > 1024 && suffixes[index + 1]) { + size /= 1024; + index++; } - - // round - size = size + ((remainder + 500) / 1000); - - if (index == 0) - (*text) << size << " B"; - else if (index < n) - (*text) << size << " " << kPrefix[index] << "B"; - else - (*text) << "-"; + + char buffer[128]; + snprintf(buffer, sizeof(buffer), "%.2f%s", size, suffixes[index]); + + *text = buffer; }