Compare commits

..

No commits in common. "ca38b98e8ae8c84b91f9eac0740c189d93e207c3" and "616cf7260a53e302f01bdbfa3a825d01c1138c65" have entirely different histories.

3 changed files with 26 additions and 90 deletions

View File

@ -1,55 +1,16 @@
#include "commandLineParser.h" #include "commandLineParser.h"
#include <iostream> #include <iostream>
#include <stdexcept> // for std::invalid_argument
//implement hashmap for the code
CLI::CLI(int argc, char** argv){ CLI::CLI(int argc, char** argv){
try{ if (argc > 0){
if (arc < 2) {
throw std::invalid_argument("Error: No input file provided.\n
Usage: <program> <input_file>");
}
//sets program info
programName = argv[0]; 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++){ for (int i = 1; i < argc; i++){
args.push_back(argv[i]); args.push_back(argv[i]);
} }
} }
catch (const std::invalid_argument& e) {
std::cerr << e.what() << "\n";
PrintHelp();
std::exit(EXIT_FAILURE);
}
}
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;
}
//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(){ void CLI::RunCommands(){
for (int i = 0; i < args.size(); i++){ for (int i = 0; i < args.size(); i++){
@ -58,7 +19,7 @@ void CLI::RunCommands(){
void CLI::PrintHelp() const{ void CLI::PrintHelp() const{
std::cout << "Usage:\n" std::cout << "Usage:\n"
<< " " << programName << " <input_file> REQUIRED\n" << " " << programName << " <input_file>\n"
<< "Flags:\n" << "Flags:\n"
<< " --o, --output <outputFile>, optional output filename\n" << " --o, --output <outputFile>, optional output filename\n"
<< " --w, --watch, enables watchdog\n" << " --w, --watch, enables watchdog\n"

View File

@ -60,17 +60,6 @@ public:
std::string GetOutputFile() const {return outputFile;} std::string GetOutputFile() const {return outputFile;}
private: private:
/**
* @brief Ensures the provided filename has a .markup extension
*
* @param filename The file name to validate
* @return true if file ends with .markup
* @return false otherwise
* @author Preston Shultz (shultzp1@my.erau.edu)
*/
static bool IsMarkupFile(const std::string& filename);
std::string programName; std::string programName;
std::vector<std::string> args; std::vector<std::string> args;
std::string inputFile; std::string inputFile;

View File

@ -1,76 +1,62 @@
#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; watching = false;
hasInitialTime = false; 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); last_write_time = fs::last_write_time(path);
//Sets watchdog
watching = true; watching = true;
hasInitialTime = true; has_initial_time = true;
std::cout << "WatchDog: Started" << std::endl; std::cout << "WatchDog: Started" << std::endl;
} }
void WatchDog::stop(){
watching = false; bool Watchdog::CheckFile(){
}
bool WatchDog::checkFile(){
//If not watching returns false //If not watching returns false
if (!watching) return false; if (!watching) return false;
//Checking if file was deleted //Checking if file was deleted
if(!fs::exists(path)) if(!fs::exists(path))
{ {
if (hasInitialTime) { if (has_initial_time) {
std::cout << "WatchDog: File was delete: " << path << std::endl; std::cout << "WatchDog: File was delete: " << path << std::endl;
hasInitialTime = false; has_initial_time = false;
return true; return true;
} }
return false; return false;
} }
//Built in function with file system to check last write tim //Built in function with file system to check last write time
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(!has_initial_time)
{ {
lastWriteTime = currentWriteTime; last_write_time = currentWriteTime;
hasInitialTime = true; has_initial_time = 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 != last_write_time)
{ {
lastWriteTime = currentWriteTime; last_write_time = currentWriteTime;
std::cout << "WatchDog: File modifed at " std::cout << "WatchDog: File modifed at "
<< timePointToString(lastWriteTime) << std::endl; << TimePointToString(last_write_time) << std::endl;
return true; 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;
}
//No change //No change
return false; return false;
} }