oct-stdstrm.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_octave_stdiostream_h)
00024 #define octave_octave_stdiostream_h 1
00025 
00026 #include "oct-stream.h"
00027 #include "c-file-ptr-stream.h"
00028 
00029 template <typename BUF_T, typename STREAM_T, typename FILE_T>
00030 class
00031 octave_tstdiostream : public octave_base_stream
00032 {
00033 public:
00034 
00035   octave_tstdiostream (const std::string& n, FILE_T f = 0, int fid = 0,
00036                        std::ios::openmode m = std::ios::in|std::ios::out,
00037                        oct_mach_info::float_format ff
00038                          = oct_mach_info::native_float_format (),
00039                        typename BUF_T::close_fcn cf = BUF_T::file_close)
00040     : octave_base_stream (m, ff), nm (n), md (m),
00041       s (f ? new STREAM_T (f, cf) : 0), fnum (fid)
00042   { }
00043 
00044   // Position a stream at OFFSET relative to ORIGIN.
00045 
00046   int seek (long offset, int origin)
00047     { return s ? s->seek (offset, origin) : -1; }
00048 
00049   // Return current stream position.
00050 
00051   long tell (void) { return s ? s->tell () : -1; }
00052 
00053   // Return non-zero if EOF has been reached on this stream.
00054 
00055   bool eof (void) const { return s ? s->eof () : true; }
00056 
00057   // The name of the file.
00058 
00059   std::string name (void) const { return nm; }
00060 
00061   std::istream *input_stream (void) { return (md & std::ios::in) ? s : 0; }
00062 
00063   std::ostream *output_stream (void) { return (md & std::ios::out) ? s : 0; }
00064 
00065   // FIXME -- should not have to cast away const here.
00066   BUF_T *rdbuf (void) const
00067     { return s ? (const_cast<STREAM_T *> (s))->rdbuf () : 0; }
00068 
00069   int file_number (void) const { return fnum; }
00070 
00071   bool bad (void) const { return s ? s->bad () : true; }
00072 
00073   void clear (void) { if (s) s->clear (); }
00074 
00075   void do_close (void) { if (s) s->stream_close (); }
00076 
00077 protected:
00078 
00079   std::string nm;
00080 
00081   std::ios::openmode md;
00082 
00083   STREAM_T *s;
00084 
00085   // The file number associated with this file.
00086   int fnum;
00087 
00088   ~octave_tstdiostream (void) { delete s; }
00089 
00090 private:
00091 
00092   // No copying!
00093 
00094   octave_tstdiostream (const octave_tstdiostream&);
00095 
00096   octave_tstdiostream& operator = (const octave_tstdiostream&);
00097 };
00098 
00099 class
00100 octave_stdiostream
00101   : public octave_tstdiostream<c_file_ptr_buf, io_c_file_ptr_stream, FILE *>
00102 {
00103 public:
00104 
00105   octave_stdiostream (const std::string& n, FILE *f = 0,
00106                       std::ios::openmode m = std::ios::in|std::ios::out,
00107                       oct_mach_info::float_format ff
00108                         = oct_mach_info::native_float_format (),
00109                       c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::file_close)
00110     : octave_tstdiostream<c_file_ptr_buf, io_c_file_ptr_stream, FILE *> (n, f, f ? fileno (f) : -1, m, ff, cf) { }
00111 
00112   static octave_stream
00113   create (const std::string& n, FILE *f = 0,
00114           std::ios::openmode m = std::ios::in|std::ios::out,
00115           oct_mach_info::float_format ff
00116             = oct_mach_info::native_float_format (),
00117           c_file_ptr_buf::close_fcn cf = c_file_ptr_buf::file_close)
00118   {
00119     return octave_stream (new octave_stdiostream (n, f, m, ff, cf));
00120   }
00121 
00122 protected:
00123 
00124   ~octave_stdiostream (void) { }
00125 
00126 private:
00127 
00128   // No copying!
00129 
00130   octave_stdiostream (const octave_stdiostream&);
00131 
00132   octave_stdiostream& operator = (const octave_stdiostream&);
00133 };
00134 
00135 #ifdef HAVE_ZLIB
00136 
00137 class
00138 octave_zstdiostream
00139   : public octave_tstdiostream<c_zfile_ptr_buf, io_c_zfile_ptr_stream, gzFile>
00140 {
00141 public:
00142 
00143   octave_zstdiostream (const std::string& n, gzFile f = 0, int fid = 0,
00144                        std::ios::openmode m = std::ios::in|std::ios::out,
00145                        oct_mach_info::float_format ff
00146                          = oct_mach_info::native_float_format (),
00147                        c_zfile_ptr_buf::close_fcn cf = c_zfile_ptr_buf::file_close)
00148     : octave_tstdiostream<c_zfile_ptr_buf, io_c_zfile_ptr_stream, gzFile> (n, f, fid, m, ff, cf) { }
00149 
00150   static octave_stream
00151   create (const std::string& n, gzFile f = 0, int fid = 0,
00152           std::ios::openmode m = std::ios::in|std::ios::out,
00153           oct_mach_info::float_format ff
00154             = oct_mach_info::native_float_format (),
00155           c_zfile_ptr_buf::close_fcn cf = c_zfile_ptr_buf::file_close)
00156   {
00157     return octave_stream (new octave_zstdiostream (n, f, fid, m, ff, cf));
00158   }
00159 
00160 protected:
00161 
00162   ~octave_zstdiostream (void) { }
00163 
00164 private:
00165 
00166   // No copying!
00167 
00168   octave_zstdiostream (const octave_zstdiostream&);
00169 
00170   octave_zstdiostream& operator = (const octave_zstdiostream&);
00171 };
00172 
00173 #endif
00174 
00175 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines