1//----------------------------------------------------------------------
2//  This software is part of the OpenBeOS distribution and is covered
3//  by the OpenBeOS license.
4//
5//  Copyright (c) 2003 Tyler Dauwalder, tyler@dauwalder.net
6//----------------------------------------------------------------------
7
8/*! \file Statistics.h
9
10	BDataIO wrapper around a given attribute for a file. (implementation)
11*/
12
13#include "Statistics.h"
14
15#include <stdio.h>
16
17/*! \brief Returns a string describing the given number of bytes
18	in the most appropriate units (i.e. bytes, KB, MB, etc.).
19*/
20std::string
21bytes_to_string(uint64 bytes)
22{
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;
52	}
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;
58}
59
60/*! \brief Creates a new statistics object and sets the start time
61	for the duration timer.
62*/
63Statistics::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)
72{
73}
74
75/*! \brief Resets all statistics fields, including the start time of
76	the duration timer.
77*/
78void
79Statistics::Reset()
80{
81	Statistics null;
82	*this = null;
83}
84
85
86/*! \brief Returns a string describing the amount of time
87	elapsed since the object was created..
88*/
89std::string
90Statistics::ElapsedTimeString() const
91{
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;
123				}
124			}
125		}
126	}
127	return result;
128}
129
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.).
133*/
134std::string
135Statistics::DirectoryBytesString() const
136{
137	return bytes_to_string(DirectoryBytes());
138}
139
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.).
143*/
144std::string
145Statistics::FileBytesString() const
146{
147	return bytes_to_string(FileBytes());
148}
149
150/*! \brief Returns a string describing the total image size,
151	displayed in the appropriate units (i.e. bytes, KB, MB, etc.).
152*/
153std::string
154Statistics::ImageSizeString() const
155{
156	return bytes_to_string(ImageSize());
157}
158
159