2 * @file DiffItemList.cpp
4 * @brief Implementation of DiffItemList
8 #include "DiffItemList.h"
15 DiffItemList::DiffItemList() : m_pRoot(nullptr)
22 DiffItemList::~DiffItemList()
28 * @brief Add new diffitem to structured DIFFITEM tree.
29 * @param [in] par Parent item, or `nullptr` if no parent.
30 * @return Pointer to the added item.
32 DIFFITEM *DiffItemList::AddNewDiff(DIFFITEM *par)
34 DIFFITEM *p = new DIFFITEM;
37 // if there is no `parent`, this item becomes a child of `m_pRoot`
38 assert(m_pRoot != nullptr);
39 m_pRoot->AddChildToParent(p);
42 par->AddChildToParent(p);
48 * @brief Empty structured DIFFITEM tree
50 void DiffItemList::RemoveAll()
56 void DiffItemList::InitDiffItemList()
58 assert(m_pRoot == nullptr);
59 m_pRoot = new DIFFITEM;
63 * @brief Get position of first item in structured DIFFITEM tree
65 DIFFITEM *DiffItemList::GetFirstDiffPosition() const
67 return GetFirstChildDiffPosition(m_pRoot);
71 * @brief Get position of first child item in structured DIFFITEM tree
72 * @param [in] par Position of parent diff item (maybe `nullptr`)
73 * @return Position of first child item (or `nullptr` if no children)
75 DIFFITEM *DiffItemList::GetFirstChildDiffPosition(const DIFFITEM *par) const
79 assert(m_pRoot != nullptr);
80 return m_pRoot->GetFirstChild();
83 return par->GetFirstChild();
87 * @brief Get position of next item in structured DIFFITEM tree
88 * @param [in,out] diffpos Position of current item, updated to next item position
89 * @return Diff Item (by reference) in current position
91 const DIFFITEM &DiffItemList::GetNextDiffPosition(DIFFITEM *&diffpos) const
93 DIFFITEM *p = diffpos;
94 if (diffpos->HasChildren())
96 diffpos = GetFirstChildDiffPosition(diffpos);
100 DIFFITEM *cur = diffpos;
103 diffpos = cur->GetFwdSiblingLink();
104 cur = cur->GetParentLink();
105 assert(cur != nullptr);
106 } while (diffpos == nullptr && cur->HasParent());
112 * @brief Get position of next item in structured DIFFITEM tree
113 * @param [in,out] diffpos Position of current item, updated to next item position
114 * @return Diff Item (by reference) in current position
116 DIFFITEM &DiffItemList::GetNextDiffRefPosition(DIFFITEM *&diffpos)
118 return (DIFFITEM &)GetNextDiffPosition(diffpos);
122 * @brief Get position of next sibling item in structured DIFFITEM tree
123 * @param [in,out] diffpos Position of current item, updated to next sibling item position
124 * @return Diff Item (by reference) in current position
126 const DIFFITEM &DiffItemList::GetNextSiblingDiffPosition(DIFFITEM *&diffpos) const
128 DIFFITEM *p = diffpos;
129 diffpos = p->GetFwdSiblingLink();
130 assert(p==nullptr || diffpos==nullptr || p->GetParentLink() == diffpos->GetParentLink());
131 assert(p==nullptr || p->HasParent());
136 * @brief Get position of next sibling item in structured DIFFITEM tree
137 * @param [in,out] diffpos Position of current item, updated to next sibling item position
138 * @return Diff Item (by reference) in current position
140 DIFFITEM &DiffItemList::GetNextSiblingDiffRefPosition(DIFFITEM *&diffpos)
142 return (DIFFITEM &)GetNextSiblingDiffPosition(diffpos);
146 * @brief Alter some bit flags of the diffcode.
149 * SetDiffStatusCode(pos, DIFFCODE::SAME, DIFFCODE::COMPAREFLAGS)
150 * changes the comparison result to be the same.
152 * SetDiffStatusCode(pos, DIFFCODE::BOTH, DIFFCODE::SIDEFLAG)
153 * changes the side status to be both (sides).
155 * SetDiffStatusCode(pos, DIFFCODE::SAME+DIFFCODE::BOTH, DIFFCODE::COMPAREFLAGS+DIFFCODE::SIDEFLAG);
156 * changes the comparison result to be the same and the side status to be both
158 void DiffItemList::SetDiffStatusCode(DIFFITEM *diffpos, unsigned diffcode, unsigned mask)
160 assert(diffpos != nullptr);
161 DIFFITEM &di = GetDiffRefAt(diffpos);
162 assert( ((~mask) & diffcode) == 0 ); // make sure they only set flags in their mask
163 di.diffcode.diffcode &= (~mask); // remove current data
164 di.diffcode.diffcode |= diffcode; // add new data
168 * @brief Update difference counts.
170 void DiffItemList::SetDiffCounts(DIFFITEM *diffpos, unsigned diffs, unsigned ignored)
172 assert(diffpos != nullptr);
173 DIFFITEM &di = GetDiffRefAt(diffpos);
174 di.nidiffs = ignored; // see StoreDiffResult() in DirScan.cpp
179 * @brief Returns item's custom (user) flags.
180 * @param [in] diffpos Position of item.
181 * @return Custom flags from item.
183 unsigned DiffItemList::GetCustomFlags1(DIFFITEM *diffpos) const
185 assert(diffpos != nullptr);
186 const DIFFITEM &di = GetDiffAt(diffpos);
187 return di.customFlags;
191 * @brief Sets item's custom (user) flags.
192 * @param [in] diffpos Position of item.
193 * @param [in] flag Value of flag to set.
195 void DiffItemList::SetCustomFlags1(DIFFITEM *diffpos, unsigned flag)
197 assert(diffpos != nullptr);
198 DIFFITEM &di = GetDiffRefAt(diffpos);
199 di.customFlags = flag;
202 void DiffItemList::Swap(int idx1, int idx2)
204 assert(m_pRoot != nullptr);
205 for (DIFFITEM *p = GetFirstDiffPosition(); p != nullptr; p = p->GetFwdSiblingLink())