forked from sabrogden/Ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegExFilterHelper.cpp
131 lines (110 loc) · 2.45 KB
/
RegExFilterHelper.cpp
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
#include "stdafx.h"
#include "CP_Main.h"
#include "RegExFilterHelper.h"
#include "Shared\Tokenizer.h"
#include "Options.h"
#include "WildCardMatch.h"
#include <regex>
#include <string>
void CRegExFilterData::ParseFilters()
{
m_parsedProcessFilters.RemoveAll();
CTokenizer token(m_processFilters, CGetSetOptions::GetCopyAppSeparator());
CString line;
while (token.Next(line))
{
if (line != "")
{
m_parsedProcessFilters.Add(line);
}
}
}
bool CRegExFilterData::MatchesProcessFilters(CString &activeApp)
{
if (activeApp == _T(""))
{
return true;
}
int count = (int)m_parsedProcessFilters.GetCount();
if (count == 0)
{
return true;
}
for (int i = 0; i < count; i++)
{
if (CWildCardMatch::WildMatch(m_parsedProcessFilters[i], activeApp, ""))
{
return true;
}
}
return false;
}
bool CRegExFilterData::MatchesRegEx(std::wstring &copiedText)
{
//std::wregex integer(_T("(\\+|-)?[[:digit:]]+"));
//std::wstring input(copiedText);
if (m_regEx != _T(""))
{
try
{
std::wregex integer(m_regEx);
if (regex_match(copiedText, integer))
{
return true;
}
}
catch (regex_error e)
{
CString w(e.what());
Log(StrF(_T("MatchesRegEx exception: %s, Code Is: %d"), w, e.code()));
}
}
return false;
}
CRegExFilterHelper::CRegExFilterHelper()
{
}
CRegExFilterHelper::~CRegExFilterHelper()
{
}
void CRegExFilterHelper::Add(int pos, CRegExFilterData &data)
{
if (pos >= 0 && pos < MAX_REGEX_FILTERS)
{
ATL::CCritSecLock csLock(m_critSection.m_sect);
m_filters[pos] = data;
}
}
void CRegExFilterHelper::SetRegEx(int pos, std::wstring regEx)
{
if (pos >= 0 && pos < MAX_REGEX_FILTERS)
{
ATL::CCritSecLock csLock(m_critSection.m_sect);
m_filters[pos].m_regEx = regEx;
}
}
void CRegExFilterHelper::SetProcessFilter(int pos, CString processName)
{
if (pos >= 0 && pos < MAX_REGEX_FILTERS)
{
ATL::CCritSecLock csLock(m_critSection.m_sect);
m_filters[pos].m_processFilters = processName;
m_filters[pos].ParseFilters();
}
}
bool CRegExFilterHelper::TextMatchFilters(CString &activeApp, std::wstring &copiedText)
{
ATL::CCritSecLock csLock(m_critSection.m_sect);
for (int i = 0; i < MAX_REGEX_FILTERS; i++)
{
if (m_filters[i].MatchesProcessFilters(activeApp))
{
if (m_filters[i].MatchesRegEx(copiedText))
{
Log(StrF(_T("regex matches copied text NOT SAVING CLIP, regex: %s, text: %s, active app: %s"), m_filters[i].m_regEx.c_str(), copiedText.c_str(), activeApp));
return true;
}
}
}
return false;
}