libzypp  17.3.1
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
pos_type zSeekTo(off_type off_r, std::ios_base::seekdir way_r)
Definition: GzStream.cc:263
std::vector< char > buffer_type
Definition: GzStream.h:151
gzstream_detail::fXstream< std::istream, gzstream_detail::fgzstreambuf > ifgzstream
istream reading gzip files as well as plain files.
Definition: GzStream.h:275
fgzstreambuf(unsigned bufferSize_r=512)
Definition: GzStream.h:90
virtual pos_type seekpos(pos_type pos_r, std::ios_base::openmode)
Definition: GzStream.h:146
bool zWriteFrom(const char *buffer_r, std::streamsize count_r)
Definition: GzStream.cc:246
String related utilities and Regular expression matching.
int _errno
errno, valid if zError is Z_ERRNO
Definition: GzStream.h:53
ZlibError zError() const
The last error returned retuned fron zlib.
Definition: GzStream.h:244
Definition: Arch.h:344
gzstream_detail::ZlibError ZlibError
Definition: GzStream.h:203
int _fd
file descriptor of the compressed file
Definition: GzStream.h:154
const streambuf_type & getbuf() const
Similar to ios::rdbuf.
Definition: GzStream.h:250
pos_type seekTo(off_type off_r, std::ios_base::seekdir way_r)
Definition: GzStream.cc:291
std::ios_base::openmode defMode(const std::ostream &str_r)
Definition: GzStream.h:262
void open(const char *file_r)
Definition: GzStream.h:225
Common template to define ifgzstream/ofgzstream reading/writing gzip files.
Definition: GzStream.h:199
std::ios_base::openmode _mode
Definition: GzStream.h:158
std::ostream & operator<<(std::ostream &str, const ZlibError &obj)
Definition: GzStream.h:68
fXstream(const char *file_r)
Definition: GzStream.h:212
virtual int_type overflow(int_type c=traits_type::eof())
Definition: GzStream.cc:173
std::streamsize zReadTo(char *buffer_r, std::streamsize maxcount_r)
Definition: GzStream.cc:232
pos_type compressed_tell() const
Tell the file position in the compressed file.
Definition: GzStream.cc:352
gzstream_detail::fXstream< std::ostream, gzstream_detail::fgzstreambuf > ofgzstream
ostream writing gzip files.
Definition: GzStream.h:280
int _zError
The zlib error code.
Definition: GzStream.h:48
ZlibError zError() const
The last error returned fron zlib.
Definition: GzStream.h:127
virtual pos_type seekoff(off_type off_r, std::ios_base::seekdir way_r, std::ios_base::openmode)
Definition: GzStream.h:142
std::ios_base::openmode defMode(const std::istream &str_r)
Definition: GzStream.h:258
std::string strerror() const
Return string describing the zlib error code.
Definition: GzStream.cc:54
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
Helper class to ship zlib errors.
Definition: GzStream.h:43
Streambuffer reading or writing gzip files.
Definition: GzStream.h:86
fgzstreambuf * open(const char *name_r, std::ios_base::openmode mode_r=std::ios_base::in)
Definition: GzStream.cc:74