Mercurial > jwe > octave
annotate libinterp/corefcn/zfstream.h @ 21139:538b57866b90
consistently use "typename" intead of "class" in template declarations
* Object.h, QtHandlesUtils.cc, QtHandlesUtils.h, ToolBarButton.cc,
ToolBarButton.h, Cell.h, __lin_interpn__.cc, bitfcns.cc, bsxfun.cc,
cellfun.cc, data.cc, filter.cc, gcd.cc, graphics.cc, help.cc, kron.cc,
lookup.cc, ls-mat5.cc, ls-oct-text.h, lu.cc, max.cc, mgorth.cc,
oct-map.cc, oct-map.h, oct-stream.cc, oct-stream.h, octave-link.h,
pr-output.cc, profiler.h, schur.cc, sparse-xdiv.cc, sparse-xpow.cc,
sqrtm.cc, symtab.h, tril.cc, typecast.cc, variables.cc, xdiv.cc,
zfstream.h, __init_fltk__.cc, __magick_read__.cc, chol.cc, qr.cc,
ov-base-diag.cc, ov-base-diag.h, ov-base-int.cc, ov-base-int.h,
ov-base-mat.cc, ov-base-mat.h, ov-base-scalar.cc, ov-base-scalar.h,
ov-base-sparse.cc, ov-base-sparse.h, ov-base.h, ov-classdef.cc,
ov-int-traits.h, ov-java.h, ov-usr-fcn.h, ov.cc, ov.h,
op-dms-template.cc, oct-parse.in.yy, parse.h, pt-mat.cc, Array-b.cc,
Array.cc, Array.h, CDiagMatrix.h, CMatrix.h, CNDArray.h,
DiagArray2.cc, DiagArray2.h, MArray.cc, MArray.h, MDiagArray2.cc,
MDiagArray2.h, MSparse.cc, MSparse.h, MatrixType.cc, Sparse.cc,
Sparse.h, dDiagMatrix.h, dMatrix.h, dNDArray.h, fCDiagMatrix.h,
fCMatrix.h, fCNDArray.h, fDiagMatrix.h, fMatrix.h, fNDArray.h,
idx-vector.cc, idx-vector.h, intNDArray.cc, intNDArray.h, DET.h,
base-aepbal.h, base-lu.cc, base-lu.h, base-qr.cc, base-qr.h,
bsxfun-defs.cc, eigs-base.cc, lo-mappers.h, lo-specfun.cc,
lo-specfun.h, oct-convn.cc, oct-fftw.cc, oct-norm.cc,
sparse-base-chol.cc, sparse-base-chol.h, sparse-base-lu.cc,
sparse-base-lu.h, sparse-dmsolve.cc, mx-inlines.cc,
action-container.h, base-list.h, lo-traits.h, lo-utils.h,
oct-base64.h, oct-binmap.h, oct-cmplx.h, oct-inttypes.cc,
oct-inttypes.h, oct-locbuf.h, oct-refcount.h, oct-sort.cc, oct-sort.h:
Use "typename" instead of "class" in template declarations.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sun, 24 Jan 2016 13:50:04 -0500 |
parents | 6eff66fb8a02 |
children | f7d1050b9b53 |
rev | line source |
---|---|
5269 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3 Copyright (C) 2005-2015 Ludwig Schwardt, Kevin Ruland |
5269 | 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. | |
5269 | 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/>. | |
5269 | 20 |
21 */ | |
22 | |
23 /* | |
24 | |
25 This file is adapted from the zlib 1.2.2 contrib/iostream3 code, | |
26 written by | |
27 | |
28 Ludwig Schwardt <schwardt@sun.ac.za> | |
29 original version by Kevin Ruland <kevin@rodin.wustl.edu> | |
30 | |
31 */ | |
32 | |
33 #ifndef ZFSTREAM_H | |
34 #define ZFSTREAM_H | |
35 | |
36 #ifdef HAVE_ZLIB | |
37 | |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
38 #include <iosfwd> |
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
39 |
5269 | 40 #include "zlib.h" |
41 | |
42 /** | |
43 * @brief Gzipped file stream buffer class. | |
44 * | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
45 * This class implements basic_filebuf for gzipped files. It doesn't yet |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
46 * support seeking (allowed by zlib but slow/limited), putback and read/write |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
47 * access * (tricky). Otherwise, it attempts to be a drop-in replacement for |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
48 * the standard file streambuf. |
5269 | 49 */ |
50 class gzfilebuf : public std::streambuf | |
51 { | |
52 public: | |
53 // Default constructor. | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
54 gzfilebuf (); |
5269 | 55 |
56 // Destructor. | |
57 virtual | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
58 ~gzfilebuf (); |
5269 | 59 |
60 /** | |
61 * @brief Set compression level and strategy on the fly. | |
62 * @param comp_level Compression level (see zlib.h for allowed values) | |
63 * @param comp_strategy Compression strategy (see zlib.h for allowed values) | |
64 * @return Z_OK on success, Z_STREAM_ERROR otherwise. | |
65 * | |
66 * Unfortunately, these parameters cannot be modified separately, as the | |
67 * previous zfstream version assumed. Since the strategy is seldom changed, | |
68 * it can default and setcompression(level) then becomes like the old | |
69 * setcompressionlevel(level). | |
70 */ | |
71 int | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
72 setcompression (int comp_level, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
73 int comp_strategy = Z_DEFAULT_STRATEGY); |
5269 | 74 |
75 /** | |
76 * @brief Check if file is open. | |
77 * @return True if file is open. | |
78 */ | |
79 bool | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
80 is_open () const { return (file != 0); } |
5269 | 81 |
82 /** | |
83 * @brief Open gzipped file. | |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
84 * @param name Filename. |
5269 | 85 * @param mode Open mode flags. |
86 * @return @c this on success, NULL on failure. | |
87 */ | |
88 gzfilebuf* | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
89 open (const char* name, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
90 std::ios_base::openmode mode); |
5269 | 91 |
92 /** | |
93 * @brief Attach to already open gzipped file. | |
94 * @param fd File descriptor. | |
95 * @param mode Open mode flags. | |
96 * @return @c this on success, NULL on failure. | |
97 */ | |
98 gzfilebuf* | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
99 attach (int fd, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
100 std::ios_base::openmode mode); |
5269 | 101 |
102 /** | |
103 * @brief Close gzipped file. | |
104 * @return @c this on success, NULL on failure. | |
105 */ | |
106 gzfilebuf* | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
107 close (); |
5269 | 108 |
109 protected: | |
110 /** | |
111 * @brief Convert ios open mode int to mode string used by zlib. | |
112 * @return True if valid mode flag combination. | |
113 */ | |
114 bool | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
115 open_mode (std::ios_base::openmode mode, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
116 char* c_mode) const; |
5269 | 117 |
118 /** | |
119 * @brief Number of characters available in stream buffer. | |
120 * @return Number of characters. | |
121 * | |
122 * This indicates number of characters in get area of stream buffer. | |
123 * These characters can be read without accessing the gzipped file. | |
124 */ | |
125 virtual std::streamsize | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
126 showmanyc (); |
5269 | 127 |
128 /** | |
129 * @brief Fill get area from gzipped file. | |
130 * @return First character in get area on success, EOF on error. | |
131 * | |
132 * This actually reads characters from gzipped file to stream | |
133 * buffer. Always buffered. | |
134 */ | |
135 virtual int_type | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
136 underflow (); |
5269 | 137 |
138 /** | |
139 * @brief Write put area to gzipped file. | |
140 * @param c Extra character to add to buffer contents. | |
141 * @return Non-EOF on success, EOF on error. | |
142 * | |
143 * This actually writes characters in stream buffer to | |
144 * gzipped file. With unbuffered output this is done one | |
145 * character at a time. | |
146 */ | |
147 virtual int_type | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
148 overflow (int_type c = traits_type::eof ()); |
5269 | 149 |
150 /** | |
151 * @brief Installs external stream buffer. | |
152 * @param p Pointer to char buffer. | |
153 * @param n Size of external buffer. | |
154 * @return @c this on success, NULL on failure. | |
155 * | |
156 * Call setbuf(0,0) to enable unbuffered output. | |
157 */ | |
158 virtual std::streambuf* | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
159 setbuf (char_type* p, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
160 std::streamsize n); |
5269 | 161 |
162 /** | |
163 * @brief Flush stream buffer to file. | |
164 * @return 0 on success, -1 on error. | |
165 * | |
166 * This calls underflow(EOF) to do the job. | |
167 */ | |
168 virtual int | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
169 sync (); |
5269 | 170 |
171 /** | |
172 * @brief Alters the stream positions. | |
173 * | |
174 * Each derived class provides its own appropriate behavior. | |
175 */ | |
176 virtual pos_type | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
177 seekoff (off_type off, std::ios_base::seekdir way, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
178 std::ios_base::openmode mode = |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
179 std::ios_base::in|std::ios_base::out); |
5269 | 180 |
181 /** | |
182 * @brief Alters the stream positions. | |
183 * | |
184 * Each derived class provides its own appropriate behavior. | |
185 */ | |
186 virtual pos_type | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
187 seekpos (pos_type sp, std::ios_base::openmode mode = |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
188 std::ios_base::in|std::ios_base::out); |
5269 | 189 |
6777 | 190 virtual int_type |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
191 pbackfail (int_type c = traits_type::eof ()); |
6777 | 192 |
5269 | 193 // |
194 // Some future enhancements | |
195 // | |
196 // virtual int_type uflow(); | |
197 // virtual int_type pbackfail(int_type c = traits_type::eof()); | |
198 | |
199 private: | |
12122
f4689107dd8c
Explicitly disallow copying in some classes.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11586
diff
changeset
|
200 |
f4689107dd8c
Explicitly disallow copying in some classes.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11586
diff
changeset
|
201 // No copying! |
f4689107dd8c
Explicitly disallow copying in some classes.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11586
diff
changeset
|
202 |
f4689107dd8c
Explicitly disallow copying in some classes.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11586
diff
changeset
|
203 gzfilebuf (const gzfilebuf&); |
f4689107dd8c
Explicitly disallow copying in some classes.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11586
diff
changeset
|
204 |
f4689107dd8c
Explicitly disallow copying in some classes.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11586
diff
changeset
|
205 gzfilebuf& operator = (const gzfilebuf&); |
f4689107dd8c
Explicitly disallow copying in some classes.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11586
diff
changeset
|
206 |
5269 | 207 /** |
208 * @brief Allocate internal buffer. | |
209 * | |
210 * This function is safe to call multiple times. It will ensure | |
211 * that a proper internal buffer exists if it is required. If the | |
212 * buffer already exists or is external, the buffer pointers will be | |
213 * reset to their original state. | |
214 */ | |
215 void | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
216 enable_buffer (); |
5269 | 217 |
218 /** | |
219 * @brief Destroy internal buffer. | |
220 * | |
221 * This function is safe to call multiple times. It will ensure | |
222 * that the internal buffer is deallocated if it exists. In any | |
223 * case, it will also reset the buffer pointers. | |
224 */ | |
225 void | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
226 disable_buffer (); |
5269 | 227 |
228 /** | |
229 * Underlying file pointer. | |
230 */ | |
231 gzFile file; | |
232 | |
233 /** | |
234 * Mode in which file was opened. | |
235 */ | |
236 std::ios_base::openmode io_mode; | |
237 | |
238 /** | |
239 * @brief True if this object owns file descriptor. | |
240 * | |
241 * This makes the class responsible for closing the file | |
242 * upon destruction. | |
243 */ | |
244 bool own_fd; | |
245 | |
246 /** | |
247 * @brief Stream buffer. | |
248 * | |
249 * For simplicity this remains allocated on the free store for the | |
250 * entire life span of the gzfilebuf object, unless replaced by setbuf. | |
251 */ | |
252 char_type* buffer; | |
253 | |
254 /** | |
255 * @brief Stream buffer size. | |
256 * | |
257 * Defaults to system default buffer size (typically 8192 bytes). | |
258 * Modified by setbuf. | |
259 */ | |
260 std::streamsize buffer_size; | |
261 | |
262 /** | |
263 * @brief True if this object owns stream buffer. | |
264 * | |
265 * This makes the class responsible for deleting the buffer | |
266 * upon destruction. | |
267 */ | |
268 bool own_buffer; | |
269 }; | |
270 | |
271 /** | |
272 * @brief Gzipped file input stream class. | |
273 * | |
274 * This class implements ifstream for gzipped files. Seeking and putback | |
275 * is not supported yet. | |
276 */ | |
277 class gzifstream : public std::istream | |
278 { | |
279 public: | |
280 // Default constructor | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
281 gzifstream (); |
5269 | 282 |
283 /** | |
284 * @brief Construct stream on gzipped file to be opened. | |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
285 * @param name Filename. |
5269 | 286 * @param mode Open mode flags (forced to contain ios::in). |
287 */ | |
288 explicit | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
289 gzifstream (const char* name, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
290 std::ios_base::openmode mode = std::ios_base::in); |
5269 | 291 |
292 /** | |
293 * @brief Construct stream on already open gzipped file. | |
294 * @param fd File descriptor. | |
295 * @param mode Open mode flags (forced to contain ios::in). | |
296 */ | |
297 explicit | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
298 gzifstream (int fd, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
299 std::ios_base::openmode mode = std::ios_base::in); |
5269 | 300 |
301 /** | |
302 * Obtain underlying stream buffer. | |
303 */ | |
304 gzfilebuf* | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
305 rdbuf () const |
5269 | 306 { return const_cast<gzfilebuf*>(&sb); } |
307 | |
308 /** | |
309 * @brief Check if file is open. | |
310 * @return True if file is open. | |
311 */ | |
312 bool | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
313 is_open () { return sb.is_open (); } |
5269 | 314 |
315 /** | |
316 * @brief Open gzipped file. | |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
317 * @param name Filename. |
5269 | 318 * @param mode Open mode flags (forced to contain ios::in). |
319 * | |
320 * Stream will be in state good() if file opens successfully; | |
321 * otherwise in state fail(). This differs from the behavior of | |
322 * ifstream, which never sets the state to good() and therefore | |
323 * won't allow you to reuse the stream for a second file unless | |
324 * you manually clear() the state. The choice is a matter of | |
325 * convenience. | |
326 */ | |
327 void | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
328 open (const char* name, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
329 std::ios_base::openmode mode = std::ios_base::in); |
5269 | 330 |
331 /** | |
332 * @brief Attach to already open gzipped file. | |
333 * @param fd File descriptor. | |
334 * @param mode Open mode flags (forced to contain ios::in). | |
335 * | |
336 * Stream will be in state good() if attach succeeded; otherwise | |
337 * in state fail(). | |
338 */ | |
339 void | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
340 attach (int fd, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
341 std::ios_base::openmode mode = std::ios_base::in); |
5269 | 342 |
343 /** | |
344 * @brief Close gzipped file. | |
345 * | |
346 * Stream will be in state fail() if close failed. | |
347 */ | |
348 void | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
349 close (); |
5269 | 350 |
351 private: | |
352 /** | |
353 * Underlying stream buffer. | |
354 */ | |
355 gzfilebuf sb; | |
356 }; | |
357 | |
358 /** | |
359 * @brief Gzipped file output stream class. | |
360 * | |
361 * This class implements ofstream for gzipped files. Seeking and putback | |
362 * is not supported yet. | |
363 */ | |
364 class gzofstream : public std::ostream | |
365 { | |
366 public: | |
367 // Default constructor | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
368 gzofstream (); |
5269 | 369 |
370 /** | |
371 * @brief Construct stream on gzipped file to be opened. | |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
372 * @param name Filename. |
5269 | 373 * @param mode Open mode flags (forced to contain ios::out). |
374 */ | |
375 explicit | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
376 gzofstream (const char* name, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
377 std::ios_base::openmode mode = std::ios_base::out); |
5269 | 378 |
379 /** | |
380 * @brief Construct stream on already open gzipped file. | |
381 * @param fd File descriptor. | |
382 * @param mode Open mode flags (forced to contain ios::out). | |
383 */ | |
384 explicit | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
385 gzofstream (int fd, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
386 std::ios_base::openmode mode = std::ios_base::out); |
5269 | 387 |
388 /** | |
389 * Obtain underlying stream buffer. | |
390 */ | |
391 gzfilebuf* | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
392 rdbuf () const |
5269 | 393 { return const_cast<gzfilebuf*>(&sb); } |
394 | |
395 /** | |
396 * @brief Check if file is open. | |
397 * @return True if file is open. | |
398 */ | |
399 bool | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
400 is_open () { return sb.is_open (); } |
5269 | 401 |
402 /** | |
403 * @brief Open gzipped file. | |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
404 * @param name Filename. |
5269 | 405 * @param mode Open mode flags (forced to contain ios::out). |
406 * | |
407 * Stream will be in state good() if file opens successfully; | |
408 * otherwise in state fail(). This differs from the behavior of | |
409 * ofstream, which never sets the state to good() and therefore | |
410 * won't allow you to reuse the stream for a second file unless | |
411 * you manually clear() the state. The choice is a matter of | |
412 * convenience. | |
413 */ | |
414 void | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
415 open (const char* name, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
416 std::ios_base::openmode mode = std::ios_base::out); |
5269 | 417 |
418 /** | |
419 * @brief Attach to already open gzipped file. | |
420 * @param fd File descriptor. | |
421 * @param mode Open mode flags (forced to contain ios::out). | |
422 * | |
423 * Stream will be in state good() if attach succeeded; otherwise | |
424 * in state fail(). | |
425 */ | |
426 void | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
427 attach (int fd, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
428 std::ios_base::openmode mode = std::ios_base::out); |
5269 | 429 |
430 /** | |
431 * @brief Close gzipped file. | |
432 * | |
433 * Stream will be in state fail() if close failed. | |
434 */ | |
435 void | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
436 close (); |
5269 | 437 |
438 private: | |
439 /** | |
440 * Underlying stream buffer. | |
441 */ | |
442 gzfilebuf sb; | |
443 }; | |
444 | |
445 /** | |
446 * @brief Gzipped file output stream manipulator class. | |
447 * | |
448 * This class defines a two-argument manipulator for gzofstream. It is used | |
449 * as base for the setcompression(int,int) manipulator. | |
450 */ | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
451 template <typename T1, typename T2> |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
452 class gzomanip2 |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
453 { |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
454 public: |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
455 // Allows insertor to peek at internals |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
456 template <typename Ta, typename Tb> |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
457 friend gzofstream& |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
458 operator<<(gzofstream&, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
459 const gzomanip2<Ta,Tb>&); |
5269 | 460 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
461 // Constructor |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
462 gzomanip2 (gzofstream& (*f)(gzofstream&, T1, T2), |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
463 T1 v1, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
464 T2 v2); |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
465 private: |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
466 // Underlying manipulator function |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
467 gzofstream& |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
468 (*func)(gzofstream&, T1, T2); |
5269 | 469 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
470 // Arguments for manipulator function |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
471 T1 val1; |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
472 T2 val2; |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
473 }; |
5269 | 474 |
475 // Manipulator function thunks through to stream buffer | |
476 inline gzofstream& | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
477 setcompression (gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY) |
5269 | 478 { |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
479 (gzs.rdbuf ())->setcompression (l, s); |
5269 | 480 return gzs; |
481 } | |
482 | |
483 // Manipulator constructor stores arguments | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
484 template <typename T1, typename T2> |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
485 inline |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
486 gzomanip2<T1,T2>::gzomanip2 (gzofstream &(*f)(gzofstream &, T1, T2), |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
487 T1 v1, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
488 T2 v2) |
5269 | 489 : func(f), val1(v1), val2(v2) |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
490 { } |
5269 | 491 |
492 // Insertor applies underlying manipulator function to stream | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
493 template <typename T1, typename T2> |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
494 inline gzofstream& |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
495 operator<<(gzofstream& s, const gzomanip2<T1,T2>& m) |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
496 { return (*m.func)(s, m.val1, m.val2); } |
5269 | 497 |
498 // Insert this onto stream to simplify setting of compression level | |
499 inline gzomanip2<int,int> | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
500 setcompression (int l, int s = Z_DEFAULT_STRATEGY) |
5269 | 501 { return gzomanip2<int,int>(&setcompression, l, s); } |
502 | |
20945
9d9270e2f98f
eliminate comments after preprocessor conditionals
John W. Eaton <jwe@octave.org>
parents:
20713
diff
changeset
|
503 #endif |
5269 | 504 |
20945
9d9270e2f98f
eliminate comments after preprocessor conditionals
John W. Eaton <jwe@octave.org>
parents:
20713
diff
changeset
|
505 #endif |