libzypp: zypp-core/base/gzstream.h Source File

libzypp 17.38.14

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_CORE_BASE_GZSTREAM_H
22#define ZYPP_CORE_BASE_GZSTREAM_H
23
24#include <iosfwd>
25#include <streambuf>
26#include <vector>
27#include <zlib.h>
28
29#include <zypp-core/base/SimpleStreambuf>
30#include <zypp-core/base/fXstream>
31
33namespace 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
58 ZlibError()
59 : _zError( 0 ), _errno( 0 )
60 {}
61
65 std::string
66 strerror() const;
67 };
68
69
71 inline std::ostream & operator<<( std::ostream & str, const ZlibError & obj )
72 { return str << obj.strerror(); }
73
74
76 //
77 // CLASS NAME : gzstreambufimpl
90 class gzstreambufimpl {
91 public:
92
93 using error_type = ZlibError;
94
95 ~gzstreambufimpl()
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 };
151 using fgzstreambuf = detail::SimpleStreamBuf<gzstreambufimpl>;
152 } // namespace gzstream_detail
153
157 using ifgzstream = detail::fXstream<std::istream, gzstream_detail::fgzstreambuf>;
158
162 using ofgzstream = detail::fXstream<std::ostream, gzstream_detail::fgzstreambuf>;
163
165} // namespace zypp
167
168#endif // ZYPP_CORE_BASE_GZSTREAM_H
zypp::detail::SimpleStreamBuf
Definition simplestreambuf.h:55
zypp::detail::fXstream
Common template to define ifgzstream/ofgzstream reading/writing compressed files.
Definition fxstream.h:27
zypp::gzstream_detail::gzstreambufimpl
Streambuffer reading or writing gzip files.
Definition gzstream.h:90
zypp::gzstream_detail::gzstreambufimpl::_file
gzFile _file
Definition gzstream.h:144
zypp::gzstream_detail::gzstreambufimpl::setZError
void setZError() const
Definition gzstream.h:138
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::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::gzstream_detail::gzstreambufimpl::canSeek
bool canSeek(std::ios_base::seekdir way_r) const
Definition gzstream.h:111
zypp::gzstream_detail::gzstreambufimpl::tell
off_t tell() const
Definition gzstream.cc:188
zypp::gzstream_detail::gzstreambufimpl::readData
std::streamsize readData(char *buffer_r, std::streamsize maxcount_r)
Definition gzstream.cc:143
zypp::gzstream_detail::gzstreambufimpl::isOpen
bool isOpen() const
Definition gzstream.h:99
zypp::gzstream_detail::gzstreambufimpl::error
error_type error() const
The last error returned fron zlib.
Definition gzstream.h:127
zypp::gzstream_detail::gzstreambufimpl::_error
ZlibError _error
Definition gzstream.h:148
zypp::gzstream_detail::gzstreambufimpl::_fd
int _fd
file descriptor of the compressed file
Definition gzstream.h:142
zypp::gzstream_detail::gzstreambufimpl::error_type
ZlibError error_type
Definition gzstream.h:93
zypp::gzstream_detail::gzstreambufimpl::canWrite
bool canWrite() const
Definition gzstream.h:107
zypp::gzstream_detail::gzstreambufimpl::~gzstreambufimpl
~gzstreambufimpl()
Definition gzstream.h:95
zypp::gzstream_detail::gzstreambufimpl::compressed_tell
off_t compressed_tell() const
Definition gzstream.cc:197
zypp::gzstream_detail::gzstreambufimpl::_mode
std::ios_base::openmode _mode
Definition gzstream.h:146
zypp::gzstream_detail::gzstreambufimpl::canRead
bool canRead() const
Definition gzstream.h:103
zypp::gzstream_detail::gzstreambufimpl::closeImpl
bool closeImpl()
Definition gzstream.cc:110
str
String related utilities and Regular expression matching.
zypp::gzstream_detail
Definition gzstream.cc:39
zypp::gzstream_detail::fgzstreambuf
detail::SimpleStreamBuf< gzstreambufimpl > fgzstreambuf
Definition gzstream.h:151
zypp::gzstream_detail::operator<<
std::ostream & operator<<(std::ostream &str, const ZlibError &obj)
relates: ZlibError Stream output.
Definition gzstream.h:71
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::ifgzstream
detail::fXstream< std::istream, gzstream_detail::fgzstreambuf > ifgzstream
istream reading gzip files as well as plain files.
Definition gzstream.h:157
zypp::ofgzstream
detail::fXstream< std::ostream, gzstream_detail::fgzstreambuf > ofgzstream
ostream writing gzip files.
Definition gzstream.h:162
zypp::gzstream_detail::ZlibError
Helper class to ship zlib errors.
Definition gzstream.h:47
zypp::gzstream_detail::ZlibError::ZlibError
ZlibError()
Definition gzstream.h:58
zypp::gzstream_detail::ZlibError::_errno
int _errno
errno, valid if zError is Z_ERRNO
Definition gzstream.h:56
zypp::gzstream_detail::ZlibError::_zError
int _zError
The zlib error code.
Definition gzstream.h:51
zypp::gzstream_detail::ZlibError::strerror
std::string strerror() const
Return string describing the zlib error code.
Definition gzstream.cc:54