(FIX): Resolved the naming issues.
When this was merged, it seemed to produce a miss matched result. The names were not migrated. But now they have been.
This commit is contained in:
parent
152c4e8e04
commit
d26dd1b5a2
@ -2,66 +2,67 @@
|
||||
#include <iostream>
|
||||
#include <stdexcept> // for std::invalid_argument
|
||||
|
||||
//implement hashmap for the code
|
||||
CLI::CLI(int argc, char** argv){
|
||||
try{
|
||||
if (arc < 2) {
|
||||
throw std::invalid_argument("Error: No input file provided.\n
|
||||
Usage: <program> <input_file>");
|
||||
}
|
||||
//sets program info
|
||||
programName = argv[0];
|
||||
inputFile = argv[1]
|
||||
// implement hashmap for the code
|
||||
CLI::CLI(int argc, char **argv) {
|
||||
try {
|
||||
if (argc < 2) {
|
||||
throw std::invalid_argument(
|
||||
"Error: No input file provided.\nUsage: <program> <input_file>");
|
||||
}
|
||||
// sets program info
|
||||
programName = argv[0];
|
||||
inputFile = argv[1];
|
||||
|
||||
//checks that file has correct file extension
|
||||
if(!(CLI::IsMarkupFile(inputFile))) {
|
||||
throw std::invalid_argument("Error: Invalid file extension. Expected a '.md' (Markdown) file.");
|
||||
}
|
||||
//stores remaining arguments
|
||||
for (int i = 1; i < argc; i++){
|
||||
args.push_back(argv[i]);
|
||||
}
|
||||
}
|
||||
catch (const std::invalid_argument& e) {
|
||||
std::cerr << e.what() << "\n";
|
||||
PrintHelp();
|
||||
std::exit(EXIT_FAILURE);
|
||||
// checks that file has correct file extension
|
||||
if (!(CLI::IsMarkupFile(inputFile))) {
|
||||
throw std::invalid_argument(
|
||||
"Error: Invalid file extension. Expected a '.md' (Markdown) file.");
|
||||
}
|
||||
// stores remaining arguments
|
||||
for (int i = 1; i < argc; i++) {
|
||||
args.push_back(argv[i]);
|
||||
}
|
||||
|
||||
bool CLI::IsMarkupFile(const std::string& filename) {
|
||||
//markdown file extension
|
||||
const std::string requiredExtension = ".md";
|
||||
//checks to see if it can even include extension
|
||||
if (filename.size() <= requiredExtension.size()) {
|
||||
return false;
|
||||
}
|
||||
// Extracts the last N characters of the filename (where N is the length of the required extension)
|
||||
// and checks if they match the required file extension (e.g., ".md").
|
||||
//
|
||||
// Example:
|
||||
// filename = "notes.md"
|
||||
// requiredExtension = ".md"
|
||||
// filename.substr(filename.size() - requiredExtension.size()) == ".md" → true
|
||||
//
|
||||
// Reference: https://en.cppreference.com/w/cpp/string/basic_string/substr
|
||||
return filename.substr(filename.size() - requiredExtension.size()) == requiredExtension;
|
||||
} catch (const std::invalid_argument &e) {
|
||||
std::cerr << e.what() << "\n";
|
||||
PrintHelp();
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//place to run commands but IDK IF WE SHOULD USE A HASMAP or how we are going to get each method to run
|
||||
void CLI::RunCommands(){
|
||||
for (int i = 0; i < args.size(); i++){
|
||||
|
||||
}
|
||||
bool CLI::IsMarkupFile(const std::string &filename) {
|
||||
// markdown file extension
|
||||
const std::string requiredExtension = ".md";
|
||||
// checks to see if it can even include extension
|
||||
if (filename.size() <= requiredExtension.size()) {
|
||||
return false;
|
||||
}
|
||||
// Extracts the last N characters of the filename (where N is the length of
|
||||
// the required extension) and checks if they match the required file
|
||||
// extension (e.g., ".md").
|
||||
//
|
||||
// Example:
|
||||
// filename = "notes.md"
|
||||
// requiredExtension = ".md"
|
||||
// filename.substr(filename.size() - requiredExtension.size()) == ".md" →
|
||||
// true
|
||||
//
|
||||
// Reference: https://en.cppreference.com/w/cpp/string/basic_string/substr
|
||||
return filename.substr(filename.size() - requiredExtension.size()) ==
|
||||
requiredExtension;
|
||||
}
|
||||
|
||||
void CLI::PrintHelp() const{
|
||||
std::cout << "Usage:\n"
|
||||
<< " " << programName << " <input_file> REQUIRED\n"
|
||||
<< "Flags:\n"
|
||||
<< " --o, --output <outputFile>, optional output filename\n"
|
||||
<< " --w, --watch, enables watchdog\n"
|
||||
<< " --s, --stop, stops watchdog\n";
|
||||
// place to run commands but IDK IF WE SHOULD USE A HASMAP or how we are going
|
||||
// to get each method to run
|
||||
void CLI::RunCommands() {
|
||||
for (int i = 0; i < args.size(); i++) {
|
||||
}
|
||||
}
|
||||
|
||||
void CLI::PrintHelp() const {
|
||||
std::cout << "Usage:\n"
|
||||
<< " " << programName << " <input_file> REQUIRED\n"
|
||||
<< "Flags:\n"
|
||||
<< " --o, --output <outputFile>, optional output filename\n"
|
||||
<< " --w, --watch, enables watchdog\n"
|
||||
<< " --s, --stop, stops watchdog\n";
|
||||
}
|
||||
|
||||
208
lib/watchDog.cpp
208
lib/watchDog.cpp
@ -1,123 +1,121 @@
|
||||
#include "watchdog.h"
|
||||
#include "watchDog.h"
|
||||
|
||||
namespace fs = std::filesystem; //makes it easier to read
|
||||
void WatchDog::start()
|
||||
{
|
||||
//checks if file exist
|
||||
if(!fs::exists(path))
|
||||
{
|
||||
//returns and sets parameters to false if file doesnt exist
|
||||
watching = false;
|
||||
hasInitialTime = false;
|
||||
std::cout << "WatchDog: File does not exists: " << path << std::endl;
|
||||
return;
|
||||
}
|
||||
//grabs intial write time
|
||||
lastWriteTime = fs::last_write_time(path);
|
||||
watching = true;
|
||||
hasInitialTime = true;
|
||||
std::cout << "WatchDog: Started" << std::endl;
|
||||
}
|
||||
void WatchDog::stop(){
|
||||
watching = false;
|
||||
}
|
||||
bool WatchDog::checkFile(){
|
||||
//If not watching returns false
|
||||
if (!watching) return false;
|
||||
namespace fs = std::filesystem; // makes it easier to read
|
||||
|
||||
//Checking if file was deleted
|
||||
if(!fs::exists(path))
|
||||
{
|
||||
if (hasInitialTime) {
|
||||
std::cout << "WatchDog: File was delete: " << path << std::endl;
|
||||
hasInitialTime = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
void Watchdog::Start() {
|
||||
// checks if file exist
|
||||
if (!fs::exists(path)) {
|
||||
// returns and sets parameters to false if file doesnt exist
|
||||
this->watching = false;
|
||||
this->has_initial_time = false;
|
||||
std::cout << "Watchdog: File does not exists: " << path << std::endl;
|
||||
return;
|
||||
}
|
||||
// grabs intial write time
|
||||
this->last_write_time = fs::last_write_time(path);
|
||||
this->watching = true;
|
||||
this->has_initial_time = true;
|
||||
std::cout << "Watchdog: Started" << std::endl;
|
||||
}
|
||||
|
||||
bool Watchdog::CheckFile() {
|
||||
// If not watching returns false
|
||||
if (!watching)
|
||||
return false;
|
||||
|
||||
try {
|
||||
// Checking if file was deleted
|
||||
if (!fs::exists(path)) {
|
||||
if (this->has_initial_time) {
|
||||
std::cout << "Watchdog: File was delete: " << path << std::endl;
|
||||
this->has_initial_time = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//Built in function with file system to check last write tim
|
||||
// Built in function with file system to check last write tim
|
||||
fs::file_time_type currentWriteTime = fs::last_write_time(path);
|
||||
|
||||
//File was just created
|
||||
if(!hasInitialTime)
|
||||
{
|
||||
lastWriteTime = currentWriteTime;
|
||||
hasInitialTime = true;
|
||||
std::cout << "WatchDog: File created: " << path << std::endl;
|
||||
return true;
|
||||
// File was just created
|
||||
if (!this->has_initial_time) {
|
||||
this->last_write_time = currentWriteTime;
|
||||
this->has_initial_time = true;
|
||||
std::cout << "Watchdog: File created: " << path << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
//File modified
|
||||
if (currentWriteTime != lastWriteTime)
|
||||
{
|
||||
lastWriteTime = currentWriteTime;
|
||||
std::cout << "WatchDog: File modifed at "
|
||||
<< timePointToString(lastWriteTime) << std::endl;
|
||||
return true;
|
||||
// File modified
|
||||
if (currentWriteTime != this->last_write_time) {
|
||||
this->last_write_time = currentWriteTime;
|
||||
std::cout << "Watchdog: File modifed at "
|
||||
<< TimePointToString(this->last_write_time) << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
} catch (const fs::filesystem_error& e) {
|
||||
// File deleted, inaccessible, or path invalid
|
||||
if (hasInitialTime) {
|
||||
std::cout << "WatchDog: File deleted or inaccessible: " << path << std::endl;
|
||||
hasInitialTime = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "WatchDog: Unexpected error checking file: " << e.what() << std::endl;
|
||||
return false;
|
||||
} catch (const fs::filesystem_error &e) {
|
||||
// File deleted, inaccessible, or path invalid
|
||||
if (this->has_initial_time) {
|
||||
std::cout << "Watchdog: File deleted or inaccessible: " << path
|
||||
<< std::endl;
|
||||
this->has_initial_time = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// No change
|
||||
return false;
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Watchdog: Unexpected error checking file: " << e.what()
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// No change
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string Watchdog::TimePointToString(const fs::file_time_type& timePoint){
|
||||
/**
|
||||
* Step 1: timePoint
|
||||
* - This is the last write time of the file, returned by std::filesystem.
|
||||
* - Its clock is platform-dependent (filesystem clock).
|
||||
*
|
||||
* Step 2: fs::file_time_type::clock::now()
|
||||
* - Current time according to the filesystem clock.
|
||||
*
|
||||
* Step 3: std::chrono::system_clock::now()
|
||||
* - Current time according to the system clock (standard C++ clock).
|
||||
*
|
||||
* Conversion formula:
|
||||
* timePoint - fs::file_time_type::clock::now() + std::chrono::system_clock::now()
|
||||
*
|
||||
* Explanation:
|
||||
* a) timePoint - fs::file_time_type::clock::now()
|
||||
* - Calculates the duration between the file's last write time and "now"
|
||||
* according to the filesystem clock.
|
||||
* b) + std::chrono::system_clock::now()
|
||||
* - Shifts that duration to align with the system clock timeline.
|
||||
* c) std::chrono::time_point_cast<std::chrono::system_clock::duration>(...)
|
||||
* - Ensures the resulting time_point uses the correct duration type
|
||||
* for std::chrono::system_clock.
|
||||
*
|
||||
* Result:
|
||||
* - systemTime is a std::chrono::system_clock::time_point representing
|
||||
* the same instant as timePoint, but compatible with system_clock.
|
||||
*/
|
||||
std::chrono::system_clock::time_point systemTimePoint =
|
||||
std::chrono::time_point_cast<std::chrono::system_clock::duration>(
|
||||
timePoint - fs::file_time_type::clock::now() + std::chrono::system_clock::now()
|
||||
);
|
||||
std::string Watchdog::TimePointToString(const fs::file_time_type &timePoint) {
|
||||
/**
|
||||
* Step 1: timePoint
|
||||
* - This is the last write time of the file, returned by std::filesystem.
|
||||
* - Its clock is platform-dependent (filesystem clock).
|
||||
*
|
||||
* Step 2: fs::file_time_type::clock::now()
|
||||
* - Current time according to the filesystem clock.
|
||||
*
|
||||
* Step 3: std::chrono::system_clock::now()
|
||||
* - Current time according to the system clock (standard C++ clock).
|
||||
*
|
||||
* Conversion formula:
|
||||
* timePoint - fs::file_time_type::clock::now() +
|
||||
* std::chrono::system_clock::now()
|
||||
*
|
||||
* Explanation:
|
||||
* a) timePoint - fs::file_time_type::clock::now()
|
||||
* - Calculates the duration between the file's last write time and "now"
|
||||
* according to the filesystem clock.
|
||||
* b) + std::chrono::system_clock::now()
|
||||
* - Shifts that duration to align with the system clock timeline.
|
||||
* c) std::chrono::time_point_cast<std::chrono::system_clock::duration>(...)
|
||||
* - Ensures the resulting time_point uses the correct duration type
|
||||
* for std::chrono::system_clock.
|
||||
*
|
||||
* Result:
|
||||
* - systemTime is a std::chrono::system_clock::time_point representing
|
||||
* the same instant as timePoint, but compatible with system_clock.
|
||||
*/
|
||||
std::chrono::system_clock::time_point systemTimePoint =
|
||||
std::chrono::time_point_cast<std::chrono::system_clock::duration>(
|
||||
timePoint - fs::file_time_type::clock::now() +
|
||||
std::chrono::system_clock::now());
|
||||
|
||||
//Converts to seconds
|
||||
std::time_t timeInSeconds = std::chrono::system_clock::to_time_t(systemTimePoint);
|
||||
// Converts to seconds
|
||||
std::time_t timeInSeconds =
|
||||
std::chrono::system_clock::to_time_t(systemTimePoint);
|
||||
|
||||
//Converts to local time, built in function
|
||||
std::tm localTime = *std::localtime(&timeInSeconds);
|
||||
// Converts to local time, built in function
|
||||
std::tm localTime = *std::localtime(&timeInSeconds);
|
||||
|
||||
// Format the time into a string using strftime
|
||||
char buffer[20];
|
||||
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &localTime);
|
||||
// Format the time into a string using strftime
|
||||
char buffer[20];
|
||||
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &localTime);
|
||||
|
||||
return std::string(buffer);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user