diff options
Diffstat (limited to 'src/3rdParty/efsw/FileWatcherCWrapper.cpp')
-rwxr-xr-x | src/3rdParty/efsw/FileWatcherCWrapper.cpp | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/3rdParty/efsw/FileWatcherCWrapper.cpp b/src/3rdParty/efsw/FileWatcherCWrapper.cpp new file mode 100755 index 0000000..5c49a66 --- /dev/null +++ b/src/3rdParty/efsw/FileWatcherCWrapper.cpp | |||
@@ -0,0 +1,113 @@ | |||
1 | #include <efsw/efsw.h> | ||
2 | #include <efsw/efsw.hpp> | ||
3 | #include <vector> | ||
4 | |||
5 | #define TOBOOL( i ) ( ( i ) == 0 ? false : true ) | ||
6 | |||
7 | /*************************************************************************************************/ | ||
8 | class Watcher_CAPI : public efsw::FileWatchListener { | ||
9 | public: | ||
10 | efsw_watcher mWatcher; | ||
11 | efsw_pfn_fileaction_callback mFn; | ||
12 | void* mParam; | ||
13 | |||
14 | public: | ||
15 | Watcher_CAPI( efsw_watcher watcher, efsw_pfn_fileaction_callback fn, void* param ) : | ||
16 | mWatcher( watcher ), mFn( fn ), mParam( param ) {} | ||
17 | |||
18 | void handleFileAction( efsw::WatchID watchid, const std::string& dir, | ||
19 | const std::string& filename, efsw::Action action, | ||
20 | std::string oldFilename = "" ) { | ||
21 | mFn( mWatcher, watchid, dir.c_str(), filename.c_str(), (enum efsw_action)action, | ||
22 | oldFilename.c_str(), mParam ); | ||
23 | } | ||
24 | }; | ||
25 | |||
26 | /************************************************************************************************* | ||
27 | * globals | ||
28 | */ | ||
29 | static std::vector<Watcher_CAPI*> g_callbacks; | ||
30 | |||
31 | Watcher_CAPI* find_callback( efsw_watcher watcher, efsw_pfn_fileaction_callback fn ) { | ||
32 | for ( std::vector<Watcher_CAPI*>::iterator i = g_callbacks.begin(); i != g_callbacks.end(); | ||
33 | ++i ) { | ||
34 | Watcher_CAPI* callback = *i; | ||
35 | |||
36 | if ( callback->mFn == fn && callback->mWatcher == watcher ) | ||
37 | return *i; | ||
38 | } | ||
39 | |||
40 | return NULL; | ||
41 | } | ||
42 | |||
43 | Watcher_CAPI* remove_callback( efsw_watcher watcher ) { | ||
44 | std::vector<Watcher_CAPI*>::iterator i = g_callbacks.begin(); | ||
45 | |||
46 | while ( i != g_callbacks.end() ) { | ||
47 | Watcher_CAPI* callback = *i; | ||
48 | |||
49 | if ( callback->mWatcher == watcher ) | ||
50 | i = g_callbacks.erase( i ); | ||
51 | else | ||
52 | ++i; | ||
53 | } | ||
54 | |||
55 | return NULL; | ||
56 | } | ||
57 | |||
58 | /*************************************************************************************************/ | ||
59 | efsw_watcher efsw_create( int generic_mode ) { | ||
60 | return ( efsw_watcher ) new efsw::FileWatcher( TOBOOL( generic_mode ) ); | ||
61 | } | ||
62 | |||
63 | void efsw_release( efsw_watcher watcher ) { | ||
64 | remove_callback( watcher ); | ||
65 | delete (efsw::FileWatcher*)watcher; | ||
66 | } | ||
67 | |||
68 | const char* efsw_getlasterror() { | ||
69 | static std::string log_str; | ||
70 | log_str = efsw::Errors::Log::getLastErrorLog(); | ||
71 | return log_str.c_str(); | ||
72 | } | ||
73 | |||
74 | efsw_watchid efsw_addwatch( efsw_watcher watcher, const char* directory, | ||
75 | efsw_pfn_fileaction_callback callback_fn, int recursive, void* param ) { | ||
76 | Watcher_CAPI* callback = find_callback( watcher, callback_fn ); | ||
77 | |||
78 | if ( callback == NULL ) { | ||
79 | callback = new Watcher_CAPI( watcher, callback_fn, param ); | ||
80 | g_callbacks.push_back( callback ); | ||
81 | } | ||
82 | |||
83 | return ( (efsw::FileWatcher*)watcher ) | ||
84 | ->addWatch( std::string( directory ), callback, TOBOOL( recursive ) ); | ||
85 | } | ||
86 | |||
87 | void efsw_removewatch( efsw_watcher watcher, const char* directory ) { | ||
88 | ( (efsw::FileWatcher*)watcher )->removeWatch( std::string( directory ) ); | ||
89 | } | ||
90 | |||
91 | void efsw_removewatch_byid( efsw_watcher watcher, efsw_watchid watchid ) { | ||
92 | ( (efsw::FileWatcher*)watcher )->removeWatch( watchid ); | ||
93 | } | ||
94 | |||
95 | void efsw_watch( efsw_watcher watcher ) { | ||
96 | ( (efsw::FileWatcher*)watcher )->watch(); | ||
97 | } | ||
98 | |||
99 | void efsw_follow_symlinks( efsw_watcher watcher, int enable ) { | ||
100 | ( (efsw::FileWatcher*)watcher )->followSymlinks( TOBOOL( enable ) ); | ||
101 | } | ||
102 | |||
103 | int efsw_follow_symlinks_isenabled( efsw_watcher watcher ) { | ||
104 | return (int)( (efsw::FileWatcher*)watcher )->followSymlinks(); | ||
105 | } | ||
106 | |||
107 | void efsw_allow_outofscopelinks( efsw_watcher watcher, int allow ) { | ||
108 | ( (efsw::FileWatcher*)watcher )->allowOutOfScopeLinks( TOBOOL( allow ) ); | ||
109 | } | ||
110 | |||
111 | int efsw_outofscopelinks_isallowed( efsw_watcher watcher ) { | ||
112 | return (int)( (efsw::FileWatcher*)watcher )->allowOutOfScopeLinks(); | ||
113 | } | ||