2010-03-23 19:49:49 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Haiku Inc. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StringForSize.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-11-24 12:02:06 +03:00
|
|
|
#include <MessageFormat.h>
|
2012-04-05 00:23:14 +04:00
|
|
|
#include <SystemCatalog.h>
|
|
|
|
|
|
|
|
using BPrivate::gSystemCatalog;
|
2010-04-12 03:00:25 +04:00
|
|
|
|
2010-03-23 19:49:49 +03:00
|
|
|
|
2012-04-16 23:31:22 +04:00
|
|
|
#undef B_TRANSLATION_CONTEXT
|
|
|
|
#define B_TRANSLATION_CONTEXT "StringForSize"
|
2010-07-26 02:35:58 +04:00
|
|
|
|
|
|
|
|
2010-03-23 19:49:49 +03:00
|
|
|
namespace BPrivate {
|
|
|
|
|
|
|
|
|
|
|
|
const char*
|
|
|
|
string_for_size(double size, char* string, size_t stringSize)
|
|
|
|
{
|
|
|
|
double kib = size / 1024.0;
|
|
|
|
if (kib < 1.0) {
|
2014-11-24 12:02:06 +03:00
|
|
|
const char* trKey = B_TRANSLATE_MARK(
|
2014-12-31 00:32:51 +03:00
|
|
|
"{0, plural, one{# byte} other{# bytes}}");
|
2014-11-24 12:02:06 +03:00
|
|
|
|
|
|
|
BString tmp;
|
|
|
|
BMessageFormat format(
|
|
|
|
gSystemCatalog.GetString(trKey, B_TRANSLATION_CONTEXT));
|
|
|
|
format.Format(tmp, (int)size);
|
|
|
|
|
|
|
|
strlcpy(string, tmp.String(), stringSize);
|
2010-03-23 19:49:49 +03:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
double mib = kib / 1024.0;
|
|
|
|
if (mib < 1.0) {
|
2010-07-26 02:35:58 +04:00
|
|
|
const char* trKey = B_TRANSLATE_MARK("%3.2f KiB");
|
2012-04-14 19:58:58 +04:00
|
|
|
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
|
2012-04-16 23:31:22 +04:00
|
|
|
B_TRANSLATION_CONTEXT), kib);
|
2010-03-23 19:49:49 +03:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
double gib = mib / 1024.0;
|
|
|
|
if (gib < 1.0) {
|
2010-07-26 02:35:58 +04:00
|
|
|
const char* trKey = B_TRANSLATE_MARK("%3.2f MiB");
|
2012-04-14 19:58:58 +04:00
|
|
|
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
|
2012-04-16 23:31:22 +04:00
|
|
|
B_TRANSLATION_CONTEXT), mib);
|
2010-03-23 19:49:49 +03:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
double tib = gib / 1024.0;
|
|
|
|
if (tib < 1.0) {
|
2010-07-26 02:35:58 +04:00
|
|
|
const char* trKey = B_TRANSLATE_MARK("%3.2f GiB");
|
2012-04-14 19:58:58 +04:00
|
|
|
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
|
2012-04-16 23:31:22 +04:00
|
|
|
B_TRANSLATION_CONTEXT), gib);
|
2010-03-23 19:49:49 +03:00
|
|
|
return string;
|
|
|
|
}
|
2010-07-26 02:35:58 +04:00
|
|
|
const char* trKey = B_TRANSLATE_MARK("%.2f TiB");
|
2012-04-14 19:58:58 +04:00
|
|
|
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
|
2012-04-16 23:31:22 +04:00
|
|
|
B_TRANSLATION_CONTEXT), tib);
|
2010-03-23 19:49:49 +03:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace BPrivate
|
|
|
|
|