Skip to content

Commit

Permalink
Add units TiBit, TiByte, PiBit, PiByte, TBit, TByte, PBit and PByte
Browse files Browse the repository at this point in the history
  • Loading branch information
Low-power committed May 24, 2022
1 parent 472230d commit be55008
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 27 deletions.
47 changes: 40 additions & 7 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,20 @@ App::App()
valueMapping[toString(Statistics::mebiByte)] = "MiByte";
valueMapping[toString(Statistics::gibiBit)] = "GiBit";
valueMapping[toString(Statistics::gibiByte)] = "GiByte";
valueMapping[toString(Statistics::tebiBit)] = "TiBit";
valueMapping[toString(Statistics::tebiByte)] = "TiByte";
valueMapping[toString(Statistics::pebiBit)] = "PiBit";
valueMapping[toString(Statistics::pebiByte)] = "PiByte";
valueMapping[toString(Statistics::kiloBit)] = "kBit";
valueMapping[toString(Statistics::kiloByte)] = "kByte";
valueMapping[toString(Statistics::megaBit)] = "MBit";
valueMapping[toString(Statistics::megaByte)] = "MByte";
valueMapping[toString(Statistics::gigaBit)] = "GBit";
valueMapping[toString(Statistics::gigaByte)] = "GByte";
valueMapping[toString(Statistics::teraBit)] = "TBit";
valueMapping[toString(Statistics::teraByte)] = "TByte";
valueMapping[toString(Statistics::petaBit)] = "PBit";
valueMapping[toString(Statistics::petaByte)] = "PByte";
SettingStore::get("TrafficFormat").pushFilter(new SettingFilterMap(valueMapping));
SettingStore::get("DataFormat").pushFilter(new SettingFilterMap(valueMapping));
valueMapping.clear();
Expand Down Expand Up @@ -252,6 +260,18 @@ int App::run(const vector<string>& arguments)
case 'g':
setting = Statistics::gibiBit;
break;
case 'T':
setting = Statistics::tebiByte;
break;
case 't':
setting = Statistics::tebiBit;
break;
case 'P':
setting = Statistics::pebiByte;
break;
case 'p':
setting = Statistics::pebiBit;
break;
default:
printHelpAndExit = true;
break;
Expand Down Expand Up @@ -290,6 +310,18 @@ int App::run(const vector<string>& arguments)
case Statistics::gibiByte:
setting = Statistics::gigaByte;
break;
case Statistics::tebiBit:
setting = Statistics::teraBit;
break;
case Statistics::tebiByte:
setting = Statistics::teraByte;
break;
case Statistics::pebiBit:
setting = Statistics::petaBit;
break;
case Statistics::pebiByte:
setting = Statistics::petaByte;
break;
}
break;
default:
Expand Down Expand Up @@ -510,14 +542,15 @@ void App::printHelp(bool error)
<< " Default is " << STANDARD_MAX_DEFLECTION << ".\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"
<< " H|B|K|M|G h: auto, b: Bit/s, k: kBit/s, m: MBit/s etc.\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"
<< "-u h|b|k|m|g|t|p|H|B|K|M|G|T|P[i|s]\n"
<< " Sets the type of unit used for the display of traffic numbers.\n"
<< " h: auto, b: Bit/s, k: {k|Ki}Bit/s, m: M[i]Bit/s etc.\n"
<< " H: auto, B: Byte/s, K: {k|Ki}Byte/s, M: M[i]Byte/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"
<< "-U h|b|k|m|g|t|p|H|B|K|M|G|T|P[i|s]\n"
<< " Same as -u, but for a total amount of data (without \"/s\").\n"
<< " Default is Hi.\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"
Expand Down
40 changes: 32 additions & 8 deletions src/statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ float Statistics::getUnitFactor(dataUnit unit, unsigned long long value)
{
case humanReadableBit:
case humanReadableByte:
for(int i = 0; i < 3; ++i)
for(int i = 0; i < 5; ++i)
{
if(value / factor < 1024)
return factor;
Expand All @@ -113,7 +113,7 @@ float Statistics::getUnitFactor(dataUnit unit, unsigned long long value)
return factor;
case humanReadableSiBit:
case humanReadableSiByte:
for(int i = 0; i < 3; i++) {
for(int i = 0; i < 5; i++) {
if(value / factor < 1000)
return factor;

Expand All @@ -132,6 +132,12 @@ float Statistics::getUnitFactor(dataUnit unit, unsigned long long value)
case gibiBit:
case gibiByte:
return factor * 1024 * 1024 * 1024;
case tebiBit:
case tebiByte:
return factor * 1024 * 1024 * 1024 * 1024;
case pebiBit:
case pebiByte:
return factor * 1024 * 1024 * 1024 * 1024 * 1024;
case kiloBit:
case kiloByte:
return factor * 1000;
Expand All @@ -141,6 +147,12 @@ float Statistics::getUnitFactor(dataUnit unit, unsigned long long value)
case gigaBit:
case gigaByte:
return factor * 1000 * 1000 * 1000;
case teraBit:
case teraByte:
return factor * 1000 * 1000 * 1000 * 1000;
case petaBit:
case petaByte:
return factor * 1000 * 1000 * 1000 * 1000 * 1000;
default: // should never be executed
return factor;
}
Expand All @@ -149,32 +161,32 @@ 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[] = { "", "Ki", "Mi", "Gi" };
const string si_units[] = { "", "K", "M", "G" };
const string units[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
const string si_units[] = { "", "k", "M", "G", "T", "P" };

switch(unit)
{
case humanReadableBit:
case humanReadableByte:
value *= (unit % 2 == 0 ? 8 : 1);
for(int i = 0; i < 3; ++i)
for(int i = 0; i < 5; ++i)
{
if(value < 1024)
return units[i] + description;

value /= 1024;
}
return units[3] + description;
return units[5] + description;
case humanReadableSiBit:
case humanReadableSiByte:
value *= (unit % 2 == 0 ? 8 : 1);
for(int i = 0; i < 3; i++) {
for(int i = 0; i < 5; i++) {
if(value < 1000)
return si_units[i] + description;

value /= 1000;
}
return si_units[3] + description;
return si_units[5] + description;
case bit:
case byte:
return description;
Expand All @@ -187,6 +199,12 @@ string Statistics::getUnitString(dataUnit unit, unsigned long long value)
case gibiBit:
case gibiByte:
return "Gi" + description;
case tebiBit:
case tebiByte:
return "Ti" + description;
case pebiBit:
case pebiByte:
return "Pi" + description;
case kiloBit:
case kiloByte:
return "k" + description;
Expand All @@ -196,6 +214,12 @@ string Statistics::getUnitString(dataUnit unit, unsigned long long value)
case gigaBit:
case gigaByte:
return "G" + description;
case teraBit:
case teraByte:
return "T" + description;
case petaBit:
case petaByte:
return "P" + description;
default: // should never be executed
return description;
}
Expand Down
32 changes: 20 additions & 12 deletions src/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,26 @@ class Statistics
humanReadableByte = -1,
bit = 0,
byte = 1,
kibiBit = 2,
kibiByte = 3,
mebiBit = 4,
mebiByte = 5,
gibiBit = 6,
gibiByte = 7,
kiloBit = 8,
kiloByte = 9,
megaBit = 10,
megaByte = 11,
gigaBit = 12,
gigaByte = 13
kibiBit,
kibiByte,
mebiBit,
mebiByte,
gibiBit,
gibiByte,
tebiBit,
tebiByte,
pebiBit,
pebiByte,
kiloBit,
kiloByte,
megaBit,
megaByte,
gigaBit,
gigaByte,
teraBit,
teraByte,
petaBit,
petaByte
};

Statistics() {}
Expand Down

0 comments on commit be55008

Please sign in to comment.