Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sdottaka committed Sep 30, 2023
1 parent 312cb45 commit f6b1b14
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 99 deletions.
29 changes: 0 additions & 29 deletions Src/DiffWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1694,35 +1694,6 @@ void CDiffWrapper::WritePatchFile(struct change * script, file_data * inf)
free((void *)inf_patch[1].name);
}

/**
* @brief Set line filters, given as one string.
* @param [in] filterStr Filters.
*/
void CDiffWrapper::SetFilterList(const String& filterStr)
{
// Remove filterlist if new filter is empty
if (filterStr.empty())
{
m_pFilterList.reset();
return;
}

// Adding new filter without previous filter
if (m_pFilterList == nullptr)
{
m_pFilterList.reset(new FilterList);
}

m_pFilterList->RemoveAllFilters();

std::string regexp_str = ucr::toUTF8(filterStr);

// Add every "line" of regexps to regexp list
StringTokenizer tokens(regexp_str, "\r\n");
for (StringTokenizer::Iterator it = tokens.begin(); it != tokens.end(); ++it)
m_pFilterList->AddRegExp(*it);
}

const FilterList* CDiffWrapper::GetFilterList() const
{
return m_pFilterList.get();
Expand Down
1 change: 0 additions & 1 deletion Src/DiffWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ class CDiffWrapper
void WritePatchFileHeader(enum output_style output_style, bool bAppendFiles);
void WritePatchFileTerminator(enum output_style output_style);
const FilterList* GetFilterList() const;
void SetFilterList(const String& filterStr);
void SetFilterList(std::shared_ptr<FilterList> pFilterList);
const SubstitutionList* GetSubstitutionList() const;
void SetSubstitutionList(std::shared_ptr<SubstitutionList> pSubstitutionFiltersList);
Expand Down
17 changes: 3 additions & 14 deletions Src/DirDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,24 +191,13 @@ void CDirDoc::LoadLineFilterList(CDiffContext *pCtxt)
ASSERT(pCtxt != nullptr);

bool bFilters = GetOptionsMgr()->GetBool(OPT_LINEFILTER_ENABLED);
String filters = theApp.m_pLineFilters->GetAsString();
if (!bFilters || filters.empty())
auto filters = theApp.m_pLineFilters->MakeFilterList(false);
if (!bFilters || !filters->HasRegExps())
{
pCtxt->m_pFilterList.reset();
return;
}

if (pCtxt->m_pFilterList)
pCtxt->m_pFilterList->RemoveAllFilters();
else
pCtxt->m_pFilterList.reset(new FilterList());

std::string regexp_str = ucr::toUTF8(filters);

// Add every "line" of regexps to regexp list
StringTokenizer tokens(regexp_str, "\r\n");
for (StringTokenizer::Iterator it = tokens.begin(); it != tokens.end(); ++it)
pCtxt->m_pFilterList->AddRegExp(*it);
pCtxt->m_pFilterList = filters;
}

void CDirDoc::LoadSubstitutionFiltersList(CDiffContext* pCtxt)
Expand Down
8 changes: 5 additions & 3 deletions Src/FilterList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "FilterList.h"
#include <vector>
#include <Poco/RegularExpression.h>
#include <Poco/Exception.h>
#include "unicoder.h"

using Poco::RegularExpression;
Expand All @@ -33,16 +34,17 @@ FilterList::~FilterList()
* @param [in] encoding Expression encoding.
* @param [in] excluded
*/
void FilterList::AddRegExp(const std::string& regularExpression, bool exclude)
void FilterList::AddRegExp(const std::string& regularExpression, bool exclude, bool throwIfInvalid)
{
try
{
auto& list = exclude ? m_listExclude : m_list;
list.push_back(filter_item_ptr(new filter_item(regularExpression, RegularExpression::RE_UTF8)));
}
catch (...)
catch (Poco::RegularExpressionException& e)
{
// TODO:
if (throwIfInvalid)
throw std::runtime_error(e.message().c_str());
}
}

Expand Down
2 changes: 1 addition & 1 deletion Src/FilterList.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FilterList
FilterList();
~FilterList();

void AddRegExp(const std::string& regularExpression, bool exclude = false);
void AddRegExp(const std::string& regularExpression, bool exclude = false, bool throwIfInvalid = false);
void RemoveAllFilters();
bool HasRegExps() const;
bool Match(const std::string& string, int codepage = ucr::CP_UTF_8);
Expand Down
16 changes: 13 additions & 3 deletions Src/LineFiltersDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Merge.h"
#include "LineFiltersDlg.h"
#include "Constants.h"
#include "unicoder.h"

#ifdef _DEBUG
#define new DEBUG_NEW
Expand Down Expand Up @@ -163,7 +164,7 @@ void LineFiltersDlg::OnBnClickedLfilterEditbtn()
/**
* @brief Save filters to list when exiting the dialog.
*/
void LineFiltersDlg::OnOK()
BOOL LineFiltersDlg::OnApply()
{
m_pList->Empty();

Expand All @@ -174,10 +175,19 @@ void LineFiltersDlg::OnOK()

m_pList->AddFilter(text, enabled);
}
// Test
try
{
m_pList->MakeFilterList(true);
}
catch (std::runtime_error& e)
{
AfxMessageBox(ucr::toTString(e.what()).c_str(), MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}

AfxGetApp()->WriteProfileInt(_T("Settings"), _T("FilterStartPage"), GetParentSheet()->GetActiveIndex());

CPropertyPage::OnClose();
return TRUE;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Src/LineFiltersDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class LineFiltersDlg : public CTrPropertyPage
//{{AFX_MSG(LineFiltersDlg)
virtual BOOL OnInitDialog() override;
afx_msg void OnHelp();
virtual void OnOK() override;
virtual BOOL OnApply() override;
afx_msg void OnBnClickedLfilterAddBtn();
afx_msg void OnBnClickedLfilterEditbtn();
afx_msg void OnBnClickedLfilterRemovebtn();
Expand Down
53 changes: 28 additions & 25 deletions Src/LineFiltersList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "pch.h"
#include "LineFiltersList.h"
#include "FilterList.h"
#include <vector>
#include <cassert>
#include "OptionsMgr.h"
Expand Down Expand Up @@ -42,31 +43,6 @@ void LineFiltersList::AddFilter(const String& filter, bool enabled)
m_items.push_back(item);
}

/**
* @brief Returns the filter list as one filter string.
* This function returns the list of filters as one string that can be
* given to regular expression engine as filter. Filter strings in
* the list are separated by "|".
* @return Filter string.
*/
String LineFiltersList::GetAsString() const
{
String filter;
vector<LineFilterItemPtr>::const_iterator iter = m_items.begin();

while (iter != m_items.end())
{
if ((*iter)->enabled && !(*iter)->filterStr.empty())
{
if (!filter.empty())
filter += _T("|");
filter += (*iter)->filterStr;
}
++iter;
}
return filter;
}

/**
* @brief Return filter from given index.
* @param [in] ind Index of filter.
Expand Down Expand Up @@ -197,3 +173,30 @@ void LineFiltersList::SaveFilters()
retval2 = m_pOptionsMgr->RemoveOption(filter);
}
}

std::shared_ptr<FilterList> LineFiltersList::MakeFilterList(bool throwIfInvalid)
{
int i = 0;
std::shared_ptr<FilterList> plist(new FilterList);
for (auto& item : m_items)
{
if (item->enabled && !item->filterStr.empty())
{
try
{
plist->AddRegExp(ucr::toUTF8(item->filterStr), false, throwIfInvalid);
}
catch (const std::runtime_error& e)
{
if (throwIfInvalid)
{
plist.reset();
const String msg = strutils::format(_T("#%d: %S"), i + 1, e.what());
throw std::runtime_error(ucr::toUTF8(msg).c_str());
}
}
}
i++;
}
return plist;
}
4 changes: 3 additions & 1 deletion Src/LineFiltersList.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "UnicodeString.h"

class COptionsMgr;
class FilterList;

/**
@brief Structure for one line filter.
Expand All @@ -35,14 +36,15 @@ class LineFiltersList
void AddFilter(const String& filter, bool enabled);
size_t GetCount() const;
void Empty();
String GetAsString() const;
const LineFilterItem & GetAt(size_t ind) const;
void CloneFrom(const LineFiltersList *list);
bool Compare(const LineFiltersList *list) const;

void Initialize(COptionsMgr *pOptionsMgr);
void SaveFilters();

std::shared_ptr<FilterList> MakeFilterList(bool throwIfInvalid = false);

private:
std::vector<LineFilterItemPtr> m_items; /**< List for linefilter items */
COptionsMgr * m_pOptionsMgr; /**< Options-manager for storage */
Expand Down
23 changes: 6 additions & 17 deletions Src/MergeDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,23 +334,12 @@ int CMergeDoc::Rescan(bool &bBinary, IDENTLEVEL &identical,

ClearWordDiffCache();

if (GetOptionsMgr()->GetBool(OPT_LINEFILTER_ENABLED))
{
m_diffWrapper.SetFilterList(theApp.m_pLineFilters->GetAsString());
}
else
{
m_diffWrapper.SetFilterList(_T(""));
}

if (theApp.m_pSubstitutionFiltersList && theApp.m_pSubstitutionFiltersList->GetEnabled())
{
m_diffWrapper.SetSubstitutionList(theApp.m_pSubstitutionFiltersList->MakeSubstitutionList());
}
else
{
m_diffWrapper.SetSubstitutionList(nullptr);
}
m_diffWrapper.SetFilterList(
GetOptionsMgr()->GetBool(OPT_LINEFILTER_ENABLED) ?
theApp.m_pLineFilters->MakeFilterList() : nullptr);
m_diffWrapper.SetSubstitutionList(
(theApp.m_pSubstitutionFiltersList && theApp.m_pSubstitutionFiltersList->GetEnabled()) ?
theApp.m_pSubstitutionFiltersList->MakeSubstitutionList() : nullptr);

if (GetView(0, 0)->m_CurSourceDef->type != 0)
m_diffWrapper.SetFilterCommentsSourceDef(GetView(0, 0)->m_CurSourceDef);
Expand Down
5 changes: 2 additions & 3 deletions Src/SubstitutionFiltersDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "Merge.h"
#include "SubstitutionFiltersDlg.h"
#include "Constants.h"
#include <Poco/Exception.h>

#ifdef _DEBUG
#define new DEBUG_NEW
Expand Down Expand Up @@ -174,9 +173,9 @@ BOOL SubstitutionFiltersDlg::OnApply()
{
m_pSubstitutionFiltersList->MakeSubstitutionList(true);
}
catch (Poco::RegularExpressionException& e)
catch (std::runtime_error& e)
{
AfxMessageBox(ucr::toTString(e.message()).c_str(), MB_OK | MB_ICONEXCLAMATION);
AfxMessageBox(ucr::toTString(e.what()).c_str(), MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}

Expand Down
2 changes: 1 addition & 1 deletion Src/SubstitutionFiltersList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ std::shared_ptr<SubstitutionList> SubstitutionFiltersList::MakeSubstitutionList(
{
plist.reset();
const String msg = strutils::format(_T("#%d: %S"), i + 1, e.message().c_str());
throw Poco::RegularExpressionException(ucr::toUTF8(msg).c_str(), e.code());
throw std::runtime_error(ucr::toUTF8(msg).c_str());
}
}
}
Expand Down

0 comments on commit f6b1b14

Please sign in to comment.