add a parser to read the UID per user
This commit is contained in:
51
include/aman/config/IdentifierFileFormat.h
Normal file
51
include/aman/config/IdentifierFileFormat.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* @brief Defines the communication file format
|
||||
* @file aman/config/CommunicationFileFormat.h
|
||||
* @author Sven Czarnian <devel@svcz.de>
|
||||
* @copyright Copyright 2021 Sven Czarnian
|
||||
* @license This project is published under the GNU General Public License v3 (GPLv3)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <aman/config/FileFormat.h>
|
||||
#include <aman/types/Communication.h>
|
||||
|
||||
namespace aman {
|
||||
/**
|
||||
* @brief Defines the parser of communication configurations
|
||||
* @ingroup format
|
||||
*
|
||||
* All configuration entries need to be stored in 'AmanCommunication.txt'.
|
||||
*
|
||||
* The following entries of the configuration exist:
|
||||
* <table>
|
||||
* <caption id="multi_row">Setting entries</caption>
|
||||
* <tr>
|
||||
* <th>Name</th><th>Description</th><th>Default value</th><th>Unit</th>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>UID</td>
|
||||
* <td>Defines the identifier to access the backend.</td>
|
||||
* <td></td><td></td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*/
|
||||
class IdentifierFileFormat : public FileFormat {
|
||||
#ifndef DOXYGEN_IGNORE
|
||||
public:
|
||||
/**
|
||||
* @brief Initializes the identifier file format
|
||||
*/
|
||||
IdentifierFileFormat();
|
||||
|
||||
/**
|
||||
* @brief Parses the set configuration file
|
||||
* @param[in] filename The path to the configuration file
|
||||
* @param[out] config The resulting configuration
|
||||
* @return True if the configuration file was valid, else false
|
||||
*/
|
||||
bool parse(const std::string& filename, Communication& config);
|
||||
#endif
|
||||
};
|
||||
}
|
||||
@@ -16,6 +16,7 @@ SET(SOURCE_FILES
|
||||
)
|
||||
SET(SOURCE_CONFIG_FILES
|
||||
config/FileFormat.cpp
|
||||
config/IdentifierFileFormat.cpp
|
||||
)
|
||||
SET(SOURCE_FILES_RES
|
||||
${CMAKE_BINARY_DIR}/ArrivalMANager.rc
|
||||
@@ -24,6 +25,7 @@ SET(SOURCE_FILES_RES
|
||||
)
|
||||
SET(INCLUDE_CONFIG_FILES
|
||||
${CMAKE_SOURCE_DIR}/include/aman/config/FileFormat.h
|
||||
${CMAKE_SOURCE_DIR}/include/aman/config/IdentifierFileFormat.h
|
||||
)
|
||||
SET(INCLUDE_HELPER_FILES
|
||||
${CMAKE_SOURCE_DIR}/include/aman/helper/String.h
|
||||
@@ -57,6 +59,7 @@ IF(MSVC)
|
||||
ENDIF()
|
||||
|
||||
SOURCE_GROUP("Source Files" FILES ${SOURCE_FILES})
|
||||
SOURCE_GROUP("Source Files\\config" FILES ${SOURCE_CONFIG_FILES})
|
||||
SOURCE_GROUP("Source Files\\res" FILES ${SOURCE_FILES_RES})
|
||||
SOURCE_GROUP("Header Files\\config" FILES ${INCLUDE_CONFIG_FILES})
|
||||
SOURCE_GROUP("Header Files\\helper" FILES ${INCLUDE_HELPER_FILES})
|
||||
|
||||
94
src/config/IdentifierFileFormat.cpp
Normal file
94
src/config/IdentifierFileFormat.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
auto entry = String::splitString(trimmed, "=");
|
||||
if (2 > entry.size()) {
|
||||
this->m_errorLine = lineOffset;
|
||||
this->m_errorMessage = "Invalid configuration entry";
|
||||
config.valid = false;
|
||||
return false;
|
||||
}
|
||||
else if (2 < entry.size()) {
|
||||
for (std::size_t idx = 1; idx < entry.size() - 1; ++idx)
|
||||
value += gsl::at(entry, idx) + "=";
|
||||
value += entry.back();
|
||||
}
|
||||
else {
|
||||
value = gsl::at(entry, 1);
|
||||
}
|
||||
|
||||
/* found an invalid line */
|
||||
if (0 == value.length()) {
|
||||
this->m_errorLine = lineOffset;
|
||||
this->m_errorMessage = "Invalid entry";
|
||||
config.valid = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
if ("UID" == gsl::at(entry, 0)) {
|
||||
config.identifier = gsl::at(entry, 1);
|
||||
}
|
||||
else {
|
||||
this->m_errorLine = lineOffset;
|
||||
this->m_errorMessage = "Unknown entry: " + gsl::at(entry, 0);
|
||||
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;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user