xref: /haiku/src/kits/shared/StringForRate.cpp (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
1 /*
2  * Copyright 2012 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 <SystemCatalog.h>
11 
12 using BPrivate::gSystemCatalog;
13 
14 
15 #undef B_TRANSLATION_CONTEXT
16 #define B_TRANSLATION_CONTEXT "StringForRate"
17 
18 
19 namespace BPrivate {
20 
21 
22 const char*
23 string_for_rate(double rate, char* string, size_t stringSize, double base)
24 {
25 	double kbps = rate / base;
26 	if (kbps < 1.0) {
27 		const char* trKey = B_TRANSLATE_MARK("%.0f bps");
28 		snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
29 			B_TRANSLATION_CONTEXT), (int)rate);
30 		return string;
31 	}
32 	double mbps = kbps / base;
33 	if (mbps < 1.0) {
34 		const char* trKey = B_TRANSLATE_MARK("%.0f kbps");
35 		snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
36 			B_TRANSLATION_CONTEXT), kbps);
37 		return string;
38 	}
39 	double gbps = mbps / base;
40 	if (gbps < 1.0) {
41 		const char* trKey = B_TRANSLATE_MARK("%.0f mbps");
42 		snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
43 			B_TRANSLATION_CONTEXT), mbps);
44 		return string;
45 	}
46 	double tbps = gbps / base;
47 	if (tbps < 1.0) {
48 		const char* trKey = B_TRANSLATE_MARK("%.0f gbps");
49 		snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
50 			B_TRANSLATION_CONTEXT), gbps);
51 		return string;
52 	}
53 	const char* trKey = B_TRANSLATE_MARK("%.0f tbps");
54 	snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
55 		B_TRANSLATION_CONTEXT), tbps);
56 	return string;
57 }
58 
59 
60 }	// namespace BPrivate
61 
62