[Positioning] TextFileReader: '-' for stdin
The user can now use '-' as file name to read inputs from stdin.
This commit is contained in:
parent
d25aa4276b
commit
2467c3261e
|
@ -1,6 +1,8 @@
|
|||
#include "textfilereader.hh"
|
||||
#include "posexcept.hh"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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() ;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -4,12 +4,20 @@
|
|||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
/// 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
|
||||
|
|
Loading…
Reference in New Issue