Created
August 29, 2016 09:54
-
-
Save Hikari9/777a7d70dc8da7701a3d05fba62f1fd5 to your computer and use it in GitHub Desktop.
Windows Alias
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main(int argc, char *args[]) { | |
if (argc < 3) { | |
invalid: cout << "usage: alias name \"command\"" << endl; | |
return 0; | |
} | |
string name = args[1]; | |
string command = args[2]; | |
// escape string | |
string buffer; | |
for (char c : command) { | |
if (c == '\\') | |
buffer += "\\\\"; | |
else if (c == '"') | |
buffer += "\\\""; | |
else | |
buffer += c; | |
} | |
command = buffer; | |
ofstream file("tmp.cpp"); | |
file << "#include <iostream>" << endl; | |
file << "using namespace std;" << endl; | |
file << "int main(int argc, char *args[]) {" << endl; | |
file << "\tstring s = \"" << command << "\";" << endl; | |
file << "\tfor (int i = 1; i < argc; ++i) {" << endl; | |
file << "\t\ts.append(\" \");" << endl; | |
file << "\t\ts.append(args[i]);" << endl; | |
file << "\t}" << endl; | |
file << "\tsystem(s.c_str());" << endl; | |
file << "}" << endl; | |
file.close(); | |
cout << "Compiling..." << endl; | |
string path = "C:\\Users\\rico\\AppData\\"; | |
path.append(name); | |
path.append(".exe"); | |
system(("g++ tmp.cpp -o \"" + path + "\"").c_str()); | |
cout << "Cleaning..." << endl; | |
system("rm tmp.cpp"); | |
cout << "Compiled to " << path << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment