feature/SysfsReaderClass #2

Merged
unai merged 10 commits from feature/SysfsReaderClass into main 2026-03-10 16:33:24 +00:00
2 changed files with 45 additions and 0 deletions
Showing only changes of commit 6e1cdec81f - Show all commits

View File

@ -36,5 +36,9 @@ class SysfsReader
SysfsStatus read_status() const;
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.
};

View File

@ -5,11 +5,52 @@
#include "SysfsRead.hpp"
#include <algorithm>
#include <fstream>
SysfsReader::SysfsReader(const std::filesystem::path& input_path)
: m_path(input_path)
{
}
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;
}
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());
}