GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
gzfilebuf Class Reference

Gzipped file stream buffer class. More...

#include "gzfstream.h"

Inheritance diagram for gzfilebuf:
Collaboration diagram for gzfilebuf:

Public Member Functions

 gzfilebuf ()
 
 gzfilebuf (const gzfilebuf &)=delete
 
virtual ~gzfilebuf ()
 
gzfilebufattach (int fd, std::ios_base::openmode mode)
 Attach to already open gzipped file. More...
 
gzfilebufclose ()
 Close gzipped file. More...
 
bool is_open () const
 Check if file is open. More...
 
gzfilebufopen (const char *name, std::ios_base::openmode mode)
 Open gzipped file. More...
 
gzfilebufoperator= (const gzfilebuf &)=delete
 
int setcompression (int comp_level, int comp_strategy=Z_DEFAULT_STRATEGY)
 Set compression level and strategy on the fly. More...
 

Protected Member Functions

bool open_mode (std::ios_base::openmode mode, char *c_mode) const
 Convert ios open mode int to mode string used by zlib. More...
 
virtual int_type overflow (int_type c=traits_type::eof())
 Write put area to gzipped file. More...
 
virtual int_type pbackfail (int_type c=traits_type::eof())
 
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. More...
 
virtual pos_type seekpos (pos_type sp, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
 Alters the stream positions. More...
 
virtual std::streambuf * setbuf (char_type *p, std::streamsize n)
 Installs external stream buffer. More...
 
virtual std::streamsize showmanyc ()
 Number of characters available in stream buffer. More...
 
virtual int sync ()
 Flush stream buffer to file. More...
 
virtual int_type underflow ()
 Fill get area from gzipped file. More...
 

Private Member Functions

void disable_buffer ()
 Destroy internal buffer. More...
 
void enable_buffer ()
 Allocate internal buffer. More...
 

Private Attributes

char_type * m_buffer
 Stream buffer. More...
 
std::streamsize m_buffer_size
 Stream buffer size. More...
 
gzFile m_file
 Underlying file pointer. More...
 
std::ios_base::openmode m_io_mode
 Mode in which file was opened. More...
 
bool m_own_buffer
 True if this object owns stream buffer. More...
 
bool m_own_fd
 True if this object owns file descriptor. More...
 

Detailed Description

Gzipped file stream buffer class.

This class implements basic_filebuf for gzipped files. It doesn't yet support seeking (allowed by zlib but slow/limited), putback and read/write access * (tricky). Otherwise, it attempts to be a drop-in replacement for the standard file streambuf.

Definition at line 55 of file gzfstream.h.

Constructor & Destructor Documentation

◆ gzfilebuf() [1/2]

gzfilebuf::gzfilebuf ( )

Definition at line 59 of file gzfstream.cc.

References disable_buffer().

◆ gzfilebuf() [2/2]

gzfilebuf::gzfilebuf ( const gzfilebuf )
delete

◆ ~gzfilebuf()

gzfilebuf::~gzfilebuf ( )
virtual

Definition at line 68 of file gzfstream.cc.

References close(), disable_buffer(), m_own_fd, and sync().

Member Function Documentation

◆ attach()

gzfilebuf * gzfilebuf::attach ( int  fd,
std::ios_base::openmode  mode 
)

Attach to already open gzipped file.

Parameters
fdFile descriptor.
modeOpen mode flags.
Returns
this on success, NULL on failure.

Definition at line 115 of file gzfstream.cc.

References enable_buffer(), is_open(), m_file, m_io_mode, m_own_fd, and open_mode().

Referenced by gzifstream::attach(), and gzofstream::attach().

◆ close()

gzfilebuf * gzfilebuf::close ( void  )

Close gzipped file.

Returns
this on success, NULL on failure.

Definition at line 142 of file gzfstream.cc.

References disable_buffer(), is_open(), m_file, m_own_fd, and sync().

Referenced by ~gzfilebuf(), gzifstream::close(), and gzofstream::close().

◆ disable_buffer()

void gzfilebuf::disable_buffer ( )
private

Destroy internal buffer.

This function is safe to call multiple times. It will ensure that the internal buffer is deallocated if it exists. In any case, it will also reset the buffer pointers.

Definition at line 439 of file gzfstream.cc.

References m_buffer, m_buffer_size, and m_own_buffer.

Referenced by gzfilebuf(), ~gzfilebuf(), close(), and setbuf().

◆ enable_buffer()

void gzfilebuf::enable_buffer ( )
private

Allocate internal buffer.

This function is safe to call multiple times. It will ensure that a proper internal buffer exists if it is required. If the buffer already exists or is external, the buffer pointers will be reset to their original state.

Definition at line 399 of file gzfstream.cc.

References m_buffer, m_buffer_size, m_own_buffer, and SMALLBUFSIZE.

Referenced by attach(), open(), pbackfail(), seekoff(), seekpos(), and setbuf().

◆ is_open()

bool gzfilebuf::is_open ( void  ) const
inline

Check if file is open.

Returns
True if file is open.

Definition at line 90 of file gzfstream.h.

References m_file.

Referenced by attach(), close(), gzifstream::is_open(), gzofstream::is_open(), open(), overflow(), pbackfail(), seekoff(), seekpos(), showmanyc(), and underflow().

◆ open()

gzfilebuf * gzfilebuf::open ( const char *  name,
std::ios_base::openmode  mode 
)

Open gzipped file.

Parameters
nameFilename.
modeOpen mode flags.
Returns
this on success, NULL on failure.

Definition at line 88 of file gzfstream.cc.

References enable_buffer(), is_open(), m_file, m_io_mode, m_own_fd, and open_mode().

Referenced by gzifstream::open(), and gzofstream::open().

◆ open_mode()

bool gzfilebuf::open_mode ( std::ios_base::openmode  mode,
char *  c_mode 
) const
protected

Convert ios open mode int to mode string used by zlib.

Returns
True if valid mode flag combination.

Definition at line 166 of file gzfstream.cc.

References strlen(), and trunc().

Referenced by attach(), and open().

◆ operator=()

gzfilebuf& gzfilebuf::operator= ( const gzfilebuf )
delete

◆ overflow()

gzfilebuf::int_type gzfilebuf::overflow ( int_type  c = traits_type::eof ())
protectedvirtual

Write put area to gzipped file.

Parameters
cExtra character to add to buffer contents.
Returns
Non-EOF on success, EOF on error.

This actually writes characters in stream buffer to gzipped file. With unbuffered output this is done one character at a time.

Definition at line 303 of file gzfstream.cc.

References is_open(), m_file, and m_io_mode.

Referenced by seekoff(), seekpos(), and sync().

◆ pbackfail()

gzfilebuf::int_type gzfilebuf::pbackfail ( int_type  c = traits_type::eof ())
protectedvirtual

Definition at line 222 of file gzfstream.cc.

References enable_buffer(), is_open(), m_buffer, m_buffer_size, m_file, and SEEK_CUR.

◆ seekoff()

gzfilebuf::pos_type gzfilebuf::seekoff ( off_type  off,
std::ios_base::seekdir  way,
std::ios_base::openmode  mode = std::ios_base::in | std::ios_base::out 
)
protectedvirtual

Alters the stream positions.

Each derived class provides its own appropriate behavior.

Definition at line 467 of file gzfstream.cc.

References enable_buffer(), is_open(), m_file, m_io_mode, overflow(), SEEK_CUR, SEEK_END, and SEEK_SET.

◆ seekpos()

gzfilebuf::pos_type gzfilebuf::seekpos ( pos_type  sp,
std::ios_base::openmode  mode = std::ios_base::in | std::ios_base::out 
)
protectedvirtual

Alters the stream positions.

Each derived class provides its own appropriate behavior.

Definition at line 504 of file gzfstream.cc.

References enable_buffer(), is_open(), m_file, m_io_mode, overflow(), and SEEK_SET.

◆ setbuf()

std::streambuf * gzfilebuf::setbuf ( char_type *  p,
std::streamsize  n 
)
protectedvirtual

Installs external stream buffer.

Parameters
pPointer to char buffer.
nSize of external buffer.
Returns
this on success, NULL on failure.

Call setbuf(0,0) to enable unbuffered output.

Definition at line 356 of file gzfstream.cc.

References disable_buffer(), enable_buffer(), m_buffer, m_buffer_size, m_own_buffer, n, and sync().

◆ setcompression()

int gzfilebuf::setcompression ( int  comp_level,
int  comp_strategy = Z_DEFAULT_STRATEGY 
)

Set compression level and strategy on the fly.

Parameters
comp_levelCompression level (see zlib.h for allowed values)
comp_strategyCompression strategy (see zlib.h for allowed values)
Returns
Z_OK on success, Z_STREAM_ERROR otherwise.

Unfortunately, these parameters cannot be modified separately, as the previous zfstream version assumed. Since the strategy is seldom changed, it can default and setcompression(level) then becomes like the old setcompressionlevel(level).

Definition at line 81 of file gzfstream.cc.

References m_file.

◆ showmanyc()

std::streamsize gzfilebuf::showmanyc ( )
protectedvirtual

Number of characters available in stream buffer.

Returns
Number of characters.

This indicates number of characters in get area of stream buffer. These characters can be read without accessing the gzipped file.

Definition at line 205 of file gzfstream.cc.

References is_open(), and m_io_mode.

◆ sync()

int gzfilebuf::sync ( void  )
protectedvirtual

Flush stream buffer to file.

Returns
0 on success, -1 on error.

This calls underflow(EOF) to do the job.

Definition at line 389 of file gzfstream.cc.

References overflow().

Referenced by ~gzfilebuf(), close(), and setbuf().

◆ underflow()

gzfilebuf::int_type gzfilebuf::underflow ( void  )
protectedvirtual

Fill get area from gzipped file.

Returns
First character in get area on success, EOF on error.

This actually reads characters from gzipped file to stream buffer. Always buffered.

Definition at line 260 of file gzfstream.cc.

References is_open(), m_buffer, m_buffer_size, m_file, m_io_mode, and STASHED_CHARACTERS.

Member Data Documentation

◆ m_buffer

char_type* gzfilebuf::m_buffer
private

Stream buffer.

For simplicity this remains allocated on the free store for the entire life span of the gzfilebuf object, unless replaced by setbuf.

Definition at line 256 of file gzfstream.h.

Referenced by disable_buffer(), enable_buffer(), pbackfail(), setbuf(), and underflow().

◆ m_buffer_size

std::streamsize gzfilebuf::m_buffer_size
private

Stream buffer size.

Defaults to system default buffer size (typically 8192 bytes). Modified by setbuf.

Definition at line 264 of file gzfstream.h.

Referenced by disable_buffer(), enable_buffer(), pbackfail(), setbuf(), and underflow().

◆ m_file

gzFile gzfilebuf::m_file
private

Underlying file pointer.

Definition at line 235 of file gzfstream.h.

Referenced by attach(), close(), is_open(), open(), overflow(), pbackfail(), seekoff(), seekpos(), setcompression(), and underflow().

◆ m_io_mode

std::ios_base::openmode gzfilebuf::m_io_mode
private

Mode in which file was opened.

Definition at line 240 of file gzfstream.h.

Referenced by attach(), open(), overflow(), seekoff(), seekpos(), showmanyc(), and underflow().

◆ m_own_buffer

bool gzfilebuf::m_own_buffer
private

True if this object owns stream buffer.

This makes the class responsible for deleting the buffer upon destruction.

Definition at line 272 of file gzfstream.h.

Referenced by disable_buffer(), enable_buffer(), and setbuf().

◆ m_own_fd

bool gzfilebuf::m_own_fd
private

True if this object owns file descriptor.

This makes the class responsible for closing the file upon destruction.

Definition at line 248 of file gzfstream.h.

Referenced by ~gzfilebuf(), attach(), close(), and open().


The documentation for this class was generated from the following files: