From 9690706bbfa01ca8694e1aa25303cb792bf4eb54 Mon Sep 17 00:00:00 2001 From: Toni500git Date: Sun, 8 Sep 2024 13:18:27 +0200 Subject: [PATCH] chore: code stuff and remove PARSER_TEST --- Makefile | 5 ----- include/query.hpp | 4 ++-- src/main.cpp | 41 ++--------------------------------------- src/parse.cpp | 8 ++++---- src/query/unix/disk.cpp | 13 ++++++------- src/util.cpp | 37 ++++++++++++++++++++++--------------- 6 files changed, 36 insertions(+), 72 deletions(-) diff --git a/Makefile b/Makefile index 6dd339d..785d1f5 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,6 @@ VARS ?= GUI_MODE ?= 0 DEBUG ?= 1 -PARSER_TEST ?= 0 VENDOR_TEST ?= 0 DEVICE_TEST ?= 0 # https://stackoverflow.com/a/1079861 @@ -24,10 +23,6 @@ else BUILDDIR = build/release endif -ifeq ($(PARSER_TEST), 1) - VARS += -DPARSER_TEST=1 -endif - ifeq ($(VENDOR_TEST), 1) VARS += -DVENDOR_TEST=1 endif diff --git a/include/query.hpp b/include/query.hpp index 67ecbd2..fb26fbe 100644 --- a/include/query.hpp +++ b/include/query.hpp @@ -206,7 +206,7 @@ class Disk float used_amount = 0; std::string typefs; std::string device; - std::string mountponit; + std::string mountdir; }; Disk(const std::string_view path, std::vector& paths); @@ -216,7 +216,7 @@ class Disk float& used_amount(); std::string& typefs(); std::string& device(); - std::string& mountponit(); + std::string& mountdir(); private: static struct statvfs m_statvfs; diff --git a/src/main.cpp b/src/main.cpp index d334e87..a2b34bb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -386,45 +386,8 @@ static bool parseargs(int argc, char* argv[], Config& config, const std::string_ return true; } -int main (int argc, char *argv[]) { -#ifdef PARSER_TEST - // test - fmt::println("=== PARSER TEST! ==="); - - std::string test_1 = "Hello, World!"; - std::string test_2 = "Hello, $(echo \"World\")!"; - std::string test_3 = "Hello, \\$(echo \"World\")!"; - std::string test_4 = "Hello, $\\(echo \"World\")!"; - std::string test_5 = "Hello, \\\\$(echo \"World\")!"; - std::string test_6 = "$(echo \"World\")!"; - systemInfo_t systemInfo; - std::unique_ptr pureOutput = std::make_unique(); - std::string clr = "#d3dae3"; - - fmt::print("Useless string (input: {}): ", test_1); - parse(test_1, systemInfo, pureOutput, clr); - fmt::println("\t{}", test_1); - - fmt::print("Exec string (input: {}): ", test_2); - parse(test_2, systemInfo, pureOutput, clr); - fmt::println("\t{}", test_2); - - fmt::print("Bypassed exec string #1 (input: {}): ", test_3); - parse(test_3, systemInfo, pureOutput, clr); - fmt::println("\t{}", test_3); - - fmt::print("Bypassed exec string #2 (input: {}): ", test_4); - parse(test_4, systemInfo, pureOutput, clr); - fmt::println("\t{}", test_4); - - fmt::print("Escaped backslash before exec string (input: {}): ", test_5); - parse(test_5, systemInfo, pureOutput, clr); - fmt::println("\t{}", test_5); - - fmt::print("Exec string at start of the string (input: {}): ", test_6); - parse(test_6, systemInfo, pureOutput, clr); - fmt::println("\t{}", test_6); -#endif +int main (int argc, char *argv[]) +{ #ifdef VENDOR_TEST // test diff --git a/src/parse.cpp b/src/parse.cpp index ad9f5a5..fa21e0f 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -999,22 +999,22 @@ void addValueFromModule(systemInfo_t& sysInfo, const std::string& moduleName, co debug("disk path = {}", path); Query::Disk query_disk(path, queried_disks); + std::array byte_units; if (sysInfo.find(moduleName) == sysInfo.end()) sysInfo.insert({ moduleName, {} }); if (sysInfo[moduleName].find(moduleMemberName) == sysInfo[moduleName].end()) { - std::array byte_units; byte_units.at(TOTAL) = auto_devide_bytes(query_disk.total_amount()); byte_units.at(USED) = auto_devide_bytes(query_disk.used_amount()); byte_units.at(FREE) = auto_devide_bytes(query_disk.free_amount()); switch (moduleMember_hash) { - case "fs"_fnv1a16: SYSINFO_INSERT(query_disk.typefs()); break; - case "device"_fnv1a16: SYSINFO_INSERT(query_disk.device()); break; - case "mountdir"_fnv1a16: SYSINFO_INSERT(query_disk.mountponit()); break; + case "fs"_fnv1a16: SYSINFO_INSERT(query_disk.typefs()); break; + case "device"_fnv1a16: SYSINFO_INSERT(query_disk.device()); break; + case "mountdir"_fnv1a16: SYSINFO_INSERT(query_disk.mountdir()); break; // clang-format off case "disk"_fnv1a16: diff --git a/src/query/unix/disk.cpp b/src/query/unix/disk.cpp index 4baf44a..e80d2f1 100644 --- a/src/query/unix/disk.cpp +++ b/src/query/unix/disk.cpp @@ -21,7 +21,7 @@ Disk::Disk(const std::string_view path, std::vector& paths) // then let's just "try" to remove it m_disk_infos.typefs = MAGIC_LINE; m_disk_infos.device = MAGIC_LINE; - m_disk_infos.mountponit = MAGIC_LINE; + m_disk_infos.mountdir = MAGIC_LINE; return; } @@ -39,13 +39,12 @@ Disk::Disk(const std::string_view path, std::vector& paths) debug("pDevice->mnt_dir = {} && pDevice->mnt_fsname = {}", pDevice->mnt_dir, pDevice->mnt_fsname); if (path == pDevice->mnt_dir || path == pDevice->mnt_fsname) { - m_disk_infos.typefs = pDevice->mnt_type; + m_disk_infos.typefs = pDevice->mnt_type; + m_disk_infos.device = pDevice->mnt_fsname; + m_disk_infos.mountdir = pDevice->mnt_dir; break; } } - - m_disk_infos.device = pDevice->mnt_fsname; - m_disk_infos.mountponit = pDevice->mnt_dir; const std::string_view statpath = (hasStart(path, "/dev") ? pDevice->mnt_dir : path); @@ -77,8 +76,8 @@ float& Disk::free_amount() std::string& Disk::typefs() { return m_disk_infos.typefs; } -std::string& Disk::mountponit() -{ return m_disk_infos.mountponit; } +std::string& Disk::mountdir() +{ return m_disk_infos.mountdir; } std::string& Disk::device() { return m_disk_infos.device; } diff --git a/src/util.cpp b/src/util.cpp index 7adac19..6408aca 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -74,11 +74,11 @@ std::string expandVar(std::string ret) { ret.erase(0, 1); - std::string temp; - size_t pos = ret.find('/'); + std::string temp; + const size_t& pos = ret.find('/'); if (pos != std::string::npos) { - temp = ret.substr(pos + 1); + temp = ret.substr(pos); ret.erase(pos); } @@ -230,9 +230,9 @@ fmt::rgb hexStringToColor(const std::string_view hexstr) uint intValue; ss >> intValue; - uint red = (intValue >> 16) & 0xFF; - uint green = (intValue >> 8) & 0xFF; - uint blue = intValue & 0xFF; + const uint red = (intValue >> 16) & 0xFF; + const uint green = (intValue >> 8) & 0xFF; + const uint blue = intValue & 0xFF; return fmt::rgb(red, green, blue); } @@ -269,12 +269,18 @@ std::string which(const std::string& command) const std::string_view env = std::getenv("PATH"); struct stat sb; std::string fullPath; - + fullPath.reserve(1024); + for (const std::string& dir : split(env, ':')) { - fullPath = dir + '/' + command; - if ((stat(fullPath.c_str(), &sb) == 0) && sb.st_mode & S_IXUSR) - return fullPath; + // -300ns for not creating a string. stonks + fullPath += dir; + fullPath += '/'; + fullPath += command; + if ((stat(fullPath.data(), &sb) == 0) && sb.st_mode & S_IXUSR) + return fullPath.data(); + + fullPath.clear(); } return UNKNOWN; // not found @@ -451,8 +457,8 @@ std::string binarySearchPCIArray(const std::string_view vendor_id_s) // http://stackoverflow.com/questions/478898/ddg#478960 std::string shell_exec(const std::string_view cmd) { - std::array buffer; - std::string result; + std::array buffer; + std::string result; std::unique_ptr pipe(popen(cmd.data(), "r"), pclose); if (!pipe) @@ -487,10 +493,10 @@ std::string vendor_from_entry(const size_t vendor_entry_pos, const std::string_v if (end_line_pos == std::string::npos) end_line_pos = all_ids.length(); // If no newline is found, set to end of string - const std::string line = all_ids.substr(vendor_entry_pos, end_line_pos - vendor_entry_pos); + const std::string& line = all_ids.substr(vendor_entry_pos, end_line_pos - vendor_entry_pos); - const size_t after_id_pos = line.find(vendor_id) + 4; - const std::string description = line.substr(after_id_pos); + const size_t after_id_pos = line.find(vendor_id) + 4; + const std::string& description = line.substr(after_id_pos); const size_t first = description.find_first_not_of(' '); if (first == std::string::npos) @@ -505,6 +511,7 @@ std::string vendor_from_entry(const size_t vendor_entry_pos, const std::string_v return vendor; } +// clang-format off /* * Get the user config directory * either from $XDG_CONFIG_HOME or from $HOME/.config/