1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : arb_misc.cxx // |
---|
4 | // Purpose : misc that doesnt fit elsewhere // |
---|
5 | // // |
---|
6 | // Coded by Ralf Westram (coder@reallysoft.de) in October 2012 // |
---|
7 | // Institute of Microbiology (Technical University Munich) // |
---|
8 | // http://www.arb-home.de/ // |
---|
9 | // // |
---|
10 | // =============================================================== // |
---|
11 | |
---|
12 | #include "arb_misc.h" |
---|
13 | #include "arb_msg.h" |
---|
14 | #include <arb_assert.h> |
---|
15 | |
---|
16 | // AISC_MKPT_PROMOTE:#ifndef _GLIBCXX_CSTDLIB |
---|
17 | // AISC_MKPT_PROMOTE:#include <cstdlib> |
---|
18 | // AISC_MKPT_PROMOTE:#endif |
---|
19 | |
---|
20 | const char *GBS_readable_size(unsigned long long size, const char *unit_suffix) { |
---|
21 | // return human readable size information |
---|
22 | // returned string is maximal 6+strlen(unit) characters long |
---|
23 | // (using "b" as 'unit_suffix' produces '### b', '### Mb' etc) |
---|
24 | |
---|
25 | if (size<1000) return GBS_global_string("%llu %s", size, unit_suffix); |
---|
26 | |
---|
27 | const char *units = "kMGTPEZY"; // kilo, Mega, Giga, Tera, ... should be enough forever |
---|
28 | int i; |
---|
29 | |
---|
30 | for (i = 0; units[i]; ++i) { |
---|
31 | char unit = units[i]; |
---|
32 | if (size<1000*1024) { |
---|
33 | double amount = size/(double)1024; |
---|
34 | if (amount<10.0) return GBS_global_string("%4.2f %c%s", amount+0.005, unit, unit_suffix); |
---|
35 | if (amount<100.0) return GBS_global_string("%4.1f %c%s", amount+0.05, unit, unit_suffix); |
---|
36 | return GBS_global_string("%i %c%s", (int)(amount+0.5), unit, unit_suffix); |
---|
37 | } |
---|
38 | size /= 1024; // next unit |
---|
39 | } |
---|
40 | return GBS_global_string("MUCH %s", unit_suffix); |
---|
41 | } |
---|
42 | |
---|
43 | const char *GBS_readable_timediff(size_t seconds) { |
---|
44 | size_t mins = seconds/60; seconds -= mins * 60; |
---|
45 | size_t hours = mins/60; mins -= hours * 60; |
---|
46 | size_t days = hours/24; hours -= days * 24; |
---|
47 | |
---|
48 | const int MAXPRINT = 40; |
---|
49 | int printed = 0; |
---|
50 | static char buffer[MAXPRINT+1]; |
---|
51 | |
---|
52 | if (days>0) printed += sprintf(buffer+printed, "%zud", days); |
---|
53 | if (printed || hours>0) printed += sprintf(buffer+printed, "%zuh", hours); |
---|
54 | if (printed || mins>0) printed += sprintf(buffer+printed, "%zum", mins); |
---|
55 | |
---|
56 | printed += sprintf(buffer+printed, "%zus", seconds); |
---|
57 | |
---|
58 | arb_assert(printed>0 && printed<MAXPRINT); |
---|
59 | |
---|
60 | return buffer; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|