polish: interactive function added

This commit is contained in:
kuoi 2023-07-17 05:35:19 +08:00
parent 39eaf9d7a5
commit 92c7918df5
2 changed files with 167 additions and 39 deletions

15
CMakeLists.txt Normal file
View file

@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.0)
project(tnt2winclada)
SET( CMAKE_EXPORT_COMPILE_COMMANDS ON )
set(CMAKE_CXX_STANDARD 11)
add_executable(tnt2winclada tnt2winclada.cpp)
target_compile_options(tnt2winclada PRIVATE -Wall -Wextra -pedantic)
install(TARGETS tnt2winclada DESTINATION /usr/bin)
install(FILES guoyi.run DESTINATION /usr/share/tnt/tnt_scripts)
install(FILES LICENSE DESTINATION /usr/share/licenses/tnt2winclada)
install(FILES README.md DESTINATION /usr/share/doc/tnt2winclada)

View file

@ -1,6 +1,6 @@
#include <iostream>
#include <fstream> #include <fstream>
#include <sstream> #include <iostream>
#include <sstream>
#include <string> #include <string>
using namespace std; using namespace std;
@ -10,30 +10,124 @@ string inputFileName, outputFileName, tnt_format, winclada_format;
void argument(int argc, char** argv); void argument(int argc, char** argv);
int read_tnt(); int read_tnt();
void showhelp(int help_num); void showhelp(int help_num);
void parse_line(int trelen); void parse_line();
void add_space (string winclada, char x); void add_space(string winclada, char x);
bool isNum(string strnum); bool isNum(string strnum);
void write_winclada();
void interactive_commands();
void show_help (int help_num){ void interactive_commands()
if (help_num == 0) { {
string command, filename1, filename2;
cout << "~|~|\\ |~|~ ~) | |o._ |~| _ _| _ " << endl; cout << "~|~|\\ |~|~ ~) | |o._ |~| _ _| _ " << endl;
cout << " | | \\| | /_ \\/\\/ || ||_|(_|(_|(_|" << endl; cout << " | | \\| | /_ \\/\\/ || ||_|(_|(_|(_|" << endl;
cout << "TNT2WinClada\nMIT, Guoyi Zhang, 2023\nplease use -h to see more options" << endl; cout << "TNT2WinClada\nMIT, Guoyi Zhang, 2023\nplease type help "
} else { "to see more commands"
cout << "~|~|\\ |~|~ ~) | |o._ |~| _ _| _ " << endl; << endl;
cout << " | | \\| | /_ \\/\\/ || ||_|(_|(_|(_|" << endl; while (true) {
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; 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 "
"winclada accessable tree file\nquit\t\t\texit "
"the program"
<< 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 {
ifstream infile(filename1.c_str());
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 {
ofstream outfile(filename2.c_str());
outputFileName = filename2;
cout << "tnt2winclada> Output file set to "
<< filename2 << endl;
write_winclada();
cout << "tnt2winclada> Finished." << endl;
}
}
else if (command == "quit") {
break;
}
else {
cout << "tnt2wincalda> unrecognized command. Type "
"'help' for "
"a list of commands."
<< endl;
}
} }
} }
void argument(int argc, char **argv) { void show_help(int help_num)
{
if (help_num == 0) {
cout << "~|~|\\ |~|~ ~) | |o._ |~| _ _| _ " << endl;
cout << " | | \\| | /_ \\/\\/ || ||_|(_|(_|(_|" << endl;
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;
}
}
void argument(int argc, char** argv)
{
// -i -h -o option // -i -h -o option
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
if (argc == 2 && string(argv[1]) == "-h") { if (argc == 2 && string(argv[1]) == "-h") {
show_help(1); show_help(1);
exit(0); exit(0);
} }
if (argc != 5 || string(argv[1]) != "-i" || string(argv[3]) != "-o") { if (argc != 5 || string(argv[1]) != "-i" ||
string(argv[3]) != "-o") {
cout << "input or output can't be empty." << endl; cout << "input or output can't be empty." << endl;
show_help(0); show_help(0);
exit(0); exit(0);
@ -49,42 +143,48 @@ void argument(int argc, char **argv) {
} }
} }
int read_tnt() { int read_tnt()
int trelen; {
int trelen = 0;
ifstream inputFile(inputFileName); ifstream inputFile(inputFileName);
if (inputFile.is_open()) { if (inputFile.is_open()) {
getline(inputFile, tnt_format); getline(inputFile, tnt_format);
inputFile.close(); inputFile.close();
trelen = tnt_format.length(); trelen = tnt_format.length();
} else { }
else {
cout << "File can't be opened" << endl; cout << "File can't be opened" << endl;
} }
inputFile.close(); inputFile.close();
return trelen; return trelen;
} }
void add_space (char x) { void add_space(char x)
{
int i = 0; int i = 0;
string winclada; string winclada;
for (i=0; i<(int)tnt_format.length(); i++) { for (i = 0; i < (int)tnt_format.length(); i++) {
if (tnt_format[i] != x) { if (tnt_format[i] != x) {
winclada=winclada+tnt_format[i]; winclada = winclada + tnt_format[i];
} else { }
winclada=winclada+" "+tnt_format[i]+" "; else {
} winclada = winclada + " " + tnt_format[i] + " ";
}
} }
tnt_format=winclada; tnt_format = winclada;
} }
bool isNum(string strnum) { bool isNum(string strnum)
{
char* p; char* p;
strtol(strnum.c_str(), &p, 10); strtol(strnum.c_str(), &p, 10);
return *p == 0; return *p == 0;
} }
void parse_line (int trelen) { void parse_line()
{
// add space // add space
char x1 = '(', x2 = ')', x3= ','; char x1 = '(', x2 = ')', x3 = ',';
add_space(x1); add_space(x1);
add_space(x2); add_space(x2);
add_space(x3); add_space(x3);
@ -94,31 +194,44 @@ void parse_line (int trelen) {
int tem; int tem;
while (getline(istr, buffer, ' ')) { while (getline(istr, buffer, ' ')) {
istr >> buffer; istr >> buffer;
if(isNum(buffer)){ if (isNum(buffer)) {
tem = stoi(buffer)-1; tem = stoi(buffer) - 1;
buffer = to_string(tem); buffer = to_string(tem);
} }
winclada_format=winclada_format+buffer; winclada_format = winclada_format + buffer;
} }
} }
void write_winclada() { void write_winclada()
{
ofstream outputFile(outputFileName); ofstream outputFile(outputFileName);
if (outputFile.is_open()) { if (outputFile.is_open()) {
outputFile << "tread" << endl; outputFile << "tread" << endl;
outputFile << winclada_format << endl; outputFile << winclada_format << endl;
outputFile << "proc-;" ; outputFile << "proc-;";
} else { }
else {
cout << "File can't be written" << endl; cout << "File can't be written" << endl;
} }
outputFile.close(); outputFile.close();
} }
int main (int argc, char **argv){ int main(int argc, char** argv)
argument(argc, argv); {
int trelen; if (argc == 1) {
trelen = read_tnt(); interactive_commands();
parse_line(trelen); }
write_winclada(); else {
return 0; 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;
}
}
} }