Mercurial > jwe > octave
annotate libinterp/corefcn/gzfstream.h @ 30168:4412f57132c4
maint: use "m_" prefix for member variables in classes gzifstream, gzofstream.
* gzfstream.cc, gzfstream.h: Use "m_" prefix for member variables in classes
gzifstream, gzofstream.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 12 Sep 2021 20:57:27 -0700 |
parents | d809b99f1280 |
children | a61e1a0f6024 |
rev | line source |
---|---|
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
2 // |
29363
0a5b15007766
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27928
diff
changeset
|
3 // Copyright (C) 2005-2021 The Octave Project Developers |
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
5269 | 25 |
26 /* | |
27 | |
28 This file is adapted from the zlib 1.2.2 contrib/iostream3 code, | |
29 written by | |
30 | |
31 Ludwig Schwardt <schwardt@sun.ac.za> | |
32 original version by Kevin Ruland <kevin@rodin.wustl.edu> | |
33 | |
34 */ | |
35 | |
21213
f7d1050b9b53
maint: Clean up various usages of #ifdef.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
36 #if ! defined (octave_zfsstream_h) |
f7d1050b9b53
maint: Clean up various usages of #ifdef.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
37 #define octave_zfsstream_h 1 |
5269 | 38 |
21244
1473547f50f5
include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents:
21213
diff
changeset
|
39 #include "octave-config.h" |
1473547f50f5
include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents:
21213
diff
changeset
|
40 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21662
diff
changeset
|
41 #if defined (HAVE_ZLIB) |
5269 | 42 |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
43 #include <iosfwd> |
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
44 |
5269 | 45 #include "zlib.h" |
46 | |
47 /** | |
48 * @brief Gzipped file stream buffer class. | |
49 * | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
50 * This class implements basic_filebuf for gzipped files. It doesn't yet |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
51 * support seeking (allowed by zlib but slow/limited), putback and read/write |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
52 * access * (tricky). Otherwise, it attempts to be a drop-in replacement for |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
53 * the standard file streambuf. |
5269 | 54 */ |
55 class gzfilebuf : public std::streambuf | |
56 { | |
57 public: | |
58 // 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
|
59 gzfilebuf (); |
5269 | 60 |
22869
f75d289645ec
make deleted functions public
John W. Eaton <jwe@octave.org>
parents:
22865
diff
changeset
|
61 // No copying! |
f75d289645ec
make deleted functions public
John W. Eaton <jwe@octave.org>
parents:
22865
diff
changeset
|
62 |
f75d289645ec
make deleted functions public
John W. Eaton <jwe@octave.org>
parents:
22865
diff
changeset
|
63 gzfilebuf (const gzfilebuf&) = delete; |
f75d289645ec
make deleted functions public
John W. Eaton <jwe@octave.org>
parents:
22865
diff
changeset
|
64 |
f75d289645ec
make deleted functions public
John W. Eaton <jwe@octave.org>
parents:
22865
diff
changeset
|
65 gzfilebuf& operator = (const gzfilebuf&) = delete; |
f75d289645ec
make deleted functions public
John W. Eaton <jwe@octave.org>
parents:
22865
diff
changeset
|
66 |
5269 | 67 // Destructor. |
22869
f75d289645ec
make deleted functions public
John W. Eaton <jwe@octave.org>
parents:
22865
diff
changeset
|
68 virtual ~gzfilebuf (); |
5269 | 69 |
70 /** | |
71 * @brief Set compression level and strategy on the fly. | |
72 * @param comp_level Compression level (see zlib.h for allowed values) | |
73 * @param comp_strategy Compression strategy (see zlib.h for allowed values) | |
74 * @return Z_OK on success, Z_STREAM_ERROR otherwise. | |
75 * | |
76 * Unfortunately, these parameters cannot be modified separately, as the | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
77 * previous zfstream version assumed. Since the strategy is seldom changed, |
5269 | 78 * it can default and setcompression(level) then becomes like the old |
79 * setcompressionlevel(level). | |
80 */ | |
81 int | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
82 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
|
83 int comp_strategy = Z_DEFAULT_STRATEGY); |
5269 | 84 |
85 /** | |
86 * @brief Check if file is open. | |
87 * @return True if file is open. | |
88 */ | |
89 bool | |
30122
a7813409b8c6
maint: use "m_" prefix for member variables in class gzfilebuf.
Rik <rik@octave.org>
parents:
29363
diff
changeset
|
90 is_open () const { return (m_file != nullptr); } |
5269 | 91 |
92 /** | |
93 * @brief Open gzipped file. | |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
94 * @param name Filename. |
5269 | 95 * @param mode Open mode flags. |
96 * @return @c this on success, NULL on failure. | |
97 */ | |
98 gzfilebuf* | |
23449
c763214a8260
maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents:
23447
diff
changeset
|
99 open (const char *name, |
14861
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 Attach to already open gzipped file. | |
104 * @param fd File descriptor. | |
105 * @param mode Open mode flags. | |
106 * @return @c this on success, NULL on failure. | |
107 */ | |
108 gzfilebuf* | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
109 attach (int fd, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
110 std::ios_base::openmode mode); |
5269 | 111 |
112 /** | |
113 * @brief Close gzipped file. | |
114 * @return @c this on success, NULL on failure. | |
115 */ | |
116 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
|
117 close (); |
5269 | 118 |
119 protected: | |
120 /** | |
121 * @brief Convert ios open mode int to mode string used by zlib. | |
122 * @return True if valid mode flag combination. | |
123 */ | |
124 bool | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
125 open_mode (std::ios_base::openmode mode, |
23449
c763214a8260
maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents:
23447
diff
changeset
|
126 char *c_mode) const; |
5269 | 127 |
128 /** | |
129 * @brief Number of characters available in stream buffer. | |
130 * @return Number of characters. | |
131 * | |
132 * This indicates number of characters in get area of stream buffer. | |
133 * These characters can be read without accessing the gzipped file. | |
134 */ | |
135 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
|
136 showmanyc (); |
5269 | 137 |
138 /** | |
139 * @brief Fill get area from gzipped file. | |
140 * @return First character in get area on success, EOF on error. | |
141 * | |
142 * This actually reads characters from gzipped file to stream | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
143 * buffer. Always buffered. |
5269 | 144 */ |
145 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
|
146 underflow (); |
5269 | 147 |
148 /** | |
149 * @brief Write put area to gzipped file. | |
150 * @param c Extra character to add to buffer contents. | |
151 * @return Non-EOF on success, EOF on error. | |
152 * | |
153 * This actually writes characters in stream buffer to | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
154 * gzipped file. With unbuffered output this is done one |
5269 | 155 * character at a time. |
156 */ | |
157 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
|
158 overflow (int_type c = traits_type::eof ()); |
5269 | 159 |
160 /** | |
161 * @brief Installs external stream buffer. | |
162 * @param p Pointer to char buffer. | |
163 * @param n Size of external buffer. | |
164 * @return @c this on success, NULL on failure. | |
165 * | |
166 * Call setbuf(0,0) to enable unbuffered output. | |
167 */ | |
168 virtual std::streambuf* | |
23449
c763214a8260
maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents:
23447
diff
changeset
|
169 setbuf (char_type *p, |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
170 std::streamsize n); |
5269 | 171 |
172 /** | |
173 * @brief Flush stream buffer to file. | |
174 * @return 0 on success, -1 on error. | |
175 * | |
176 * This calls underflow(EOF) to do the job. | |
177 */ | |
178 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
|
179 sync (); |
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 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
|
188 std::ios_base::openmode mode = |
21662
5b9868c2e212
maint: Octave coding convention cleanups.
Rik <rik@octave.org>
parents:
21244
diff
changeset
|
189 std::ios_base::in | std::ios_base::out); |
5269 | 190 |
191 /** | |
192 * @brief Alters the stream positions. | |
193 * | |
194 * Each derived class provides its own appropriate behavior. | |
195 */ | |
196 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
|
197 seekpos (pos_type sp, std::ios_base::openmode mode = |
21662
5b9868c2e212
maint: Octave coding convention cleanups.
Rik <rik@octave.org>
parents:
21244
diff
changeset
|
198 std::ios_base::in | std::ios_base::out); |
5269 | 199 |
6777 | 200 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
|
201 pbackfail (int_type c = traits_type::eof ()); |
6777 | 202 |
5269 | 203 // |
204 // Some future enhancements | |
205 // | |
206 // virtual int_type uflow(); | |
207 // virtual int_type pbackfail(int_type c = traits_type::eof()); | |
208 | |
209 private: | |
12122
f4689107dd8c
Explicitly disallow copying in some classes.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11586
diff
changeset
|
210 |
5269 | 211 /** |
212 * @brief Allocate internal buffer. | |
213 * | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
214 * This function is safe to call multiple times. It will ensure |
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
215 * that a proper internal buffer exists if it is required. If the |
5269 | 216 * buffer already exists or is external, the buffer pointers will be |
217 * reset to their original state. | |
218 */ | |
219 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
|
220 enable_buffer (); |
5269 | 221 |
222 /** | |
223 * @brief Destroy internal buffer. | |
224 * | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
225 * This function is safe to call multiple times. It will ensure |
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
226 * that the internal buffer is deallocated if it exists. In any |
5269 | 227 * case, it will also reset the buffer pointers. |
228 */ | |
229 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
|
230 disable_buffer (); |
5269 | 231 |
232 /** | |
233 * Underlying file pointer. | |
234 */ | |
30122
a7813409b8c6
maint: use "m_" prefix for member variables in class gzfilebuf.
Rik <rik@octave.org>
parents:
29363
diff
changeset
|
235 gzFile m_file; |
5269 | 236 |
237 /** | |
238 * Mode in which file was opened. | |
239 */ | |
30122
a7813409b8c6
maint: use "m_" prefix for member variables in class gzfilebuf.
Rik <rik@octave.org>
parents:
29363
diff
changeset
|
240 std::ios_base::openmode m_io_mode; |
5269 | 241 |
242 /** | |
243 * @brief True if this object owns file descriptor. | |
244 * | |
245 * This makes the class responsible for closing the file | |
246 * upon destruction. | |
247 */ | |
30122
a7813409b8c6
maint: use "m_" prefix for member variables in class gzfilebuf.
Rik <rik@octave.org>
parents:
29363
diff
changeset
|
248 bool m_own_fd; |
5269 | 249 |
250 /** | |
251 * @brief Stream buffer. | |
252 * | |
253 * For simplicity this remains allocated on the free store for the | |
254 * entire life span of the gzfilebuf object, unless replaced by setbuf. | |
255 */ | |
30122
a7813409b8c6
maint: use "m_" prefix for member variables in class gzfilebuf.
Rik <rik@octave.org>
parents:
29363
diff
changeset
|
256 char_type *m_buffer; |
5269 | 257 |
258 /** | |
259 * @brief Stream buffer size. | |
260 * | |
261 * Defaults to system default buffer size (typically 8192 bytes). | |
262 * Modified by setbuf. | |
263 */ | |
30122
a7813409b8c6
maint: use "m_" prefix for member variables in class gzfilebuf.
Rik <rik@octave.org>
parents:
29363
diff
changeset
|
264 std::streamsize m_buffer_size; |
5269 | 265 |
266 /** | |
267 * @brief True if this object owns stream buffer. | |
268 * | |
269 * This makes the class responsible for deleting the buffer | |
270 * upon destruction. | |
271 */ | |
30122
a7813409b8c6
maint: use "m_" prefix for member variables in class gzfilebuf.
Rik <rik@octave.org>
parents:
29363
diff
changeset
|
272 bool m_own_buffer; |
5269 | 273 }; |
274 | |
275 /** | |
276 * @brief Gzipped file input stream class. | |
277 * | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
278 * This class implements ifstream for gzipped files. Seeking and putback |
5269 | 279 * is not supported yet. |
280 */ | |
281 class gzifstream : public std::istream | |
282 { | |
283 public: | |
284 // 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
|
285 gzifstream (); |
5269 | 286 |
287 /** | |
288 * @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
|
289 * @param name Filename. |
5269 | 290 * @param mode Open mode flags (forced to contain ios::in). |
291 */ | |
292 explicit | |
23449
c763214a8260
maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents:
23447
diff
changeset
|
293 gzifstream (const char *name, |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
294 std::ios_base::openmode mode = std::ios_base::in); |
5269 | 295 |
296 /** | |
297 * @brief Construct stream on already open gzipped file. | |
298 * @param fd File descriptor. | |
299 * @param mode Open mode flags (forced to contain ios::in). | |
300 */ | |
301 explicit | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
302 gzifstream (int fd, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
303 std::ios_base::openmode mode = std::ios_base::in); |
5269 | 304 |
305 /** | |
306 * Obtain underlying stream buffer. | |
307 */ | |
308 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
|
309 rdbuf () const |
30168
4412f57132c4
maint: use "m_" prefix for member variables in classes gzifstream, gzofstream.
Rik <rik@octave.org>
parents:
30123
diff
changeset
|
310 { return const_cast<gzfilebuf *>(&m_sb); } |
5269 | 311 |
312 /** | |
313 * @brief Check if file is open. | |
314 * @return True if file is open. | |
315 */ | |
316 bool | |
30168
4412f57132c4
maint: use "m_" prefix for member variables in classes gzifstream, gzofstream.
Rik <rik@octave.org>
parents:
30123
diff
changeset
|
317 is_open () { return m_sb.is_open (); } |
5269 | 318 |
319 /** | |
320 * @brief Open gzipped file. | |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
321 * @param name Filename. |
5269 | 322 * @param mode Open mode flags (forced to contain ios::in). |
323 * | |
324 * Stream will be in state good() if file opens successfully; | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
325 * otherwise in state fail(). This differs from the behavior of |
5269 | 326 * ifstream, which never sets the state to good() and therefore |
327 * won't allow you to reuse the stream for a second file unless | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
328 * you manually clear() the state. The choice is a matter of |
5269 | 329 * convenience. |
330 */ | |
331 void | |
23449
c763214a8260
maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents:
23447
diff
changeset
|
332 open (const char *name, |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
333 std::ios_base::openmode mode = std::ios_base::in); |
5269 | 334 |
335 /** | |
336 * @brief Attach to already open gzipped file. | |
337 * @param fd File descriptor. | |
338 * @param mode Open mode flags (forced to contain ios::in). | |
339 * | |
340 * Stream will be in state good() if attach succeeded; otherwise | |
341 * in state fail(). | |
342 */ | |
343 void | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
344 attach (int fd, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
345 std::ios_base::openmode mode = std::ios_base::in); |
5269 | 346 |
347 /** | |
348 * @brief Close gzipped file. | |
349 * | |
350 * Stream will be in state fail() if close failed. | |
351 */ | |
352 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
|
353 close (); |
5269 | 354 |
355 private: | |
356 /** | |
357 * Underlying stream buffer. | |
358 */ | |
30168
4412f57132c4
maint: use "m_" prefix for member variables in classes gzifstream, gzofstream.
Rik <rik@octave.org>
parents:
30123
diff
changeset
|
359 gzfilebuf m_sb; |
5269 | 360 }; |
361 | |
362 /** | |
363 * @brief Gzipped file output stream class. | |
364 * | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
365 * This class implements ofstream for gzipped files. Seeking and putback |
5269 | 366 * is not supported yet. |
367 */ | |
368 class gzofstream : public std::ostream | |
369 { | |
370 public: | |
371 // 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
|
372 gzofstream (); |
5269 | 373 |
374 /** | |
375 * @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
|
376 * @param name Filename. |
5269 | 377 * @param mode Open mode flags (forced to contain ios::out). |
378 */ | |
379 explicit | |
23449
c763214a8260
maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents:
23447
diff
changeset
|
380 gzofstream (const char *name, |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
381 std::ios_base::openmode mode = std::ios_base::out); |
5269 | 382 |
383 /** | |
384 * @brief Construct stream on already open gzipped file. | |
385 * @param fd File descriptor. | |
386 * @param mode Open mode flags (forced to contain ios::out). | |
387 */ | |
388 explicit | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
389 gzofstream (int fd, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
390 std::ios_base::openmode mode = std::ios_base::out); |
5269 | 391 |
392 /** | |
393 * Obtain underlying stream buffer. | |
394 */ | |
395 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
|
396 rdbuf () const |
30168
4412f57132c4
maint: use "m_" prefix for member variables in classes gzifstream, gzofstream.
Rik <rik@octave.org>
parents:
30123
diff
changeset
|
397 { return const_cast<gzfilebuf *>(&m_sb); } |
5269 | 398 |
399 /** | |
400 * @brief Check if file is open. | |
401 * @return True if file is open. | |
402 */ | |
403 bool | |
30168
4412f57132c4
maint: use "m_" prefix for member variables in classes gzifstream, gzofstream.
Rik <rik@octave.org>
parents:
30123
diff
changeset
|
404 is_open () { return m_sb.is_open (); } |
5269 | 405 |
406 /** | |
407 * @brief Open gzipped file. | |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
408 * @param name Filename. |
5269 | 409 * @param mode Open mode flags (forced to contain ios::out). |
410 * | |
411 * Stream will be in state good() if file opens successfully; | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
412 * otherwise in state fail(). This differs from the behavior of |
5269 | 413 * ofstream, which never sets the state to good() and therefore |
414 * won't allow you to reuse the stream for a second file unless | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
415 * you manually clear() the state. The choice is a matter of |
5269 | 416 * convenience. |
417 */ | |
418 void | |
23449
c763214a8260
maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents:
23447
diff
changeset
|
419 open (const char *name, |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
420 std::ios_base::openmode mode = std::ios_base::out); |
5269 | 421 |
422 /** | |
423 * @brief Attach to already open gzipped file. | |
424 * @param fd File descriptor. | |
425 * @param mode Open mode flags (forced to contain ios::out). | |
426 * | |
427 * Stream will be in state good() if attach succeeded; otherwise | |
428 * in state fail(). | |
429 */ | |
430 void | |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
431 attach (int fd, |
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
432 std::ios_base::openmode mode = std::ios_base::out); |
5269 | 433 |
434 /** | |
435 * @brief Close gzipped file. | |
436 * | |
437 * Stream will be in state fail() if close failed. | |
438 */ | |
439 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
|
440 close (); |
5269 | 441 |
442 private: | |
443 /** | |
444 * Underlying stream buffer. | |
445 */ | |
30168
4412f57132c4
maint: use "m_" prefix for member variables in classes gzifstream, gzofstream.
Rik <rik@octave.org>
parents:
30123
diff
changeset
|
446 gzfilebuf m_sb; |
5269 | 447 }; |
448 | |
449 /** | |
450 * @brief Gzipped file output stream manipulator class. | |
451 * | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
452 * This class defines a two-argument manipulator for gzofstream. It is used |
5269 | 453 * as base for the setcompression(int,int) manipulator. |
454 */ | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
455 template <typename T1, typename T2> |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
456 class gzomanip2 |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
457 { |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
458 public: |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
459 // Allows insertor to peek at internals |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
460 template <typename Ta, typename Tb> |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
461 friend gzofstream& |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
462 operator<<(gzofstream&, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
463 const gzomanip2<Ta,Tb>&); |
5269 | 464 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
465 // Constructor |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
466 gzomanip2 (gzofstream& (*f)(gzofstream&, T1, T2), |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
467 T1 v1, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
468 T2 v2); |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
469 private: |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
470 // Underlying manipulator function |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
471 gzofstream& |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
472 (*func)(gzofstream&, T1, T2); |
5269 | 473 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
474 // Arguments for manipulator function |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
475 T1 val1; |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
476 T2 val2; |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
477 }; |
5269 | 478 |
479 // Manipulator function thunks through to stream buffer | |
480 inline gzofstream& | |
23433
c9fab0bc983e
maint: Use convention 'int& x' for naming references.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
481 setcompression (gzofstream& gzs, int l, int s = Z_DEFAULT_STRATEGY) |
5269 | 482 { |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
483 (gzs.rdbuf ())->setcompression (l, s); |
5269 | 484 return gzs; |
485 } | |
486 | |
487 // 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
|
488 template <typename T1, typename T2> |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
489 inline |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
490 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
|
491 T1 v1, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
492 T2 v2) |
5269 | 493 : 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
|
494 { } |
5269 | 495 |
496 // 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
|
497 template <typename T1, typename T2> |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
498 inline gzofstream& |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
499 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
|
500 { return (*m.func)(s, m.val1, m.val2); } |
5269 | 501 |
502 // Insert this onto stream to simplify setting of compression level | |
503 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
|
504 setcompression (int l, int s = Z_DEFAULT_STRATEGY) |
5269 | 505 { return gzomanip2<int,int>(&setcompression, l, s); } |
506 | |
20945
9d9270e2f98f
eliminate comments after preprocessor conditionals
John W. Eaton <jwe@octave.org>
parents:
20713
diff
changeset
|
507 #endif |
5269 | 508 |
20945
9d9270e2f98f
eliminate comments after preprocessor conditionals
John W. Eaton <jwe@octave.org>
parents:
20713
diff
changeset
|
509 #endif |