<commit_before>#include "ServerConfig.h"

//@ XML reading
#include <fstream>
#include <string>
#include "Poco/DOM/DOMParser.h"
#include "Poco/DOM/NodeIterator.h"
#include "Poco/DOM/NodeFilter.h"
#include "Poco/DOM/NodeList.h"
#include "Poco/DOM/NamedNodeMap.h"
#include "Poco/SAX/InputSource.h"

Config::Config()
{
    std::ifstream in("Config.xml");
    Poco::XML::InputSource src(in);
    Poco::XML::DOMParser parser;
    _doc = parser.parse(&src);
}

Config::StringConfigsMap Config::getDatabaseInformation()
{
    return _connectionStrings;
}

void Config::readConfiguration()
{
    Poco::XML::Node* baseNode = firstChild(firstChild(_doc));

    while(baseNode)
    {
        if (baseNode->nodeName().compare("database") == 0)
        {
            Poco::XML::Node* databaseNode = firstChild(baseNode);

            while (databaseNode)
            {
                std::string databaseName = databaseNode->nodeName();

                Poco::XML::Node* databaseKey = firstChild(databaseNode);

                while (databaseKey)
                {
                    if (!_connectionStrings[databaseName].empty())
                        _connectionStrings[databaseName].append(";");

                    _connectionStrings[databaseName].append(databaseKey->nodeName() + "=" + databaseKey->innerText());
                    databaseKey = nextNode(databaseKey);
                }

                databaseNode = nextNode(databaseNode);
            }
        }
        else if (baseNode->nodeName().compare("coreconfig") == 0)
        {
            Poco::XML::Node* configNode = firstChild(baseNode);

            while (configNode)
            {
                Poco::XML::NamedNodeMap* atribs = configNode->attributes();
                
                if(atribs->getNamedItem("type")->nodeValue().compare("int") == 0)
                    _intConfigs[configNode->nodeName()] = std::atoi(configNode->innerText().c_str());
                else if(atribs->getNamedItem("type")->nodeValue().compare("bool") == 0)
                    _boolConfigs[configNode->nodeName()] = std::atoi(configNode->innerText().c_str()) != 0;

                configNode = nextNode(configNode);
            }
        }

        baseNode = nextNode(baseNode);
    }
}

int Config::getIntConfig(std::string configName)
{
    return _intConfigs[configName];
}

bool Config::getBoolConfig(std::string configName)
{
    return _boolConfigs[configName];
}

int Config::getDefaultInt(std::string configName, int _default)
{
    if (_intConfigs.find(configName) != _intConfigs.end())
        return _intConfigs[configName];
    return _default;
}
    
bool Config::getDefaultBool(std::string configName, int _default)
{
    if (_boolConfigs.find(configName) != _boolConfigs.end())
        return _boolConfigs[configName];
    return _default;
}


Poco::XML::Node* Config::firstChild(Poco::XML::Node* node)
{
    if (!node)
        return NULL;

    Poco::XML::Node* key = node->firstChild();
    while (key && key->nodeType() != Poco::XML::Node::ELEMENT_NODE)<commit_msg>Core/Config: Make the other functions use the new one<commit_after>#include "ServerConfig.h"

//@ XML reading
#include <fstream>
#include <string>
#include "Poco/DOM/DOMParser.h"
#include "Poco/DOM/NodeIterator.h"
#include "Poco/DOM/NodeFilter.h"
#include "Poco/DOM/NodeList.h"
#include "Poco/DOM/NamedNodeMap.h"
#include "Poco/SAX/InputSource.h"

Config::Config()
{
    std::ifstream in("Config.xml");
    Poco::XML::InputSource src(in);
    Poco::XML::DOMParser parser;
    _doc = parser.parse(&src);
}

Config::StringConfigsMap Config::getDatabaseInformation()
{
    return _connectionStrings;
}

void Config::readConfiguration()
{
    Poco::XML::Node* baseNode = firstChild(firstChild(_doc));

    while(baseNode)
    {
        if (baseNode->nodeName().compare("database") == 0)
        {
            Poco::XML::Node* databaseNode = firstChild(baseNode);

            while (databaseNode)
            {
                std::string databaseName = databaseNode->nodeName();

                Poco::XML::Node* databaseKey = firstChild(databaseNode);

                while (databaseKey)
                {
                    if (!_connectionStrings[databaseName].empty())
                        _connectionStrings[databaseName].append(";");

                    _connectionStrings[databaseName].append(databaseKey->nodeName() + "=" + databaseKey->innerText());
                    databaseKey = nextNode(databaseKey);
                }

                databaseNode = nextNode(databaseNode);
            }
        }
        else if (baseNode->nodeName().compare("coreconfig") == 0)
        {
            Poco::XML::Node* configNode = firstChild(baseNode);

            while (configNode)
            {
                Poco::XML::NamedNodeMap* atribs = configNode->attributes();
                
                if(atribs->getNamedItem("type")->nodeValue().compare("int") == 0)
                    _intConfigs[configNode->nodeName()] = std::atoi(configNode->innerText().c_str());
                else if(atribs->getNamedItem("type")->nodeValue().compare("bool") == 0)
                    _boolConfigs[configNode->nodeName()] = std::atoi(configNode->innerText().c_str()) != 0;

                configNode = nextNode(configNode);
            }
        }

        baseNode = nextNode(baseNode);
    }
}

int Config::getIntConfig(std::string configName)
{
    return getDefaultInt(configName, 0);
}

bool Config::getBoolConfig(std::string configName)
{
    return getDefaultBool(configName, false);
}

int Config::getDefaultInt(std::string configName, int _default)
{
    if (_intConfigs.find(configName) != _intConfigs.end())
        return _intConfigs[configName];
    return _default;
}
    
bool Config::getDefaultBool(std::string configName, int _default)
{
    if (_boolConfigs.find(configName) != _boolConfigs.end())
        return _boolConfigs[configName];
    return _default;
}


Poco::XML::Node* Config::firstChild(Poco::XML::Node* node)
{
    if (!node)
        return NULL;

    Poco::XML::Node* key = node->firstChild();
    while (key && key->nodeType() != Poco::XML::Node::ELEMENT_NODE)
{'fraction_non_alphanumeric': 0.14588082057961577, 'fraction_numerical': 0.0017909475740801042, 'mean_word_length': 2.73661800486618, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7560159', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1802, 'n_words': 285}