Search
SailfishOS Open Build Service
>
Projects
>
sailfishos
:
chum
:
testing
>
nload
> _service:tar_git:06_pr.patch
Log In
Username
Password
Cancel
Overview
Repositories
Revisions
Requests
Users
Advanced
Attributes
Meta
File _service:tar_git:06_pr.patch of Package nload
From 472230d4e29924af4fcaec681d29f7dda2632e43 Mon Sep 17 00:00:00 2001 From: WHR <msl0000023508@gmail.com> Date: Tue, 6 Aug 2019 12:24:45 +0800 Subject: [PATCH] Use correct names for binary prefixes; Add support of metric prefixes --- src/app.cpp | 152 ++++++++++++++++++++++----------------------- src/statistics.cpp | 54 +++++++++++++--- src/statistics.h | 20 ++++-- 3 files changed, 133 insertions(+), 93 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index 04bb63c..968cf76 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -86,6 +86,12 @@ App::App() valueMapping[toString(Statistics::humanReadableByte)] = "Human Readable (Byte)"; valueMapping[toString(Statistics::bit)] = "Bit"; valueMapping[toString(Statistics::byte)] = "Byte"; + valueMapping[toString(Statistics::kibiBit)] = "KiBit"; + valueMapping[toString(Statistics::kibiByte)] = "KiByte"; + valueMapping[toString(Statistics::mebiBit)] = "MiBit"; + valueMapping[toString(Statistics::mebiByte)] = "MiByte"; + valueMapping[toString(Statistics::gibiBit)] = "GiBit"; + valueMapping[toString(Statistics::gibiByte)] = "GiByte"; valueMapping[toString(Statistics::kiloBit)] = "kBit"; valueMapping[toString(Statistics::kiloByte)] = "kByte"; valueMapping[toString(Statistics::megaBit)] = "MBit"; @@ -207,12 +213,12 @@ int App::run(const vector<string>& arguments) break; } } - // has the user set a non-default unit for traffic numbers? - else if(*itArg == "-u") + // has the user set a non-default unit for traffic numbers or amount numbers? + else if(*itArg == "-u" || *itArg == "-U") { - Setting& setting = SettingStore::get("TrafficFormat"); - - if(haveNextArg && itNextArg->length() == 1) + char opt = (*itArg)[1]; + Setting& setting = SettingStore::get(opt == 'u' ? "TrafficFormat" : "DataFormat"); + if(haveNextArg && (itNextArg->length() == 1 || itNextArg->length() == 2)) { switch((*itNextArg)[0]) { @@ -229,83 +235,67 @@ int App::run(const vector<string>& arguments) setting = Statistics::bit; break; case 'K': - setting = Statistics::kiloByte; + setting = Statistics::kibiByte; break; case 'k': - setting = Statistics::kiloBit; + setting = Statistics::kibiBit; break; case 'M': - setting = Statistics::megaByte; + setting = Statistics::mebiByte; break; case 'm': - setting = Statistics::megaBit; + setting = Statistics::mebiBit; break; case 'G': - setting = Statistics::gigaByte; + setting = Statistics::gibiByte; break; case 'g': - setting = Statistics::gigaBit; + setting = Statistics::gibiBit; break; default: printHelpAndExit = true; break; } - - ++itArg; - } - else - { - printHelpAndExit = true; - } - - if(printHelpAndExit) - { - cerr << "Wrong argument for the -u parameter." << endl; - break; - } - } - // has the user set a non-default unit for numbers of amount of data? - else if(*itArg == "-U") - { - Setting& setting = SettingStore::get("DataFormat"); - - if(haveNextArg && itNextArg->length() == 1) - { - switch((*itNextArg)[0]) + if(itNextArg->length() == 2) { - case 'H': - setting = Statistics::humanReadableByte; - break; - case 'h': - setting = Statistics::humanReadableBit; - break; - case 'B': - setting = Statistics::byte; - break; - case 'b': - setting = Statistics::bit; - break; - case 'K': - setting = Statistics::kiloByte; - break; - case 'k': - setting = Statistics::kiloBit; - break; - case 'M': - setting = Statistics::megaByte; - break; - case 'm': - setting = Statistics::megaBit; - break; - case 'G': - setting = Statistics::gigaByte; - break; - case 'g': - setting = Statistics::gigaBit; - break; - default: - printHelpAndExit = true; - break; + switch((*itNextArg)[1]) + { + case 'i': + // IEC binary prefixes + break; + case 's': + // SI metric prefixes + switch((int)setting) { + case Statistics::humanReadableBit: + setting = Statistics::humanReadableSiBit; + break; + case Statistics::humanReadableByte: + setting = Statistics::humanReadableSiByte; + break; + case Statistics::kibiBit: + setting = Statistics::kiloBit; + break; + case Statistics::kibiByte: + setting = Statistics::kiloByte; + break; + case Statistics::mebiBit: + setting = Statistics::megaBit; + break; + case Statistics::mebiByte: + setting = Statistics::megaByte; + break; + case Statistics::gibiBit: + setting = Statistics::gigaBit; + break; + case Statistics::gibiByte: + setting = Statistics::gigaByte; + break; + } + break; + default: + printHelpAndExit = true; + break; + } } ++itArg; @@ -317,7 +307,7 @@ int App::run(const vector<string>& arguments) if(printHelpAndExit) { - cerr << "Wrong argument for the -U parameter." << endl; + cerr << "Wrong argument for the -" << opt << " parameter." << endl; break; } } @@ -501,31 +491,35 @@ void App::printHelp(bool error) << "GNU General Public License Version 2 (http://www.gnu.org/copyleft/gpl.html).\n\n" << "Command line syntax:\n" - << PACKAGE << " [options] [devices]\n" + << PACKAGE << " [<options>] [<devices>]\n" << PACKAGE << " --help|-h\n\n" << "Options:\n" - << "-a period Sets the length in seconds of the time window for average\n" + << "-a <period> Sets the length in seconds of the time window for average\n" << " calculation.\n" << " Default is " << STANDARD_AVERAGE_WINDOW << ".\n" - << "-i max_scaling Specifies the 100% mark in kBit/s of the graph indicating the\n" + << "-i <max_scaling>\n" + << " Specifies the 100% mark in kBit/s of the graph indicating the\n" << " incoming bandwidth usage. Use 0 for automatic scaling.\n" << " Ignored if the switch -m is given.\n" << " Default is " << STANDARD_MAX_DEFLECTION << ".\n" << "-m Show multiple devices at a time; no traffic graphs.\n" - << "-o max_scaling Same as -i but for the graph indicating the outgoing bandwidth\n" + << "-o <max_scaling>\n" + << " Same as -i but for the graph indicating the outgoing bandwidth\n" << " usage.\n" << " Default is " << STANDARD_MAX_DEFLECTION << ".\n" - << "-t interval Determines the refresh interval of the display in milliseconds.\n" + << "-t <interval> Determines the refresh interval of the display in milliseconds.\n" << " Default is " << STANDARD_REFRESH_INTERVAL << ".\n" - << "-u h|b|k|m|g Sets the type of unit used for the display of traffic numbers.\n" + << "-u h|b|k|m|g| Sets the type of unit used for the display of traffic numbers.\n" << " H|B|K|M|G h: auto, b: Bit/s, k: kBit/s, m: MBit/s etc.\n" - << " H: auto, B: Byte/s, K: kByte/s, M: MByte/s etc.\n" - << " Default is h.\n" - << "-U h|b|k|m|g Same as -u, but for a total amount of data (without \"/s\").\n" - << " H|B|K|M|G Default is H.\n" - << "-f filename Append traffic data to the named file.\n" - << "devices Network devices to use.\n" + << " [i|s] H: auto, B: Byte/s, K: kByte/s, M: MByte/s etc.\n" + << " i: IEC binary prefixes, s: SI metric prefixes\n" + << " Default is hi.\n" + << "-U h|b|k|m|g| Same as -u, but for a total amount of data (without \"/s\").\n" + << " H|B|K|M|G Default is Hi.\n" + << " [i|s]\n" + << "-f <filename> Append traffic data to the named file.\n" + << "<devices> Network devices to use.\n" << " Default is to use all auto-detected devices.\n" << "--help\n" << "-h Print this help.\n\n" diff --git a/src/statistics.cpp b/src/statistics.cpp index eccd505..94f57e0 100644 --- a/src/statistics.cpp +++ b/src/statistics.cpp @@ -98,7 +98,7 @@ unsigned long Statistics::getLatestTimeStampMicroseconds() const float Statistics::getUnitFactor(dataUnit unit, unsigned long long value) { float factor = 1.0 / (unit % 2 == 0 ? 8 : 1); - + switch(unit) { case humanReadableBit: @@ -111,18 +111,36 @@ float Statistics::getUnitFactor(dataUnit unit, unsigned long long value) factor *= 1024; } return factor; + case humanReadableSiBit: + case humanReadableSiByte: + for(int i = 0; i < 3; i++) { + if(value / factor < 1000) + return factor; + + factor *= 1000; + } + return factor; case bit: case byte: return factor; + case kibiBit: + case kibiByte: + return factor * 1024; + case mebiBit: + case mebiByte: + return factor * 1024 * 1024; + case gibiBit: + case gibiByte: + return factor * 1024 * 1024 * 1024; case kiloBit: case kiloByte: - return factor * 1024; + return factor * 1000; case megaBit: case megaByte: - return factor * 1024 * 1024; + return factor * 1000 * 1000; case gigaBit: case gigaByte: - return factor * 1024 * 1024 * 1024; + return factor * 1000 * 1000 * 1000; default: // should never be executed return factor; } @@ -131,7 +149,8 @@ float Statistics::getUnitFactor(dataUnit unit, unsigned long long value) string Statistics::getUnitString(dataUnit unit, unsigned long long value) { const string description = (unit % 2 == 0 ? "Bit" : "Byte"); - const string units[] = { "", "k", "M", "G" }; + const string units[] = { "", "Ki", "Mi", "Gi" }; + const string si_units[] = { "", "K", "M", "G" }; switch(unit) { @@ -146,18 +165,37 @@ string Statistics::getUnitString(dataUnit unit, unsigned long long value) value /= 1024; } return units[3] + description; + case humanReadableSiBit: + case humanReadableSiByte: + value *= (unit % 2 == 0 ? 8 : 1); + for(int i = 0; i < 3; i++) { + if(value < 1000) + return si_units[i] + description; + + value /= 1000; + } + return si_units[3] + description; case bit: case byte: return description; + case kibiBit: + case kibiByte: + return "Ki" + description; + case mebiBit: + case mebiByte: + return "Mi" + description; + case gibiBit: + case gibiByte: + return "Gi" + description; case kiloBit: case kiloByte: - return 'k' + description; + return "k" + description; case megaBit: case megaByte: - return 'M' + description; + return "M" + description; case gigaBit: case gigaByte: - return 'G' + description; + return "G" + description; default: // should never be executed return description; } diff --git a/src/statistics.h b/src/statistics.h index 00f8424..6b21bdf 100644 --- a/src/statistics.h +++ b/src/statistics.h @@ -31,16 +31,24 @@ class Statistics public: enum dataUnit { + humanReadableSiBit = -4, + humanReadableSiByte = -3, humanReadableBit = -2, humanReadableByte = -1, bit = 0, byte = 1, - kiloBit = 2, - kiloByte = 3, - megaBit = 4, - megaByte = 5, - gigaBit = 6, - gigaByte = 7 + kibiBit = 2, + kibiByte = 3, + mebiBit = 4, + mebiByte = 5, + gibiBit = 6, + gibiByte = 7, + kiloBit = 8, + kiloByte = 9, + megaBit = 10, + megaByte = 11, + gigaBit = 12, + gigaByte = 13 }; Statistics() {}