2010-03-09 12:04:00 +01:00
|
|
|
#include "textfilewriter.hh"
|
2011-03-29 14:21:14 +02:00
|
|
|
#include "configuration.hh"
|
2010-03-08 17:23:20 +01:00
|
|
|
#include "posexcept.hh"
|
|
|
|
|
2011-04-06 11:12:23 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2010-03-08 17:23:20 +01:00
|
|
|
using namespace std ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* *** Constructors *** */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-04-06 11:12:23 +02:00
|
|
|
* @param _file_name The name of the file to open ('-' for the standard
|
|
|
|
* output).
|
2010-03-08 17:23:20 +01:00
|
|
|
* @throw error_opening_input_file if the file cannot be opened.
|
|
|
|
*/
|
2010-03-09 12:04:00 +01:00
|
|
|
TextFileWriter::TextFileWriter(const string &_file_name):
|
2010-03-08 17:23:20 +01:00
|
|
|
file_name(_file_name)
|
|
|
|
{
|
2011-04-06 11:12:23 +02:00
|
|
|
if (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) ;
|
|
|
|
}
|
2010-03-08 17:23:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-09 12:04:00 +01:00
|
|
|
TextFileWriter::~TextFileWriter()
|
2010-03-08 17:23:20 +01:00
|
|
|
{
|
2011-04-06 11:12:23 +02:00
|
|
|
if (file_name == "-")
|
|
|
|
file.std::ios::rdbuf(file_buf) ;
|
|
|
|
else
|
|
|
|
file.close() ;
|
2010-03-08 17:23:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true if the string has been written successfuly, or false
|
|
|
|
* if not.
|
|
|
|
*/
|
2010-03-09 12:04:00 +01:00
|
|
|
bool TextFileWriter::write_text(const string &text)
|
2010-03-08 17:23:20 +01:00
|
|
|
{
|
|
|
|
if (! file)
|
|
|
|
return false ;
|
|
|
|
|
2010-06-23 10:32:29 +02:00
|
|
|
file << text ;
|
2011-03-29 14:21:14 +02:00
|
|
|
|
|
|
|
if (Configuration::bool_value("flush-output-files"))
|
|
|
|
file.flush() ;
|
|
|
|
|
2010-03-08 17:23:20 +01:00
|
|
|
return true ;
|
|
|
|
}
|