colobot/src/common/config_file.cpp

260 lines
6.6 KiB
C++
Raw Normal View History

/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
2015-07-19 13:30:30 +00:00
#include "common/config_file.h"
#include "app/system.h"
2014-08-12 19:24:33 +00:00
#include "common/logger.h"
#include "common/make_unique.h"
2014-08-12 19:24:33 +00:00
#include "common/resources/inputstream.h"
#include "common/resources/outputstream.h"
2014-10-07 20:05:16 +00:00
#include <memory>
#include <utility>
#include <cstring>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/regex.hpp>
2015-07-19 13:30:30 +00:00
template<> CConfigFile* CSingleton<CConfigFile>::m_instance = nullptr;
namespace bp = boost::property_tree;
2015-07-19 13:30:30 +00:00
CConfigFile::CConfigFile()
: m_needsSave(false)
, m_useCurrentDirectory(false)
2015-07-19 13:30:30 +00:00
, m_loaded(false)
{
}
2015-07-19 13:30:30 +00:00
CConfigFile::~CConfigFile()
{
if (m_needsSave)
{
GetLogger()->Warn("Config file was not properly saved! Saving now...\n");
Save();
}
}
2015-07-19 13:30:30 +00:00
void CConfigFile::SetUseCurrentDirectory(bool useCurrentDirectory)
{
2014-08-12 19:24:33 +00:00
m_useCurrentDirectory = useCurrentDirectory;
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::Init()
{
try
{
2014-10-07 20:05:16 +00:00
std::unique_ptr<std::istream> stream;
bool good;
2014-10-07 20:05:16 +00:00
if (m_useCurrentDirectory)
{
auto inputStream = MakeUnique<std::ifstream>("./colobot.ini");
good = inputStream->good();
stream = std::move(inputStream);
2014-10-07 20:05:16 +00:00
}
else
{
auto inputStream = MakeUnique<CInputStream>("colobot.ini");
good = inputStream->is_open();
stream = std::move(inputStream);
2014-10-07 20:05:16 +00:00
}
if (good)
2014-10-07 20:05:16 +00:00
{
bp::ini_parser::read_ini(*stream, m_propertyTree);
2015-07-19 13:30:30 +00:00
m_loaded = true;
2014-10-07 20:05:16 +00:00
}
else
{
GetLogger()->Error("Error on parsing config file: failed to open file\n");
2014-07-24 21:38:13 +00:00
return false;
}
}
catch (std::exception & e)
{
GetLogger()->Error("Error on parsing config file: %s\n", e.what());
return false;
}
return true;
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::Save()
{
if (m_needsSave)
{
try
2015-07-19 13:30:30 +00:00
{
std::unique_ptr<std::ostream> stream;
bool good;
if (m_useCurrentDirectory)
{
auto outputStream = MakeUnique<std::ofstream>("./colobot.ini");
good = outputStream->good();
stream = std::move(outputStream);
}
else
{
auto outputStream = MakeUnique<COutputStream>("colobot.ini");
good = outputStream->is_open();
stream = std::move(outputStream);
}
if (good)
{
bp::ini_parser::write_ini(*stream, m_propertyTree);
m_needsSave = false;
}
else
{
GetLogger()->Error("Error on storing config file: failed to open file\n");
return false;
}
2015-07-19 13:30:30 +00:00
}
catch (std::exception & e)
2015-07-19 13:30:30 +00:00
{
GetLogger()->Error("Error on storing config file: %s\n", e.what());
return false;
}
}
return true;
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::SetStringProperty(std::string section, std::string key, std::string value)
{
try
{
m_propertyTree.put(section + "." + key, value);
m_needsSave = true;
}
catch (std::exception & e)
{
GetLogger()->Error("Error on editing config file: %s\n", e.what());
return false;
}
return true;
}
2015-08-15 16:26:27 +00:00
bool CConfigFile::GetStringProperty(std::string section, std::string key, std::string &value)
{
try
{
2015-08-15 16:26:27 +00:00
std::string readValue = m_propertyTree.get<std::string>(section + "." + key);
value = std::move(readValue);
}
catch (std::exception & e)
{
2015-08-15 17:45:49 +00:00
GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s\n", e.what());
return false;
}
return true;
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::SetIntProperty(std::string section, std::string key, int value)
{
try
{
m_propertyTree.put(section + "." + key, value);
m_needsSave = true;
}
catch (std::exception & e)
{
GetLogger()->Error("Error on editing config file: %s\n", e.what());
return false;
}
return true;
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::GetIntProperty(std::string section, std::string key, int &value)
{
try
{
2015-08-15 16:26:27 +00:00
int readValue = m_propertyTree.get<int>(section + "." + key);
value = readValue;
}
catch (std::exception & e)
{
2015-08-15 17:45:49 +00:00
GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s\n", e.what());
return false;
}
return true;
}
2015-08-15 16:26:27 +00:00
bool CConfigFile::SetBoolProperty(std::string section, std::string key, bool value)
{
return SetIntProperty(section, key, value ? 1 : 0);
}
bool CConfigFile::GetBoolProperty(std::string section, std::string key, bool& value)
{
int intValue = 0;
bool result = GetIntProperty(section, key, intValue);
if (result)
{
2015-08-15 17:45:49 +00:00
if (intValue == 0)
{
value = false;
}
else if (intValue == 1)
{
value = true;
}
else
{
GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing bool property %s.%s (expected 0 or 1, not %d)\n", section, key, intValue);
return false;
}
2015-08-15 16:26:27 +00:00
}
return result;
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::SetFloatProperty(std::string section, std::string key, float value)
{
try
{
m_propertyTree.put(section + "." + key, value);
m_needsSave = true;
}
catch (std::exception & e)
{
GetLogger()->Error("Error on editing config file: %s\n", e.what());
return false;
}
return true;
}
2015-07-19 13:30:30 +00:00
bool CConfigFile::GetFloatProperty(std::string section, std::string key, float &value)
{
try
{
2015-08-15 16:26:27 +00:00
float readValue = m_propertyTree.get<float>(section + "." + key);
value = readValue;
}
catch (std::exception & e)
{
2015-08-15 17:45:49 +00:00
GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s\n", e.what());
2015-07-19 13:30:30 +00:00
return false;
}
2015-07-19 13:30:30 +00:00
return true;
}