Loading backends/yaml/yaml.cpp +17 −43 Original line number Diff line number Diff line Loading @@ -25,6 +25,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <string> #include "conf.h" #include "yaml.h" Loading @@ -35,39 +37,6 @@ confplus::Yaml::Yaml(){ confplus::Yaml::~Yaml(){ } // confplus::ConfigData * confplus::Yaml::_scanBlock(yaml_token_t* curblock,yaml_token_t *next,ConfigData *parent,ConfigData *cur){ // /* BEGIN new code */ // if (curblock->type != YAML_STREAM_END_TOKEN){ // switch(curblock->type) { // case(YAML_BLOCK_MAPPING_START_TOKEN):{ // if(!parent){ // cur = new ConfigData; // cur->Key.insert(0,(const char*)curblock->data.scalar.value,curblock->data.scalar.length); // }else{ // cur->childData = new ConfigData; // parent=cur->childData; // } // }break; // case(YAML_BLOCK_END_TOKEN):{ // return parent; // }break; // case YAML_VALUE_TOKEN:{ // if(conf.firstData){ // lastData->nextData= new ConfigData; // lastData=lastData->nextData; // }else{ // firstData= new ConfigData; // lastData=firstData; // } // }break; // default: // printf("Got token of type %d\n", curblock->type); // } // } // return parent; // } const char* confplus::Yaml::getName(){ return "yaml"; } Loading @@ -87,7 +56,11 @@ void confplus::Yaml::saveConfig(const char *path,const Config *conf){ void confplus::Yaml::loadConfig(const char *path,Config *conf){ FILE *fh = fopen(path,"r"); yaml_token_t token,token2; yaml_token_t token; yaml_event_t event; yaml_event_type_t event_type; int curlevel=0; /* Initialize parser */ if(!yaml_parser_initialize(&_Parser)) Loading @@ -98,20 +71,21 @@ void confplus::Yaml::loadConfig(const char *path,Config *conf){ /* Set input file */ yaml_parser_set_input_file(&_Parser, fh); yaml_parser_scan(&_Parser, &token); yaml_parser_scan(&_Parser, &token2); if(conf->firstData){ delete conf->firstData; conf->firstData=nullptr; } do { if (!yaml_parser_parse(&_Parser, &event)) break; switch (event.type) { case YAML_SCALAR_EVENT:{ }break; // conf->lastData=_scanBlock(&token,&token2,nullptr,conf->firstData); } event_type=event.type; yaml_event_delete(&event); } while (event_type!= YAML_STREAM_END_EVENT); yaml_token_delete(&token); yaml_token_delete(&token2); yaml_parser_delete(&_Parser); fclose(fh); } src/conf.cpp +154 −25 Original line number Diff line number Diff line Loading @@ -38,53 +38,114 @@ #include "conf.h" #include "config.h" confplus::ConfigValue::ConfigValue(){ _firstValue=nullptr; confplus::Config::ConfigValue::ConfigValue(){ _nextValue=nullptr; _Index=0; } confplus::ConfigValue::~ConfigValue(){ confplus::Config::ConfigValue::~ConfigValue(){ delete _nextValue; } const char * confplus::ConfigValue::operator[](size_t idx){ for(ConfigValue *cval=_firstValue; cval; cval=cval->_nextValue){ if(cval->_Index==idx) return _Value.c_str(); confplus::Config::ConfigData *confplus::Config::getKey(const char* key){ ConfigData *child=firstData; size_t start=0; for(size_t pos=0; pos<strlen(key); ++pos){ if(key[pos]=='/'){ std::string childkey; std::copy(key+start,key+(pos-1),std::inserter<std::string>(childkey,childkey.begin())); if(child->haveChild){ for(ConfigData *cdat=child; cdat; cdat=cdat->nextData){ if(cdat->Key==childkey){ child=cdat->Child; } } start=++pos; } } return nullptr; } confplus::ConfigValue & confplus::Config::Value(const char* key){ for(ConfigData *cdat=firstData; cdat; cdat=cdat->nextData){ if(cdat->Key==key){ return cdat->Value; std::string vkey; std::copy(key+start,key+strlen(key),std::inserter<std::string>(vkey,vkey.begin())); for(ConfigData *cdat=child; cdat; cdat=cdat->nextData){ if(!child->haveChild && cdat->Key==vkey){ return cdat->Child; } } ConfException exp; exp[ConfException::Critical] << "Config: key not found"; throw exp; } confplus::Config::ConfigData & confplus::Config::Child(const char* key){ for(ConfigData *cdat=firstData; cdat; cdat=cdat->nextData){ if(cdat->Key==key){ return *cdat->childData; } } confplus::Config::ConfigData *confplus::Config::setKey(const char* key){ ConfigData *child=firstData,*ekey=nullptr; size_t start=0; for(size_t pos=0; pos<strlen(key); ++pos){ if(key[pos]=='/'){ if(ekey){ ConfException exp; exp[ConfException::Critical] << "Config: key not found"; exp[ConfException::Critical] << "Config: set key it's value not index type"; throw exp; } std::string childkey; std::copy(key+start,key+(pos-1),std::inserter<std::string>(childkey,childkey.begin())); ConfigData *cdat=child->nextData,*before=child; while(cdat){ if(cdat->Key==childkey){ if(cdat->haveChild){ child=cdat->Child; break; } else{ ekey=cdat->Child; } } before=cdat; cdat=cdat->nextData; }; if(!cdat){ before->nextData=new ConfigData; before->haveChild=true; cdat=before->nextData; } start=++pos; } } std::string reskey; std::copy(key+start,key+strlen(key),std::inserter<std::string>(reskey,reskey.begin())); if(!ekey){ child->nextData=new ConfigData; ConfigData *res=child->nextData; res->haveChild=false; res->Value._nextValue=nullptr; return res; } return ekey; } void confplus::Config::delKey(confplus::Config::ConfigData* key){ ConfigData *bef=nullptr; for(ConfigData *cur=firstData; cur; cur=cur->nextData){ if(cur==key){ bef->nextData=key->nextData; key->nextData=nullptr; delete key; } bef=cur; } } confplus::Config::ConfigData::ConfigData(){ childData=nullptr; nextData=nullptr; } confplus::Config::ConfigData::~ConfigData(){ delete childData; if(haveChild) delete Child; delete nextData; } Loading @@ -94,7 +155,6 @@ size_t confplus::Config::ConfigData::getElements(){ confplus::Config::Config(const char* path){ firstData=nullptr; lastData=nullptr; int i =0; Loading Loading @@ -125,7 +185,7 @@ confplus::Config::Config(const char* path){ const char* dlsym_error = dlerror(); if (dlsym_error) { ConfException err; err << "Cannot load symbol create: " << dlsym_error << '\n'; err[ConfException::Critical] << "Cannot load symbol create: " << dlsym_error << '\n'; throw err; } Loading Loading @@ -165,3 +225,72 @@ const char* confplus::Config::getAuthor(){ void confplus::Config::saveConfig(const char *path){ _currApi->saveConfig(path,this); } size_t confplus::Config::getElements(confplus::Config::ConfigData* key){ return key->Elements; } const char * confplus::Config::getValue(confplus::Config::ConfigData* key, size_t pos){ if(key->haveChild){ ConfException err; err[ConfException::Error] << "getValue it is a path not key" << pos << '\n'; throw err; } if(key->Elements<pos){ ConfException err; err[ConfException::Error] << "getValue pos out of range !" << pos << '\n'; throw err; } size_t lvl=0; for(ConfigValue *cur=&key->Value; cur; cur=cur->_nextValue){ if(lvl==pos){ return cur->_Value.c_str(); } ++lvl; } return nullptr; } int confplus::Config::getIntValue(confplus::Config::ConfigData* key, size_t pos){ return atoi(getValue(key,pos)); } void confplus::Config::setValue(confplus::Config::ConfigData* key, size_t pos, const char* value){ if(key->haveChild){ ConfException err; err[ConfException::Error] << "getValue it is a path not key" << pos << '\n'; throw err; } for(ConfigValue *cur=&key->Value; cur; cur=cur->_nextValue){ if(cur->_Pos==pos){ cur->_Value=value; return; } } ConfigValue *cur=&key->Value,*bef=nullptr; while(cur){ bef=cur; cur=cur->_nextValue; }; bef->_nextValue = new ConfigValue(); bef->_Value = value; bef->_Pos = pos; ++key->Elements; return; } void confplus::Config::setIntValue(confplus::Config::ConfigData* key, size_t pos, int value){ char inv[512]; snprintf(inv,512,"%d",value); setValue(key,pos,inv); } src/conf.h +31 −18 Original line number Diff line number Diff line Loading @@ -33,43 +33,56 @@ #pragma once namespace confplus { class ConfigValue{ class Config { public: const char *operator[](size_t idx); Config(const char *path); ~Config(); class ConfigValue{ private: ConfigValue(); ~ConfigValue(); std::string _Value; size_t _Index; size_t _Pos; ConfigValue *_nextValue; ConfigValue *_firstValue; friend class Config; }; class Config { public: Config(const char *path); ~Config(); class ConfigData{ public: private: size_t getElements(); ConfigData(); ~ConfigData(); std::string Key; size_t Elements; bool haveChild; union { ConfigValue Value; ConfigData *childData; ConfigData *Child; }; ConfigData *nextData; friend class Config; friend class Yaml; }; ConfigValue &Value(const char* key); ConfigData &Child(const char* key); ConfigData *getKey(const char* key); ConfigData *setKey(const char *key); void delKey(ConfigData *key); size_t getElements(ConfigData *key); void setValue(ConfigData *key,size_t pos,const char *value); void setIntValue(ConfigData *key,size_t pos,int value); const char* getValue(ConfigData *key,size_t pos); int getIntValue(ConfigData *key,size_t pos); ConfigData *firstData; ConfigData *lastData; public: const char* getName(); const char* getVersion(); Loading Loading
backends/yaml/yaml.cpp +17 −43 Original line number Diff line number Diff line Loading @@ -25,6 +25,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <string> #include "conf.h" #include "yaml.h" Loading @@ -35,39 +37,6 @@ confplus::Yaml::Yaml(){ confplus::Yaml::~Yaml(){ } // confplus::ConfigData * confplus::Yaml::_scanBlock(yaml_token_t* curblock,yaml_token_t *next,ConfigData *parent,ConfigData *cur){ // /* BEGIN new code */ // if (curblock->type != YAML_STREAM_END_TOKEN){ // switch(curblock->type) { // case(YAML_BLOCK_MAPPING_START_TOKEN):{ // if(!parent){ // cur = new ConfigData; // cur->Key.insert(0,(const char*)curblock->data.scalar.value,curblock->data.scalar.length); // }else{ // cur->childData = new ConfigData; // parent=cur->childData; // } // }break; // case(YAML_BLOCK_END_TOKEN):{ // return parent; // }break; // case YAML_VALUE_TOKEN:{ // if(conf.firstData){ // lastData->nextData= new ConfigData; // lastData=lastData->nextData; // }else{ // firstData= new ConfigData; // lastData=firstData; // } // }break; // default: // printf("Got token of type %d\n", curblock->type); // } // } // return parent; // } const char* confplus::Yaml::getName(){ return "yaml"; } Loading @@ -87,7 +56,11 @@ void confplus::Yaml::saveConfig(const char *path,const Config *conf){ void confplus::Yaml::loadConfig(const char *path,Config *conf){ FILE *fh = fopen(path,"r"); yaml_token_t token,token2; yaml_token_t token; yaml_event_t event; yaml_event_type_t event_type; int curlevel=0; /* Initialize parser */ if(!yaml_parser_initialize(&_Parser)) Loading @@ -98,20 +71,21 @@ void confplus::Yaml::loadConfig(const char *path,Config *conf){ /* Set input file */ yaml_parser_set_input_file(&_Parser, fh); yaml_parser_scan(&_Parser, &token); yaml_parser_scan(&_Parser, &token2); if(conf->firstData){ delete conf->firstData; conf->firstData=nullptr; } do { if (!yaml_parser_parse(&_Parser, &event)) break; switch (event.type) { case YAML_SCALAR_EVENT:{ }break; // conf->lastData=_scanBlock(&token,&token2,nullptr,conf->firstData); } event_type=event.type; yaml_event_delete(&event); } while (event_type!= YAML_STREAM_END_EVENT); yaml_token_delete(&token); yaml_token_delete(&token2); yaml_parser_delete(&_Parser); fclose(fh); }
src/conf.cpp +154 −25 Original line number Diff line number Diff line Loading @@ -38,53 +38,114 @@ #include "conf.h" #include "config.h" confplus::ConfigValue::ConfigValue(){ _firstValue=nullptr; confplus::Config::ConfigValue::ConfigValue(){ _nextValue=nullptr; _Index=0; } confplus::ConfigValue::~ConfigValue(){ confplus::Config::ConfigValue::~ConfigValue(){ delete _nextValue; } const char * confplus::ConfigValue::operator[](size_t idx){ for(ConfigValue *cval=_firstValue; cval; cval=cval->_nextValue){ if(cval->_Index==idx) return _Value.c_str(); confplus::Config::ConfigData *confplus::Config::getKey(const char* key){ ConfigData *child=firstData; size_t start=0; for(size_t pos=0; pos<strlen(key); ++pos){ if(key[pos]=='/'){ std::string childkey; std::copy(key+start,key+(pos-1),std::inserter<std::string>(childkey,childkey.begin())); if(child->haveChild){ for(ConfigData *cdat=child; cdat; cdat=cdat->nextData){ if(cdat->Key==childkey){ child=cdat->Child; } } start=++pos; } } return nullptr; } confplus::ConfigValue & confplus::Config::Value(const char* key){ for(ConfigData *cdat=firstData; cdat; cdat=cdat->nextData){ if(cdat->Key==key){ return cdat->Value; std::string vkey; std::copy(key+start,key+strlen(key),std::inserter<std::string>(vkey,vkey.begin())); for(ConfigData *cdat=child; cdat; cdat=cdat->nextData){ if(!child->haveChild && cdat->Key==vkey){ return cdat->Child; } } ConfException exp; exp[ConfException::Critical] << "Config: key not found"; throw exp; } confplus::Config::ConfigData & confplus::Config::Child(const char* key){ for(ConfigData *cdat=firstData; cdat; cdat=cdat->nextData){ if(cdat->Key==key){ return *cdat->childData; } } confplus::Config::ConfigData *confplus::Config::setKey(const char* key){ ConfigData *child=firstData,*ekey=nullptr; size_t start=0; for(size_t pos=0; pos<strlen(key); ++pos){ if(key[pos]=='/'){ if(ekey){ ConfException exp; exp[ConfException::Critical] << "Config: key not found"; exp[ConfException::Critical] << "Config: set key it's value not index type"; throw exp; } std::string childkey; std::copy(key+start,key+(pos-1),std::inserter<std::string>(childkey,childkey.begin())); ConfigData *cdat=child->nextData,*before=child; while(cdat){ if(cdat->Key==childkey){ if(cdat->haveChild){ child=cdat->Child; break; } else{ ekey=cdat->Child; } } before=cdat; cdat=cdat->nextData; }; if(!cdat){ before->nextData=new ConfigData; before->haveChild=true; cdat=before->nextData; } start=++pos; } } std::string reskey; std::copy(key+start,key+strlen(key),std::inserter<std::string>(reskey,reskey.begin())); if(!ekey){ child->nextData=new ConfigData; ConfigData *res=child->nextData; res->haveChild=false; res->Value._nextValue=nullptr; return res; } return ekey; } void confplus::Config::delKey(confplus::Config::ConfigData* key){ ConfigData *bef=nullptr; for(ConfigData *cur=firstData; cur; cur=cur->nextData){ if(cur==key){ bef->nextData=key->nextData; key->nextData=nullptr; delete key; } bef=cur; } } confplus::Config::ConfigData::ConfigData(){ childData=nullptr; nextData=nullptr; } confplus::Config::ConfigData::~ConfigData(){ delete childData; if(haveChild) delete Child; delete nextData; } Loading @@ -94,7 +155,6 @@ size_t confplus::Config::ConfigData::getElements(){ confplus::Config::Config(const char* path){ firstData=nullptr; lastData=nullptr; int i =0; Loading Loading @@ -125,7 +185,7 @@ confplus::Config::Config(const char* path){ const char* dlsym_error = dlerror(); if (dlsym_error) { ConfException err; err << "Cannot load symbol create: " << dlsym_error << '\n'; err[ConfException::Critical] << "Cannot load symbol create: " << dlsym_error << '\n'; throw err; } Loading Loading @@ -165,3 +225,72 @@ const char* confplus::Config::getAuthor(){ void confplus::Config::saveConfig(const char *path){ _currApi->saveConfig(path,this); } size_t confplus::Config::getElements(confplus::Config::ConfigData* key){ return key->Elements; } const char * confplus::Config::getValue(confplus::Config::ConfigData* key, size_t pos){ if(key->haveChild){ ConfException err; err[ConfException::Error] << "getValue it is a path not key" << pos << '\n'; throw err; } if(key->Elements<pos){ ConfException err; err[ConfException::Error] << "getValue pos out of range !" << pos << '\n'; throw err; } size_t lvl=0; for(ConfigValue *cur=&key->Value; cur; cur=cur->_nextValue){ if(lvl==pos){ return cur->_Value.c_str(); } ++lvl; } return nullptr; } int confplus::Config::getIntValue(confplus::Config::ConfigData* key, size_t pos){ return atoi(getValue(key,pos)); } void confplus::Config::setValue(confplus::Config::ConfigData* key, size_t pos, const char* value){ if(key->haveChild){ ConfException err; err[ConfException::Error] << "getValue it is a path not key" << pos << '\n'; throw err; } for(ConfigValue *cur=&key->Value; cur; cur=cur->_nextValue){ if(cur->_Pos==pos){ cur->_Value=value; return; } } ConfigValue *cur=&key->Value,*bef=nullptr; while(cur){ bef=cur; cur=cur->_nextValue; }; bef->_nextValue = new ConfigValue(); bef->_Value = value; bef->_Pos = pos; ++key->Elements; return; } void confplus::Config::setIntValue(confplus::Config::ConfigData* key, size_t pos, int value){ char inv[512]; snprintf(inv,512,"%d",value); setValue(key,pos,inv); }
src/conf.h +31 −18 Original line number Diff line number Diff line Loading @@ -33,43 +33,56 @@ #pragma once namespace confplus { class ConfigValue{ class Config { public: const char *operator[](size_t idx); Config(const char *path); ~Config(); class ConfigValue{ private: ConfigValue(); ~ConfigValue(); std::string _Value; size_t _Index; size_t _Pos; ConfigValue *_nextValue; ConfigValue *_firstValue; friend class Config; }; class Config { public: Config(const char *path); ~Config(); class ConfigData{ public: private: size_t getElements(); ConfigData(); ~ConfigData(); std::string Key; size_t Elements; bool haveChild; union { ConfigValue Value; ConfigData *childData; ConfigData *Child; }; ConfigData *nextData; friend class Config; friend class Yaml; }; ConfigValue &Value(const char* key); ConfigData &Child(const char* key); ConfigData *getKey(const char* key); ConfigData *setKey(const char *key); void delKey(ConfigData *key); size_t getElements(ConfigData *key); void setValue(ConfigData *key,size_t pos,const char *value); void setIntValue(ConfigData *key,size_t pos,int value); const char* getValue(ConfigData *key,size_t pos); int getIntValue(ConfigData *key,size_t pos); ConfigData *firstData; ConfigData *lastData; public: const char* getName(); const char* getVersion(); Loading