Instantly share code, notes, and snippets.

Embed
What would you like to do?
Diff inverter
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main(int argc, char* argv[]) {
ifstream file(argv[1]);
string s;
ostringstream sbuf;
bool is_collect = false;
while (getline(file, s)) {
if (s[0] == '-' && (s.size() == 1 || s[1] == ' ')) {
s[0] = '+';
if (!is_collect) {
is_collect = true;
sbuf.str("");
}
sbuf << s << endl;
} else if (s[0] == '+' && (s.size() == 1 || s[1] == ' ')) {
s[0] = '-';
cout << s << endl;
} else {
if (is_collect) {
is_collect = false;
cout << sbuf.str();
}
cout << s << endl;
}
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment