flycast/core/cfg/cfg.cpp

121 lines
2.7 KiB
C++
Raw Normal View History

2013-12-19 17:10:14 +00:00
#include "cfg.h"
#include "ini.h"
#include "stdclass.h"
#include <cerrno>
2013-12-19 17:10:14 +00:00
2020-03-29 17:29:14 +00:00
static std::string cfgPath;
static bool save_config = true;
2019-10-29 13:34:29 +00:00
static bool autoSave = true;
2013-12-19 17:10:14 +00:00
static emucfg::ConfigFile cfgdb;
2013-12-19 17:10:14 +00:00
static void saveConfigFile()
2013-12-19 17:10:14 +00:00
{
FILE* cfgfile = nowide::fopen(cfgPath.c_str(), "wt");
2013-12-19 17:10:14 +00:00
if (!cfgfile)
2015-08-24 12:46:45 +00:00
{
2019-07-01 16:23:10 +00:00
WARN_LOG(COMMON, "Error: Unable to open file '%s' for saving", cfgPath.c_str());
2015-08-24 12:46:45 +00:00
}
2013-12-19 17:10:14 +00:00
else
{
cfgdb.save(cfgfile);
std::fclose(cfgfile);
2013-12-19 17:10:14 +00:00
}
}
void cfgSaveStr(const std::string& section, const std::string& key, const std::string& value)
2013-12-19 17:10:14 +00:00
{
cfgdb.set(section, key, value);
2019-10-29 13:34:29 +00:00
if (save_config && autoSave)
saveConfigFile();
2013-12-19 17:10:14 +00:00
}
bool cfgOpen()
{
if (get_writable_config_path("").empty())
// Config dir not set (android onboarding)
return false;
const char* filename = "emu.cfg";
2020-03-29 17:29:14 +00:00
std::string config_path_read = get_readonly_config_path(filename);
cfgPath = get_writable_config_path(filename);
FILE* cfgfile = nowide::fopen(config_path_read.c_str(), "r");
if(cfgfile != NULL) {
cfgdb.parse(cfgfile);
std::fclose(cfgfile);
2015-08-24 12:05:51 +00:00
}
else
{
// Config file can't be opened
int error_code = errno;
2019-07-01 16:23:10 +00:00
WARN_LOG(COMMON, "Warning: Unable to open the config file '%s' for reading (%s)", config_path_read.c_str(), strerror(error_code));
if (error_code == ENOENT || cfgPath != config_path_read)
{
// Config file didn't exist
2019-07-01 16:23:10 +00:00
INFO_LOG(COMMON, "Creating new empty config file at '%s'", cfgPath.c_str());
saveConfigFile();
}
else
{
// There was some other error (may be a permissions problem or something like that)
save_config = false;
}
2013-12-19 17:10:14 +00:00
}
return true;
}
std::string cfgLoadStr(const std::string& section, const std::string& key, const std::string& def)
2015-03-22 00:16:28 +00:00
{
return cfgdb.get(section, key, def);
2015-03-22 00:16:28 +00:00
}
void cfgSaveInt(const std::string& section, const std::string& key, s32 value)
2013-12-19 17:10:14 +00:00
{
cfgSaveStr(section, key, std::to_string(value));
2013-12-19 17:10:14 +00:00
}
s32 cfgLoadInt(const std::string& section, const std::string& key, s32 def)
2013-12-19 17:10:14 +00:00
{
return cfgdb.get_int(section, key, def);
2016-03-02 05:48:34 +00:00
}
void cfgSaveBool(const std::string& section, const std::string& key, bool value)
2013-12-19 17:10:14 +00:00
{
cfgSaveStr(section, key, value ? "yes" : "no");
}
bool cfgLoadBool(const std::string& section, const std::string& key, bool def)
{
return cfgdb.get_bool(section, key, def);
}
void cfgSetVirtual(const std::string& section, const std::string& key, const std::string& value)
{
cfgdb.set(section, key, value, true);
}
bool cfgIsVirtual(const std::string& section, const std::string& key)
{
return cfgdb.is_virtual(section, key);
}
bool cfgHasSection(const std::string& section)
{
return cfgdb.has_section(section);
}
void cfgDeleteSection(const std::string& section)
{
cfgdb.delete_section(section);
}
2019-10-29 13:34:29 +00:00
void cfgSetAutoSave(bool autoSave)
{
::autoSave = autoSave;
if (autoSave)
saveConfigFile();
2019-10-29 13:34:29 +00:00
}