forked from WinMerge/winmerge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffItem.cpp
166 lines (147 loc) · 3.43 KB
/
DiffItem.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @file DiffItem.cpp
*
* @brief Implementation of DIFFITEM
*/
#include "pch.h"
#include "DiffItem.h"
#include "paths.h"
#include "DebugNew.h"
DIFFITEM DIFFITEM::emptyitem;
/** @brief DIFFITEM's destructor */
DIFFITEM::~DIFFITEM()
{
RemoveChildren();
assert(children == nullptr);
}
/** @brief Return path to left/right file, including all but file name */
String DIFFITEM::getFilepath(int nIndex, const String &sRoot) const
{
if (diffcode.exists(nIndex))
{
return paths::ConcatPath(sRoot, diffFileInfo[nIndex].path);
}
return _T("");
}
/** @brief Return depth of path */
int DIFFITEM::GetDepth() const
{
const DIFFITEM *cur;
int depth;
for (depth = 0, cur = parent; cur->parent != nullptr; depth++, cur = cur->parent)
;
return depth;
}
/**
* @brief Return whether the specified item is an ancestor of the current item
*/
bool DIFFITEM::IsAncestor(const DIFFITEM *pdi) const
{
const DIFFITEM *cur;
for (cur = this; cur->parent != nullptr; cur = cur->parent)
{
if (cur->parent == pdi)
return true;
}
return false;
}
/** @brief Remove and delete all children DIFFITEM entries */
void DIFFITEM::RemoveChildren()
{
DIFFITEM *pRem = children;
while (pRem != nullptr)
{
assert(pRem->parent == this);
DIFFITEM *pNext = pRem->Flink;
delete pRem;
pRem = pNext;
}
children = nullptr;
}
/** @brief Swap two items in `diffFileInfo[]`. Used when swapping GUI panes. */
void DIFFITEM::Swap(int idx1, int idx2)
{
std::swap(diffFileInfo[idx1], diffFileInfo[idx2]);
diffcode.swap(idx1, idx2);
if (HasChildren())
{
for (DIFFITEM *p = children; p != nullptr; p = p->Flink)
p->Swap(idx1, idx2);
}
}
/* static */
DIFFITEM *DIFFITEM::GetEmptyItem()
{
// TODO: It would be better if there were individual items
// (for whatever these special items are?) because here we
// have to *hope* client does not modify this static (shared) item
assert(emptyitem.parent == nullptr);
assert(emptyitem.Flink == nullptr);
assert(emptyitem.Blink == nullptr);
assert(emptyitem.children == nullptr);
assert(emptyitem.nidiffs == -1);
assert(emptyitem.nsdiffs == -1);
assert(emptyitem.customFlags == ViewCustomFlags::INVALID_CODE);
assert(emptyitem.diffcode.diffcode == 0);
return &emptyitem;
}
/**
* @brief Add Sibling item
* @param [in] p The item to be added
*/
void DIFFITEM::AppendSibling(DIFFITEM *p)
{
assert(parent->children == this);
// Two situations
if (Blink == nullptr)
{
// Insert first sibling (besides ourself)
assert(Flink == nullptr);
p->Flink = nullptr;
p->Blink = this;
Flink = p;
}
else
{
// Insert additional siblings
assert(Flink != nullptr);
p->Flink = nullptr;
p->Blink = Blink;
Blink->Flink = p;
}
Blink = p;
}
void DIFFITEM::AddChildToParent(DIFFITEM *p)
{
p->parent = this;
if (children == nullptr)
// First child
children = p;
else
// More siblings
children->AppendSibling(p);
}
void DIFFITEM::DelinkFromSiblings()
{
if (parent != nullptr && parent->children != nullptr)
{
// If `this` is at end of Sibling linkage, fix First Child's end link
if (parent->children->Blink == this)
{
assert(Flink == nullptr);
parent->children->Blink = Blink;
if (Blink == this)
Blink = nullptr;
}
// If `this` is the First Child, link parent to next Sibling
if (parent->children == this)
{
parent->children = Flink;
}
}
if (Blink != nullptr && Blink->Flink != nullptr)
Blink->Flink = Flink;
if (Flink != nullptr)
Flink->Blink = Blink;
Flink = Blink = nullptr;
}