1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
10 | (C) SuSE Linux Products GmbH |
11 \----------------------------------------------------------------------/
15 Author: Michael Andres <ma@suse.de>
16 Maintainer: Michael Andres <ma@suse.de>
18 Purpose: Streams reading and writing gzip files.
21 #ifndef ZYPP_BASE_GZSTREAM_H
22 #define ZYPP_BASE_GZSTREAM_H
29 #include "zypp/base/SimpleStreambuf.h"
30 #include "zypp/base/fXstream.h"
32 ///////////////////////////////////////////////////////////////////
34 { /////////////////////////////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////
37 namespace gzstream_detail
38 { /////////////////////////////////////////////////////////////////
40 ///////////////////////////////////////////////////////////////////
42 // CLASS NAME : ZlibError
44 * @short Helper class to ship zlib errors.
54 * errno, valid if zError is Z_ERRNO
59 : _zError( 0 ), _errno( 0 )
63 * Return string describing the zlib error code
68 ///////////////////////////////////////////////////////////////////
70 /** \relates ZlibError Stream output. */
71 inline std::ostream & operator<<( std::ostream & str, const ZlibError & obj )
72 { return str << obj.strerror(); }
75 ///////////////////////////////////////////////////////////////////
77 // CLASS NAME : gzstreambufimpl
79 * @short Streambuffer reading or writing gzip files.
81 * Read and write mode are mutual exclusive. Seek is supported,
82 * but zlib restrictions appy (only forward seek in write mode;
83 * backward seek in read mode might be expensive).Putback is not
86 * Reading plain (no gziped) files is possible as well.
88 * This streambuf is used in @ref ifgzstream and @ref ofgzstream.
90 class gzstreambufimpl {
93 using error_type = ZlibError;
104 { return( _mode == std::ios_base::in ); }
108 { return( _mode == std::ios_base::out ); }
111 canSeek ( std::ios_base::seekdir way_r ) const
112 { return ( way_r == std::ios_base::beg || way_r == std::ios_base::cur ); }
115 bool openImpl( const char * name_r, std::ios_base::openmode mode_r );
118 //! Tell the file position in the compressed file.
119 //! Analogous to tell(2), complementary to gztell.
120 off_t compressed_tell() const;
124 * The last error returned fron zlib.
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 );
139 { gzerror( _file, &_error._zError ); }
141 //! file descriptor of the compressed file
144 gzFile _file = nullptr;
146 std::ios_base::openmode _mode = std::ios_base::openmode(0);
148 mutable ZlibError _error;
151 using fgzstreambuf = detail::SimpleStreamBuf<gzstreambufimpl>;
152 } // namespace gzstream_detail
155 * istream reading gzip files as well as plain files.
157 typedef detail::fXstream<std::istream,gzstream_detail::fgzstreambuf> ifgzstream;
160 * ostream writing gzip files.
162 typedef detail::fXstream<std::ostream,gzstream_detail::fgzstreambuf> ofgzstream;
164 /////////////////////////////////////////////////////////////////
166 ///////////////////////////////////////////////////////////////////
168 #endif // ZYPP_BASE_GZSTREAM_H