|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <iostream> |
|
#include <boost/algorithm/string/predicate.hpp> |
|
#include <boost/iostreams/filter/gzip.hpp> |
|
#include "OutputFileStream.h" |
|
#include "gzfilebuf.h" |
|
|
|
using namespace std; |
|
using namespace boost::algorithm; |
|
|
|
namespace probingpt |
|
{ |
|
OutputFileStream::OutputFileStream() : |
|
boost::iostreams::filtering_ostream(), m_outFile(NULL), m_open(false) |
|
{ |
|
} |
|
|
|
OutputFileStream::OutputFileStream(const std::string &filePath) : |
|
m_outFile(NULL), m_open(false) |
|
{ |
|
Open(filePath); |
|
} |
|
|
|
OutputFileStream::~OutputFileStream() |
|
{ |
|
Close(); |
|
} |
|
|
|
bool OutputFileStream::Open(const std::string &filePath) |
|
{ |
|
assert(!m_open); |
|
if (filePath == std::string("-")) { |
|
|
|
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); |
|
} |
|
|
|
m_open = true; |
|
return true; |
|
} |
|
|
|
void OutputFileStream::Close() |
|
{ |
|
if (!m_open) return; |
|
this->flush(); |
|
if (m_outFile) { |
|
this->pop(); |
|
|
|
m_outFile->close(); |
|
delete m_outFile; |
|
m_outFile = NULL; |
|
} |
|
m_open = false; |
|
} |
|
|
|
} |
|
|
|
|