RegExFilterHelper.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "RegExFilterHelper.h"
  4. #include "Shared\Tokenizer.h"
  5. #include "Options.h"
  6. #include "WildCardMatch.h"
  7. #include <regex>
  8. #include <string>
  9. void CRegExFilterData::ParseFilters()
  10. {
  11. m_parsedProcessFilters.RemoveAll();
  12. CTokenizer token(m_processFilters, CGetSetOptions::GetCopyAppSeparator());
  13. CString line;
  14. while (token.Next(line))
  15. {
  16. if (line != "")
  17. {
  18. m_parsedProcessFilters.Add(line);
  19. }
  20. }
  21. }
  22. bool CRegExFilterData::MatchesProcessFilters(CString &activeApp)
  23. {
  24. if (activeApp == _T(""))
  25. {
  26. return true;
  27. }
  28. int count = (int)m_parsedProcessFilters.GetCount();
  29. if (count == 0)
  30. {
  31. return true;
  32. }
  33. for (int i = 0; i < count; i++)
  34. {
  35. if (CWildCardMatch::WildMatch(m_parsedProcessFilters[i], activeApp, ""))
  36. {
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. bool CRegExFilterData::MatchesRegEx(std::wstring &copiedText)
  43. {
  44. //std::wregex integer(_T("(\\+|-)?[[:digit:]]+"));
  45. //std::wstring input(copiedText);
  46. if (m_regEx != _T(""))
  47. {
  48. try
  49. {
  50. std::wregex integer(m_regEx);
  51. if (regex_match(copiedText, integer))
  52. {
  53. return true;
  54. }
  55. }
  56. catch (regex_error e)
  57. {
  58. CString w(e.what());
  59. Log(StrF(_T("MatchesRegEx exception: %s, Code Is: %d"), w, e.code()));
  60. }
  61. }
  62. return false;
  63. }
  64. CRegExFilterHelper::CRegExFilterHelper()
  65. {
  66. }
  67. CRegExFilterHelper::~CRegExFilterHelper()
  68. {
  69. }
  70. void CRegExFilterHelper::Add(int pos, CRegExFilterData &data)
  71. {
  72. if (pos >= 0 && pos < MAX_REGEX_FILTERS)
  73. {
  74. ATL::CCritSecLock csLock(m_critSection.m_sect);
  75. m_filters[pos] = data;
  76. }
  77. }
  78. void CRegExFilterHelper::SetRegEx(int pos, std::wstring regEx)
  79. {
  80. if (pos >= 0 && pos < MAX_REGEX_FILTERS)
  81. {
  82. ATL::CCritSecLock csLock(m_critSection.m_sect);
  83. m_filters[pos].m_regEx = regEx;
  84. }
  85. }
  86. void CRegExFilterHelper::SetProcessFilter(int pos, CString processName)
  87. {
  88. if (pos >= 0 && pos < MAX_REGEX_FILTERS)
  89. {
  90. ATL::CCritSecLock csLock(m_critSection.m_sect);
  91. m_filters[pos].m_processFilters = processName;
  92. m_filters[pos].ParseFilters();
  93. }
  94. }
  95. bool CRegExFilterHelper::TextMatchFilters(CString &activeApp, std::wstring &copiedText)
  96. {
  97. ATL::CCritSecLock csLock(m_critSection.m_sect);
  98. for (int i = 0; i < MAX_REGEX_FILTERS; i++)
  99. {
  100. if (m_filters[i].MatchesProcessFilters(activeApp))
  101. {
  102. if (m_filters[i].MatchesRegEx(copiedText))
  103. {
  104. 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));
  105. return true;
  106. }
  107. }
  108. }
  109. return false;
  110. }