mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-10-05 15:58:03 +03:00
OutputFileStream: accept ‘-’ for “stdout”.
This is a common convention: when a program gets a dash as the path of a file that it should write, it writes to standard output instead. Enhances portability to systems that don't have /dev/stdout.
This commit is contained in:
parent
f6f56d11af
commit
ea9b097aba
@ -19,6 +19,7 @@
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
***********************************************************************/
|
||||
|
||||
#include <iostream>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/iostreams/filter/gzip.hpp>
|
||||
#include "OutputFileStream.h"
|
||||
@ -32,11 +33,13 @@ namespace Moses
|
||||
OutputFileStream::OutputFileStream()
|
||||
:boost::iostreams::filtering_ostream()
|
||||
,m_outFile(NULL)
|
||||
,m_open(false)
|
||||
{
|
||||
}
|
||||
|
||||
OutputFileStream::OutputFileStream(const std::string &filePath)
|
||||
: m_outFile(NULL)
|
||||
:m_outFile(NULL)
|
||||
,m_open(false)
|
||||
{
|
||||
Open(filePath);
|
||||
}
|
||||
@ -48,32 +51,38 @@ OutputFileStream::~OutputFileStream()
|
||||
|
||||
bool OutputFileStream::Open(const std::string &filePath)
|
||||
{
|
||||
m_outFile = new ofstream(filePath.c_str(), ios_base::out | ios_base::binary);
|
||||
if (m_outFile->fail()) {
|
||||
return false;
|
||||
assert(!m_open);
|
||||
if (filePath == std::string("-")) {
|
||||
// Write to standard output. Leave m_outFile null.
|
||||
this->push(std::cout);
|
||||
} else {
|
||||
m_outFile = new ofstream(filePath.c_str(), ios_base::out | ios_base::binary);
|
||||
if (m_outFile->fail()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ends_with(filePath, ".gz")) {
|
||||
this->push(boost::iostreams::gzip_compressor());
|
||||
}
|
||||
this->push(*m_outFile);
|
||||
}
|
||||
|
||||
if (ends_with(filePath, ".gz")) {
|
||||
this->push(boost::iostreams::gzip_compressor());
|
||||
}
|
||||
this->push(*m_outFile);
|
||||
|
||||
m_open = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void OutputFileStream::Close()
|
||||
{
|
||||
if (m_outFile == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_open) return;
|
||||
this->flush();
|
||||
this->pop(); // file
|
||||
if (m_outFile) {
|
||||
this->pop(); // file
|
||||
|
||||
m_outFile->close();
|
||||
delete m_outFile;
|
||||
m_outFile = NULL;
|
||||
return;
|
||||
m_outFile->close();
|
||||
delete m_outFile;
|
||||
m_outFile = NULL;
|
||||
}
|
||||
m_open = false;
|
||||
}
|
||||
|
||||
|
||||
|
@ -30,19 +30,50 @@
|
||||
namespace Moses
|
||||
{
|
||||
|
||||
/** Used in place of std::istream, can read zipped files if it ends in .gz
|
||||
/** Version of std::ostream with transparent compression.
|
||||
*
|
||||
* Transparently compresses output when writing to a file whose name ends in
|
||||
* ".gz". Or, writes to stdout instead of a file when given a filename
|
||||
* consisting of just a dash ("-").
|
||||
*/
|
||||
class OutputFileStream : public boost::iostreams::filtering_ostream
|
||||
{
|
||||
protected:
|
||||
private:
|
||||
/** File that needs flushing & closing when we close this stream.
|
||||
*
|
||||
* Is NULL when no file is opened, e.g. when writing to standard output.
|
||||
*/
|
||||
std::ofstream *m_outFile;
|
||||
|
||||
/// Is this stream open?
|
||||
bool m_open;
|
||||
|
||||
public:
|
||||
/** Create an unopened OutputFileStream.
|
||||
*
|
||||
* Until it's been opened, nothing can be done with this stream.
|
||||
*/
|
||||
OutputFileStream();
|
||||
|
||||
/// Create an OutputFileStream, and open it by calling Open().
|
||||
OutputFileStream(const std::string &filePath);
|
||||
virtual ~OutputFileStream();
|
||||
|
||||
// TODO: Can we please just always throw an exception when this fails?
|
||||
/** Open stream.
|
||||
*
|
||||
* If filePath is "-" (just a dash), this opens the stream for writing to
|
||||
* standard output. Otherwise, it opens the given file. If the filename
|
||||
* has the ".gz" suffix, output will be transparently compressed.
|
||||
*
|
||||
* Call Close() to close the file.
|
||||
*
|
||||
* Returns whether opening the file was successful. It may also throw an
|
||||
* exception on failure.
|
||||
*/
|
||||
bool Open(const std::string &filePath);
|
||||
|
||||
/// Flush and close stream. After this, the stream can be opened again.
|
||||
void Close();
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user