Merge pull request 'feature/cmdl' (#29) from feature/cmdl into main
Reviewed-on: azpect/MarkdownToHtmlCompiler#29 Reviewed-by: Hayden Hargreaves <hayden@gophernest.net>
This commit is contained in:
commit
152c4e8e04
67
lib/commandLineParser.cpp
Normal file
67
lib/commandLineParser.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
#include "commandLineParser.h"
|
||||
#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]
|
||||
|
||||
//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);
|
||||
}
|
||||
}
|
||||
|
||||
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(){
|
||||
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";
|
||||
}
|
||||
|
||||
80
lib/commandLineParser.h
Normal file
80
lib/commandLineParser.h
Normal file
@ -0,0 +1,80 @@
|
||||
/**
|
||||
* @file commandLineParser.h
|
||||
* @brief Parses command line
|
||||
* @author Preston Shultz
|
||||
* Sources:
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef COMMAND_LINE_PARSER_H
|
||||
#define COMMAND_LINE_PARSER_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* @brief CommandLine Helper class
|
||||
*
|
||||
* Parse input arguments, sets input and output files
|
||||
* Lets user use commands with program
|
||||
*
|
||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||
*/
|
||||
class CLI{
|
||||
public:
|
||||
/**
|
||||
* @brief Takes in argc and argv and converts it to vector
|
||||
*
|
||||
* CLI constructor
|
||||
*
|
||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||
*/
|
||||
CLI(int argc, char **argv);
|
||||
|
||||
/**
|
||||
* @brief Runs Commands
|
||||
*
|
||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||
*/
|
||||
void RunCommands();
|
||||
|
||||
/**
|
||||
* @brief Prints a list of commands that can be used
|
||||
*
|
||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||
*/
|
||||
void PrintHelp() const;
|
||||
|
||||
/**
|
||||
* @brief Returns input file
|
||||
*
|
||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||
*/
|
||||
std::string GetInputFile() const {return inputFile;}
|
||||
|
||||
/**
|
||||
* @brief Returns output file
|
||||
*
|
||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||
*/
|
||||
std::string GetOutputFile() const {return outputFile;}
|
||||
|
||||
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::vector<std::string> args;
|
||||
std::string inputFile;
|
||||
std::string outputFile;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -1,9 +1,4 @@
|
||||
/*
|
||||
*I don't know how threads work, so I might come back to see how they work.
|
||||
*Right now you have to manually call the checkfile function to see if any changes have occured
|
||||
*/
|
||||
|
||||
#include "watchDog.h"
|
||||
#include "watchdog.h"
|
||||
|
||||
namespace fs = std::filesystem; //makes it easier to read
|
||||
void WatchDog::start()
|
||||
@ -62,26 +57,26 @@ bool WatchDog::checkFile(){
|
||||
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;
|
||||
}
|
||||
|
||||
std::string WatchDog::timePointToString(const fs::file_time_type& timePoint){
|
||||
/*
|
||||
* https://en.cppreference.com/w/cpp/chrono.html
|
||||
* std::chrono -> name space, system_clock -> c++ clock(computers clock),
|
||||
* ime_point -> exact instant on system clock, used to cast
|
||||
* Comments: I had to use a lot of sources and googling to get this work
|
||||
* We can get rid of this or document my troubles, because tbh this time stuff confuses me
|
||||
*
|
||||
* std::chrono::system_clock::time_point systemTimePoint =
|
||||
* std::chrono::clock_cast<std::chrono::system_clock>(timePoint);
|
||||
* This solutions doesn't work as it only works with C++ 20 only :(
|
||||
*/
|
||||
|
||||
//Convert std::filesystem::file_time_type (timePoint) to std::chrono::system_clock::time_point
|
||||
|
||||
/*
|
||||
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).
|
||||
@ -114,14 +109,13 @@ std::string WatchDog::timePointToString(const fs::file_time_type& timePoint){
|
||||
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);
|
||||
|
||||
//Converts to local time, built in function
|
||||
std::tm localTime = *std::localtime(&timeInSeconds);
|
||||
|
||||
// Format the time into a string using strftime
|
||||
//https://en.cppreference.com/w/cpp/chrono/c/strftime.html
|
||||
char buffer[20];
|
||||
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &localTime);
|
||||
|
||||
|
||||
@ -1,19 +1,25 @@
|
||||
/**
|
||||
* @file watchdog.h
|
||||
* @brief file watcher
|
||||
* @author Preston Shultz
|
||||
* Sources:
|
||||
* https://en.cppreference.com/w/cpp/filesystem.html
|
||||
* https://en.cppreference.com/w/cpp/chrono.html
|
||||
* Format the time into a string using strftime
|
||||
* https://en.cppreference.com/w/cpp/chrono/c/strftime.html
|
||||
*/
|
||||
|
||||
#ifndef WATCHDOG_H
|
||||
#define WATCHDOG_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
/*
|
||||
*https://en.cppreference.com/w/cpp/filesystem.html
|
||||
*allow access to files platform independent
|
||||
*/
|
||||
#include <filesystem>
|
||||
/*
|
||||
*https://en.cppreference.com/w/cpp/chrono.html
|
||||
*makes timestamps easier
|
||||
*how to do lots of research to get this to work
|
||||
*/
|
||||
#include <chrono>
|
||||
#include <filesystem> //allow access to files platform independent
|
||||
#include <chrono> //makes timestamps easier
|
||||
#include <ctime> //Convert time_t to std::tm for local time
|
||||
#include <iomanip> //Formats std::tim into "YYYY-MM-DD HH-MM-SS"
|
||||
|
||||
|
||||
/**
|
||||
* @brief watchdog class.
|
||||
*
|
||||
@ -21,14 +27,10 @@
|
||||
*
|
||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||
*/
|
||||
#include <ctime> //Convert time_t to std::tm for local time
|
||||
#include <iomanip> //Formats std::tim into "YYYY-MM-DD HH-MM-SS"
|
||||
|
||||
|
||||
class WatchDog{
|
||||
public:
|
||||
WatchDog(const std::string& path) :
|
||||
path(path), watching(false), hasInitialTime(false) {}
|
||||
class Watchdog{
|
||||
public:
|
||||
Watchdog(const std::string& path) :
|
||||
path(path), watching(false), has_initial_time(false) {}
|
||||
|
||||
/**
|
||||
* @brief watchdog class.
|
||||
@ -37,7 +39,7 @@ class WatchDog{
|
||||
*
|
||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||
*/
|
||||
void start();
|
||||
void Start();
|
||||
/**
|
||||
* @brief watchdog class.
|
||||
*
|
||||
@ -45,7 +47,7 @@ class WatchDog{
|
||||
*
|
||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||
*/
|
||||
void stop(); //Disable
|
||||
void Stop() {watching = false;} //Disable
|
||||
|
||||
/**
|
||||
* @brief watchdog class.
|
||||
@ -54,7 +56,7 @@ class WatchDog{
|
||||
*
|
||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||
*/
|
||||
bool checkFile();
|
||||
bool CheckFile();
|
||||
|
||||
/**
|
||||
* @brief watchdog class.
|
||||
@ -63,8 +65,8 @@ class WatchDog{
|
||||
*
|
||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||
*/
|
||||
std::filesystem::file_time_type getLastWriteTime()
|
||||
const {return lastWriteTime;}
|
||||
std::filesystem::file_time_type GetLastWriteTime()
|
||||
const {return last_write_time;}
|
||||
|
||||
/**
|
||||
* @brief watchdog class.
|
||||
@ -73,7 +75,7 @@ class WatchDog{
|
||||
*
|
||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||
*/
|
||||
static std::string timePointToString(const std::filesystem::file_time_type& timePoint);
|
||||
static std::string TimePointToString(const std::filesystem::file_time_type& timePoint);
|
||||
|
||||
/**
|
||||
* @brief watchdog class.
|
||||
@ -82,13 +84,13 @@ class WatchDog{
|
||||
*
|
||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||
*/
|
||||
bool isWatching() const{return watching;}
|
||||
bool IsWatching() const{return watching;}
|
||||
|
||||
private:
|
||||
private:
|
||||
std::string path; //file path
|
||||
bool watching; //Is watchdog on?
|
||||
std::filesystem::file_time_type lastWriteTime;
|
||||
bool hasInitialTime; //checks if initial time is given
|
||||
std::filesystem::file_time_type last_write_time;
|
||||
bool has_initial_time; //checks if initial time is given
|
||||
};
|
||||
|
||||
#endif
|
||||
17
src/main.cpp
17
src/main.cpp
@ -39,18 +39,18 @@ void test_nodes() {
|
||||
*Preston: Test to see if watchdog works :)
|
||||
*/
|
||||
void test_watchdog() {
|
||||
WatchDog wd("test/input.md");
|
||||
wd.start();
|
||||
Watchdog wd("test/input.md");
|
||||
wd.Start();
|
||||
|
||||
std::cout << "Initial check (should do nothing if file unchanged):\n";
|
||||
wd.checkFile();
|
||||
wd.CheckFile();
|
||||
|
||||
std::cout << "Now, modify or create the file 'example.txt' manually and "
|
||||
"press Enter:\n";
|
||||
std::cin.get(); // Wait for user to press Enter
|
||||
|
||||
// Check again after manual change
|
||||
wd.checkFile();
|
||||
wd.CheckFile();
|
||||
|
||||
std::cout << "Done testing.\n";
|
||||
}
|
||||
@ -78,11 +78,4 @@ void test_input(int argc, char **argv) {
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
Parser p("input.md");
|
||||
p.ParseDocument();
|
||||
p.WriteOutput();
|
||||
|
||||
// Parser p2("README.md");
|
||||
// p2.ParseDocument();
|
||||
}
|
||||
int main(int argc, char **argv) { test_watchdog(); }
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
|
||||
|
||||
# Hello world in a h1 tag
|
||||
# Hello world in an h1 tag
|
||||
|
||||
|
||||
## This is a h2 tag
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user