/** * Allows to collect all keys in the given path. Does not include child section names. * @param d the default value which should be returned if the given path does not exist. * @param path Specifies the level inside the configuration, at which the names of the child sections should be * collected. * @return The names of the sections at the given path */ std::shared_ptr<std::vector<std::string>> Configuration::tryGetNames(std::string d, const char* path, ...) { va_list ap; va_start(ap, path); std::shared_ptr<std::vector<std::string>> params = getParams('.', path, ap); va_end(ap); std::vector<ConfigNode*> nodes; collectSections(this->configRoot.get(), params.get(), 0, &nodes); std::shared_ptr<std::vector<std::string>> result(new std::vector<std::string>()); if (nodes.size() == 0) { result->push_back(d); return result; } for (size_t i = 0; i < nodes.size(); i++) { if (nodes[i]->getType() == ConfigNode::Leaf) { result->push_back(nodes[i]->getName()); } } return result; }
/** * Allows to collect the names of all sections in the given path. It does not include the keys of key-value pair in the * sections. * @param path * @return A vector with the names of all sections in the given path. */ std::shared_ptr<std::vector<std::string>> Configuration::getSections(const char* path, ...) { va_list ap; va_start(ap, path); std::shared_ptr<std::vector<std::string>> params = getParams('.', path, ap); va_end(ap); std::vector<ConfigNode*> nodes; collectSections(this->configRoot.get(), params.get(), 0, &nodes); std::shared_ptr<std::vector<std::string>> result(new std::vector<std::string>()); if (nodes.size() == 0) { std::cerr << pathNotFound(params.get()) << std::endl; throw std::exception(); } for (unsigned int i = 0; i < nodes.size(); i++) { if (nodes[i]->getType() == ConfigNode::Node) { result->push_back(nodes[i]->getName()); } } return result; }
void Configuration::collectSections(ConfigNode *node, std::vector<std::string> *params, size_t offset, std::vector<ConfigNode *> *result) { std::vector<ConfigNodePtr> *children = node->getChildren(); // for(unsigned int i = 0; i < children->size(); i++){ // std::cout << "Children " << i << " " << (*children)[i]->getName().c_str() << std::endl; // } // std::cout << "offset " << offset << std::endl; // std::cout << "params->size " << params->size() << std::endl; if (offset == params->size()) { // std::cout << "pushed " << node->getName().c_str() << std::endl; //result->push_back(node); for(unsigned int i = 0; i < children->size(); i++){ result->push_back((*children)[i].get()); } return; } for (size_t i = offset; i < params->size(); i++) { bool found = false; for (size_t j = 0; j < children->size(); j++) { if ((*children)[j]->getName().compare((*params)[i]) == 0) { // std::cout << "found true with " << (*children)[j]->getName().c_str() << std::endl; collectSections((*children)[j].get(), params, offset + 1, result); found = true; } } if (!found) return; } }
void Configuration::collectSections(ConfigNode* node, std::vector<std::string>* params, size_t offset, std::vector<ConfigNode*>* result) { std::vector<ConfigNodePtr>* children = node->getChildren(); if (offset == params->size()) { for (unsigned int i = 0; i < children->size(); i++) { result->push_back((*children)[i].get()); } return; } for (size_t i = offset; i < params->size(); i++) { bool found = false; for (size_t j = 0; j < children->size(); j++) { if ((*children)[j]->getName().compare((*params)[i]) == 0) { collectSections((*children)[j].get(), params, offset + 1, result); found = true; } } if (!found) return; } }
std::vector<std::string> Configuration::getSections(const char *path, ...) { CONSUME_PARAMS(path); // Get relevant nodes std::vector<ConfigNode *> nodes; collectSections(this->configRoot.get(), params.get(), 0, &nodes); // If there are no nodes, exit if (nodes.size() == 0) { throw ConfigException(pathNotFound(params.get())); } // Copy only the sections std::vector<std::string> result; for (size_t i = 0; i < nodes.size(); i++) { if (nodes[i]->getType() == ConfigNode::Node) { result.push_back(nodes[i]->getName()); } } return result; }