| 1 | // SPDX-License-Identifier: GPL-3.0-or-latercxx . . $ `<file>/src/common/fileutils.h`/ |
| 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 Utilscxx . . $ Utils/::Filecxx . . $ 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 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<char> readFilecxx . . $ Utils/File/readFile(ca87ac95f82c0a15).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/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 writeFilecxx . . $ Utils/File/writeFile(231cf0731af4b996).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# const &path, stdcxx . . $ std/::vectorcxx . . $ 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 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> readFileLinescxx . . $ Utils/File/readFileLines(951e54d16e2206f).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/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 isFilePathValidcxx . . $ Utils/File/isFilePathValid(c53a6ef3278968b1).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/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 isDirectoryPathValidcxx . . $ Utils/File/isDirectoryPathValid(c53a6ef3278968b1).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/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 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#> searchcxx . . $ Utils/File/search(3ad90eba8943f9ac).(stdcxx . . $ std/::regexcxx . . $ std/__cxx11/regex# const ®ex, |
| 54 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/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 | stdcxx . . $ std/::optionalcxx . . $ std/optional#<stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#> |
| 60 | findHWMonXDirectorycxx . . $ Utils/File/findHWMonXDirectory(9f128ed55dd5b6be).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/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 isSysFSEntryValidcxx . . $ Utils/File/isSysFSEntryValid(c53a6ef3278968b1).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/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 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<char> readQrcFilecxx . . $ Utils/File/readQrcFile(cf0abd84c180245d).(stdcxx . . $ std/::string_viewcxx . . $ std/string_view# qrcPath); |
| 71 | |
| 72 | } // namespace Utils::File |