57 lines
1.3 KiB
C++
57 lines
1.3 KiB
C++
// SysfsRead.cxx
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Author: Unai Blazquez <unaibg2000@gmail.com>
|
|
|
|
#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());
|
|
}
|