#include #include void help() { cerr << "Usage: filename filename\n"; exit(1); } string process(string tmp) { static bool in_c_comment = false; if (!in_c_comment) { if (tmp.find("/") == string::npos) return tmp; } if (in_c_comment) { if (tmp.find("*/") == string::npos) return string(); unsigned where = tmp.find("*/"); in_c_comment = false; where += 2; // For the star ('*') if (where == tmp.size()) return string(); string str(tmp, where, string::npos); return process(str); } bool in_string = false; for (unsigned position = 0; position < tmp.size(); position++) { if (tmp[position] == '\"' || tmp[position] == '\'') { if (in_string && tmp[position -1] != '\\') // beware of: "\" still in string" in_string = false; if (!in_string) in_string = true; continue; } if (in_string) continue; if (tmp[position] == '/') { if (tmp[position +1] == '/') // C++ type comment { return string(tmp, 0, position); } if (tmp[position +1] == '*') // C type comment { in_c_comment = true; string str(tmp, 0, position); string str2(tmp, position +2, string::npos); return str + process(str2); } } } return tmp; } int main(int argc, char** argv) { if (argc != 3 || !argv[1] || !argv[2]) help(); ifstream in(argv[1]); ofstream out(argv[2]); if (!in || !out) { cerr << "Error opening files!"; exit(2); } string buf; while (getline(in,buf)) out << process(buf) << '\n'; return 0; }