commit 41e5560a7db598d947917746e9e6ef350321afd3 Author: Tanmai Khanna Date: Tue Aug 20 23:33:27 2019 +0530 Added option to give non standard input and output | Updated help message diff --git a/src/anaphora.cc b/src/anaphora.cc index 236b8f5..a11ae4f 100644 --- a/src/anaphora.cc +++ b/src/anaphora.cc @@ -36,10 +36,35 @@ using namespace std; +FILE * open_input(string const &filename) +{ + FILE *input = fopen(filename.c_str(), "r"); + if(!input) + { + wcerr << "Error: can't open input file '"; + wcerr << filename.c_str() << "'." << endl; + exit(EXIT_FAILURE); + } + + return input; +} + +FILE * open_output(string const &filename) +{ + FILE *output = fopen(filename.c_str(), "w"); + if(!output) + { + wcerr << "Error: can't open output file '"; + wcerr << filename.c_str() << "'." << endl; + exit(EXIT_FAILURE); + } + return output; +} + void help_message(char *progname) { - wcerr << "USAGE: " << basename(progname) << " arx_file" << endl; - wcerr << " " << basename(progname) << " -z arx_file" << endl; + wcerr << "USAGE: " << basename(progname) << " arx_file [input [output]]" << endl; + wcerr << " " << basename(progname) << " -z arx_file [input [output]]" << endl; wcerr << " arx_file Anaphora Resolution rules file (apertium-xxx-yyy.xxx-yyy.arx)" << endl; //wcerr << " input input file, standard input by default" << endl; @@ -48,7 +73,7 @@ void help_message(char *progname) wcerr << " -z null-flushing output on \\0" << endl; wcerr << " -h shows this message" << endl; - return 1; //return 1 if error + exit(EXIT_FAILURE); } static int debug_flag; //flag set by --debug @@ -112,10 +137,29 @@ int main(int argc, char **argv) if(debug_flag) fprintf(stderr, "Debug Flag is set.\n"); - if(argc - optind != 1) - help_message(argv[0]); + FILE *input = stdin, *output = stdout; + + switch(argc - optind) + { + case 1: // if only one argument left, it has to be the arx_file + arxFileName = argv[argc - 1]; + break; - arxFileName = argv[optind]; //Name of Arx File is the remaining argument + case 2: // if two arguments, it has to be arx_file and input_file + arxFileName = argv[argc - 2]; + input = open_input(argv[argc - 1]); + break; + + case 3: // if three arguments, it has to be arx_file, input file and output file + arxFileName = argv[argc - 3]; + input = open_input(argv[argc - 2]); + output = open_output(argv[argc - 1]); + break; + + default: + help_message(argv[0]); + break; + } wchar_t input_char; @@ -135,9 +179,9 @@ int main(int argc, char **argv) int flag_LU = 0; - input_char = fgetwc(stdin); + input_char = fgetwc(input); - while(input_char!=EOF) // should I made feof(input_char) ? + while(input_char!=EOF) { if(nullFlush && input_char == L'\0') //nullFlush { @@ -158,20 +202,20 @@ int main(int argc, char **argv) { if(flag_LU == 0) // not inside LU { - fputwc(input_char, stdout); + fputwc(input_char, output); - input_char = fgetwc(stdin); + input_char = fgetwc(input); - fputwc(input_char, stdout); + fputwc(input_char, output); } else //inside LU { input_stream.push_back(input_char); - fputwc(input_char, stdout); + fputwc(input_char, output); - input_char = fgetwc(stdin); + input_char = fgetwc(input); - fputwc(input_char, stdout); + fputwc(input_char, output); input_stream.push_back(input_char); } } @@ -179,7 +223,7 @@ int main(int argc, char **argv) { if(flag_LU == 0) //Not Part of an LU { - fputwc(input_char, stdout); + fputwc(input_char, output); if(input_char == L'^') flag_LU = 1; @@ -191,7 +235,7 @@ int main(int argc, char **argv) { gen_id++; //generate ids for LUs - fputwc(L'/', stdout); //for adding ref + fputwc(L'/', output); //for adding ref flag_LU = 0; @@ -214,7 +258,7 @@ int main(int argc, char **argv) { final_ref = score_module.get_antecedent(); - fputws(final_ref.c_str(), stdout); //add antecedent to side ref of LU + fputws(final_ref.c_str(), output); //add antecedent to side ref of LU } } @@ -225,12 +269,12 @@ int main(int argc, char **argv) input_stream.push_back(input_char); } - fputwc(input_char, stdout); + fputwc(input_char, output); } } - input_char = fgetwc(stdin); + input_char = fgetwc(input); } //fclose(fin); diff --git a/src/parse_arx.cc b/src/parse_arx.cc index 99477e2..05b6614 100644 --- a/src/parse_arx.cc +++ b/src/parse_arx.cc @@ -28,7 +28,7 @@ #include #include -void print_tags(const vector& input) +void print_tags(const vector& input) //testing function { for (size_t i = 0; i < input.size(); ++i) { diff --git a/src/pattern_arx.cc b/src/pattern_arx.cc index 2232e9c..db633c9 100644 --- a/src/pattern_arx.cc +++ b/src/pattern_arx.cc @@ -27,7 +27,7 @@ using namespace std; -void print_markable(acceptable_patterns inp) +void print_markable(acceptable_patterns inp) //testing function { for(acceptable_patterns::iterator i = inp.begin(); i != inp.end(); i++) {