Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef CONFIGMANAGER_H
- #define CONFIGMANAGER_H
- #include "FileManager.h"
- #include <boost\lexical_cast.hpp>
- #include <boost\regex.hpp>
- #define CONFIGS_DIRECTORY "configs"
- enum EConfigErrorType
- {
- CONFIG_NO_ERROR = 0,
- CONFIG_ERROR_OPEN = 1,
- CONFIG_ERROR_NOT_FOUND = 2,
- CONFIG_ERROR_SYNTAX = 3
- };
- class ConfigManager : public ManagerBase
- {
- public:
- ConfigManager() {};
- ~ConfigManager() {};
- void init(const std::string& name);
- void terminate();
- template<typename T>
- T readValue(const std::string& key)
- {
- std::string line = m_file->searchString(key);
- if (line.empty())
- {
- m_lastError = CONFIG_ERROR_NOT_FOUND;
- return T();
- }
- boost::smatch match;
- if (!boost::regex_search(line, match, boost::regex(key + "=\"(.*)\"")))
- {
- m_lastError = CONFIG_ERROR_SYNTAX;
- return T();
- }
- m_lastError = CONFIG_NO_ERROR;
- return boost::lexical_cast<T>(match[1]);
- }
- template<typename T>
- void writeValue(const std::string& key, T value)
- {
- char prefix = typeid(T).name()[0];
- std::string line = m_file->searchString(key);
- if (line.empty())
- m_file->addContent(prefix + key + "=\"" + std::to_string(value) + "\"");
- else
- m_file->replaceString(line, prefix + key + "=\"" + std::to_string(value) + "\"");
- m_lastError = CONFIG_NO_ERROR;
- }
- private:
- FileManager* m_file;
- };
- extern ConfigManager g_config;
- #endif // CONFIGMANAGER_H
- #include "ConfigManager.h"
- ConfigManager g_config;
- void ConfigManager::init(const std::string & name)
- {
- m_file = new FileManager(CONFIGS_DIRECTORY "/" + name);
- if (!m_file->isGood())
- m_lastError = CONFIG_ERROR_OPEN;
- }
- void ConfigManager::terminate()
- {
- if (m_file->isGood())
- m_file->save();
- delete m_file;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement