forked from CobaltFusion/DebugViewPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilter.cpp
More file actions
182 lines (162 loc) · 5.4 KB
/
Copy pathFilter.cpp
File metadata and controls
182 lines (162 loc) · 5.4 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// (C) Copyright Gert-Jan de Vos and Jan Wilmans 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/algorithm/string/case_conv.hpp>
#include "Win32/Registry.h"
#include "CobaltFusion/stringbuilder.h"
#include "DebugViewppLib/Colors.h"
#include "DebugViewppLib/Filter.h"
namespace fusion {
namespace debugviewpp {
std::string MatchKey(const std::smatch& match, MatchType::type matchType)
{
if (matchType == MatchType::RegexGroups && match.size() > 1)
{
std::string key;
auto it = match.begin();
while (++it != match.end())
{
if (!key.empty())
{
key.append(1, '\0');
}
key += boost::to_lower_copy(it->str());
}
return key;
}
return boost::to_lower_copy(match.str());
}
Filter::Filter() :
matchType(MatchType::Simple),
filterType(FilterType::Include),
bgColor(RGB(255, 255, 255)),
fgColor(RGB(0, 0, 0)),
enable(true),
matched(false)
{
}
std::regex_constants::syntax_option_type MakeSot(MatchType::type matchType)
{
if (matchType == MatchType::RegexCase)
{
return std::regex_constants::optimize;
}
return std::regex_constants::icase | std::regex_constants::optimize;
}
Filter::Filter(const std::string& text, MatchType::type matchType, FilterType::type filterType, COLORREF bgColor, COLORREF fgColor, bool enable, bool matched) :
text(text),
re(MakePattern(matchType, text), MakeSot(matchType)),
matchType(matchType),
filterType(filterType),
bgColor(bgColor),
fgColor(fgColor),
enable(enable),
matched(matched)
{
}
void SaveFilterSettings(const std::vector<Filter>& filters, CRegKey& reg)
{
int i = 0;
for (auto& filter : filters)
{
CRegKey regFilter;
regFilter.Create(reg, WStr(wstringbuilder() << L"Filter" << i));
regFilter.SetStringValue(L"", WStr(filter.text));
regFilter.SetDWORDValue(L"MatchType", MatchTypeToInt(filter.matchType));
regFilter.SetDWORDValue(L"FilterType", FilterTypeToInt(filter.filterType));
regFilter.SetDWORDValue(L"Type", FilterTypeToInt(filter.filterType));
regFilter.SetDWORDValue(L"BgColor", filter.bgColor);
regFilter.SetDWORDValue(L"FgColor", filter.fgColor);
regFilter.SetDWORDValue(L"Enable", static_cast<DWORD>(filter.enable));
++i;
}
}
// Temporary backward compatibilty for loading FilterType::MatchColor:
Filter MakeFilter(const std::string& text, MatchType::type matchType, FilterType::type filterType, COLORREF bgColor, COLORREF fgColor, bool enable, bool matched)
{
if (filterType == FilterType::MatchColor)
{
filterType = FilterType::Token;
bgColor = Colors::Auto;
}
return Filter(text, matchType, filterType, bgColor, fgColor, enable, matched);
}
void LoadFilterSettings(std::vector<Filter>& filters, CRegKey& reg)
{
for (int i = 0;; ++i)
{
CRegKey regFilter;
if (regFilter.Open(reg, WStr(wstringbuilder() << L"Filter" << i)) != ERROR_SUCCESS)
{
break;
}
filters.push_back(MakeFilter(
Str(Win32::RegGetStringValue(regFilter)),
IntToMatchType(Win32::RegGetDWORDValue(regFilter, L"MatchType", MatchType::Regex)),
IntToFilterType(Win32::RegGetDWORDValue(regFilter, L"Type")),
Win32::RegGetDWORDValue(regFilter, L"BgColor", Colors::BackGround),
Win32::RegGetDWORDValue(regFilter, L"FgColor", Colors::Text),
Win32::RegGetDWORDValue(regFilter, L"Enable", 1) != 0));
}
}
bool IsIncluded(std::vector<Filter>& filters, const std::string& text, MatchColors& matchColors)
{
for (auto& filter : filters)
{
if (!filter.enable)
{
continue;
}
if (filter.filterType == FilterType::Exclude && std::regex_search(text, filter.re))
{
return false;
}
}
bool included = false;
bool includeFilterPresent = false;
for (auto& filter : filters)
{
if (!filter.enable)
{
continue;
}
if (filter.bgColor == Colors::Auto)
{
std::sregex_iterator begin(text.begin(), text.end(), filter.re);
std::sregex_iterator end;
for (auto tok = begin; tok != end; ++tok)
{
auto key = MatchKey(*tok, filter.matchType);
if (matchColors.find(key) == matchColors.end())
{
matchColors.emplace(std::make_pair(key, GetRandomBackColor()));
}
}
}
if (filter.filterType == FilterType::Include)
{
includeFilterPresent = true;
included |= std::regex_search(text, filter.re);
}
if (filter.filterType == FilterType::Once && std::regex_search(text, filter.re))
{
included |= !filter.matched;
filter.matched = true;
}
}
return !includeFilterPresent || included;
}
bool MatchFilterType(const std::vector<Filter>& filters, FilterType::type type, const std::string& text)
{
for (auto& filter : filters)
{
if (filter.enable && filter.filterType == type && std::regex_search(text, filter.re))
{
return true;
}
}
return false;
}
} // namespace debugviewpp
} // namespace fusion