Skip to content

Commit

Permalink
query: convert float to double
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Oct 13, 2024
1 parent f77e8be commit dbc93b4
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 59 deletions.
4 changes: 2 additions & 2 deletions include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ white = "\e[1;37m"
alias-colors = ["purple=magenta"]
# Used in disk, ram and swap modules.
# If true, we're going to use the SI standard byte unit (1kb == 1000 bytes)
# Else if false, we using the IEC byte unit (1kb == 1024 bytes)
# If true, we're going to use the SI standard byte unit (1kB == 1000 bytes)
# Else if false, we using the IEC byte unit (1KiB == 1024 bibytes)
# Really nerdy stuff
use-SI-byte-unit = false
Expand Down
58 changes: 29 additions & 29 deletions include/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ extern "C" {
}

using systemInfo_t =
std::unordered_map<std::string, std::unordered_map<std::string, std::variant<std::string, size_t, float>>>;
using variant = std::variant<std::string, size_t, float>;
std::unordered_map<std::string, std::unordered_map<std::string, std::variant<std::string, size_t, double>>>;
using variant = std::variant<std::string, size_t, double>;

namespace Query
{
Expand Down Expand Up @@ -151,23 +151,23 @@ class CPU
std::string name{ UNKNOWN };
std::string nproc{ UNKNOWN };

float freq_max = 0;
float freq_min = 0;
float freq_cur = 0;
float freq_bios_limit = 0;
double freq_max = 0;
double freq_min = 0;
double freq_cur = 0;
double freq_bios_limit = 0;

// private:
float freq_max_cpuinfo = 0;
double freq_max_cpuinfo = 0;
};

CPU() noexcept;

std::string& name() noexcept;
std::string& nproc() noexcept;
float& freq_max() noexcept;
float& freq_min() noexcept;
float& freq_cur() noexcept;
float& freq_bios_limit() noexcept;
double& freq_max() noexcept;
double& freq_min() noexcept;
double& freq_cur() noexcept;
double& freq_bios_limit() noexcept;

private:
static bool m_bInit;
Expand Down Expand Up @@ -202,19 +202,19 @@ class Disk
public:
struct Disk_t
{
float total_amount = 0;
float free_amount = 0;
float used_amount = 0;
double total_amount = 0;
double free_amount = 0;
double used_amount = 0;
std::string typefs;
std::string device;
std::string mountdir;
};

Disk(const std::string_view path, std::vector<std::string>& paths);

float& total_amount() noexcept;
float& free_amount() noexcept;
float& used_amount() noexcept;
double& total_amount() noexcept;
double& free_amount() noexcept;
double& used_amount() noexcept;
std::string& typefs() noexcept;
std::string& device() noexcept;
std::string& mountdir() noexcept;
Expand All @@ -229,22 +229,22 @@ class RAM
public:
struct RAM_t
{
float total_amount = 0;
float free_amount = 0;
float used_amount = 0;
float swap_free_amount = 0;
float swap_used_amount = 0;
float swap_total_amount = 0;
double total_amount = 0;
double free_amount = 0;
double used_amount = 0;
double swap_free_amount = 0;
double swap_used_amount = 0;
double swap_total_amount = 0;
};

RAM() noexcept;

float& total_amount() noexcept;
float& free_amount() noexcept;
float& used_amount() noexcept;
float& swap_free_amount() noexcept;
float& swap_used_amount() noexcept;
float& swap_total_amount() noexcept;
double& total_amount() noexcept;
double& free_amount() noexcept;
double& used_amount() noexcept;
double& swap_free_amount() noexcept;
double& swap_used_amount() noexcept;
double& swap_total_amount() noexcept;

private:
static bool m_bInit;
Expand Down
12 changes: 6 additions & 6 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ std::string getInfoFromName(const systemInfo_t& systemInfo, const std::string_vi
if (std::holds_alternative<std::string>(result))
return std::get<std::string>(result);

else if (std::holds_alternative<float>(result))
return fmt::format("{:.2f}", (std::get<float>(result)));
else if (std::holds_alternative<double>(result))
return fmt::format("{:.2f}", (std::get<double>(result)));

else
return fmt::to_string(std::get<size_t>(result));
Expand Down Expand Up @@ -873,11 +873,11 @@ void addValueFromModule(const std::string& moduleName, const std::string& module
const std::uint16_t byte_unit = config.use_SI_unit ? 1000 : 1024;
constexpr std::array<std::string_view, 32> sorted_valid_prefixes = {"B", "EB", "EiB", "GB", "GiB", "kB", "KiB", "MB", "MiB", "PB", "PiB", "TB", "TiB", "YB", "YiB", "ZB", "ZiB"};

const auto& return_devided_bytes = [&sorted_valid_prefixes, &moduleMemberName](const float& amount) -> float
const auto& return_devided_bytes = [&sorted_valid_prefixes, &moduleMemberName](const double& amount) -> double
{
const std::string& prefix = moduleMemberName.substr(moduleMemberName.find('-')+1);
if (std::binary_search(sorted_valid_prefixes.begin(), sorted_valid_prefixes.end(), prefix))
return static_cast<float>(devide_bytes(amount, prefix).num_bytes);
return devide_bytes(amount, prefix).num_bytes;

return 0;
};
Expand Down Expand Up @@ -1389,9 +1389,9 @@ void addValueFromModule(const std::string& moduleName, const std::string& module
if (sysInfo.at(moduleName).find(moduleMemberName) == sysInfo.at(moduleName).end())
{
// idk, trick the diviser
byte_units.at(USED) = auto_devide_bytes(query_ram.used_amount() * byte_unit, byte_unit);
byte_units.at(USED) = auto_devide_bytes(query_ram.used_amount()* byte_unit, byte_unit);
byte_units.at(TOTAL) = auto_devide_bytes(query_ram.total_amount() * byte_unit, byte_unit);
byte_units.at(FREE) = auto_devide_bytes(query_ram.free_amount() * byte_unit, byte_unit);
byte_units.at(FREE) = auto_devide_bytes(query_ram.free_amount()* byte_unit, byte_unit);

switch (moduleMember_hash)
{
Expand Down
22 changes: 11 additions & 11 deletions src/query/unix/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static CPU::CPU_t get_cpu_infos()

if (hasStart(line, "cpu MHz"))
{
float tmp = std::stof(get_from_text(line));
double tmp = std::stof(get_from_text(line));
if (tmp > cpu_mhz)
cpu_mhz = tmp;
}
Expand All @@ -50,7 +50,7 @@ static CPU::CPU_t get_cpu_infos()
// sometimes /proc/cpuinfo at model name
// the name will contain the min freq
// happens on intel cpus especially
const size_t& pos = ret.name.rfind('@');
const size_t pos = ret.name.rfind('@');
if (pos != std::string::npos)
ret.name.erase(pos - 1);

Expand All @@ -60,13 +60,13 @@ static CPU::CPU_t get_cpu_infos()
// add 1 to the nproc
ret.nproc = fmt::to_string(std::stoi(ret.nproc) + 1);

constexpr std::string_view freq_dir = "/sys/devices/system/cpu/cpu0/cpufreq";
const std::string freq_dir = "/sys/devices/system/cpu/cpu0/cpufreq";
if (std::filesystem::exists(freq_dir))
{
std::ifstream cpu_bios_limit_f(fmt::format("{}/bios_limit", freq_dir));
std::ifstream cpu_scaling_cur_f(fmt::format("{}/scaling_cur_freq", freq_dir));
std::ifstream cpu_scaling_max_f(fmt::format("{}/scaling_max_freq", freq_dir));
std::ifstream cpu_scaling_min_f(fmt::format("{}/scaling_min_freq", freq_dir));
std::ifstream cpu_bios_limit_f(freq_dir + "/bios_limit");
std::ifstream cpu_scaling_cur_f(freq_dir + "/scaling_cur_freq");
std::ifstream cpu_scaling_max_f(freq_dir + "/scaling_max_freq");
std::ifstream cpu_scaling_min_f(freq_dir + "/scaling_min_freq");

std::string freq_bios_limit, freq_cpu_scaling_cur, freq_cpu_scaling_max, freq_cpu_scaling_min;

Expand Down Expand Up @@ -99,14 +99,14 @@ std::string& CPU::name() noexcept
std::string& CPU::nproc() noexcept
{ return m_cpu_infos.nproc; }

float& CPU::freq_bios_limit() noexcept
double& CPU::freq_bios_limit() noexcept
{ return m_cpu_infos.freq_bios_limit; }

float& CPU::freq_cur() noexcept
double& CPU::freq_cur() noexcept
{ return m_cpu_infos.freq_cur; }

float& CPU::freq_max() noexcept
double& CPU::freq_max() noexcept
{ return (m_cpu_infos.freq_max <= 0) ? m_cpu_infos.freq_max_cpuinfo : m_cpu_infos.freq_max; }

float& CPU::freq_min() noexcept
double& CPU::freq_min() noexcept
{ return m_cpu_infos.freq_min; }
10 changes: 5 additions & 5 deletions src/query/unix/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ Disk::Disk(const std::string_view path, std::vector<std::string>& paths)
return;
}

m_disk_infos.total_amount = static_cast<float>(m_statvfs.f_blocks * m_statvfs.f_frsize);
m_disk_infos.free_amount = static_cast<float>(m_statvfs.f_bfree * m_statvfs.f_frsize);
m_disk_infos.total_amount = static_cast<double>(m_statvfs.f_blocks * m_statvfs.f_frsize);
m_disk_infos.free_amount = static_cast<double>(m_statvfs.f_bfree * m_statvfs.f_frsize);
m_disk_infos.used_amount = m_disk_infos.total_amount - m_disk_infos.free_amount;

endmntent(mountsFile);

}

// clang-format off
float& Disk::total_amount() noexcept
double& Disk::total_amount() noexcept
{ return m_disk_infos.total_amount; }

float& Disk::used_amount() noexcept
double& Disk::used_amount() noexcept
{ return m_disk_infos.used_amount; }

float& Disk::free_amount() noexcept
double& Disk::free_amount() noexcept
{ return m_disk_infos.free_amount; }

std::string& Disk::typefs() noexcept
Expand Down
12 changes: 6 additions & 6 deletions src/query/unix/ram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,20 @@ RAM::RAM() noexcept
}

// clang-format off
float& RAM::free_amount() noexcept
double& RAM::free_amount() noexcept
{ return m_memory_infos.free_amount; }

float& RAM::total_amount() noexcept
double& RAM::total_amount() noexcept
{ return m_memory_infos.total_amount; }

float& RAM::used_amount() noexcept
double& RAM::used_amount() noexcept
{ return m_memory_infos.used_amount; }

float& RAM::swap_total_amount() noexcept
double& RAM::swap_total_amount() noexcept
{ return m_memory_infos.swap_total_amount; }

float& RAM::swap_used_amount() noexcept
double& RAM::swap_used_amount() noexcept
{ return m_memory_infos.swap_used_amount; }

float& RAM::swap_free_amount() noexcept
double& RAM::swap_free_amount() noexcept
{ return m_memory_infos.swap_free_amount; }

0 comments on commit dbc93b4

Please sign in to comment.