xref: /haiku/src/kits/shared/StringForSize.cpp (revision fce97ba360ff70fb19f9cd2a57a16152f0925c06)
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 <SystemCatalog.h>
11 
12 using BPrivate::gSystemCatalog;
13 
14 
15 #undef B_TRANSLATION_CONTEXT
16 #define B_TRANSLATION_CONTEXT "StringForSize"
17 
18 
19 namespace BPrivate {
20 
21 
22 const char*
23 string_for_size(double size, char* string, size_t stringSize)
24 {
25 	double kib = size / 1024.0;
26 	if (kib < 1.0) {
27 		const char* trKey = B_TRANSLATE_MARK("%d bytes");
28 		snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
29 			B_TRANSLATION_CONTEXT), (int)size);
30 		return string;
31 	}
32 	double mib = kib / 1024.0;
33 	if (mib < 1.0) {
34 		const char* trKey = B_TRANSLATE_MARK("%3.2f KiB");
35 		snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
36 			B_TRANSLATION_CONTEXT), kib);
37 		return string;
38 	}
39 	double gib = mib / 1024.0;
40 	if (gib < 1.0) {
41 		const char* trKey = B_TRANSLATE_MARK("%3.2f MiB");
42 		snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
43 			B_TRANSLATION_CONTEXT), mib);
44 		return string;
45 	}
46 	double tib = gib / 1024.0;
47 	if (tib < 1.0) {
48 		const char* trKey = B_TRANSLATE_MARK("%3.2f GiB");
49 		snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
50 			B_TRANSLATION_CONTEXT), gib);
51 		return string;
52 	}
53 	const char* trKey = B_TRANSLATE_MARK("%.2f TiB");
54 	snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
55 		B_TRANSLATION_CONTEXT), tib);
56 	return string;
57 }
58 
59 
60 }	// namespace BPrivate
61 
62