2012-02-06 16:44:09 +01:00
|
|
|
/*
|
2013-05-29 18:43:35 +02:00
|
|
|
* 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
|
|
|
|
* http://code.lm7.fr/p/owlps/source/tree/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
|
2013-06-22 23:30:17 +02:00
|
|
|
* this notice by the COPYRIGHT.t2t file's contents.
|
2012-02-06 16:44:09 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
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 *** */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-05-30 21:23:05 +02:00
|
|
|
* @returns `true` if the string has been written successfuly, or
|
|
|
|
* `false` if not.
|
2010-03-08 17:23:20 +01:00
|
|
|
*/
|
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 ;
|
|
|
|
}
|