| 1 | #pragma once
|
|---|
| 2 |
|
|---|
| 3 | #include "settings_logger_impl.hpp"
|
|---|
| 4 | #include <settings/Settings.h>
|
|---|
| 5 | #include <settings/settings_ini.hpp>
|
|---|
| 6 | #ifdef WIN32
|
|---|
| 7 | #include <settings/settings_old.hpp>
|
|---|
| 8 | #include <settings/settings_registry.hpp>
|
|---|
| 9 | #endif
|
|---|
| 10 |
|
|---|
| 11 | namespace settings_manager {
|
|---|
| 12 | class NSCSettingsImpl : public Settings::SettingsHandlerImpl {
|
|---|
| 13 | private:
|
|---|
| 14 | boost::filesystem::wpath boot_;
|
|---|
| 15 | bool old_;
|
|---|
| 16 | public:
|
|---|
| 17 | NSCSettingsImpl() : old_(false) {}
|
|---|
| 18 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 19 | /// Get a string form the boot file.
|
|---|
| 20 | ///
|
|---|
| 21 | /// @param section section to read a value from.
|
|---|
| 22 | /// @param key the key to read.
|
|---|
| 23 | /// @param def a default value.
|
|---|
| 24 | /// @return the value of the key or the default value.
|
|---|
| 25 | ///
|
|---|
| 26 | /// @author mickem
|
|---|
| 27 | std::wstring get_boot_string(std::wstring section, std::wstring key, std::wstring def) {
|
|---|
| 28 | #ifdef WIN32
|
|---|
| 29 | wchar_t* buffer = new wchar_t[1024];
|
|---|
| 30 | GetPrivateProfileString(section.c_str(), key.c_str(), def.c_str(), buffer, 1023, boot_.string().c_str());
|
|---|
| 31 | std::wstring ret = buffer;
|
|---|
| 32 | delete [] buffer;
|
|---|
| 33 | return ret;
|
|---|
| 34 | #else
|
|---|
| 35 | return def;
|
|---|
| 36 | #endif
|
|---|
| 37 | }
|
|---|
| 38 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 39 | /// Boot the settings subsystem from the given file (boot.ini).
|
|---|
| 40 | ///
|
|---|
| 41 | /// @param file the file to use when booting.
|
|---|
| 42 | ///
|
|---|
| 43 | /// @author mickem
|
|---|
| 44 | void boot(std::wstring file = _T("boot.ini")) {
|
|---|
| 45 | boot_ = get_base() / file;
|
|---|
| 46 | #ifdef WIN32
|
|---|
| 47 | std::wstring subsystem = get_boot_string(_T("settings"), _T("type"), _T("old"));
|
|---|
| 48 | #else
|
|---|
| 49 | std::wstring subsystem = get_boot_string(_T("settings"), _T("type"), _T("ini"));
|
|---|
| 50 | #endif
|
|---|
| 51 | get_logger()->debug(__FILEW__, __LINE__, _T("Trying to boot: ") + subsystem + _T(" from base: ") + boot_.string());
|
|---|
| 52 | settings_type type = string_to_type(subsystem);
|
|---|
| 53 | std::wstring context = get_boot_string(_T("settings"), _T("context"), subsystem);
|
|---|
| 54 | Settings::SettingsInterface *impl = create_instance(type, context);
|
|---|
| 55 | if (impl == NULL)
|
|---|
| 56 | throw Settings::SettingsException(_T("Could not create settings instance: ") + subsystem);
|
|---|
| 57 | add_type_impl(type, impl);
|
|---|
| 58 | set_type(type);
|
|---|
| 59 | }
|
|---|
| 60 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 61 | /// Create an instance of a given type.
|
|---|
| 62 | /// Used internally to create instances of various settings types.
|
|---|
| 63 | ///
|
|---|
| 64 | /// @param type the type to create
|
|---|
| 65 | /// @param context the context to use
|
|---|
| 66 | /// @return a new instance of given type.
|
|---|
| 67 | ///
|
|---|
| 68 | /// @author mickem
|
|---|
| 69 | Settings::SettingsInterface* create_instance(settings_type type, std::wstring context) {
|
|---|
| 70 | get_logger()->debug(__FILEW__, __LINE__, _T("Trying to create: ") + SettingsCore::type_to_string(type) + _T(": ") + context);
|
|---|
| 71 | #ifdef WIN32
|
|---|
| 72 | if (type == SettingsCore::old_ini_file) {
|
|---|
| 73 | old_ = true;
|
|---|
| 74 | return new Settings::OLDSettings(this, context);
|
|---|
| 75 | }
|
|---|
| 76 | if (type == SettingsCore::registry)
|
|---|
| 77 | return new Settings::REGSettings(this, context);
|
|---|
| 78 | #endif
|
|---|
| 79 | if (type == SettingsCore::ini_file)
|
|---|
| 80 | return new Settings::INISettings(this, context);
|
|---|
| 81 | throw SettingsException(_T("Undefined settings type: ") + SettingsCore::type_to_string(type));
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | typedef Singleton<NSCSettingsImpl> SettingsHandler;
|
|---|
| 87 |
|
|---|
| 88 | // Alias to make handling "compatible" with old syntax
|
|---|
| 89 | Settings::SettingsInterface* get_settings();
|
|---|
| 90 | Settings::SettingsCore* get_core();
|
|---|
| 91 | void destroy_settings();
|
|---|
| 92 | bool init_settings(boost::filesystem::wpath path);
|
|---|
| 93 | }
|
|---|