-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
190 lines (159 loc) · 5.4 KB
/
main.cpp
File metadata and controls
190 lines (159 loc) · 5.4 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
#include <filesystem>
#include <iostream>
using namespace std;
namespace fs = std::filesystem;
#define HIGHLIGHT_START_FILE "\033[1;31m"
#define HIGHLIGHT_START_DIR "\033[1;32m"
#define HIGHLIGHT_END "\033[0m"
#define BLIND_MSG "yes you are"
#define NOT_BLIND_MSG "no."
// yes i'm using snake_case and camelCase together and the earth is still the same as it was before, shock
struct Config {
string name;
string path;
long long int blind_count;
bool only_file;
bool only_dir;
bool recursive; // find it recusrively?
bool match_exactly; // find substring or da name must match exactly?
Config()
: recursive(false), match_exactly(false), only_file(false), only_dir(false), path(fs::current_path()),
blind_count(0) {} // default constructor
};
void handleMatchExactly(const string &name, const string &pathStr, Config &config, const fs::directory_entry &entry) {
if (name == config.name && (!config.only_file && !config.only_dir) || (config.only_dir && fs::is_directory(entry)) ||
(config.only_file && !fs::is_directory(entry))) {
config.blind_count += 1;
cout << pathStr.substr(0, pathStr.length() - name.length());
cout << (fs::is_directory(entry) ? HIGHLIGHT_START_DIR : HIGHLIGHT_START_FILE) << name << HIGHLIGHT_END << endl;
}
}
void handleMatchSubstring(const string &name, const string &pathStr, Config &config, const fs::directory_entry &entry) {
size_t pos = name.find(config.name);
if (pos != std::string::npos) {
pos = pathStr.rfind(config.name);
bool isDir = fs::is_directory(entry);
if ((!config.only_file && !config.only_dir) || (config.only_dir && isDir) || (config.only_file && !isDir)) {
config.blind_count += 1;
std::cout << pathStr.substr(0, pos);
std::cout << (isDir ? HIGHLIGHT_START_DIR : HIGHLIGHT_START_FILE) << config.name << HIGHLIGHT_END;
std::cout << pathStr.substr(pos + config.name.length()) << std::endl;
}
}
}
void handleMatch(const fs::directory_entry &entry, Config &config) {
try {
string name = entry.path().filename();
string pathStr = entry.path().string();
if (config.match_exactly) {
handleMatchExactly(name, pathStr, config, entry);
} else {
handleMatchSubstring(name, pathStr, config, entry);
}
} catch (const fs::filesystem_error &e) {
cerr << "Error accessing " << entry.path() << ": " << e.what() << endl;
} catch (const exception &e) {
cerr << "Unexpected error: " << e.what() << endl;
}
}
// find the garbage you specified
void findThisGarbage(Config &config) {
bool blind = false;
for (const auto &entry : fs::directory_iterator(config.path)) {
handleMatch(entry, config);
}
}
// recursively find the garbage you specified
void findThisGarbageRecursively(Config &config) {
try {
fs::recursive_directory_iterator it(config.path, fs::directory_options::skip_permission_denied), end;
for (; it != end; ++it) {
try {
const auto &entry = *it;
handleMatch(entry, config);
} catch (const std::exception &e) {
std::cerr << "Error handling entry: " << it->path() << " - " << e.what() << std::endl;
}
}
} catch (const fs::filesystem_error &e) {
std::cerr << "Error during iteration: " << e.what() << std::endl;
}
}
// handle ~
void handleDaFuqingSquigglyLine(Config &config) {
if (!config.path.empty() && config.path[0] == '~') {
const char *homeDir = getenv("HOME");
if (homeDir) {
config.path = string(homeDir) + config.path.substr(1);
}
}
}
void printHelp() {
cout << "Usage: am-i-blind [OPTIONS] <name> [path]" << endl << endl;
cout << "-r, --recursive Search Recursively" << endl;
cout << "-e, --exact Match the name exactly" << endl;
cout << "-f, --file Only search for files" << endl;
cout << "-d, --dir Only search for directories" << endl;
exit(0);
}
Config parseDaFuqingArgs(const int &argc, char *argv[]) {
if (argc == 1) {
printHelp();
}
Config config;
bool isNameSet = false;
bool isPathSet = false;
bool isFile = false;
bool isDir = false;
for (int i = 1; i < argc; ++i) {
string arg = argv[i];
if (arg == "-h" || arg == "--help") {
printHelp();
} else if (arg == "-e" || arg == "--exact") {
config.match_exactly = true;
} else if (arg == "-r" || arg == "--recursive") {
config.recursive = true;
} else if ((arg == "-f" || arg == "--file") && !isFile && !isDir) {
config.only_file = true;
isFile = true;
} else if ((arg == "-d" || arg == "--dir") && !isFile && !isDir) {
config.only_dir = true;
isDir = true;
} else if (!isNameSet) {
config.name = arg;
isNameSet = true;
} else if (!isPathSet) {
config.path = arg;
isPathSet = true;
} else {
throw "r u domb, use -h";
}
}
if (!isNameSet) {
throw "u defo dumb, use -h";
}
return config;
}
int main(int argc, char *argv[]) {
Config config;
try {
config = parseDaFuqingArgs(argc, argv);
} catch (const char *e) {
cerr << e << endl;
return 1;
}
if (!fs::exists(config.path)) {
cerr << "Path don't exist: " << config.path << endl;
return 1;
}
handleDaFuqingSquigglyLine(config);
if (config.recursive)
findThisGarbageRecursively(config);
else
findThisGarbage(config);
if (config.blind_count == 0)
cout << NOT_BLIND_MSG << endl;
else
cout << BLIND_MSG << " x" << config.blind_count << " times";
return 0;
}