Skip to content

Commit

Permalink
misc: use STL std::fill
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Jun 29, 2024
1 parent 0a393e3 commit 9d2b3ca
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 25 deletions.
8 changes: 0 additions & 8 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ fmt::rgb hexStringToColor(std::string_view hexstr);
std::string getHomeConfigDir();
std::string getConfigDir();

// must be used with std::array
template <typename T, typename E>
void init_array(T&& array, E&& elements) {
for (size_t i = 0; i < array.size(); i++) {
array.at(i) = elements;
}
}

template <typename... Args>
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>(args)...));
Expand Down
26 changes: 12 additions & 14 deletions src/query/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, 3> get_cpu_infos_str() {
std::array<std::string, 3> 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";
Expand All @@ -40,17 +39,16 @@ static std::array<std::string, 3> 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;
}
Expand All @@ -64,16 +62,16 @@ static std::array<std::string, 3> 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;
}

static std::array<float, 4> get_cpu_infos_t() {
debug("calling in CPU {}", __PRETTY_FUNCTION__);
std::array<float, 4> 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)) {
Expand Down
1 change: 0 additions & 1 deletion src/query/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ static std::array<std::string, 2> 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;
Expand Down
3 changes: 2 additions & 1 deletion src/query/ram.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "query.hpp"
#include "util.hpp"

#include <algorithm>
#include <array>
#include <fstream>

Expand Down Expand Up @@ -32,7 +33,7 @@ static std::array<size_t, 3> get_amount() {
debug("calling in RAM {}", __PRETTY_FUNCTION__);
constexpr std::string_view meminfo_path = "/proc/meminfo";
std::array<size_t, 3> memory_infos;
init_array(memory_infos, 0);
std::fill(memory_infos.begin(), memory_infos.end(), 0);

//std::array<size_t, 5> extra_mem_info;
std::ifstream file(meminfo_path.data());
Expand Down
2 changes: 1 addition & 1 deletion src/query/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static std::string get_var(std::string& line, u_short& iter_index) {

static std::array<std::string, 4> get_os_release_vars() {
std::array<std::string, 4> 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";
Expand Down

0 comments on commit 9d2b3ca

Please sign in to comment.