Mercurial > hg > octave-nkf
annotate src/c-file-ptr-stream.h @ 10825:cace99cb01ab
rewrite logm (M. Caliari, R.T. Guy)
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 28 Jul 2010 09:22:41 +0200 |
parents | 479cc8a0a846 |
children | fd0a3ac60b0e |
rev | line source |
---|---|
3628 | 1 /* |
2 | |
9245 | 3 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 |
7017 | 4 John W. Eaton |
3628 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
3628 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
3628 | 21 |
22 */ | |
23 | |
24 #if !defined (octave_c_file_ptr_stream_h) | |
25 #define octave_c_file_ptr_stream_h 1 | |
26 | |
3775 | 27 #include <cstdio> |
3628 | 28 |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
29 #include <iosfwd> |
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
30 |
3628 | 31 class |
4310 | 32 c_file_ptr_buf : public std::streambuf |
3628 | 33 { |
34 public: | |
35 | |
3775 | 36 #if !defined (CXX_ISO_COMPLIANT_LIBRARY) |
37 typedef int int_type; | |
3944 | 38 #else |
4310 | 39 typedef std::streambuf::int_type int_type; |
3775 | 40 #endif |
41 | |
3716 | 42 typedef int (*close_fcn) (FILE *); |
43 | |
4797 | 44 FILE* stdiofile (void) { return f; } |
3628 | 45 |
10288
5e972e2deffe
avoid some possible gnulib #defines
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
46 c_file_ptr_buf (FILE *f_arg, close_fcn cf_arg = file_close) |
4310 | 47 : std::streambuf (), f (f_arg), cf (cf_arg) |
3775 | 48 { } |
3628 | 49 |
50 ~c_file_ptr_buf (void); | |
51 | |
3775 | 52 int_type overflow (int_type); |
3628 | 53 |
4310 | 54 int_type underflow (void) { return underflow_common (false); } |
3628 | 55 |
4310 | 56 int_type uflow (void) { return underflow_common (true); } |
3628 | 57 |
3775 | 58 int_type pbackfail (int_type); |
3628 | 59 |
60 std::streamsize xsputn (const char*, std::streamsize); | |
61 | |
62 std::streamsize xsgetn (char *, std::streamsize); | |
63 | |
64 std::streampos seekoff (std::streamoff, std::ios::seekdir, | |
10313 | 65 std::ios::openmode = std::ios::in | std::ios::out); |
3628 | 66 |
67 std::streampos seekpos (std::streampos, | |
10313 | 68 std::ios::openmode = std::ios::in | std::ios::out); |
3628 | 69 |
70 int sync (void); | |
3644 | 71 |
3652 | 72 int flush (void); |
73 | |
10288
5e972e2deffe
avoid some possible gnulib #defines
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
74 int buf_close (void); |
3652 | 75 |
4310 | 76 int file_number () const { return f ? fileno (f) : -1; } |
3775 | 77 |
4797 | 78 int seek (long offset, int origin) |
79 { return f ? fseek (f, offset, origin) : -1; } | |
80 | |
81 long tell (void) { return f ? ftell (f) : -1; } | |
82 | |
4888 | 83 void clear (void) { if (f) clearerr (f); } |
84 | |
10411 | 85 static int file_close (FILE *f); |
3951 | 86 |
3644 | 87 protected: |
88 | |
89 FILE *f; | |
3716 | 90 |
91 close_fcn cf; | |
3775 | 92 |
93 private: | |
94 | |
4310 | 95 int_type underflow_common (bool); |
3628 | 96 }; |
97 | |
5775 | 98 // FIXME -- the following three classes could probably share |
5325 | 99 // some code... |
100 | |
101 template <typename STREAM_T, typename FILE_T, typename BUF_T> | |
3628 | 102 class |
5325 | 103 c_file_ptr_stream : public STREAM_T |
3628 | 104 { |
3644 | 105 public: |
106 | |
10288
5e972e2deffe
avoid some possible gnulib #defines
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
107 c_file_ptr_stream (FILE_T f, typename BUF_T::close_fcn cf = BUF_T::file_close) |
5884 | 108 : STREAM_T (0), buf (new BUF_T (f, cf)) { STREAM_T::init (buf); } |
3644 | 109 |
5325 | 110 ~c_file_ptr_stream (void) { delete buf; buf = 0; } |
3644 | 111 |
5325 | 112 BUF_T *rdbuf (void) { return buf; } |
3644 | 113 |
10288
5e972e2deffe
avoid some possible gnulib #defines
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
114 void stream_close (void) { if (buf) buf->buf_close (); } |
3652 | 115 |
4797 | 116 int seek (long offset, int origin) |
117 { return buf ? buf->seek (offset, origin) : -1; } | |
118 | |
119 long tell (void) { return buf ? buf->tell () : -1; } | |
120 | |
5325 | 121 void clear (void) { if (buf) buf->clear (); STREAM_T::clear (); } |
4888 | 122 |
3628 | 123 private: |
124 | |
5325 | 125 BUF_T *buf; |
3628 | 126 }; |
127 | |
5325 | 128 typedef c_file_ptr_stream<std::istream, FILE *, c_file_ptr_buf> i_c_file_ptr_stream; |
129 typedef c_file_ptr_stream<std::ostream, FILE *, c_file_ptr_buf> o_c_file_ptr_stream; | |
130 typedef c_file_ptr_stream<std::iostream, FILE *, c_file_ptr_buf> io_c_file_ptr_stream; | |
131 | |
132 #ifdef HAVE_ZLIB | |
133 | |
134 #ifdef HAVE_ZLIB_H | |
135 #include <zlib.h> | |
136 #endif | |
137 | |
3628 | 138 class |
5325 | 139 c_zfile_ptr_buf : public std::streambuf |
3628 | 140 { |
3644 | 141 public: |
142 | |
5325 | 143 #if !defined (CXX_ISO_COMPLIANT_LIBRARY) |
144 typedef int int_type; | |
145 #else | |
146 typedef std::streambuf::int_type int_type; | |
147 #endif | |
148 | |
149 typedef int (*close_fcn) (gzFile); | |
150 | |
151 gzFile stdiofile (void) { return f; } | |
152 | |
10288
5e972e2deffe
avoid some possible gnulib #defines
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
153 c_zfile_ptr_buf (gzFile f_arg, close_fcn cf_arg = file_close) |
5325 | 154 : std::streambuf (), f (f_arg), cf (cf_arg) |
155 { } | |
156 | |
157 ~c_zfile_ptr_buf (void); | |
158 | |
159 int_type overflow (int_type); | |
160 | |
161 int_type underflow (void) { return underflow_common (false); } | |
3644 | 162 |
5325 | 163 int_type uflow (void) { return underflow_common (true); } |
164 | |
165 int_type pbackfail (int_type); | |
166 | |
167 std::streamsize xsputn (const char*, std::streamsize); | |
168 | |
169 std::streamsize xsgetn (char *, std::streamsize); | |
3644 | 170 |
5325 | 171 std::streampos seekoff (std::streamoff, std::ios::seekdir, |
10313 | 172 std::ios::openmode = std::ios::in | std::ios::out); |
5325 | 173 |
174 std::streampos seekpos (std::streampos, | |
10313 | 175 std::ios::openmode = std::ios::in | std::ios::out); |
3644 | 176 |
5325 | 177 int sync (void); |
178 | |
179 int flush (void); | |
180 | |
10288
5e972e2deffe
avoid some possible gnulib #defines
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
181 int buf_close (void); |
5325 | 182 |
183 int file_number () const { return -1; } | |
4326 | 184 |
4797 | 185 int seek (long offset, int origin) |
5325 | 186 { return f ? gzseek (f, offset, origin) : -1; } |
187 | |
188 long tell (void) { return f ? gztell (f) : -1; } | |
189 | |
190 void clear (void) { if (f) gzclearerr (f); } | |
4797 | 191 |
10288
5e972e2deffe
avoid some possible gnulib #defines
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
192 static int file_close (gzFile f) { return ::gzclose (f); } |
5325 | 193 |
194 protected: | |
4797 | 195 |
5325 | 196 gzFile f; |
197 | |
198 close_fcn cf; | |
4888 | 199 |
4326 | 200 private: |
201 | |
5325 | 202 int_type underflow_common (bool); |
4326 | 203 }; |
204 | |
5325 | 205 typedef c_file_ptr_stream<std::istream, gzFile, c_zfile_ptr_buf> i_c_zfile_ptr_stream; |
206 typedef c_file_ptr_stream<std::ostream, gzFile, c_zfile_ptr_buf> o_c_zfile_ptr_stream; | |
207 typedef c_file_ptr_stream<std::iostream, gzFile, c_zfile_ptr_buf> io_c_zfile_ptr_stream; | |
4326 | 208 |
5325 | 209 #endif |
3628 | 210 |
211 #endif |