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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
#include <efsw/efsw.h>
#include <efsw/efsw.hpp>
#include <vector>
#define TOBOOL( i ) ( ( i ) == 0 ? false : true )
/*************************************************************************************************/
class Watcher_CAPI : public efsw::FileWatchListener {
public:
efsw_watcher mWatcher;
efsw_pfn_fileaction_callback mFn;
void* mParam;
public:
Watcher_CAPI( efsw_watcher watcher, efsw_pfn_fileaction_callback fn, void* param ) :
mWatcher( watcher ), mFn( fn ), mParam( param ) {}
void handleFileAction( efsw::WatchID watchid, const std::string& dir,
const std::string& filename, efsw::Action action,
std::string oldFilename = "" ) {
mFn( mWatcher, watchid, dir.c_str(), filename.c_str(), (enum efsw_action)action,
oldFilename.c_str(), mParam );
}
};
/*************************************************************************************************
* globals
*/
static std::vector<Watcher_CAPI*> g_callbacks;
Watcher_CAPI* find_callback( efsw_watcher watcher, efsw_pfn_fileaction_callback fn ) {
for ( std::vector<Watcher_CAPI*>::iterator i = g_callbacks.begin(); i != g_callbacks.end();
++i ) {
Watcher_CAPI* callback = *i;
if ( callback->mFn == fn && callback->mWatcher == watcher )
return *i;
}
return NULL;
}
Watcher_CAPI* remove_callback( efsw_watcher watcher ) {
std::vector<Watcher_CAPI*>::iterator i = g_callbacks.begin();
while ( i != g_callbacks.end() ) {
Watcher_CAPI* callback = *i;
if ( callback->mWatcher == watcher )
i = g_callbacks.erase( i );
else
++i;
}
return NULL;
}
/*************************************************************************************************/
efsw_watcher efsw_create( int generic_mode ) {
return ( efsw_watcher ) new efsw::FileWatcher( TOBOOL( generic_mode ) );
}
void efsw_release( efsw_watcher watcher ) {
remove_callback( watcher );
delete (efsw::FileWatcher*)watcher;
}
const char* efsw_getlasterror() {
static std::string log_str;
log_str = efsw::Errors::Log::getLastErrorLog();
return log_str.c_str();
}
efsw_watchid efsw_addwatch( efsw_watcher watcher, const char* directory,
efsw_pfn_fileaction_callback callback_fn, int recursive, void* param ) {
Watcher_CAPI* callback = find_callback( watcher, callback_fn );
if ( callback == NULL ) {
callback = new Watcher_CAPI( watcher, callback_fn, param );
g_callbacks.push_back( callback );
}
return ( (efsw::FileWatcher*)watcher )
->addWatch( std::string( directory ), callback, TOBOOL( recursive ) );
}
void efsw_removewatch( efsw_watcher watcher, const char* directory ) {
( (efsw::FileWatcher*)watcher )->removeWatch( std::string( directory ) );
}
void efsw_removewatch_byid( efsw_watcher watcher, efsw_watchid watchid ) {
( (efsw::FileWatcher*)watcher )->removeWatch( watchid );
}
void efsw_watch( efsw_watcher watcher ) {
( (efsw::FileWatcher*)watcher )->watch();
}
void efsw_follow_symlinks( efsw_watcher watcher, int enable ) {
( (efsw::FileWatcher*)watcher )->followSymlinks( TOBOOL( enable ) );
}
int efsw_follow_symlinks_isenabled( efsw_watcher watcher ) {
return (int)( (efsw::FileWatcher*)watcher )->followSymlinks();
}
void efsw_allow_outofscopelinks( efsw_watcher watcher, int allow ) {
( (efsw::FileWatcher*)watcher )->allowOutOfScopeLinks( TOBOOL( allow ) );
}
int efsw_outofscopelinks_isallowed( efsw_watcher watcher ) {
return (int)( (efsw::FileWatcher*)watcher )->allowOutOfScopeLinks();
}
|