You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ubl-settings-bootloader/source/util.cc

117 lines
3.4 KiB

#include "util.h"
array<vector<string>, 5> read_csv(const string& filename) {
array<vector<string>, 5> array_vectors;
vector<string> vec_cmd_get;
vector<string> vec_cmd_set_true;
vector<string> vec_cmd_set_false;
vector<string> vec_option;
vector<string> vec_opcision;
ifstream file(filename);
string line;
char delimiter = ',';
getline(file, line);
while (getline(file, line)) {
stringstream stream(line);
string cmd_get;
string cmd_set_true;
string cmd_set_false;
string option;
string opcision;
getline(stream, cmd_get, delimiter);
getline(stream, cmd_set_true, delimiter);
getline(stream, cmd_set_false, delimiter);
getline(stream, option, delimiter);
string line_local = stream.str();
if (line_local.find("\"") != string::npos) {
string str_delimiter = "\"";
vector<int> point = find_all(line_local, str_delimiter);
size_t len = point.size();
if (len >= 2) {
int index_start = point[len-2];
int index_end = point[len-1];
opcision = line_local.substr(index_start, index_end);
index_end = opcision.find("\"");
if (opcision.find("\"") != string::npos) {
opcision.replace(index_end, opcision.length(), "");
}
}
else {
opcision = "error";
}
}
else{
getline(stream, opcision, delimiter);
}
vec_cmd_get.push_back(cmd_get);
vec_cmd_set_true.push_back(cmd_set_true);
vec_cmd_set_false.push_back(cmd_set_false);
vec_option.push_back(option);
vec_opcision.push_back(opcision);
}
array_vectors[0] = vec_cmd_get;
array_vectors[1] = vec_cmd_set_true;
array_vectors[2] = vec_cmd_set_false;
array_vectors[3] = vec_option;
array_vectors[4] = vec_opcision;
return array_vectors;
}
string call(string cmd) {
FILE *fp;
int status;
char path[PATH_MAX] = {0};
fp = popen(cmd.c_str(), "r");
if (fp == NULL) {
exit(1);
}
while (fgets(path, PATH_MAX, fp) != NULL) {
break;
}
status = pclose(fp);
if (status == -1) {
exit(1);
}
return path;
}
vector<int> find_all(string &str_ntp, string substr) {
size_t index = 0;
vector<int> sub_index;
while ((index = str_ntp.find(substr, index)) != std::string::npos) {
index += substr.length();
sub_index.push_back(index);
}
return sub_index;
}
void str_remove(std::string& source, std::string to_remove) {
string::size_type n = to_remove.length();
for (string::size_type i = source.find(to_remove);
i != string::npos;
i = source.find(to_remove))
source.erase(i, n);
}
void str_replace_all(std::string& str_base, string str_find, string sReplacement)
{
size_t pos = 0, fpos;
while ((fpos = str_base.find(str_find, pos)) != std::string::npos)
{
str_base.replace(fpos, str_find.size(), sReplacement);
pos = fpos + sReplacement.size();
}
}
std::vector<std::string> split(std::string text, char delim) {
std::string line;
std::vector<std::string> vec;
std::stringstream ss(text);
while(std::getline(ss, line, delim)) {
vec.push_back(line);
}
return vec;
}