Skip to content

Commit

Permalink
Use correct names for binary prefixes; Add support of metric prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Low-power committed Aug 6, 2019
1 parent 8f92dc0 commit 44f7d70
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 94 deletions.
152 changes: 73 additions & 79 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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])
{
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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"
Expand Down
56 changes: 47 additions & 9 deletions src/statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
}
Expand All @@ -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)
{
Expand All @@ -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;
}
Expand All @@ -166,7 +204,7 @@ string Statistics::getUnitString(dataUnit unit, unsigned long long value)
void Statistics::calculateAverage(const DataFrame& dataFrameFrom, const DataFrame& dataFrameTo, DataFrame& result)
{
float timeSpan = (dataFrameTo.getTimeStampSeconds() + dataFrameTo.getTimeStampMicroseconds() / 1000000.0) -
(dataFrameFrom.getTimeStampSeconds() + dataFrameFrom.getTimeStampMicroseconds() / 1000000.0);
(dataFrameFrom.getTimeStampSeconds() + dataFrameFrom.getTimeStampMicroseconds() / 1000000.0);

if(timeSpan <= 0)
return;
Expand Down
20 changes: 14 additions & 6 deletions src/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down

0 comments on commit 44f7d70

Please sign in to comment.