[Positioning] TextFileWriter: '-' for stdout
The user can now use '-' as file name to write results or logs to stdout.
This commit is contained in:
parent
ed6001a05b
commit
d25aa4276b
|
@ -2,6 +2,8 @@
|
||||||
#include "configuration.hh"
|
#include "configuration.hh"
|
||||||
#include "posexcept.hh"
|
#include "posexcept.hh"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
using namespace std ;
|
using namespace std ;
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,21 +12,33 @@ using namespace std ;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param _file_name The name of the file to open.
|
* @param _file_name The name of the file to open ('-' for the standard
|
||||||
|
* output).
|
||||||
* @throw error_opening_input_file if the file cannot be opened.
|
* @throw error_opening_input_file if the file cannot be opened.
|
||||||
*/
|
*/
|
||||||
TextFileWriter::TextFileWriter(const string &_file_name):
|
TextFileWriter::TextFileWriter(const string &_file_name):
|
||||||
file_name(_file_name)
|
file_name(_file_name)
|
||||||
{
|
{
|
||||||
file.open(file_name.c_str()) ;
|
if (file_name == "-")
|
||||||
if (! file)
|
{
|
||||||
throw error_opening_output_file(file_name) ;
|
file_buf = file.rdbuf() ;
|
||||||
|
file.std::ios::rdbuf(std::cout.rdbuf()) ;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
file.open(file_name.c_str()) ;
|
||||||
|
if (! file)
|
||||||
|
throw error_opening_output_file(file_name) ;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TextFileWriter::~TextFileWriter()
|
TextFileWriter::~TextFileWriter()
|
||||||
{
|
{
|
||||||
file.close() ;
|
if (file_name == "-")
|
||||||
|
file.std::ios::rdbuf(file_buf) ;
|
||||||
|
else
|
||||||
|
file.close() ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,12 +4,19 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
/// Write text to a file
|
/// Writes text to a file
|
||||||
|
/**
|
||||||
|
* If the file name is a dash ('-'), the standard output is used.
|
||||||
|
*/
|
||||||
class TextFileWriter
|
class TextFileWriter
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
/// Name of the output file
|
||||||
std::string file_name ;
|
std::string file_name ;
|
||||||
|
/// Stream associated with the file
|
||||||
std::ofstream file ;
|
std::ofstream file ;
|
||||||
|
/// Original rdbuf of the stream
|
||||||
|
std::streambuf *file_buf ;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TextFileWriter(const std::string &_file_name) ;
|
TextFileWriter(const std::string &_file_name) ;
|
||||||
|
|
Loading…
Reference in New Issue