Skip to content

Instantly share code, notes, and snippets.

@highwaycoder
Forked from Ratstail91/palindrome.cpp
Last active December 14, 2015 10:18
Show Gist options
  • Save highwaycoder/5070617 to your computer and use it in GitHub Desktop.
Save highwaycoder/5070617 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " INPUTFILE" << endl;
return 0;
}
ifstream is(argv[1]);
if (!is.is_open()) {
cerr << "Failed to open the input file: " << argv[1] << endl;
return 1;
}
// this could be sharpened up a bit and made to convert from .c to .out instead of just appending .out...
std::string out_filename(argv[1]);
out_filename += '.out';
ofstream os(out_filename);
if (!os.is_open()) {
is.close();
cerr << "Failed to open the output file: " << argv[2] << endl;
return 1;
}
// the addition of // is redundant in both directions, but whatever
os << "#define foo // fidne#" << endl;
vector<string> store;
while(!is.eof()) {
string s;
getline(is, s);
store.push_back(s);
os << s << endl;
}
is.close();
os << "#if 0" << endl << "0 fi#" << endl;
for (vector<string>::reverse_iterator it = store.rbegin(); it != store.rend(); it++) {
for (string::reverse_iterator sit = it->rbegin(); sit != it->rend(); sit++) {
os << *sit;
}
/* - the below only breaks the program on unix, because it creates files that don't end in a blank line, which makes 'cat' screwy
store.pop_back();
if (store.size()) {
os << endl;
}
*/
}
// again, the // is redundant but the reason I put it there is more obvious here
os << endl << "#endif // oof enifed#";
os.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment