Merge branch 'feature/cli' into feature/cmdl
This commit is contained in:
commit
ca38b98e8a
@ -1,25 +1,64 @@
|
|||||||
#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){
|
||||||
if (argc > 0){
|
try{
|
||||||
|
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++){
|
||||||
|
args.push_back(argv[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const std::invalid_argument& e) {
|
||||||
|
std::cerr << e.what() << "\n";
|
||||||
|
PrintHelp();
|
||||||
|
std::exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++){
|
bool CLI::IsMarkupFile(const std::string& filename) {
|
||||||
args.push_back(argv[i]);
|
//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++){
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CLI::PrintHelp() const{
|
void CLI::PrintHelp() const{
|
||||||
std::cout << "Usage:\n"
|
std::cout << "Usage:\n"
|
||||||
<< " " << programName << " <input_file>\n"
|
<< " " << programName << " <input_file> REQUIRED\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"
|
||||||
|
|||||||
@ -60,6 +60,17 @@ 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;
|
||||||
|
|||||||
@ -1,63 +1,77 @@
|
|||||||
#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;
|
||||||
has_initial_time = false;
|
hasInitialTime = 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
|
||||||
last_write_time = fs::last_write_time(path);
|
lastWriteTime = fs::last_write_time(path);
|
||||||
//Sets watchdog
|
|
||||||
watching = true;
|
watching = true;
|
||||||
has_initial_time = true;
|
hasInitialTime = true;
|
||||||
std::cout << "WatchDog: Started" << std::endl;
|
std::cout << "WatchDog: Started" << std::endl;
|
||||||
}
|
}
|
||||||
|
void WatchDog::stop(){
|
||||||
bool Watchdog::CheckFile(){
|
watching = false;
|
||||||
|
}
|
||||||
|
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 (has_initial_time) {
|
if (hasInitialTime) {
|
||||||
std::cout << "WatchDog: File was delete: " << path << std::endl;
|
std::cout << "WatchDog: File was delete: " << path << std::endl;
|
||||||
has_initial_time = false;
|
hasInitialTime = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Built in function with file system to check last write time
|
//Built in function with file system to check last write tim
|
||||||
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(!has_initial_time)
|
if(!hasInitialTime)
|
||||||
{
|
{
|
||||||
last_write_time = currentWriteTime;
|
lastWriteTime = currentWriteTime;
|
||||||
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 != last_write_time)
|
if (currentWriteTime != lastWriteTime)
|
||||||
{
|
{
|
||||||
last_write_time = currentWriteTime;
|
lastWriteTime = currentWriteTime;
|
||||||
std::cout << "WatchDog: File modifed at "
|
std::cout << "WatchDog: File modifed at "
|
||||||
<< TimePointToString(last_write_time) << std::endl;
|
<< timePointToString(lastWriteTime) << std::endl;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//No change
|
} 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
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user