Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The task of this assignment is to conform to the following: If no command line arguments are passed to this program, then the path used

The task of this assignment is to conform to the following:
If no command line arguments are passed to this program, then the path used is the current directory, i.e.,".".
If a single command line argument is passed to this program, then the path used is that, i.e., argv[1].
The regular expression to be used to check for matching file names is "[[:alnum:]]+\\.txt" with
regex_constants::ECMAScript | regex_constants::icase.
If a std::filesystem::filesystem_error exception occurs at anytime during processing, the following must be output before exiting with an exit status of 1: "EXCEPTION: " follows by e.what()(where e is the caught exception) followed by a newline character all written to std::clog.
If any other exception occurs at anytime during processing, the following must be output to std::clog: "EXCEPTION: unknown
" and then the program must exit with an exit status of 2.
The program must search the path non-recursively and output all files that are between 24 hours and 7 days (i.e.,7*24h) old based on the current time. For any file that matches such, "FOUND: " followed by that file's std::filesystem::path, a space, and then that file's time (simply output the object returned from std::filesystem::directory_entry's last_write_time() member function).
Tips and Comments
To assist with this assignment note the following:
Consider using these #include files: ,,, and .
Refer to your textbooks for help with these. As a reference only (not as a tutorial) cppreference.com is also useful.
Recall from COMP-2560(Systems Programming) how command line arguments are passed in to main().(If more than one argument is passed in, don't worry about the extra arguments --only process the first argument.)
When storing the path to start searching for filesm, use std::filesystem::path.
As it can get tedious writing std::filesystem and std::chrono everyone consider using these namespace aliases:
namespace fs = std::filesystem;
namespace chr = std::chrono;
You are only permitted to use two "using namespace" statements in your program:
using namespace std;
using namespace std::literals::chrono_literals;
(Place these at file scope AFTER your #include files.)
To get the current FILESYSTEM time, use:
auto const cut_time = chr::file_clock::now();
Then compute the two times you need storing these values in variables so you can use them later:
cur_time -7*24h
cur_time -24h
The rest of the code in main() will be all in a try ... catch ... block.
Store a fs::path holding the path passed in from the command line.
Use a range-based for loop to iterate over the current directory only (i.e., don't do this recursively!).
Pass to the constructor of the iterator used fs::directory_options::skip_permission_denied
Inside the for loop declare a static std::regex variable passing the aforementioned regular expression and the std::regex_constants to that variable's constructor.
ASIDE: If the std::regex variable is NOT inside the loop then it need not be static. Technically inside the loop it doesn't need to be static either. What is the difference? The value of the std::regex variable never changes --but std::regex argument string are actually compiled in the constructor code and that takes time. In this loop you are reusing the std::regex variable multiple times and the regular expression is not changing, thus, one shouldn't be recompiling the regular expression each time unnecessarily. Making it static causes it to be only initialized once (and so the compilation occurs only once and since its regular expression is only one string always using static works).
Determine if there is a match (i.e., use std::smatch which matches std::string objects) and call std::regex_match passing in your fs::path variable's .path().filename().string() as the string to match and the std::regex static variable you declared earlier. When there is a match get that file's last_write_time() and check that such is between the two times you calculated earlier. If it is, then output such to the screen with "FOUND: ", etc. as detailed above.
NOTE: A number of persons have asked about smatch as it is not being used in this assignment. Correct --we are just using the return value of regex_match(). Normally with regex_match one makes use of smatch. (Thus, if you don't use smatch in your code, no worries.)
Sample Program Run
After compiling the code, e.g.,
g++-std=c++20-Wall -Wextra -Wold-style-cast -Werror -O3-march=native -o a1-soln.exe a1-soln.cxx
and determining the date, e.g., the date command: date +%C%y%m%d%H%M
One can then create some files with specific dates within and outside the range and test the program, e.g., like this:
mkdir -p rundir
for ext in txt txT tXt tXT Txt TxT TXt TXT ; do \
touch -t 202401081651 rundir/notokay.$ext ; \
touch -t 202401101651 rundir/okay1.$ext ; \
touch -t 202401151651 rundir/okay2.$ext ; \
touch -t 202401161051 rundir/notokay.$ext ; \
done
./a1-soln.exe ./

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 2 Lncs 13427

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124251, 978-3031124259

More Books

Students also viewed these Databases questions

Question

What advice would you provide to Jennifer?

Answered: 1 week ago

Question

What are the issues of concern for each of the affected parties?

Answered: 1 week ago