implement API to save lists in configs file and terminal history

This commit is contained in:
Eduardo Bart
2012-01-15 13:13:22 -02:00
parent a238111c07
commit 9ec40f016d
20 changed files with 228 additions and 58 deletions

View File

@@ -27,6 +27,11 @@
ConfigManager g_configs;
ConfigManager::ConfigManager()
{
m_confsDoc = OTMLDocument::create();
}
bool ConfigManager::load(const std::string& file)
{
m_fileName = file;
@@ -35,9 +40,9 @@ bool ConfigManager::load(const std::string& file)
return false;
try {
OTMLDocumentPtr doc = OTMLDocument::parse(file);
for(const OTMLNodePtr& child : doc->children())
m_confsMap[child->tag()] = child->value();
OTMLDocumentPtr confsDoc = OTMLDocument::parse(file);
if(confsDoc)
m_confsDoc = confsDoc;
return true;
} catch(Exception& e) {
logError("could not load configurations: ", e.what());
@@ -47,13 +52,65 @@ bool ConfigManager::load(const std::string& file)
bool ConfigManager::save()
{
OTMLDocumentPtr doc = OTMLDocument::create();
for(auto it : m_confsMap) {
if(it.second == "")
continue;
OTMLNodePtr node = OTMLNode::create(it.first, it.second);
doc->addChild(node);
}
return doc->save(m_fileName);
if(m_fileName.length() == 0)
return false;
return m_confsDoc->save(m_fileName);
}
void ConfigManager::set(const std::string& key, const std::string& value)
{
if(key == "") {
remove(key);
return;
}
OTMLNodePtr child = OTMLNode::create(key, value);
m_confsDoc->addChild(child);
}
void ConfigManager::setList(const std::string& key, const std::vector<std::string>& list)
{
remove(key);
if(list.size() == 0)
return;
OTMLNodePtr child = OTMLNode::create(key, true);
for(const std::string& value : list) {
child->writeIn(value);
dump << "insert" << value;
}
m_confsDoc->addChild(child);
}
bool ConfigManager::exists(const std::string& key)
{
return m_confsDoc->hasChildAt(key);
}
std::string ConfigManager::get(const std::string& key)
{
OTMLNodePtr child = m_confsDoc->get(key);
if(child)
return child->value();
else
return "";
}
std::vector<std::string> ConfigManager::getList(const std::string& key)
{
std::vector<std::string> list;
OTMLNodePtr child = m_confsDoc->get(key);
if(child) {
for(const OTMLNodePtr& subchild : child->children())
list.push_back(subchild->value());
}
return list;
}
void ConfigManager::remove(const std::string& key)
{
OTMLNodePtr child = m_confsDoc->get(key);
if(child)
m_confsDoc->removeChild(child);
}

View File

@@ -24,21 +24,25 @@
#define CONFIGMANAGER_H
#include "declarations.h"
#include <framework/otml/declarations.h>
class ConfigManager
{
public:
ConfigManager();
bool load(const std::string& file);
bool save();
bool exists(const std::string& key) { return m_confsMap.find(key) != m_confsMap.end(); }
void set(const std::string& key, const std::string& value) { m_confsMap[key] = value; }
std::string get(const std::string& key) { return m_confsMap[key]; }
void remove(const std::string& key) { m_confsMap[key] = ""; }
void set(const std::string& key, const std::string& value);
void setList(const std::string& key, const std::vector<std::string>& list);
std::string get(const std::string& key);
std::vector<std::string> getList(const std::string& key);
bool exists(const std::string& key);
void remove(const std::string& key);
private:
std::string m_fileName;
std::map<std::string, std::string> m_confsMap;
OTMLDocumentPtr m_confsDoc;
};
extern ConfigManager g_configs;

View File

@@ -366,7 +366,9 @@ void Application::registerLuaFunctions()
// ConfigManager
g_lua.registerStaticClass("g_configs");
g_lua.bindClassStaticFunction("g_configs", "set", std::bind(&ConfigManager::set, &g_configs, _1, _2));
g_lua.bindClassStaticFunction("g_configs", "setList", std::bind(&ConfigManager::setList, &g_configs, _1, _2));
g_lua.bindClassStaticFunction("g_configs", "get", std::bind(&ConfigManager::get, &g_configs, _1));
g_lua.bindClassStaticFunction("g_configs", "getList", std::bind(&ConfigManager::getList, &g_configs, _1));
g_lua.bindClassStaticFunction("g_configs", "exists", std::bind(&ConfigManager::exists, &g_configs, _1));
g_lua.bindClassStaticFunction("g_configs", "remove", std::bind(&ConfigManager::remove, &g_configs, _1));

View File

@@ -125,6 +125,9 @@ luavalue_cast(int index, std::function<Ret(Args...)>& func);
template<typename T>
void push_luavalue(const std::vector<T>& vec);
template<typename T>
bool luavalue_cast(int index, std::vector<T>& vec);
// deque
template<class T>
void push_luavalue(const std::deque<T>& vec);
@@ -249,6 +252,22 @@ void push_luavalue(const std::vector<T>& vec) {
}
}
template<typename T>
bool luavalue_cast(int index, std::vector<T>& vec)
{
if(g_lua.isTable(index)) {
g_lua.pushNil();
while(g_lua.next(index < 0 ? index-1 : index)) {
T value;
if(luavalue_cast(-1, value))
vec.push_back(value);
g_lua.pop();
}
return true;
}
return false;
}
template<typename T>
void push_luavalue(const std::deque<T>& vec) {
g_lua.newTable();