From: ThorstenB Date: Wed, 7 Nov 2012 22:44:44 +0000 (+0100) Subject: sgstream: implement gzipped output file stream (gzofstream) X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=b70b81f4cc298317c71faef348a8bab5e1703780;p=simgear.git sgstream: implement gzipped output file stream (gzofstream) Add missing output implementation for gzfilebuf. --- diff --git a/simgear/misc/sgstream.cxx b/simgear/misc/sgstream.cxx index 6d5352c8..70da33af 100644 --- a/simgear/misc/sgstream.cxx +++ b/simgear/misc/sgstream.cxx @@ -29,6 +29,7 @@ using std::string; using std::istream; +using std::ostream; sg_gzifstream::sg_gzifstream() : istream(&gzbuf) @@ -146,3 +147,44 @@ skipcomment( istream& in ) return in; } + +sg_gzofstream::sg_gzofstream() + : ostream(&gzbuf) +{ +} + +//----------------------------------------------------------------------------- +// +// Open a file for gzipped writing. +// +sg_gzofstream::sg_gzofstream( const string& name, ios_openmode io_mode ) + : ostream(&gzbuf) +{ + this->open( name, io_mode ); +} + +//----------------------------------------------------------------------------- +// +// Attach a stream to an already opened file descriptor. +// +sg_gzofstream::sg_gzofstream( int fd, ios_openmode io_mode ) + : ostream(&gzbuf) +{ + gzbuf.attach( fd, io_mode ); +} + +//----------------------------------------------------------------------------- +// +// Open a file for gzipped writing. +// +void +sg_gzofstream::open( const string& name, ios_openmode io_mode ) +{ + gzbuf.open( name.c_str(), io_mode ); +} + +void +sg_gzofstream::attach( int fd, ios_openmode io_mode ) +{ + gzbuf.attach( fd, io_mode ); +} diff --git a/simgear/misc/sgstream.hxx b/simgear/misc/sgstream.hxx index a0414b18..35c64459 100644 --- a/simgear/misc/sgstream.hxx +++ b/simgear/misc/sgstream.hxx @@ -34,6 +34,7 @@ #include # include +# include #include @@ -115,6 +116,58 @@ std::istream& skipws( std::istream& in ); */ std::istream& skipcomment( std::istream& in ); +/** + * An envelope class for gzofstream. + */ +class sg_gzofstream : private gzofstream_base, public std::ostream +{ +public: + /** Default constructor */ + sg_gzofstream(); + + /** + * Constructor to open a file for writing. + * @param name name of file + * @param io_mode file open mode(s) "or'd" together + */ + sg_gzofstream( const std::string& name, + ios_openmode io_mode = ios_out | ios_binary ); + + /** + * Constructor that attaches itself to an existing file descriptor. + * @param fd file descriptor + * @param io_mode file open mode(s) "or'd" together + */ + sg_gzofstream( int fd, ios_openmode io_mode = ios_out|ios_binary ); + + /** + * Attempt to open a file for writing. + * @param name name of file + * @param io_mode file open mode(s) "or'd" together + */ + void open( const std::string& name, + ios_openmode io_mode = ios_out|ios_binary ); + + /** + * Attach to an existing file descriptor. + * @param fd file descriptor + * @param io_mode file open mode(s) "or'd" together + */ + void attach( int fd, ios_openmode io_mode = ios_out|ios_binary ); + + /** + * Close the stream. + */ + void close() { gzbuf.close(); } + + /** @return true if the file is successfully opened, false otherwise. */ + bool is_open() { return gzbuf.is_open(); } + +private: + // Not defined! + sg_gzofstream( const sg_gzofstream& ); + void operator= ( const sg_gzofstream& ); +}; #endif /* _SGSTREAM_HXX */ diff --git a/simgear/misc/zfstream.cxx b/simgear/misc/zfstream.cxx index 2e50eba1..b5755625 100644 --- a/simgear/misc/zfstream.cxx +++ b/simgear/misc/zfstream.cxx @@ -39,7 +39,9 @@ gzfilebuf::gzfilebuf() mode(ios_openmode(0)), own_file_descriptor(false), ibuf_size(0), - ibuffer(0) + ibuffer(0), + obuf_size(0), + obuffer(0) { // try { ibuf_size = page_size / sizeof(char); @@ -59,6 +61,8 @@ gzfilebuf::~gzfilebuf() if ( own_file_descriptor ) this->close(); delete [] ibuffer; + if (obuffer) + delete [] obuffer; } void @@ -153,17 +157,17 @@ gzfilebuf::close() return this; } -// int -// gzfilebuf::setcompressionlevel( int comp_level ) -// { -// return gzsetparams(file, comp_level, -2); -// } +int +gzfilebuf::setcompressionlevel( int comp_level ) +{ + return gzsetparams(file, comp_level, -2); +} -// int -// gzfilebuf::setcompressionstrategy( int comp_strategy ) -// { -// return gzsetparams(file, -2, comp_strategy); -// } +int +gzfilebuf::setcompressionstrategy( int comp_strategy ) +{ + return gzsetparams(file, -2, comp_strategy); +} std::streampos @@ -173,10 +177,9 @@ gzfilebuf::seekoff( std::streamoff, ios_seekdir, ios_openmode ) } gzfilebuf::int_type -gzfilebuf::overflow( int_type ) +gzfilebuf::overflow( int_type c ) { -#if 0 - if ( !is_open() || !(mode & ios::out) ) + if ( !is_open() || !(mode & ios_out) ) return EOF; if ( !base() ) @@ -207,7 +210,6 @@ gzfilebuf::overflow( int_type ) *pptr() = c; pbump(1); } -#endif return 0; } @@ -223,6 +225,22 @@ gzfilebuf::sync() return 0; } +bool +gzfilebuf::out_waiting() +{ + char* q = pbase(); + int n = pptr() - q; + return n>0; +} + +char +gzfilebuf::allocate() +{ + obuf_size = page_size / sizeof(char); + obuffer = new char [ibuf_size]; + return 0; +} + gzfilebuf::int_type gzfilebuf::flushbuf() { diff --git a/simgear/misc/zfstream.hxx b/simgear/misc/zfstream.hxx index a9f065ea..635397a7 100644 --- a/simgear/misc/zfstream.hxx +++ b/simgear/misc/zfstream.hxx @@ -67,7 +67,7 @@ public: /** * Open a stream * @param name file name - * @param io_mode mdoe flags + * @param io_mode mode flags * @return file stream */ gzfilebuf* open( const char* name, ios_openmode io_mode ); @@ -83,8 +83,8 @@ public: /** Close stream */ gzfilebuf* close(); - // int setcompressionlevel( int comp_level ); - // int setcompressionstrategy( int comp_strategy ); + int setcompressionlevel( int comp_level ); + int setcompressionstrategy( int comp_strategy ); /** @return true if open, false otherwise */ bool is_open() const { return (file != NULL); } @@ -100,6 +100,11 @@ protected: virtual int_type underflow(); virtual int_type overflow( int_type c = parent::traits_type::eof() ); + bool out_waiting(); + char* base() {return obuffer;} + int blen() {return ibuf_size;} + char allocate(); + private: int_type flushbuf(); @@ -118,6 +123,10 @@ private: int ibuf_size; char* ibuffer; + // Put (output) buffer. + int obuf_size; + char* obuffer; + enum { page_size = 4096 }; private: @@ -136,5 +145,14 @@ struct gzifstream_base gzfilebuf gzbuf; }; -#endif // _zfstream_hxx +/** + * document me too + */ +struct gzofstream_base +{ + gzofstream_base() {} + gzfilebuf gzbuf; +}; + +#endif // _zfstream_hxx