forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigParser.h
165 lines (124 loc) · 6.74 KB
/
ConfigParser.h
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (C) 2004 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
#ifndef __CONFIGPARSER_H__
#define __CONFIGPARSER_H__
#pragma warning(disable: 4503)
#include <windows.h>
#include <string>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <cstdint>
#include <d2d1.h>
class Rainmeter;
class Skin;
class Section;
class Measure;
class Meter;
enum class PairedPunctuation : BYTE
{
SingleQuote,
DoubleQuote,
BothQuotes,
Parentheses,
Brackets,
Braces,
Guillemet
};
class ConfigParser
{
public:
enum class VariableType : BYTE
{ // Old Style: New Style:
Section, // [MeasureName], [Meter:X], etc. [&MeasureName], [&Meter:X], etc.
Variable, // #Variable# [#Variable]
Mouse, // $MouseX$, $MouseX:%$, etc. [$MouseX], [$MouseX:%], etc.
CharacterReference // Not available. [\8364], [\x20AC], [\X20AC], etc.
};
ConfigParser();
~ConfigParser();
ConfigParser(const ConfigParser& other) = delete;
ConfigParser& operator=(ConfigParser other) = delete;
void Initialize(const std::wstring& filename, Skin* skin = nullptr, LPCTSTR skinSection = nullptr, const std::wstring* resourcePath = nullptr);
void AddMeasure(Measure* pMeasure);
Measure* GetMeasure(const std::wstring& name);
const std::wstring* GetVariable(const std::wstring& strVariable);
const std::wstring* GetVariableOriginalName(const std::wstring& strVariable);
void SetVariable(std::wstring strVariable, const std::wstring& strValue);
void SetBuiltInVariable(const std::wstring& strVariable, const std::wstring& strValue);
const std::unordered_map<std::wstring, std::wstring>& GetVariables() { return m_Variables; }
const std::wstring& GetValue(const std::wstring& strSection, const std::wstring& strKey, const std::wstring& strDefault);
void SetValue(const std::wstring& strSection, const std::wstring& strKey, const std::wstring& strValue);
void DeleteValue(const std::wstring& strSection, const std::wstring& strKey);
void SetStyleTemplate(const std::wstring& strStyle) { static const std::wstring delim(1, L'|'); Tokenize(strStyle, delim).swap(m_StyleTemplate); }
void ClearStyleTemplate() { m_StyleTemplate.clear(); }
bool GetLastReplaced() { return m_LastReplaced; }
bool GetLastDefaultUsed() { return m_LastDefaultUsed; }
bool GetLastKeyDefined() { return !m_LastDefaultUsed; }
bool GetLastValueDefined() { return m_LastValueDefined; }
void ResetMonitorVariables(Skin* skin = nullptr);
const std::wstring& ReadString(LPCTSTR section, LPCTSTR key, LPCTSTR defValue, bool bReplaceMeasures = true);
bool IsKeyDefined(LPCTSTR section, LPCTSTR key);
bool IsValueDefined(LPCTSTR section, LPCTSTR key);
bool ReadBool(LPCTSTR section, LPCTSTR key, bool defValue) { return ReadInt(section, key, (int)defValue) != 0; }
int ReadInt(LPCTSTR section, LPCTSTR key, int defValue);
uint32_t ReadUInt(LPCTSTR section, LPCTSTR key, uint32_t defValue);
uint64_t ReadUInt64(LPCTSTR section, LPCTSTR key, uint64_t defValue);
double ReadFloat(LPCTSTR section, LPCTSTR key, double defValue);
D2D1_COLOR_F ReadColor(LPCTSTR section, LPCTSTR key, const D2D1_COLOR_F& defValue);
D2D1_RECT_F ReadRect(LPCTSTR section, LPCTSTR key, const D2D1_RECT_F& defValue);
RECT ReadRECT(LPCTSTR section, LPCTSTR key, const RECT& defValue);
std::vector<FLOAT> ReadFloats(LPCTSTR section, LPCTSTR key);
bool ParseFormula(const std::wstring& formula, double* resultValue);
std::wstring ParseFormulaWithModifiers(const std::wstring& formula);
const std::list<std::wstring>& GetSections() { return m_Sections; }
bool ReplaceVariables(std::wstring& result, bool isNewStyle = false);
bool ReplaceMeasures(std::wstring& result);
bool ParseVariables(std::wstring& result, const VariableType type, Meter* meter = nullptr);
bool ContainsNewStyleVariable(const std::wstring& str);
std::wstring GetMouseVariable(const std::wstring& variable, Meter* meter);
static std::vector<std::wstring> Tokenize(const std::wstring& str, const std::wstring& delimiters);
static std::vector<std::wstring> Tokenize2(const std::wstring& str, const WCHAR delimiter, const PairedPunctuation punct);
static double ParseDouble(LPCTSTR str, double defValue);
static int ParseInt(LPCTSTR str, int defValue);
static uint32_t ParseUInt(LPCTSTR str, uint32_t defValue);
static uint64_t ParseUInt64(LPCTSTR str, uint64_t defValue);
static D2D1_COLOR_F ParseColor(LPCTSTR str);
static D2D1_RECT_F ParseRect(LPCTSTR str);
static RECT ParseRECT(LPCTSTR str);
static void ClearMultiMonitorVariables() { c_MonitorVariables.clear(); }
static void UpdateWorkareaVariables() { SetMultiMonitorVariables(false); }
static bool IsVariableKey(const WCHAR ch) { for (auto& k : c_VariableMap) { if (k.second == ch) return true; } return false; }
private:
void SetBuiltInVariables(const std::wstring& filename, const std::wstring* resourcePath, Skin* skin);
void ReadVariables();
void ReadIniFile(const std::wstring& iniFile, LPCTSTR skinSection = nullptr, int depth = 0);
void SetAutoSelectedMonitorVariables(Skin* skin);
bool GetSectionVariable(std::wstring& strVariable, std::wstring& strValue, void* logEntry = nullptr);
static void SetMultiMonitorVariables(bool reset);
static std::wstring StrToUpper(const std::wstring& str) { std::wstring strTmp(str); StrToUpperC(strTmp); return strTmp; }
static std::wstring StrToUpper(const WCHAR* str) { std::wstring strTmp(str); StrToUpperC(strTmp); return strTmp; }
static std::wstring& StrToUpperC(std::wstring& str) { _wcsupr(&str[0]); return str; }
std::unordered_map<std::wstring, Measure*> m_Measures;
std::vector<std::wstring> m_StyleTemplate;
bool m_LastReplaced;
bool m_LastDefaultUsed;
bool m_LastValueDefined;
std::wstring* m_CurrentSection;
std::list<std::wstring> m_Sections; // Ordered section
std::unordered_map<std::wstring, std::wstring> m_Values;
std::unordered_set<std::wstring> m_FoundSections;
std::list<std::wstring> m_ListVariables;
std::list<std::wstring>::const_iterator m_SectionInsertPos;
std::unordered_map<std::wstring, std::wstring> m_BuiltInVariables;
std::unordered_map<std::wstring, std::wstring> m_Variables;
std::unordered_map<std::wstring, std::wstring> m_OriginalVariableNames;
Skin* m_Skin;
static std::unordered_map<std::wstring, std::wstring> c_MonitorVariables;
static std::unordered_map<VariableType, WCHAR> c_VariableMap;
};
#endif