feature/watchdog #16

Merged
azpect merged 2 commits from feature/watchdog into main 2025-10-16 12:16:48 -07:00
6 changed files with 355 additions and 14 deletions

64
.vscode/settings.json vendored
View File

@ -1,3 +1,65 @@
{
"C_Cpp.errorSquiggles": "disabled"
"C_Cpp.errorSquiggles": "disabled",
"files.associations": {
"filesystem": "cpp",
"algorithm": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"deque": "cpp",
"exception": "cpp",
"format": "cpp",
"forward_list": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"locale": "cpp",
"memory": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"vector": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocbuf": "cpp",
"xlocinfo": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xlocnum": "cpp",
"xloctime": "cpp",
"xmemory": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xutility": "cpp"
}
}

View File

@ -1,17 +1,48 @@
#ifndef INLINENODE_H
#define INLINENODE_H
#include "node.h"
class InlineNode : public Node{
public:
InlineNode(std::string content) : content(content);
protected:
#include "node.h"
#include <string>
/**
* @brief InlineNode class.
*
* Inherits from Node. Represents an inline object that is used
* by the DOM tree to convert inline tags from a markup file
* to an HTML file.
*
* @author Preston Shultz
*/
class InlineNode : public Node {
public:
InlineNode(const std::string& content) : content(content) {}
// Optional: allow access to the content
std::string getContent() const { return content; }
protected:
std::string content;
};
class TextNode : public InlineNode{};
class BoldNode : public InlineNode{};
class Italic : public InlineNode{};
class BoldItalic : public InlineNode{};
// These classes inherit the InlineNode constructor using `using`
class TextNode : public InlineNode {
public:
using InlineNode::InlineNode;
};
#endif
class BoldNode : public InlineNode {
public:
using InlineNode::InlineNode;
};
class ItalicNode : public InlineNode {
public:
using InlineNode::InlineNode;
};
class BoldItalicNode : public InlineNode {
public:
using InlineNode::InlineNode;
};
#endif // INLINENODE_H

129
lib/watchDog.cpp Normal file
View File

@ -0,0 +1,129 @@
/*
*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"
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;
}
//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
/*
* 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
//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);
return std::string(buffer);
}

94
lib/watchDog.h Normal file
View File

@ -0,0 +1,94 @@
#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>
/**
* @brief watchdog class.
*
* Checks if a file is modified and displays date modified
*
* @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) {}
/**
* @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(); //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 lastWriteTime;}
/**
* @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 lastWriteTime;
bool hasInitialTime; //checks if initial time is given
};
#endif

View File

@ -1,4 +1,4 @@
#include "../lib/parser.h"
/*#include "../lib/parser.h"
#include <stdexcept>
int main(int argc, char **argv) {
@ -25,3 +25,28 @@ int main(int argc, char **argv) {
return 0;
}
*/
/**
*Preston: Test to see if watchdog works :)
*/
#include "watchDog.h"
#include <iostream>
int main() {
WatchDog wd("test/input.md");
wd.start();
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";
return 0;
}

View File

@ -1,6 +1,6 @@
# Hello world in an h1 tag
# Hello world in a h1 tag
## This is a h2 tag