fixing bugs, changing some code from char arrays to c++ std::string
This commit is contained in:
@@ -2,54 +2,33 @@
|
||||
#define LOCAL_COM_CPP
|
||||
#include "local_com.h"
|
||||
|
||||
int check_for_com_tasks(const char* com_name, const char* com_path) {
|
||||
//check for new tasks in com file and add them into the queue
|
||||
FILE* fp=nullptr;
|
||||
char* command=new char [300];
|
||||
if ((fopen_s(&fp,com_path, "r")) != 0) {
|
||||
//no com file found = no communication needed
|
||||
delete[] command;
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
while (!feof(fp)) {
|
||||
fscanf_s(fp, "%s", command, 295); // get the command
|
||||
if (!feof(fp)) { //last line =\n we dont want to process that
|
||||
//we scanned the command, now we will scan the path. If the process does not need a path the path will be nopath. (set from client)
|
||||
//get a full path. => " = start, second "= end
|
||||
char* path = new char[300];
|
||||
path[0] = '\0';
|
||||
//search for starting ", then loop until ending "
|
||||
int cnt = 0;
|
||||
int chr = 0;
|
||||
fgetc(fp);
|
||||
fgetc(fp);
|
||||
while (cnt < 295 && chr != '\"') {
|
||||
chr = fgetc(fp); //get a char
|
||||
if(chr!='\"')
|
||||
path[cnt] = chr;
|
||||
path[cnt+1] = '\0';
|
||||
cnt++;
|
||||
}
|
||||
//printf("%s\n", path);
|
||||
//we now have the command and the path. Now we will add it to 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
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
queue_push(queue_entry);
|
||||
//(queue_entry);
|
||||
delete[] queue_entry;
|
||||
delete[] path;
|
||||
}
|
||||
}
|
||||
}
|
||||
int check_for_com_tasks(const std::string& com_name, const std::string& com_path) {
|
||||
std::ifstream file(com_path);
|
||||
|
||||
remove(com_path);
|
||||
fclose(fp);
|
||||
delete[] command;
|
||||
return 0;
|
||||
if (!file.is_open()) {
|
||||
// No com file found = no communication needed
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string command;
|
||||
while (file >> command) {
|
||||
// Scan the command
|
||||
std::string path;
|
||||
file >> path; // Scan the path
|
||||
|
||||
// Add command and path to the queue
|
||||
std::string queue_entry = command + ";" + path;
|
||||
queue_push(queue_entry.c_str());
|
||||
}
|
||||
|
||||
// Remove the com file after processing
|
||||
file.close();
|
||||
std::remove(com_path.c_str());
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif // !LOCAL_COM_CPP
|
||||
#endif // !LOCAL_COM_CPP
|
||||
|
||||
Reference in New Issue
Block a user