libzypp  17.23.5
GzStream.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | |
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
8 | |
9 | core system |
10 | (C) SuSE Linux Products GmbH |
11 \----------------------------------------------------------------------/
12 
13  File: GzStream.h
14 
15  Author: Michael Andres <ma@suse.de>
16  Maintainer: Michael Andres <ma@suse.de>
17 
18  Purpose: Streams reading and writing gzip files.
19 
20 /-*/
21 #ifndef ZYPP_BASE_GZSTREAM_H
22 #define ZYPP_BASE_GZSTREAM_H
23 
24 #include <iosfwd>
25 #include <streambuf>
26 #include <vector>
27 #include <zlib.h>
28 
30 #include <zypp/base/fXstream.h>
31 
33 namespace zypp
34 {
35 
37  namespace gzstream_detail
38  {
39 
41  //
42  // CLASS NAME : ZlibError
46  struct ZlibError
47  {
51  int _zError;
52 
56  int _errno;
57 
59  : _zError( 0 ), _errno( 0 )
60  {}
61 
65  std::string
66  strerror() const;
67  };
69 
71  inline std::ostream & operator<<( std::ostream & str, const ZlibError & obj )
72  { return str << obj.strerror(); }
73 
74 
76  //
77  // CLASS NAME : gzstreambufimpl
91  public:
92 
94 
96  { closeImpl(); }
97 
98  bool
99  isOpen () const
100  { return _file; }
101 
102  bool
103  canRead () const
104  { return( _mode == std::ios_base::in ); }
105 
106  bool
107  canWrite () const
108  { return( _mode == std::ios_base::out ); }
109 
110  bool
111  canSeek ( std::ios_base::seekdir way_r ) const
112  { return ( way_r == std::ios_base::beg || way_r == std::ios_base::cur ); }
113 
114  protected:
115  bool openImpl( const char * name_r, std::ios_base::openmode mode_r );
116  bool closeImpl ();
117 
120  off_t compressed_tell() const;
121 
122  public:
126  error_type
127  error() const
128  { return _error; }
129 
130  std::streamsize readData ( char * buffer_r, std::streamsize maxcount_r );
131  bool writeData( const char * buffer_r, std::streamsize count_r );
132  off_t seekTo( off_t off_r, std::ios_base::seekdir way_r, std::ios_base::openmode omode_r );
133  off_t tell() const;
134 
135  private:
136 
137  void
138  setZError() const
139  { gzerror( _file, &_error._zError ); }
140 
142  int _fd = -1;
143 
144  gzFile _file = nullptr;
145 
146  std::ios_base::openmode _mode = std::ios_base::openmode(0);
147 
148  mutable ZlibError _error;
149 
150  };
152  } // namespace gzstream_detail
153 
158 
163 
165 } // namespace zypp
167 
168 #endif // ZYPP_BASE_GZSTREAM_H
zypp::gzstream_detail::gzstreambufimpl::isOpen
bool isOpen() const
Definition: GzStream.h:99
zypp::gzstream_detail::ZlibError::_zError
int _zError
The zlib error code.
Definition: GzStream.h:51
zypp::gzstream_detail::ZlibError::operator<<
std::ostream & operator<<(std::ostream &str, const ZlibError &obj)
Definition: GzStream.h:71
zypp::gzstream_detail::gzstreambufimpl::canWrite
bool canWrite() const
Definition: GzStream.h:107
zypp::ifgzstream
detail::fXstream< std::istream, gzstream_detail::fgzstreambuf > ifgzstream
istream reading gzip files as well as plain files.
Definition: GzStream.h:157
zypp::gzstream_detail::gzstreambufimpl::error_type
ZlibError error_type
Definition: GzStream.h:93
zypp::gzstream_detail::gzstreambufimpl::canRead
bool canRead() const
Definition: GzStream.h:103
zypp::gzstream_detail::gzstreambufimpl::_file
gzFile _file
Definition: GzStream.h:144
zypp::gzstream_detail::gzstreambufimpl
Streambuffer reading or writing gzip files.
Definition: GzStream.h:90
zypp::gzstream_detail::gzstreambufimpl::canSeek
bool canSeek(std::ios_base::seekdir way_r) const
Definition: GzStream.h:111
zypp::gzstream_detail::gzstreambufimpl::_fd
int _fd
file descriptor of the compressed file
Definition: GzStream.h:142
zypp::gzstream_detail::ZlibError::_errno
int _errno
errno, valid if zError is Z_ERRNO
Definition: GzStream.h:56
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
zypp::gzstream_detail::gzstreambufimpl::compressed_tell
off_t compressed_tell() const
Tell the file position in the compressed file.
Definition: GzStream.cc:197
zypp::gzstream_detail::gzstreambufimpl::closeImpl
bool closeImpl()
Definition: GzStream.cc:110
SimpleStreambuf.h
zypp::gzstream_detail::gzstreambufimpl::_mode
std::ios_base::openmode _mode
Definition: GzStream.h:146
zypp::gzstream_detail::gzstreambufimpl::error
error_type error() const
The last error returned fron zlib.
Definition: GzStream.h:127
zypp::gzstream_detail::gzstreambufimpl::writeData
bool writeData(const char *buffer_r, std::streamsize count_r)
Definition: GzStream.cc:157
zypp::gzstream_detail::gzstreambufimpl::openImpl
bool openImpl(const char *name_r, std::ios_base::openmode mode_r)
Definition: GzStream.cc:74
zypp::detail::fXstream
Common template to define ifgzstream/ofgzstream reading/writing compressed files.
Definition: fXstream.h:26
zypp::gzstream_detail::gzstreambufimpl::seekTo
off_t seekTo(off_t off_r, std::ios_base::seekdir way_r, std::ios_base::openmode omode_r)
Definition: GzStream.cc:174
zypp::gzstream_detail::gzstreambufimpl::~gzstreambufimpl
~gzstreambufimpl()
Definition: GzStream.h:95
zypp::detail::SimpleStreamBuf
Definition: SimpleStreambuf.h:54
fXstream.h
zypp::gzstream_detail::gzstreambufimpl::readData
std::streamsize readData(char *buffer_r, std::streamsize maxcount_r)
Definition: GzStream.cc:143
zypp::gzstream_detail::ZlibError
Helper class to ship zlib errors.
Definition: GzStream.h:46
str
String related utilities and Regular expression matching.
zypp::gzstream_detail::gzstreambufimpl::_error
ZlibError _error
Definition: GzStream.h:148
zypp::gzstream_detail::gzstreambufimpl::setZError
void setZError() const
Definition: GzStream.h:138
zypp::gzstream_detail::gzstreambufimpl::tell
off_t tell() const
Definition: GzStream.cc:188
zypp::gzstream_detail::ZlibError::ZlibError
ZlibError()
Definition: GzStream.h:58
zypp::gzstream_detail::ZlibError::strerror
std::string strerror() const
Return string describing the zlib error code.
Definition: GzStream.cc:54
zypp::ofgzstream
detail::fXstream< std::ostream, gzstream_detail::fgzstreambuf > ofgzstream
ostream writing gzip files.
Definition: GzStream.h:162