From 7308e6ef001683266fcbeb8701ed4f68bea2ff59 Mon Sep 17 00:00:00 2001 From: Mathieu Schroeter Date: Sun, 13 Aug 2017 23:01:04 +0200 Subject: [PATCH] Convert ini file to json --- resources/data/config.ini | 3 -- resources/data/config.json | 5 ++ src/blupi.cxx | 105 +++++++++++++------------------------ 3 files changed, 40 insertions(+), 73 deletions(-) delete mode 100644 resources/data/config.ini create mode 100644 resources/data/config.json diff --git a/resources/data/config.ini b/resources/data/config.ini deleted file mode 100644 index ec8fe57..0000000 --- a/resources/data/config.ini +++ /dev/null @@ -1,3 +0,0 @@ -FullScreen=1 -SpeedRate=1 -Timer=50ms diff --git a/resources/data/config.json b/resources/data/config.json new file mode 100644 index 0000000..f873153 --- /dev/null +++ b/resources/data/config.json @@ -0,0 +1,5 @@ +{ + "fullscreen": 1, + "speedrate": 1, + "timerinterval": 50 +} diff --git a/src/blupi.cxx b/src/blupi.cxx index cb47f7a..2192a59 100644 --- a/src/blupi.cxx +++ b/src/blupi.cxx @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -97,25 +98,6 @@ static std::vector split (const std::string & s, char delim) return elems; } -/** - * \brief Read an integer from a string. - * - * \param[in] p - Input string. - * \returns the integer. - */ -static Sint32 GetNum (char * p) -{ - Sint32 n = 0; - - while (*p >= '0' && *p <= '9') - { - n *= 10; - n += (*p++) - '0'; - } - - return n; -} - /** * \brief Read the config file. * @@ -123,67 +105,50 @@ static Sint32 GetNum (char * p) */ static bool ReadConfig () { - FILE * file = nullptr; - char buffer[200]; - char * pText; - size_t nb; + const auto config = GetBaseDir () + "data/config.json"; - const auto config = GetBaseDir () + "data/config.ini"; + std::ifstream file (config, std::ifstream::in); + nlohmann::json j; + file >> j; - file = fopen (config.c_str (), "rb"); - if (file == nullptr) - return false; - nb = fread (buffer, sizeof (char), 200 - 1, file); - buffer[nb] = 0; - fclose (file); - - if (!(g_settingsOverload & SETTING_SPEEDRATE)) + if ( + !(g_settingsOverload & SETTING_SPEEDRATE) && + j.find ("speedrate") != j.end ()) { - pText = strstr (buffer, "SpeedRate="); - if (pText != nullptr) - { - g_speedRate = GetNum (pText + 10); - if (g_speedRate < 1) - g_speedRate = 1; - if (g_speedRate > 2) - g_speedRate = 2; - } + g_speedRate = j["speedrate"].get (); + if (g_speedRate < 1) + g_speedRate = 1; + if (g_speedRate > 2) + g_speedRate = 2; } - if (!(g_settingsOverload & SETTING_TIMERINTERVAL)) + if ( + !(g_settingsOverload & SETTING_TIMERINTERVAL) && + j.find ("timerinterval") != j.end ()) { - pText = strstr (buffer, "Timer="); - if (pText != nullptr) - { - g_timerInterval = GetNum (pText + 6); - if (g_timerInterval < 10) - g_timerInterval = 10; - if (g_timerInterval > 1000) - g_timerInterval = 1000; - } + g_timerInterval = j["timerinterval"].get (); + if (g_timerInterval < 10) + g_timerInterval = 10; + if (g_timerInterval > 1000) + g_timerInterval = 1000; } - if (!(g_settingsOverload & SETTING_FULLSCREEN)) + if ( + !(g_settingsOverload & SETTING_FULLSCREEN) && + j.find ("fullscreen") != j.end ()) { - pText = strstr (buffer, "FullScreen="); - if (pText != nullptr) - { - g_bFullScreen = !!GetNum (pText + 11); - if (g_bFullScreen != 0) - g_bFullScreen = 1; - } + g_bFullScreen = !j["fullscreen"].get (); + if (g_bFullScreen != 0) + g_bFullScreen = 1; } - if (!(g_settingsOverload & SETTING_RENDERER)) + if ( + !(g_settingsOverload & SETTING_RENDERER) && j.find ("renderer") != j.end ()) { - pText = strstr (buffer, "Renderer="); - if (pText) - { - if (!strncmp (pText + 9, "software", 8)) - g_rendererType = SDL_RENDERER_SOFTWARE; - else if (!strncmp (pText + 9, "accelerated", 11)) - g_rendererType = SDL_RENDERER_ACCELERATED; - } + if (j["renderer"] == "software") + g_rendererType = SDL_RENDERER_SOFTWARE; + else if (j["renderer"] == "accelerated") + g_rendererType = SDL_RENDERER_ACCELERATED; } return true; @@ -618,7 +583,7 @@ static int DoInit (int argc, char * argv[], bool & exit) RECT rcRect; bool bOK; - bOK = ReadConfig (); // lit le fichier config.ini + bOK = ReadConfig (); // lit le fichier config.json auto res = SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER); if (res < 0) @@ -655,7 +620,7 @@ static int DoInit (int argc, char * argv[], bool & exit) return EXIT_FAILURE; } - if (!bOK) // Something wrong with config.ini file? + if (!bOK) // Something wrong with config.json file? { InitFail ("Game not correctly installed"); return EXIT_FAILURE;