-
Notifications
You must be signed in to change notification settings - Fork 1
/
pack2md.cpp
90 lines (76 loc) · 2.38 KB
/
pack2md.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* MinGW Distro 17.1 :https://nuwen.net/mingw.html#install (using gcc 9.2.0)
* C++17 reference :https://en.cppreference.com/w/cpp/filesystem
* StackOverflow ref :https://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c/612176#612176
*
* 目标:
* - 遍历指定路径rootpath
* - 取出所有文件/文件夹的filename与深度
* - 数据导出到output.md
*/
#include <windows.h>
#include <string>
#include <iostream>
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;
std::string parseExt(const fs::path &p) {
std::string res;
for (wchar_t wch:p.wstring()) {
if (wch == '.') {
res.clear();
} else {
res += char(wch);
}
}
return res;
}
void inputfile(const fs::path &p) {
std::ifstream fin(p);
std::string inputs;
std::string ext = parseExt(p);
if (ext == "ignore") {
fin.close();
return;
}
std::cout << "``` " << ext << std::endl;
while (std::getline(fin, inputs))
std::cout << inputs << std::endl;
std::cout << "```" << std::endl;
fin.close();
}
void display_file(const fs::directory_entry &e, const int &d) {
std::cout << std::string(d, '#') << " ";
std::cout << e.path().filename().string() << '\n';
inputfile(e.path());
}
void display_dir(const fs::directory_entry &e, const int &d) {
std::cout << std::string(d, '#') << " ";
std::cout << e.path().filename().string() << std::endl;
}
void unexpected(const fs::path &p, const int &d) {
std::cerr << "*** [warning]: unexpected file ***" << std::endl;
std::cerr << p.string() << std::endl << std::endl;
}
void traverse(const fs::path &p, const int &d) {
for (const auto &entry : fs::directory_iterator(p)) {
if (fs::is_regular_file(entry)) {
display_file(entry, d);
} else if (fs::is_directory(entry)) {
display_dir(entry, d);
traverse(entry, d + 1);
} else {
unexpected(p, d);
}
}
}
int main() {
system("chcp 65001 > nul");
// https://blog.csdn.net/weixin_43851212/article/details/90485420
// cout 控制台中文
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
freopen("output.md", "w+", stdout);
fs::path root = "D:\\Twy59sGthb\\HappyACEveryday\\mycode";
traverse(root, 1);
}