Files
aman-es/src/config/IdentifierFileFormat.cpp
2021-08-17 17:30:10 +02:00

84 lines
2.2 KiB
C++

/*
* Author:
* Sven Czarnian <devel@svcz.de>
* Brief:
* Implements the identifier file format
* Copyright:
* 2021 Sven Czarnian
* License:
* GNU General Public License v3 (GPLv3)
*/
#include <fstream>
#include <gsl/gsl>
#include <aman/config/IdentifierFileFormat.h>
#include <aman/helper/String.h>
using namespace aman;
IdentifierFileFormat::IdentifierFileFormat() :
FileFormat() { }
bool IdentifierFileFormat::parse(const std::string& filename, Communication& config) {
config.valid = true;
std::ifstream stream(filename);
if (false == stream.is_open()) {
this->m_errorMessage = "Unable to open the configuration file: " + filename;
this->m_errorLine = 0;
config.valid = false;
return false;
}
std::string line;
std::uint32_t lineOffset = 0;
while (std::getline(stream, line)) {
std::string value;
lineOffset += 1;
/* skip a new line */
if (0 == line.length())
continue;
/* trimm the line and check if a comment line is used */
std::string trimmed = String::trim(line);
if (0 == trimmed.find_first_of('#', 0))
continue;
if (0 == config.serverPublicIdentifier.length()) {
config.serverPublicIdentifier = trimmed;
}
else if (0 == config.clientPublicIdentifier.length()) {
config.clientPublicIdentifier = trimmed;
}
else if (0 == config.clientPrivateIdentifier.length()) {
config.clientPrivateIdentifier = trimmed;
}
else {
this->m_errorLine = lineOffset;
this->m_errorMessage = "Invalid number of keys found";
config.valid = false;
return false;
}
}
if (0 == lineOffset) {
this->m_errorLine = 0;
this->m_errorMessage = "No data found in " + filename;
config.valid = false;
return false;
}
if (0 == config.serverPublicIdentifier.length() || 0 == config.clientPublicIdentifier.length() || 0 == config.clientPrivateIdentifier.length()) {
this->m_errorLine = 0;
this->m_errorMessage = "Not enough keys found";
config.valid = false;
return false;
}
return true;
}