1 /* 2 * Copyright 2010 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #include "StringForSize.h" 7 8 #include <stdio.h> 9 10 #include <MessageFormat.h> 11 #include <SystemCatalog.h> 12 13 using BPrivate::gSystemCatalog; 14 15 16 #undef B_TRANSLATION_CONTEXT 17 #define B_TRANSLATION_CONTEXT "StringForSize" 18 19 20 namespace BPrivate { 21 22 23 const char* 24 string_for_size(double size, char* string, size_t stringSize) 25 { 26 double kib = size / 1024.0; 27 if (kib < 1.0) { 28 const char* trKey = B_TRANSLATE_MARK( 29 "{0, plural, one{# byte} other{# bytes}}"); 30 31 BString tmp; 32 BMessageFormat format( 33 gSystemCatalog.GetString(trKey, B_TRANSLATION_CONTEXT)); 34 format.Format(tmp, (int)size); 35 36 strlcpy(string, tmp.String(), stringSize); 37 return string; 38 } 39 double mib = kib / 1024.0; 40 if (mib < 1.0) { 41 const char* trKey = B_TRANSLATE_MARK("%3.2f KiB"); 42 snprintf(string, stringSize, gSystemCatalog.GetString(trKey, 43 B_TRANSLATION_CONTEXT), kib); 44 return string; 45 } 46 double gib = mib / 1024.0; 47 if (gib < 1.0) { 48 const char* trKey = B_TRANSLATE_MARK("%3.2f MiB"); 49 snprintf(string, stringSize, gSystemCatalog.GetString(trKey, 50 B_TRANSLATION_CONTEXT), mib); 51 return string; 52 } 53 double tib = gib / 1024.0; 54 if (tib < 1.0) { 55 const char* trKey = B_TRANSLATE_MARK("%3.2f GiB"); 56 snprintf(string, stringSize, gSystemCatalog.GetString(trKey, 57 B_TRANSLATION_CONTEXT), gib); 58 return string; 59 } 60 const char* trKey = B_TRANSLATE_MARK("%.2f TiB"); 61 snprintf(string, stringSize, gSystemCatalog.GetString(trKey, 62 B_TRANSLATION_CONTEXT), tib); 63 return string; 64 } 65 66 67 } // namespace BPrivate 68 69