owlps/owlps-positioning/src/textfilewriter.cc

72 lines
1.2 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS).
* OwlPS is a project of the University of Franche-Comté
* (Université de Franche-Comté), France.
*/
#include "textfilewriter.hh"
#include "configuration.hh"
#include "posexcept.hh"
#include <iostream>
using namespace std ;
/* *** Constructors *** */
/**
* @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.
*/
TextFileWriter::TextFileWriter(const string &_file_name):
file_name(_file_name)
{
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) ;
}
}
TextFileWriter::~TextFileWriter()
{
if (file_name == "-")
file.std::ios::rdbuf(file_buf) ;
else
file.close() ;
}
/* *** Operations *** */
/**
* @return \em true if the string has been written successfuly, or
* \em false if not.
*/
bool TextFileWriter::write_text(const string &text)
{
if (! file)
return false ;
file << text ;
if (Configuration::bool_value("flush-output-files"))
file.flush() ;
return true ;
}