diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/iman.cpp | 2 | ||||
-rw-r--r-- | src/common/logger.cpp | 2 | ||||
-rw-r--r-- | src/common/profile.cpp | 96 | ||||
-rw-r--r-- | src/common/profile.h | 33 | ||||
-rw-r--r-- | src/common/test/CMakeLists.txt | 10 | ||||
-rw-r--r-- | src/common/test/colobot.ini | 8 | ||||
-rw-r--r-- | src/common/test/profile_test.cpp | 35 |
7 files changed, 114 insertions, 72 deletions
diff --git a/src/common/iman.cpp b/src/common/iman.cpp index 4c70cde..4b89ecf 100644 --- a/src/common/iman.cpp +++ b/src/common/iman.cpp @@ -23,7 +23,7 @@ #include "common/iman.h" -template<> CInstanceManager* CSingleton<CInstanceManager>::mInstance = 0; +template<> CInstanceManager* CSingleton<CInstanceManager>::mInstance = nullptr; CInstanceManager& CInstanceManager::GetInstance() diff --git a/src/common/logger.cpp b/src/common/logger.cpp index f24726e..a0dcca0 100644 --- a/src/common/logger.cpp +++ b/src/common/logger.cpp @@ -21,7 +21,7 @@ #include <stdio.h> -template<> CLogger* CSingleton<CLogger>::mInstance = 0; +template<> CLogger* CSingleton<CLogger>::mInstance = nullptr; CLogger& CLogger::GetInstance() 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 <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(); +} -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; +} diff --git a/src/common/profile.h b/src/common/profile.h index 2c76a0b..ae67e52 100644 --- a/src/common/profile.h +++ b/src/common/profile.h @@ -18,13 +18,32 @@ #pragma once +#include <cstdlib> -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> + +class CProfile : public CSingleton<CProfile> +{ + public: + CProfile(); + ~CProfile(); + + bool InitCurrentDirectory(); + bool SetLocalProfileString(std::string section, std::string key, std::string value); + bool GetLocalProfileString(std::string section, std::string key, std::string& buffer); + + bool SetLocalProfileInt(std::string section, std::string key, int value); + bool GetLocalProfileInt(std::string section, std::string key, int &value); + + bool SetLocalProfileFloat(std::string section, std::string key, float value); + bool GetLocalProfileFloat(std::string section, std::string key, float &value); + + static CProfile& GetInstance(); + static CProfile* GetInstancePointer(); + + private: + CSimpleIniA *m_ini; +}; diff --git a/src/common/test/CMakeLists.txt b/src/common/test/CMakeLists.txt index 680116c..3adca4e 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") -add_executable(image_test ../image.cpp image_test.cpp) +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..c4d2162 --- /dev/null +++ b/src/common/test/colobot.ini @@ -0,0 +1,8 @@ +[test_float] +float_value=1.5 + +[test_string] +string_value=Hello world + +[test_int] +int_value=42 diff --git a/src/common/test/profile_test.cpp b/src/common/test/profile_test.cpp new file mode 100644 index 0000000..3ba0fad --- /dev/null +++ b/src/common/test/profile_test.cpp @@ -0,0 +1,35 @@ +#include "../profile.h" + +#include <iostream> +#include <string> + +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; + } + + return 0; +} |