36 #if defined (HAVE_CONFIG_H)
46 #if defined (HAVE_ZLIB)
54 #define STASHED_CHARACTERS 16
55 #define BIGBUFSIZE (256 * 1024 + STASHED_CHARACTERS)
56 #define SMALLBUFSIZE 1
60 : m_file(nullptr), m_io_mode(std::ios_base::openmode(0)), m_own_fd(false),
61 m_buffer(nullptr), m_buffer_size(
BIGBUFSIZE), m_own_buffer(true)
64 this->disable_buffer ();
76 this->disable_buffer ();
83 return gzsetparams (m_file, comp_level, comp_strategy);
94 if ((mode & std::ios_base::in) && (mode & std::ios_base::out))
98 char char_mode[6] =
"\0\0\0\0\0";
103 if ((m_file = gzopen (name, char_mode)) ==
nullptr)
107 this->enable_buffer ();
121 if ((mode & std::ios_base::in) && (mode & std::ios_base::out))
125 char char_mode[6] =
"\0\0\0\0\0";
130 if ((m_file = gzdopen (fd, char_mode)) ==
nullptr)
134 this->enable_buffer ();
150 if (this->
sync () == -1)
152 if (gzclose (m_file) < 0)
158 this->disable_buffer ();
170 bool testi = mode & std::ios_base::in;
171 bool testo = mode & std::ios_base::out;
173 bool testa = mode & std::ios_base::app;
180 if (! testi && testo && ! testt && ! testa)
181 strcpy (c_mode,
"w");
182 if (! testi && testo && ! testt && testa)
183 strcpy (c_mode,
"a");
184 if (! testi && testo && testt && ! testa)
185 strcpy (c_mode,
"w");
186 if (testi && ! testo && ! testt && ! testa)
187 strcpy (c_mode,
"r");
198 strcat (c_mode,
"b");
208 if (! this->
is_open () || ! (m_io_mode & std::ios_base::in))
211 if (this->gptr () && (this->gptr () < this->egptr ()))
212 return std::streamsize (this->egptr () - this->gptr ());
226 if (gzseek (m_file, this->gptr () - this->egptr () - 1,
SEEK_CUR) < 0)
227 return traits_type::eof ();
234 int bytes_read = gzread (m_file, m_buffer, m_buffer_size);
239 this->setg (m_buffer, m_buffer, m_buffer);
240 return traits_type::eof ();
244 this->setg (m_buffer, m_buffer, m_buffer + bytes_read);
248 gzfilebuf::int_type ret = traits_type::to_int_type (*(this->gptr ()));
250 return traits_type::eof ();
255 return traits_type::eof ();
265 if (this->gptr () && (this->gptr () < this->egptr ()))
266 return traits_type::to_int_type (*(this->gptr ()));
269 if (! this->
is_open () || ! (m_io_mode & std::ios_base::in))
270 return traits_type::eof ();
276 char_type *ptr1 = m_buffer;
278 if (ptr2 > this->eback ())
285 int bytes_read = gzread (m_file, m_buffer + stash, m_buffer_size - stash);
291 this->setg (m_buffer, m_buffer, m_buffer);
292 return traits_type::eof ();
295 this->setg (m_buffer, m_buffer + stash, m_buffer + bytes_read + stash);
298 return traits_type::to_int_type (*(this->gptr ()));
309 if (this->pptr () > this->epptr () || this->pptr () < this->pbase ())
310 return traits_type::eof ();
312 if (! traits_type::eq_int_type (c, traits_type::eof ()))
314 *(this->pptr ()) = traits_type::to_char_type (c);
318 int bytes_to_write = this->pptr () - this->pbase ();
320 if (bytes_to_write > 0)
323 if (! this->
is_open () || ! (m_io_mode & std::ios_base::out))
324 return traits_type::eof ();
326 if (gzwrite (m_file, this->pbase (), bytes_to_write)
328 return traits_type::eof ();
330 this->pbump (-bytes_to_write);
334 else if (! traits_type::eq_int_type (c, traits_type::eof ()))
337 if (! this->
is_open () || ! (m_io_mode & std::ios_base::out))
338 return traits_type::eof ();
340 char_type last_char = traits_type::to_char_type (c);
342 if (gzwrite (m_file, &last_char, 1) != 1)
343 return traits_type::eof ();
348 if (traits_type::eq_int_type (c, traits_type::eof ()))
349 return traits_type::not_eof (c);
359 if (this->
sync () == -1)
369 this->disable_buffer ();
373 this->enable_buffer ();
378 this->disable_buffer ();
381 m_own_buffer =
false;
382 this->enable_buffer ();
391 return traits_type::eq_int_type (this->
overflow (),
392 traits_type::eof ()) ? -1 : 0;
399 gzfilebuf::enable_buffer ()
402 if (m_own_buffer && ! m_buffer)
405 if (m_buffer_size > 0)
408 m_buffer =
new char_type [m_buffer_size];
410 this->setg (m_buffer, m_buffer, m_buffer);
416 this->setp (m_buffer, m_buffer + m_buffer_size - 1);
422 m_buffer =
new char_type [m_buffer_size];
423 this->setg (m_buffer, m_buffer, m_buffer);
425 this->setp (
nullptr,
nullptr);
432 this->setg (m_buffer, m_buffer, m_buffer);
433 this->setp (m_buffer, m_buffer + m_buffer_size - 1);
439 gzfilebuf::disable_buffer ()
442 if (m_own_buffer && m_buffer)
445 if (! this->pbase ())
449 this->setg (
nullptr,
nullptr,
nullptr);
450 this->setp (
nullptr,
nullptr);
455 this->setg (m_buffer, m_buffer, m_buffer);
457 this->setp (m_buffer, m_buffer + m_buffer_size - 1);
459 this->setp (
nullptr,
nullptr);
468 std::ios_base::openmode)
470 pos_type ret = pos_type (off_type (-1));
474 off_type computed_off = off;
476 if ((m_io_mode & std::ios_base::in) && way == std::ios_base::cur)
477 computed_off += this->gptr () - this->egptr ();
481 if (off == 0 && way == std::ios_base::cur)
482 return pos_type (gztell (m_file) + computed_off);
484 if (way == std::ios_base::beg)
485 ret = pos_type (gzseek (m_file, computed_off,
SEEK_SET));
486 else if (way == std::ios_base::cur)
487 ret = pos_type (gzseek (m_file, computed_off,
SEEK_CUR));
490 ret = pos_type (gzseek (m_file, computed_off,
SEEK_END));
492 if (m_io_mode & std::ios_base::in)
506 pos_type ret = pos_type (off_type (-1));
510 ret = pos_type (gzseek (m_file, sp,
SEEK_SET));
512 if (m_io_mode & std::ios_base::in)
525 : std::
istream (nullptr), m_sb ()
526 { this->init (&m_sb); }
530 : std::
istream (nullptr), m_sb ()
533 this->
open (name, mode);
538 : std::
istream (nullptr), m_sb ()
548 if (! m_sb.
open (name, mode | std::ios_base::in))
549 this->setstate (std::ios_base::failbit);
558 if (! m_sb.
attach (fd, mode | std::ios_base::in))
559 this->setstate (std::ios_base::failbit);
569 this->setstate (std::ios_base::failbit);
574 : std::
ostream (nullptr), m_sb ()
575 { this->init (&m_sb); }
579 : std::
ostream (nullptr), m_sb ()
582 this->
open (name, mode);
587 : std::
ostream (nullptr), m_sb ()
597 if (! m_sb.
open (name, mode | std::ios_base::out))
598 this->setstate (std::ios_base::failbit);
607 if (! m_sb.
attach (fd, mode | std::ios_base::out))
608 this->setstate (std::ios_base::failbit);
618 this->setstate (std::ios_base::failbit);
Gzipped file stream buffer class.
gzfilebuf * close()
Close gzipped file.
bool is_open() const
Check if file is open.
virtual int_type underflow()
Fill get area from gzipped file.
virtual int_type overflow(int_type c=traits_type::eof())
Write put area to gzipped file.
virtual pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
Alters the stream positions.
virtual int_type pbackfail(int_type c=traits_type::eof())
virtual std::streambuf * setbuf(char_type *p, std::streamsize n)
Installs external stream buffer.
gzfilebuf * open(const char *name, std::ios_base::openmode mode)
Open gzipped file.
virtual pos_type seekpos(pos_type sp, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
Alters the stream positions.
bool open_mode(std::ios_base::openmode mode, char *c_mode) const
Convert ios open mode int to mode string used by zlib.
int setcompression(int comp_level, int comp_strategy=Z_DEFAULT_STRATEGY)
Set compression level and strategy on the fly.
virtual int sync()
Flush stream buffer to file.
gzfilebuf * attach(int fd, std::ios_base::openmode mode)
Attach to already open gzipped file.
virtual std::streamsize showmanyc()
Number of characters available in stream buffer.
void close()
Close gzipped file.
void attach(int fd, std::ios_base::openmode mode=std::ios_base::in)
Attach to already open gzipped file.
void open(const char *name, std::ios_base::openmode mode=std::ios_base::in)
Open gzipped file.
void close()
Close gzipped file.
void attach(int fd, std::ios_base::openmode mode=std::ios_base::out)
Attach to already open gzipped file.
void open(const char *name, std::ios_base::openmode mode=std::ios_base::out)
Open gzipped file.
#define STASHED_CHARACTERS
std::complex< T > trunc(const std::complex< T > &x)
T::size_type strlen(const typename T::value_type *str)