Bump CMake minimum to 4.0 for VoidLibrary toolchain
This commit is contained in:
1 parent
0fa9db4691
commit
05c9f8bc6c
11 files changed
+2524
-2125
No files matched your search
+79
-75
@@ -1,94 +1,98 @@
|
||||
#include "echoion/plugin/FileWatcher.hpp"
|
||||
#include <chrono>
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
|
||||
namespace Echoion::Plugin {
|
||||
namespace Echoion::Plugin
|
||||
{
|
||||
|
||||
FileWatcher::FileWatcher() = default;
|
||||
FileWatcher::FileWatcher() = default;
|
||||
|
||||
FileWatcher::~FileWatcher() {
|
||||
stopWatching();
|
||||
}
|
||||
FileWatcher::~FileWatcher() { stopWatching(); }
|
||||
|
||||
void FileWatcher::startWatching(
|
||||
const std::filesystem::path& filePath,
|
||||
ChangeCallback callback,
|
||||
std::chrono::milliseconds intervalMs) {
|
||||
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
if (watching_.load()) {
|
||||
stopWatching();
|
||||
}
|
||||
void FileWatcher::startWatching(const std::filesystem::path& filePath,
|
||||
ChangeCallback callback,
|
||||
std::chrono::milliseconds intervalMs)
|
||||
{
|
||||
|
||||
watchedPath_ = filePath;
|
||||
callback_ = std::move(callback);
|
||||
intervalMs_ = intervalMs;
|
||||
stopRequested_ = false;
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
// Get initial file state
|
||||
if (std::filesystem::exists(watchedPath_)) {
|
||||
lastWriteTime_ = std::filesystem::last_write_time(watchedPath_);
|
||||
lastFileSize_ = std::filesystem::file_size(watchedPath_);
|
||||
}
|
||||
if (watching_.load()) {
|
||||
stopWatching();
|
||||
}
|
||||
|
||||
watching_ = true;
|
||||
watchThread_ = std::thread(&FileWatcher::watchLoop, this);
|
||||
}
|
||||
watchedPath_ = filePath;
|
||||
callback_ = std::move(callback);
|
||||
intervalMs_ = intervalMs;
|
||||
stopRequested_ = false;
|
||||
|
||||
void FileWatcher::stopWatching() {
|
||||
stopRequested_ = true;
|
||||
|
||||
if (watchThread_.joinable()) {
|
||||
watchThread_.join();
|
||||
}
|
||||
|
||||
watching_ = false;
|
||||
}
|
||||
// Get initial file state
|
||||
if (std::filesystem::exists(watchedPath_)) {
|
||||
lastWriteTime_ = std::filesystem::last_write_time(watchedPath_);
|
||||
lastFileSize_ = std::filesystem::file_size(watchedPath_);
|
||||
}
|
||||
|
||||
bool FileWatcher::isWatching() const noexcept {
|
||||
return watching_.load();
|
||||
}
|
||||
watching_ = true;
|
||||
watchThread_ = std::thread(&FileWatcher::watchLoop, this);
|
||||
}
|
||||
|
||||
void FileWatcher::watchLoop() {
|
||||
while (!stopRequested_.load()) {
|
||||
// Sleep first to avoid checking immediately
|
||||
std::this_thread::sleep_for(intervalMs_);
|
||||
|
||||
if (stopRequested_.load()) {
|
||||
break;
|
||||
}
|
||||
void FileWatcher::stopWatching()
|
||||
{
|
||||
stopRequested_ = true;
|
||||
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
if (!std::filesystem::exists(watchedPath_)) {
|
||||
continue;
|
||||
}
|
||||
if (watchThread_.joinable()) {
|
||||
watchThread_.join();
|
||||
}
|
||||
|
||||
try {
|
||||
auto currentWriteTime = std::filesystem::last_write_time(watchedPath_);
|
||||
auto currentFileSize = std::filesystem::file_size(watchedPath_);
|
||||
watching_ = false;
|
||||
}
|
||||
|
||||
// Check if file has changed (time or size)
|
||||
if (currentWriteTime != lastWriteTime_ || currentFileSize != lastFileSize_) {
|
||||
// Update stored values
|
||||
lastWriteTime_ = currentWriteTime;
|
||||
lastFileSize_ = currentFileSize;
|
||||
bool FileWatcher::isWatching() const noexcept { return watching_.load(); }
|
||||
|
||||
// Trigger callback
|
||||
if (callback_) {
|
||||
try {
|
||||
callback_();
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << std::format("FileWatcher callback error: {}", e.what()) << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
// Ignore errors (file might be locked during write)
|
||||
}
|
||||
}
|
||||
}
|
||||
void FileWatcher::watchLoop()
|
||||
{
|
||||
while (!stopRequested_.load()) {
|
||||
// Sleep first to avoid checking immediately
|
||||
std::this_thread::sleep_for(intervalMs_);
|
||||
|
||||
if (stopRequested_.load()) {
|
||||
break;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
if (!std::filesystem::exists(watchedPath_)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
auto currentWriteTime =
|
||||
std::filesystem::last_write_time(watchedPath_);
|
||||
auto currentFileSize = std::filesystem::file_size(watchedPath_);
|
||||
|
||||
// Check if file has changed (time or size)
|
||||
if (currentWriteTime != lastWriteTime_ ||
|
||||
currentFileSize != lastFileSize_) {
|
||||
// Update stored values
|
||||
lastWriteTime_ = currentWriteTime;
|
||||
lastFileSize_ = currentFileSize;
|
||||
|
||||
// Trigger callback
|
||||
if (callback_) {
|
||||
try {
|
||||
callback_();
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr
|
||||
<< std::format("FileWatcher callback error: {}",
|
||||
e.what())
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
// Ignore errors (file might be locked during write)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Echoion::Plugin
|
||||
Reference in new issue
Block a user