#include #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 { cerr << "ERROR: " << key << " not found!" << endl; } return os; } int main(int argc, char* argv[]) { int show_tags = 0; int ch; while ( (ch = getopt(argc, argv, "t")) != -1 ) { switch ( ch ) { case 't': show_tags = 1; break; default: cerr << "unbekannte option" << endl; return EXIT_FAILURE; } } argc -= optind; argv += optind; map> container; cin >> container; if ( show_tags ) { for ( const auto& it: container ) { cout << it.first << endl; } } else { for ( int i = 0; i < argc; ++i ) { print(cout, argv[i], container); } } }