libroot/posix/stdio: Remove unused portions.
[haiku.git] / src / bin / makeudfimage / Statistics.cpp
bloba94f9c8669799a1a1662d7ba0b218542a49b0097
1 //----------------------------------------------------------------------
2 // This software is part of the OpenBeOS distribution and is covered
3 // by the MIT License.
4 //
5 // Copyright (c) 2003 Tyler Dauwalder, tyler@dauwalder.net
6 //----------------------------------------------------------------------
8 /*! \file Statistics.h
10 BDataIO wrapper around a given attribute for a file. (implementation)
13 #include "Statistics.h"
15 #include <stdio.h>
17 /*! \brief Returns a string describing the given number of bytes
18 in the most appropriate units (i.e. bytes, KB, MB, etc.).
20 std::string
21 bytes_to_string(uint64 bytes)
23 const uint64 kb = 1024; // kilo
24 const uint64 mb = 1024 * kb; // mega
25 const uint64 gb = 1024 * mb; // giga
26 const uint64 tb = 1024 * gb; // tera
27 const uint64 pb = 1024 * tb; // peta
28 const uint64 eb = 1024 * pb; // exa
29 std::string units;
30 uint64 divisor = 1;
31 if (bytes >= eb) {
32 units = "EB";
33 divisor = eb;
34 } else if (bytes >= pb) {
35 units = "PB";
36 divisor = pb;
37 } else if (bytes >= tb) {
38 units = "TB";
39 divisor = tb;
40 } else if (bytes >= gb) {
41 units = "GB";
42 divisor = gb;
43 } else if (bytes >= mb) {
44 units = "MB";
45 divisor = mb;
46 } else if (bytes >= kb) {
47 units = "KB";
48 divisor = kb;
49 } else {
50 units = "bytes";
51 divisor = 1;
53 double scaledValue = double(bytes) / double(divisor);
54 char scaledString[10];
55 // Should really only need 7 chars + NULL...
56 sprintf(scaledString, divisor == 1 ? "%.0f " : "%.1f ", scaledValue);
57 return std::string(scaledString) + units;
60 /*! \brief Creates a new statistics object and sets the start time
61 for the duration timer.
63 Statistics::Statistics()
64 : fStartTime(real_time_clock())
65 , fDirectories(0)
66 , fFiles(0)
67 , fSymlinks(0)
68 , fAttributes(0)
69 , fDirectoryBytes(0)
70 , fFileBytes(0)
71 , fImageSize(0)
75 /*! \brief Resets all statistics fields, including the start time of
76 the duration timer.
78 void
79 Statistics::Reset()
81 Statistics null;
82 *this = null;
86 /*! \brief Returns a string describing the amount of time
87 elapsed since the object was created..
89 std::string
90 Statistics::ElapsedTimeString() const
92 time_t time = ElapsedTime();
93 std::string result;
94 char buffer[256];
95 // seconds
96 uint32 seconds = time % 60;
97 sprintf(buffer, "%ld second%s", seconds, seconds == 1 ? "" : "s");
98 result = buffer;
99 time /= 60;
100 if (time > 0) {
101 // minutes
102 uint32 minutes = time % 60;
103 sprintf(buffer, "%ld minute%s", minutes, minutes == 1 ? "" : "s");
104 result = std::string(buffer) + ", " + result;
105 time /= 60;
106 if (time > 0) {
107 // hours
108 uint32 hours = time % 24;
109 sprintf(buffer, "%ld hour%s", hours, hours == 1 ? "" : "s");
110 result = std::string(buffer) + ", " + result;
111 time /= 24;
112 if (time > 0) {
113 // days
114 uint32 days = time % 365;
115 sprintf(buffer, "%ld day%s", days, days == 1 ? "" : "s");
116 result = std::string(buffer) + ", " + result;
117 time /= 365;
118 if (time > 0) {
119 // years
120 sprintf(buffer, "%ld year%s", time, time == 1 ? "" : "s");
121 result = std::string(buffer) + ", " + result;
122 time /= 60;
127 return result;
130 /*! \brief Returns a string describing the number of bytes
131 allocated to directory data and metadata, displayed in the
132 appropriate units (i.e. bytes, KB, MB, etc.).
134 std::string
135 Statistics::DirectoryBytesString() const
137 return bytes_to_string(DirectoryBytes());
140 /*! \brief Returns a string describing the number of bytes
141 allocated to file data and metadata, displayed in the
142 appropriate units (i.e. bytes, KB, MB, etc.).
144 std::string
145 Statistics::FileBytesString() const
147 return bytes_to_string(FileBytes());
150 /*! \brief Returns a string describing the total image size,
151 displayed in the appropriate units (i.e. bytes, KB, MB, etc.).
153 std::string
154 Statistics::ImageSizeString() const
156 return bytes_to_string(ImageSize());