xref: /haiku/src/kits/shared/StringForRate.cpp (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 /*
2  * Copyright 2012-2018, 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 <StringFormat.h>
11 #include <SystemCatalog.h>
12 
13 using BPrivate::gSystemCatalog;
14 
15 
16 #undef B_TRANSLATION_CONTEXT
17 #define B_TRANSLATION_CONTEXT "StringForRate"
18 
19 
20 namespace BPrivate {
21 
22 
23 const char*
24 string_for_rate(double rate, char* string, size_t stringSize)
25 {
26 	double kib = rate / 1024.0;
27 	if (kib < 1.0) {
28 		BString tmp;
29 		BStringFormat format(
30 			gSystemCatalog.GetString(B_TRANSLATE_MARK(
31 			"{0, plural, one{# byte/s} other{# bytes/s}}"),
32 			B_TRANSLATION_CONTEXT, "bytes per second"));
33 		format.Format(tmp, (int)rate);
34 
35 		strlcpy(string, tmp.String(), stringSize);
36 		return string;
37 	}
38 	double mib = kib / 1024.0;
39 	if (mib < 1.0) {
40 		const char* trKey = B_TRANSLATE_MARK("%3.2f KiB/s");
41 		snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
42 			B_TRANSLATION_CONTEXT, "KiB per second"), kib);
43 		return string;
44 	}
45 	double gib = mib / 1024.0;
46 	if (gib < 1.0) {
47 		const char* trKey = B_TRANSLATE_MARK("%3.2f MiB/s");
48 		snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
49 			B_TRANSLATION_CONTEXT, "MiB per second"), mib);
50 		return string;
51 	}
52 	double tib = gib / 1024.0;
53 	if (tib < 1.0) {
54 		const char* trKey = B_TRANSLATE_MARK("%3.2f GiB/s");
55 		snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
56 			B_TRANSLATION_CONTEXT, "GiB per second"), gib);
57 		return string;
58 	}
59 	const char* trKey = B_TRANSLATE_MARK("%.2f TiB/s");
60 	snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
61 		B_TRANSLATION_CONTEXT, "TiB per second"), tib);
62 	return string;
63 }
64 
65 }	// namespace BPrivate
66 
67