mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-09-11 11:25:40 +03:00
Move the GzFileBuf implementation into .cpp file.
Add error check when gzopen() fails.
This commit is contained in:
parent
91df03e2ed
commit
0a852b60d1
79
mert/GzFileBuf.cpp
Normal file
79
mert/GzFileBuf.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "GzFileBuf.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
GzFileBuf::GzFileBuf(const char* filename) {
|
||||
m_gz_file = gzopen(filename, "rb");
|
||||
if (m_gz_file == NULL) {
|
||||
std::cerr << "ERROR: Failed to open " << filename << std::endl;
|
||||
std::exit(1);
|
||||
}
|
||||
setg(m_buf + sizeof(int), // beginning of putback area
|
||||
m_buf + sizeof(int), // read position
|
||||
m_buf + sizeof(int)); // end position
|
||||
}
|
||||
|
||||
GzFileBuf::~GzFileBuf() {
|
||||
gzclose(m_gz_file);
|
||||
}
|
||||
|
||||
int GzFileBuf::overflow(int_type c) {
|
||||
throw;
|
||||
}
|
||||
|
||||
// read one character
|
||||
int GzFileBuf::underflow() {
|
||||
// is read position before end of m_buf?
|
||||
if (gptr() < egptr()) {
|
||||
return traits_type::to_int_type(*gptr());
|
||||
}
|
||||
|
||||
/* process size of putback area
|
||||
* - use number of characters read
|
||||
* - but at most four
|
||||
*/
|
||||
unsigned int num_put_back = static_cast<unsigned int>(gptr() - eback());
|
||||
if (num_put_back > sizeof(int)) {
|
||||
num_put_back = sizeof(int);
|
||||
}
|
||||
|
||||
/* copy up to four characters previously read into
|
||||
* the putback m_buf (area of first four characters)
|
||||
*/
|
||||
std::memmove(m_buf + (sizeof(int) - num_put_back),
|
||||
gptr() - num_put_back, num_put_back);
|
||||
|
||||
// read new characters
|
||||
const int num = gzread(m_gz_file, m_buf + sizeof(int),
|
||||
kBufSize - sizeof(int));
|
||||
if (num <= 0) {
|
||||
// ERROR or EOF
|
||||
return EOF;
|
||||
}
|
||||
|
||||
// reset m_buf pointers
|
||||
setg(m_buf + (sizeof(int) - num_put_back), // beginning of putback area
|
||||
m_buf + sizeof(int), // read position
|
||||
m_buf + sizeof(int) + num); // end of buffer
|
||||
|
||||
// return next character
|
||||
return traits_type::to_int_type(*gptr());
|
||||
}
|
||||
|
||||
std::streampos GzFileBuf::seekpos(
|
||||
std::streampos sp,
|
||||
std::ios_base::openmode which) {
|
||||
throw;
|
||||
}
|
||||
|
||||
std::streamsize GzFileBuf::xsgetn(char* s,
|
||||
std::streamsize num) {
|
||||
return static_cast<std::streamsize>(gzread(m_gz_file,s,num));
|
||||
}
|
||||
|
||||
std::streamsize GzFileBuf::xsputn(const char* s,
|
||||
std::streamsize num) {
|
||||
throw;
|
||||
}
|
@ -3,82 +3,27 @@
|
||||
|
||||
#include <streambuf>
|
||||
#include <zlib.h>
|
||||
#include <cstring>
|
||||
|
||||
class GzFileBuf : public std::streambuf
|
||||
{
|
||||
public:
|
||||
explicit GzFileBuf(const char *filename) {
|
||||
m_gz_file = gzopen(filename, "rb");
|
||||
setg(m_buf + sizeof(int), // beginning of putback area
|
||||
m_buf + sizeof(int), // read position
|
||||
m_buf + sizeof(int)); // end position
|
||||
}
|
||||
|
||||
virtual ~GzFileBuf() {
|
||||
gzclose(m_gz_file);
|
||||
}
|
||||
explicit GzFileBuf(const char* filename);
|
||||
virtual ~GzFileBuf();
|
||||
|
||||
protected:
|
||||
virtual int_type overflow(int_type c) {
|
||||
throw;
|
||||
}
|
||||
virtual int_type overflow(int_type c);
|
||||
|
||||
// write multiple characters
|
||||
virtual std::streamsize xsputn(const char* s,
|
||||
std::streamsize num) {
|
||||
throw;
|
||||
}
|
||||
// Read one character
|
||||
virtual int_type underflow();
|
||||
|
||||
virtual std::streampos seekpos(
|
||||
std::streampos sp,
|
||||
std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) {
|
||||
throw;
|
||||
}
|
||||
std::ios_base::openmode which = std::ios_base::in | std::ios_base::out);
|
||||
|
||||
// read one character
|
||||
virtual int_type underflow() {
|
||||
// is read position before end of m_buf?
|
||||
if (gptr() < egptr()) {
|
||||
return traits_type::to_int_type(*gptr());
|
||||
}
|
||||
virtual std::streamsize xsgetn(char* s, std::streamsize num);
|
||||
|
||||
/* process size of putback area
|
||||
* - use number of characters read
|
||||
* - but at most four
|
||||
*/
|
||||
unsigned int num_put_back = gptr() - eback();
|
||||
if (num_put_back > sizeof(int)) {
|
||||
num_put_back = sizeof(int);
|
||||
}
|
||||
|
||||
/* copy up to four characters previously read into
|
||||
* the putback m_buf (area of first four characters)
|
||||
*/
|
||||
std::memmove(m_buf + (sizeof(int) - num_put_back),
|
||||
gptr() - num_put_back, num_put_back);
|
||||
|
||||
// read new characters
|
||||
const int num = gzread(m_gz_file, m_buf + sizeof(int),
|
||||
kBufSize - sizeof(int));
|
||||
if (num <= 0) {
|
||||
// ERROR or EOF
|
||||
return EOF;
|
||||
}
|
||||
|
||||
// reset m_buf pointers
|
||||
setg(m_buf + (sizeof(int) - num_put_back), // beginning of putback area
|
||||
m_buf + sizeof(int), // read position
|
||||
m_buf + sizeof(int) + num); // end of buffer
|
||||
|
||||
// return next character
|
||||
return traits_type::to_int_type(*gptr());
|
||||
}
|
||||
|
||||
std::streamsize xsgetn(char* s,
|
||||
std::streamsize num) {
|
||||
return gzread(m_gz_file,s,num);
|
||||
}
|
||||
// write multiple characters
|
||||
virtual std::streamsize xsputn(const char* s, std::streamsize num);
|
||||
|
||||
private:
|
||||
gzFile m_gz_file;
|
||||
|
@ -4,6 +4,7 @@ lib m ;
|
||||
|
||||
lib mert_lib :
|
||||
Util.cpp
|
||||
GzFileBuf.cpp
|
||||
FileStream.cpp
|
||||
Timer.cpp
|
||||
ScoreStats.cpp
|
||||
|
Loading…
Reference in New Issue
Block a user