| 1 | // SPDX-License-Identifier: GPL-3.0-or-latercxx . . $ `<file>/src/core/sysfsdatasource.h`/ |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include "idatasource.h"cxx . . $ `<file>/src/core/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 = stdcxx . . $ std/::is_same_v<T, stdcxx . . $ std/::stringcxx . . $ std/string#> || |
| 18 | stdcxx . . $ std/::is_same_v<T, stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ 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 = stdcxx . . $ std/::stringcxx . . $ std/string#> |
| 27 | class SysFSDataSourcecxx . . $ SysFSDataSource# : public IDataSourcecxx . . $ 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 | SysFSDataSourcecxx . . $ SysFSDataSource#`SysFSDataSource<Output, Input>`(7da52c13476efd47).( |
| 40 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# const &path, |
| 41 | stdcxx . . $ std/::functioncxx . . $ std/function#<void(Input const &, Output &output)> &&transform = |
| 42 | [](Input const &, Output &) {}) noexcept |
| 43 | : path_cxx . . $ SysFSDataSource#path_.(path.stringcxx . . $ std/filesystem/__cxx11/path#string(219584694fe2b248).()) |
| 44 | , transform_cxx . . $ SysFSDataSource#transform_.(stdcxx . . $ std/::movecxx . . $ std/move(88304b602929f6f4).(transform)) |
| 45 | { |
| 46 | file_cxx . . $ SysFSDataSource#file_..opencxx . . $ std/basic_ifstream#open(16164bf63388f14d).(path); |
| 47 | if (!file_cxx . . $ SysFSDataSource#file_..is_opencxx . . $ std/basic_ifstream#is_open(c9857a73d68e59ba).()) |
| 48 | SPDLOG_DEBUGcxx . . $ spdlog/level/("Cannot open {}", path_.c_str()); |
| 49 | } |
| 50 | |
| 51 | stdcxx . . $ std/::stringcxx . . $ std/string# sourcecxx . . $ SysFSDataSource#source(219584694fe2b248).() const override |
| 52 | { |
| 53 | return path_cxx . . $ SysFSDataSource#path_.; |
| 54 | } |
| 55 | |
| 56 | bool readcxx . . $ SysFSDataSource#read(e5bfd7b2d24a2428).(Output &data) override |
| 57 | { |
| 58 | if (file_cxx . . $ SysFSDataSource#file_..is_opencxx . . $ std/basic_ifstream#is_open(c9857a73d68e59ba).()) { |
| 59 | if constexpr (stdcxx . . $ std/::is_same_v<Output, stdcxx . . $ std/::stringcxx . . $ std/string#>) { |
| 60 | readFirstLinecxx . . $ SysFSDataSource#readFirstLine(80d0450aec2fd74).(data); |
| 61 | } |
| 62 | else if constexpr (stdcxx . . $ std/::is_same_v<Output, stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#>>) { |
| 63 | readAllcxx . . $ SysFSDataSource#readAll(f5a3f8b4650a788e).(data); |
| 64 | } |
| 65 | else if constexpr (stdcxx . . $ std/::is_same_v<Input, stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#>>) { |
| 66 | readAllcxx . . $ SysFSDataSource#readAll(f5a3f8b4650a788e).(fileData_cxx . . $ SysFSDataSource#fileData_.); |
| 67 | transform_cxx . . $ SysFSDataSource#transform_.(fileData_cxx . . $ SysFSDataSource#fileData_., data); |
| 68 | } |
| 69 | else if constexpr (stdcxx . . $ std/::is_same_v<Input, stdcxx . . $ std/::stringcxx . . $ std/string#>) { |
| 70 | readFirstLinecxx . . $ SysFSDataSource#readFirstLine(80d0450aec2fd74).(lineData_cxx . . $ SysFSDataSource#lineData_.); |
| 71 | transform_cxx . . $ SysFSDataSource#transform_.(lineData_cxx . . $ SysFSDataSource#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 readFirstLinecxx . . $ SysFSDataSource#readFirstLine(80d0450aec2fd74).(stdcxx . . $ std/::stringcxx . . $ std/string# &data) |
| 83 | { |
| 84 | file_cxx . . $ SysFSDataSource#file_..clearcxx . . $ std/basic_ios#clear(3e5fb2eb616d497d).(); |
| 85 | file_cxx . . $ SysFSDataSource#file_..seekgcxx . . $ std/basic_istream#seekg(d37dee3798fd4fd).(0cxx . . $ std/fpos#fpos(1624ee8275b10bda).); |
| 86 | stdcxx . . $ std/::getlinecxx . . $ std/getline(4ec722ed0b69c2ab).(file_cxx . . $ SysFSDataSource#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 readAllcxx . . $ SysFSDataSource#readAll(f5a3f8b4650a788e).(stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> &data) |
| 92 | { |
| 93 | file_cxx . . $ SysFSDataSource#file_..clearcxx . . $ std/basic_ios#clear(3e5fb2eb616d497d).(); |
| 94 | file_cxx . . $ SysFSDataSource#file_..seekgcxx . . $ std/basic_istream#seekg(d37dee3798fd4fd).(0cxx . . $ std/fpos#fpos(1624ee8275b10bda).); |
| 95 | |
| 96 | size_t index = 0; |
| 97 | while (stdcxx . . $ std/::getlinecxx . . $ std/getline(4ec722ed0b69c2ab).(file_cxx . . $ SysFSDataSource#file_., lineData_cxx . . $ SysFSDataSource#lineData_.)) { |
| 98 | if (data.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() == index) |
| 99 | data.emplace_backcxx . . $ std/vector#emplace_back(a1bcdfdabc3a37df).(stdcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).::stringcxx . . $ std/string#()); |
| 100 | |
| 101 | stdcxx . . $ std/::swapcxx . . $ std/swap(fd72003b7956e937).(lineData_cxx . . $ SysFSDataSource#lineData_., data[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).index++]); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | stdcxx . . $ std/::stringcxx . . $ std/string# const path_cxx . . $ SysFSDataSource#path_.; |
| 106 | stdcxx . . $ std/::functioncxx . . $ std/function#<void(Input const &, Output &output)> const transform_cxx . . $ SysFSDataSource#transform_.; |
| 107 | |
| 108 | stdcxx . . $ std/::ifstreamcxx . . $ std/ifstream# file_cxx . . $ SysFSDataSource#file_.; |
| 109 | stdcxx . . $ std/::stringcxx . . $ std/string# lineData_cxx . . $ SysFSDataSource#lineData_.; |
| 110 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> fileData_cxx . . $ SysFSDataSource#fileData_.; |
| 111 | }; |