Share some more registry-related code.

Signed-off-by: Andrea Odetti <mariofutire@gmail.com>
This commit is contained in:
Andrea Odetti 2020-12-28 11:35:58 +00:00
parent 84b6afe758
commit 2c9e4561df
4 changed files with 104 additions and 92 deletions

View file

@ -3,6 +3,7 @@ include(GNUInstallDirs)
add_library(common2 STATIC
resources.cpp
fileregistry.cpp
ptreeregistry.cpp
programoptions.cpp
utils.cpp
timer.cpp

View file

@ -1,15 +1,14 @@
#include "StdAfx.h"
#include "frontends/common2/fileregistry.h"
#include "frontends/common2/ptreeregistry.h"
#include "frontends/common2/programoptions.h"
#include "Log.h"
#include "linux/registry.h"
#include "linux/windows/files.h"
#include "frontends/qt/applicationname.h"
#include <boost/property_tree/ini_parser.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <sys/stat.h>
@ -29,63 +28,19 @@ namespace
std::replace(value.begin(), value.end(), '_', ' ');
}
struct KeyEncodedLess
{
static std::string decodeKey(const std::string & key)
{
std::string result = key;
// quick implementation, just to make it work.
// is there a library function available somewhere?
boost::algorithm::replace_all(result, "%20", " ");
return result;
}
bool operator()( const std::string & lhs, const std::string & rhs ) const
{
const std::string key1 = decodeKey(lhs);
const std::string key2 = decodeKey(rhs);
return key1 < key2;
}
};
class Configuration : public Registry
class Configuration : public PTreeRegistry
{
public:
typedef boost::property_tree::basic_ptree<std::string, std::string, KeyEncodedLess> ini_t;
Configuration(const std::string & filename, const bool saveOnExit);
~Configuration();
static std::shared_ptr<Configuration> instance;
virtual std::string getString(const std::string & section, const std::string & key) const;
virtual DWORD getDWord(const std::string & section, const std::string & key) const;
virtual bool getBool(const std::string & section, const std::string & key) const;
virtual void putString(const std::string & section, const std::string & key, const std::string & value);
virtual void putDWord(const std::string & section, const std::string & key, const DWORD value);
template<typename T>
T getValue(const std::string & section, const std::string & key) const;
template<typename T>
void putValue(const std::string & section, const std::string & key, const T & value);
const ini_t & getProperties() const;
void addExtraOptions(const std::vector<std::string> & options);
private:
const std::string myFilename;
const bool mySaveOnExit;
static std::string decodeKey(const std::string & key);
ini_t myINI;
};
std::shared_ptr<Configuration> Configuration::instance;
Configuration::Configuration(const std::string & filename, const bool saveOnExit) : myFilename(filename), mySaveOnExit(saveOnExit)
{
if (GetFileAttributes(myFilename.c_str()) != INVALID_FILE_ATTRIBUTES)
@ -106,51 +61,6 @@ namespace
}
}
const Configuration::ini_t & Configuration::getProperties() const
{
return myINI;
}
std::string Configuration::getString(const std::string & section, const std::string & key) const
{
return getValue<std::string>(section, key);
}
DWORD Configuration::getDWord(const std::string & section, const std::string & key) const
{
return getValue<DWORD>(section, key);
}
bool Configuration::getBool(const std::string & section, const std::string & key) const
{
return getValue<bool>(section, key);
}
void Configuration::putString(const std::string & section, const std::string & key, const std::string & value)
{
putValue(section, key, value);
}
void Configuration::putDWord(const std::string & section, const std::string & key, const DWORD value)
{
putValue(section, key, value);
}
template <typename T>
T Configuration::getValue(const std::string & section, const std::string & key) const
{
const std::string path = section + "." + key;
const T value = myINI.get<T>(path);
return value;
}
template <typename T>
void Configuration::putValue(const std::string & section, const std::string & key, const T & value)
{
const std::string path = section + "." + key;
myINI.put(path, value);
}
void Configuration::addExtraOptions(const std::vector<std::string> & options)
{
for (const std::string & option : options)
@ -160,6 +70,7 @@ namespace
myINI.put(path, value);
}
}
}
void InitializeFileRegistry(const EmulatorOptions & options)

View file

@ -0,0 +1,64 @@
#include "frontends/common2/ptreeregistry.h"
#include <boost/algorithm/string/replace.hpp>
namespace
{
std::string decodeKey(const std::string & key)
{
std::string result = key;
// quick implementation, just to make it work.
// is there a library function available somewhere?
boost::algorithm::replace_all(result, "%20", " ");
return result;
}
}
bool PTreeRegistry::KeyQtEncodedLess::operator()(const std::string & lhs, const std::string & rhs) const
{
const std::string key1 = decodeKey(lhs);
const std::string key2 = decodeKey(rhs);
return key1 < key2;
}
std::string PTreeRegistry::getString(const std::string & section, const std::string & key) const
{
return getValue<std::string>(section, key);
}
DWORD PTreeRegistry::getDWord(const std::string & section, const std::string & key) const
{
return getValue<DWORD>(section, key);
}
bool PTreeRegistry::getBool(const std::string & section, const std::string & key) const
{
return getValue<bool>(section, key);
}
void PTreeRegistry::putString(const std::string & section, const std::string & key, const std::string & value)
{
putValue(section, key, value);
}
void PTreeRegistry::putDWord(const std::string & section, const std::string & key, const DWORD value)
{
putValue(section, key, value);
}
template <typename T>
T PTreeRegistry::getValue(const std::string & section, const std::string & key) const
{
const std::string path = section + "." + key;
const T value = myINI.get<T>(path);
return value;
}
template <typename T>
void PTreeRegistry::putValue(const std::string & section, const std::string & key, const T & value)
{
const std::string path = section + "." + key;
myINI.put(path, value);
}

View file

@ -0,0 +1,36 @@
#pragma once
#include "linux/registry.h"
#include <boost/property_tree/ptree.hpp>
#include <string>
class PTreeRegistry : public Registry
{
public:
struct KeyQtEncodedLess
{
// this function is used to make the Qt registry compatible with sa2 and napple
// it is here, in the base class PTreeRegistry simply because it makes things easier
// KeyQtEncodedLess goes in the typedef init_t below
bool operator()(const std::string & lhs, const std::string & rhs) const;
};
typedef boost::property_tree::basic_ptree<std::string, std::string, KeyQtEncodedLess> ini_t;
virtual std::string getString(const std::string & section, const std::string & key) const;
virtual DWORD getDWord(const std::string & section, const std::string & key) const;
virtual bool getBool(const std::string & section, const std::string & key) const;
virtual void putString(const std::string & section, const std::string & key, const std::string & value);
virtual void putDWord(const std::string & section, const std::string & key, const DWORD value);
template<typename T>
T getValue(const std::string & section, const std::string & key) const;
template<typename T>
void putValue(const std::string & section, const std::string & key, const T & value);
protected:
ini_t myINI;
};