shadow_copy2: add comments explaining decisions in shadow_copy2_strip_snapshot()
[Samba.git] / lib / zlib / contrib / iostream / zfstream.cpp
blobd0cd85faaf5b2260b241b1f05ee88437bd6c6a60
2 #include "zfstream.h"
4 gzfilebuf::gzfilebuf() :
5 file(NULL),
6 mode(0),
7 own_file_descriptor(0)
8 { }
10 gzfilebuf::~gzfilebuf() {
12 sync();
13 if ( own_file_descriptor )
14 close();
18 gzfilebuf *gzfilebuf::open( const char *name,
19 int io_mode ) {
21 if ( is_open() )
22 return NULL;
24 char char_mode[10];
25 char *p = char_mode;
27 if ( io_mode & ios::in ) {
28 mode = ios::in;
29 *p++ = 'r';
30 } else if ( io_mode & ios::app ) {
31 mode = ios::app;
32 *p++ = 'a';
33 } else {
34 mode = ios::out;
35 *p++ = 'w';
38 if ( io_mode & ios::binary ) {
39 mode |= ios::binary;
40 *p++ = 'b';
43 // Hard code the compression level
44 if ( io_mode & (ios::out|ios::app )) {
45 *p++ = '9';
48 // Put the end-of-string indicator
49 *p = '\0';
51 if ( (file = gzopen(name, char_mode)) == NULL )
52 return NULL;
54 own_file_descriptor = 1;
56 return this;
60 gzfilebuf *gzfilebuf::attach( int file_descriptor,
61 int io_mode ) {
63 if ( is_open() )
64 return NULL;
66 char char_mode[10];
67 char *p = char_mode;
69 if ( io_mode & ios::in ) {
70 mode = ios::in;
71 *p++ = 'r';
72 } else if ( io_mode & ios::app ) {
73 mode = ios::app;
74 *p++ = 'a';
75 } else {
76 mode = ios::out;
77 *p++ = 'w';
80 if ( io_mode & ios::binary ) {
81 mode |= ios::binary;
82 *p++ = 'b';
85 // Hard code the compression level
86 if ( io_mode & (ios::out|ios::app )) {
87 *p++ = '9';
90 // Put the end-of-string indicator
91 *p = '\0';
93 if ( (file = gzdopen(file_descriptor, char_mode)) == NULL )
94 return NULL;
96 own_file_descriptor = 0;
98 return this;
102 gzfilebuf *gzfilebuf::close() {
104 if ( is_open() ) {
106 sync();
107 gzclose( file );
108 file = NULL;
112 return this;
116 int gzfilebuf::setcompressionlevel( int comp_level ) {
118 return gzsetparams(file, comp_level, -2);
122 int gzfilebuf::setcompressionstrategy( int comp_strategy ) {
124 return gzsetparams(file, -2, comp_strategy);
129 streampos gzfilebuf::seekoff( streamoff off, ios::seek_dir dir, int which ) {
131 return streampos(EOF);
135 int gzfilebuf::underflow() {
137 // If the file hasn't been opened for reading, error.
138 if ( !is_open() || !(mode & ios::in) )
139 return EOF;
141 // if a buffer doesn't exists, allocate one.
142 if ( !base() ) {
144 if ( (allocate()) == EOF )
145 return EOF;
146 setp(0,0);
148 } else {
150 if ( in_avail() )
151 return (unsigned char) *gptr();
153 if ( out_waiting() ) {
154 if ( flushbuf() == EOF )
155 return EOF;
160 // Attempt to fill the buffer.
162 int result = fillbuf();
163 if ( result == EOF ) {
164 // disable get area
165 setg(0,0,0);
166 return EOF;
169 return (unsigned char) *gptr();
173 int gzfilebuf::overflow( int c ) {
175 if ( !is_open() || !(mode & ios::out) )
176 return EOF;
178 if ( !base() ) {
179 if ( allocate() == EOF )
180 return EOF;
181 setg(0,0,0);
182 } else {
183 if (in_avail()) {
184 return EOF;
186 if (out_waiting()) {
187 if (flushbuf() == EOF)
188 return EOF;
192 int bl = blen();
193 setp( base(), base() + bl);
195 if ( c != EOF ) {
197 *pptr() = c;
198 pbump(1);
202 return 0;
206 int gzfilebuf::sync() {
208 if ( !is_open() )
209 return EOF;
211 if ( out_waiting() )
212 return flushbuf();
214 return 0;
218 int gzfilebuf::flushbuf() {
220 int n;
221 char *q;
223 q = pbase();
224 n = pptr() - q;
226 if ( gzwrite( file, q, n) < n )
227 return EOF;
229 setp(0,0);
231 return 0;
235 int gzfilebuf::fillbuf() {
237 int required;
238 char *p;
240 p = base();
242 required = blen();
244 int t = gzread( file, p, required );
246 if ( t <= 0) return EOF;
248 setg( base(), base(), base()+t);
250 return t;
254 gzfilestream_common::gzfilestream_common() :
255 ios( gzfilestream_common::rdbuf() )
258 gzfilestream_common::~gzfilestream_common()
261 void gzfilestream_common::attach( int fd, int io_mode ) {
263 if ( !buffer.attach( fd, io_mode) )
264 clear( ios::failbit | ios::badbit );
265 else
266 clear();
270 void gzfilestream_common::open( const char *name, int io_mode ) {
272 if ( !buffer.open( name, io_mode ) )
273 clear( ios::failbit | ios::badbit );
274 else
275 clear();
279 void gzfilestream_common::close() {
281 if ( !buffer.close() )
282 clear( ios::failbit | ios::badbit );
286 gzfilebuf *gzfilestream_common::rdbuf()
288 return &buffer;
291 gzifstream::gzifstream() :
292 ios( gzfilestream_common::rdbuf() )
294 clear( ios::badbit );
297 gzifstream::gzifstream( const char *name, int io_mode ) :
298 ios( gzfilestream_common::rdbuf() )
300 gzfilestream_common::open( name, io_mode );
303 gzifstream::gzifstream( int fd, int io_mode ) :
304 ios( gzfilestream_common::rdbuf() )
306 gzfilestream_common::attach( fd, io_mode );
309 gzifstream::~gzifstream() { }
311 gzofstream::gzofstream() :
312 ios( gzfilestream_common::rdbuf() )
314 clear( ios::badbit );
317 gzofstream::gzofstream( const char *name, int io_mode ) :
318 ios( gzfilestream_common::rdbuf() )
320 gzfilestream_common::open( name, io_mode );
323 gzofstream::gzofstream( int fd, int io_mode ) :
324 ios( gzfilestream_common::rdbuf() )
326 gzfilestream_common::attach( fd, io_mode );
329 gzofstream::~gzofstream() { }