-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathqfind.cpp
73 lines (64 loc) · 1.86 KB
/
qfind.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
#include "dir.h"
#include "match.h"
#include <algorithm>
#include <getopt.h>
#include <iostream>
#include <string.h>
using namespace std;
void usage(void)
{
cerr << "Quickfind version 1.0" << endl;
cerr << "Usage: qfind [-adf] <pattern> [path...]" << endl;
cerr << " qfind -h" << endl;
cerr << "Where options are:" << endl;
cerr << " -a Show also hidden files and directories" << endl;
cerr << " -d Print matching directories" << endl;
cerr << " -f Print matching files" << endl;
cerr << " -h Print this info and exit" << endl;
cerr << "Omitting both 'd' and 'f' flags is the same as specifying -df" << endl;
exit(1);
}
int main(int argc, char *argv[])
{
auto flags = 0;
int opt{};
while ((opt = getopt(argc, argv, "adfh")) != -1) {
switch (opt) {
case 'a':
flags |= DW_HIDDEN;
break;
case 'd':
flags |= DW_DIRECTORIES;
break;
case 'f':
flags |= DW_FILES;
break;
case 'h':
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
if ((flags & (DW_DIRECTORIES | DW_FILES)) == 0) {
flags |= DW_DIRECTORIES | DW_FILES;
}
vector<string> dirs = { "." };
if (argc > 1) {
dirs.clear();
for (int i = 1; i < argc; ++i) {
auto len = strlen(argv[i]);
if (argv[i][len - 1] == '/')
argv[i][len - 1] = '\0';
dirs.emplace_back(argv[i]);
}
}
string pattern = argc == 0 ? "" : *argv;
vector<wstr> strings;
for (auto& dir : dirs)
dirwalk(dir, flags, [&](auto name) { strings.emplace_back(name); });
auto last = match(strings.begin(), strings.end(), pattern);
for_each(strings.begin(), last, [](auto& s) { puts(s.str.c_str()); });
return 0;
}