diff --git a/mert/FileStream.cpp b/mert/FileStream.cpp index 93b7138b2..1a52e53fa 100644 --- a/mert/FileStream.cpp +++ b/mert/FileStream.cpp @@ -1,7 +1,7 @@ #include "FileStream.h" #include -#include "gzfilebuf.h" +#include "GzFileBuf.h" using namespace std; @@ -22,7 +22,7 @@ inputfilestream::inputfilestream(const std::string &filePath) if (IsGzipFile(filePath)) { fb->close(); delete fb; - m_streambuf = new gzfilebuf(filePath.c_str()); + m_streambuf = new GzFileBuf(filePath.c_str()); } else { m_streambuf = fb; } diff --git a/mert/GzFileBuf.h b/mert/GzFileBuf.h new file mode 100644 index 000000000..c8a98aecf --- /dev/null +++ b/mert/GzFileBuf.h @@ -0,0 +1,89 @@ +#ifndef MERT_GZFILEBUF_H_ +#define MERT_GZFILEBUF_H_ + +#include +#include +#include + +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); + } + +protected: + virtual int_type overflow(int_type c) { + throw; + } + + // write multiple characters + virtual std::streamsize xsputn(const char* s, + std::streamsize num) { + throw; + } + + virtual std::streampos seekpos( + std::streampos sp, + std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) { + throw; + } + + // 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()); + } + + /* 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); + } + +private: + gzFile m_gz_file; + static const unsigned int kBufSize = 1024; + char m_buf[kBufSize]; +}; + +#endif // MERT_GZFILEBUF_H_ diff --git a/mert/gzfilebuf.h b/mert/gzfilebuf.h deleted file mode 100644 index b0f9637b3..000000000 --- a/mert/gzfilebuf.h +++ /dev/null @@ -1,87 +0,0 @@ -#ifndef MERT_GZFILEBUF_H_ -#define MERT_GZFILEBUF_H_ - -#include -#include -#include - -class gzfilebuf : public std::streambuf -{ -public: - explicit gzfilebuf(const char *filename) { - _gzf = gzopen(filename, "rb"); - setg (_buff+sizeof(int), // beginning of putback area - _buff+sizeof(int), // read position - _buff+sizeof(int)); // end position - } - - virtual ~gzfilebuf() { - gzclose(_gzf); - } - -protected: - virtual int_type overflow (int_type c) { - throw; - } - - // write multiple characters - virtual - std::streamsize xsputn (const char* s, - std::streamsize num) { - throw; - } - - virtual std::streampos seekpos ( std::streampos sp, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out ) { - throw; - } - - // read one character - virtual int_type underflow () { - // is read position before end of _buff? - 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 numPutback = gptr() - eback(); - if (numPutback > sizeof(int)) { - numPutback = sizeof(int); - } - - /* copy up to four characters previously read into - * the putback _buff (area of first four characters) - */ - std::memmove (_buff+(sizeof(int)-numPutback), gptr()-numPutback, - numPutback); - - // read new characters - int num = gzread(_gzf, _buff+sizeof(int), _buffsize-sizeof(int)); - if (num <= 0) { - // ERROR or EOF - return EOF; - } - - // reset _buff pointers - setg (_buff+(sizeof(int)-numPutback), // beginning of putback area - _buff+sizeof(int), // read position - _buff+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(_gzf,s,num); - } - -private: - gzFile _gzf; - static const unsigned int _buffsize = 1024; - char _buff[_buffsize]; -}; - -#endif // MERT_GZFILEBUF_H_