owlps/owlps-positioner/textfilewriter.cc

79 lines
1.7 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS) project.
* It is subject to the copyright notice and license terms in the
* COPYRIGHT.t2t file found in the top-level directory of this
* distribution and at
* https://code.lm7.fr/mcy/owlps/src/master/COPYRIGHT.t2t
* No part of the OwlPS Project, including this file, may be copied,
* modified, propagated, or distributed except according to the terms
* contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
* distributed along with this file, either separately or by replacing
* this notice by the COPYRIGHT.t2t file's contents.
*/
#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 *** */
/**
* @returns `true` if the string has been written successfuly, or
* `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 ;
}