Skip to content

Instantly share code, notes, and snippets.

@itarato
Created September 14, 2017 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itarato/05516838e119539db3e326174f3a3e8b to your computer and use it in GitHub Desktop.
Save itarato/05516838e119539db3e326174f3a3e8b to your computer and use it in GitHub Desktop.
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