libzypp  17.7.0
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 namespace zypp
31 {
32 
34  namespace gzstream_detail
35  {
36 
38  //
39  // CLASS NAME : ZlibError
43  struct ZlibError
44  {
48  int _zError;
49 
53  int _errno;
54 
56  : _zError( 0 ), _errno( 0 )
57  {}
58 
62  std::string
63  strerror() const;
64  };
66 
68  inline std::ostream & operator<<( std::ostream & str, const ZlibError & obj )
69  { return str << obj.strerror(); }
70 
72  //
73  // CLASS NAME : fgzstreambuf
86  class fgzstreambuf : public std::streambuf {
87 
88  public:
89 
90  fgzstreambuf( unsigned bufferSize_r = 512 )
91  : _fd( -1 )
92  ,_file( NULL )
93  , _mode( std::ios_base::openmode(0) )
94  , _buffer( (bufferSize_r?bufferSize_r:1), 0 )
95  {}
96 
97  virtual
99  { close(); }
100 
101  bool
102  isOpen() const
103  { return _file; }
104 
105  bool
106  inReadMode() const
107  { return( _mode == std::ios_base::in ); }
108 
109  bool
110  inWriteMode() const
111  { return( _mode == std::ios_base::out ); }
112 
113  fgzstreambuf *
114  open( const char * name_r, std::ios_base::openmode mode_r = std::ios_base::in );
115 
116  fgzstreambuf *
117  close();
118 
121  pos_type compressed_tell() const;
122 
126  ZlibError
127  zError() const
128  { return _error; }
129 
130  protected:
131 
132  virtual int
133  sync();
134 
135  virtual int_type
136  overflow( int_type c = traits_type::eof() );
137 
138  virtual int_type
139  underflow();
140 
141  virtual pos_type
142  seekoff( off_type off_r, std::ios_base::seekdir way_r, std::ios_base::openmode /* ignored */ )
143  { return seekTo( off_r, way_r ); }
144 
145  virtual pos_type
146  seekpos( pos_type pos_r, std::ios_base::openmode /* ignored */ )
147  { return seekTo( off_type(pos_r), std::ios_base::beg ); }
148 
149  private:
150 
151  typedef std::vector<char> buffer_type;
152 
154  int _fd;
155 
156  gzFile _file;
157 
158  std::ios_base::openmode _mode;
159 
161 
163 
164  private:
165 
166  void
168  { gzerror( _file, &_error._zError ); }
169 
170  std::streamsize
171  zReadTo( char * buffer_r, std::streamsize maxcount_r );
172 
173  bool
174  zWriteFrom( const char * buffer_r, std::streamsize count_r );
175 
176  pos_type
177  zSeekTo( off_type off_r, std::ios_base::seekdir way_r );
178 
179  pos_type
180  zTell();
181 
182  pos_type
183  seekTo( off_type off_r, std::ios_base::seekdir way_r );
184  };
186 
188  //
189  // CLASS NAME : fXstream<class TBStr,class TSBuf>
198  template<class TBStream,class TStreamBuf>
199  class fXstream : public TBStream
200  {
201  public:
202 
205  typedef TStreamBuf streambuf_type;
206 
208  : stream_type( NULL )
209  { this->init( &_streambuf ); }
210 
211  explicit
212  fXstream( const char * file_r )
213  : stream_type( NULL )
214  { this->init( &_streambuf ); this->open( file_r ); }
215 
216  virtual
218  {}
219 
220  bool
221  is_open() const
222  { return _streambuf.isOpen(); }
223 
224  void
225  open( const char * file_r )
226  {
227  if ( !_streambuf.open( file_r, defMode(*this) ) )
228  this->setstate(std::ios_base::failbit);
229  else
230  this->clear();
231  }
232 
233  void
235  {
236  if ( !_streambuf.close() )
237  this->setstate(std::ios_base::failbit);
238  }
239 
243  ZlibError
244  zError() const
245  { return _streambuf.zError(); }
246 
249  const streambuf_type&
250  getbuf() const
251  { return _streambuf; }
252 
253  private:
254 
256 
257  std::ios_base::openmode
258  defMode( const std::istream & str_r )
259  { return std::ios_base::in; }
260 
261  std::ios_base::openmode
262  defMode( const std::ostream & str_r )
263  { return std::ios_base::out; }
264 
265  };
267 
269  } // namespace gzstream_detail
271 
276 
281 
283 } // namespace zypp
285 
286 #endif // ZYPP_BASE_GZSTREAM_H
zypp::gzstream_detail::fgzstreambuf::seekpos
virtual pos_type seekpos(pos_type pos_r, std::ios_base::openmode)
Definition: GzStream.h:146
zypp::gzstream_detail::fXstream::zError
ZlibError zError() const
The last error returned retuned fron zlib.
Definition: GzStream.h:244
zypp::gzstream_detail::fXstream::~fXstream
virtual ~fXstream()
Definition: GzStream.h:217
zypp::gzstream_detail::fgzstreambuf::compressed_tell
pos_type compressed_tell() const
Tell the file position in the compressed file.
Definition: GzStream.cc:352
zypp::gzstream_detail::fgzstreambuf::zError
ZlibError zError() const
The last error returned fron zlib.
Definition: GzStream.h:127
zypp::gzstream_detail::fgzstreambuf::_file
gzFile _file
Definition: GzStream.h:156
zypp::gzstream_detail::fgzstreambuf::_fd
int _fd
file descriptor of the compressed file
Definition: GzStream.h:154
zypp::gzstream_detail::fgzstreambuf::zTell
pos_type zTell()
Definition: GzStream.cc:277
zypp::gzstream_detail::ZlibError::_zError
int _zError
The zlib error code.
Definition: GzStream.h:48
zypp::gzstream_detail::fgzstreambuf::setZError
void setZError()
Definition: GzStream.h:167
zypp::gzstream_detail::ZlibError::operator<<
std::ostream & operator<<(std::ostream &str, const ZlibError &obj)
Definition: GzStream.h:68
zypp::gzstream_detail::fgzstreambuf::_mode
std::ios_base::openmode _mode
Definition: GzStream.h:158
zypp::gzstream_detail::fXstream::is_open
bool is_open() const
Definition: GzStream.h:221
zypp::gzstream_detail::fXstream::fXstream
fXstream(const char *file_r)
Definition: GzStream.h:212
zypp::gzstream_detail::fgzstreambuf::zReadTo
std::streamsize zReadTo(char *buffer_r, std::streamsize maxcount_r)
Definition: GzStream.cc:232
zypp::gzstream_detail::fXstream::ZlibError
gzstream_detail::ZlibError ZlibError
Definition: GzStream.h:203
zypp::gzstream_detail::fgzstreambuf::_error
ZlibError _error
Definition: GzStream.h:162
zypp::gzstream_detail::fgzstreambuf::seekTo
pos_type seekTo(off_type off_r, std::ios_base::seekdir way_r)
Definition: GzStream.cc:291
zypp::gzstream_detail::fgzstreambuf
Streambuffer reading or writing gzip files.
Definition: GzStream.h:86
zypp::gzstream_detail::fgzstreambuf::inReadMode
bool inReadMode() const
Definition: GzStream.h:106
zypp::gzstream_detail::fgzstreambuf::underflow
virtual int_type underflow()
Definition: GzStream.cc:202
zypp::gzstream_detail::fXstream::_streambuf
streambuf_type _streambuf
Definition: GzStream.h:255
zypp::gzstream_detail::fgzstreambuf::buffer_type
std::vector< char > buffer_type
Definition: GzStream.h:151
zypp::gzstream_detail::fXstream
Common template to define ifgzstream/ofgzstream reading/writing gzip files.
Definition: GzStream.h:199
zypp::gzstream_detail::fXstream::defMode
std::ios_base::openmode defMode(const std::ostream &str_r)
Definition: GzStream.h:262
zypp::gzstream_detail::ZlibError::_errno
int _errno
errno, valid if zError is Z_ERRNO
Definition: GzStream.h:53
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
zypp::gzstream_detail::fgzstreambuf::isOpen
bool isOpen() const
Definition: GzStream.h:102
zypp::ofgzstream
gzstream_detail::fXstream< std::ostream, gzstream_detail::fgzstreambuf > ofgzstream
ostream writing gzip files.
Definition: GzStream.h:280
zypp::gzstream_detail::fgzstreambuf::sync
virtual int sync()
Definition: GzStream.cc:156
zypp::gzstream_detail::fXstream::stream_type
TBStream stream_type
Definition: GzStream.h:204
zypp::gzstream_detail::fXstream::streambuf_type
TStreamBuf streambuf_type
Definition: GzStream.h:205
zypp::gzstream_detail::fXstream::fXstream
fXstream()
Definition: GzStream.h:207
zypp::gzstream_detail::fgzstreambuf::seekoff
virtual pos_type seekoff(off_type off_r, std::ios_base::seekdir way_r, std::ios_base::openmode)
Definition: GzStream.h:142
zypp::gzstream_detail::fgzstreambuf::close
fgzstreambuf * close()
Definition: GzStream.cc:120
zypp::gzstream_detail::fgzstreambuf::open
fgzstreambuf * open(const char *name_r, std::ios_base::openmode mode_r=std::ios_base::in)
Definition: GzStream.cc:74
zypp::gzstream_detail::fXstream::close
void close()
Definition: GzStream.h:234
zypp::gzstream_detail::fXstream::defMode
std::ios_base::openmode defMode(const std::istream &str_r)
Definition: GzStream.h:258
std
Definition: Arch.h:344
TBStream
zypp::gzstream_detail::fgzstreambuf::fgzstreambuf
fgzstreambuf(unsigned bufferSize_r=512)
Definition: GzStream.h:90
zypp::gzstream_detail::fgzstreambuf::~fgzstreambuf
virtual ~fgzstreambuf()
Definition: GzStream.h:98
zypp::gzstream_detail::fgzstreambuf::zSeekTo
pos_type zSeekTo(off_type off_r, std::ios_base::seekdir way_r)
Definition: GzStream.cc:263
zypp::gzstream_detail::fgzstreambuf::_buffer
buffer_type _buffer
Definition: GzStream.h:160
zypp::gzstream_detail::ZlibError
Helper class to ship zlib errors.
Definition: GzStream.h:43
str
String related utilities and Regular expression matching.
zypp::gzstream_detail::fgzstreambuf::zWriteFrom
bool zWriteFrom(const char *buffer_r, std::streamsize count_r)
Definition: GzStream.cc:246
zypp::gzstream_detail::fgzstreambuf::overflow
virtual int_type overflow(int_type c=traits_type::eof())
Definition: GzStream.cc:173
zypp::gzstream_detail::fgzstreambuf::inWriteMode
bool inWriteMode() const
Definition: GzStream.h:110
zypp::gzstream_detail::fXstream::open
void open(const char *file_r)
Definition: GzStream.h:225
zypp::ifgzstream
gzstream_detail::fXstream< std::istream, gzstream_detail::fgzstreambuf > ifgzstream
istream reading gzip files as well as plain files.
Definition: GzStream.h:275
zypp::gzstream_detail::ZlibError::ZlibError
ZlibError()
Definition: GzStream.h:55
zypp::gzstream_detail::fXstream::getbuf
const streambuf_type & getbuf() const
Similar to ios::rdbuf.
Definition: GzStream.h:250
zypp::gzstream_detail::ZlibError::strerror
std::string strerror() const
Return string describing the zlib error code.
Definition: GzStream.cc:54