229 lines
7.6 KiB
C++
229 lines
7.6 KiB
C++
/*****************************************************************************
|
|
* Copyright (c) 2020, rrcSmall FM10K-Documentation Contributors
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* * Neither the name of the copyright holder nor the names of its contributors
|
|
* may be used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*****************************************************************************/
|
|
|
|
#include "Configuration.h"
|
|
|
|
#include <algorithm>
|
|
|
|
bool Configuration::addEntry(const std::string &entry) {
|
|
if (entry.empty() || entry[0] == '#') {
|
|
return false;
|
|
}
|
|
std::string fullKey;
|
|
std::string currentKey;
|
|
fullKey.reserve(32);
|
|
currentKey.reserve(16);
|
|
|
|
uint32_t state = 0;
|
|
|
|
ConfigurationNode *currentNode = &mainNode;
|
|
|
|
for (size_t i = 0; i < entry.size(); ++i) {
|
|
auto c = entry[i];
|
|
if (state == 0) {
|
|
if ((c == '.' || c == ' ' || c == '\t')) {
|
|
if (!currentKey.empty()) {
|
|
currentNode = ¤tNode->getOrCreateSubNode(currentKey);
|
|
|
|
fullKey += currentKey;
|
|
|
|
currentKey.clear();
|
|
|
|
if (c == ' ' || c == '\t') {
|
|
state = 1;
|
|
} else {
|
|
fullKey += ".";
|
|
}
|
|
}
|
|
} else {
|
|
currentKey += c;
|
|
}
|
|
} else if (state == 1) {
|
|
if ((c == ' ' || c == '\t')) {
|
|
if (!currentKey.empty()) {
|
|
if (currentKey == "int") {
|
|
currentNode->type = ConfigurationNode::Type::ValueInt;
|
|
} else if (currentKey == "text") {
|
|
currentNode->type = ConfigurationNode::Type::ValueText;
|
|
} else if (currentKey == "bool") {
|
|
currentNode->type = ConfigurationNode::Type::ValueBool;
|
|
}
|
|
|
|
currentKey.clear();
|
|
|
|
state = 2;
|
|
}
|
|
} else {
|
|
currentKey += c;
|
|
}
|
|
} else if (state == 2) {
|
|
if (i == (entry.size() - 1)) {
|
|
currentKey += c;
|
|
currentNode->value = currentKey;
|
|
if (std::find(orderedNodes.begin(), orderedNodes.end(), fullKey) == orderedNodes.end()) {
|
|
orderedNodes.emplace_back(fullKey);
|
|
}
|
|
} else if ((c == ' ' || c == '\t') && currentKey.empty()) {
|
|
|
|
} else {
|
|
currentKey += c;
|
|
}
|
|
}
|
|
}
|
|
|
|
return state == 2;
|
|
}
|
|
|
|
const Configuration::ConfigurationNode &Configuration::getEntry(const std::string &entry) const {
|
|
if (entry.empty() || entry[0] == '#') {
|
|
return mainNode;
|
|
}
|
|
std::string currentKey;
|
|
currentKey.reserve(16);
|
|
|
|
const ConfigurationNode *currentNode = &mainNode;
|
|
|
|
for (char c : entry) {
|
|
if ((c == '.' || c == ' ' || c == '\t')) {
|
|
if (!currentKey.empty()) {
|
|
currentNode = currentNode->getSubNode(currentKey);
|
|
|
|
if (currentNode == nullptr) {
|
|
return mainNode;
|
|
}
|
|
|
|
currentKey.clear();
|
|
|
|
if (c == ' ' || c == '\t') {
|
|
return *currentNode;
|
|
}
|
|
}
|
|
} else {
|
|
currentKey += c;
|
|
}
|
|
}
|
|
|
|
if (!currentKey.empty()) {
|
|
currentNode = currentNode->getSubNode(currentKey);
|
|
if (currentNode != nullptr) {
|
|
return *currentNode;
|
|
}
|
|
}
|
|
|
|
return mainNode;
|
|
}
|
|
|
|
void
|
|
Configuration::getAllEntriesForNode(std::vector<std::string> &entries, const Configuration::ConfigurationNode &node,
|
|
const std::string &key) const {
|
|
for (const auto &n : node.possibleNodes) {
|
|
if (n.type != ConfigurationNode::Type::Node) {
|
|
entries.push_back(key + n.name + " " + n.getTypeString() + " " + n.value);
|
|
} else {
|
|
getAllEntriesForNode(entries, n, key + n.name + ".");
|
|
}
|
|
}
|
|
}
|
|
|
|
std::vector<std::string> Configuration::getAllEntries() const {
|
|
std::vector<std::string> entries;
|
|
|
|
for (const auto &key : orderedNodes) {
|
|
auto &v = getEntry(key);
|
|
entries.push_back(key + " " + v.getTypeString() + " " + v.value);
|
|
}
|
|
|
|
return entries;
|
|
}
|
|
|
|
Configuration::ConfigurationNode &Configuration::ConfigurationNode::getOrCreateSubNode(const std::string &k) {
|
|
for (auto &node : possibleNodes) {
|
|
if (node.name == k) {
|
|
return node;
|
|
}
|
|
}
|
|
|
|
possibleNodes.emplace_back(ConfigurationNode(k, ConfigurationNode::Type::Node));
|
|
return possibleNodes[possibleNodes.size() - 1];
|
|
}
|
|
|
|
const Configuration::ConfigurationNode *Configuration::ConfigurationNode::getSubNode(const std::string &k) const {
|
|
for (auto &node : possibleNodes) {
|
|
if (node.name == k) {
|
|
return &node;
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
std::pair<uint32_t, uint32_t> Configuration::ConfigurationNode::getEUI64ToInteger() const {
|
|
std::pair<uint32_t, uint32_t> pair{0xFFFFFFFF, 0xFFFFFFFF};
|
|
uint32_t b[8];
|
|
int scanCount;
|
|
|
|
/* Try with the ':' delimiter */
|
|
scanCount = sscanf(getText().c_str(),
|
|
"%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x",
|
|
&b[0],
|
|
&b[1],
|
|
&b[2],
|
|
&b[3],
|
|
&b[4],
|
|
&b[5],
|
|
&b[6],
|
|
&b[7]);
|
|
|
|
/* Try with the '-' delimiter */
|
|
if (scanCount == 0) {
|
|
scanCount = sscanf(getText().c_str(),
|
|
"%2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x",
|
|
&b[0],
|
|
&b[1],
|
|
&b[2],
|
|
&b[3],
|
|
&b[4],
|
|
&b[5],
|
|
&b[6],
|
|
&b[7]);
|
|
}
|
|
|
|
if (scanCount == 8) {
|
|
pair.second = (b[0] & 0xFF) << 24;
|
|
pair.second |= (b[1] & 0xFF) << 16;
|
|
pair.second |= (b[2] & 0xFF) << 8;
|
|
pair.second |= (b[3] & 0xFF);
|
|
|
|
pair.first = (b[4] & 0xFF) << 24;
|
|
pair.first |= (b[5] & 0xFF) << 16;
|
|
pair.first |= (b[6] & 0xFF) << 8;
|
|
pair.first |= (b[7] & 0xFF);
|
|
}
|
|
|
|
return pair;
|
|
}
|