TNT_Script/tnt2winclada.cpp

238 lines
5 KiB
C++
Raw Normal View History

#include <fstream>
2023-07-17 05:35:19 +08:00
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string inputFileName, outputFileName, tnt_format, winclada_format;
void argument(int argc, char** argv);
int read_tnt();
void showhelp(int help_num);
2023-07-17 05:35:19 +08:00
void parse_line();
void add_space(string winclada, char x);
bool isNum(string strnum);
2023-07-17 05:35:19 +08:00
void write_winclada();
void interactive_commands();
2023-07-17 05:35:19 +08:00
void interactive_commands()
{
string command, filename1, filename2;
cout << "~|~|\\ |~|~ ~) | |o._ |~| _ _| _ " << endl;
cout << " | | \\| | /_ \\/\\/ || ||_|(_|(_|(_|" << endl;
2023-07-17 05:35:19 +08:00
cout << "TNT2WinClada\nMIT, Guoyi Zhang, 2023\nplease type help "
"to see more commands"
<< endl;
while (true) {
cout << "tnt2winclada> ";
getline(cin, command);
if (command == "help") {
cout << "help\t\t\tshow interactive commands "
"help\ninput "
"<filename>\tinput a TNT output tree from the "
"specified file\noutput <filename>\toutput a "
2023-07-17 15:41:22 +08:00
"winclada accessible tree file\nquit\t\t\tquit "
2023-07-17 06:50:26 +08:00
"the program\nexit\t\t\texit the program"
2023-07-17 05:35:19 +08:00
<< endl;
}
else if (command.find("input ") == 0) {
std::stringstream ss(command.substr(6));
ss >> filename1;
if (filename1.empty()) {
cout << "tnt2winclada> please specify a "
"filename."
<< endl;
}
else {
2023-07-17 06:43:44 +08:00
ifstream infile(filename1);
2023-07-17 05:35:19 +08:00
if (infile.good()) {
inputFileName = filename1;
cout << "tnt2winclada> Input file set "
"to "
<< filename1 << endl;
int trelen;
trelen = read_tnt();
if (trelen == 0) {
cout << "tnt2winclada> Input "
"format occurs error"
<< endl;
}
else {
parse_line();
}
}
else {
cout << "tnt2winclada> file not found. "
"Please "
"try again."
<< endl;
}
}
}
else if (command.find("output ") == 0) {
std::stringstream ss(command.substr(7));
ss >> filename2;
if (filename2.empty()) {
cout << "tnt2winclada> please specify a "
"filename."
<< endl;
}
else {
2023-07-17 06:43:44 +08:00
ofstream outfile(filename2);
2023-07-17 05:35:19 +08:00
outputFileName = filename2;
cout << "tnt2winclada> Output file set to "
<< filename2 << endl;
write_winclada();
cout << "tnt2winclada> Finished." << endl;
}
}
2023-07-17 06:50:26 +08:00
else if (command == "quit" || command == "exit") {
2023-07-17 05:35:19 +08:00
break;
}
else {
cout << "tnt2wincalda> unrecognized command. Type "
"'help' for "
"a list of commands."
<< endl;
}
}
}
void show_help(int help_num)
{
if (help_num == 0) {
cout << "~|~|\\ |~|~ ~) | |o._ |~| _ _| _ " << endl;
cout << " | | \\| | /_ \\/\\/ || ||_|(_|(_|(_|" << endl;
2023-07-17 05:35:19 +08:00
cout << "TNT2WinClada\nMIT, Guoyi Zhang, 2023\nplease use -h "
"to see more options"
<< endl;
}
else {
cout << "~|~|\\ |~|~ ~) | |o._ |~| _ _| _ " << endl;
cout << " | | \\| | /_ \\/\\/ || ||_|(_|(_|(_|" << endl;
cout << "Usage: tnt2winclada -i ${input_file} -o "
"${output_file}\n-h help;\n-i input: -i "
"${input_file};\n-o output: -o ${output_file}"
<< endl;
}
}
2023-07-17 05:35:19 +08:00
void argument(int argc, char** argv)
{
// -i -h -o option
for (int i = 0; i < argc; i++) {
if (argc == 2 && string(argv[1]) == "-h") {
show_help(1);
2023-02-28 16:02:03 +08:00
exit(0);
}
2023-07-17 05:35:19 +08:00
if (argc != 5 || string(argv[1]) != "-i" ||
string(argv[3]) != "-o") {
2023-02-28 16:02:03 +08:00
cout << "input or output can't be empty." << endl;
show_help(0);
2023-02-28 16:02:03 +08:00
exit(0);
}
if (string(argv[i]) == "-i") {
i++;
inputFileName = argv[i];
}
if (string(argv[i]) == "-o") {
i++;
outputFileName = argv[i];
}
}
}
2023-07-17 05:35:19 +08:00
int read_tnt()
{
int trelen = 0;
ifstream inputFile(inputFileName);
if (inputFile.is_open()) {
getline(inputFile, tnt_format);
inputFile.close();
trelen = tnt_format.length();
2023-07-17 05:35:19 +08:00
}
else {
cout << "File can't be opened" << endl;
}
inputFile.close();
return trelen;
}
2023-07-17 05:35:19 +08:00
void add_space(char x)
{
int i = 0;
string winclada;
2023-07-17 05:35:19 +08:00
for (i = 0; i < (int)tnt_format.length(); i++) {
if (tnt_format[i] != x) {
2023-07-17 05:35:19 +08:00
winclada = winclada + tnt_format[i];
}
else {
winclada = winclada + " " + tnt_format[i] + " ";
}
}
2023-07-17 05:35:19 +08:00
tnt_format = winclada;
}
2023-07-17 05:35:19 +08:00
bool isNum(string strnum)
{
char* p;
strtol(strnum.c_str(), &p, 10);
return *p == 0;
}
2023-07-17 05:35:19 +08:00
void parse_line()
{
// add space
2023-07-17 05:35:19 +08:00
char x1 = '(', x2 = ')', x3 = ',';
add_space(x1);
add_space(x2);
add_space(x3);
// use space as separator
istringstream istr(tnt_format);
string buffer;
int tem;
while (getline(istr, buffer, ' ')) {
istr >> buffer;
2023-07-17 05:35:19 +08:00
if (isNum(buffer)) {
tem = stoi(buffer) - 1;
buffer = to_string(tem);
}
2023-07-17 05:35:19 +08:00
winclada_format = winclada_format + buffer;
}
}
2023-07-17 05:35:19 +08:00
void write_winclada()
{
ofstream outputFile(outputFileName);
if (outputFile.is_open()) {
2023-03-01 19:07:42 +08:00
outputFile << "tread" << endl;
outputFile << winclada_format << endl;
2023-07-17 05:35:19 +08:00
outputFile << "proc-;";
}
else {
cout << "File can't be written" << endl;
}
outputFile.close();
}
2023-07-17 05:35:19 +08:00
int main(int argc, char** argv)
{
if (argc == 1) {
interactive_commands();
}
else {
argument(argc, argv);
int trelen;
trelen = read_tnt();
if (trelen != 0) {
parse_line();
write_winclada();
return 0;
}
else {
cout << "Input file format occurs error." << endl;
}
}
}