diff --git a/owlps-positioning/src/textfilereader.cc b/owlps-positioning/src/textfilereader.cc index 6ae90c6..9f9f2cb 100644 --- a/owlps-positioning/src/textfilereader.cc +++ b/owlps-positioning/src/textfilereader.cc @@ -1,6 +1,8 @@ #include "textfilereader.hh" #include "posexcept.hh" +#include + using namespace std ; @@ -15,15 +17,26 @@ using namespace std ; TextFileReader::TextFileReader(const string &_file_name): file_name(_file_name), current_line_nb(0) { - file.open(file_name.c_str()) ; - if (! file) - throw error_opening_input_file(file_name) ; + if (file_name == "-") + { + file_buf = file.rdbuf() ; + file.std::ios::rdbuf(std::cin.rdbuf()) ; + } + else + { + file.open(file_name.c_str()) ; + if (! file) + throw error_opening_input_file(file_name) ; + } } TextFileReader::~TextFileReader() { - file.close() ; + if (file_name == "-") + file.std::ios::rdbuf(file_buf) ; + else + file.close() ; } diff --git a/owlps-positioning/src/textfilereader.hh b/owlps-positioning/src/textfilereader.hh index 9a55f53..7b5fdae 100644 --- a/owlps-positioning/src/textfilereader.hh +++ b/owlps-positioning/src/textfilereader.hh @@ -4,12 +4,20 @@ #include #include -/// Read text from a file, line by line +/// Reads text from a file, line by line +/** + * If the file name is a dash ('-'), the standard input is used. + */ class TextFileReader { protected: + /// Name of the input file std::string file_name ; + /// Stream associated with the file std::ifstream file ; +/// Original rdbuf of the stream + std::streambuf *file_buf ; + /// Number of the last line read unsigned int current_line_nb ; /// Checks if the file is readable and closes it if not