diff options
author | Shauren <shauren.trinity@gmail.com> | 2024-05-29 12:23:06 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2024-05-29 12:23:06 +0200 |
commit | 937e61853319ac0e5b29f0bcbe5390a71cb7625f (patch) | |
tree | 3297cd17cfea2445dca66926975f9e09f3dfb654 /dep/efsw/src/test | |
parent | 67cb7369501780e2120c79b447619052da63d2fa (diff) |
Dep/efsw: Update to SpartanJ/efsw@36c1c7004a34b6f40719f0830bcfb10325415451
Diffstat (limited to 'dep/efsw/src/test')
-rw-r--r-- | dep/efsw/src/test/efsw-test.c | 164 | ||||
-rw-r--r-- | dep/efsw/src/test/efsw-test.cpp | 126 |
2 files changed, 221 insertions, 69 deletions
diff --git a/dep/efsw/src/test/efsw-test.c b/dep/efsw/src/test/efsw-test.c new file mode 100644 index 00000000000..54a3e21bba5 --- /dev/null +++ b/dep/efsw/src/test/efsw-test.c @@ -0,0 +1,164 @@ +#include <efsw/efsw.h> + +#include <signal.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <time.h> + +#ifdef _WIN32 + #include <Windows.h> +#else + #include <unistd.h> +#endif + +const char PATH_SEPARATOR = +#ifdef _WIN32 + '\\'; +#else + '/'; +#endif + +bool STOP = false; + +void sigend( int sig ) { + printf( "Bye bye" ); + STOP = true; +} + +void sleepMsecs( int msecs ) { +#ifdef _WIN32 + Sleep( msecs ); +#else + sleep( msecs ); +#endif +} + +const char * getActionName( enum efsw_action action ) { + switch ( action ) { + case EFSW_ADD: + return "Add"; + case EFSW_MODIFIED: + return "Modified"; + case EFSW_DELETE: + return "Delete"; + case EFSW_MOVED: + return "Moved"; + default: + return "Bad Action"; + } +} + +void handleFileAction( efsw_watcher watcher, efsw_watchid watchid, + const char* dir, const char* filename, + enum efsw_action action, const char* oldFilename, + void* param ) { + if ( strlen( oldFilename ) == 0 ) { + printf( "Watch ID %ld DIR (%s) FILE (%s) has event %s\n", + watchid, dir, filename, getActionName( action )); + } else { + printf( "Watch ID %ld DIR (%s) FILE (from file %s to %s) has event %s\n", + watchid, dir, oldFilename, filename, getActionName( action )); + } +} + +efsw_watchid handleWatchID( efsw_watchid watchid ) { + switch ( watchid ) { + case EFSW_NOTFOUND: + case EFSW_REPEATED: + case EFSW_OUTOFSCOPE: + case EFSW_REMOTE: + case EFSW_WATCHER_FAILED: + case EFSW_UNSPECIFIED: { + printf( "%s\n", efsw_getlasterror() ); + break; + } + default: { + printf( "Added WatchID: %ld\n", watchid ); + } + } + + return watchid; +} + +int main( int argc, char** argv ) { + signal( SIGABRT, sigend ); + signal( SIGINT, sigend ); + signal( SIGTERM, sigend ); + + printf("Press ^C to exit demo\n"); + + bool commonTest = true; + bool useGeneric = false; + char *path = 0; + + if ( argc >= 2 ) { + path = argv[1]; + + struct stat s; + if( stat(path,&s) == 0 && (s.st_mode & S_IFDIR) == S_IFDIR ) { + commonTest = false; + } + + if ( argc >= 3 ) { + if ( strcmp( argv[2], "true" ) == 0 ) { + useGeneric = true; + } + } + } + + /// create the file watcher object + efsw_watcher fileWatcher = efsw_create( useGeneric ); + efsw_follow_symlinks( fileWatcher, false ); + efsw_allow_outofscopelinks( fileWatcher, false ); + + if ( commonTest ) { + char cwd[256]; + getcwd( cwd, sizeof(cwd) ); + printf( "CurPath: %s\n", cwd ); + + /// starts watching + efsw_watch( fileWatcher ); + + /// add a watch to the system + char path1[256]; + sprintf(path1, "%s%ctest", cwd, PATH_SEPARATOR ); + handleWatchID( efsw_addwatch_withoptions( fileWatcher, path1, handleFileAction, true, 0, 0, 0 ) ); + + /// adds another watch after started watching... + sleepMsecs( 100 ); + + char path2[256]; + sprintf(path2, "%s%ctest2", cwd, PATH_SEPARATOR ); + efsw_watchid watchID = handleWatchID( + efsw_addwatch_withoptions( fileWatcher, path2, handleFileAction, true, 0, 0, 0 ) ); + + /// delete the watch + if ( watchID > 0 ) { + sleepMsecs( 1000 ); + efsw_removewatch_byid( fileWatcher, watchID ); + } + } else { + if ( efsw_addwatch( fileWatcher, path, handleFileAction, true, 0 ) > 0 ) { + efsw_watch( fileWatcher ); + + printf( "Watching directory: %s\n", path ); + + if ( useGeneric ) { + printf( "Using generic backend watcher\n" ); + } + } else { + printf( "Error trying to watch directory: %s\n", path ); + printf( "%s\n", efsw_getlasterror() ); + } + } + + while ( !STOP ) { + sleepMsecs( 100 ); + } + + efsw_release( fileWatcher ); + + return 0; +} diff --git a/dep/efsw/src/test/efsw-test.cpp b/dep/efsw/src/test/efsw-test.cpp index a49e3414fcc..d51d68d7599 100644 --- a/dep/efsw/src/test/efsw-test.cpp +++ b/dep/efsw/src/test/efsw-test.cpp @@ -1,56 +1,59 @@ -#include <efsw/efsw.hpp> -#include <efsw/System.hpp> #include <efsw/FileSystem.hpp> -#include <signal.h> +#include <efsw/System.hpp> +#include <efsw/efsw.hpp> #include <iostream> +#include <signal.h> bool STOP = false; -void sigend(int signal) -{ +void sigend( int ) { std::cout << std::endl << "Bye bye" << std::endl; STOP = true; } /// Processes a file action -class UpdateListener : public efsw::FileWatchListener -{ - public: - UpdateListener() {} - - std::string getActionName( efsw::Action action ) - { - switch ( action ) - { - case efsw::Actions::Add: return "Add"; - case efsw::Actions::Modified: return "Modified"; - case efsw::Actions::Delete: return "Delete"; - case efsw::Actions::Moved: return "Moved"; - default: return "Bad Action"; - } +class UpdateListener : public efsw::FileWatchListener { + public: + UpdateListener() {} + + std::string getActionName( efsw::Action action ) { + switch ( action ) { + case efsw::Actions::Add: + return "Add"; + case efsw::Actions::Modified: + return "Modified"; + case efsw::Actions::Delete: + return "Delete"; + case efsw::Actions::Moved: + return "Moved"; + default: + return "Bad Action"; } + } - void handleFileAction( efsw::WatchID watchid, const std::string& dir, const std::string& filename, efsw::Action action, std::string oldFilename = "" ) - { - std::cout << "DIR (" << dir + ") FILE (" + ( oldFilename.empty() ? "" : "from file " + oldFilename + " to " ) + filename + ") has event " << getActionName( action ) << std::endl; - } + void handleFileAction( efsw::WatchID watchid, const std::string& dir, + const std::string& filename, efsw::Action action, + std::string oldFilename = "" ) override { + std::cout << "Watch ID " << watchid << " DIR (" + << dir + ") FILE (" + + ( oldFilename.empty() ? "" : "from file " + oldFilename + " to " ) + + filename + ") has event " + << getActionName( action ) << std::endl; + } }; -efsw::WatchID handleWatchID( efsw::WatchID watchid ) -{ - switch ( watchid ) - { +efsw::WatchID handleWatchID( efsw::WatchID watchid ) { + switch ( watchid ) { case efsw::Errors::FileNotFound: case efsw::Errors::FileRepeated: case efsw::Errors::FileOutOfScope: case efsw::Errors::FileRemote: - case efsw::Errors::Unspecified: - { + case efsw::Errors::WatcherFailed: + case efsw::Errors::Unspecified: { std::cout << efsw::Errors::Log::getLastErrorLog().c_str() << std::endl; break; } - default: - { + default: { std::cout << "Added WatchID: " << watchid << std::endl; } } @@ -58,11 +61,10 @@ efsw::WatchID handleWatchID( efsw::WatchID watchid ) return watchid; } -int main(int argc, char **argv) -{ - signal( SIGABRT , sigend ); - signal( SIGINT , sigend ); - signal( SIGTERM , sigend ); +int main( int argc, char** argv ) { + signal( SIGABRT, sigend ); + signal( SIGINT, sigend ); + signal( SIGTERM, sigend ); std::cout << "Press ^C to exit demo" << std::endl; @@ -70,25 +72,21 @@ int main(int argc, char **argv) bool useGeneric = false; std::string path; - if ( argc >= 2 ) - { + if ( argc >= 2 ) { path = std::string( argv[1] ); - if ( efsw::FileSystem::isDirectory( path ) ) - { - commonTest = false; + if ( efsw::FileSystem::isDirectory( path ) ) { + commonTest = false; } - if ( argc >= 3 ) - { - if ( std::string( argv[2] ) == "true" ) - { + if ( argc >= 3 ) { + if ( std::string( argv[2] ) == "true" ) { useGeneric = true; } } } - UpdateListener * ul = new UpdateListener(); + UpdateListener* ul = new UpdateListener(); /// create the file watcher object efsw::FileWatcher fileWatcher( useGeneric ); @@ -96,54 +94,44 @@ int main(int argc, char **argv) fileWatcher.followSymlinks( false ); fileWatcher.allowOutOfScopeLinks( false ); - if ( commonTest ) - { + if ( commonTest ) { std::string CurPath( efsw::System::getProcessPath() ); std::cout << "CurPath: " << CurPath.c_str() << std::endl; + /// starts watching + fileWatcher.watch(); + /// add a watch to the system handleWatchID( fileWatcher.addWatch( CurPath + "test", ul, true ) ); - /// starts watching - fileWatcher.watch(); - /// adds another watch after started watching... efsw::System::sleep( 100 ); - efsw::WatchID watchID = handleWatchID( fileWatcher.addWatch( CurPath + "test2", ul, true ) ); + efsw::WatchID watchID = + handleWatchID( fileWatcher.addWatch( CurPath + "test2", ul, true ) ); /// delete the watch - if ( watchID > 0 ) - { + if ( watchID > 0 ) { efsw::System::sleep( 1000 ); fileWatcher.removeWatch( watchID ); } - } - else - { - efsw::WatchID err; - - if ( ( err = fileWatcher.addWatch( path, ul, true ) ) > 0 ) - { + } else { + if ( fileWatcher.addWatch( path, ul, true ) > 0 ) { fileWatcher.watch(); std::cout << "Watching directory: " << path.c_str() << std::endl; - if ( useGeneric ) - { + if ( useGeneric ) { std::cout << "Using generic backend watcher" << std::endl; } - } - else - { + } else { std::cout << "Error trying to watch directory: " << path.c_str() << std::endl; std::cout << efsw::Errors::Log::getLastErrorLog().c_str() << std::endl; } } - while( !STOP ) - { + while ( !STOP ) { efsw::System::sleep( 100 ); } |