| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include "idatasource.h" |
| 7 | #include <filesystem> |
| 8 | #include <fstream> |
| 9 | #include <functional> |
| 10 | #include <spdlog/spdlog.h> |
| 11 | #include <string> |
| 12 | #include <type_traits> |
| 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | |
| 16 | template<typename T> |
| 17 | concept SysFSDataType = std::is_same_v<T, std::string> || |
| 18 | std::is_same_v<T, std::vector<std::string>>; |
| 19 | |
| 20 | /// SysFS data source that allows the transformation of the source data to other |
| 21 | /// types. |
| 22 | /// |
| 23 | /// To perform a transformation of the whole contents of the file, use the type |
| 24 | /// std::vector<std::string> as the Input template type parameter. Otherwise, only |
| 25 | /// the first line of the file will be passed to the transformation function. |
| 26 | template<typename Output, SysFSDataType Input = std::string> |
| 27 | class SysFSDataSource : public IDataSource<Output> |
| 28 | { |
| 29 | public: |
| 30 | /// Creates a new SysFSDataSource instance using a file as data source and, |
| 31 | /// optionally, transforming the sysfs data source data to the Output type data |
| 32 | /// using the custom transform function. |
| 33 | /// |
| 34 | /// @param path path to the sysfs file. |
| 35 | /// |
| 36 | /// @param transform function to map the sysfs data source data to the |
| 37 | /// specified Output type. This function is only needed when the Output type is not |
| 38 | /// a SysFSDataType, requiring a transformation between the types. |
| 39 | SysFSDataSource( |
| 40 | std::filesystem::path const &path, |
| 41 | std::function<void(Input const &, Output &output)> &&transform = |
| 42 | [](Input const &, Output &) {}) noexcept |
| 43 | : path_(path.string()) |
| 44 | , transform_(std::move(transform)) |
| 45 | { |
| 46 | file_.open(path); |
| 47 | if (!file_.is_open()) |
| 48 | SPDLOG_DEBUG("Cannot open {}", path_.c_str()); |
| 49 | } |
| 50 | |
| 51 | std::string source() const override |
| 52 | { |
| 53 | return path_; |
| 54 | } |
| 55 | |
| 56 | bool read(Output &data) override |
| 57 | { |
| 58 | if (file_.is_open()) { |
| 59 | if constexpr (std::is_same_v<Output, std::string>) { |
| 60 | readFirstLine(data); |
| 61 | } |
| 62 | else if constexpr (std::is_same_v<Output, std::vector<std::string>>) { |
| 63 | readAll(data); |
| 64 | } |
| 65 | else if constexpr (std::is_same_v<Input, std::vector<std::string>>) { |
| 66 | readAll(fileData_); |
| 67 | transform_(fileData_, data); |
| 68 | } |
| 69 | else if constexpr (std::is_same_v<Input, std::string>) { |
| 70 | readFirstLine(lineData_); |
| 71 | transform_(lineData_, data); |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | /// Reads the first line of the file into data. |
| 82 | void readFirstLine(std::string &data) |
| 83 | { |
| 84 | file_.clear(); |
| 85 | file_.seekg(0); |
| 86 | std::getline(file_, data); |
| 87 | } |
| 88 | |
| 89 | /// Reads the file lines into data, reusing existing data elements as |
| 90 | /// the new data holders when possible. |
| 91 | void readAll(std::vector<std::string> &data) |
| 92 | { |
| 93 | file_.clear(); |
| 94 | file_.seekg(0); |
| 95 | |
| 96 | size_t index = 0; |
| 97 | while (std::getline(file_, lineData_)) { |
| 98 | if (data.size() == index) |
| 99 | data.emplace_back(std::string()); |
| 100 | |
| 101 | std::swap(lineData_, data[index++]); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | std::string const path_; |
| 106 | std::function<void(Input const &, Output &output)> const transform_; |
| 107 | |
| 108 | std::ifstream file_; |
| 109 | std::string lineData_; |
| 110 | std::vector<std::string> fileData_; |
| 111 | }; |