Notify when changes are made to a file or directory.
ofxWatchPath(path, callback, [option], [retain])
ofxWatchPath(path, loader, callback, [option], [retain])
A path is specified as a relative or absolute path from the data folder.
It can be a file or a directory or a path with wildcards.
A wildcard is *
(matches to any string) or **
(matches to any directory recursively).
If the path is for a directory, it watches all of the children.
(Example)
If there are these files...
"videos/highQ/recorded.mov"
"videos/highQ/recorded.mp4"
"videos/lowQ/half.mov"
"videos/lowQ/sequence.mov"
"settings/playback/sequence.json"
"videos/*/*.mov"
matches to1,3,4
"**/highQ"
matches to1,2
"**/sequence*"
matches to4,5
"**"
matches to1,2,3,4,5
When changes are made to a file or directory while watching, the callback is called.
Callback functions can take the following 4 forms.
You can use function pointer or lambda as well.
callback(void)
callback(const std::filesystem::path&)
callback(const MediumType&)
callback(const MediumType&, const std::filesystem::path&)
MediumType
is a product of loader function.
Loader function modifies a path into any types.
It is mainly intended to be used for loading the file.
(Example)
ofxWatchPath("settings/*.json", ofLoadJson, [](const ofJson &json) {
std::cout << json.dump(2) << std::endl;
});
Tips
If you want to use an existing function that expects arguments other than filepath or a member function, consider using std::bind
.
ofxWatcherOption option;
option.check_interval = 10;
option.file_type_flag = ofx::watcher::REGULAR | ofx::watcher::DIRECTORY;
option.absolute_path = true;
option.finder_option.excludes = {".DS_Store",".git"};
option.finder_option.allow_ext = {".png",".jpg"};
Check for modifies at this interval in seconds.
default: 1
Only checks files or directories of this type.
See FileTypeFlag
in ofxWatcher.h
default: REGULAR
If true, callback or loader is called with absolute path.
default: false
Excludes files or directories that has a name in this vector.
An exact match is required.
default: [".DS_Store"]
If specified, files with unspecified extensions are ignored.
Each ext should start with .
.
default: (empty)
If true, ofxWatcher keeps the shared_ptr
created by ofxWatchPath
.
If you want to control the object's lifecycle, call ofxWatchPath
with retain=false
.
default: true