// SPDX-License-Identifier: GPL-3.0-or-later // Copyright 2019 Juan Palacios #pragma once #include "idatasource.h" #include #include #include #include #include #include #include #include template concept SysFSDataType = std::is_same_v || std::is_same_v>; /// SysFS data source that allows the transformation of the source data to other /// types. /// /// To perform a transformation of the whole contents of the file, use the type /// std::vector as the Input template type parameter. Otherwise, only /// the first line of the file will be passed to the transformation function. template class SysFSDataSource : public IDataSource { public: /// Creates a new SysFSDataSource instance using a file as data source and, /// optionally, transforming the sysfs data source data to the Output type data /// using the custom transform function. /// /// @param path path to the sysfs file. /// /// @param transform function to map the sysfs data source data to the /// specified Output type. This function is only needed when the Output type is not /// a SysFSDataType, requiring a transformation between the types. SysFSDataSource( std::filesystem::path const &path, std::function &&transform = [](Input const &, Output &) {}) noexcept : path_(path.string()) , transform_(std::move(transform)) { file_.open(path); if (!file_.is_open()) SPDLOG_DEBUG("Cannot open {}", path_.c_str()); } std::string source() const override { return path_; } bool read(Output &data) override { if (file_.is_open()) { if constexpr (std::is_same_v) { readFirstLine(data); } else if constexpr (std::is_same_v>) { readAll(data); } else if constexpr (std::is_same_v>) { readAll(fileData_); transform_(fileData_, data); } else if constexpr (std::is_same_v) { readFirstLine(lineData_); transform_(lineData_, data); } return true; } return false; } private: /// Reads the first line of the file into data. void readFirstLine(std::string &data) { file_.clear(); file_.seekg(0); std::getline(file_, data); } /// Reads the file lines into data, reusing existing data elements as /// the new data holders when possible. void readAll(std::vector &data) { file_.clear(); file_.seekg(0); size_t index = 0; while (std::getline(file_, lineData_)) { if (data.size() == index) data.emplace_back(std::string()); std::swap(lineData_, data[index++]); } } std::string const path_; std::function const transform_; std::ifstream file_; std::string lineData_; std::vector fileData_; };