source: nscp/service/settings_manager_impl.h @ 818b54e

0.4.00.4.10.4.2
Last change on this file since 818b54e was 818b54e, checked in by Michael Medin <michael@…>, 4 years ago

First version which builds on Linux!!! (though 99% of all features are still disabled).
All it does is build and tell you there is no settings subsystem (which there isn't as simpleini does not support wchar_t on unix :(

  • Property mode set to 100644
File size: 3.2 KB
Line 
1#pragma once
2
3#include "settings_logger_impl.hpp"
4#include <settings/Settings.h>
5#ifdef WIN32
6#include <settings/settings_ini.hpp>
7#include <settings/settings_old.hpp>
8#include <settings/settings_registry.hpp>
9#endif
10
11namespace settings_manager {
12        class NSCSettingsImpl : public Settings::SettingsHandlerImpl {
13        private:
14                std::wstring 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_.c_str());
31                        std::wstring ret = buffer;
32                        delete [] buffer;
33                        return ret;
34#else
35                        return _T("ini");
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() + _T("\\") + file;
46                        std::wstring subsystem = get_boot_string(_T("settings"), _T("type"), _T("old"));
47                        get_logger()->debug(__FILEW__, __LINE__, _T("Trying to boot: ") + subsystem + _T(" from base: ") + boot_);
48                        settings_type type = string_to_type(subsystem);
49                        std::wstring context = get_boot_string(_T("settings"), _T("context"), subsystem);
50                        Settings::SettingsInterface *impl = create_instance(type, context);
51                        if (impl == NULL)
52                                throw Settings::SettingsException(_T("Could not create settings instance: ") + subsystem);
53                        add_type_impl(type, impl);
54                        set_type(type);
55                }
56                //////////////////////////////////////////////////////////////////////////
57                /// Create an instance of a given type.
58                /// Used internally to create instances of various settings types.
59                ///
60                /// @param type the type to create
61                /// @param context the context to use
62                /// @return a new instance of given type.
63                ///
64                /// @author mickem
65                Settings::SettingsInterface* create_instance(settings_type type, std::wstring context) {
66                        get_logger()->debug(__FILEW__, __LINE__, _T("Trying to create: ") + SettingsCore::type_to_string(type) + _T(": ") + context);
67#ifdef WIN32
68                        if (type == SettingsCore::old_ini_file) {
69                                old_ = true;
70                                return new Settings::OLDSettings(this, context);
71                        }
72                        if (type == SettingsCore::registry)
73                                return new Settings::REGSettings(this, context);
74                        if (type == SettingsCore::ini_file)
75                                return new Settings::INISettings(this, context);
76#endif
77                        throw SettingsException(_T("Undefined settings type: ") + SettingsCore::type_to_string(type));
78                }
79
80        };
81
82        typedef Singleton<NSCSettingsImpl> SettingsHandler;
83
84        // Alias to make handling "compatible" with old syntax
85        Settings::SettingsInterface* get_settings();
86        Settings::SettingsCore* get_core();
87        void destroy_settings();
88        bool init_settings(std::wstring path);
89}
Note: See TracBrowser for help on using the repository browser.