2009-02-27 21:49:31 +00:00
|
|
|
/**
|
|
|
|
* A C wrapper around James Wynn's FileWatcher library.
|
|
|
|
*
|
|
|
|
* Released under a free dont-bother-me license. I don't claim this
|
|
|
|
* software won't destroy everything that you hold dear, but I really
|
|
|
|
* doubt it will. And please try not to take credit for others' work.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include "file-watcher.h"
|
|
|
|
#include "FileWatcher.h"
|
|
|
|
|
|
|
|
using namespace FW;
|
|
|
|
|
|
|
|
struct CFW_Impl: public FileWatchListener
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
FileWatcher myWatcher;
|
|
|
|
WatchID myID;
|
|
|
|
CFW_ActionCallback * myCallback;
|
|
|
|
void * myCallbackData;
|
|
|
|
|
|
|
|
public:
|
2009-02-28 14:43:37 +00:00
|
|
|
CFW_Impl( const char * dir,
|
|
|
|
CFW_ActionCallback * callback,
|
|
|
|
void * callbackData ):
|
2009-02-27 21:49:31 +00:00
|
|
|
myID( myWatcher.addWatch( dir, this ) ),
|
|
|
|
myCallback( callback ),
|
|
|
|
myCallbackData( callbackData )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~CFW_Impl( )
|
|
|
|
{
|
|
|
|
myWatcher.removeWatch( myID );
|
|
|
|
}
|
2009-02-28 14:43:37 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
virtual void handleFileAction( WatchID watchid,
|
|
|
|
const String & dir,
|
|
|
|
const String & filename,
|
|
|
|
FileWatcher::Action action )
|
2009-02-27 21:49:31 +00:00
|
|
|
{
|
2009-02-28 14:43:37 +00:00
|
|
|
(*myCallback)( this,
|
|
|
|
dir.c_str(),
|
|
|
|
filename.c_str(),
|
|
|
|
(CFW_Action)action,
|
|
|
|
myCallbackData );
|
2009-02-27 21:49:31 +00:00
|
|
|
}
|
|
|
|
void update( )
|
|
|
|
{
|
|
|
|
myWatcher.update( );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-02-28 14:43:37 +00:00
|
|
|
extern "C"
|
2009-02-27 21:49:31 +00:00
|
|
|
{
|
2009-02-28 14:43:37 +00:00
|
|
|
CFW_Watch*
|
|
|
|
cfw_addWatch( const char * directory, CFW_ActionCallback * callback, void * callbackData )
|
|
|
|
{
|
|
|
|
return new CFW_Impl( directory, callback, callbackData );
|
|
|
|
}
|
2009-02-27 21:49:31 +00:00
|
|
|
|
2009-02-28 14:43:37 +00:00
|
|
|
void
|
|
|
|
cfw_removeWatch( CFW_Watch * watch )
|
|
|
|
{
|
|
|
|
if( watch != 0 )
|
|
|
|
delete watch;
|
|
|
|
}
|
2009-02-27 21:49:31 +00:00
|
|
|
|
2009-02-28 14:43:37 +00:00
|
|
|
void
|
|
|
|
cfw_update( CFW_Watch * watch )
|
|
|
|
{
|
|
|
|
if( watch != 0 )
|
|
|
|
watch->update( );
|
|
|
|
}
|
2009-02-27 21:49:31 +00:00
|
|
|
}
|