Unravel Engine C++ Reference
Loading...
Searching...
No Matches
watcher.cpp
Go to the documentation of this file.
1#include "watcher.h"
4
5namespace fs
6{
7
8namespace
9{
10// #ifndef WIN32
11// #define FS_WATCHER_WTR 1
12// #endif
13
14#ifdef FS_WATCHER_WTR
15using watcher_impl = watcher_wtr;
16#else
17using watcher_impl = watcher_fallback;
18#endif
19
20auto get_watcher() -> watcher_impl&
21{
22 static watcher_impl wd;
23 return wd;
24}
25} // namespace
26
27auto watcher::watch(const fs::path& path,
28 const pattern_filter& filter,
29 bool recursive,
30 bool initial_list,
31 clock_t::duration poll_interval,
32 notify_callback callback,
33 const std::string& watcher_name) -> std::uint64_t
34{
35 auto& wd = get_watcher();
36
37 auto id = wd.watch_impl(path, filter, recursive, initial_list, poll_interval, std::move(callback), watcher_name);
38 if(!wd.watching_)
39 {
40 wd.start();
41 }
42 return id;
43}
44
45void watcher::unwatch(std::uint64_t key)
46{
47 auto& wd = get_watcher();
48 wd.unwatch_impl(key);
49}
50
52{
53 auto& wd = get_watcher();
54 wd.unwatch_all_impl();
55}
56
57void watcher::touch(const fs::path& path, bool recursive, fs::file_time_type time)
58{
59 fs::error_code err;
60 if(fs::exists(path, err))
61 {
62 if(fs::is_directory(path, err))
63 {
64 if(recursive)
65 {
66 for(auto& entry : fs::recursive_directory_iterator(path, err))
67 {
68 fs::last_write_time(entry.path(), time, err);
69 }
70 }
71 else
72 {
73 for(auto& entry : fs::directory_iterator(path, err))
74 {
75 fs::last_write_time(entry.path(), time, err);
76 }
77 }
78 fs::last_write_time(path, time, err);
79 }
80 else
81 {
82 fs::last_write_time(path, time, err);
83 }
84 }
85}
86
88{
89 auto& wd = get_watcher();
90 wd.pause();
91}
92
94{
95 auto& wd = get_watcher();
96 wd.resume();
97}
98
99auto to_string(const watcher::entry& e) -> std::string
100{
101 static auto file_type_to_string = [](file_type type) -> std::string
102 {
103 switch(type)
104 {
105 case file_type::regular:
106 return "file";
107 case file_type::directory:
108 return "directory";
109 default:
110 return "other";
111 }
112 };
113
114 static auto status_to_string = [](watcher::entry_status status) -> std::string
115 {
116 switch(status)
117 {
119 return "created";
121 return "modified";
123 return "removed";
125 return "renamed";
126 default:
127 return "unmodified";
128 }
129 };
130
131
132 std::stringstream ss;
133 ss << "{\"" << int64_t(e.last_mod_time.time_since_epoch().count()) << "\":[";
134 if(e.status == watcher::entry_status::renamed)
135 {
136 ss << e.last_path << " -> ";
137 }
138 ss << e.path;
139 ss << "," << file_type_to_string(e.type);
140 ss << "," << status_to_string(e.status);
141
142 ss << "]}";
143 return ss.str();
144}
145} // namespace fs
A filter that combines include and exclude patterns for file/directory filtering.
static void unwatch(std::uint64_t key)
Un-watches a previously registered file or directory.
Definition watcher.cpp:45
static void resume()
Resumes all watchers.
Definition watcher.cpp:93
std::function< void(const std::vector< entry > &, bool)> notify_callback
Definition watcher.h:41
static void pause()
Pauses all watchers.
Definition watcher.cpp:87
static void unwatch_all()
Un-watches all previously registered file or directory.
Definition watcher.cpp:51
static void touch(const fs::path &path, bool recursive, fs::file_time_type time=fs::now())
Sets the last modification time of a file or directory. by default sets the time to the current time.
Definition watcher.cpp:57
static auto watch(const fs::path &path, const pattern_filter &filter, bool recursive, bool initial_list, clock_t::duration poll_interval, notify_callback callback, const std::string &watcher_name="") -> std::uint64_t
Watches a file or directory for modification and call back the specified std::function....
Definition watcher.cpp:27
const char * id
texture_job_type type
Definition cache.hpp:11
auto to_string(const watcher::entry &e) -> std::string
Definition watcher.cpp:99