-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_file_structure.cpp
More file actions
86 lines (73 loc) · 2.51 KB
/
Copy pathget_file_structure.cpp
File metadata and controls
86 lines (73 loc) · 2.51 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
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_set>
#include <filesystem>
#include <vector>
namespace fs = std::filesystem;
// Load .gitignore patterns into a set
std::unordered_set<std::string> load_gitignore(const fs::path& root) {
std::unordered_set<std::string> ignores;
ignores.insert(".git"); // always ignore .git
std::ifstream file((root / ".gitignore").string());
std::string line;
while (std::getline(file, line)) {
if (line.empty() || line[0] == '#') continue;
ignores.insert(line);
}
return ignores;
}
// Check if a path should be ignored
bool is_ignored(const fs::path& p, const std::unordered_set<std::string>& ignores) {
std::string filename = p.filename().string();
std::string pathStr = p.string();
for (const auto& pattern : ignores) {
if (filename == pattern || pathStr.find(pattern) != std::string::npos) {
return true;
}
}
return false;
}
// Recursively build JSON structure
void build_json(const fs::path& root,
const std::unordered_set<std::string>& ignores,
std::ostream& out,
int depth = 0) {
out << "{";
bool first = true;
// Collect files separately
std::vector<std::string> files;
for (const auto& entry : fs::directory_iterator(root)) {
if (is_ignored(entry.path(), ignores)) continue;
std::string name = entry.path().filename().string();
if (fs::is_directory(entry)) {
if (!first) out << ",";
first = false;
out << "\"" << name << "\":";
build_json(entry.path(), ignores, out, depth + 1);
} else {
files.push_back(name);
}
}
// Add files array if any
if (!files.empty()) {
if (!first) out << ",";
out << "\"files\":[";
for (size_t i = 0; i < files.size(); ++i) {
if (i > 0) out << ",";
out << "\"" << files[i] << "\"";
}
out << "]";
}
out << "}";
}
int main(int argc, char* argv[]) {
fs::path root = (argc > 1) ? argv[1] : fs::current_path();
auto ignores = load_gitignore(root);
std::ofstream out(root / "structure.jsonc");
out << "{ \"" << root.filename().string() << "\":";
build_json(root, ignores, out);
out << "}" << std::endl;
std::cout << "Project structure written to " << (root / "structure.jsonc").string() << "\n";
return 0;
}