This repository was archived by the owner on Dec 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiletree.cpp
More file actions
207 lines (170 loc) · 7.09 KB
/
filetree.cpp
File metadata and controls
207 lines (170 loc) · 7.09 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//---------------------------------------------------------------------------
#pragma hdrstop
#include "filetree.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#include <memory>
#include <unordered_map>
#include <map>
#include <System.Threading.hpp>
namespace FileTreeUtils {
struct NodeBuildInfo {
UnicodeString text;
int imageIndex;
UnicodeString parentPath;
UnicodeString fullPath;
UnicodeString relativePath;
bool isFolder;
};
//UnicodeString MakeRelativePath(const UnicodeString& fullPath, const UnicodeString& rootFolder) {
// if (fullPath.Length() <= rootFolder.Length() + 1) {
// return {};
// }
// UnicodeString result = fullPath.SubString(rootFolder.Length() + 2, fullPath.Length());
// return StringReplace(result, L"\\", L"/", TReplaceFlags() << rfReplaceAll);
//}
UnicodeString MakeRelativePath(const UnicodeString& fullPath, const UnicodeString& rootFolder)
{
UnicodeString normalizedRoot = System::Sysutils::ExcludeTrailingPathDelimiter( rootFolder );
if (fullPath.Length() <= normalizedRoot.Length() + 1) {
return {};
}
UnicodeString result = fullPath.SubString(normalizedRoot.Length() + 2, fullPath.Length());
return StringReplace(result, L"\\", L"/", TReplaceFlags() << rfReplaceAll);
}
TTreeViewItem* CreateTreeItem(const NodeBuildInfo& nodeInfo, TTreeView* treeView) {
auto newItem = new TTreeViewItem(treeView);
newItem->Text = nodeInfo.text;
newItem->IsExpanded = false;
newItem->ImageIndex = nodeInfo.imageIndex;
newItem->TagString = nodeInfo.relativePath;
return newItem;
}
void BuildFileTree(
TTreeView* treeView,
const UnicodeString& rootFolder,
const UnicodeString& filePattern,
TreeBuildCallback onComplete)
{
if (!treeView || rootFolder.IsEmpty()) {
if (onComplete) onComplete();
return;
}
TThread::Synchronize(nullptr, [treeView]() {
treeView->Clear();
});
TThread::CreateAnonymousThread([treeView, rootFolder, filePattern, onComplete]() {
try {
std::vector<NodeBuildInfo> nodeInfos;
NodeBuildInfo rootNode;
rootNode.text = Ioutils::TPath::GetFileName( System::Sysutils::ExcludeTrailingPathDelimiter( rootFolder ) );
rootNode.imageIndex = 0;
rootNode.parentPath = {};
rootNode.fullPath = rootFolder;
rootNode.relativePath = {};
rootNode.isFolder = true;
nodeInfos.push_back(rootNode);
std::map<UnicodeString, bool> processedPaths;
processedPaths[rootFolder] = true;
try {
auto files = Ioutils::TDirectory::GetFiles(rootFolder, filePattern, TSearchOption::soAllDirectories);
for (int i = 0; i < files.Length; ++i) {
const auto& filePath = files[i];
const auto relPath = MakeRelativePath(filePath, rootFolder);
auto pathParts = SplitString(relPath, L"/");
UnicodeString currentPath = rootFolder;
UnicodeString parentPath = rootFolder;
for (int j = 0; j < pathParts.Length - 1; ++j) {
currentPath = currentPath + L"\\" + pathParts[j];
if (processedPaths.find(currentPath) == processedPaths.end()) {
NodeBuildInfo folderNode;
folderNode.text = pathParts[j];
folderNode.imageIndex = 0;
folderNode.parentPath = parentPath;
folderNode.fullPath = currentPath;
folderNode.isFolder = true;
int endPos = relPath.Pos(pathParts[j]) + pathParts[j].Length() - 1;
folderNode.relativePath = relPath.SubString(1, endPos);
nodeInfos.push_back(folderNode);
processedPaths[currentPath] = true;
}
parentPath = currentPath;
}
const auto& fileName = pathParts[pathParts.Length - 1];
NodeBuildInfo fileNode;
fileNode.text = Ioutils::TPath::GetFileNameWithoutExtension(fileName);
fileNode.imageIndex = 1;
fileNode.parentPath = parentPath;
fileNode.fullPath = filePath;
fileNode.relativePath = Ioutils::TPath::ChangeExtension(relPath, L"");
if (!fileNode.relativePath.IsEmpty() && fileNode.relativePath[fileNode.relativePath.Length()] == L'.') fileNode.relativePath.SetLength(fileNode.relativePath.Length() - 1);
fileNode.isFolder = false;
nodeInfos.push_back(fileNode);
}
// Sort Tree
std::unordered_map<UnicodeString, std::vector<NodeBuildInfo>> grouped;
for (const auto& node : nodeInfos)
grouped[node.parentPath].push_back(node);
nodeInfos.clear();
for (auto& [parent, group] : grouped) {
std::sort(group.begin(), group.end(), [](const NodeBuildInfo& a, const NodeBuildInfo& b) {
if (a.imageIndex != b.imageIndex)
return a.imageIndex < b.imageIndex;
return a.text.CompareIC(b.text) < 0;
});
nodeInfos.insert(nodeInfos.end(), group.begin(), group.end());
}
// ----------
TThread::Synchronize(nullptr, [treeView, nodeInfos]() {
std::unordered_map<UnicodeString, TTreeViewItem*> nodeMap;
try {
for (const auto& nodeInfo : nodeInfos) {
auto newItem = CreateTreeItem(nodeInfo, treeView);
nodeMap[nodeInfo.fullPath] = newItem;
}
for (const auto& nodeInfo : nodeInfos) {
auto item = nodeMap[nodeInfo.fullPath];
if (!item) continue;
if (nodeInfo.parentPath.IsEmpty()) {
treeView->AddObject(item);
item->IsExpanded = true;
} else {
auto parentItem = nodeMap[nodeInfo.parentPath];
if (parentItem) {
parentItem->AddObject(item);
}
}
}
} catch (const Exception& e) {
OutputDebugString((L"[BuildFileTree] UI build error: " + e.Message).c_str());
}
});
} catch (const Exception& e) {
OutputDebugString((L"[BuildFileTree] File scan error: " + e.Message).c_str());
}
} catch (...) {
OutputDebugString(L"[BuildFileTree] Unexpected error occurred");
}
if (onComplete) {
TThread::Queue(nullptr, [onComplete]() {
onComplete();
});
}
})->Start();
}
std::unique_ptr<TreeNodeInfo> GetSelectedNodeInfo(TTreeView* treeView) {
if (!treeView || !treeView->Selected) {
return nullptr;
}
auto selectedItem = dynamic_cast<TTreeViewItem*>(treeView->Selected);
if (!selectedItem) {
return nullptr;
}
auto result = std::make_unique<TreeNodeInfo>();
result->text = selectedItem->Text;
result->imageIndex = selectedItem->ImageIndex;
result->relativePath = selectedItem->TagString;
result->isFolder = (selectedItem->ImageIndex == 0);
return result;
}
}