-
Notifications
You must be signed in to change notification settings - Fork 0
/
ofxFileWatcher.h
47 lines (41 loc) · 1.27 KB
/
ofxFileWatcher.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "ofMain.h"
class ofxFileWatcher {
public:
ofxFileWatcher() : mFileExists(false), mLastCheck(0.0f) {}
void setFile(const of::filesystem::path& filePath) {
mFilePath = ofToDataPath(filePath);
ofLogNotice("FileWatcher") << "Watching file: " << mFilePath;
if (!std::filesystem::exists(mFilePath)) {
ofLogError("FileWatcher") << "File does not exist: " << mFilePath;
mFileExists = false;
} else {
mLastModified = std::filesystem::last_write_time(mFilePath);
mFileExists = true;
}
}
bool hasFileChanged() {
if (ofGetElapsedTimef() - mInterval > mLastCheck) {
mLastCheck = ofGetElapsedTimef();
ofLogVerbose("FileWatcher") << "Checking for file changes...";
if (!mFileExists) {
return false;
}
auto currentLastModified = std::filesystem::last_write_time(mFilePath);
if (currentLastModified != mLastModified) {
ofLogVerbose("FileWatcher") << "Detected file change.";
mLastModified = currentLastModified;
return true;
}
}
return false;
}
void setInterval(float interval) {
mInterval = interval;
}
private:
std::string mFilePath;
std::filesystem::file_time_type mLastModified;
bool mFileExists;
float mLastCheck;
float mInterval = 3.0f;
};