#include #include #include #include #include #include using namespace std; istream& operator>>(istream& in, map>& container) { static constexpr auto begin_marker = "--8<--"; static constexpr auto end_marker = "-->8--"; auto is_comment = [](string_view s) -> bool { return s == "//" || s == "/*" || s == "#" || s == "##" || s == "--"; }; // --8<-- match_begin auto match_begin = [&is_comment](const string& line, string& name) -> bool { stringstream ss{ line }; string comment, cut_mark; if ( !(ss >> comment >> cut_mark >> name) ) { return false; } if ( !is_comment(comment) ) { return false; } if ( cut_mark != begin_marker ) { return false; } return true; }; // -->8-- // --8<-- match_end auto match_end = [&is_comment](const string& line) -> bool { stringstream ss{ line }; string comment, cut_mark; if ( !(ss >> comment >> cut_mark) ) { return false; } if ( !is_comment(comment) ) { return false; } if ( cut_mark != end_marker ) { return false; } return true; }; // -->8-- vector chunks; for ( string line; getline(in, line); ) { string marker; if ( match_begin(line, marker) ) { chunks.emplace_back(marker); } else if ( match_end(line) ) { if ( !chunks.empty() ) { chunks.pop_back(); } } else { for ( const auto& chunk: chunks ) { container[chunk].emplace_back(line); } } } return in; } ostream& print(ostream& os, const string& key, const map>& container) { const auto it = container.find(key); if ( it != container.end() ) { auto line = begin(it->second); while ( line != end(it->second) ) { os << *line << '\n'; ++line; } } else { os << "ERROR: " << key << " not found!" << endl; } return os; } int main(int argc, char* argv[]) { map> container; cin >> container; for ( int i = 1; i < argc; ++i ) { print(cout, argv[i], container); } }