add tools to data and db

master
IoTcat 5 years ago
parent de3daadf67
commit f266d53301
  1. 44
      src/ovo.cpp
  2. 37
      src/ovo.h

@ -1973,9 +1973,9 @@ void ovo::db::pushData(data& data, const string& key){
data.iter=data.begin();
for(;data.iter!=data.end();data.iter++){
if(_AES){
ous << m.aes_encode(data.iter->first)<<" "<<m.aes_encode(data.iter->second)<<endl;
ous << "_" << m.aes_encode(data.iter->first)<<" _"<<m.aes_encode(data.iter->second)<<endl;
}else{
ous << m.base64_encode(data.iter->first)<<" "<<m.base64_encode(data.iter->second)<<endl;
ous << "_" << m.base64_encode(data.iter->first)<<" _"<<m.base64_encode(data.iter->second)<<endl;
}
}
ous.close();
@ -2002,9 +2002,9 @@ void ovo::db::addData(data& data, const string& key){
data.iter=data.begin();
for(;data.iter!=data.end();data.iter++){
if(_AES){
ous << m.aes_encode(data.iter->first)<<" "<<m.aes_encode(data.iter->second)<<endl;
ous << "_" << m.aes_encode(data.iter->first)<<" _"<<m.aes_encode(data.iter->second)<<endl;
}else{
ous << m.base64_encode(data.iter->first)<<" "<<m.base64_encode(data.iter->second)<<endl;
ous << "_" << m.base64_encode(data.iter->first)<<" _"<<m.base64_encode(data.iter->second)<<endl;
}
}
ous.close();
@ -2038,17 +2038,53 @@ ovo::data ovo::db::getData(const string& key){
}
while(!ins.eof()){
ins >> t_first >> t_second;
t_first = t_first.substr(1);
t_second = t_second.substr(1);
if(_AES){
data.insert((m.aes_decode(t_first)), m.aes_decode(t_second));
}else{
data.insert((m.base64_decode(t_first)), m.base64_decode(t_second));
}
}
data.clear(m.base64_decode(t_first));
ins.close();
return data;
}
/**
* classify data in database
*
* @Author yimian
* @access public
* @param string key
* @param data delData
* @return void
*/
void ovo::db::classify(const string& key, std::vector<string> v){
ovo::data d;
d = this->getData(key);
if(d["_isExist"] == "NO") return;
d.classify();
if(!v.size()){
this->pushData(d, key);
return;
}
for(int i = 0; i < v.size(); i ++){
d.clear(v[i]);
}
this->pushData(d, key);
}
/**
* delete data from database

@ -285,18 +285,20 @@ namespace ovo{
/* insert data */
template <typename T, typename Y>
inline void insert(const T& key, const Y& val){
if(this->isExist(key)) this->_data[this->toStr(key)] = this->toStr(val);
else this->_data.insert(make_pair(this->toStr(key), this->toStr(val)));
this->isExist(key);
this->_data[this->toStr(key)] = this->toStr(val);
};
/* overload [] */
template <typename T>
string& operator[](const T& i){
isExist(i);
return this->_data[this->toStr(i)];
}
/* overload [] */
template <typename T>
const string& operator[](const T& i) const{
isExist(i);
return this->_data[this->toStr(i)];
};
/* find begin */
@ -319,8 +321,8 @@ namespace ovo{
/* clear elements */
template <typename T>
inline void clear(const T& key){
if(isExist(key)){
map<string, string>::iterator t_iter = this->find(key);
if(this->_data.count(this->toStr(key))){
map<string, string>::iterator t_iter = this->find(this->toStr(key));
this->_data.erase(t_iter);
}
}
@ -336,8 +338,24 @@ namespace ovo{
map<string, string>::iterator iter;
/* if key is exist */
template <typename T>
inline int isExist(T& key) const{
return this->_data.count(this->toStr(key));
inline bool isExist(T& key){
if(this->_data.count(this->toStr(key)) && this->_data[this->toStr(key)] != "undefined"){
return true;
}
this->_data[this->toStr(key)] = "undefined";
return false;
}
/* classify elements */
void classify(){
map<string, string>::iterator t_iter = this->_data.begin();
while(t_iter != this->_data.end()){
if(t_iter->second == "undefined"){
this->_data.erase(t_iter++);
}else{
t_iter++;
}
}
}
/* get size of data */
inline int size() const{
@ -389,6 +407,13 @@ namespace ovo{
void addData(data& data, const string& key);
/* get data from database */
data getData(const string& key);
/* classify data in db */
inline void classify(const string& key){
std::vector<string> v;
this->classify(key, v);
};
/* classify data in db */
void classify(const string& key, std::vector<string> v);
/* del data */
void del(const string& key);

Loading…
Cancel
Save