Change separator detection on csv parsing.

This commit is contained in:
Deukhoofd 2021-09-07 18:55:36 +02:00
parent 85b80f4be7
commit d3e95dad61
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 14 additions and 7 deletions

View File

@ -20,6 +20,8 @@ static CreatureLib::Library::Statistic ParseStatistic(const std::string& stat) {
return static_cast<CreatureLib::Library::Statistic>(254); return static_cast<CreatureLib::Library::Statistic>(254);
} }
const char PossibleSeparators[] = {',', ';', '|', '\t', ':'};
PkmnLib::Library::NatureLibrary* BuildNatures::Build(const std::string& path) { PkmnLib::Library::NatureLibrary* BuildNatures::Build(const std::string& path) {
std::ifstream file(path); std::ifstream file(path);
if (file.fail()) { if (file.fail()) {
@ -33,11 +35,13 @@ PkmnLib::Library::NatureLibrary* BuildNatures::Build(const std::string& path) {
return nullptr; return nullptr;
} }
auto divider = ','; auto divider = ',';
if (strncmp(line.c_str(), "sep=", 4) == 0) { for (const auto& s : PossibleSeparators) {
divider = line[4]; if (line.find(s) != std::string::npos) {
std::getline(file, line); divider = s;
break;
}
} }
auto library = new PkmnLib::Library::NatureLibrary(); auto* library = new PkmnLib::Library::NatureLibrary();
std::string substr; std::string substr;
while (std::getline(file, line)) { while (std::getline(file, line)) {
size_t lastStart = 0; size_t lastStart = 0;

View File

@ -1,4 +1,5 @@
#include "BuildTypes.hpp" #include "BuildTypes.hpp"
const char PossibleSeparators[] = {',', ';', '|', '\t', ':'};
CreatureLib::Library::TypeLibrary* BuildTypes::Build(const std::string& path) { CreatureLib::Library::TypeLibrary* BuildTypes::Build(const std::string& path) {
std::ifstream file(path); std::ifstream file(path);
@ -13,9 +14,11 @@ CreatureLib::Library::TypeLibrary* BuildTypes::Build(const std::string& path) {
return nullptr; return nullptr;
} }
auto divider = ','; auto divider = ',';
if (strncmp(line.c_str(), "sep=", 4) == 0) { for (const auto& s :PossibleSeparators) {
divider = line[4]; if (line.find(s) != std::string::npos){
std::getline(file, line); divider = s;
break;
}
} }
auto* library = new CreatureLib::Library::TypeLibrary(); auto* library = new CreatureLib::Library::TypeLibrary();