Skip to content

Commit

Permalink
Minor fixes and updates (#2)
Browse files Browse the repository at this point in the history
* Adds .gitignore

* - Updates signal_handler
- Uses specific filesystem namespace modules

* Updates README
  • Loading branch information
ankushkhanna1998 authored Dec 19, 2023
1 parent ef099a9 commit cc64ef6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.swp
build
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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)

Expand All @@ -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
```
11 changes: 6 additions & 5 deletions movie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include <regex>
#include <vector>

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() {
Expand Down Expand Up @@ -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());
}
Expand All @@ -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()) {
Expand Down

0 comments on commit cc64ef6

Please sign in to comment.