OSDN Git Service

An attempt to reduce build time
[winmerge-jp/winmerge-jp.git] / Src / LineFiltersList.cpp
1 /** 
2  * @file  LineFiltersList.cpp
3  *
4  * @brief Implementation for LineFiltersList class.
5  */
6
7 #include "pch.h"
8 #include "LineFiltersList.h"
9 #include <vector>
10 #include <cassert>
11 #include "OptionsMgr.h"
12 #include "UnicodeString.h"
13
14 using std::vector;
15
16 /** @brief Registry key for saving linefilters. */
17 static const tchar_t LineFiltersRegPath[] = _T("LineFilters");
18
19 /**
20  * @brief Default constructor.
21  */
22 LineFiltersList::LineFiltersList()
23 : m_pOptionsMgr(nullptr)
24 {
25 }
26
27 /**
28  * @brief Destructor, empties the list.
29  */
30 LineFiltersList::~LineFiltersList() = default;
31
32 /**
33  * @brief Add new filter to the list.
34  * @param [in] filter Filter string to add.
35  * @param [in] enabled Is filter enabled?
36  */
37 void LineFiltersList::AddFilter(const String& filter, bool enabled)
38 {
39         auto item = std::make_shared<LineFilterItem>(LineFilterItem());
40         item->enabled = enabled;
41         item->filterStr = filter;
42         m_items.push_back(item);
43 }
44
45 /**
46  * @brief Returns the filter list as one filter string.
47  * This function returns the list of filters as one string that can be
48  * given to regular expression engine as filter. Filter strings in
49  * the list are separated by "|".
50  * @return Filter string.
51  */
52 String LineFiltersList::GetAsString() const
53 {
54         String filter;
55         vector<LineFilterItemPtr>::const_iterator iter = m_items.begin();
56
57         while (iter != m_items.end())
58         {
59                 if ((*iter)->enabled && !(*iter)->filterStr.empty())
60                 {
61                         if (!filter.empty())
62                                 filter += _T("|");
63                         filter += (*iter)->filterStr;
64                 }
65                 ++iter;
66         }
67         return filter;  
68 }
69
70 /**
71  * @brief Return filter from given index.
72  * @param [in] ind Index of filter.
73  * @return Filter item from the index. If the index is beyond table limit,
74  *  return the last item in the list.
75  */
76 const LineFilterItem & LineFiltersList::GetAt(size_t ind) const
77 {
78         if (ind < m_items.size())
79                 return *m_items[ind];
80         else
81                 return *m_items.back();
82 }
83
84 /**
85  * @brief Clone filter list from another list.
86  * This function clones filter list from another list. Current items in the
87  * list are removed and new items added from the given list.
88  * @param [in] list List to clone.
89  */
90 void LineFiltersList::CloneFrom(const LineFiltersList *list)
91 {
92         Empty();
93         size_t count = list->GetCount();
94
95         for (size_t i = 0; i < count; i++)
96         {
97                 const LineFilterItem &item = list->GetAt(i);
98                 AddFilter(item.filterStr, item.enabled);
99         }
100 }
101
102 /**
103  * @brief Compare filter lists.
104  * @param [in] list List to compare.
105  * @return true if lists are identical, false otherwise.
106  */
107 bool LineFiltersList::Compare(const LineFiltersList *list) const
108 {
109         if (list->GetCount() != GetCount())
110                 return false;
111
112         for (size_t i = 0; i < GetCount(); i++)
113         {
114                 const LineFilterItem &item1 = list->GetAt(i);
115                 const LineFilterItem &item2 = GetAt(i);
116
117                 if (item1.enabled != item2.enabled)
118                         return false;
119
120                 if (item1.filterStr != item2.filterStr)
121                         return false;
122         }
123         return true;
124 }
125
126 /**
127  * @brief Read filter list from the options system.
128  * @param [in] pOptionsMgr Pointer to options system.
129  */
130 void LineFiltersList::Initialize(COptionsMgr *pOptionsMgr)
131 {
132         assert(pOptionsMgr != nullptr);
133         String valuename(LineFiltersRegPath);
134
135         m_pOptionsMgr = pOptionsMgr;
136
137         Empty();
138
139         valuename += _T("/Values");
140         m_pOptionsMgr->InitOption(valuename, 0);
141         size_t count = m_pOptionsMgr->GetInt(valuename);
142
143         for (unsigned i = 0; i < count; i++)
144         {
145                 String name = strutils::format(_T("%s/Filter%02u"), LineFiltersRegPath, i);
146                 m_pOptionsMgr->InitOption(name, _T(""));
147                 String filter = m_pOptionsMgr->GetString(name);
148
149                 name = strutils::format(_T("%s/Enabled%02u"), LineFiltersRegPath, i);
150                 m_pOptionsMgr->InitOption(name, (int)true);
151                 int enabled = m_pOptionsMgr->GetInt(name);
152                 bool bEnabled = enabled ? true : false;
153
154                 AddFilter(filter, bEnabled);
155         }
156 }
157
158 /**
159  * @brief Save linefilters to options system.
160  */
161 void LineFiltersList::SaveFilters()
162 {
163         assert(m_pOptionsMgr != nullptr);
164         String valuename(LineFiltersRegPath);
165
166         size_t count = m_items.size();
167         valuename += _T("/Values");
168         m_pOptionsMgr->SaveOption(valuename, static_cast<int>(count));
169
170         for (size_t i = 0; i < count; i++)
171         {
172                 const LineFilterItemPtr& item = m_items[i];
173
174                 String name = strutils::format(_T("%s/Filter%02u"), LineFiltersRegPath, i);
175                 m_pOptionsMgr->InitOption(name, _T(""));
176                 m_pOptionsMgr->SaveOption(name, item->filterStr);
177
178                 name = strutils::format(_T("%s/Enabled%02u"), LineFiltersRegPath, i);
179                 m_pOptionsMgr->InitOption(name, 0);
180                 m_pOptionsMgr->SaveOption(name, (int)item->enabled);
181         }
182
183         // Remove options we don't need anymore
184         // We could have earlier 10 pcs but now we only need 5
185         String filter = strutils::format(_T("%s/Filter%02u"), LineFiltersRegPath, count);
186         int retval1 = m_pOptionsMgr->RemoveOption(filter);
187
188         filter = strutils::format(_T("%s/Enabled%02u"), LineFiltersRegPath, count);
189         int retval2 = m_pOptionsMgr->RemoveOption(filter);
190         
191         while (retval1 == COption::OPT_OK || retval2 == COption::OPT_OK)
192         {
193                 ++count;
194                 filter = strutils::format(_T("%s/Filter%02u"), LineFiltersRegPath, count);
195                 retval1 = m_pOptionsMgr->RemoveOption(filter);
196                 filter = strutils::format(_T("%s/Enabled%02u"), LineFiltersRegPath, count);
197                 retval2 = m_pOptionsMgr->RemoveOption(filter);
198         }
199 }