Feature: Watchdog V2 implementation. #31
@ -5,24 +5,24 @@
|
|||||||
// implement hashmap for the code
|
// implement hashmap for the code
|
||||||
CLI::CLI(int argc, char **argv) {
|
CLI::CLI(int argc, char **argv) {
|
||||||
try {
|
try {
|
||||||
if (arc < 2) {
|
if (argc < 2) {
|
||||||
throw std::invalid_argument("Error: No input file provided.\n
|
throw std::invalid_argument(
|
||||||
Usage: <program> <input_file>");
|
"Error: No input file provided.\nUsage: <program> <input_file>");
|
||||||
}
|
}
|
||||||
// sets program info
|
// sets program info
|
||||||
programName = argv[0];
|
programName = argv[0];
|
||||||
inputFile = argv[1]
|
inputFile = argv[1];
|
||||||
|
|
||||||
// checks that file has correct file extension
|
// checks that file has correct file extension
|
||||||
if (!(CLI::IsMarkupFile(inputFile))) {
|
if (!(CLI::IsMarkupFile(inputFile))) {
|
||||||
throw std::invalid_argument("Error: Invalid file extension. Expected a '.md' (Markdown) file.");
|
throw std::invalid_argument(
|
||||||
|
"Error: Invalid file extension. Expected a '.md' (Markdown) file.");
|
||||||
}
|
}
|
||||||
// stores remaining arguments
|
// stores remaining arguments
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
args.push_back(argv[i]);
|
args.push_back(argv[i]);
|
||||||
}
|
}
|
||||||
}
|
} catch (const std::invalid_argument &e) {
|
||||||
catch (const std::invalid_argument& e) {
|
|
||||||
std::cerr << e.what() << "\n";
|
std::cerr << e.what() << "\n";
|
||||||
PrintHelp();
|
PrintHelp();
|
||||||
std::exit(EXIT_FAILURE);
|
std::exit(EXIT_FAILURE);
|
||||||
@ -36,23 +36,25 @@ bool CLI::IsMarkupFile(const std::string& filename) {
|
|||||||
if (filename.size() <= requiredExtension.size()) {
|
if (filename.size() <= requiredExtension.size()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Extracts the last N characters of the filename (where N is the length of the required extension)
|
// Extracts the last N characters of the filename (where N is the length of
|
||||||
// and checks if they match the required file extension (e.g., ".md").
|
// the required extension) and checks if they match the required file
|
||||||
|
// extension (e.g., ".md").
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
// filename = "notes.md"
|
// filename = "notes.md"
|
||||||
// requiredExtension = ".md"
|
// requiredExtension = ".md"
|
||||||
// filename.substr(filename.size() - requiredExtension.size()) == ".md" → true
|
// filename.substr(filename.size() - requiredExtension.size()) == ".md" →
|
||||||
|
// true
|
||||||
//
|
//
|
||||||
// Reference: https://en.cppreference.com/w/cpp/string/basic_string/substr
|
// Reference: https://en.cppreference.com/w/cpp/string/basic_string/substr
|
||||||
return filename.substr(filename.size() - requiredExtension.size()) == requiredExtension;
|
return filename.substr(filename.size() - requiredExtension.size()) ==
|
||||||
|
requiredExtension;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// place to run commands but IDK IF WE SHOULD USE A HASMAP or how we are going
|
||||||
//place to run commands but IDK IF WE SHOULD USE A HASMAP or how we are going to get each method to run
|
// to get each method to run
|
||||||
void CLI::RunCommands() {
|
void CLI::RunCommands() {
|
||||||
for (int i = 0; i < args.size(); i++) {
|
for (int i = 0; i < args.size(); i++) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,4 +66,3 @@ void CLI::PrintHelp() const{
|
|||||||
<< " --w, --watch, enables watchdog\n"
|
<< " --w, --watch, enables watchdog\n"
|
||||||
<< " --s, --stop, stops watchdog\n";
|
<< " --s, --stop, stops watchdog\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,36 +1,34 @@
|
|||||||
#include "watchdog.h"
|
#include "watchDog.h"
|
||||||
|
|
||||||
namespace fs = std::filesystem; // makes it easier to read
|
namespace fs = std::filesystem; // makes it easier to read
|
||||||
void WatchDog::start()
|
|
||||||
{
|
void Watchdog::Start() {
|
||||||
// checks if file exist
|
// checks if file exist
|
||||||
if(!fs::exists(path))
|
if (!fs::exists(path)) {
|
||||||
{
|
|
||||||
// returns and sets parameters to false if file doesnt exist
|
// returns and sets parameters to false if file doesnt exist
|
||||||
watching = false;
|
this->watching = false;
|
||||||
hasInitialTime = false;
|
this->has_initial_time = false;
|
||||||
std::cout << "WatchDog: File does not exists: " << path << std::endl;
|
std::cout << "Watchdog: File does not exists: " << path << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// grabs intial write time
|
// grabs intial write time
|
||||||
lastWriteTime = fs::last_write_time(path);
|
this->last_write_time = fs::last_write_time(path);
|
||||||
watching = true;
|
this->watching = true;
|
||||||
hasInitialTime = true;
|
this->has_initial_time = true;
|
||||||
std::cout << "WatchDog: Started" << std::endl;
|
std::cout << "Watchdog: Started" << std::endl;
|
||||||
}
|
}
|
||||||
void WatchDog::stop(){
|
|
||||||
watching = false;
|
|
||||||
}
|
|
||||||
bool WatchDog::checkFile(){
|
|
||||||
//If not watching returns false
|
|
||||||
if (!watching) return false;
|
|
||||||
|
|
||||||
|
bool Watchdog::CheckFile() {
|
||||||
|
// If not watching returns false
|
||||||
|
if (!watching)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
try {
|
||||||
// Checking if file was deleted
|
// Checking if file was deleted
|
||||||
if(!fs::exists(path))
|
if (!fs::exists(path)) {
|
||||||
{
|
if (this->has_initial_time) {
|
||||||
if (hasInitialTime) {
|
std::cout << "Watchdog: File was delete: " << path << std::endl;
|
||||||
std::cout << "WatchDog: File was delete: " << path << std::endl;
|
this->has_initial_time = false;
|
||||||
hasInitialTime = false;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -40,34 +38,32 @@ bool WatchDog::checkFile(){
|
|||||||
fs::file_time_type currentWriteTime = fs::last_write_time(path);
|
fs::file_time_type currentWriteTime = fs::last_write_time(path);
|
||||||
|
|
||||||
// File was just created
|
// File was just created
|
||||||
if(!hasInitialTime)
|
if (!this->has_initial_time) {
|
||||||
{
|
this->last_write_time = currentWriteTime;
|
||||||
lastWriteTime = currentWriteTime;
|
this->has_initial_time = true;
|
||||||
hasInitialTime = true;
|
std::cout << "Watchdog: File created: " << path << std::endl;
|
||||||
std::cout << "WatchDog: File created: " << path << std::endl;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// File modified
|
// File modified
|
||||||
if (currentWriteTime != lastWriteTime)
|
if (currentWriteTime != this->last_write_time) {
|
||||||
{
|
this->last_write_time = currentWriteTime;
|
||||||
lastWriteTime = currentWriteTime;
|
std::cout << "Watchdog: File modifed at "
|
||||||
std::cout << "WatchDog: File modifed at "
|
<< TimePointToString(this->last_write_time) << std::endl;
|
||||||
<< timePointToString(lastWriteTime) << std::endl;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (const fs::filesystem_error &e) {
|
} catch (const fs::filesystem_error &e) {
|
||||||
// File deleted, inaccessible, or path invalid
|
// File deleted, inaccessible, or path invalid
|
||||||
if (hasInitialTime) {
|
if (this->has_initial_time) {
|
||||||
std::cout << "WatchDog: File deleted or inaccessible: " << path << std::endl;
|
std::cout << "Watchdog: File deleted or inaccessible: " << path
|
||||||
hasInitialTime = false;
|
<< std::endl;
|
||||||
|
this->has_initial_time = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
std::cerr << "WatchDog: Unexpected error checking file: " << e.what() << std::endl;
|
std::cerr << "Watchdog: Unexpected error checking file: " << e.what()
|
||||||
|
<< std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,7 +84,8 @@ std::string Watchdog::TimePointToString(const fs::file_time_type& timePoint){
|
|||||||
* - Current time according to the system clock (standard C++ clock).
|
* - Current time according to the system clock (standard C++ clock).
|
||||||
*
|
*
|
||||||
* Conversion formula:
|
* Conversion formula:
|
||||||
* timePoint - fs::file_time_type::clock::now() + std::chrono::system_clock::now()
|
* timePoint - fs::file_time_type::clock::now() +
|
||||||
|
* std::chrono::system_clock::now()
|
||||||
*
|
*
|
||||||
* Explanation:
|
* Explanation:
|
||||||
* a) timePoint - fs::file_time_type::clock::now()
|
* a) timePoint - fs::file_time_type::clock::now()
|
||||||
@ -106,11 +103,12 @@ std::string Watchdog::TimePointToString(const fs::file_time_type& timePoint){
|
|||||||
*/
|
*/
|
||||||
std::chrono::system_clock::time_point systemTimePoint =
|
std::chrono::system_clock::time_point systemTimePoint =
|
||||||
std::chrono::time_point_cast<std::chrono::system_clock::duration>(
|
std::chrono::time_point_cast<std::chrono::system_clock::duration>(
|
||||||
timePoint - fs::file_time_type::clock::now() + std::chrono::system_clock::now()
|
timePoint - fs::file_time_type::clock::now() +
|
||||||
);
|
std::chrono::system_clock::now());
|
||||||
|
|
||||||
// Converts to seconds
|
// Converts to seconds
|
||||||
std::time_t timeInSeconds = std::chrono::system_clock::to_time_t(systemTimePoint);
|
std::time_t timeInSeconds =
|
||||||
|
std::chrono::system_clock::to_time_t(systemTimePoint);
|
||||||
|
|
||||||
// Converts to local time, built in function
|
// Converts to local time, built in function
|
||||||
std::tm localTime = *std::localtime(&timeInSeconds);
|
std::tm localTime = *std::localtime(&timeInSeconds);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user