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