From 6e1cdec81f08f92ec106663c51cea4a4d3d826f4 Mon Sep 17 00:00:00 2001 From: unai_71 Date: Tue, 10 Mar 2026 16:30:03 +0000 Subject: [PATCH] feat: implemented sysfs reader class, added new trim method to clean strings --- include/SysfsRead.hpp | 4 ++++ src/core/SysfsRead.cxx | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/SysfsRead.hpp b/include/SysfsRead.hpp index bf910f3..c8c9e34 100644 --- a/include/SysfsRead.hpp +++ b/include/SysfsRead.hpp @@ -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. }; diff --git a/src/core/SysfsRead.cxx b/src/core/SysfsRead.cxx index 962dbef..807bcbf 100644 --- a/src/core/SysfsRead.cxx +++ b/src/core/SysfsRead.cxx @@ -5,11 +5,52 @@ #include "SysfsRead.hpp" +#include +#include + 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()); +}