From 9946459c0cd65c3b66719a2aefc42c7ab2a29c04 Mon Sep 17 00:00:00 2001 From: erihel Date: Thu, 9 Aug 2012 23:04:29 +0200 Subject: * changed 0, NULL to nullptr * changed profile.cpp to use SimpleIni to load config files * added new CProfile singleton class for loading config * added SimpleIni to lib/ dir * added config loading tests --- src/common/profile.cpp | 96 ++++++++++++++++++-------------------------------- 1 file changed, 35 insertions(+), 61 deletions(-) (limited to 'src/common/profile.cpp') diff --git a/src/common/profile.cpp b/src/common/profile.cpp index d921d34..467e991 100644 --- a/src/common/profile.cpp +++ b/src/common/profile.cpp @@ -17,98 +17,72 @@ // profile.cpp -#include -#include -#include +#include -#include "common/language.h" -#include "common/struct.h" -#include "common/profile.h" +template<> CProfile* CSingleton::mInstance = nullptr; -static char g_filename[100]; - +CProfile::CProfile() +{ + m_ini = new CSimpleIniA(); + m_ini->SetUnicode(); +} -bool InitCurrentDirectory() +CProfile::~CProfile() { -#if _SCHOOL - _fullpath(g_filename, "ceebot.ini", 100); -#else - _fullpath(g_filename, "colobot.ini", 100); -#endif - return true; + m_ini->Reset(); + delete m_ini; } -bool SetLocalProfileString(char* section, char* key, char* string) +bool CProfile::InitCurrentDirectory() { - WritePrivateProfileString(section, key, string, g_filename); + m_ini->LoadFile("colobot.ini"); return true; } -bool GetLocalProfileString(char* section, char* key, char* buffer, int max) -{ - int nb; - nb = GetPrivateProfileString(section, key, "", buffer, max, g_filename); - if ( nb == 0 ) - { - buffer[0] = 0; - return false; - } - return true; +bool CProfile::SetLocalProfileString(std::string section, std::string key, std::string value) +{ + return (m_ini->SetValue(section.c_str(), key.c_str(), value.c_str()) == SI_OK); } -bool SetLocalProfileInt(char* section, char* key, int value) +bool CProfile::GetLocalProfileString(std::string section, std::string key, std::string &buffer) { - char s[20]; + const char* value = m_ini->GetValue(section.c_str(), key.c_str(), nullptr); + if (strlen(value) > 0) { + buffer = std::string(value); + return true; + } - sprintf(s, "%d", value); - WritePrivateProfileString(section, key, s, g_filename); - return true; + return false; } -bool GetLocalProfileInt(char* section, char* key, int &value) + +bool CProfile::SetLocalProfileInt(std::string section, std::string key, int value) { - char s[20]; - int nb; - - nb = GetPrivateProfileString(section, key, "", s, 20, g_filename); - if ( nb == 0 ) - { - value = 0; - return false; - } - sscanf(s, "%d", &value); - return true; + return (m_ini->SetLongValue(section.c_str(), key.c_str(), value) == SI_OK); } -bool SetLocalProfileFloat(char* section, char* key, float value) +bool CProfile::GetLocalProfileInt(std::string section, std::string key, int &value) { - char s[20]; - - sprintf(s, "%.2f", value); - WritePrivateProfileString(section, key, s, g_filename); + value = m_ini->GetLongValue(section.c_str(), key.c_str(), 0L); return true; } -bool GetLocalProfileFloat(char* section, char* key, float &value) + +bool CProfile::SetLocalProfileFloat(std::string section, std::string key, float value) { - char s[20]; - int nb; - - nb = GetPrivateProfileString(section, key, "", s, 20, g_filename); - if ( nb == 0 ) - { - value = 0.0f; - return false; - } - sscanf(s, "%f", &value); - return true; + return (m_ini->SetDoubleValue(section.c_str(), key.c_str(), value) == SI_OK); } +bool CProfile::GetLocalProfileFloat(std::string section, std::string key, float &value) +{ + value = m_ini->GetDoubleValue(section.c_str(), key.c_str(), 0.0d); + return true; +} -- cgit v1.2.3-1-g7c22 From 5e271e550dbb88f0bbea5f46aad9f0fd1d750eb3 Mon Sep 17 00:00:00 2001 From: erihel Date: Sun, 12 Aug 2012 15:00:37 +0200 Subject: * New CPluginManager class for managing plugins based on colobot.ini * Moved sound plugin into sound dir * Minor changes in logger and profile --- src/common/profile.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/common/profile.cpp') diff --git a/src/common/profile.cpp b/src/common/profile.cpp index 467e991..29a68e1 100644 --- a/src/common/profile.cpp +++ b/src/common/profile.cpp @@ -27,6 +27,7 @@ CProfile::CProfile() { m_ini = new CSimpleIniA(); m_ini->SetUnicode(); + m_ini->SetMultiKey(); } @@ -39,8 +40,8 @@ CProfile::~CProfile() bool CProfile::InitCurrentDirectory() { - m_ini->LoadFile("colobot.ini"); - return true; + bool result = m_ini->LoadFile("colobot.ini") == SI_OK; + return result; } @@ -86,3 +87,19 @@ bool CProfile::GetLocalProfileFloat(std::string section, std::string key, float value = m_ini->GetDoubleValue(section.c_str(), key.c_str(), 0.0d); return true; } + + +std::vector< std::string > CProfile::GetLocalProfileSection(std::string section, std::string key) +{ + std::vector< std::string > ret_list; + + CSimpleIniA::TNamesDepend values; + m_ini->GetAllValues(section.c_str(), key.c_str(), values); + values.sort(CSimpleIniA::Entry::LoadOrder()); + + for (auto item : values) { + ret_list.push_back(item.pItem); + } + + return ret_list; +} -- cgit v1.2.3-1-g7c22