Add missing output implementation for gzfilebuf.
using std::string;
using std::istream;
+using std::ostream;
sg_gzifstream::sg_gzifstream()
: istream(&gzbuf)
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 );
+}
#include <simgear/compiler.h>
# include <istream>
+# include <ostream>
#include <string>
*/
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 */
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);
if ( own_file_descriptor )
this->close();
delete [] ibuffer;
+ if (obuffer)
+ delete [] obuffer;
}
void
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
}
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() )
*pptr() = c;
pbump(1);
}
-#endif
return 0;
}
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()
{
/**
* 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 );
/** 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); }
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();
int ibuf_size;
char* ibuffer;
+ // Put (output) buffer.
+ int obuf_size;
+ char* obuffer;
+
enum { page_size = 4096 };
private:
gzfilebuf gzbuf;
};
-#endif // _zfstream_hxx
+/**
+ * document me too
+ */
+struct gzofstream_base
+{
+ gzofstream_base() {}
+ gzfilebuf gzbuf;
+};
+
+#endif // _zfstream_hxx