diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d5a1572 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.swp +build diff --git a/README.md b/README.md index 77c635d..02b855a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Movie Selector -This is a console application written in C++ (GNU ISO C++17) which helps select a random movie to watch from the available ones in the given directories. +This is a console application written in C++ (initially in ISO C++17) which helps select a random movie to watch from the available ones in the given directories. Basically, not just movies, it can be used to select anything from anywhere. One just needs to write a valid [Regular Expression](https://en.wikipedia.org/wiki/Regular_expression) to match the desired files in the given directories, and then among the matched ones, this application provides a random file name. @@ -11,10 +11,17 @@ One just needs to add the search path(s) as mentioned in the code (where we have Then, one needs to compile the code. The most simple way to do so is: ```bash -g++ -std=c++17 -o movie movie.cpp +mkdir --parents build/ +g++ -std=c++17 -o build/movie movie.cpp ``` -You can use the compile options of your choice, just make sure you compile the source with C++17 enabled (`-std=c++17` or `-std=gnu++17`), as this code makes use of the `std::filesystem` API which came out with the ISO C++17 standard. +Running it is simple then: + +```bash +./build/movie +``` + +You can use the compile options of your choice, just make sure you compile the source with C++17 (or higher) enabled (`-std=c++17`, `-std=gnu++17` or higher), as this code makes use of the `std::filesystem` API which came out with the ISO C++17 standard. ### Some History (in case you're interested) @@ -29,5 +36,5 @@ Though it is very general purpose, still the the only reason why I named it to ` And, by the way, this is how I compiled it for myself: ```bash -g++ -no-pie -fno-pie -pthread -Wall -Wextra -Wpedantic -O3 -g -std=gnu++17 -o movie movie.cpp +g++ -no-pie -fno-pie -fsanitize=address -fno-omit-frame-pointer -fno-common -pthread -Wall -Wextra -Wpedantic -O3 -ggdb3 -std=c++23 -o build/movie movie.cpp ``` diff --git a/movie.cpp b/movie.cpp index 9e4489a..ffa0610 100644 --- a/movie.cpp +++ b/movie.cpp @@ -6,9 +6,9 @@ #include #include -void signal_handler(int signal_code) { +inline void signal_handler(const int signal_code) { std::cerr << std::endl << "Keyboard Interrupt!" << std::endl; - exit(128 + signal_code); + exit(128 | signal_code); } int main() { @@ -38,10 +38,11 @@ int main() { const std::regex pattern_to_match("(.*)"); - namespace fs = std::filesystem; + using std::filesystem::directory_entry; + using std::filesystem::recursive_directory_iterator; for (const std::string &search_path : search_paths) { - for (const fs::directory_entry &entry : fs::recursive_directory_iterator(search_path)) { + for (const directory_entry &entry : recursive_directory_iterator(search_path)) { if (entry.is_regular_file() && std::regex_match(entry.path().filename().string(), pattern_to_match)) { candidate_items.push_back(entry.path().filename().string()); } @@ -54,7 +55,7 @@ int main() { std::cerr << " Unmounted Device, and/or" << std::endl; std::cerr << " Invalid/Incorrect/Non-Existing/Restricted Search Path, and/or" << std::endl; std::cerr << " Invalid/Incorrect Regular Expression Matching Pattern, ..., etc." << std::endl << std::endl; - exit(128 + 126); + exit(128 | 126); } if (candidate_items.empty()) {