1 /* 2 * Copyright 2012-2024, Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #include "StringForRate.h" 7 8 #include <stdio.h> 9 10 #include <Catalog.h> 11 #include <NumberFormat.h> 12 #include <StringFormat.h> 13 #include <SystemCatalog.h> 14 15 16 using BPrivate::gSystemCatalog; 17 18 19 #undef B_TRANSLATION_CONTEXT 20 #define B_TRANSLATION_CONTEXT "StringForRate" 21 22 23 namespace BPrivate { 24 25 26 const char* 27 string_for_rate(double rate, char* string, size_t stringSize) 28 { 29 BString printedRate; 30 31 double value = rate / 1024.0; 32 if (value < 1.0) { 33 BStringFormat format( 34 B_TRANSLATE_MARK_ALL("{0, plural, one{# byte/s} other{# bytes/s}}", 35 B_TRANSLATION_CONTEXT, "bytes per second")); 36 37 format.Format(printedRate, (int)rate); 38 strlcpy(string, gSystemCatalog.GetString(printedRate.String(), B_TRANSLATION_CONTEXT, 39 "bytes per second"), stringSize); 40 41 return string; 42 } 43 44 const char* kFormats[] = { 45 B_TRANSLATE_MARK_ALL("%s KiB/s", B_TRANSLATION_CONTEXT, "units per second"), 46 B_TRANSLATE_MARK_ALL("%s MiB/s", B_TRANSLATION_CONTEXT, "units per second"), 47 B_TRANSLATE_MARK_ALL("%s GiB/s", B_TRANSLATION_CONTEXT, "units per second"), 48 B_TRANSLATE_MARK_ALL("%s TiB/s", B_TRANSLATION_CONTEXT, "units per second") 49 }; 50 51 size_t index = 0; 52 while (index < B_COUNT_OF(kFormats) && value >= 1024.0) { 53 value /= 1024.0; 54 index++; 55 } 56 57 BNumberFormat numberFormat; 58 numberFormat.SetPrecision(2); 59 numberFormat.Format(printedRate, value); 60 snprintf(string, stringSize, gSystemCatalog.GetString(kFormats[index], B_TRANSLATION_CONTEXT, 61 "units per second"), printedRate.String()); 62 63 return string; 64 } 65 66 67 } // namespace BPrivate 68 69