code
This commit is contained in:
parent
f45e129797
commit
6d2d778231
123
owo.cpp
Normal file
123
owo.cpp
Normal file
@ -0,0 +1,123 @@
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
|
||||
string replacements[6][2] = {
|
||||
{"(?:r|l)", "w"},
|
||||
{"(?:R|L)", "W"},
|
||||
{"n([aeiou])", "ny$1"},
|
||||
{"N([aeiou])", "Ny$1"},
|
||||
{"N([AEIOU])", "Ny$1"},
|
||||
{"ove", "uv"}
|
||||
};
|
||||
|
||||
string owo(string input) {
|
||||
string output = input;
|
||||
for (int i = 0; i < 6; i++) {
|
||||
output = regex_replace(output, regex(replacements[i][0]), replacements[i][1]);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
int help() {
|
||||
cout << "Usage: owo [-p] [-h] [-f file] [-o file] [text]" << endl;
|
||||
cout << " owo [text]" << endl;
|
||||
cout << " owo -f [file] -o [file]" << endl;
|
||||
cout << " echo [text] | owo -p" << endl << endl;
|
||||
cout << "Options:" << endl;
|
||||
cout << " -p Read from stdin" << endl;
|
||||
cout << " -h Show this help message" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
string input, inputfile, outputfile;
|
||||
bool stdin = false;
|
||||
|
||||
if (argc < 2) {
|
||||
cout << owo("No text provided") << endl << endl;
|
||||
help();
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (string(argv[i]) == "-f") {
|
||||
inputfile = string(argv[i+1]);
|
||||
i++;
|
||||
} else if (string(argv[i]) == "-o") {
|
||||
outputfile = string(argv[i+1]);
|
||||
i++;
|
||||
} else if (string(argv[i]) == "-p") {
|
||||
stdin = true;
|
||||
} else if (string(argv[i]) == "-h") {
|
||||
return help();
|
||||
} else if (i == argc-1) {
|
||||
input = string(argv[i]);
|
||||
} else {
|
||||
cout << owo("Invalid argument: " + string(argv[i])) << endl << endl;
|
||||
help();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (stdin && inputfile != "" || stdin && input != "" || inputfile != "" && input != "") {
|
||||
cout << owo("Cannot use multiple input methods at once") << endl << endl;
|
||||
help();
|
||||
return 1;
|
||||
}
|
||||
|
||||
ostream *output;
|
||||
|
||||
if (outputfile != "") {
|
||||
output = new ofstream(outputfile);
|
||||
if (!output->good()) {
|
||||
cout << owo("Failed to open output file") << endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (stdin) {
|
||||
while (getline(cin, input)) {
|
||||
if (outputfile != "") {
|
||||
*output << owo(input) << endl;
|
||||
} else {
|
||||
cout << owo(input) << endl;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (inputfile != "") {
|
||||
ifstream inputstream(inputfile);
|
||||
if (!inputstream.good()) {
|
||||
cout << owo("Failed to open input file") << endl;
|
||||
if (outputfile != "") {
|
||||
((ofstream*)output)->close();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (getline(inputstream, input)) {
|
||||
if (outputfile != "") {
|
||||
*output << owo(input) << endl;
|
||||
} else {
|
||||
cout << owo(input) << endl;
|
||||
}
|
||||
}
|
||||
inputstream.close();
|
||||
|
||||
} else if (input != "") {
|
||||
if (outputfile != "") {
|
||||
*output << owo(input) << endl;
|
||||
} else {
|
||||
cout << owo(input) << endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (outputfile != "") {
|
||||
((ofstream*)output)->close();
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user