/
find-dup-names.cc
53 lines (43 loc) · 1.18 KB
/
find-dup-names.cc
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
// davep 20190408 walk a filesystem, find duplicate filenames
// Learning exercise for std::filesystem std::regex std::map
//
// gcc -g -Wall -o find-dup-names find-dup-names.cc -std=c++17 -lstdc++ -lstdc++fs
//
#include <iostream>
#include <filesystem>
#include <map>
#include <regex>
namespace fs = std::filesystem;
namespace {
// https://en.cppreference.com/w/cpp/regex/basic_regex
std::regex src_file { "[a-zA-Z_]+\\.c" };
}
int main()
{
std::map<std::string, std::string> pathmap;
std::map<std::string, std::string>::iterator iter;
bool flag;
for (auto& f : fs::recursive_directory_iterator(".")) {
if (! f.is_regular_file()) {
continue;
}
fs::path path = f.path();
std::cout << path << "\n";
// iterate over the components in the path
for (auto& p : path) {
std::cout << "p=" << p << "\n";
}
std::string filename = path.filename();
std::cout << filename << "\n";
if (!std::regex_match(filename, src_file)) {
continue;
}
std::cout << "regex match\n";
std::tie(iter, flag) = pathmap.emplace(filename, path);
if (!flag) {
std::cerr << "duplicate filename found " << filename << " at " << iter->second << "\n";
throw filename;
}
}
return 0;
}