-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
186 lines (158 loc) · 6.12 KB
/
Copy pathmain.cpp
File metadata and controls
186 lines (158 loc) · 6.12 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
#include "ConsoleUtils.h"
#include "AVL.cpp"
#include <iostream>
#include <filesystem>
#include <string>
using namespace std;
namespace fs = std::filesystem;
/*
Explanation:
The loadDirectory function recursively traverses the given directory path and its subdirectories. For each regular file found, it extracts the filename and full path, then inserts this information into the AVL tree index. This allows for efficient O(log n) search performance when looking up files by name later on.
*/
void loadDirectory(const fs::path& directoryPath, AVL& index) {
if (!fs::is_directory(directoryPath)) {
ConsoleUtils::setForegroundColor(BrightRed);
cout << " [!] Error: Not a valid directory path.\n";
ConsoleUtils::setDefaultColor();
return;
}
for (const auto& entry : fs::directory_iterator{ directoryPath }) {
fs::path entryPath = entry.path();
if (fs::is_regular_file(entryPath)) {
string fname = entryPath.filename().string();
string fullPath = fs::absolute(entryPath).string();
index.insert(fname, fullPath);
}
else if (fs::is_directory(entryPath)) {
loadDirectory(entryPath, index);
}
}
}
/*
Explanation:
The refresh method first frees all existing nodes in the tree to clear the current index, then calls the loadDirectory function to repopulate the tree with files from the specified directory. This allows users to update the index if files have been added, removed, or changed since the last indexing operation.
*/
void AVL::refresh(string rootPath) {
freeTree(root);
root = nullptr;
loadDirectory(rootPath, *this);
}
/*
Explanation:
The drawHeader function is responsible for displaying the application header with a styled rectangle and title.
*/
void drawHeader() {
ConsoleUtils::setForegroundColor(BrightCyan);
ConsoleUtils::DrawDoubleLineRectangle(1, 1, 50, 4);
ConsoleUtils::moveCursor(14, 2);
ConsoleUtils::setForegroundColor(BrightWhite);
cout << "Turbo Path - Path Finder";
ConsoleUtils::moveCursor(11, 3);
ConsoleUtils::setForegroundColor(Cyan);
cout << "AVL-Powered O(logn) File Search";
ConsoleUtils::moveCursor(35, 5);
ConsoleUtils::setForegroundColor(BrightYellow);
cout << "By: Jawad Ahmed";
ConsoleUtils::moveCursor(1, 8);
ConsoleUtils::setDefaultColor();
}
/*
Explanation:
The drawDivider function prints a horizontal divider line to visually separate different sections of the console output, enhancing readability.
*/
void drawDivider() {
ConsoleUtils::setForegroundColor(BrightBlack);
cout << " -----------------------------------------------------------------\n";
ConsoleUtils::setDefaultColor();
}
/*
Explanation:
The printHelp function displays a list of available commands and their descriptions to the user.
*/
void printHelp() {
ConsoleUtils::setForegroundColor(BrightBlack);
cout << " Commands: (filename) search | refresh - re-index | exit - quit\n";
ConsoleUtils::setDefaultColor();
}
/*
Explanation:
The printResult function displays the search results for a given filename, showing all matching file paths or an error message if no matches are found.
*/
void printResult(node* result, string fname) {
if (result == nullptr) {
ConsoleUtils::setForegroundColor(BrightRed);
cout << "\n [!] File not found: " << fname << "\n\n";
ConsoleUtils::setDefaultColor();
return;
}
ConsoleUtils::setForegroundColor(BrightGreen);
cout << "\n [+] Found " << result->paths.size() << " match(es) for \"" << fname << "\":\n";
ConsoleUtils::setDefaultColor();
for (int i = 0; i < result->paths.size(); i++) {
ConsoleUtils::setForegroundColor(BrightYellow);
cout << " [" << i + 1 << "] ";
ConsoleUtils::setForegroundColor(White);
cout << result->paths[i] << "\n";
}
cout << "\n";
ConsoleUtils::setDefaultColor();
}
/*
Explanation:
The main function initializes the application, prompts the user for a root directory path, indexes the files in that directory, and then enters a loop to handle user commands for searching or refreshing the index.
*/
int main() {
ConsoleUtils::enableVirtualTerminal();
ConsoleUtils::clearConsole();
ConsoleUtils::hideCursor();
drawHeader();
ConsoleUtils::setForegroundColor(BrightWhite);
cout << " Enter root directory path to index:\n";
ConsoleUtils::setForegroundColor(BrightMagenta);
string rootPath = getValidInput<string>(" >> ");
cout << "\n";
ConsoleUtils::setForegroundColor(Yellow);
cout << " [~] Indexing... please wait.\n";
ConsoleUtils::setDefaultColor();
AVL index;
loadDirectory(rootPath, index);
if (index.isEmpty()) {
ConsoleUtils::setForegroundColor(BrightRed);
cout << " [!] No files found in that directory. Exiting.\n";
ConsoleUtils::setDefaultColor();
ConsoleUtils::showCursor();
system("pause>0");
return 1;
}
ConsoleUtils::setForegroundColor(BrightGreen);
cout << " [+] Indexing complete. Ready to search.\n\n";
ConsoleUtils::setDefaultColor();
drawDivider();
printHelp();
drawDivider();
ConsoleUtils::showCursor();
string query;
while (true) {
ConsoleUtils::setForegroundColor(BrightCyan);
cout << "\n Search >> ";
ConsoleUtils::setDefaultColor();
cin >> query;
if (query == "exit" || query == "quit") break;
if (query == "refresh" || query == "re-index") {
ConsoleUtils::setForegroundColor(Yellow);
cout << "\n [*] Refreshing index...\n";
ConsoleUtils::setDefaultColor();
index.refresh(rootPath);
ConsoleUtils::setForegroundColor(BrightGreen);
cout << " [+] Index refreshed.\n";
ConsoleUtils::setDefaultColor();
continue;
}
printResult(index.search(query), query);
}
ConsoleUtils::setForegroundColor(BrightBlack);
cout << "\n Allah Hafiz.\n\n";
ConsoleUtils::setDefaultColor();
system("pause>0");
return 0;
}