1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
#pragma once
#include <KeyFileHelper.h>
#include <memory>
#include "FARString.hpp"
class ConfigSection
{
protected:
std::string _section;
virtual void OnSectionSelected() {}
public:
virtual ~ConfigSection() {}
void SelectSection(const std::string §ion);
void SelectSection(const wchar_t *section);
void FN_PRINTF_ARGS(2) SelectSectionFmt(const char *format, ...);
};
class ConfigReader : public ConfigSection
{
std::map<std::string, std::unique_ptr<KeyFileReadHelper> > _ini2kfh;
std::unique_ptr<KeyFileValues> _empty_values;
KeyFileReadHelper *_selected_kfh = nullptr;
const KeyFileValues *_selected_section_values = nullptr;
bool _has_section;
virtual void OnSectionSelected();
public:
ConfigReader();
ConfigReader(const std::string §ion);
static struct stat SavedSectionStat(const std::string §ion);
inline const struct stat &LoadedSectionStat() const { return _selected_kfh->LoadedFileStat(); }
std::vector<std::string> EnumKeys();
std::vector<std::string> EnumSectionsAt();
inline bool HasSection() const { return _has_section; };
bool HasKey(const std::string &name) const;
FARString GetString(const std::string &name, const wchar_t *def = L"") const;
bool GetString(FARString &out, const std::string &name, const wchar_t *def) const;
bool GetString(std::string &out, const std::string &name, const char *def) const;
int GetInt(const std::string &name, int def = 0) const;
unsigned int GetUInt(const std::string &name, unsigned int def = 0) const;
unsigned long long GetULL(const std::string &name, unsigned long long def = 0) const;
size_t GetBytes(unsigned char *out, size_t len, const std::string &name, const unsigned char *def = nullptr) const;
bool GetBytes(std::vector<unsigned char> &out, const std::string &name) const;
template <class POD> void GetPOD(const std::string &name, POD &pod)
{ GetBytes((unsigned char *)&pod, sizeof(pod), name); }
};
class ConfigWriter : public ConfigSection
{
std::map<std::string, std::unique_ptr<KeyFileHelper> > _ini2kfh;
KeyFileHelper *_selected_kfh = nullptr;
size_t _bytes_space_interval = 0;
virtual void OnSectionSelected();
std::vector<std::string> EnumIndexedSections(const char *indexed_prefix);
public:
ConfigWriter();
ConfigWriter(const std::string &preselect_section);
bool Save();
void RemoveSection();
void DefragIndexedSections(const char *indexed_prefix);
void MoveIndexedSection(const char *indexed_prefix, unsigned int old_index, unsigned int new_index);
void ReserveIndexedSection(const char *indexed_prefix, unsigned int index);
void SetString(const std::string &name, const wchar_t *value);
void SetString(const std::string &name, const std::string &value);
void SetInt(const std::string &name, int value);
void SetUInt(const std::string &name, unsigned int value);
void SetULL(const std::string &name, unsigned long long value);
void SetBytes(const std::string &name, const unsigned char *buf, size_t len);
template <class POD> void SetPOD(const std::string &name, const POD &pod)
{ SetBytes(name, (const unsigned char *)&pod, sizeof(pod)); }
void RemoveKey(const std::string &name);
};
void CheckForConfigUpgrade();
class ConfigReaderScope
{
std::unique_ptr<ConfigReader> &_cfg_reader;
public:
ConfigReaderScope(std::unique_ptr<ConfigReader> &cfg_reader)
: _cfg_reader(cfg_reader)
{
_cfg_reader.reset(new ConfigReader);
}
~ConfigReaderScope()
{
_cfg_reader.reset();
}
static void Update(std::unique_ptr<ConfigReader> &cfg_reader)
{
if (cfg_reader) {
cfg_reader.reset();
cfg_reader.reset(new ConfigReader);
}
}
};
|