|
#include "GzFileBuf.h" |
|
|
|
#include <cstring> |
|
#include <cstdlib> |
|
#include <cstdio> |
|
#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), |
|
m_buf + sizeof(int), |
|
m_buf + sizeof(int)); |
|
} |
|
|
|
GzFileBuf::~GzFileBuf() |
|
{ |
|
gzclose(m_gz_file); |
|
} |
|
|
|
int GzFileBuf::overflow(int_type c) |
|
{ |
|
throw; |
|
} |
|
|
|
|
|
int GzFileBuf::underflow() |
|
{ |
|
|
|
if (gptr() < egptr()) { |
|
return traits_type::to_int_type(*gptr()); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
unsigned int num_put_back = static_cast<unsigned int>(gptr() - eback()); |
|
if (num_put_back > sizeof(int)) { |
|
num_put_back = sizeof(int); |
|
} |
|
|
|
|
|
|
|
|
|
std::memmove(m_buf + (sizeof(int) - num_put_back), |
|
gptr() - num_put_back, num_put_back); |
|
|
|
|
|
const int num = gzread(m_gz_file, m_buf + sizeof(int), |
|
kBufSize - sizeof(int)); |
|
if (num <= 0) { |
|
|
|
return EOF; |
|
} |
|
|
|
|
|
setg(m_buf + (sizeof(int) - num_put_back), |
|
m_buf + sizeof(int), |
|
m_buf + sizeof(int) + num); |
|
|
|
|
|
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; |
|
} |
|
|