Files
Stress_UI/src/configFile.cpp
2021-03-26 16:00:51 +01:00

105 lines
4.1 KiB
C++
Executable File

#include "configFile.hpp"
#include "textos.hpp"
Config cf::openConfig() {
std::ifstream file;
file.open(cf::g_configFile);
if (file.is_open()) {
std::string line;
line = cf::getLine(&file);
if (line == "") {
return Config(0, "");
}
int8_t numHilos = std::stoi(line);
std::string tiempo = cf::getLine(&file);
if (tiempo == "") {
return Config(0, "");
}
file.close();
return Config(numHilos, tiempo);
} else {
return Config(0, "");
}
}
void cf::closeConfig(Config *config) { delete config; }
std::string cf::getLine(std::ifstream *file) {
std::string line;
if (file->peek() != EOF) {
std::getline(*file, line);
// Mientras se leen las lineas se comprueban que no estén vacias
// o sean comentarios de manera que se devuelva el primera línea
// correcta. De esta manera se puede cambiar el documento añadiendo
// cuantos comentarios o líneas vacias se desee y dinámicamente se
// buscará la línea deseada.
if (line[0] == '#' || line == "") {
return cf::getLine(file);
}
line = line.substr(line.find("= ") + 1, line.length());
return line;
} else {
return "";
}
}
void cf::saveConfig(Config *config) {
std::ofstream file(g_configFile);
std::string header = "#######################################################################";
file << header << std::endl;
file << "# + + + + #" << std::endl;
file << "# + + + #" << std::endl;
file << "# + + #" << std::endl;
file << "# \\ / #" << std::endl;
file << "# + _ - _+_ - ___ #" << std::endl;
file << "# _=. .:. /=\\ _|===|_ || ::| #" << std::endl;
file << "# | | _|. | | | | | | __===_ -=- || ::| #" << std::endl;
file << "# |==| | | __ |.:.| /\\| | :.| | | | .|| : ||| ::| #" << std::endl;
file << "# | |- |.:|_|. :__ |.: |--|==| | .| |_ | . |. ||. ||| :.| #" << std::endl;
file << "# __|. | |_|. | |.|...||---| |==| | | | |_--. || |||. | #" << std::endl;
file << "# | | | |. | | |::.|| :.| |==| | . : |=|===| :|| . ||| .| #" << std::endl;
file << "# |:.| .| | | | |:.:|| . | |==| | |=|===| . | | | | #" << std::endl;
file << "# | | | | | : . | ; ; | #" << std::endl;
file << "# : : . . . : #" << std::endl;
file << "# #" << std::endl;
cf::escribirText(&file, &txt::g_confArchText_1, header.length());
cf::escribirText(&file, &txt::g_confArchText_2, header.length());
file << header << std::endl;
file << std::endl;
file << txt::g_confHilosInput.substr(2,txt::g_confHilosInput.find("\t>") - 2) << "\t\t= " << std::to_string(config->getNumHilos());
file << std::endl;
file << txt::g_confTiempoInput.substr(2,txt::g_confTiempoInput.find("\t>") - 2) << "\t= " << config->getTiempo();
file.close();
}
void cf::escribirText(std::ofstream *file, std::string *text, int numSpcs){
*file << "# " << *text;
for (int i = text->length() + 3; i < numSpcs; i++){
*file << " ";
}
for (int i = 0; i < text->length(); i++) {
if (text->substr(i, 2).compare("á") == 0 |
text->substr(i, 2).compare("é") == 0 |
text->substr(i, 2).compare("í") == 0 |
text->substr(i, 2).compare("ó") == 0 |
text->substr(i, 2).compare("ú") == 0 |
text->substr(i, 2).compare("Á") == 0 |
text->substr(i, 2).compare("É") == 0 |
text->substr(i, 2).compare("Ó") == 0 |
text->substr(i, 2).compare("Ú") == 0 |
text->substr(i, 2).compare("ñ") == 0 |
text->substr(i, 2).compare("Ñ") == 0) {
*file << " ";
}
}
*file << "#" << std::endl;
}