00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <iostream>
00023 #include <boost/algorithm/string/predicate.hpp>
00024 #include <boost/iostreams/filter/gzip.hpp>
00025 #include "OutputFileStream.h"
00026 #include "gzfilebuf.h"
00027
00028 using namespace std;
00029 using namespace boost::algorithm;
00030
00031 namespace Moses
00032 {
00033 OutputFileStream::OutputFileStream()
00034 :boost::iostreams::filtering_ostream()
00035 ,m_outFile(NULL)
00036 ,m_open(false)
00037 {
00038 }
00039
00040 OutputFileStream::OutputFileStream(const std::string &filePath)
00041 :m_outFile(NULL)
00042 ,m_open(false)
00043 {
00044 Open(filePath);
00045 }
00046
00047 OutputFileStream::~OutputFileStream()
00048 {
00049 Close();
00050 }
00051
00052 bool OutputFileStream::Open(const std::string &filePath)
00053 {
00054 assert(!m_open);
00055 if (filePath == std::string("-")) {
00056
00057 this->push(std::cout);
00058 } else {
00059 m_outFile = new ofstream(filePath.c_str(), ios_base::out | ios_base::binary);
00060 if (m_outFile->fail()) {
00061 return false;
00062 }
00063
00064 if (ends_with(filePath, ".gz")) {
00065 this->push(boost::iostreams::gzip_compressor());
00066 }
00067 this->push(*m_outFile);
00068 }
00069
00070 m_open = true;
00071 return true;
00072 }
00073
00074 void OutputFileStream::Close()
00075 {
00076 if (!m_open) return;
00077 this->flush();
00078 if (m_outFile) {
00079 this->pop();
00080
00081 m_outFile->close();
00082 delete m_outFile;
00083 m_outFile = NULL;
00084 }
00085 m_open = false;
00086 }
00087
00088
00089 }
00090