diff options
| -rw-r--r-- | src/srcut.cpp | 91 |
1 files changed, 81 insertions, 10 deletions
diff --git a/src/srcut.cpp b/src/srcut.cpp index 194cc65..042608e 100644 --- a/src/srcut.cpp +++ b/src/srcut.cpp | |||
| @@ -1,34 +1,105 @@ | |||
| 1 | #include <fstream> | 1 | #include <fstream> |
| 2 | #include <iostream> | 2 | #include <iostream> |
| 3 | #include <map> | ||
| 4 | #include <sstream> | ||
| 3 | #include <string> | 5 | #include <string> |
| 6 | #include <vector> | ||
| 4 | 7 | ||
| 5 | using namespace std; | 8 | using namespace std; |
| 6 | 9 | ||
| 7 | static void | 10 | istream& |
| 8 | process(istream& in, ostream& os) | 11 | operator>>(istream& in, map<string, vector<string>>& container) |
| 9 | { | 12 | { |
| 10 | auto match_begin = [](string_view s) -> bool { | 13 | auto is_comment = [](string_view s) -> bool { |
| 11 | return s.empty(); | 14 | return s == "//" || s == "/*" || s == "#" || s == "--"; |
| 12 | }; | 15 | }; |
| 13 | 16 | ||
| 14 | auto match_end = [](string_view s) -> bool { | 17 | // --8<-- match_begin |
| 15 | return s.empty(); | 18 | auto match_begin = [&is_comment](const string& line, string& name) -> bool { |
| 19 | stringstream ss{ line }; | ||
| 20 | |||
| 21 | string comment, cut_mark; | ||
| 22 | |||
| 23 | if ( !(ss >> comment >> cut_mark >> name) ) { | ||
| 24 | return false; | ||
| 25 | } | ||
| 26 | |||
| 27 | if ( !is_comment(comment) ) { | ||
| 28 | return false; | ||
| 29 | } | ||
| 30 | |||
| 31 | if ( cut_mark != "--8<--" ) { | ||
| 32 | return false; | ||
| 33 | } | ||
| 34 | |||
| 35 | return true; | ||
| 36 | }; | ||
| 37 | // -->8-- | ||
| 38 | |||
| 39 | // --8<-- match_end | ||
| 40 | auto match_end = [&is_comment](const string& line) -> bool { | ||
| 41 | stringstream ss{ line }; | ||
| 42 | |||
| 43 | string comment, cut_mark; | ||
| 44 | |||
| 45 | if ( !(ss >> comment >> cut_mark) ) { | ||
| 46 | return false; | ||
| 47 | } | ||
| 48 | |||
| 49 | if ( !is_comment(comment) ) { | ||
| 50 | return false; | ||
| 51 | } | ||
| 52 | |||
| 53 | if ( cut_mark != "-->8--" ) { | ||
| 54 | return false; | ||
| 55 | } | ||
| 56 | |||
| 57 | return true; | ||
| 16 | }; | 58 | }; |
| 59 | // -->8-- | ||
| 17 | 60 | ||
| 61 | vector<string> chunks; | ||
| 18 | for ( string line; getline(in, line); ) { | 62 | for ( string line; getline(in, line); ) { |
| 19 | if ( match_begin(line) ) { | 63 | string marker; |
| 64 | if ( match_begin(line, marker) ) { | ||
| 65 | chunks.emplace_back(marker); | ||
| 20 | } | 66 | } |
| 21 | else if ( match_end(line) ) { | 67 | else if ( match_end(line) ) { |
| 68 | if ( !chunks.empty() ) { | ||
| 69 | chunks.pop_back(); | ||
| 70 | } | ||
| 22 | } | 71 | } |
| 23 | else { | 72 | else { |
| 24 | os << line << '\n'; | 73 | for ( const auto& chunk: chunks ) { |
| 74 | container[chunk].emplace_back(line); | ||
| 75 | } | ||
| 25 | } | 76 | } |
| 26 | } | 77 | } |
| 27 | os << flush; | 78 | |
| 79 | return in; | ||
| 80 | } | ||
| 81 | |||
| 82 | ostream& | ||
| 83 | operator<<(ostream& os, const map<string, vector<string>>& container) | ||
| 84 | { | ||
| 85 | for ( const auto& chunk: container ) { | ||
| 86 | os << "CHUNK: " << chunk.first << '\n'; | ||
| 87 | auto line = begin(chunk.second); | ||
| 88 | while ( line != end(chunk.second) ) { | ||
| 89 | os << *line << '\n'; | ||
| 90 | |||
| 91 | ++line; | ||
| 92 | } | ||
| 93 | os << "-------\n\n"; | ||
| 94 | } | ||
| 95 | return os; | ||
| 28 | } | 96 | } |
| 29 | 97 | ||
| 30 | int | 98 | int |
| 31 | main(void) | 99 | main(void) |
| 32 | { | 100 | { |
| 33 | process(cin); | 101 | map<string, vector<string>> container; |
| 102 | |||
| 103 | cin >> container; | ||
| 104 | cout << container; | ||
| 34 | } | 105 | } |
