From 9d2b3caa948f0aa432a4e1aee610a78b55b11d1f Mon Sep 17 00:00:00 2001 From: Toni500git Date: Sat, 29 Jun 2024 21:54:27 +0200 Subject: [PATCH] misc: use STL std::fill --- include/util.hpp | 8 -------- src/query/cpu.cpp | 26 ++++++++++++-------------- src/query/gpu.cpp | 1 - src/query/ram.cpp | 3 ++- src/query/system.cpp | 2 +- 5 files changed, 15 insertions(+), 25 deletions(-) diff --git a/include/util.hpp b/include/util.hpp index 23edb117..c63c8b4e 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -33,14 +33,6 @@ fmt::rgb hexStringToColor(std::string_view hexstr); std::string getHomeConfigDir(); std::string getConfigDir(); -// must be used with std::array -template -void init_array(T&& array, E&& elements) { - for (size_t i = 0; i < array.size(); i++) { - array.at(i) = elements; - } -} - template void _error_log(fmt::runtime_format_string<> fmt, Args&&... args) { fmt::println(stderr, BOLD_TEXT(fmt::rgb(fmt::color::red)), "ERROR: {}", fmt::format(fmt, std::forward(args)...)); diff --git a/src/query/cpu.cpp b/src/query/cpu.cpp index c1b054b1..b81611ea 100644 --- a/src/query/cpu.cpp +++ b/src/query/cpu.cpp @@ -20,16 +20,15 @@ enum { FREQ_MIN }; -static std::string get_from_text(std::string& line, u_short& iter_index) { +static std::string get_from_text(std::string& line) { std::string amount = line.substr(line.find(':')+1); strip(amount); - iter_index++; return amount; } static std::array get_cpu_infos_str() { std::array ret; - init_array(ret, UNKNOWN); + std::fill(ret.begin(), ret.end(), UNKNOWN); debug("calling in CPU {}", __PRETTY_FUNCTION__); constexpr std::string_view cpuinfo_path = "/proc/cpuinfo"; @@ -40,17 +39,16 @@ static std::array get_cpu_infos_str() { } std::string line; - static u_short iter_index = 0; float cpu_mhz = -1; - while (std::getline(file, line) && iter_index < 3) { - if (line.find("model name") != std::string::npos) - ret.at(NAME) = get_from_text(line, iter_index); + while (std::getline(file, line)) { + if (hasStart(line, "model name")) + ret.at(NAME) = get_from_text(line); - if (line.find("siblings") != std::string::npos) - ret.at(NPROC) = get_from_text(line, iter_index); + if (hasStart(line, "siblings")) + ret.at(NPROC) = get_from_text(line); - if (line.find("cpu MHz") != std::string::npos) { - float tmp = std::stof(get_from_text(line, iter_index)); + if (hasStart(line, "cpu MHz")) { + float tmp = std::stof(get_from_text(line)); if (tmp > cpu_mhz) cpu_mhz = tmp; } @@ -64,7 +62,7 @@ static std::array get_cpu_infos_str() { ret[NAME].erase(pos-1); cpu_mhz /= 1000; - ret.at(FREQ_MAX_CPUINFO) = std::to_string(cpu_mhz); + ret.at(FREQ_MAX_CPUINFO) = fmt::to_string(cpu_mhz); return ret; } @@ -72,8 +70,8 @@ static std::array get_cpu_infos_str() { static std::array get_cpu_infos_t() { debug("calling in CPU {}", __PRETTY_FUNCTION__); std::array ret; - init_array(ret, -1); - + std::fill(ret.begin(), ret.end(), -1); + constexpr std::string_view freq_dir = "/sys/devices/system/cpu/cpu0/cpufreq"; if (std::filesystem::exists(freq_dir)) { diff --git a/src/query/gpu.cpp b/src/query/gpu.cpp index dcee4ffa..10d79339 100644 --- a/src/query/gpu.cpp +++ b/src/query/gpu.cpp @@ -58,7 +58,6 @@ static std::array get_gpu_infos(uint16_t& m_vendor_id, uint16_t& GPU::GPU(smart_pci_access_ptr &pac, u_short id) : m_pPac(pac.get()) { debug("Constructing {}", __func__); if (!m_bInit) { - init_array(m_gpu_infos, UNKNOWN); const u_short max_iter = 10; u_short id_iter = id; std::string sys_path; diff --git a/src/query/ram.cpp b/src/query/ram.cpp index 74bf61b2..dfc03ba2 100644 --- a/src/query/ram.cpp +++ b/src/query/ram.cpp @@ -1,6 +1,7 @@ #include "query.hpp" #include "util.hpp" +#include #include #include @@ -32,7 +33,7 @@ static std::array get_amount() { debug("calling in RAM {}", __PRETTY_FUNCTION__); constexpr std::string_view meminfo_path = "/proc/meminfo"; std::array memory_infos; - init_array(memory_infos, 0); + std::fill(memory_infos.begin(), memory_infos.end(), 0); //std::array extra_mem_info; std::ifstream file(meminfo_path.data()); diff --git a/src/query/system.cpp b/src/query/system.cpp index bfe771bf..44bd3acc 100644 --- a/src/query/system.cpp +++ b/src/query/system.cpp @@ -28,7 +28,7 @@ static std::string get_var(std::string& line, u_short& iter_index) { static std::array get_os_release_vars() { std::array ret; - init_array(ret, UNKNOWN); + std::fill(ret.begin(), ret.end(), -1); debug("calling {}", __PRETTY_FUNCTION__); std::string_view os_release_path = "/etc/os-release";