feat: implemented sysfs reader class, added new trim method to clean strings
This commit is contained in:
parent
f04de7ea34
commit
6e1cdec81f
@ -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.
|
||||
};
|
||||
|
||||
@ -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());
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user