comparison libinterp/corefcn/zfstream.cc @ 17787:175b392e91fe

Use GNU style coding conventions for code in libinterp/ * libinterp/corefcn/Cell.h, libinterp/corefcn/__contourc__.cc, libinterp/corefcn/__dispatch__.cc, libinterp/corefcn/__lin_interpn__.cc, libinterp/corefcn/__pchip_deriv__.cc, libinterp/corefcn/__qp__.cc, libinterp/corefcn/balance.cc, libinterp/corefcn/besselj.cc, libinterp/corefcn/betainc.cc, libinterp/corefcn/bitfcns.cc, libinterp/corefcn/bsxfun.cc, libinterp/corefcn/c-file-ptr-stream.cc, libinterp/corefcn/c-file-ptr-stream.h, libinterp/corefcn/cellfun.cc, libinterp/corefcn/colloc.cc, libinterp/corefcn/comment-list.h, libinterp/corefcn/conv2.cc, libinterp/corefcn/daspk.cc, libinterp/corefcn/dasrt.cc, libinterp/corefcn/dassl.cc, libinterp/corefcn/data.cc, libinterp/corefcn/debug.cc, libinterp/corefcn/defaults.cc, libinterp/corefcn/defaults.in.h, libinterp/corefcn/defun-int.h, libinterp/corefcn/defun.cc, libinterp/corefcn/det.cc, libinterp/corefcn/dirfns.cc, libinterp/corefcn/display.cc, libinterp/corefcn/dlmread.cc, libinterp/corefcn/dot.cc, libinterp/corefcn/dynamic-ld.cc, libinterp/corefcn/dynamic-ld.h, libinterp/corefcn/eig.cc, libinterp/corefcn/ellipj.cc, libinterp/corefcn/error.cc, libinterp/corefcn/error.h, libinterp/corefcn/event-queue.h, libinterp/corefcn/fft.cc, libinterp/corefcn/fft2.cc, libinterp/corefcn/fftn.cc, libinterp/corefcn/file-io.cc, libinterp/corefcn/filter.cc, libinterp/corefcn/find.cc, libinterp/corefcn/gammainc.cc, libinterp/corefcn/gcd.cc, libinterp/corefcn/getgrent.cc, libinterp/corefcn/getpwent.cc, libinterp/corefcn/getrusage.cc, libinterp/corefcn/givens.cc, libinterp/corefcn/gl-render.cc, libinterp/corefcn/gl2ps-renderer.cc, libinterp/corefcn/gl2ps-renderer.h, libinterp/corefcn/graphics.cc, libinterp/corefcn/graphics.in.h, libinterp/corefcn/gripes.cc, libinterp/corefcn/gripes.h, libinterp/corefcn/help.cc, libinterp/corefcn/hess.cc, libinterp/corefcn/hex2num.cc, libinterp/corefcn/input.cc, libinterp/corefcn/input.h, libinterp/corefcn/inv.cc, libinterp/corefcn/jit-ir.h, libinterp/corefcn/jit-typeinfo.cc, libinterp/corefcn/jit-typeinfo.h, libinterp/corefcn/jit-util.h, libinterp/corefcn/kron.cc, libinterp/corefcn/load-path.cc, libinterp/corefcn/load-path.h, libinterp/corefcn/load-save.cc, libinterp/corefcn/load-save.h, libinterp/corefcn/lookup.cc, libinterp/corefcn/ls-ascii-helper.cc, libinterp/corefcn/ls-hdf5.cc, libinterp/corefcn/ls-hdf5.h, libinterp/corefcn/ls-mat-ascii.cc, libinterp/corefcn/ls-mat-ascii.h, libinterp/corefcn/ls-mat4.cc, libinterp/corefcn/ls-mat5.cc, libinterp/corefcn/ls-mat5.h, libinterp/corefcn/ls-oct-ascii.cc, libinterp/corefcn/lsode.cc, libinterp/corefcn/lu.cc, libinterp/corefcn/luinc.cc, libinterp/corefcn/mappers.cc, libinterp/corefcn/matrix_type.cc, libinterp/corefcn/max.cc, libinterp/corefcn/md5sum.cc, libinterp/corefcn/mex.cc, libinterp/corefcn/mexproto.h, libinterp/corefcn/mgorth.cc, libinterp/corefcn/mxarray.in.h, libinterp/corefcn/nproc.cc, libinterp/corefcn/oct-hist.cc, libinterp/corefcn/oct-lvalue.h, libinterp/corefcn/oct-map.cc, libinterp/corefcn/oct-map.h, libinterp/corefcn/oct-obj.h, libinterp/corefcn/oct-prcstrm.h, libinterp/corefcn/oct-stdstrm.h, libinterp/corefcn/oct-stream.cc, libinterp/corefcn/oct-stream.h, libinterp/corefcn/octave-link.cc, libinterp/corefcn/octave-link.h, libinterp/corefcn/pager.cc, libinterp/corefcn/pinv.cc, libinterp/corefcn/pr-output.cc, libinterp/corefcn/procstream.h, libinterp/corefcn/profiler.cc, libinterp/corefcn/pt-jit.cc, libinterp/corefcn/pt-jit.h, libinterp/corefcn/quad.cc, libinterp/corefcn/quadcc.cc, libinterp/corefcn/qz.cc, libinterp/corefcn/rand.cc, libinterp/corefcn/rcond.cc, libinterp/corefcn/regexp.cc, libinterp/corefcn/schur.cc, libinterp/corefcn/sighandlers.cc, libinterp/corefcn/sighandlers.h, libinterp/corefcn/sparse-xdiv.cc, libinterp/corefcn/sparse-xdiv.h, libinterp/corefcn/sparse-xpow.cc, libinterp/corefcn/sparse.cc, libinterp/corefcn/spparms.cc, libinterp/corefcn/sqrtm.cc, libinterp/corefcn/str2double.cc, libinterp/corefcn/strfind.cc, libinterp/corefcn/strfns.cc, libinterp/corefcn/sub2ind.cc, libinterp/corefcn/svd.cc, libinterp/corefcn/syl.cc, libinterp/corefcn/symtab.cc, libinterp/corefcn/symtab.h, libinterp/corefcn/syscalls.cc, libinterp/corefcn/sysdep.cc, libinterp/corefcn/sysdep.h, libinterp/corefcn/time.cc, libinterp/corefcn/toplev.cc, libinterp/corefcn/toplev.h, libinterp/corefcn/tril.cc, libinterp/corefcn/txt-eng-ft.cc, libinterp/corefcn/txt-eng-ft.h, libinterp/corefcn/txt-eng.h, libinterp/corefcn/typecast.cc, libinterp/corefcn/urlwrite.cc, libinterp/corefcn/utils.cc, libinterp/corefcn/variables.cc, libinterp/corefcn/variables.h, libinterp/corefcn/xdiv.cc, libinterp/corefcn/xdiv.h, libinterp/corefcn/xnorm.h, libinterp/corefcn/xpow.cc, libinterp/corefcn/xpow.h, libinterp/corefcn/zfstream.cc, libinterp/corefcn/zfstream.h, libinterp/dldfcn/__delaunayn__.cc, libinterp/dldfcn/__dsearchn__.cc, libinterp/dldfcn/__eigs__.cc, libinterp/dldfcn/__fltk_uigetfile__.cc, libinterp/dldfcn/__glpk__.cc, libinterp/dldfcn/__init_fltk__.cc, libinterp/dldfcn/__init_gnuplot__.cc, libinterp/dldfcn/__magick_read__.cc, libinterp/dldfcn/__voronoi__.cc, libinterp/dldfcn/amd.cc, libinterp/dldfcn/ccolamd.cc, libinterp/dldfcn/chol.cc, libinterp/dldfcn/colamd.cc, libinterp/dldfcn/convhulln.cc, libinterp/dldfcn/dmperm.cc, libinterp/dldfcn/fftw.cc, libinterp/dldfcn/qr.cc, libinterp/dldfcn/symbfact.cc, libinterp/dldfcn/symrcm.cc, libinterp/dldfcn/tsearch.cc, libinterp/octave-value/ov-base-diag.cc, libinterp/octave-value/ov-base-diag.h, libinterp/octave-value/ov-base-int.cc, libinterp/octave-value/ov-base-int.h, libinterp/octave-value/ov-base-mat.h, libinterp/octave-value/ov-base-scalar.cc, libinterp/octave-value/ov-base-scalar.h, libinterp/octave-value/ov-base-sparse.cc, libinterp/octave-value/ov-base-sparse.h, libinterp/octave-value/ov-base.cc, libinterp/octave-value/ov-base.h, libinterp/octave-value/ov-bool-mat.cc, libinterp/octave-value/ov-bool-mat.h, libinterp/octave-value/ov-bool-sparse.cc, libinterp/octave-value/ov-bool-sparse.h, libinterp/octave-value/ov-bool.cc, libinterp/octave-value/ov-bool.h, libinterp/octave-value/ov-builtin.cc, libinterp/octave-value/ov-builtin.h, libinterp/octave-value/ov-cell.cc, libinterp/octave-value/ov-cell.h, libinterp/octave-value/ov-ch-mat.cc, libinterp/octave-value/ov-ch-mat.h, libinterp/octave-value/ov-class.cc, libinterp/octave-value/ov-class.h, libinterp/octave-value/ov-colon.h, libinterp/octave-value/ov-complex.cc, libinterp/octave-value/ov-complex.h, libinterp/octave-value/ov-cx-diag.cc, libinterp/octave-value/ov-cx-diag.h, libinterp/octave-value/ov-cx-mat.cc, libinterp/octave-value/ov-cx-mat.h, libinterp/octave-value/ov-cx-sparse.cc, libinterp/octave-value/ov-cx-sparse.h, libinterp/octave-value/ov-dld-fcn.h, libinterp/octave-value/ov-fcn-handle.cc, libinterp/octave-value/ov-fcn-handle.h, libinterp/octave-value/ov-fcn-inline.cc, libinterp/octave-value/ov-fcn-inline.h, libinterp/octave-value/ov-fcn.h, libinterp/octave-value/ov-float.cc, libinterp/octave-value/ov-float.h, libinterp/octave-value/ov-flt-complex.cc, libinterp/octave-value/ov-flt-complex.h, libinterp/octave-value/ov-flt-cx-diag.cc, libinterp/octave-value/ov-flt-cx-diag.h, libinterp/octave-value/ov-flt-cx-mat.cc, libinterp/octave-value/ov-flt-cx-mat.h, libinterp/octave-value/ov-flt-re-diag.cc, libinterp/octave-value/ov-flt-re-diag.h, libinterp/octave-value/ov-flt-re-mat.cc, libinterp/octave-value/ov-flt-re-mat.h, libinterp/octave-value/ov-int16.cc, libinterp/octave-value/ov-int32.cc, libinterp/octave-value/ov-int64.cc, libinterp/octave-value/ov-int8.cc, libinterp/octave-value/ov-intx.h, libinterp/octave-value/ov-java.cc, libinterp/octave-value/ov-lazy-idx.h, libinterp/octave-value/ov-mex-fcn.cc, libinterp/octave-value/ov-mex-fcn.h, libinterp/octave-value/ov-null-mat.cc, libinterp/octave-value/ov-null-mat.h, libinterp/octave-value/ov-oncleanup.cc, libinterp/octave-value/ov-perm.cc, libinterp/octave-value/ov-perm.h, libinterp/octave-value/ov-range.cc, libinterp/octave-value/ov-range.h, libinterp/octave-value/ov-re-diag.cc, libinterp/octave-value/ov-re-diag.h, libinterp/octave-value/ov-re-mat.cc, libinterp/octave-value/ov-re-mat.h, libinterp/octave-value/ov-re-sparse.cc, libinterp/octave-value/ov-re-sparse.h, libinterp/octave-value/ov-scalar.cc, libinterp/octave-value/ov-scalar.h, libinterp/octave-value/ov-str-mat.cc, libinterp/octave-value/ov-str-mat.h, libinterp/octave-value/ov-struct.cc, libinterp/octave-value/ov-struct.h, libinterp/octave-value/ov-type-conv.h, libinterp/octave-value/ov-typeinfo.cc, libinterp/octave-value/ov-typeinfo.h, libinterp/octave-value/ov-uint16.cc, libinterp/octave-value/ov-uint32.cc, libinterp/octave-value/ov-uint64.cc, libinterp/octave-value/ov-uint8.cc, libinterp/octave-value/ov-usr-fcn.cc, libinterp/octave-value/ov-usr-fcn.h, libinterp/octave-value/ov.cc, libinterp/octave-value/ov.h, libinterp/octave.cc, libinterp/operators/op-b-bm.cc, libinterp/operators/op-b-sbm.cc, libinterp/operators/op-bm-b.cc, libinterp/operators/op-bm-bm.cc, libinterp/operators/op-cdm-cdm.cc, libinterp/operators/op-chm.cc, libinterp/operators/op-class.cc, libinterp/operators/op-cm-cm.cc, libinterp/operators/op-cm-cs.cc, libinterp/operators/op-cm-s.cc, libinterp/operators/op-cm-scm.cc, libinterp/operators/op-cm-sm.cc, libinterp/operators/op-cs-cm.cc, libinterp/operators/op-cs-cs.cc, libinterp/operators/op-cs-scm.cc, libinterp/operators/op-cs-sm.cc, libinterp/operators/op-dm-dm.cc, libinterp/operators/op-dm-scm.cc, libinterp/operators/op-double-conv.cc, libinterp/operators/op-fcdm-fcdm.cc, libinterp/operators/op-fcm-fcm.cc, libinterp/operators/op-fcm-fcs.cc, libinterp/operators/op-fcm-fm.cc, libinterp/operators/op-fcm-fs.cc, libinterp/operators/op-fcs-fcm.cc, libinterp/operators/op-fcs-fcs.cc, libinterp/operators/op-fcs-fm.cc, libinterp/operators/op-fcs-fs.cc, libinterp/operators/op-fdm-fdm.cc, libinterp/operators/op-float-conv.cc, libinterp/operators/op-fm-fcm.cc, libinterp/operators/op-fm-fcs.cc, libinterp/operators/op-fm-fm.cc, libinterp/operators/op-fm-fs.cc, libinterp/operators/op-fs-fcm.cc, libinterp/operators/op-fs-fcs.cc, libinterp/operators/op-fs-fm.cc, libinterp/operators/op-fs-fs.cc, libinterp/operators/op-m-cm.cc, libinterp/operators/op-m-cs.cc, libinterp/operators/op-m-m.cc, libinterp/operators/op-m-s.cc, libinterp/operators/op-m-scm.cc, libinterp/operators/op-m-sm.cc, libinterp/operators/op-pm-scm.cc, libinterp/operators/op-range.cc, libinterp/operators/op-s-cm.cc, libinterp/operators/op-s-cs.cc, libinterp/operators/op-s-scm.cc, libinterp/operators/op-sbm-b.cc, libinterp/operators/op-sbm-bm.cc, libinterp/operators/op-sbm-sbm.cc, libinterp/operators/op-scm-cm.cc, libinterp/operators/op-scm-cs.cc, libinterp/operators/op-scm-m.cc, libinterp/operators/op-scm-s.cc, libinterp/operators/op-scm-scm.cc, libinterp/operators/op-scm-sm.cc, libinterp/operators/op-sm-cm.cc, libinterp/operators/op-sm-m.cc, libinterp/operators/op-sm-s.cc, libinterp/operators/op-sm-scm.cc, libinterp/operators/op-sm-sm.cc, libinterp/operators/op-str-m.cc, libinterp/operators/op-str-s.cc, libinterp/operators/op-str-str.cc, libinterp/operators/ops.h, libinterp/parse-tree/lex.h, libinterp/parse-tree/parse.h, libinterp/parse-tree/pt-arg-list.cc, libinterp/parse-tree/pt-arg-list.h, libinterp/parse-tree/pt-assign.cc, libinterp/parse-tree/pt-assign.h, libinterp/parse-tree/pt-binop.cc, libinterp/parse-tree/pt-binop.h, libinterp/parse-tree/pt-bp.h, libinterp/parse-tree/pt-cbinop.cc, libinterp/parse-tree/pt-check.cc, libinterp/parse-tree/pt-colon.cc, libinterp/parse-tree/pt-colon.h, libinterp/parse-tree/pt-const.cc, libinterp/parse-tree/pt-decl.cc, libinterp/parse-tree/pt-decl.h, libinterp/parse-tree/pt-eval.cc, libinterp/parse-tree/pt-except.h, libinterp/parse-tree/pt-exp.h, libinterp/parse-tree/pt-fcn-handle.cc, libinterp/parse-tree/pt-id.cc, libinterp/parse-tree/pt-id.h, libinterp/parse-tree/pt-idx.cc, libinterp/parse-tree/pt-idx.h, libinterp/parse-tree/pt-loop.h, libinterp/parse-tree/pt-mat.cc, libinterp/parse-tree/pt-misc.cc, libinterp/parse-tree/pt-misc.h, libinterp/parse-tree/pt-pr-code.cc, libinterp/parse-tree/pt-select.h, libinterp/parse-tree/pt-stmt.h, libinterp/parse-tree/token.h, libinterp/version.cc: Use GNU style coding conventions for code in libinterp/
author Rik <rik@octave.org>
date Mon, 28 Oct 2013 19:51:46 -0700
parents d63878346099
children 446c46af4b42
comparison
equal deleted inserted replaced
17786:34d9812a943b 17787:175b392e91fe
51 51
52 /*****************************************************************************/ 52 /*****************************************************************************/
53 53
54 // Default constructor 54 // Default constructor
55 gzfilebuf::gzfilebuf () 55 gzfilebuf::gzfilebuf ()
56 : file(0), io_mode(std::ios_base::openmode(0)), own_fd(false), 56 : file(0), io_mode(std::ios_base::openmode(0)), own_fd(false),
57 buffer(0), buffer_size(BIGBUFSIZE), own_buffer(true) 57 buffer(0), buffer_size(BIGBUFSIZE), own_buffer(true)
58 { 58 {
59 // No buffers to start with 59 // No buffers to start with
60 this->disable_buffer (); 60 this->disable_buffer ();
61 } 61 }
62 62
72 this->disable_buffer (); 72 this->disable_buffer ();
73 } 73 }
74 74
75 // Set compression level and strategy 75 // Set compression level and strategy
76 int 76 int
77 gzfilebuf::setcompression (int comp_level, 77 gzfilebuf::setcompression (int comp_level, int comp_strategy)
78 int comp_strategy)
79 { 78 {
80 return gzsetparams (file, comp_level, comp_strategy); 79 return gzsetparams (file, comp_level, comp_strategy);
81 } 80 }
82 81
83 // Open gzipped file 82 // Open gzipped file
84 gzfilebuf* 83 gzfilebuf*
85 gzfilebuf::open (const char *name, 84 gzfilebuf::open (const char *name, std::ios_base::openmode mode)
86 std::ios_base::openmode mode)
87 { 85 {
88 // Fail if file already open 86 // Fail if file already open
89 if (this->is_open ()) 87 if (this->is_open ())
90 return 0; 88 return 0;
91 // Don't support simultaneous read/write access (yet) 89 // Don't support simultaneous read/write access (yet)
108 return this; 106 return this;
109 } 107 }
110 108
111 // Attach to gzipped file 109 // Attach to gzipped file
112 gzfilebuf* 110 gzfilebuf*
113 gzfilebuf::attach (int fd, 111 gzfilebuf::attach (int fd, std::ios_base::openmode mode)
114 std::ios_base::openmode mode)
115 { 112 {
116 // Fail if file already open 113 // Fail if file already open
117 if (this->is_open ()) 114 if (this->is_open ())
118 return 0; 115 return 0;
119 // Don't support simultaneous read/write access (yet) 116 // Don't support simultaneous read/write access (yet)
160 157
161 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 158 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
162 159
163 // Convert int open mode to mode string 160 // Convert int open mode to mode string
164 bool 161 bool
165 gzfilebuf::open_mode (std::ios_base::openmode mode, 162 gzfilebuf::open_mode (std::ios_base::openmode mode, char* c_mode) const
166 char* c_mode) const 163 {
167 { 164 // FIXME: do we need testb?
168 // FIXME -- do we need testb?
169 // bool testb = mode & std::ios_base::binary; 165 // bool testb = mode & std::ios_base::binary;
170 bool testi = mode & std::ios_base::in; 166 bool testi = mode & std::ios_base::in;
171 bool testo = mode & std::ios_base::out; 167 bool testo = mode & std::ios_base::out;
172 bool testt = mode & std::ios_base::trunc; 168 bool testt = mode & std::ios_base::trunc;
173 bool testa = mode & std::ios_base::app; 169 bool testa = mode & std::ios_base::app;
184 if (!testi && testo && testt && !testa) 180 if (!testi && testo && testt && !testa)
185 strcpy (c_mode, "w"); 181 strcpy (c_mode, "w");
186 if (testi && !testo && !testt && !testa) 182 if (testi && !testo && !testt && !testa)
187 strcpy (c_mode, "r"); 183 strcpy (c_mode, "r");
188 // No read/write mode yet 184 // No read/write mode yet
189 // if (testi && testo && !testt && !testa) 185 // if (testi && testo && !testt && !testa)
190 // strcpy(c_mode, "r+"); 186 // strcpy(c_mode, "r+");
191 // if (testi && testo && testt && !testa) 187 // if (testi && testo && testt && !testa)
192 // strcpy(c_mode, "w+"); 188 // strcpy(c_mode, "w+");
193 189
194 // Mode string should be empty for invalid combination of flags 190 // Mode string should be empty for invalid combination of flags
195 if (strlen (c_mode) == 0) 191 if (strlen (c_mode) == 0)
196 return false; 192 return false;
197 193
284 // (buffer must be guaranteed to exist...) 280 // (buffer must be guaranteed to exist...)
285 int bytes_read = gzread (file, buffer + stash, buffer_size - stash); 281 int bytes_read = gzread (file, buffer + stash, buffer_size - stash);
286 282
287 // Indicates error or EOF 283 // Indicates error or EOF
288 if (bytes_read <= 0) 284 if (bytes_read <= 0)
289 { 285 {
290 // Reset get area 286 // Reset get area
291 this->setg (buffer, buffer, buffer); 287 this->setg (buffer, buffer, buffer);
292 return traits_type::eof (); 288 return traits_type::eof ();
293 } 289 }
294 // Make all bytes read from file plus the stash available as get area 290 // Make all bytes read from file plus the stash available as get area
295 this->setg (buffer, buffer + stash, buffer + bytes_read + stash); 291 this->setg (buffer, buffer + stash, buffer + bytes_read + stash);
296 292
297 // Return next character in get area 293 // Return next character in get area
298 return traits_type::to_int_type (*(this->gptr ())); 294 return traits_type::to_int_type (*(this->gptr ()));
302 gzfilebuf::int_type 298 gzfilebuf::int_type
303 gzfilebuf::overflow (int_type c) 299 gzfilebuf::overflow (int_type c)
304 { 300 {
305 // Determine whether put area is in use 301 // Determine whether put area is in use
306 if (this->pbase ()) 302 if (this->pbase ())
307 { 303 {
308 // Double-check pointer range 304 // Double-check pointer range
309 if (this->pptr () > this->epptr () || this->pptr () < this->pbase ()) 305 if (this->pptr () > this->epptr () || this->pptr () < this->pbase ())
310 return traits_type::eof (); 306 return traits_type::eof ();
311 // Add extra character to buffer if not EOF 307 // Add extra character to buffer if not EOF
312 if (! traits_type::eq_int_type (c, traits_type::eof ())) 308 if (! traits_type::eq_int_type (c, traits_type::eof ()))
313 { 309 {
314 *(this->pptr ()) = traits_type::to_char_type (c); 310 *(this->pptr ()) = traits_type::to_char_type (c);
315 this->pbump (1); 311 this->pbump (1);
316 } 312 }
317 // Number of characters to write to file 313 // Number of characters to write to file
318 int bytes_to_write = this->pptr () - this->pbase (); 314 int bytes_to_write = this->pptr () - this->pbase ();
319 // Overflow doesn't fail if nothing is to be written 315 // Overflow doesn't fail if nothing is to be written
320 if (bytes_to_write > 0) 316 if (bytes_to_write > 0)
317 {
318 // If the file hasn't been opened for writing, produce error
319 if (! this->is_open () || !(io_mode & std::ios_base::out))
320 return traits_type::eof ();
321 // If gzipped file won't accept all bytes written to it, fail
322 if (gzwrite (file, this->pbase (), bytes_to_write) != bytes_to_write)
323 return traits_type::eof ();
324 // Reset next pointer to point to pbase on success
325 this->pbump (-bytes_to_write);
326 }
327 }
328 // Write extra character to file if not EOF
329 else if (! traits_type::eq_int_type (c, traits_type::eof ()))
321 { 330 {
322 // If the file hasn't been opened for writing, produce error 331 // If the file hasn't been opened for writing, produce error
323 if (! this->is_open () || !(io_mode & std::ios_base::out)) 332 if (! this->is_open () || !(io_mode & std::ios_base::out))
324 return traits_type::eof (); 333 return traits_type::eof ();
325 // If gzipped file won't accept all bytes written to it, fail 334 // Impromptu char buffer (allows "unbuffered" output)
326 if (gzwrite (file, this->pbase (), bytes_to_write) != bytes_to_write) 335 char_type last_char = traits_type::to_char_type (c);
336 // If gzipped file won't accept this character, fail
337 if (gzwrite (file, &last_char, 1) != 1)
327 return traits_type::eof (); 338 return traits_type::eof ();
328 // Reset next pointer to point to pbase on success 339 }
329 this->pbump (-bytes_to_write);
330 }
331 }
332 // Write extra character to file if not EOF
333 else if (! traits_type::eq_int_type (c, traits_type::eof ()))
334 {
335 // If the file hasn't been opened for writing, produce error
336 if (! this->is_open () || !(io_mode & std::ios_base::out))
337 return traits_type::eof ();
338 // Impromptu char buffer (allows "unbuffered" output)
339 char_type last_char = traits_type::to_char_type (c);
340 // If gzipped file won't accept this character, fail
341 if (gzwrite (file, &last_char, 1) != 1)
342 return traits_type::eof ();
343 }
344 340
345 // If you got here, you have succeeded (even if c was EOF) 341 // If you got here, you have succeeded (even if c was EOF)
346 // The return value should therefore be non-EOF 342 // The return value should therefore be non-EOF
347 if (traits_type::eq_int_type (c, traits_type::eof ())) 343 if (traits_type::eq_int_type (c, traits_type::eof ()))
348 return traits_type::not_eof (c); 344 return traits_type::not_eof (c);
350 return c; 346 return c;
351 } 347 }
352 348
353 // Assign new buffer 349 // Assign new buffer
354 std::streambuf* 350 std::streambuf*
355 gzfilebuf::setbuf (char_type* p, 351 gzfilebuf::setbuf (char_type* p, std::streamsize n)
356 std::streamsize n)
357 { 352 {
358 // First make sure stuff is sync'ed, for safety 353 // First make sure stuff is sync'ed, for safety
359 if (this->sync () == -1) 354 if (this->sync () == -1)
360 return 0; 355 return 0;
361 // If buffering is turned off on purpose via setbuf(0,0), still allocate one... 356 // If buffering is turned off on purpose via setbuf(0,0), still allocate one.
362 // "Unbuffered" only really refers to put [27.8.1.4.10], while get needs at 357 // "Unbuffered" only really refers to put [27.8.1.4.10], while get needs at
363 // least a buffer of size 1 (very inefficient though, therefore make it bigger?) 358 // least a buffer of size 1 (very inefficient though, therefore make it
364 // This follows from [27.5.2.4.3]/12 (gptr needs to point at something, it seems) 359 // bigger?). This follows from [27.5.2.4.3]/12 (gptr needs to point at
360 // something, it seems).
365 if (!p || !n) 361 if (!p || !n)
366 { 362 {
367 // Replace existing buffer (if any) with small internal buffer 363 // Replace existing buffer (if any) with small internal buffer
368 this->disable_buffer (); 364 this->disable_buffer ();
369 buffer = 0; 365 buffer = 0;
370 buffer_size = 0; 366 buffer_size = 0;
371 own_buffer = true; 367 own_buffer = true;
372 this->enable_buffer (); 368 this->enable_buffer ();
373 } 369 }
374 else 370 else
375 { 371 {
376 // Replace existing buffer (if any) with external buffer 372 // Replace existing buffer (if any) with external buffer
377 this->disable_buffer (); 373 this->disable_buffer ();
378 buffer = p; 374 buffer = p;
379 buffer_size = n; 375 buffer_size = n;
380 own_buffer = false; 376 own_buffer = false;
381 this->enable_buffer (); 377 this->enable_buffer ();
382 } 378 }
383 return this; 379 return this;
384 } 380 }
385 381
386 // Write put area to gzipped file (i.e. ensures that put area is empty) 382 // Write put area to gzipped file (i.e. ensures that put area is empty)
387 int 383 int
388 gzfilebuf::sync () 384 gzfilebuf::sync ()
389 { 385 {
390 return traits_type::eq_int_type (this->overflow (), traits_type::eof ()) ? -1 : 0; 386 return traits_type::eq_int_type (this->overflow (),
387 traits_type::eof ()) ? -1 : 0;
391 } 388 }
392 389
393 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 390 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
394 391
395 // Allocate internal buffer 392 // Allocate internal buffer
396 void 393 void
397 gzfilebuf::enable_buffer () 394 gzfilebuf::enable_buffer ()
398 { 395 {
399 // If internal buffer required, allocate one 396 // If internal buffer required, allocate one
400 if (own_buffer && !buffer) 397 if (own_buffer && !buffer)
401 { 398 {
402 // Check for buffered vs. "unbuffered" 399 // Check for buffered vs. "unbuffered"
403 if (buffer_size > 0) 400 if (buffer_size > 0)
404 { 401 {
405 // Allocate internal buffer 402 // Allocate internal buffer
406 buffer = new char_type [buffer_size]; 403 buffer = new char_type [buffer_size];
407 // Get area starts empty and will be expanded by underflow as need arises 404 // Get area starts empty and will be expanded by underflow as needed
405 this->setg (buffer, buffer, buffer);
406 // Setup entire internal buffer as put area.
407 // The one-past-end pointer actually points to the last element of
408 // the buffer, so that overflow(c) can safely add the extra character
409 // c to the sequence. These pointers remain in place for the
410 // duration of the buffer
411 this->setp (buffer, buffer + buffer_size - 1);
412 }
413 else
414 {
415 // Even in "unbuffered" case, (small?) get buffer is still required
416 buffer_size = SMALLBUFSIZE;
417 buffer = new char_type [buffer_size];
418 this->setg (buffer, buffer, buffer);
419 // "Unbuffered" means no put buffer
420 this->setp (0, 0);
421 }
422 }
423 else
424 {
425 // If buffer already allocated, reset buffer pointers just to make sure no
426 // stale chars are lying around
408 this->setg (buffer, buffer, buffer); 427 this->setg (buffer, buffer, buffer);
409 // Setup entire internal buffer as put area.
410 // The one-past-end pointer actually points to the last element of the buffer,
411 // so that overflow(c) can safely add the extra character c to the sequence.
412 // These pointers remain in place for the duration of the buffer
413 this->setp (buffer, buffer + buffer_size - 1); 428 this->setp (buffer, buffer + buffer_size - 1);
414 } 429 }
415 else
416 {
417 // Even in "unbuffered" case, (small?) get buffer is still required
418 buffer_size = SMALLBUFSIZE;
419 buffer = new char_type [buffer_size];
420 this->setg (buffer, buffer, buffer);
421 // "Unbuffered" means no put buffer
422 this->setp (0, 0);
423 }
424 }
425 else
426 {
427 // If buffer already allocated, reset buffer pointers just to make sure no
428 // stale chars are lying around
429 this->setg (buffer, buffer, buffer);
430 this->setp (buffer, buffer + buffer_size - 1);
431 }
432 } 430 }
433 431
434 // Destroy internal buffer 432 // Destroy internal buffer
435 void 433 void
436 gzfilebuf::disable_buffer () 434 gzfilebuf::disable_buffer ()
437 { 435 {
438 // If internal buffer exists, deallocate it 436 // If internal buffer exists, deallocate it
439 if (own_buffer && buffer) 437 if (own_buffer && buffer)
440 { 438 {
441 // Preserve unbuffered status by zeroing size 439 // Preserve unbuffered status by zeroing size
442 if (! this->pbase ()) 440 if (! this->pbase ())
443 buffer_size = 0; 441 buffer_size = 0;
444 delete[] buffer; 442 delete[] buffer;
445 buffer = 0; 443 buffer = 0;
446 this->setg (0, 0, 0); 444 this->setg (0, 0, 0);
447 this->setp (0, 0);
448 }
449 else
450 {
451 // Reset buffer pointers to initial state if external buffer exists
452 this->setg (buffer, buffer, buffer);
453 if (buffer)
454 this->setp (buffer, buffer + buffer_size - 1);
455 else
456 this->setp (0, 0); 445 this->setp (0, 0);
457 } 446 }
447 else
448 {
449 // Reset buffer pointers to initial state if external buffer exists
450 this->setg (buffer, buffer, buffer);
451 if (buffer)
452 this->setp (buffer, buffer + buffer_size - 1);
453 else
454 this->setp (0, 0);
455 }
458 } 456 }
459 457
460 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 458 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
461 459
462 // Seek functions 460 // Seek functions
463 gzfilebuf::pos_type 461 gzfilebuf::pos_type
464 gzfilebuf::seekoff (off_type off, std::ios_base::seekdir way, 462 gzfilebuf::seekoff (off_type off, std::ios_base::seekdir way,
465 std::ios_base::openmode) 463 std::ios_base::openmode)
466 { 464 {
467 pos_type ret = pos_type (off_type (-1)); 465 pos_type ret = pos_type (off_type (-1));
468 466
469 if (this->is_open ()) 467 if (this->is_open ())
470 { 468 {
519 517
520 /*****************************************************************************/ 518 /*****************************************************************************/
521 519
522 // Default constructor initializes stream buffer 520 // Default constructor initializes stream buffer
523 gzifstream::gzifstream () 521 gzifstream::gzifstream ()
524 : std::istream (0), sb () 522 : std::istream (0), sb ()
525 { this->init (&sb); } 523 { this->init (&sb); }
526 524
527 // Initialize stream buffer and open file 525 // Initialize stream buffer and open file
528 gzifstream::gzifstream (const char* name, 526 gzifstream::gzifstream (const char* name, std::ios_base::openmode mode)
529 std::ios_base::openmode mode) 527 : std::istream (0), sb ()
530 : std::istream (0), sb ()
531 { 528 {
532 this->init (&sb); 529 this->init (&sb);
533 this->open (name, mode); 530 this->open (name, mode);
534 } 531 }
535 532
536 // Initialize stream buffer and attach to file 533 // Initialize stream buffer and attach to file
537 gzifstream::gzifstream (int fd, 534 gzifstream::gzifstream (int fd, std::ios_base::openmode mode)
538 std::ios_base::openmode mode) 535 : std::istream (0), sb ()
539 : std::istream (0), sb ()
540 { 536 {
541 this->init (&sb); 537 this->init (&sb);
542 this->attach (fd, mode); 538 this->attach (fd, mode);
543 } 539 }
544 540
545 // Open file and go into fail() state if unsuccessful 541 // Open file and go into fail() state if unsuccessful
546 void 542 void
547 gzifstream::open (const char* name, 543 gzifstream::open (const char* name, std::ios_base::openmode mode)
548 std::ios_base::openmode mode)
549 { 544 {
550 if (! sb.open (name, mode | std::ios_base::in)) 545 if (! sb.open (name, mode | std::ios_base::in))
551 this->setstate (std::ios_base::failbit); 546 this->setstate (std::ios_base::failbit);
552 else 547 else
553 this->clear (); 548 this->clear ();
554 } 549 }
555 550
556 // Attach to file and go into fail() state if unsuccessful 551 // Attach to file and go into fail() state if unsuccessful
557 void 552 void
558 gzifstream::attach (int fd, 553 gzifstream::attach (int fd, std::ios_base::openmode mode)
559 std::ios_base::openmode mode)
560 { 554 {
561 if (! sb.attach (fd, mode | std::ios_base::in)) 555 if (! sb.attach (fd, mode | std::ios_base::in))
562 this->setstate (std::ios_base::failbit); 556 this->setstate (std::ios_base::failbit);
563 else 557 else
564 this->clear (); 558 this->clear ();
574 568
575 /*****************************************************************************/ 569 /*****************************************************************************/
576 570
577 // Default constructor initializes stream buffer 571 // Default constructor initializes stream buffer
578 gzofstream::gzofstream () 572 gzofstream::gzofstream ()
579 : std::ostream (0), sb () 573 : std::ostream (0), sb ()
580 { this->init (&sb); } 574 { this->init (&sb); }
581 575
582 // Initialize stream buffer and open file 576 // Initialize stream buffer and open file
583 gzofstream::gzofstream (const char* name, 577 gzofstream::gzofstream (const char* name, std::ios_base::openmode mode)
584 std::ios_base::openmode mode) 578 : std::ostream (0), sb ()
585 : std::ostream (0), sb ()
586 { 579 {
587 this->init (&sb); 580 this->init (&sb);
588 this->open (name, mode); 581 this->open (name, mode);
589 } 582 }
590 583
591 // Initialize stream buffer and attach to file 584 // Initialize stream buffer and attach to file
592 gzofstream::gzofstream (int fd, 585 gzofstream::gzofstream (int fd, std::ios_base::openmode mode)
593 std::ios_base::openmode mode) 586 : std::ostream (0), sb ()
594 : std::ostream (0), sb ()
595 { 587 {
596 this->init (&sb); 588 this->init (&sb);
597 this->attach (fd, mode); 589 this->attach (fd, mode);
598 } 590 }
599 591
600 // Open file and go into fail() state if unsuccessful 592 // Open file and go into fail() state if unsuccessful
601 void 593 void
602 gzofstream::open (const char* name, 594 gzofstream::open (const char* name, std::ios_base::openmode mode)
603 std::ios_base::openmode mode)
604 { 595 {
605 if (! sb.open (name, mode | std::ios_base::out)) 596 if (! sb.open (name, mode | std::ios_base::out))
606 this->setstate (std::ios_base::failbit); 597 this->setstate (std::ios_base::failbit);
607 else 598 else
608 this->clear (); 599 this->clear ();
609 } 600 }
610 601
611 // Attach to file and go into fail() state if unsuccessful 602 // Attach to file and go into fail() state if unsuccessful
612 void 603 void
613 gzofstream::attach (int fd, 604 gzofstream::attach (int fd, std::ios_base::openmode mode)
614 std::ios_base::openmode mode)
615 { 605 {
616 if (! sb.attach (fd, mode | std::ios_base::out)) 606 if (! sb.attach (fd, mode | std::ios_base::out))
617 this->setstate (std::ios_base::failbit); 607 this->setstate (std::ios_base::failbit);
618 else 608 else
619 this->clear (); 609 this->clear ();