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