Added an unsigned overload of si_string.

Converted everything I could find to use si_string or at least use the
units correctly.
This commit is contained in:
Alexander van Gessel 2011-09-03 14:13:58 +01:00
parent f0632041f9
commit 002fead566
5 changed files with 31 additions and 10 deletions

View file

@ -364,7 +364,7 @@ namespace {
config response;
response.add_child("campaigns",campaign_list);
std::cerr << " size: " << (network::send_data(response, sock)/1024) << "kb\n";
std::cerr << " size: " << (network::send_data(response, sock)/1024) << "KiB\n";
}
else if (const config &req = data.child("request_campaign"))
{
@ -373,7 +373,7 @@ namespace {
if (!campaign) {
network::send_data(construct_error("Add-on '" + req["name"].str() + "'not found."), sock);
} else {
std::cerr << " size: " << (file_size(campaign["filename"])/1024) << "kb\n";
std::cerr << " size: " << (file_size(campaign["filename"])/1024) << "KiB\n";
network::send_file(campaign["filename"], sock);
int downloads = campaign["downloads"].to_int() + 1;
campaign["downloads"] = downloads;

View file

@ -26,6 +26,7 @@
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "log.hpp"
#include "serialization/string_utils.hpp"
namespace gui2 {
@ -50,14 +51,13 @@ void tnetwork_transmission::pump_monitor::process(events::pump_info&)
find_widget<tprogress_bar>(&(window_.get()), "progress", false)
.set_percentage((completed*100)/total);
string_map symbols;
symbols["total"] = str_cast(total/1024);
symbols["completed"] = str_cast(completed/1024);
std::stringstream ss;
ss << utils::si_string(total, true, _("unit_byte^B"))
<< "/"
<< utils::si_string(total, true, _("unit_byte^B"));
find_widget<tlabel>(&(window_.get()), "numeric_progress", false)
.set_label(vgettext(
"$completed KiB/$total KiB"
, symbols));
.set_label(ss.str());
window_->invalidate_layout();
}
}

View file

@ -1016,9 +1016,9 @@ std::string get_bandwidth_stats(int hour)
ss << "Hour stat starting from " << hour << "\n " << std::left << std::setw(bandwidth_stats::type_width) << "Type of packet" << "| "
<< std::setw(bandwidth_stats::packet_width)<< "out #" << "| "
<< std::setw(bandwidth_stats::bytes_width) << "out kb" << "| " /* Are these bytes or bits? base10 or base2? */
<< std::setw(bandwidth_stats::bytes_width) << "out KiB" << "| "
<< std::setw(bandwidth_stats::packet_width)<< "in #" << "| "
<< std::setw(bandwidth_stats::bytes_width) << "in kb" << "\n";
<< std::setw(bandwidth_stats::bytes_width) << "in KiB" << "\n";
bandwidth_stats_output outputer(ss);
std::for_each(hour_stats[hour].begin(), hour_stats[hour].end(), outputer);

View file

@ -276,6 +276,9 @@ void si_string_impl_stream_write(std::stringstream &ss, T input) {
template <> void si_string_impl_stream_write(std::stringstream &ss, int input) {
ss << input;
}
template <> void si_string_impl_stream_write(std::stringstream &ss, unsigned input) {
ss << input;
}
template <> void si_string_impl_stream_write(std::stringstream &ss, long input) {
ss << input;
}
@ -284,6 +287,20 @@ template <> void si_string_impl_stream_write(std::stringstream &ss, long long in
}
template <typename T>
std::string si_string_impl(T input, bool base2, std::string unit) {
// (input == -input) can happen if we're at the minimum of the native signed integer type, so make sure we don't recurse infinitely
// It will still give bad output, but it won't overflow the stack
if (input < 0 && input != -input)
return unicode_minus + si_string(-input, base2, unit);
return si_string_impl(input, base2, unit);
}
// Specialisation for unsigned as the template causes warnings otherwise
template <> std::string si_string_impl(unsigned input, bool base2, std::string unit) {
return si_string_impl(input, base2, unit);
}
// A second layer of indirection, for the above specialisation
template <typename T>
std::string si_string_impl2(T input, bool base2, std::string unit) {
const int multiplier = base2 ? 1024 : 1000;
// (input == -input) can happen if we're at the minimum of the native signed integer type, so make sure we don't recurse infinitely
// It will still give bad output, but it won't overflow the stack
@ -349,6 +366,9 @@ std::string si_string(double input, bool base2, std::string unit) {
std::string si_string(int input, bool base2, std::string unit) {
return si_string_impl(input, base2, unit);
}
std::string si_string(unsigned input, bool base2, std::string unit) {
return si_string_impl(input, base2, unit);
}
std::string si_string(long input, bool base2, std::string unit) {
return si_string_impl(input, base2, unit);
}

View file

@ -147,6 +147,7 @@ inline std::string signed_percent(int val) {return signed_value(val) + "%";}
*/
std::string si_string(double input, bool base2=true, std::string unit="B");
std::string si_string(int input, bool base2=true, std::string unit="B");
std::string si_string(unsigned input, bool base2=true, std::string unit="B");
/* Some extra-long versions */
std::string si_string(long input, bool base2=true, std::string unit="B");
std::string si_string(long long input, bool base2=true, std::string unit="B");