1 //----------------------------------------------------------------------
2 // This software is part of the OpenBeOS distribution and is covered
5 // Copyright (c) 2003 Tyler Dauwalder, tyler@dauwalder.net
6 //----------------------------------------------------------------------
10 BDataIO wrapper around a given attribute for a file. (implementation)
13 #include "Statistics.h"
17 /*! \brief Returns a string describing the given number of bytes
18 in the most appropriate units (i.e. bytes, KB, MB, etc.).
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
34 } else if (bytes
>= pb
) {
37 } else if (bytes
>= tb
) {
40 } else if (bytes
>= gb
) {
43 } else if (bytes
>= mb
) {
46 } else if (bytes
>= kb
) {
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())
75 /*! \brief Resets all statistics fields, including the start time of
86 /*! \brief Returns a string describing the amount of time
87 elapsed since the object was created..
90 Statistics::ElapsedTimeString() const
92 time_t time
= ElapsedTime();
96 uint32 seconds
= time
% 60;
97 sprintf(buffer
, "%ld second%s", seconds
, seconds
== 1 ? "" : "s");
102 uint32 minutes
= time
% 60;
103 sprintf(buffer
, "%ld minute%s", minutes
, minutes
== 1 ? "" : "s");
104 result
= std::string(buffer
) + ", " + result
;
108 uint32 hours
= time
% 24;
109 sprintf(buffer
, "%ld hour%s", hours
, hours
== 1 ? "" : "s");
110 result
= std::string(buffer
) + ", " + result
;
114 uint32 days
= time
% 365;
115 sprintf(buffer
, "%ld day%s", days
, days
== 1 ? "" : "s");
116 result
= std::string(buffer
) + ", " + result
;
120 sprintf(buffer
, "%ld year%s", time
, time
== 1 ? "" : "s");
121 result
= std::string(buffer
) + ", " + 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.).
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.).
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.).
154 Statistics::ImageSizeString() const
156 return bytes_to_string(ImageSize());