updating scheduled tasks

This commit is contained in:
jakani24
2024-02-17 15:09:25 +01:00
parent f21276fd5c
commit 57cf07b679
19 changed files with 118 additions and 77 deletions

View File

@@ -85,7 +85,7 @@ void unlock_task(const std::string& task_name) {
int check_for_sched_tasks(const char* sched_name, const char* sched_path) {
FILE* fp = nullptr;
char* command = new char[300];
char* command = new char[505];
if ((fopen_s(&fp, sched_path, "r")) != 0) {
//panic, create log entry, return 1;
//no schedule file found. this is not normal
@@ -94,77 +94,87 @@ int check_for_sched_tasks(const char* sched_name, const char* sched_path) {
return 1;
}
else {
fscanf_s(fp, "%s", command, 500); //the cert is always the firs tline
fgetc(fp); //get the newline
while (!feof(fp)) {
//read date-time config. it starts with " and ends with "
char* datetime = new char[300];
datetime[0] = '\0';
//search for datetime, starting ", then loop until ending "
int cnt = 0;
int chr = 0;
chr = fgetc(fp);//read in the first ", or at least try it
//printf("%c\n", chr);
if (chr == '\"'){
if (!feof(fp)) {
//read date-time config. it starts with " and ends with "
char* datetime = new char[300];
datetime[0] = '\0';
//search for datetime, starting ", then loop until ending "
int cnt = 0;
int chr = 0;
chr = fgetc(fp);//read in the first ", or at least try it
//printf("%c\n", chr);
if (chr == '\"') {
chr = 0;
while (cnt < 295 && chr != '\"') {
chr = fgetc(fp); //get a char
if (chr != '\"')
datetime[cnt] = chr;
datetime[cnt + 1] = '\0';
cnt++;
//printf("scanning...\n");
}
}
//now we had datetime. we can scan the command and the path now
fscanf_s(fp, "%s", command, 295); // get the command
char* path = new char[300];
path[0] = '\0';
//search for arg, starting ", then loop until ending "
cnt = 0;
chr = 0;
while (cnt < 295 && chr != '\"') {
chr = fgetc(fp); //get a char
if (chr != '\"')
datetime[cnt] = chr;
datetime[cnt + 1] = '\0';
cnt++;
//printf("scanning...\n");
}
}
//now we had datetime. we can scan the command and the path now
fscanf_s(fp, "%s", command, 295); // get the command
char* path = new char[300];
path[0] = '\0';
//search for datetime, starting ", then loop until ending "
cnt = 0;
chr = 0;
fgetc(fp); //get th ewhitespoace after the command
chr = fgetc(fp);//read in the first ", or at least try it
if (chr == '\"') {
chr = 0;
while (cnt < 295 && chr != '\"') {
chr = fgetc(fp); //get a char
if (chr != '\"')
path[cnt] = chr;
path[cnt + 1] = '\0';
cnt++;
}
}
//now get the taskname
char* taskname = new char[300];
taskname[0] = '\0';
fscanf_s(fp, "%s", taskname, 295); // get the taskname
fgetc(fp); //get th ewhitespoace after the command
chr = fgetc(fp);//read in the first ", or at least try it
//lets check if the command should be executed
//get the current time
std::time_t t = std::time(nullptr);
struct std::tm current_time;
localtime_s(&current_time, &t);
//printf("%s\n", datetime);
if (strcmp(datetime, "") != 0 && strcmp(command, "") != 0 && strcmp(path, "") != 0 && strcmp(taskname, "") != 0) {
if (is_task_due(taskname,datetime, current_time)) {
//printf("command:%s\n", command);
//printf("path:%s\n", path);
//now we can build up the command for the queue
char* queue_entry = new char[300 * 2 + 5]; //to enshure we have enough space
queue_entry[0] = '\0';
strcpy_s(queue_entry, 600, command); //copy the command
strcat_s(queue_entry, 600, ";"); //add a ; to seperate command and path
strcat_s(queue_entry, 600, path); //add the path
queue_push(queue_entry);
delete[] queue_entry;
if (chr == '\"') {
chr = 0;
while (cnt < 295 && chr != '\"') {
chr = fgetc(fp); //get a char
if (chr != '\"')
path[cnt] = chr;
path[cnt + 1] = '\0';
cnt++;
}
}
//now get the taskname
char* taskname = new char[300];
taskname[0] = '\0';
fscanf_s(fp, "%s", taskname, 295); // get the taskname
fgetc(fp); //get the newline
//lets check if the command should be executed
//get the current time
std::time_t t = std::time(nullptr);
struct std::tm current_time;
localtime_s(&current_time, &t);
//printf("%s\n", datetime);
if (strcmp(datetime, "") != 0 && strcmp(command, "") != 0 && strcmp(path, "") != 0 && strcmp(taskname, "") != 0) {
if (is_task_due(taskname, datetime, current_time)) {
//printf("command:%s\n", command);
//printf("path:%s\n", path);
//now we can build up the command for the queue
char* queue_entry = new char[300 * 2 + 5]; //to enshure we have enough space
queue_entry[0] = '\0';
strcpy_s(queue_entry, 600, command); //copy the command
strcat_s(queue_entry, 600, ";"); //add a ; to seperate command and path
strcat_s(queue_entry, 600, path); //add the path
queue_push(queue_entry);
delete[] queue_entry;
}
}
//else {
// printf("datetime:%s\n", datetime);
// printf("command:%s\n", command);
// printf("path:%s\n", path);
// printf("taskname:%s\n", taskname);
// log(LOGLEVEL::ERR, "[check_for_sched_tasks()]: Error reading schedule file: ", sched_path, " while checking for scheduled tasks (malformat); aborting");
//}
delete[] datetime;
delete[] path;
delete[] taskname;
}
else {
log(LOGLEVEL::ERR, "[check_for_sched_tasks()]: Error reading schedule file: ", sched_path, " while checking for scheduled tasks (malformat); aborting");
}
delete[] datetime;
delete[] path;
delete[] taskname;
}
}
delete[] command;