feat: implemented sysfs reader class, added new trim method to clean strings

This commit is contained in:
unai_71 2026-03-10 16:30:03 +00:00
parent f04de7ea34
commit 6e1cdec81f
2 changed files with 45 additions and 0 deletions

View File

@ -36,5 +36,9 @@ class SysfsReader
SysfsStatus read_status() const; SysfsStatus read_status() const;
private: private:
/// @brief Helper method for trimming trailing whitespaces and
/// newline indicators
/// @param String from the m_path file
static void trim_in_place(std::string& string);
std::filesystem::path m_path; // Path to the input file. std::filesystem::path m_path; // Path to the input file.
}; };

View File

@ -5,11 +5,52 @@
#include "SysfsRead.hpp" #include "SysfsRead.hpp"
#include <algorithm>
#include <fstream>
SysfsReader::SysfsReader(const std::filesystem::path& input_path) SysfsReader::SysfsReader(const std::filesystem::path& input_path)
: m_path(input_path) : m_path(input_path)
{ {
} }
SysfsStatus SysfsReader::read_status() const SysfsStatus SysfsReader::read_status() const
{ {
std::ifstream input_file_stream(m_path);
if (!input_file_stream.is_open())
{
return SysfsStatus::Unreachable;
}
std::stringstream buffer;
buffer << input_file_stream.rdbuf(); // read entire stream into buffer
std::string contents = buffer.str();
trim_in_place(contents); // clean input string
// compare
if (contents.empty())
{
return SysfsStatus::Empty;
}
if (contents == "1")
{
return SysfsStatus::Enabled;
}
if (contents == "error: temp too high")
{
return SysfsStatus::ErrorTempTooHigh;
}
return SysfsStatus::UnexpectedValue; return SysfsStatus::UnexpectedValue;
} }
void SysfsReader::trim_in_place(std::string& string)
{
// left trim
string.erase(string.begin(),
std::find_if(string.begin(), string.end(), [](unsigned char ch)
{ return !std::isspace(ch); }));
// right trim
string.erase(std::find_if(string.rbegin(), string.rend(),
[](unsigned char ch) { return !std::isspace(ch); })
.base(),
string.end());
}