Initial revision
[official-gcc.git] / zlib / contrib / iostream / zfstream.cpp
bloba690bbefceb4183dff61c11138386b8bc28306b6
2 #include <memory.h>
3 #include "zfstream.h"
5 gzfilebuf::gzfilebuf() :
6 file(NULL),
7 mode(0),
8 own_file_descriptor(0)
9 { }
11 gzfilebuf::~gzfilebuf() {
13 sync();
14 if ( own_file_descriptor )
15 close();
19 gzfilebuf *gzfilebuf::open( const char *name,
20 int io_mode ) {
22 if ( is_open() )
23 return NULL;
25 char char_mode[10];
26 char *p;
27 memset(char_mode,'\0',10);
28 p = char_mode;
30 if ( io_mode & ios::in ) {
31 mode = ios::in;
32 *p++ = 'r';
33 } else if ( io_mode & ios::app ) {
34 mode = ios::app;
35 *p++ = 'a';
36 } else {
37 mode = ios::out;
38 *p++ = 'w';
41 if ( io_mode & ios::binary ) {
42 mode |= ios::binary;
43 *p++ = 'b';
46 // Hard code the compression level
47 if ( io_mode & (ios::out|ios::app )) {
48 *p++ = '9';
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;
68 memset(char_mode,'\0',10);
69 p = char_mode;
71 if ( io_mode & ios::in ) {
72 mode = ios::in;
73 *p++ = 'r';
74 } else if ( io_mode & ios::app ) {
75 mode = ios::app;
76 *p++ = 'a';
77 } else {
78 mode = ios::out;
79 *p++ = 'w';
82 if ( io_mode & ios::binary ) {
83 mode |= ios::binary;
84 *p++ = 'b';
87 // Hard code the compression level
88 if ( io_mode & (ios::out|ios::app )) {
89 *p++ = '9';
92 if ( (file = gzdopen(file_descriptor, char_mode)) == NULL )
93 return NULL;
95 own_file_descriptor = 0;
97 return this;
101 gzfilebuf *gzfilebuf::close() {
103 if ( is_open() ) {
105 sync();
106 gzclose( file );
107 file = NULL;
111 return this;
115 int gzfilebuf::setcompressionlevel( short comp_level ) {
117 return gzsetparams(file, comp_level, -2);
121 int gzfilebuf::setcompressionstrategy( short comp_strategy ) {
123 return gzsetparams(file, -2, comp_strategy);
128 streampos gzfilebuf::seekoff( streamoff off, ios::seek_dir dir, int which ) {
130 return streampos(EOF);
134 int gzfilebuf::underflow() {
136 // If the file hasn't been opened for reading, error.
137 if ( !is_open() || !(mode & ios::in) )
138 return EOF;
140 // if a buffer doesn't exists, allocate one.
141 if ( !base() ) {
143 if ( (allocate()) == EOF )
144 return EOF;
145 setp(0,0);
147 } else {
149 if ( in_avail() )
150 return (unsigned char) *gptr();
152 if ( out_waiting() ) {
153 if ( flushbuf() == EOF )
154 return EOF;
159 // Attempt to fill the buffer.
161 int result = fillbuf();
162 if ( result == EOF ) {
163 // disable get area
164 setg(0,0,0);
165 return EOF;
168 return (unsigned char) *gptr();
172 int gzfilebuf::overflow( int c ) {
174 if ( !is_open() || !(mode & ios::out) )
175 return EOF;
177 if ( !base() ) {
178 if ( allocate() == EOF )
179 return EOF;
180 setg(0,0,0);
181 } else {
182 if (in_avail()) {
183 return EOF;
185 if (out_waiting()) {
186 if (flushbuf() == EOF)
187 return EOF;
191 int bl = blen();
192 setp( base(), base() + bl);
194 if ( c != EOF ) {
196 *pptr() = c;
197 pbump(1);
201 return 0;
205 int gzfilebuf::sync() {
207 if ( !is_open() )
208 return EOF;
210 if ( out_waiting() )
211 return flushbuf();
213 return 0;
217 int gzfilebuf::flushbuf() {
219 int n;
220 char *q;
222 q = pbase();
223 n = pptr() - q;
225 if ( gzwrite( file, q, n) < n )
226 return EOF;
228 setp(0,0);
230 return 0;
234 int gzfilebuf::fillbuf() {
236 int required;
237 char *p;
239 p = base();
241 required = blen();
243 int t = gzread( file, p, required );
245 if ( t <= 0) return EOF;
247 setg( base(), base(), base()+t);
249 return t;
253 gzfilestream_common::gzfilestream_common() :
254 ios( gzfilestream_common::rdbuf() )
257 gzfilestream_common::~gzfilestream_common()
260 void gzfilestream_common::attach( int fd, int io_mode ) {
262 if ( !buffer.attach( fd, io_mode) )
263 clear( ios::failbit | ios::badbit );
264 else
265 clear();
269 void gzfilestream_common::open( const char *name, int io_mode ) {
271 if ( !buffer.open( name, io_mode ) )
272 clear( ios::failbit | ios::badbit );
273 else
274 clear();
278 void gzfilestream_common::close() {
280 if ( !buffer.close() )
281 clear( ios::failbit | ios::badbit );
285 gzfilebuf *gzfilestream_common::rdbuf() {
287 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() { }