| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <filesystem> |
| 7 | #include <optional> |
| 8 | #include <regex> |
| 9 | #include <string> |
| 10 | #include <string_view> |
| 11 | #include <vector> |
| 12 | |
| 13 | namespace Utils::File { |
| 14 | |
| 15 | /// Reads the contents of a file. |
| 16 | /// @param path path to the file to read |
| 17 | /// @returns file contents or no contents on error |
| 18 | /// |
| 19 | /// @note Reading /sys files will produce more data than expected as |
| 20 | /// its file size doesn't match the actual file contents. |
| 21 | /// @note Reading /proc files won't produce any data. Those files must |
| 22 | /// be readed line by line. Use readFileLines instead. |
| 23 | std::vector<char> readFile(std::filesystem::path const &path); |
| 24 | |
| 25 | /// Writes data to a file. Any previous file contents will be destroyed. |
| 26 | /// @param path path to the file to read |
| 27 | /// @param data to be written |
| 28 | /// @returns true on success |
| 29 | bool writeFile(std::filesystem::path const &path, std::vector<char> const &data); |
| 30 | |
| 31 | /// Read the data of a file line by line. Contents will be splitted in |
| 32 | /// lines using a delimiter. |
| 33 | /// @param path file to read |
| 34 | /// @param delimiter new line character |
| 35 | /// @returns lines of data |
| 36 | std::vector<std::string> readFileLines(std::filesystem::path const &path, |
| 37 | char delim = '\n'); |
| 38 | |
| 39 | /// Checks if a path exists and points to a file. |
| 40 | /// @param path file to validate |
| 41 | /// @returns true if the path points to a valid file |
| 42 | bool isFilePathValid(std::filesystem::path const &path); |
| 43 | |
| 44 | /// Checks if a path exists and points to a directory. |
| 45 | /// @param path directory to validate |
| 46 | /// @returns true if the path points to a valid directory |
| 47 | bool isDirectoryPathValid(std::filesystem::path const &path); |
| 48 | |
| 49 | /// Search for files or directories whose name matches a regular expression. |
| 50 | /// @param regex regular expression to match the directory entries |
| 51 | /// @param path target directory to search |
| 52 | /// @returns path to the directory entries that match the regular expression |
| 53 | std::vector<std::filesystem::path> search(std::regex const ®ex, |
| 54 | std::filesystem::path const &path); |
| 55 | |
| 56 | /// Find the hwmon[0-*] directory in a path. |
| 57 | /// @param path directory to seach for hwmon[0-*] directory |
| 58 | /// @returns path to hwmon[0-*] directory |
| 59 | std::optional<std::filesystem::path> |
| 60 | findHWMonXDirectory(std::filesystem::path const &path); |
| 61 | |
| 62 | /// Checks if a path points to an existing and non empty sysfs entry file. |
| 63 | /// @param path file to validate |
| 64 | /// @return true if the path points to a valid sysfs entry |
| 65 | bool isSysFSEntryValid(std::filesystem::path const &path); |
| 66 | |
| 67 | /// Read file data from a Qt qrc path. |
| 68 | /// @param path rcc file path |
| 69 | /// @return file contents or no contents on error |
| 70 | std::vector<char> readQrcFile(std::string_view qrcPath); |
| 71 | |
| 72 | } // namespace Utils::File |