Rename gzfilebuf.h; apply coding style.

This commit is contained in:
Tetsuo Kiso 2012-03-20 23:48:52 +09:00
parent 3a73c5990d
commit ccae7bae6e
3 changed files with 91 additions and 89 deletions

View File

@ -1,7 +1,7 @@
#include "FileStream.h"
#include <stdexcept>
#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;
}

89
mert/GzFileBuf.h Normal file
View File

@ -0,0 +1,89 @@
#ifndef MERT_GZFILEBUF_H_
#define MERT_GZFILEBUF_H_
#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);
}
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_

View File

@ -1,87 +0,0 @@
#ifndef MERT_GZFILEBUF_H_
#define MERT_GZFILEBUF_H_
#include <streambuf>
#include <zlib.h>
#include <cstring>
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_