Fixed coding style issues with watch dog, and began implementing CLI features
This commit is contained in:
parent
26aae7cf98
commit
616cf7260a
28
lib/commandLineParser.cpp
Normal file
28
lib/commandLineParser.cpp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#include "commandLineParser.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
CLI::CLI(int argc, char** argv){
|
||||||
|
if (argc > 0){
|
||||||
|
programName = argv[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < argc; i++){
|
||||||
|
args.push_back(argv[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CLI::RunCommands(){
|
||||||
|
for (int i = 0; i < args.size(); i++){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CLI::PrintHelp() const{
|
||||||
|
std::cout << "Usage:\n"
|
||||||
|
<< " " << programName << " <input_file>\n"
|
||||||
|
<< "Flags:\n"
|
||||||
|
<< " --o, --output <outputFile>, optional output filename\n"
|
||||||
|
<< " --w, --watch, enables watchdog\n"
|
||||||
|
<< " --s, --stop, stops watchdog\n";
|
||||||
|
}
|
||||||
|
|
||||||
69
lib/commandLineParser.h
Normal file
69
lib/commandLineParser.h
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
/**
|
||||||
|
* @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:
|
||||||
|
std::string programName;
|
||||||
|
std::vector<std::string> args;
|
||||||
|
std::string inputFile;
|
||||||
|
std::string outputFile;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -1,64 +1,59 @@
|
|||||||
/*
|
#include "watchdog.h"
|
||||||
*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
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,22 +61,8 @@ bool WatchDog::checkFile(){
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string WatchDog::timePointToString(const fs::file_time_type& timePoint){
|
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
|
* Step 1: timePoint
|
||||||
* - This is the last write time of the file, returned by std::filesystem.
|
* - This is the last write time of the file, returned by std::filesystem.
|
||||||
* - Its clock is platform-dependent (filesystem clock).
|
* - Its clock is platform-dependent (filesystem clock).
|
||||||
@ -114,14 +95,13 @@ std::string WatchDog::timePointToString(const fs::file_time_type& timePoint){
|
|||||||
timePoint - fs::file_time_type::clock::now() + std::chrono::system_clock::now()
|
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);
|
std::time_t timeInSeconds = std::chrono::system_clock::to_time_t(systemTimePoint);
|
||||||
|
|
||||||
//Converts to local time, built in function
|
//Converts to local time, built in function
|
||||||
std::tm localTime = *std::localtime(&timeInSeconds);
|
std::tm localTime = *std::localtime(&timeInSeconds);
|
||||||
|
|
||||||
// Format the time into a string using strftime
|
// Format the time into a string using strftime
|
||||||
//https://en.cppreference.com/w/cpp/chrono/c/strftime.html
|
|
||||||
char buffer[20];
|
char buffer[20];
|
||||||
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &localTime);
|
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
|
#ifndef WATCHDOG_H
|
||||||
#define WATCHDOG_H
|
#define WATCHDOG_H
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
/*
|
#include <filesystem> //allow access to files platform independent
|
||||||
*https://en.cppreference.com/w/cpp/filesystem.html
|
#include <chrono> //makes timestamps easier
|
||||||
*allow access to files platform independent
|
#include <ctime> //Convert time_t to std::tm for local time
|
||||||
*/
|
#include <iomanip> //Formats std::tim into "YYYY-MM-DD HH-MM-SS"
|
||||||
#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.
|
* @brief watchdog class.
|
||||||
*
|
*
|
||||||
@ -21,14 +27,10 @@
|
|||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
#include <ctime> //Convert time_t to std::tm for local time
|
class Watchdog{
|
||||||
#include <iomanip> //Formats std::tim into "YYYY-MM-DD HH-MM-SS"
|
public:
|
||||||
|
Watchdog(const std::string& path) :
|
||||||
|
path(path), watching(false), has_initial_time(false) {}
|
||||||
class WatchDog{
|
|
||||||
public:
|
|
||||||
WatchDog(const std::string& path) :
|
|
||||||
path(path), watching(false), hasInitialTime(false) {}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief watchdog class.
|
* @brief watchdog class.
|
||||||
@ -37,7 +39,7 @@ class WatchDog{
|
|||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
void start();
|
void Start();
|
||||||
/**
|
/**
|
||||||
* @brief watchdog class.
|
* @brief watchdog class.
|
||||||
*
|
*
|
||||||
@ -45,7 +47,7 @@ class WatchDog{
|
|||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
void stop(); //Disable
|
void Stop() {watching = false;} //Disable
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief watchdog class.
|
* @brief watchdog class.
|
||||||
@ -54,7 +56,7 @@ class WatchDog{
|
|||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
bool checkFile();
|
bool CheckFile();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief watchdog class.
|
* @brief watchdog class.
|
||||||
@ -63,8 +65,8 @@ class WatchDog{
|
|||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
std::filesystem::file_time_type getLastWriteTime()
|
std::filesystem::file_time_type GetLastWriteTime()
|
||||||
const {return lastWriteTime;}
|
const {return last_write_time;}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief watchdog class.
|
* @brief watchdog class.
|
||||||
@ -73,7 +75,7 @@ class WatchDog{
|
|||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @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.
|
* @brief watchdog class.
|
||||||
@ -82,13 +84,13 @@ class WatchDog{
|
|||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
bool isWatching() const{return watching;}
|
bool IsWatching() const{return watching;}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string path; //file path
|
std::string path; //file path
|
||||||
bool watching; //Is watchdog on?
|
bool watching; //Is watchdog on?
|
||||||
std::filesystem::file_time_type lastWriteTime;
|
std::filesystem::file_time_type last_write_time;
|
||||||
bool hasInitialTime; //checks if initial time is given
|
bool has_initial_time; //checks if initial time is given
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
10
src/main.cpp
10
src/main.cpp
@ -39,18 +39,18 @@ void test_nodes() {
|
|||||||
*Preston: Test to see if watchdog works :)
|
*Preston: Test to see if watchdog works :)
|
||||||
*/
|
*/
|
||||||
void test_watchdog() {
|
void test_watchdog() {
|
||||||
WatchDog wd("test/input.md");
|
Watchdog wd("test/input.md");
|
||||||
wd.start();
|
wd.Start();
|
||||||
|
|
||||||
std::cout << "Initial check (should do nothing if file unchanged):\n";
|
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 "
|
std::cout << "Now, modify or create the file 'example.txt' manually and "
|
||||||
"press Enter:\n";
|
"press Enter:\n";
|
||||||
std::cin.get(); // Wait for user to press Enter
|
std::cin.get(); // Wait for user to press Enter
|
||||||
|
|
||||||
// Check again after manual change
|
// Check again after manual change
|
||||||
wd.checkFile();
|
wd.CheckFile();
|
||||||
|
|
||||||
std::cout << "Done testing.\n";
|
std::cout << "Done testing.\n";
|
||||||
}
|
}
|
||||||
@ -78,4 +78,4 @@ void test_input(int argc, char **argv) {
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) { test_nodes(); }
|
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
|
## This is a h2 tag
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user