forked from cpp-tutor/learnmoderncpp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-code.cpp
More file actions
57 lines (55 loc) · 1.88 KB
/
extract-code.cpp
File metadata and controls
57 lines (55 loc) · 1.88 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
// extract-code.cpp : Scan a Markdown file for C++ programs
#include <string>
#include <sstream>
#include <regex>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
int main(const int argc, const char **argv) {
vector<string_view> args{ argv + 1, argv + argc };
for (const auto& filename : args) {
cout << "- " << filename << ":\n";
string input_file, line;
ifstream infile{ filename.data() };
if (!infile) {
cerr << "Error opening file: " << filename << '\n';
return 1;
}
for (;;) {
getline(infile, line);
if (infile.eof()) {
break;
}
input_file += line + '\n';
}
infile.close();
istringstream iss{ input_file };
while (!iss.eof()) {
getline(iss, line);
if ((line != "```cpp") && (line != "```")) {
continue;
}
bool no_cpp = line == "```";
getline(iss, line);
regex is_cpp { R"(^// ([[:alnum:]_-]+\.cpp) :)" };
smatch matches;
if (regex_search(line, matches, is_cpp)) {
cerr << "Filename: " << matches[1] << (no_cpp ? " (no type)" : "") << '\n';
}
string output_file = matches[1];
ofstream header{ "headers\\"s + output_file, ios_base::binary },
module{ "modules\\"s + output_file, ios_base::binary };
while (line != "```") {
header << line << '\n';
if (line == "using namespace std;") {
module << "import std;\n";
}
if (line.substr(0, 8) != "#include") {
module << line << '\n';
}
getline(iss, line);
}
}
}
}