diff src/c-file-ptr-stream.h @ 5325:ecbe4aa87e51

[project @ 2005-04-29 20:48:35 by jwe]
author jwe
date Fri, 29 Apr 2005 20:48:36 +0000
parents 4c8a2e4e0717
children ace8d8d26933
line wrap: on
line diff
--- a/src/c-file-ptr-stream.h
+++ b/src/c-file-ptr-stream.h
@@ -94,18 +94,21 @@
   int_type underflow_common (bool);
 };
 
+// XXX FIXME XXX -- the following three classes could probably share
+// some code...
+
+template <typename STREAM_T, typename FILE_T, typename BUF_T>
 class
-i_c_file_ptr_stream : public std::istream
+c_file_ptr_stream : public STREAM_T
 {
 public:
 
-  i_c_file_ptr_stream (FILE* f,
-		       c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::fclose)
-    : std::istream (0), buf (new c_file_ptr_buf (f, cf)) { init (buf); }
+  c_file_ptr_stream (FILE_T f, typename BUF_T::close_fcn cf = BUF_T::fclose)
+    : STREAM_T (0), buf (new BUF_T (f, cf)) { init (buf); }
 
-  ~i_c_file_ptr_stream (void) { delete buf; buf = 0; }
+  ~c_file_ptr_stream (void) { delete buf; buf = 0; }
 
-  c_file_ptr_buf *rdbuf (void) { return buf; }
+  BUF_T *rdbuf (void) { return buf; }
 
   void close (void) { if (buf) buf->close (); }
 
@@ -114,66 +117,95 @@
 
   long tell (void) { return buf ? buf->tell () : -1; }
 
-  void clear (void) { if (buf) buf->clear (); std::istream::clear (); }
+  void clear (void) { if (buf) buf->clear (); STREAM_T::clear (); }
 
 private:
 
-  c_file_ptr_buf *buf;
+  BUF_T *buf;
 };
 
+typedef c_file_ptr_stream<std::istream, FILE *, c_file_ptr_buf> i_c_file_ptr_stream;
+typedef c_file_ptr_stream<std::ostream, FILE *, c_file_ptr_buf> o_c_file_ptr_stream;
+typedef c_file_ptr_stream<std::iostream, FILE *, c_file_ptr_buf> io_c_file_ptr_stream;
+
+#ifdef HAVE_ZLIB
+
+#ifdef HAVE_ZLIB_H
+#include <zlib.h>
+#endif
+
 class
-o_c_file_ptr_stream : public std::ostream
+c_zfile_ptr_buf : public std::streambuf
 {
 public:
 
-  o_c_file_ptr_stream (FILE* f,
-		       c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::fclose)
-    : std::ostream (0), buf (new c_file_ptr_buf (f, cf)) { init (buf); }
+#if !defined (CXX_ISO_COMPLIANT_LIBRARY)
+  typedef int int_type;
+#else
+  typedef std::streambuf::int_type int_type;
+#endif
+
+  typedef int (*close_fcn) (gzFile);
+
+  gzFile stdiofile (void) { return f; }
+
+  c_zfile_ptr_buf (gzFile f_arg, close_fcn cf_arg = fclose)
+    : std::streambuf (), f (f_arg), cf (cf_arg)
+    { }
+
+  ~c_zfile_ptr_buf (void);
+
+  int_type overflow (int_type);
+
+  int_type underflow (void) { return underflow_common (false); }
 
-  ~o_c_file_ptr_stream (void) { delete buf; buf = 0; }
+  int_type uflow (void) { return underflow_common (true); }
+
+  int_type pbackfail (int_type);
+
+  std::streamsize xsputn (const char*, std::streamsize);
+
+  std::streamsize xsgetn (char *, std::streamsize);
 
-  c_file_ptr_buf *rdbuf (void) { return buf; }
+  std::streampos seekoff (std::streamoff, std::ios::seekdir,
+			  std::ios::openmode = std::ios::in | std::ios::out);
+  
+  std::streampos seekpos (std::streampos,
+			  std::ios::openmode = std::ios::in | std::ios::out);
 
-  void close (void) { if (buf) buf->close (); }
+  int sync (void);
+
+  int flush (void);
+
+  int close (void);
+
+  int file_number () const { return -1; }
 
   int seek (long offset, int origin)
-    { return buf ? buf->seek (offset, origin) : -1; }
+    { return f ? gzseek (f, offset, origin) : -1; }
+
+  long tell (void) { return f ? gztell (f) : -1; }
+
+  void clear (void) { if (f) gzclearerr (f); }
 
-  long tell (void) { return buf ? buf->tell () : -1; }
+  static int fclose (gzFile f) { return ::gzclose (f); }
+
+protected:
 
-  void clear (void) { if (buf) buf->clear (); std::ostream::clear (); }
+  gzFile f;
+
+  close_fcn cf;
 
 private:
 
-  c_file_ptr_buf *buf;
+  int_type underflow_common (bool);
 };
 
-class
-io_c_file_ptr_stream : public std::iostream
-{
-public:
-
-  io_c_file_ptr_stream (FILE* f,
-			c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::fclose)
-    : std::iostream (0), buf (new c_file_ptr_buf (f, cf)) { init (buf); }
-
-  ~io_c_file_ptr_stream (void) { delete buf; buf = 0; }
-
-  c_file_ptr_buf *rdbuf (void) { return buf; }
+typedef c_file_ptr_stream<std::istream, gzFile, c_zfile_ptr_buf> i_c_zfile_ptr_stream;
+typedef c_file_ptr_stream<std::ostream, gzFile, c_zfile_ptr_buf> o_c_zfile_ptr_stream;
+typedef c_file_ptr_stream<std::iostream, gzFile, c_zfile_ptr_buf> io_c_zfile_ptr_stream;
 
-  void close (void) { if (buf) buf->close (); }
-
-  int seek (long offset, int origin)
-    { return buf ? buf->seek (offset, origin) : -1; }
-
-  long tell (void) { return buf ? buf->tell () : -1; }
-
-  void clear (void) { if (buf) buf->clear (); std::iostream::clear (); }
-
-private:
-
-  c_file_ptr_buf *buf;
-};
+#endif
 
 #endif