25#include "configuration_tree.hpp"
27#include <unordered_set>
41void importTree(boost::property_tree::ptree
const &
root, Config *config)
43 for (boost::property_tree::ptree::value_type
const &node :
root) {
44 std::string key = node.first;
45 if (key ==
"<xmlattr>") {
47 }
else if (key ==
"") {
51 bool isSection = (node.second.size() != 0);
58 for (boost::property_tree::ptree::value_type
const &itemCandidate : node.second) {
59 if (itemCandidate.first !=
"") {
67 if (!config->isMultiSectionsEnabled()) {
68 throw std::runtime_error(
"config::tree:readNode reading array but Config MultiSection disabled");
71 for (boost::property_tree::ptree::value_type
const &item : node.second) {
72 Config::Section *section = &(config->addSection(key));
73 importTree(item.second, section);
76 Config::Section *section;
77 if (!config->isMultiSectionsEnabled() && config->hasSection(key)) {
78 section = &(config->getSection(key));
80 section = &(config->addSection(key));
83 importTree(node.second, section);
86 std::string value = readNodeValue(node.second);
87 config->set(key, value);
99void exportTree(
const Config &config,
bool areArraysAllowed, boost::property_tree::ptree *
root)
102 for (
const auto &entry : config.getOptions()) {
103 const std::string &key = entry.first;
104 const std::string &value = entry.second;
105 root->add(key, value);
109 std::unordered_set<std::string> processedKeys;
110 for (
const auto &entry : config.getSections()) {
111 const std::string &key = entry.first;
112 if (areArraysAllowed && processedKeys.count(key) != 0) {
117 root->push_back(boost::property_tree::ptree::value_type(key, boost::property_tree::ptree()));
118 auto §ionChild =
root->back();
119 auto §ionRoot = sectionChild.second;
122 bool isArray = areArraysAllowed && (config.getSectionCount(key) > 1);
128 for (
const auto &itemEntry : config.getSections(key)) {
130 sectionRoot.push_back(boost::property_tree::ptree::value_type(
"", boost::property_tree::ptree()));
131 auto &itemChild = sectionRoot.back();
134 const Config::Section &itemSection = *itemEntry;
135 exportTree(itemSection, areArraysAllowed, &(itemChild.second));
139 const Config::Section §ion = *(entry.second);
140 exportTree(section, areArraysAllowed, §ionRoot);
144 if (areArraysAllowed) {
145 processedKeys.insert(key);
157void writeTree(
const std::string &filename, SourceFormat format, boost::property_tree::ptree &propertyTree)
159 if (format == SOURCE_FORMAT_XML) {
160 boost::property_tree::xml_writer_settings<std::string> settings(
' ', 4);
161 write_xml(filename, propertyTree, std::locale(), settings);
162 }
else if (format == SOURCE_FORMAT_JSON) {
163 write_json(filename, propertyTree, std::locale(),
true);
173std::string readNodeValue(
const boost::property_tree::ptree &node)
175 std::string value = node.get_value(
"");
177 std::string lowercaseValue = value;
178 std::transform(lowercaseValue.begin(), lowercaseValue.end(), lowercaseValue.begin(), ::tolower);
179 if (value ==
"true") {
180 return std::to_string(1);
181 }
else if (value ==
"false") {
182 return std::to_string(0);
GlobalConfigParser & root