Use while (getline) instead of a for loop

Also make the comments surrounding the /proc/net/dev parsing clearer and
remove the apparently redundant "is the netdev file still good?" check.
pull/1230/head
Ciaran Downey 2021-08-26 11:30:06 -07:00
parent 9d9f959769
commit 5186dd27e6
1 changed files with 7 additions and 9 deletions

View File

@ -28,18 +28,14 @@ waybar::modules::Network::readBandwidthUsage() {
return {}; return {};
} }
// skip the headers
std::string line; std::string line;
// skip the headers (first two lines)
std::getline(netdev, line); std::getline(netdev, line);
std::getline(netdev, line); std::getline(netdev, line);
if (!netdev) {
spdlog::warn("Unexpectedly short netdev file {}", NETDEV_FILE);
return {};
}
unsigned long long receivedBytes = 0ull; unsigned long long receivedBytes = 0ull;
unsigned long long transmittedBytes = 0ull; unsigned long long transmittedBytes = 0ull;
for (std::getline(netdev, line); netdev; std::getline(netdev, line)) { while (std::getline(netdev, line)) {
std::istringstream iss(line); std::istringstream iss(line);
std::string ifacename; std::string ifacename;
@ -50,8 +46,11 @@ waybar::modules::Network::readBandwidthUsage() {
} }
// The rest of the line consists of whitespace separated counts divided // The rest of the line consists of whitespace separated counts divided
// into two groups (receive and transmit). The first column in each group // into two groups (receive and transmit). Each group has the following
// is bytes, which is the only one we care about. // columns: bytes, packets, errs, drop, fifo, frame, compressed, multicast
//
// We only care about the bytes count, so we'll just ignore the 7 other
// columns.
unsigned long long r = 0ull; unsigned long long r = 0ull;
unsigned long long t = 0ull; unsigned long long t = 0ull;
// Read received bytes // Read received bytes
@ -65,7 +64,6 @@ waybar::modules::Network::readBandwidthUsage() {
} }
// Read transmit bytes // Read transmit bytes
iss >> t; iss >> t;
spdlog::trace("read r={}, t={}, iface={}", r, t, ifacename);
receivedBytes += r; receivedBytes += r;
transmittedBytes += t; transmittedBytes += t;