Commit dfde41fa authored by jan.koester's avatar jan.koester
Browse files

test

parent 11cf547b
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -135,8 +135,8 @@ void confplus::Yaml::loadConfig(const char *path,Config *conf){
                std::copy(pp.begin(),pp.end(),std::inserter<std::string>(cname,std::begin(cname)));
            }
            cname+=key;
            Config::ConfigData *ckey=conf->setKey(cname.c_str());
            conf->setValue(ckey,pos,value.c_str());
            Config::ConfigData *ckey=conf->setKey(cname);
            conf->setValue(ckey,pos,value);
            value.clear();
            if(!seq){
                key.clear();
+49 −54
Original line number Diff line number Diff line
@@ -37,15 +37,13 @@
#include "config.h"

confplus::Config::ConfigValue::ConfigValue(){
    _nextValue=nullptr;
}

confplus::Config::ConfigValue::~ConfigValue(){
    delete _nextValue;
}

confplus::Config::ConfigData *confplus::Config::getKey(const char* key) const{
    ConfigData *cdat=firstData;
confplus::Config::ConfigData *confplus::Config::getKey(const std::string & key) const{
    ConfigData *cdat=firstData.get();
    if(key[0]!='/'){
        ConfException exp;
        exp[ConfException::Error] << "Config: getkey wrong path!";
@@ -53,23 +51,23 @@ confplus::Config::ConfigData *confplus::Config::getKey(const char* key) const{
    }
    bool match=false;
    size_t start=1,pos=1;
    while(pos<=strlen(key)){
    while(pos<=key.length()){
GETKEYSEARCH:
        if(key[pos]=='/' || key[pos]=='\0'){
            std::string childkey;
            std::copy(key+start,key+pos,std::inserter<std::string>(childkey,childkey.begin()));
            std::copy(&key[start],&key[pos],std::inserter<std::string>(childkey,childkey.begin()));
            while(cdat){
                if(cdat->Key==childkey){
                    if(key[pos]=='\0'){
                          return cdat;
                    }
                    cdat=cdat->Child;
                    cdat=cdat->Child.get();
                    ++pos;
                    start=pos;
                    match=true;
                    goto GETKEYSEARCH;
                }
                cdat=cdat->nextData;
                cdat=cdat->nextData.get();
            }
            if(!match)
                break;
@@ -81,14 +79,20 @@ GETKEYSEARCH:
    throw exp;
}

confplus::Config::ConfigData *confplus::Config::setKey(const char* key){
confplus::Config::ConfigData *confplus::Config::setKey(const std::string &key){

    auto createdat = [] (ConfigData **root){
        while(*root){
            root=&(*root)->nextData;
    auto createdat = [] (std::unique_ptr<ConfigData> &root){
        std::unique_ptr<ConfigData> *current_ptr = &root;

        // Traverse the unique_ptr chain to find the last element
        while(current_ptr->get()){
            current_ptr = &current_ptr->get()->nextData;
        }
        *root=new ConfigData;
        return *root;

        // current_ptr now points to the unique_ptr at the end of the list (which is nullptr)
        *current_ptr = std::unique_ptr<ConfigData>(new ConfigData);

        return current_ptr->get();
    };

    auto existsdat = [] (ConfigData *search,const std::string ckey){
@@ -96,7 +100,7 @@ confplus::Config::ConfigData *confplus::Config::setKey(const char* key){
            if(search->Key==ckey){
                return search;
            }
            search=search->nextData;
            search=search->nextData.get();
        }
        return (ConfigData*)nullptr;
    };
@@ -109,24 +113,26 @@ confplus::Config::ConfigData *confplus::Config::setKey(const char* key){

    size_t start=1,pos=1;

    ConfigData *find=nullptr,**child=&firstData;
    ConfigData *find=nullptr;

    while(pos<=strlen(key)){
    std::unique_ptr<ConfigData> *root_ptr = &firstData;

    while(pos<=key.length()){
        if(key[pos]=='/' || key[pos]=='\0'){

            std::string childkey;
            std::copy(key+start,key+pos,std::inserter<std::string>(childkey,childkey.begin()));
            std::copy(&key[start],&key[pos],std::inserter<std::string>(childkey,childkey.begin()));
            start=(pos+1);

            find=existsdat(*child,childkey);
            find = existsdat(root_ptr->get(), childkey);

            if(!find){
                find = createdat(child);
                find = createdat(*root_ptr);
                find->Key=childkey;
                if(key[pos]!='\0')
                    find->haveChild=true;
            }
            child=&find->Child;
            root_ptr = &find->Child;
        }
        ++pos;
    }
@@ -136,30 +142,22 @@ confplus::Config::ConfigData *confplus::Config::setKey(const char* key){

void confplus::Config::delKey(confplus::Config::ConfigData* key){
    ConfigData *bef=nullptr;
    for(ConfigData *cur=firstData; cur; cur=cur->nextData){
    for(ConfigData *cur=firstData.get(); cur; cur=cur->nextData.get()){
        if(cur==key){
            bef->nextData=key->nextData;
            key->nextData=nullptr;
            delete key;
            bef->nextData=std::move(key->nextData);
        }
        bef=cur;
    }
}


confplus::Config::ConfigData::ConfigData(){
    Value=nullptr;
confplus::Config::ConfigData::ConfigData(){;
    nextData=nullptr;
    haveChild=false;
    Elements=0;
}

confplus::Config::ConfigData::~ConfigData(){
    if (haveChild && Child)
        delete Child;
    if (Value)
        delete Value;
    delete nextData;
}

size_t confplus::Config::ConfigData::getElements() const{
@@ -238,7 +236,7 @@ confplus::Config::~Config() {
        _BackendData = nullptr;
    }

    delete firstData;
    firstData.reset();
}
#else
confplus::Config::Config(const std::string &path){
@@ -298,7 +296,7 @@ confplus::Config::~Config(){
        _BackendData = nullptr;
    }

    delete firstData;
    firstData.reset();
}
#endif

@@ -322,7 +320,7 @@ size_t confplus::Config::getElements(confplus::Config::ConfigData* key) const{
    return key->Elements;
}

const char * confplus::Config::getValue(confplus::Config::ConfigData* key, size_t pos) const{
const std::string confplus::Config::getValue(confplus::Config::ConfigData* key, size_t pos) const{
    if(key->haveChild){
        ConfException err;
        err[ConfException::Error] << "getValue it is a path not key" << pos;
@@ -334,50 +332,47 @@ const char * confplus::Config::getValue(confplus::Config::ConfigData* key, size_
        throw err;
    }

    for(ConfigValue *cur=key->Value; cur; cur=cur->_nextValue){
    for(ConfigValue *cur=&key->Value; cur; cur=cur->_nextValue.get()){
        if(cur->_Pos==pos){
            return cur->_Value.c_str();
            return cur->_Value;
        }
    }

    return nullptr;
    return "";

}

int confplus::Config::getIntValue(confplus::Config::ConfigData* key, size_t pos) const{
    const char *val=getValue(key,pos);
    if(val)
        return atoi(val);
    const std::string val=getValue(key,pos);
    if(!val.empty())
        return atoi(val.c_str());

    ConfException err;
    err[ConfException::Error] << "getIntValue empty at pos: " << pos;
    throw err;
}

void confplus::Config::setValue(confplus::Config::ConfigData* key, size_t pos, const char* value){
    for(ConfigValue *cur=key->Value; cur; cur=cur->_nextValue){
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()){
        if(cur->_Pos==pos){
            cur->_Value=value;
            return;
        }
    }

    ConfigValue **cur=&key->Value;
    std::unique_ptr<ConfigValue> *current_ptr = &key->Value._nextValue;

    while(*cur){
        cur=&(*cur)->_nextValue;
    };

    if(!*cur){
        *cur = new ConfigValue();
    while (current_ptr->get()) {
        current_ptr = &current_ptr->get()->_nextValue;
    }

    (*cur)->_Value = value;
    (*cur)->_Pos = pos;
    *current_ptr = std::unique_ptr<ConfigValue>(new ConfigValue);

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

    return;
    ++key->Elements;
}

void confplus::Config::setIntValue(confplus::Config::ConfigData* key, size_t pos, int value){
+14 −11
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <memory>
#include <string>
#include <stdio.h>

@@ -47,20 +48,22 @@ namespace confplus {
        ~Config();

        class ConfigValue{
        public:
            ~ConfigValue();
        private:
            ConfigValue();
            ~ConfigValue();
            std::string   _Value;
            size_t        _Pos;
            ConfigValue  *_nextValue;
            std::unique_ptr<ConfigValue> _nextValue = nullptr;
            friend class Config;
        };

        class ConfigData{
        public:
            ~ConfigData();
        private:
            size_t getElements() const;
            ConfigData();
            ~ConfigData();

            std::string  Key;
            size_t       Elements;
@@ -68,28 +71,28 @@ namespace confplus {
            bool         haveChild;

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

            ConfigData  *nextData;
            std::unique_ptr<ConfigData> nextData=nullptr ;
            friend class Config;
        };


        ConfigData  *getKey(const char* key) const;
        ConfigData  *setKey(const char *key);
        ConfigData  *getKey(const std::string &key) const;
        ConfigData  *setKey(const std::string &key);
        void         delKey(ConfigData  *key);

        size_t getElements(ConfigData *key) const;

        void setValue(ConfigData *key,size_t pos,const char *value);
        void setValue(ConfigData *key,size_t pos,const std::string &value);
        void setIntValue(ConfigData *key,size_t pos,int value);

        const char* getValue(ConfigData *key,size_t pos) const;
        const std::string getValue(ConfigData *key,size_t pos) const;
        int  getIntValue(ConfigData *key,size_t pos) const;

        ConfigData *firstData;
        std::unique_ptr<ConfigData>firstData=nullptr;
    public:
        const char* getName();
        const char* getVersion();