Merge pull request 'Feature: Watchdog V2 implementation.' (#31) from feature/watchdog-v2 into main
Reviewed-on: azpect/MarkdownToHtmlCompiler#31
This commit is contained in:
commit
df73ea5c9d
47
input.md
47
input.md
@ -1,47 +0,0 @@
|
|||||||
hello `world`
|
|
||||||
|
|
||||||
|
|
||||||
This `is also a code block`
|
|
||||||
|
|
||||||
hi `mom
|
|
||||||
hello`
|
|
||||||
|
|
||||||
hi `mom
|
|
||||||
|
|
||||||
this is too far`
|
|
||||||
|
|
||||||
|
|
||||||
*this is **words***
|
|
||||||
|
|
||||||
## **Hello world**
|
|
||||||
|
|
||||||
### hello *world*
|
|
||||||
|
|
||||||
# ***This is both!***
|
|
||||||
|
|
||||||
###### This is neither
|
|
||||||
|
|
||||||
- Hello world
|
|
||||||
- This is a list
|
|
||||||
|
|
||||||
|
|
||||||
* this is also a list
|
|
||||||
* this is still a list
|
|
||||||
|
|
||||||
|
|
||||||
1. This list is ordered
|
|
||||||
2. This is **number two**
|
|
||||||
|
|
||||||
- hello
|
|
||||||
world
|
|
||||||
|
|
||||||
- hello
|
|
||||||
|
|
||||||
world number two
|
|
||||||
|
|
||||||
```
|
|
||||||
int x = 5;
|
|
||||||
int y = 10;
|
|
||||||
|
|
||||||
console.log(x + y); // '15'
|
|
||||||
```
|
|
||||||
@ -2,66 +2,68 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept> // for std::invalid_argument
|
#include <stdexcept> // for std::invalid_argument
|
||||||
|
|
||||||
//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];
|
program_name = argv[0];
|
||||||
inputFile = argv[1]
|
input_file = argv[1];
|
||||||
|
|
||||||
//checks that file has correct file extension
|
// checks that file has correct file extension
|
||||||
if(!(CLI::IsMarkupFile(inputFile))) {
|
if (!(CLI::IsMarkupFile(input_file))) {
|
||||||
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
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
// stores remaining arguments
|
||||||
|
for (int i = 1; i < argc; i++) {
|
||||||
|
args.push_back(argv[i]);
|
||||||
}
|
}
|
||||||
|
} catch (const std::invalid_argument &e) {
|
||||||
bool CLI::IsMarkupFile(const std::string& filename) {
|
std::cerr << e.what() << "\n";
|
||||||
//markdown file extension
|
PrintHelp();
|
||||||
const std::string requiredExtension = ".md";
|
std::exit(EXIT_FAILURE);
|
||||||
//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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CLI::IsMarkupFile(const std::string &filename) {
|
||||||
//place to run commands but IDK IF WE SHOULD USE A HASMAP or how we are going to get each method to run
|
// markdown file extension
|
||||||
void CLI::RunCommands(){
|
const std::string requiredExtension = ".md";
|
||||||
for (int i = 0; i < args.size(); i++){
|
// 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{
|
// place to run commands but IDK IF WE SHOULD USE A HASMAP or how we are going
|
||||||
std::cout << "Usage:\n"
|
// to get each method to run
|
||||||
<< " " << programName << " <input_file> REQUIRED\n"
|
void CLI::RunCommands() {
|
||||||
<< "Flags:\n"
|
for (size_t i = 0; i < args.size(); i++) {
|
||||||
<< " --o, --output <outputFile>, optional output filename\n"
|
}
|
||||||
<< " --w, --watch, enables watchdog\n"
|
|
||||||
<< " --s, --stop, stops watchdog\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CLI::PrintHelp() const {
|
||||||
|
std::cout << "Usage:\n"
|
||||||
|
<< "\t" << program_name << " <input_file>\n"
|
||||||
|
<< "Flags:\n"
|
||||||
|
<< "\t-o, --output <output_file>, optional output filename\n"
|
||||||
|
<< "\t-w, --watch, enables watchdog\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// ./parser input_file -w dhuahdakhk -o output
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
/**
|
/**
|
||||||
* @file commandLineParser.h
|
* @file commandLineParser.h
|
||||||
* @brief Parses command line
|
* @brief Parses command line
|
||||||
* @author Preston Shultz
|
* @author Preston Shultz
|
||||||
* Sources:
|
* Sources:
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef COMMAND_LINE_PARSER_H
|
#ifndef COMMAND_LINE_PARSER_H
|
||||||
@ -17,64 +17,63 @@
|
|||||||
*
|
*
|
||||||
* Parse input arguments, sets input and output files
|
* Parse input arguments, sets input and output files
|
||||||
* Lets user use commands with program
|
* Lets user use commands with program
|
||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
class CLI{
|
class CLI {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Takes in argc and argv and converts it to vector
|
* @brief Takes in argc and argv and converts it to vector
|
||||||
*
|
*
|
||||||
* CLI constructor
|
* CLI constructor
|
||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
CLI(int argc, char **argv);
|
CLI(int argc, char **argv);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Runs Commands
|
* @brief Runs Commands
|
||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
void RunCommands();
|
void RunCommands();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Prints a list of commands that can be used
|
* @brief Prints a list of commands that can be used
|
||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
void PrintHelp() const;
|
void PrintHelp() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns input file
|
* @brief Returns input file
|
||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
std::string GetInputFile() const {return inputFile;}
|
std::string GetInputFile() const { return input_file; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns output file
|
* @brief Returns output file
|
||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
std::string GetOutputFile() const {return outputFile;}
|
std::string GetOutputFile() const { return output_file; }
|
||||||
|
|
||||||
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 program_name;
|
||||||
* @brief Ensures the provided filename has a .markup extension
|
std::vector<std::string> args;
|
||||||
*
|
std::string input_file;
|
||||||
* @param filename The file name to validate
|
std::string output_file;
|
||||||
* @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
|
#endif
|
||||||
|
|||||||
33
lib/documentConverter.h
Normal file
33
lib/documentConverter.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#ifndef DOCUMENT_CONVERTER_H
|
||||||
|
#define DOCUMENT_CONVERTER_H
|
||||||
|
|
||||||
|
#include "parser.h"
|
||||||
|
#include "watchdog.h"
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
class DocumentConverter {
|
||||||
|
private:
|
||||||
|
Parser parser;
|
||||||
|
Watchdog watchdog;
|
||||||
|
|
||||||
|
public:
|
||||||
|
DocumentConverter(std::string input, std::string output = "")
|
||||||
|
: parser(input, output), watchdog(input) {};
|
||||||
|
|
||||||
|
void Convert() {
|
||||||
|
this->parser.ParseDocument();
|
||||||
|
this->parser.WriteOutput();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConvertWatcher() {
|
||||||
|
// This is a lambda, which can be passed into the start function of
|
||||||
|
// watchdog.
|
||||||
|
std::function<void()> callback = [this]() {
|
||||||
|
this->parser.ParseDocument();
|
||||||
|
this->parser.WriteOutput();
|
||||||
|
};
|
||||||
|
this->watchdog.Start(callback);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -2,6 +2,7 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|||||||
@ -12,7 +12,8 @@ using std::string;
|
|||||||
using std::vector;
|
using std::vector;
|
||||||
|
|
||||||
void Parser::Inspect() {
|
void Parser::Inspect() {
|
||||||
std::cerr << "Parser::Inspect() is not yet implemented." << std::endl;
|
std::cout << this->position << std::endl;
|
||||||
|
std::cout << this->content.size() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Parser::NormalizeInputStream() {
|
void Parser::NormalizeInputStream() {
|
||||||
@ -41,6 +42,9 @@ void Parser::WriteOutput() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Parser::ParseDocument() {
|
void Parser::ParseDocument() {
|
||||||
|
// NOTE:This needs to be set so the parsing can continue
|
||||||
|
this->position = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this->content = this->filesystem.ReadInputFile();
|
this->content = this->filesystem.ReadInputFile();
|
||||||
} catch (const std::runtime_error &e) {
|
} catch (const std::runtime_error &e) {
|
||||||
|
|||||||
123
lib/watchDog.cpp
123
lib/watchDog.cpp
@ -1,123 +0,0 @@
|
|||||||
#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;
|
|
||||||
|
|
||||||
//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;
|
|
||||||
}
|
|
||||||
|
|
||||||
//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 modified
|
|
||||||
if (currentWriteTime != lastWriteTime)
|
|
||||||
{
|
|
||||||
lastWriteTime = currentWriteTime;
|
|
||||||
std::cout << "WatchDog: File modifed at "
|
|
||||||
<< timePointToString(lastWriteTime) << 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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()
|
|
||||||
);
|
|
||||||
|
|
||||||
//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
|
|
||||||
char buffer[20];
|
|
||||||
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &localTime);
|
|
||||||
|
|
||||||
return std::string(buffer);
|
|
||||||
}
|
|
||||||
@ -1,96 +0,0 @@
|
|||||||
/**
|
|
||||||
* @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>
|
|
||||||
#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.
|
|
||||||
*
|
|
||||||
* Checks if a file is modified and displays date modified
|
|
||||||
*
|
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
|
||||||
*/
|
|
||||||
class Watchdog{
|
|
||||||
public:
|
|
||||||
Watchdog(const std::string& path) :
|
|
||||||
path(path), watching(false), has_initial_time(false) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief watchdog class.
|
|
||||||
*
|
|
||||||
* Starts the watchdog to check of a file is modified
|
|
||||||
*
|
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
|
||||||
*/
|
|
||||||
void Start();
|
|
||||||
/**
|
|
||||||
* @brief watchdog class.
|
|
||||||
*
|
|
||||||
* Stops the watchdog
|
|
||||||
*
|
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
|
||||||
*/
|
|
||||||
void Stop() {watching = false;} //Disable
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief watchdog class.
|
|
||||||
*
|
|
||||||
* Checks if a file has been modified
|
|
||||||
*
|
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
|
||||||
*/
|
|
||||||
bool CheckFile();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief watchdog class.
|
|
||||||
*
|
|
||||||
* Returns files last modified date
|
|
||||||
*
|
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
|
||||||
*/
|
|
||||||
std::filesystem::file_time_type GetLastWriteTime()
|
|
||||||
const {return last_write_time;}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief watchdog class.
|
|
||||||
*
|
|
||||||
* Converts time point into a string
|
|
||||||
*
|
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
|
||||||
*/
|
|
||||||
static std::string TimePointToString(const std::filesystem::file_time_type& timePoint);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief watchdog class.
|
|
||||||
*
|
|
||||||
* Returns if the watchDog is on
|
|
||||||
*
|
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
|
||||||
*/
|
|
||||||
bool IsWatching() const{return watching;}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::string path; //file path
|
|
||||||
bool watching; //Is watchdog on?
|
|
||||||
std::filesystem::file_time_type last_write_time;
|
|
||||||
bool has_initial_time; //checks if initial time is given
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
87
lib/watchdog.cpp
Normal file
87
lib/watchdog.cpp
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
#include "watchdog.h"
|
||||||
|
#include <chrono>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
using namespace std::chrono;
|
||||||
|
|
||||||
|
void Watchdog::Start(std::function<void()> callback) {
|
||||||
|
// checks if file exist
|
||||||
|
if (!fs::exists(this->path))
|
||||||
|
throw std::runtime_error("File does not exist.");
|
||||||
|
|
||||||
|
// Loop forever and check the file.
|
||||||
|
while (true) {
|
||||||
|
CheckFile(callback);
|
||||||
|
std::this_thread::sleep_for(this->POLLING_INTERVAL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Watchdog::CheckFile(std::function<void()> callback) {
|
||||||
|
// LONG STORY SHORT:
|
||||||
|
// When a file is written to, it aquires an OS lock, which means our program
|
||||||
|
// cannot access it. So when we request, it fails. So we should basically just
|
||||||
|
// keep trying until we get access.
|
||||||
|
//
|
||||||
|
// After implementing some retry logic, I was unable to replicate the error.
|
||||||
|
|
||||||
|
for (int attempt = 0; attempt < this->MAX_RETRIES; ++attempt) {
|
||||||
|
try {
|
||||||
|
// Checking if file was deleted
|
||||||
|
if (!fs::exists(this->path))
|
||||||
|
throw std::runtime_error(
|
||||||
|
"File could not be found. Maybe it was deleted?");
|
||||||
|
|
||||||
|
// Built in function with file system to check last write time
|
||||||
|
fs::file_time_type currentWriteTime = fs::last_write_time(path);
|
||||||
|
|
||||||
|
// File modified
|
||||||
|
if (currentWriteTime != this->last_write_time) {
|
||||||
|
this->last_write_time = currentWriteTime;
|
||||||
|
|
||||||
|
time_point before = high_resolution_clock::now();
|
||||||
|
callback();
|
||||||
|
time_point after = high_resolution_clock::now();
|
||||||
|
|
||||||
|
duration dur = after - before;
|
||||||
|
long ms = std::chrono::duration_cast<milliseconds>(dur).count();
|
||||||
|
long us = std::chrono::duration_cast<microseconds>(dur).count();
|
||||||
|
|
||||||
|
if (ms > 0) {
|
||||||
|
std::cout << std::endl
|
||||||
|
<< "Recompiled in \033[36m" << ms << "ms\033[0m" << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << std::endl
|
||||||
|
<< "Recompiled in \033[36m" << us << "μs\033[0m" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (const std::exception &ex) {
|
||||||
|
// On last attempt, bubble the error outward
|
||||||
|
if (attempt == this->MAX_RETRIES - 1)
|
||||||
|
throw std::runtime_error("Watchdog failed after multiple retries: " +
|
||||||
|
std::string(ex.what()));
|
||||||
|
|
||||||
|
// Wait and then try again
|
||||||
|
std::this_thread::sleep_for(this->RETRY_DELAY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Watchdog::TimePointToString(const fs::file_time_type &timePoint) {
|
||||||
|
system_clock::time_point systemTimePoint =
|
||||||
|
time_point_cast<system_clock::duration>(
|
||||||
|
timePoint - fs::file_time_type::clock::now() + system_clock::now());
|
||||||
|
|
||||||
|
// Converts to seconds
|
||||||
|
std::time_t timeInSeconds = 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
|
||||||
|
char buffer[20];
|
||||||
|
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &localTime);
|
||||||
|
|
||||||
|
return std::string(buffer);
|
||||||
|
}
|
||||||
111
lib/watchdog.h
Normal file
111
lib/watchdog.h
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
/**
|
||||||
|
* @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 <chrono> //makes timestamps easier
|
||||||
|
#include <ctime> //Convert time_t to std::tm for local time
|
||||||
|
#include <filesystem> //allow access to files platform independent
|
||||||
|
#include <functional>
|
||||||
|
#include <iomanip> //Formats std::tim into "YYYY-MM-DD HH-MM-SS"
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief watchdog class.
|
||||||
|
*
|
||||||
|
* Checks if a file is modified and displays date modified
|
||||||
|
*
|
||||||
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
|
*/
|
||||||
|
class Watchdog {
|
||||||
|
protected:
|
||||||
|
/**
|
||||||
|
* @brief watchdog class.
|
||||||
|
*
|
||||||
|
* Checks if a file has been modified
|
||||||
|
*
|
||||||
|
* @param callback Callback function to execute when the watchdog notices a
|
||||||
|
* change.
|
||||||
|
*
|
||||||
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
|
* @author Hayden Hargreaves (hhargreaves2006@gmail.com)
|
||||||
|
*/
|
||||||
|
void CheckFile(std::function<void()> callback);
|
||||||
|
|
||||||
|
public:
|
||||||
|
Watchdog(const std::string &path) : path(path) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief watchdog class.
|
||||||
|
*
|
||||||
|
* Starts the watchdog to check of a file is modified
|
||||||
|
*
|
||||||
|
* @param callback Callback function to execute when the watchdog notices a
|
||||||
|
* change.
|
||||||
|
*
|
||||||
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
|
*/
|
||||||
|
void Start(std::function<void()> callback);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief watchdog class.
|
||||||
|
*
|
||||||
|
* Converts time point into a string
|
||||||
|
*
|
||||||
|
* Details below:
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
|
*/
|
||||||
|
static std::string
|
||||||
|
TimePointToString(const std::filesystem::file_time_type &timePoint);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string path;
|
||||||
|
std::filesystem::file_time_type last_write_time;
|
||||||
|
|
||||||
|
// Timing values for the watchdog
|
||||||
|
const std::chrono::milliseconds POLLING_INTERVAL =
|
||||||
|
std::chrono::milliseconds(100);
|
||||||
|
const int MAX_RETRIES = 20;
|
||||||
|
const std::chrono::milliseconds RETRY_DELAY = std::chrono::milliseconds(20);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
25
src/main.cpp
25
src/main.cpp
@ -1,7 +1,8 @@
|
|||||||
|
#include "../lib/documentConverter.h"
|
||||||
#include "../lib/inlineNode.h"
|
#include "../lib/inlineNode.h"
|
||||||
#include "../lib/parser.h"
|
#include "../lib/parser.h"
|
||||||
#include "../lib/structureNode.h"
|
#include "../lib/structureNode.h"
|
||||||
#include "../lib/watchDog.h"
|
#include "../lib/watchdog.h"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@ -40,19 +41,7 @@ void test_nodes() {
|
|||||||
*/
|
*/
|
||||||
void test_watchdog() {
|
void test_watchdog() {
|
||||||
Watchdog wd("test/input.md");
|
Watchdog wd("test/input.md");
|
||||||
wd.Start();
|
wd.Start(nullptr);
|
||||||
|
|
||||||
std::cout << "Initial check (should do nothing if file unchanged):\n";
|
|
||||||
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();
|
|
||||||
|
|
||||||
std::cout << "Done testing.\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_input(int argc, char **argv) {
|
void test_input(int argc, char **argv) {
|
||||||
@ -78,4 +67,10 @@ void test_input(int argc, char **argv) {
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) { test_watchdog(); }
|
void test_document_converter() {
|
||||||
|
DocumentConverter dc("test/input.md");
|
||||||
|
dc.ConvertWatcher();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
test_document_converter(); }
|
||||||
|
|||||||
45
syntax.md
45
syntax.md
@ -1,45 +0,0 @@
|
|||||||
|
|
||||||
Reference [here](https://www.markdownguide.org/basic-syntax/)
|
|
||||||
|
|
||||||
Headings, h# tags
|
|
||||||
|
|
||||||
|
|
||||||
# Header Level 1 -> <h1> Content </h1>
|
|
||||||
## Header Level 2 -> <h2> Content </h2>
|
|
||||||
### Header Level 3 -> <h3> Content </h3>
|
|
||||||
#### Header Level 4 -> <h4> Content </h4>
|
|
||||||
##### Header Level 5 -> <h5> Content </h5>
|
|
||||||
###### Header Level 6 -> <h6> Content </h6>
|
|
||||||
|
|
||||||
|
|
||||||
Alternate syntax (n number of =/-)
|
|
||||||
|
|
||||||
Header Level 1 -> <h1> Content </h1>
|
|
||||||
================
|
|
||||||
|
|
||||||
|
|
||||||
Header Level 2 -> <h2> Content </h2>
|
|
||||||
----------------
|
|
||||||
|
|
||||||
|
|
||||||
Paragraph tags
|
|
||||||
|
|
||||||
Hello world -> <p> Hello world </p>
|
|
||||||
|
|
||||||
This is also
|
|
||||||
a paragraph -> <p> this is also a paragraph regardless </p>
|
|
||||||
regardless
|
|
||||||
|
|
||||||
However
|
|
||||||
this is a break, because it ends with two spaces -> <p> However <br> this is a break, because it ends with two spaces </p>
|
|
||||||
|
|
||||||
Double returns also
|
|
||||||
|
|
||||||
yields new paragraphs -> <p> Double returns also</p> <p> yields new paragraphs </p>
|
|
||||||
|
|
||||||
|
|
||||||
*italic* -> <em>italic</em>
|
|
||||||
**bold** -> <strong>bold</strong>
|
|
||||||
***italic bold*** -> <strong><em>italic bold</em></strong>
|
|
||||||
|
|
||||||
hello **world** -> [TextClass: hello, BoldClass: world]
|
|
||||||
25
test/input.html
Normal file
25
test/input.html
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Document</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>Reference [here](https://www.markdownguide.org/basic-syntax/)</p>
|
||||||
|
<p>Headings, h# tags</p>
|
||||||
|
<h1>Header Level 1 -> <h1> Content </h1> ## Header Level 2 -> <h2> Content </h2> ### Header Level 3 -> <h3> Content </h3> #### Header Level 4 -> <h4> Content </h4> ##### Header Level 5 -> <h5> Content </h5> ###### Header Level 6 -> <h6> Content </h6></h1>
|
||||||
|
<p>Alternate syntax (n number of =/-)</p>
|
||||||
|
<p>Header Level 1 -> <h1> Content </h1> ================</p>
|
||||||
|
<p>Header Level 2 -> <h2> Content </h2> ----------------</p>
|
||||||
|
<p>Paragraph tags</p>
|
||||||
|
<p>Hello world -> <p> Hello world </p></p>
|
||||||
|
<p>This is also a paragraph -> <p> this is also a paragraph regardless </p> regardless </p>
|
||||||
|
<p>However this is a break, because it ends with two spaces -> <p> However <br> this is a break, because it ends with two spaces </p></p>
|
||||||
|
<p>Double returns also</p>
|
||||||
|
<p>yields new paragraphs -> <p> Double returns also</p> <p> yields new paragraphs </p></p>
|
||||||
|
<p><em>italic</em> -> <em>italic</em> <strong>bold</strong> -> <strong>bold</strong> <strong><em>italic bold</em></strong> -> <strong><em>italic bold</em></strong></p>
|
||||||
|
<p>hello <strong>world</strong> -> [TextClass: hello, BoldClass: world] </p>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -1,17 +1,44 @@
|
|||||||
|
Reference [here](https://www.markdownguide.org/basic-syntax/)
|
||||||
|
|
||||||
|
Headings, h# tags
|
||||||
|
|
||||||
|
|
||||||
# Hello world in an h1 tag
|
# Header Level 1 -> <h1> Content </h1>
|
||||||
|
## Header Level 2 -> <h2> Content </h2>
|
||||||
|
### Header Level 3 -> <h3> Content </h3>
|
||||||
|
#### Header Level 4 -> <h4> Content </h4>
|
||||||
|
##### Header Level 5 -> <h5> Content </h5>
|
||||||
|
###### Header Level 6 -> <h6> Content </h6>
|
||||||
|
|
||||||
|
|
||||||
## This is a h2 tag
|
Alternate syntax (n number of =/-)
|
||||||
|
|
||||||
|
Header Level 1 -> <h1> Content </h1>
|
||||||
|
================
|
||||||
|
|
||||||
|
|
||||||
### h3
|
Header Level 2 -> <h2> Content </h2>
|
||||||
|
----------------
|
||||||
|
|
||||||
|
|
||||||
#### h4
|
Paragraph tags
|
||||||
|
|
||||||
##### h5
|
Hello world -> <p> Hello world </p>
|
||||||
|
|
||||||
###### h6
|
This is also
|
||||||
|
a paragraph -> <p> this is also a paragraph regardless </p>
|
||||||
|
regardless
|
||||||
|
|
||||||
|
However
|
||||||
|
this is a break, because it ends with two spaces -> <p> However <br> this is a break, because it ends with two spaces </p>
|
||||||
|
|
||||||
|
Double returns also
|
||||||
|
|
||||||
|
yields new paragraphs -> <p> Double returns also</p> <p> yields new paragraphs </p>
|
||||||
|
|
||||||
|
|
||||||
|
*italic* -> <em>italic</em>
|
||||||
|
**bold** -> <strong>bold</strong>
|
||||||
|
***italic bold*** -> <strong><em>italic bold</em></strong>
|
||||||
|
|
||||||
|
hello **world** -> [TextClass: hello, BoldClass: world]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user