Skip to content

Commit

Permalink
Added "FileConfig::has()" function and modified "FileConfig::set<>()"…
Browse files Browse the repository at this point in the history
… behavior.
  • Loading branch information
Charles-Mahoudeau committed Sep 9, 2024
1 parent 93857e2 commit 08caf7c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
35 changes: 31 additions & 4 deletions lib/system/FileConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ namespace libsystem {
closeFileStream();
}

bool FileConfig::has(const std::string &key) const {
std::vector<std::string> keySplit = getSplicedNamespacedKey(key);

std::shared_ptr<NamespaceNode> namespaceNode = m_mainNode;
while (keySplit.size() > 1) {
if (const std::string namespaceName = keySplit.front(); namespaceNode->hasNamespace(namespaceName)) {
namespaceNode = namespaceNode->getNamespace(namespaceName);
} else {
return false;
}

keySplit.erase(keySplit.begin());
}

return namespaceNode->hasValue(keySplit[0]);
}

FileConfig::file_config_types_t FileConfig::getRaw(const std::string &key) const {
const std::shared_ptr<NamespaceNode> namespaceNode = getNamespaceNodeFromNamespaceKey(key);

Expand All @@ -65,12 +82,18 @@ namespace libsystem {
void FileConfig::setRaw(const std::string &key, const file_config_types_t& value) const {
const std::shared_ptr<NamespaceNode> namespaceNode = getNamespaceNodeFromNamespaceKey(key, true);

// Get only the last part of the key
const std::string keySuffix = key.substr(key.find_last_of('.') + 1);
// keySuffix: Get only the last part of the key
if (const std::string keySuffix = key.substr(key.find_last_of('.') + 1); namespaceNode->hasValue(keySuffix)) {
// Update value
const std::shared_ptr<ValueNode> valueNode = namespaceNode->getValue(keySuffix);

const auto valueNode = std::make_shared<ValueNode>(keySuffix, value);
valueNode->setValue(value);
} else {
// Create value
const auto valueNode = std::make_shared<ValueNode>(keySuffix, value);

namespaceNode->addValueNode(valueNode);
namespaceNode->addValueNode(valueNode);
}
}

std::string FileConfig::toString() const {
Expand Down Expand Up @@ -297,6 +320,10 @@ namespace libsystem {
return m_value;
}

void FileConfig::ValueNode::setValue(const file_config_types_t &value) {
m_value = value;
}

std::string FileConfig::ValueNode::getPath() {
if (getParent() == nullptr) {
return m_key;
Expand Down
4 changes: 4 additions & 0 deletions lib/system/FileConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace libsystem {
*/
void write();

[[nodiscard]] bool has(const std::string &key) const;

// Can't implement in .cpp
/**
* Get a value from the configuration.
Expand Down Expand Up @@ -114,7 +116,9 @@ namespace libsystem {

std::string getKey();
Type getType() const;

file_config_types_t getValue();
void setValue(const file_config_types_t &value);

std::string getPath() override;

Expand Down

0 comments on commit 08caf7c

Please sign in to comment.