fixing bugs, changing some code from char arrays to c++ std::string

This commit is contained in:
jakani24
2024-03-13 19:10:31 +01:00
parent 717c7956f5
commit 41e0889a6e
38 changed files with 1242 additions and 1038 deletions

View File

@@ -1,78 +1,60 @@
#ifndef THREAD_CTRL_CPP
#define THREAD_CTRL_CPP
#include "thread_ctrl.h"
#include "log.h"
#include "well_known.h"
#include "scan.h"
#include "app_ctrl.h"
#include "update.h"
void split(char* input,const char delimiter, char* out1, char* out2) {
//split a string at the delimiter. the delimiter only occurs once. so the first part is out1 and the second part is out2
int i = 0;
int j = 0;
int k = 0;
while (input[i] != '\0') {
if (input[i] == delimiter) {
out1[j] = '\0';
i++;
while (input[i] != '\0') {
out2[k] = input[i];
i++;
k++;
}
out2[k] = '\0';
return;
}
else {
out1[j] = input[i];
i++;
j++;
}
}
}
int start_thread(const char* command) {
if (can_run_thread()) {
char* out2 = new char[100]; //for the command
char* out1 = new char[300]; //for the arguments
split((char*)command,';', (char*)out1, (char*)out2);
//log(LOGLEVEL::INFO, "[start_thread()]: starting command: ", out1, " with arguments: ",out2);
//printf("out1: %s\n", out1);
//printf("out2: %s\n", out2);
//determine what should be executed
if (strcmp(out1, "scanfile") == 0) {
log(LOGLEVEL::INFO, "[start_thread()]: starting scanfile with arguments: ", out2);
//start a new thread with the scanfile function
std::thread t1(action_scanfile, out2);
t1.detach();
}
else if (strcmp(out1, "scanfolder") == 0) {
//start a new thread with the scanfolder function
log(LOGLEVEL::INFO, "[start_thread()]: starting scanfolder with arguments: ", out2);
std::thread t1(action_scanfolder, out2);
//log(LOGLEVEL::INFO, "[start_thread()]: started scanfolder with arguments: ", out2);
t1.detach();
}
else if (strcmp(out1, "update_settings") == 0) {
//start a new thread with the scanfolder function
log(LOGLEVEL::INFO, "[start_thread()]: starting update_settings with arguments: ", out2);
std::thread t1(action_update_settings);
t1.detach();
}
else if (strcmp(out1, "update_db") == 0) {
//start a new thread with the scanfolder function
log(LOGLEVEL::INFO, "[start_thread()]: starting update_db with arguments: ", out2);
std::thread t1(action_update_settings);
t1.detach();
}
while (can_run_thread()) {
//delay a bit, in order to wait until the thread is started
Sleep(10);
}
delete[] out1;
delete[] out2;
}
return 0;
void split(const std::string& input, char delimiter, std::string& out1, std::string& out2) {
// Split a string at the delimiter. The delimiter only occurs once.
// The first part is stored in out1 and the second part in out2.
size_t pos = input.find(delimiter);
if (pos != std::string::npos) {
out1 = input.substr(0, pos);
out2 = input.substr(pos + 1);
}
}
#endif
int start_thread(const std::string& command) {
if (can_run_thread()) {
std::string out1, out2;
split(command, ';', out1, out2);
log(LOGLEVEL::INFO_NOSEND, "[start_thread()]: command: ", out1, " arguments: ",out2, " call: ",command);
// Determine what should be executed
if (out1 == "scanfile") {
log(LOGLEVEL::INFO, "[start_thread()]: starting scanfile with arguments: ", out2);
// Start a new thread with the scanfile function
std::thread t1(action_scanfile, out2);
t1.detach();
}
else if (out1 == "scanfolder") {
// Start a new thread with the scanfolder function
log(LOGLEVEL::INFO, "[start_thread()]: starting scanfolder with arguments: ", out2);
std::thread t1(action_scanfolder, out2);
t1.detach();
}
else if (out1 == "update_settings") {
// Start a new thread with the update_settings function
log(LOGLEVEL::INFO, "[start_thread()]: starting update_settings with arguments: ", out2);
std::thread t1(action_update_settings);
t1.detach();
}
else if (out1 == "update_db") {
// Start a new thread with the update_db function
log(LOGLEVEL::INFO, "[start_thread()]: starting update_db with arguments: ", out2);
std::thread t1(action_update_db);
t1.detach();
}
while (can_run_thread()) {
// Delay a bit to wait until the thread is started
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
return 0;
}
#endif // THREAD_CTRL_CPP