Commit 51709fb1 authored by jan.koester's avatar jan.koester
Browse files

test

parent dfde41fa
Loading
Loading
Loading
Loading
+45 −24
Original line number Diff line number Diff line
@@ -36,10 +36,9 @@
#include "conf.h"
#include "config.h"

confplus::Config::ConfigValue::ConfigValue(){
}

confplus::Config::ConfigValue::~ConfigValue(){
confplus::Config::ConfigValue::ConfigValue(std::string value,size_t pos){
    _Value=value;
    _Pos=pos;
}

confplus::Config::ConfigData *confplus::Config::getKey(const std::string & key) const{
@@ -129,9 +128,11 @@ confplus::Config::ConfigData *confplus::Config::setKey(const std::string &key){
            if(!find){
                find = createdat(*root_ptr);
                find->Key=childkey;
                if(key[pos]!='\0')
                if(key.empty()){
                    find->Value=std::make_unique<ConfigValue>(key,pos);
                    find->haveChild=true;
                }
            }
            root_ptr = &find->Child;
        }
        ++pos;
@@ -151,13 +152,23 @@ void confplus::Config::delKey(confplus::Config::ConfigData* key){
}


confplus::Config::ConfigData::ConfigData(){;
    nextData=nullptr;
    haveChild=false;
    Elements=0;
confplus::Config::ConfigData::ConfigData() : Key(),
    Elements(0),
    haveChild(false),
    nextData(nullptr){
        new (&Value) std::unique_ptr<ConfigValue>();
}

confplus::Config::ConfigData::~ConfigData(){
    if (!haveChild) {
        Value.~unique_ptr<confplus::Config::ConfigValue>();;
    } else {
        Child.~unique_ptr<confplus::Config::ConfigData>();
    }

    Key.~basic_string<char, std::char_traits<char>, std::allocator<char> >();

    nextData.~unique_ptr<confplus::Config::ConfigData>();
}

size_t confplus::Config::ConfigData::getElements() const{
@@ -332,7 +343,7 @@ const std::string confplus::Config::getValue(confplus::Config::ConfigData* key,
        throw err;
    }

    for(ConfigValue *cur=&key->Value; cur; cur=cur->_nextValue.get()){
    for(ConfigValue *cur=key->Value.get(); cur; cur=cur->_nextValue.get()){
        if(cur->_Pos==pos){
            return cur->_Value;
        }
@@ -352,27 +363,37 @@ int confplus::Config::getIntValue(confplus::Config::ConfigData* key, size_t pos)
    throw err;
}

// In conf.cpp, replace the entire implementation of confplus::Config::setValue():

void confplus::Config::setValue(confplus::Config::ConfigData* key, size_t pos, const std::string &value){
    for(ConfigValue *cur=&key->Value; cur; cur=cur->_nextValue.get()){
    // 1. Check if key is a path (correct)
    if(key->haveChild){
        ConfException err;
        err[ConfException::Error] << "setValue it is a path not key";
        throw err;
    }

    for(ConfigValue *cur = key->Value.get(); cur; cur = cur->_nextValue.get()){
        if(cur->_Pos == pos){
            cur->_Value = value;
            return;
            return; // Found and updated existing value
        }
    }

    std::unique_ptr<ConfigValue> *current_ptr = &key->Value._nextValue;
    if (!key->Value) {
        // Set the head pointer directly
        key->Value = std::make_unique<ConfigValue>(value, pos);
    } else {
        // If the head is not null, traverse the linked list to find the last element
        std::unique_ptr<ConfigValue> *current_ptr = &key->Value->_nextValue;

        while (current_ptr->get()) {
            current_ptr = &current_ptr->get()->_nextValue;
        }

    *current_ptr = std::unique_ptr<ConfigValue>(new ConfigValue);

    ConfigValue *new_node = current_ptr->get();
    new_node->_Value = value;
    new_node->_Pos = pos;

    ++key->Elements;
        // Set the final unique_ptr<ConfigValue>* in the chain
        *current_ptr = std::make_unique<ConfigValue>(value, pos);
    }
}

void confplus::Config::setIntValue(confplus::Config::ConfigData* key, size_t pos, int value){
+6 −6
Original line number Diff line number Diff line
@@ -49,9 +49,9 @@ namespace confplus {

        class ConfigValue{
        public:
            ~ConfigValue();
            ConfigValue(std::string value,size_t pos);
            virtual ~ConfigValue() = default;
        private:
            ConfigValue();
            std::string   _Value;
            size_t          _Pos;
            std::unique_ptr<ConfigValue> _nextValue = nullptr;
@@ -68,11 +68,11 @@ namespace confplus {
            std::string  Key;
            size_t       Elements;

            bool         haveChild;
            bool         haveChild=false;

            union {
                ConfigValue Value;
                std::unique_ptr<ConfigData> Child = nullptr;
                std::unique_ptr<ConfigValue>Value;
                std::unique_ptr<ConfigData> Child;
            };

            std::unique_ptr<ConfigData> nextData=nullptr ;