Tightened up file handling code for config stores so that Windows is happy.
This commit is contained in:
parent
464657e20e
commit
9d8aad7ffe
|
@ -38,8 +38,8 @@ extern "C" {
|
||||||
#include <libguess.h>
|
#include <libguess.h>
|
||||||
}
|
}
|
||||||
#include <iconv.h>
|
#include <iconv.h>
|
||||||
#include <errno.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
/* http://stackoverflow.com/a/1031773 */
|
/* http://stackoverflow.com/a/1031773 */
|
||||||
static bool validUtf8(const char *string)
|
static bool validUtf8(const char *string)
|
||||||
|
@ -159,18 +159,23 @@ Config::Config()
|
||||||
midi.chorus = false;
|
midi.chorus = false;
|
||||||
midi.reverb = false;
|
midi.reverb = false;
|
||||||
SE.sourceCount = 6;
|
SE.sourceCount = 6;
|
||||||
|
execPath = std::string("");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pre-convert value to string since we don't care about the type at this point anymore */
|
/* 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 */
|
/* Open files for reading and writing */
|
||||||
std::ifstream confRead;
|
std::ifstream confRead;
|
||||||
confRead.open(CONF_FILE);
|
confRead.open(confPath.c_str());
|
||||||
std::ofstream confWrite;
|
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 */
|
/* Buffer to store config file lines */
|
||||||
char buf[512];
|
char buf[512];
|
||||||
|
@ -211,14 +216,10 @@ static bool updateConfigFileValue(const char *key, const char *value)
|
||||||
|
|
||||||
confRead.close();
|
confRead.close();
|
||||||
confWrite.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())
|
if(confRead.is_open())
|
||||||
confRead.close();
|
confRead.close();
|
||||||
|
@ -228,13 +229,29 @@ static bool updateConfigFileValue(const char *key, const char *value)
|
||||||
|
|
||||||
return false;
|
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 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;
|
std::ifstream confFileRead;
|
||||||
confFileRead.open(CONF_FILE);
|
confFileRead.open(confPath.c_str());
|
||||||
int curValue;
|
int curValue;
|
||||||
if(confFileRead)
|
if(confFileRead && confFileRead.is_open())
|
||||||
{
|
{
|
||||||
/* First read the config file to get the currently stored value
|
/* First read the config file to get the currently stored value
|
||||||
/* and check whether it's in the config file at all */
|
/* 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)
|
if(curValue == value)
|
||||||
{
|
{
|
||||||
/* We don't need to store anything */
|
/* We don't need to store anything */
|
||||||
|
confFileRead.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
/* needs file update */
|
||||||
|
update = true;
|
||||||
|
} catch (...) {}
|
||||||
|
confFileRead.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(update)
|
||||||
{
|
{
|
||||||
/* Update the relevant config file line */
|
/* Update the relevant config file line */
|
||||||
char num[21];
|
char num[21];
|
||||||
snprintf(num, 21, "%d", value);
|
snprintf(num, 21, "%d", value);
|
||||||
return updateConfigFileValue(key, num);
|
return updateConfigFileValue(key, num);
|
||||||
}
|
}
|
||||||
} catch (...) {}
|
|
||||||
confFileRead.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Open file for writing */
|
/* Open file for writing */
|
||||||
std::ofstream confFileWrite(CONF_FILE, std::ofstream::app);
|
std::ofstream confFileWrite(CONF_FILE, std::ofstream::app);
|
||||||
if(confFileWrite)
|
if(confFileWrite && confFileWrite.is_open())
|
||||||
{
|
{
|
||||||
/* Append new config line */
|
/* Append new config line */
|
||||||
confFileWrite << '\n' << key << '=' << value;
|
confFileWrite << '\n' << key << '=' << value;
|
||||||
|
@ -288,12 +310,17 @@ bool Config::store(const char *key, int value)
|
||||||
|
|
||||||
bool Config::store(const char *key, bool value)
|
bool Config::store(const char *key, bool value)
|
||||||
{
|
{
|
||||||
std::ifstream confFileRead;
|
bool update = false;
|
||||||
confFileRead.open(CONF_FILE);
|
std::string confPath = execPath + CONF_FILE;
|
||||||
bool curValue;
|
/* Insert scope to make sure file handles get cleaned up */
|
||||||
if(confFileRead)
|
|
||||||
{
|
{
|
||||||
/* 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::options_description podesc;
|
||||||
po::variables_map vm;
|
po::variables_map vm;
|
||||||
podesc.add_options()(key, po::value<bool>()->required());
|
podesc.add_options()(key, po::value<bool>()->required());
|
||||||
|
@ -305,28 +332,34 @@ bool Config::store(const char *key, bool value)
|
||||||
po::notify(vm);
|
po::notify(vm);
|
||||||
curValue = vm[key].as<bool>();
|
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 */
|
/* This is fine we're gonna remove the duplicates when storing */
|
||||||
curValue = !value;
|
curValue = ~value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(curValue == value)
|
if(curValue == value)
|
||||||
{
|
{
|
||||||
/* We don't need to store anything */
|
/* We don't need to store anything */
|
||||||
|
confFileRead.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
/* needs file update */
|
||||||
|
update = true;
|
||||||
|
} catch (...) {}
|
||||||
|
confFileRead.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(update)
|
||||||
{
|
{
|
||||||
/* Update the relevant config file line */
|
/* Update the relevant config file line */
|
||||||
return updateConfigFileValue(key, value ? "true" : "false");
|
return updateConfigFileValue(key, value ? "true" : "false");
|
||||||
}
|
}
|
||||||
} catch (...) {}
|
|
||||||
confFileRead.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Open file for writing */
|
/* Open file for writing */
|
||||||
std::ofstream confFileWrite(CONF_FILE, std::ofstream::app);
|
std::ofstream confFileWrite(CONF_FILE, std::ofstream::app);
|
||||||
if(confFileWrite)
|
if(confFileWrite && confFileWrite.is_open())
|
||||||
{
|
{
|
||||||
/* Append new config line */
|
/* Append new config line */
|
||||||
confFileWrite << '\n' << key << '=' << std::boolalpha << value;
|
confFileWrite << '\n' << key << '=' << std::boolalpha << value;
|
||||||
|
|
|
@ -90,6 +90,7 @@ struct Config
|
||||||
/* Internal */
|
/* Internal */
|
||||||
std::string customDataPath;
|
std::string customDataPath;
|
||||||
std::string commonDataPath;
|
std::string commonDataPath;
|
||||||
|
std::string execPath;
|
||||||
|
|
||||||
Config();
|
Config();
|
||||||
|
|
||||||
|
@ -97,6 +98,9 @@ struct Config
|
||||||
bool store(const char* key, int value);
|
bool store(const char* key, int value);
|
||||||
bool store(const char* key, bool value);
|
bool store(const char* key, bool value);
|
||||||
void readGameINI();
|
void readGameINI();
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool updateConfigFileValue(const char *key, const char *value);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CONFIG_H
|
#endif // CONFIG_H
|
||||||
|
|
|
@ -205,6 +205,13 @@ int main(int argc, char *argv[])
|
||||||
/* now we load the config */
|
/* now we load the config */
|
||||||
Config conf;
|
Config conf;
|
||||||
|
|
||||||
|
char *execDir = SDL_GetBasePath();
|
||||||
|
if (execDir)
|
||||||
|
{
|
||||||
|
conf.execPath = std::string(execDir);
|
||||||
|
SDL_free(execDir);
|
||||||
|
}
|
||||||
|
|
||||||
conf.read(argc, argv);
|
conf.read(argc, argv);
|
||||||
conf.readGameINI();
|
conf.readGameINI();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue