From ebc33f63e97dba52e9f063b836e0ca693dae797b Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Thu, 10 May 2012 23:49:15 +0100 Subject: [PATCH] closing gz file --- scripts/training/phrase-extract/Jamfile | 2 +- .../phrase-extract/OutputFileStream.cpp | 22 +++++++++++++++---- .../phrase-extract/OutputFileStream.h | 1 + scripts/training/phrase-extract/score.cpp | 11 ++++------ 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/scripts/training/phrase-extract/Jamfile b/scripts/training/phrase-extract/Jamfile index 995cf3fbf..496a7a6c1 100644 --- a/scripts/training/phrase-extract/Jamfile +++ b/scripts/training/phrase-extract/Jamfile @@ -12,7 +12,7 @@ exe extract-rules : tables-core.o SentenceAlignment.cpp SentenceAlignmentWithSyn exe extract-lex : extract-lex.cpp InputFileStream ; -exe score : tables-core.o AlignmentPhrase.o score.cpp PhraseAlignment.cpp InputFileStream ../../..//boost_iostreams ; +exe score : tables-core.o AlignmentPhrase.o score.cpp PhraseAlignment.cpp OutputFileStream.cpp InputFileStream ../../..//boost_iostreams ; exe consolidate : consolidate.cpp tables-core.o InputFileStream ; diff --git a/scripts/training/phrase-extract/OutputFileStream.cpp b/scripts/training/phrase-extract/OutputFileStream.cpp index becbdc9d4..960917cb1 100644 --- a/scripts/training/phrase-extract/OutputFileStream.cpp +++ b/scripts/training/phrase-extract/OutputFileStream.cpp @@ -30,11 +30,13 @@ namespace Moses OutputFileStream::OutputFileStream() :boost::iostreams::filtering_ostream() ,m_outFile(NULL) + ,m_isGZ(false) { } OutputFileStream::OutputFileStream(const std::string &filePath) : m_outFile(NULL) + , m_isGZ(false) { Open(filePath); } @@ -42,16 +44,16 @@ OutputFileStream::OutputFileStream(const std::string &filePath) OutputFileStream::~OutputFileStream() { Close(); - delete m_outFile; - m_outFile = NULL; } bool OutputFileStream::Open(const std::string &filePath) { m_outFile = new ofstream(filePath.c_str(), ios_base::out | ios_base::binary); - if (filePath.size() > 3 && filePath.substr(filePath.size() - 3, 3) == ".gz") { + if (filePath.size() > 3 && filePath.substr(filePath.size() - 3, 3) == ".gz") + { this->push(boost::iostreams::gzip_compressor()); + m_isGZ = true; } this->push(*m_outFile); @@ -60,7 +62,19 @@ bool OutputFileStream::Open(const std::string &filePath) void OutputFileStream::Close() { - this->pop(); + this->flush(); + if (m_outFile == NULL) { + return; + } + + if (m_isGZ) { + this->pop(); + } + + //this->close(); + m_outFile->close(); + delete m_outFile; + m_outFile = NULL; } diff --git a/scripts/training/phrase-extract/OutputFileStream.h b/scripts/training/phrase-extract/OutputFileStream.h index bc3a1c5cf..b42b9adee 100644 --- a/scripts/training/phrase-extract/OutputFileStream.h +++ b/scripts/training/phrase-extract/OutputFileStream.h @@ -35,6 +35,7 @@ namespace Moses class OutputFileStream : public boost::iostreams::filtering_ostream { protected: + bool m_isGZ; std::ofstream *m_outFile; public: OutputFileStream(); diff --git a/scripts/training/phrase-extract/score.cpp b/scripts/training/phrase-extract/score.cpp index b3dca5d8f..4b036f42c 100644 --- a/scripts/training/phrase-extract/score.cpp +++ b/scripts/training/phrase-extract/score.cpp @@ -190,12 +190,9 @@ int main(int argc, char* argv[]) } else { Moses::OutputFileStream *outputFile = new Moses::OutputFileStream(); - //ofstream *outputFile = new ofstream(); - //outputFile->open(fileNamePhraseTable); - - //if (outputFile->fail()) { - - { + bool success = outputFile->Open(fileNamePhraseTable); + + if (!success) { cerr << "ERROR: could not open file phrase table file " << fileNamePhraseTable << endl; exit(1); @@ -250,7 +247,7 @@ int main(int argc, char* argv[]) phraseTableFile->flush(); if (phraseTableFile != &cout) { - (dynamic_cast(phraseTableFile))->close(); + //(dynamic_cast(phraseTableFile))->close(); delete phraseTableFile; }