Skip to content

Commit

Permalink
chore: code stuff and remove PARSER_TEST
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Sep 8, 2024
1 parent ac592d7 commit 9690706
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 72 deletions.
5 changes: 0 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ VARS ?=
GUI_MODE ?= 0

DEBUG ?= 1
PARSER_TEST ?= 0
VENDOR_TEST ?= 0
DEVICE_TEST ?= 0
# https://stackoverflow.com/a/1079861
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions include/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string_view>& paths);
Expand All @@ -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;
Expand Down
41 changes: 2 additions & 39 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> pureOutput = std::make_unique<std::string>();
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
Expand Down
8 changes: 4 additions & 4 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_t, 3> byte_units;

if (sysInfo.find(moduleName) == sysInfo.end())
sysInfo.insert({ moduleName, {} });

if (sysInfo[moduleName].find(moduleMemberName) == sysInfo[moduleName].end())
{
std::array<byte_units_t, 3> 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:
Expand Down
13 changes: 6 additions & 7 deletions src/query/unix/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Disk::Disk(const std::string_view path, std::vector<std::string_view>& 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;
}

Expand All @@ -39,13 +39,12 @@ Disk::Disk(const std::string_view path, std::vector<std::string_view>& 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);

Expand Down Expand Up @@ -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; }
37 changes: 22 additions & 15 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<char, 1024> buffer;
std::string result;
std::array<char, 1024> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.data(), "r"), pclose);

if (!pipe)
Expand Down Expand Up @@ -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)
Expand All @@ -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/
Expand Down

0 comments on commit 9690706

Please sign in to comment.