Repo-Init
 
Loading...
Searching...
No Matches
FileHelpers.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <filesystem>
5#include <fstream>
6#include <regex>
7#include <string>
8#include <sys/inotify.h>
9#include <thread>
10#include <vector>
11
19inline std::vector<std::string> findFromFile(const std::filesystem::path &filePath, const std::string &pattern,
20 std::string &lastWord)
21{
22 const std::regex regExp(pattern);
23 std::ifstream inFile(filePath);
24 std::vector<std::string> matchedLines;
25
26 std::string readLine;
27 while (getline(inFile, readLine))
28 {
29 if (std::regex_search(readLine, regExp))
30 {
31 matchedLines.push_back(readLine);
32 }
33 }
34
35 if (!matchedLines.empty())
36 {
37 auto pos = matchedLines.front().find_last_of(' ');
38 if (pos != std::string::npos && pos != matchedLines.front().size())
39 {
40 lastWord = matchedLines.front().substr(pos + 1);
41 }
42 }
43
44 return matchedLines;
45}
46
53inline std::vector<std::string> findFromFile(const std::filesystem::path &filePath, const std::string &pattern)
54{
55 std::string lastWord;
56 return findFromFile(filePath, pattern, lastWord);
57}
58
60using FNotifyCallback = std::function<void(const void *)>;
61
66 private:
68 int _fDescriptor{-1};
70 int _wDescriptor{-1};
72 std::filesystem::path _filePath;
76 uint32_t _notifyEvents;
78 const void *_userPtr = nullptr;
79
81 std::unique_ptr<std::jthread> _thread;
82
83 void threadFunc(const std::stop_token &stopToken) const noexcept;
84
85 public:
91 explicit FileMonitor(std::filesystem::path filePath, uint32_t notifyEvents = IN_MODIFY);
92
94 FileMonitor(const FileMonitor & /*unused*/) = delete;
95
97 FileMonitor(FileMonitor && /*unused*/) = delete;
98
100 FileMonitor &operator=(FileMonitor /*unused*/) = delete;
101
103 FileMonitor &operator=(FileMonitor && /*unused*/) = delete;
104
105 [[nodiscard]] FNotifyCallback notifyCallback() const { return _notifyCallback; }
106 void notifyCallback(FNotifyCallback func) { _notifyCallback = std::move(func); }
107
112 void userPtr(const void *ptr) { _userPtr = ptr; }
113
117 ~FileMonitor();
118};
std::function< void(const void *)> FNotifyCallback
Callback function for file notifications.
std::vector< std::string > findFromFile(const std::filesystem::path &filePath, const std::string &pattern, std::string &lastWord)
const void * _userPtr
User pointer.
FileMonitor(const FileMonitor &)=delete
Copy constructor.
void threadFunc(const std::stop_token &stopToken) const noexcept
FNotifyCallback _notifyCallback
Callback function.
int _fDescriptor
File descriptor.
void notifyCallback(FNotifyCallback func)
void userPtr(const void *ptr)
FileMonitor(std::filesystem::path filePath, uint32_t notifyEvents=IN_MODIFY)
FileMonitor(FileMonitor &&)=delete
Move constructor.
FNotifyCallback notifyCallback() const
int _wDescriptor
Watch descriptor.
uint32_t _notifyEvents
Notify types.
FileMonitor & operator=(FileMonitor)=delete
Copy assignment operator.
std::filesystem::path _filePath
File path.
FileMonitor & operator=(FileMonitor &&)=delete
Move assignment operator.
std::unique_ptr< std::jthread > _thread
Thread.