diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/event.h | 4 | ||||
-rw-r--r-- | src/common/iman.cpp | 15 | ||||
-rw-r--r-- | src/common/iman.h | 8 | ||||
-rw-r--r-- | src/common/logger.cpp | 16 | ||||
-rw-r--r-- | src/common/logger.h | 4 | ||||
-rw-r--r-- | src/common/profile.cpp | 111 | ||||
-rw-r--r-- | src/common/profile.h | 98 | ||||
-rw-r--r-- | src/common/test/CMakeLists.txt | 8 | ||||
-rw-r--r-- | src/common/test/colobot.ini | 15 | ||||
-rw-r--r-- | src/common/test/profile_test.cpp | 43 |
10 files changed, 231 insertions, 91 deletions
diff --git a/src/common/event.h b/src/common/event.h index a82282e..1cfab35 100644 --- a/src/common/event.h +++ b/src/common/event.h @@ -19,8 +19,8 @@ #pragma once -#include "common/key.h" -#include "math/point.h" +#include <common/key.h> +#include <math/point.h> #include <string.h> diff --git a/src/common/iman.cpp b/src/common/iman.cpp index 90b0c1e..4b89ecf 100644 --- a/src/common/iman.cpp +++ b/src/common/iman.cpp @@ -23,6 +23,21 @@ #include "common/iman.h" +template<> CInstanceManager* CSingleton<CInstanceManager>::mInstance = nullptr; + + +CInstanceManager& CInstanceManager::GetInstance() +{ + assert(mInstance); + return *mInstance; +} + + +CInstanceManager* CInstanceManager::GetInstancePointer() +{ + assert(mInstance); + return mInstance; +} // Object's constructor. diff --git a/src/common/iman.h b/src/common/iman.h index 7a7b499..89b5206 100644 --- a/src/common/iman.h +++ b/src/common/iman.h @@ -18,8 +18,8 @@ #pragma once - -#include "common/misc.h" +#include <common/singleton.h> +#include <common/misc.h> @@ -32,7 +32,7 @@ struct BaseClass -class CInstanceManager +class CInstanceManager : public CSingleton<CInstanceManager> { public: CInstanceManager(); @@ -44,6 +44,8 @@ public: bool DeleteInstance(ClassType classType, void* pointer); void* SearchInstance(ClassType classType, int rank=0); + static CInstanceManager& GetInstance(); + static CInstanceManager* GetInstancePointer(); protected: void Compress(ClassType classType); diff --git a/src/common/logger.cpp b/src/common/logger.cpp index f24726e..be73ec7 100644 --- a/src/common/logger.cpp +++ b/src/common/logger.cpp @@ -21,21 +21,7 @@ #include <stdio.h> -template<> CLogger* CSingleton<CLogger>::mInstance = 0; - - -CLogger& CLogger::GetInstance() -{ - assert(mInstance); - return *mInstance; -} - - -CLogger* CLogger::GetInstancePointer() -{ - assert(mInstance); - return mInstance; -} +template<> CLogger* CSingleton<CLogger>::mInstance = nullptr; CLogger::CLogger() diff --git a/src/common/logger.h b/src/common/logger.h index 5de3d63..f126e52 100644 --- a/src/common/logger.h +++ b/src/common/logger.h @@ -21,6 +21,7 @@ #include <string> #include <cstdarg> +#include <cstdio> #include <common/singleton.h> @@ -90,9 +91,6 @@ class CLogger : public CSingleton<CLogger> */ void SetLogLevel(LogType level); - static CLogger& GetInstance(); - static CLogger* GetInstancePointer(); - private: std::string mFilename; FILE *mFile; diff --git a/src/common/profile.cpp b/src/common/profile.cpp index d921d34..29a68e1 100644 --- a/src/common/profile.cpp +++ b/src/common/profile.cpp @@ -17,98 +17,89 @@ // profile.cpp -#include <stdio.h> -#include <d3d.h> -#include <stdlib.h> +#include <common/profile.h> -#include "common/language.h" -#include "common/struct.h" -#include "common/profile.h" +template<> CProfile* CSingleton<CProfile>::mInstance = nullptr; -static char g_filename[100]; - +CProfile::CProfile() +{ + m_ini = new CSimpleIniA(); + m_ini->SetUnicode(); + m_ini->SetMultiKey(); +} -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); - return true; + bool result = m_ini->LoadFile("colobot.ini") == SI_OK; + return result; } -bool GetLocalProfileString(char* section, char* key, char* buffer, int max) + +bool CProfile::SetLocalProfileString(std::string section, std::string key, std::string value) { - int nb; + return (m_ini->SetValue(section.c_str(), key.c_str(), value.c_str()) == SI_OK); +} - nb = GetPrivateProfileString(section, key, "", buffer, max, g_filename); - if ( nb == 0 ) - { - buffer[0] = 0; - return false; + +bool CProfile::GetLocalProfileString(std::string section, std::string key, std::string &buffer) +{ + const char* value = m_ini->GetValue(section.c_str(), key.c_str(), nullptr); + if (strlen(value) > 0) { + buffer = std::string(value); + return true; } - return true; + + return false; } -bool SetLocalProfileInt(char* section, char* key, int value) +bool CProfile::SetLocalProfileInt(std::string section, std::string key, int value) { - char s[20]; - - sprintf(s, "%d", value); - WritePrivateProfileString(section, key, s, g_filename); - return true; + return (m_ini->SetLongValue(section.c_str(), key.c_str(), value) == SI_OK); } -bool GetLocalProfileInt(char* section, char* key, int &value) + +bool CProfile::GetLocalProfileInt(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); + value = m_ini->GetLongValue(section.c_str(), key.c_str(), 0L); return true; } -bool SetLocalProfileFloat(char* section, char* key, float value) +bool CProfile::SetLocalProfileFloat(std::string section, std::string key, float value) { - char s[20]; - - sprintf(s, "%.2f", value); - WritePrivateProfileString(section, key, s, g_filename); - return true; + return (m_ini->SetDoubleValue(section.c_str(), key.c_str(), value) == SI_OK); } -bool GetLocalProfileFloat(char* section, char* key, float &value) + +bool CProfile::GetLocalProfileFloat(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); + 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; +} diff --git a/src/common/profile.h b/src/common/profile.h index 2c76a0b..0886522 100644 --- a/src/common/profile.h +++ b/src/common/profile.h @@ -18,13 +18,97 @@ #pragma once +#include <cstdlib> +#include <vector> +#include <utility> -extern bool InitCurrentDirectory(); -extern bool SetLocalProfileString(char* section, char* key, char* string); -extern bool GetLocalProfileString(char* section, char* key, char* buffer, int max); -extern bool SetLocalProfileInt(char* section, char* key, int value); -extern bool GetLocalProfileInt(char* section, char* key, int &value); -extern bool SetLocalProfileFloat(char* section, char* key, float value); -extern bool GetLocalProfileFloat(char* section, char* key, float &value); +#include <lib/simpleini/SimpleIni.h> +#include <common/singleton.h> +/** + * @file common/profile.h + * @brief Class for loading profile (currently for loading ini config file) + */ + + +/** +* @class CProfile +* +* @brief Class for loading profile (currently for loading ini config file) +* +*/ +class CProfile : public CSingleton<CProfile> +{ + public: + CProfile(); + ~CProfile(); + + /** Loads colobot.ini from current directory + * @return return true on success + */ + bool InitCurrentDirectory(); + + /** Sets string value in section under specified key + * @param std::string section + * @param std::string key + * @param std::string value + * @return return true on success + */ + bool SetLocalProfileString(std::string section, std::string key, std::string value); + + /** Gets string value in section under specified key + * @param std::string section + * @param std::string key + * @param std::string& buffer + * @return return true on success + */ + bool GetLocalProfileString(std::string section, std::string key, std::string& buffer); + + /** Sets int value in section under specified key + * @param std::string section + * @param std::string key + * @param int value + * @return return true on success + */ + bool SetLocalProfileInt(std::string section, std::string key, int value); + + /** Gets int value in section under specified key + * @param std::string section + * @param std::string key + * @param int& value + * @return return true on success + */ + bool GetLocalProfileInt(std::string section, std::string key, int &value); + + /** Sets float value in section under specified key + * @param std::string section + * @param std::string key + * @param float value + * @return return true on success + */ + bool SetLocalProfileFloat(std::string section, std::string key, float value); + + /** Gets float value in section under specified key + * @param std::string section + * @param std::string key + * @param float& value + * @return return true on success + */ + bool GetLocalProfileFloat(std::string section, std::string key, float &value); + + /** Gets all values in section under specified key + * @param std::string section + * @param std::string key + * @return vector of values + */ + std::vector< std::string > GetLocalProfileSection(std::string section, std::string key); + + private: + CSimpleIniA *m_ini; +}; + +//! Global function to get profile instance +inline CProfile* GetProfile() { + return CProfile::GetInstancePointer(); +} diff --git a/src/common/test/CMakeLists.txt b/src/common/test/CMakeLists.txt index 680116c..d81acab 100644 --- a/src/common/test/CMakeLists.txt +++ b/src/common/test/CMakeLists.txt @@ -1,6 +1,12 @@ cmake_minimum_required(VERSION 2.8) set(CMAKE_BUILD_TYPE debug) -set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") +set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0 -std=c++11") + +include_directories("../../") +include_directories("../../../") add_executable(image_test ../image.cpp image_test.cpp) +add_executable(profile_test ../profile.cpp profile_test.cpp) + +add_test(profile_test ./profile_test) diff --git a/src/common/test/colobot.ini b/src/common/test/colobot.ini new file mode 100644 index 0000000..f6a5f96 --- /dev/null +++ b/src/common/test/colobot.ini @@ -0,0 +1,15 @@ +[test_float] +float_value=1.5 + +[test_string] +string_value=Hello world + +[test_int] +int_value=42 + +[test_multi] +entry=1 +entry=2 +entry=3 +entry=4 +entry=5 diff --git a/src/common/test/profile_test.cpp b/src/common/test/profile_test.cpp new file mode 100644 index 0000000..65e20c5 --- /dev/null +++ b/src/common/test/profile_test.cpp @@ -0,0 +1,43 @@ +#include "../profile.h" + +#include <iostream> +#include <string> +#include <vector> + +using namespace std; + +int main() +{ + CProfile profile; + profile.InitCurrentDirectory(); // load colobot.ini file + + string result; + profile.GetLocalProfileString("test_string", "string_value", result); + if (result != "Hello world") { + cout << "GetLocalProfileString failed!" << endl; + return 1; + } + + int int_value; + profile.GetLocalProfileInt("test_int", "int_value", int_value); + if (int_value != 42) { + cout << "GetLocalProfileInt failed!" << endl; + return 1; + } + + float float_value; + profile.GetLocalProfileFloat("test_float", "float_value", float_value); + if (float_value != 1.5) { + cout << "GetLocalProfileFloat failed!" << endl; + return 1; + } + + vector<string> list; + list = profile.GetLocalProfileSection("test_multi", "entry"); + if (list.size() != 5) { + cout << "GetLocalProfileSection failed!" << endl; + return 1; + } + + return 0; +} |