Tightened up file handling code for config stores so that Windows is happy.

This commit is contained in:
David Salvisberg 2015-07-19 01:00:47 +02:00
parent 464657e20e
commit 9d8aad7ffe
3 changed files with 164 additions and 120 deletions

View File

@ -38,8 +38,8 @@ extern "C" {
#include <libguess.h>
}
#include <iconv.h>
#include <errno.h>
#endif
#include <errno.h>
/* http://stackoverflow.com/a/1031773 */
static bool validUtf8(const char *string)
@ -159,18 +159,23 @@ Config::Config()
midi.chorus = false;
midi.reverb = false;
SE.sourceCount = 6;
execPath = std::string("");
}
/* Pre-convert value to string since we don't care about the type at this point anymore */
static bool updateConfigFileValue(const char *key, const char *value)
bool Config::updateConfigFileValue(const char *key, const char *value)
{
std::string confPath = execPath + CONF_FILE;
std::string tempPath = execPath + CONF_FILE_TMP;
/* Insert scope so that file handles definitely get cleaned up, even on bitchy windows */
{
/* Open files for reading and writing */
std::ifstream confRead;
confRead.open(CONF_FILE);
confRead.open(confPath.c_str());
std::ofstream confWrite;
confWrite.open(CONF_FILE_TMP, std::ofstream::trunc);
confWrite.open(tempPath.c_str(), std::ofstream::trunc);
if(confWrite && confRead)
if(confWrite && confWrite.is_open() && confRead && confRead.is_open())
{
/* Buffer to store config file lines */
char buf[512];
@ -211,14 +216,10 @@ static bool updateConfigFileValue(const char *key, const char *value)
confRead.close();
confWrite.close();
/* Backup old config file and move the new one into place */
remove(CONF_FILE);
rename(CONF_FILE_TMP, CONF_FILE);
return true;
}
Debug() << CONF_FILE": Failed to update config file.\n";
else
{
Debug() << CONF_FILE": Failed to update config file.";
if(confRead.is_open())
confRead.close();
@ -227,14 +228,30 @@ static bool updateConfigFileValue(const char *key, const char *value)
confWrite.close();
return false;
}
}
/* Backup old config file and move the new one into place */
if(std::remove(confPath.c_str()) == -1){
Debug() << CONF_FILE": removal of the old config failed.";
}
if(std::rename(tempPath.c_str(), confPath.c_str()) != 0){
Debug() << CONF_FILE": replacing the old config failed.";
}
return true;
}
bool Config::store(const char *key, int value)
{
bool update = false;
std::string confPath = execPath + CONF_FILE;
/* Insert scope to make sure file handles get cleaned up */
{
std::ifstream confFileRead;
confFileRead.open(CONF_FILE);
confFileRead.open(confPath.c_str());
int curValue;
if(confFileRead)
if(confFileRead && confFileRead.is_open())
{
/* First read the config file to get the currently stored value
/* and check whether it's in the config file at all */
@ -258,22 +275,27 @@ bool Config::store(const char *key, int value)
if(curValue == value)
{
/* We don't need to store anything */
confFileRead.close();
return true;
}
else
/* needs file update */
update = true;
} catch (...) {}
confFileRead.close();
}
}
if(update)
{
/* Update the relevant config file line */
char num[21];
snprintf(num, 21, "%d", value);
return updateConfigFileValue(key, num);
}
} catch (...) {}
confFileRead.close();
}
/* Open file for writing */
std::ofstream confFileWrite(CONF_FILE, std::ofstream::app);
if(confFileWrite)
if(confFileWrite && confFileWrite.is_open())
{
/* Append new config line */
confFileWrite << '\n' << key << '=' << value;
@ -288,12 +310,17 @@ bool Config::store(const char *key, int value)
bool Config::store(const char *key, bool value)
{
std::ifstream confFileRead;
confFileRead.open(CONF_FILE);
bool curValue;
if(confFileRead)
bool update = false;
std::string confPath = execPath + CONF_FILE;
/* Insert scope to make sure file handles get cleaned up */
{
/* First read the config file to get the currently stored value */
std::ifstream confFileRead;
confFileRead.open(confPath.c_str());
bool curValue;
if(confFileRead && confFileRead.is_open())
{
/* First read the config file to get the currently stored value
/* and check whether it's in the config file at all */
po::options_description podesc;
po::variables_map vm;
podesc.add_options()(key, po::value<bool>()->required());
@ -305,28 +332,34 @@ bool Config::store(const char *key, bool value)
po::notify(vm);
curValue = vm[key].as<bool>();
}
catch(po::multiple_occurrences &e)
catch(po::multiple_occurrences e)
{
/* This is fine we're gonna remove the duplicates when storing */
curValue = !value;
curValue = ~value;
}
if(curValue == value)
{
/* We don't need to store anything */
confFileRead.close();
return true;
}
else
/* needs file update */
update = true;
} catch (...) {}
confFileRead.close();
}
}
if(update)
{
/* Update the relevant config file line */
return updateConfigFileValue(key, value ? "true" : "false");
}
} catch (...) {}
confFileRead.close();
}
/* Open file for writing */
std::ofstream confFileWrite(CONF_FILE, std::ofstream::app);
if(confFileWrite)
if(confFileWrite && confFileWrite.is_open())
{
/* Append new config line */
confFileWrite << '\n' << key << '=' << std::boolalpha << value;

View File

@ -90,6 +90,7 @@ struct Config
/* Internal */
std::string customDataPath;
std::string commonDataPath;
std::string execPath;
Config();
@ -97,6 +98,9 @@ struct Config
bool store(const char* key, int value);
bool store(const char* key, bool value);
void readGameINI();
private:
bool updateConfigFileValue(const char *key, const char *value);
};
#endif // CONFIG_H

View File

@ -205,6 +205,13 @@ int main(int argc, char *argv[])
/* now we load the config */
Config conf;
char *execDir = SDL_GetBasePath();
if (execDir)
{
conf.execPath = std::string(execDir);
SDL_free(execDir);
}
conf.read(argc, argv);
conf.readGameINI();