Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #if !defined (octave_c_file_ptr_stream_h)
00024 #define octave_c_file_ptr_stream_h 1
00025
00026 #include <cstdio>
00027
00028 #include <iosfwd>
00029
00030 class
00031 c_file_ptr_buf : public std::streambuf
00032 {
00033 public:
00034
00035 #if !defined (CXX_ISO_COMPLIANT_LIBRARY)
00036 typedef int int_type;
00037 #else
00038 typedef std::streambuf::int_type int_type;
00039 #endif
00040
00041 typedef int (*close_fcn) (FILE *);
00042
00043 FILE* stdiofile (void) { return f; }
00044
00045 c_file_ptr_buf (FILE *f_arg, close_fcn cf_arg = file_close)
00046 : std::streambuf (), f (f_arg), cf (cf_arg)
00047 { }
00048
00049 ~c_file_ptr_buf (void);
00050
00051 int_type overflow (int_type);
00052
00053 int_type underflow (void) { return underflow_common (false); }
00054
00055 int_type uflow (void) { return underflow_common (true); }
00056
00057 int_type pbackfail (int_type);
00058
00059 std::streamsize xsputn (const char*, std::streamsize);
00060
00061 std::streamsize xsgetn (char *, std::streamsize);
00062
00063 std::streampos seekoff (std::streamoff, std::ios::seekdir,
00064 std::ios::openmode = std::ios::in | std::ios::out);
00065
00066 std::streampos seekpos (std::streampos,
00067 std::ios::openmode = std::ios::in | std::ios::out);
00068
00069 int sync (void);
00070
00071 int flush (void);
00072
00073 int buf_close (void);
00074
00075 int file_number () const { return f ? fileno (f) : -1; }
00076
00077 int seek (long offset, int origin)
00078 { return f ? fseek (f, offset, origin) : -1; }
00079
00080 long tell (void) { return f ? ftell (f) : -1; }
00081
00082 void clear (void) { if (f) clearerr (f); }
00083
00084 static int file_close (FILE *f);
00085
00086 protected:
00087
00088 FILE *f;
00089
00090 close_fcn cf;
00091
00092 private:
00093
00094 int_type underflow_common (bool);
00095
00096
00097
00098 c_file_ptr_buf (const c_file_ptr_buf&);
00099
00100 c_file_ptr_buf& operator = (const c_file_ptr_buf&);
00101 };
00102
00103
00104
00105
00106 template <typename STREAM_T, typename FILE_T, typename BUF_T>
00107 class
00108 c_file_ptr_stream : public STREAM_T
00109 {
00110 public:
00111
00112 c_file_ptr_stream (FILE_T f, typename BUF_T::close_fcn cf = BUF_T::file_close)
00113 : STREAM_T (0), buf (new BUF_T (f, cf)) { STREAM_T::init (buf); }
00114
00115 ~c_file_ptr_stream (void) { delete buf; buf = 0; }
00116
00117 BUF_T *rdbuf (void) { return buf; }
00118
00119 void stream_close (void) { if (buf) buf->buf_close (); }
00120
00121 int seek (long offset, int origin)
00122 { return buf ? buf->seek (offset, origin) : -1; }
00123
00124 long tell (void) { return buf ? buf->tell () : -1; }
00125
00126 void clear (void) { if (buf) buf->clear (); STREAM_T::clear (); }
00127
00128 private:
00129
00130 BUF_T *buf;
00131
00132
00133
00134 c_file_ptr_stream (const c_file_ptr_stream&);
00135
00136 c_file_ptr_stream& operator = (const c_file_ptr_stream&);
00137 };
00138
00139 typedef c_file_ptr_stream<std::istream, FILE *, c_file_ptr_buf> i_c_file_ptr_stream;
00140 typedef c_file_ptr_stream<std::ostream, FILE *, c_file_ptr_buf> o_c_file_ptr_stream;
00141 typedef c_file_ptr_stream<std::iostream, FILE *, c_file_ptr_buf> io_c_file_ptr_stream;
00142
00143 #ifdef HAVE_ZLIB
00144
00145 #ifdef HAVE_ZLIB_H
00146 #include <zlib.h>
00147 #endif
00148
00149 class
00150 c_zfile_ptr_buf : public std::streambuf
00151 {
00152 public:
00153
00154 #if !defined (CXX_ISO_COMPLIANT_LIBRARY)
00155 typedef int int_type;
00156 #else
00157 typedef std::streambuf::int_type int_type;
00158 #endif
00159
00160 typedef int (*close_fcn) (gzFile);
00161
00162 gzFile stdiofile (void) { return f; }
00163
00164 c_zfile_ptr_buf (gzFile f_arg, close_fcn cf_arg = file_close)
00165 : std::streambuf (), f (f_arg), cf (cf_arg)
00166 { }
00167
00168 ~c_zfile_ptr_buf (void);
00169
00170 int_type overflow (int_type);
00171
00172 int_type underflow (void) { return underflow_common (false); }
00173
00174 int_type uflow (void) { return underflow_common (true); }
00175
00176 int_type pbackfail (int_type);
00177
00178 std::streamsize xsputn (const char*, std::streamsize);
00179
00180 std::streamsize xsgetn (char *, std::streamsize);
00181
00182 std::streampos seekoff (std::streamoff, std::ios::seekdir,
00183 std::ios::openmode = std::ios::in | std::ios::out);
00184
00185 std::streampos seekpos (std::streampos,
00186 std::ios::openmode = std::ios::in | std::ios::out);
00187
00188 int sync (void);
00189
00190 int flush (void);
00191
00192 int buf_close (void);
00193
00194 int file_number () const { return -1; }
00195
00196 int seek (long offset, int origin)
00197 { return f ? gzseek (f, offset, origin) : -1; }
00198
00199 long tell (void) { return f ? gztell (f) : -1; }
00200
00201 void clear (void) { if (f) gzclearerr (f); }
00202
00203 static int file_close (gzFile f) { return ::gzclose (f); }
00204
00205 protected:
00206
00207 gzFile f;
00208
00209 close_fcn cf;
00210
00211 private:
00212
00213 int_type underflow_common (bool);
00214
00215
00216
00217 c_zfile_ptr_buf (const c_zfile_ptr_buf&);
00218
00219 c_zfile_ptr_buf& operator = (const c_zfile_ptr_buf&);
00220 };
00221
00222 typedef c_file_ptr_stream<std::istream, gzFile, c_zfile_ptr_buf> i_c_zfile_ptr_stream;
00223 typedef c_file_ptr_stream<std::ostream, gzFile, c_zfile_ptr_buf> o_c_zfile_ptr_stream;
00224 typedef c_file_ptr_stream<std::iostream, gzFile, c_zfile_ptr_buf> io_c_zfile_ptr_stream;
00225
00226 #endif
00227
00228 #endif