2020-12-27 19:43:35 +00:00
|
|
|
#include "StdAfx.h"
|
|
|
|
|
|
|
|
#include "frontends/common2/fileregistry.h"
|
2020-12-28 11:35:58 +00:00
|
|
|
#include "frontends/common2/ptreeregistry.h"
|
2020-12-27 19:43:35 +00:00
|
|
|
#include "frontends/common2/programoptions.h"
|
2017-06-08 21:58:26 +01:00
|
|
|
|
|
|
|
#include "Log.h"
|
2020-07-05 13:20:03 +01:00
|
|
|
#include "linux/windows/files.h"
|
2020-12-23 19:23:13 +00:00
|
|
|
#include "frontends/qt/applicationname.h"
|
2017-06-08 21:58:26 +01:00
|
|
|
|
|
|
|
#include <boost/property_tree/ini_parser.hpp>
|
|
|
|
|
2020-12-03 15:57:12 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2020-10-11 08:26:42 +01:00
|
|
|
namespace
|
2017-06-08 21:58:26 +01:00
|
|
|
{
|
2020-10-11 09:49:50 +01:00
|
|
|
|
2020-12-04 19:58:49 +00:00
|
|
|
void parseOption(const std::string & s, std::string & path, std::string & value)
|
|
|
|
{
|
|
|
|
const size_t pos = s.find('=');
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Invalid option format: " + s + ", expected: section.key=value");
|
|
|
|
}
|
|
|
|
path = s.substr(0, pos);
|
|
|
|
std::replace(path.begin(), path.end(), '_', ' ');
|
|
|
|
value = s.substr(pos + 1);
|
|
|
|
std::replace(value.begin(), value.end(), '_', ' ');
|
|
|
|
}
|
|
|
|
|
2021-02-07 16:10:54 +00:00
|
|
|
std::string getHomeDir()
|
|
|
|
{
|
|
|
|
const char* homeDir = getenv("HOME");
|
|
|
|
if (!homeDir)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("${HOME} not set, cannot locate configuration file");
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string(homeDir);
|
|
|
|
}
|
|
|
|
|
2021-02-25 16:04:50 +00:00
|
|
|
class Configuration : public common2::PTreeRegistry
|
2020-10-11 08:26:42 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Configuration(const std::string & filename, const bool saveOnExit);
|
|
|
|
~Configuration();
|
2017-06-08 21:58:26 +01:00
|
|
|
|
2020-12-04 19:58:49 +00:00
|
|
|
void addExtraOptions(const std::vector<std::string> & options);
|
|
|
|
|
2020-10-11 08:26:42 +01:00
|
|
|
private:
|
|
|
|
const std::string myFilename;
|
2020-10-11 09:49:50 +01:00
|
|
|
const bool mySaveOnExit;
|
2020-10-11 08:26:42 +01:00
|
|
|
};
|
2017-06-08 21:58:26 +01:00
|
|
|
|
2020-10-11 08:26:42 +01:00
|
|
|
Configuration::Configuration(const std::string & filename, const bool saveOnExit) : myFilename(filename), mySaveOnExit(saveOnExit)
|
2019-11-03 10:21:02 +00:00
|
|
|
{
|
2020-10-11 08:26:42 +01:00
|
|
|
if (GetFileAttributes(myFilename.c_str()) != INVALID_FILE_ATTRIBUTES)
|
|
|
|
{
|
|
|
|
boost::property_tree::ini_parser::read_ini(myFilename, myINI);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogFileOutput("Registry: configuration file '%s' not found\n", filename.c_str());
|
|
|
|
}
|
2019-11-03 10:21:02 +00:00
|
|
|
}
|
2020-10-11 08:26:42 +01:00
|
|
|
|
|
|
|
Configuration::~Configuration()
|
2019-11-03 10:21:02 +00:00
|
|
|
{
|
2020-10-11 08:26:42 +01:00
|
|
|
if (mySaveOnExit)
|
|
|
|
{
|
|
|
|
boost::property_tree::ini_parser::write_ini(myFilename, myINI);
|
|
|
|
}
|
2019-11-03 10:21:02 +00:00
|
|
|
}
|
2017-06-08 21:58:26 +01:00
|
|
|
|
2020-12-04 19:58:49 +00:00
|
|
|
void Configuration::addExtraOptions(const std::vector<std::string> & options)
|
|
|
|
{
|
|
|
|
for (const std::string & option : options)
|
|
|
|
{
|
|
|
|
std::string path, value;
|
|
|
|
parseOption(option, path, value);
|
|
|
|
myINI.put(path, value);
|
|
|
|
}
|
|
|
|
}
|
2020-12-28 11:35:58 +00:00
|
|
|
|
2017-06-08 21:58:26 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 16:04:50 +00:00
|
|
|
namespace common2
|
2017-06-08 21:58:26 +01:00
|
|
|
{
|
2021-02-25 16:04:50 +00:00
|
|
|
|
|
|
|
std::string GetConfigFile(const std::string & filename)
|
2021-02-07 16:10:54 +00:00
|
|
|
{
|
2021-02-25 16:04:50 +00:00
|
|
|
const std::string dir = getHomeDir() + "/.applewin";
|
|
|
|
const int status = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
|
|
|
if (!status || (errno == EEXIST))
|
|
|
|
{
|
|
|
|
return dir + "/" + filename;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char * s = strerror(errno);
|
|
|
|
LogFileOutput("No registry. Cannot create %s in %s: %s\n", filename.c_str(), dir.c_str(), s);
|
|
|
|
return std::string();
|
|
|
|
}
|
2021-02-07 16:10:54 +00:00
|
|
|
}
|
2021-02-25 16:04:50 +00:00
|
|
|
|
|
|
|
void InitializeFileRegistry(const EmulatorOptions & options)
|
2020-12-03 15:57:12 +00:00
|
|
|
{
|
2021-02-25 16:04:50 +00:00
|
|
|
const std::string homeDir = getHomeDir();
|
2021-02-07 16:10:54 +00:00
|
|
|
|
2021-02-25 16:04:50 +00:00
|
|
|
std::string filename;
|
|
|
|
bool saveOnExit;
|
2020-12-03 15:57:12 +00:00
|
|
|
|
2021-02-25 16:04:50 +00:00
|
|
|
if (options.useQtIni)
|
|
|
|
{
|
|
|
|
filename = homeDir + "/.config/" + ORGANIZATION_NAME + "/" + APPLICATION_NAME + ".conf";
|
|
|
|
saveOnExit = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filename = GetConfigFile("applewin.conf");
|
|
|
|
saveOnExit = !filename.empty() && options.saveConfigurationOnExit;
|
|
|
|
}
|
2020-12-13 09:01:02 +00:00
|
|
|
|
2021-02-25 16:04:50 +00:00
|
|
|
std::shared_ptr<Configuration> config(new Configuration(filename, saveOnExit));
|
|
|
|
config->addExtraOptions(options.registryOptions);
|
2020-10-11 09:49:50 +01:00
|
|
|
|
2021-02-25 16:04:50 +00:00
|
|
|
Registry::instance = config;
|
|
|
|
}
|
2017-06-08 21:58:26 +01:00
|
|
|
|
|
|
|
}
|