Biomedical Image Analysis Library
The Biomedical Image Analysis Library is a poweful tool for developers, physicians, researchers, engineers, and so on.
gzstream.hpp
Go to the documentation of this file.
1 /*
2  * ============================================================================
3  * gzstream, C++ iostream classes wrapping the zlib compression library.
4  * Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  * ============================================================================
20  *
21  * File : gzstream.hpp
22  * Revision : $Revision: 1.5 $
23  * Revision_date : $Date: 2002/04/26 23:30:15 $
24  * Author(s) : Deepak Bandyopadhyay, Lutz Kettner
25  *
26  * Standard std::streambuf implementation following Nicolai Josuttis, "The
27  * Standard C++ Library".
28  * ============================================================================
29  */
30 
31 #ifndef GZSTREAM_H
32 #define GZSTREAM_H
33 
34 #include "Common.hpp"
35 #include <fstream>
36 #include <iostream>
37 #include <zlib.h>
38 
39 namespace Bial {
40 
41  class gzstreambuf : public std::streambuf {
42 private:
43  static const int bufferSize = 47 + 256; /* size of data buff */
44  /* totals 512 bytes under g++ for igzstream at the end. */
45  gzFile file; /* file handle for compressed file */
46  char buffer[ bufferSize ]; /* data buffer */
47  char opened; /* open/close state of stream */
48  int mode; /* I/O mode */
49  int flush_buffer( );
50 public:
51  gzstreambuf( ) try : opened( 0 ) {
52  setp( buffer, buffer + ( bufferSize - 1 ) );
53  setg( buffer + 4, /* beginning of putback area */
54  buffer + 4, /* read position */
55  buffer + 4 ); /* end position */
56  /* ASSERT: both input & output capabilities will not be used together */
57  }
58  catch( std::ios_base::failure &e ) {
59  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
60  throw( std::ios_base::failure( msg ) );
61  }
62  catch( std::bad_alloc &e ) {
63  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
64  throw( std::runtime_error( msg ) );
65  }
66  catch( std::runtime_error &e ) {
67  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
68  throw( std::runtime_error( msg ) );
69  }
70  catch( const std::out_of_range &e ) {
71  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
72  throw( std::out_of_range( msg ) );
73  }
74  catch( const std::logic_error &e ) {
75  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR(
76  "Image, window end, and/or window size dimensions do not match." ) );
77  throw( std::logic_error( msg ) );
78  }
79 
80  bool is_open( ) const {
81  return( opened );
82  }
83  gzstreambuf* open( const char *name, int open_mode );
84  gzstreambuf* close( );
86  close( );
87  }
88 
89  virtual int overflow( int c = EOF );
90  virtual int underflow( );
91  virtual int sync( );
92  };
93 
94  class gzstreambase : virtual public std::ios {
95 protected:
97 public:
99  init( &buf );
100  }
101  gzstreambase( const char *name, int open_mode );
102  ~gzstreambase( );
103  void open( const char *name, int open_mode );
104  void close( );
106  return( &buf );
107  }
108  };
109 
110  /*
111  * ----------------------------------------------------------------------------
112  * User classes. Use igzstream and ogzstream analogously to ifstream and
113  * ofstream respectively. They read and write files based on the gz*
114  * function interface of the zlib. Files are compatible with gzip compression.
115  * ----------------------------------------------------------------------------
116  */
117 
118  class igzstream : public gzstreambase, public std::istream {
119 public:
120  igzstream( ) try : std::istream( &buf ) {
121  }
122  catch( std::ios_base::failure &e ) {
123  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
124  throw( std::ios_base::failure( msg ) );
125  }
126  catch( std::bad_alloc &e ) {
127  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
128  throw( std::runtime_error( msg ) );
129  }
130  catch( std::runtime_error &e ) {
131  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
132  throw( std::runtime_error( msg ) );
133  }
134  catch( const std::out_of_range &e ) {
135  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
136  throw( std::out_of_range( msg ) );
137  }
138  catch( const std::logic_error &e ) {
139  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR(
140  "Image, window end, and/or window size dimensions do not match." ) );
141  throw( std::logic_error( msg ) );
142  }
143 
144  igzstream( igzstream && ) try : std::istream( &buf ) {
145  }
146  catch( std::ios_base::failure &e ) {
147  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
148  throw( std::ios_base::failure( msg ) );
149  }
150  catch( std::bad_alloc &e ) {
151  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
152  throw( std::runtime_error( msg ) );
153  }
154  catch( std::runtime_error &e ) {
155  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
156  throw( std::runtime_error( msg ) );
157  }
158  catch( const std::out_of_range &e ) {
159  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
160  throw( std::out_of_range( msg ) );
161  }
162  catch( const std::logic_error &e ) {
163  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR(
164  "Image, window end, and/or window size dimensions do not match." ) );
165  throw( std::logic_error( msg ) );
166  }
167 
168  igzstream( const char *name, int open_mode = std::ios::in ) try : gzstreambase( name,
169  open_mode ), std::istream( &buf ) {
170  }
171  catch( std::ios_base::failure &e ) {
172  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
173  throw( std::ios_base::failure( msg ) );
174  }
175  catch( std::bad_alloc &e ) {
176  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
177  throw( std::runtime_error( msg ) );
178  }
179  catch( std::runtime_error &e ) {
180  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
181  throw( std::runtime_error( msg ) );
182  }
183  catch( const std::out_of_range &e ) {
184  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
185  throw( std::out_of_range( msg ) );
186  }
187  catch( const std::logic_error &e ) {
188  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR(
189  "Image, window end, and/or window size dimensions do not match." ) );
190  throw( std::logic_error( msg ) );
191  }
192 
194  try {
195  return( gzstreambase::rdbuf( ) );
196  }
197  catch( std::ios_base::failure &e ) {
198  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
199  throw( std::ios_base::failure( msg ) );
200  }
201  catch( std::bad_alloc &e ) {
202  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
203  throw( std::runtime_error( msg ) );
204  }
205  catch( std::runtime_error &e ) {
206  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
207  throw( std::runtime_error( msg ) );
208  }
209  catch( const std::out_of_range &e ) {
210  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
211  throw( std::out_of_range( msg ) );
212  }
213  catch( const std::logic_error &e ) {
214  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
215  throw( std::logic_error( msg ) );
216  }
217  }
218 
219  void open( const char *name, int open_mode = std::ios::in ) {
220  try {
221  gzstreambase::open( name, open_mode );
222  }
223  catch( std::ios_base::failure &e ) {
224  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
225  throw( std::ios_base::failure( msg ) );
226  }
227  catch( std::bad_alloc &e ) {
228  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
229  throw( std::runtime_error( msg ) );
230  }
231  catch( std::runtime_error &e ) {
232  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
233  throw( std::runtime_error( msg ) );
234  }
235  catch( const std::out_of_range &e ) {
236  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
237  throw( std::out_of_range( msg ) );
238  }
239  catch( const std::logic_error &e ) {
240  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
241  throw( std::logic_error( msg ) );
242  }
243  }
244 
245  bool is_open( ) const {
246  try {
247  return( gzstreambase::buf.is_open( ) );
248  }
249  catch( std::ios_base::failure &e ) {
250  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
251  throw( std::ios_base::failure( msg ) );
252  }
253  catch( std::bad_alloc &e ) {
254  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
255  throw( std::runtime_error( msg ) );
256  }
257  catch( std::runtime_error &e ) {
258  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
259  throw( std::runtime_error( msg ) );
260  }
261  catch( const std::out_of_range &e ) {
262  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
263  throw( std::out_of_range( msg ) );
264  }
265  catch( const std::logic_error &e ) {
266  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
267  throw( std::logic_error( msg ) );
268  }
269  }
270 
271  };
272 
273  class ogzstream : public gzstreambase, public std::ostream {
274 public:
275  ogzstream( ) try : std::ostream( &buf ) {
276  }
277  catch( std::ios_base::failure &e ) {
278  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
279  throw( std::ios_base::failure( msg ) );
280  }
281  catch( std::bad_alloc &e ) {
282  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
283  throw( std::runtime_error( msg ) );
284  }
285  catch( std::runtime_error &e ) {
286  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
287  throw( std::runtime_error( msg ) );
288  }
289  catch( const std::out_of_range &e ) {
290  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
291  throw( std::out_of_range( msg ) );
292  }
293  catch( const std::logic_error &e ) {
294  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR(
295  "Image, window end, and/or window size dimensions do not match." ) );
296  throw( std::logic_error( msg ) );
297  }
298 
299  ogzstream( ogzstream && ) try {
300  }
301  catch( std::ios_base::failure &e ) {
302  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
303  throw( std::ios_base::failure( msg ) );
304  }
305  catch( std::bad_alloc &e ) {
306  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
307  throw( std::runtime_error( msg ) );
308  }
309  catch( std::runtime_error &e ) {
310  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
311  throw( std::runtime_error( msg ) );
312  }
313  catch( const std::out_of_range &e ) {
314  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
315  throw( std::out_of_range( msg ) );
316  }
317  catch( const std::logic_error &e ) {
318  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR(
319  "Image, window end, and/or window size dimensions do not match." ) );
320  throw( std::logic_error( msg ) );
321  }
322 
323  ogzstream( const char *name, int mode = std::ios::out ) try : gzstreambase( name, mode ), std::ostream( &buf ) {
324  }
325  catch( std::ios_base::failure &e ) {
326  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
327  throw( std::ios_base::failure( msg ) );
328  }
329  catch( std::bad_alloc &e ) {
330  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
331  throw( std::runtime_error( msg ) );
332  }
333  catch( std::runtime_error &e ) {
334  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
335  throw( std::runtime_error( msg ) );
336  }
337  catch( const std::out_of_range &e ) {
338  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
339  throw( std::out_of_range( msg ) );
340  }
341  catch( const std::logic_error &e ) {
342  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR(
343  "Image, window end, and/or window size dimensions do not match." ) );
344  throw( std::logic_error( msg ) );
345  }
346 
348  try {
349  return( gzstreambase::rdbuf( ) );
350  }
351  catch( std::ios_base::failure &e ) {
352  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
353  throw( std::ios_base::failure( msg ) );
354  }
355  catch( std::bad_alloc &e ) {
356  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
357  throw( std::runtime_error( msg ) );
358  }
359  catch( std::runtime_error &e ) {
360  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
361  throw( std::runtime_error( msg ) );
362  }
363  catch( const std::out_of_range &e ) {
364  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
365  throw( std::out_of_range( msg ) );
366  }
367  catch( const std::logic_error &e ) {
368  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
369  throw( std::logic_error( msg ) );
370  }
371  }
372 
373  void open( const char *name, int open_mode = std::ios::out ) {
374  try {
375  gzstreambase::open( name, open_mode );
376  }
377  catch( std::ios_base::failure &e ) {
378  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
379  throw( std::ios_base::failure( msg ) );
380  }
381  catch( std::bad_alloc &e ) {
382  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
383  throw( std::runtime_error( msg ) );
384  }
385  catch( std::runtime_error &e ) {
386  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
387  throw( std::runtime_error( msg ) );
388  }
389  catch( const std::out_of_range &e ) {
390  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
391  throw( std::out_of_range( msg ) );
392  }
393  catch( const std::logic_error &e ) {
394  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
395  throw( std::logic_error( msg ) );
396  }
397  }
398 
399  bool is_open( ) const {
400  try {
401  return( gzstreambase::buf.is_open( ) );
402  }
403  catch( std::ios_base::failure &e ) {
404  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
405  throw( std::ios_base::failure( msg ) );
406  }
407  catch( std::bad_alloc &e ) {
408  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
409  throw( std::runtime_error( msg ) );
410  }
411  catch( std::runtime_error &e ) {
412  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
413  throw( std::runtime_error( msg ) );
414  }
415  catch( const std::out_of_range &e ) {
416  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
417  throw( std::out_of_range( msg ) );
418  }
419  catch( const std::logic_error &e ) {
420  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
421  throw( std::logic_error( msg ) );
422  }
423  }
424  };
425 
426 }
427 
428 /* Implementation ----------------------------------------------------------------------------------- */
429 
430 #include <iostream>
431 #include <string.h> /* for memcpy */
432 
433 namespace Bial {
434 
435  /* gzstreambuf -------------------------------------------------------------------------------------- */
436 
437  inline gzstreambuf* gzstreambuf::open( const char *name, int open_mode ) {
438  try {
439  if( is_open( ) ) {
440  return( ( gzstreambuf* ) 0 );
441  }
442  mode = open_mode;
443  /* no append nor read/write mode */
444  if( ( mode & std::ios::ate ) || ( mode & std::ios::app ) ||
445  ( ( mode & std::ios::in ) && ( mode & std::ios::out ) ) ) {
446  return( ( gzstreambuf* ) 0 );
447  }
448  char fmode[ 10 ];
449  char *fmodeptr = fmode;
450  if( mode & std::ios::in ) {
451  *fmodeptr++ = 'r';
452  }
453  else if( mode & std::ios::out ) {
454  *fmodeptr++ = 'w';
455  }
456  *fmodeptr++ = 'b';
457  *fmodeptr = '\0';
458  file = gzopen( name, fmode );
459  if( file == 0 ) {
460  return( ( gzstreambuf* ) 0 );
461  }
462  opened = 1;
463  return( this );
464  }
465  catch( std::ios_base::failure &e ) {
466  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
467  throw( std::ios_base::failure( msg ) );
468  }
469  catch( std::bad_alloc &e ) {
470  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
471  throw( std::runtime_error( msg ) );
472  }
473  catch( std::runtime_error &e ) {
474  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
475  throw( std::runtime_error( msg ) );
476  }
477  catch( const std::out_of_range &e ) {
478  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
479  throw( std::out_of_range( msg ) );
480  }
481  catch( const std::logic_error &e ) {
482  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
483  throw( std::logic_error( msg ) );
484  }
485  }
486 
488  try {
489  if( is_open( ) ) {
490  sync( );
491  opened = 0;
492  if( gzclose( file ) == Z_OK ) {
493  return( this );
494  }
495  }
496  return( ( gzstreambuf* ) 0 );
497  }
498  catch( std::ios_base::failure &e ) {
499  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
500  throw( std::ios_base::failure( msg ) );
501  }
502  catch( std::bad_alloc &e ) {
503  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
504  throw( std::runtime_error( msg ) );
505  }
506  catch( std::runtime_error &e ) {
507  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
508  throw( std::runtime_error( msg ) );
509  }
510  catch( const std::out_of_range &e ) {
511  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
512  throw( std::out_of_range( msg ) );
513  }
514  catch( const std::logic_error &e ) {
515  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
516  throw( std::logic_error( msg ) );
517  }
518  }
519 
520  inline int gzstreambuf::underflow( ) { /* used for input buffer only */
521  try {
522  if( gptr( ) && ( gptr( ) < egptr( ) ) ) {
523  return( *reinterpret_cast< unsigned char* >( gptr( ) ) );
524  }
525  if( !( mode & std::ios::in ) || !opened ) {
526  return( EOF );
527  }
528  /* Josuttis' implementation of inbuf */
529  int n_putback = gptr( ) - eback( );
530  if( n_putback > 4 ) {
531  n_putback = 4;
532  }
533  memcpy( buffer + ( 4 - n_putback ), gptr( ) - n_putback, n_putback );
534 
535  int num = gzread( file, buffer + 4, bufferSize - 4 );
536  if( num <= 0 ) { /* ERROR or EOF */
537  return( EOF );
538  }
539  /* reset buffer pointers */
540  setg( buffer + ( 4 - n_putback ), /* beginning of putback area */
541  buffer + 4, /* read position */
542  buffer + 4 + num ); /* end of buffer */
543 
544  /* return next character */
545  return( *reinterpret_cast< unsigned char* >( gptr( ) ) );
546  }
547  catch( std::ios_base::failure &e ) {
548  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
549  throw( std::ios_base::failure( msg ) );
550  }
551  catch( std::bad_alloc &e ) {
552  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
553  throw( std::runtime_error( msg ) );
554  }
555  catch( std::runtime_error &e ) {
556  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
557  throw( std::runtime_error( msg ) );
558  }
559  catch( const std::out_of_range &e ) {
560  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
561  throw( std::out_of_range( msg ) );
562  }
563  catch( const std::logic_error &e ) {
564  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
565  throw( std::logic_error( msg ) );
566  }
567  }
568 
569  inline int gzstreambuf::flush_buffer( ) {
570  try {
571  /*
572  * Separate the writing of the buffer from overflow() and
573  * sync( ) operation.
574  */
575  int w = std::streambuf::pptr( ) - std::streambuf::pbase( );
576  if( gzwrite( file, std::streambuf::pbase( ), w ) != w ) {
577  return( EOF );
578  }
579  pbump( -w );
580  return( w );
581  }
582  catch( std::ios_base::failure &e ) {
583  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
584  throw( std::ios_base::failure( msg ) );
585  }
586  catch( std::bad_alloc &e ) {
587  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
588  throw( std::runtime_error( msg ) );
589  }
590  catch( std::runtime_error &e ) {
591  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
592  throw( std::runtime_error( msg ) );
593  }
594  catch( const std::out_of_range &e ) {
595  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
596  throw( std::out_of_range( msg ) );
597  }
598  catch( const std::logic_error &e ) {
599  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
600  throw( std::logic_error( msg ) );
601  }
602  }
603 
604  inline int gzstreambuf::overflow( int c ) { /* used for output buffer only */
605  try {
606  if( !( mode & std::ios::out ) || !opened ) {
607  return( EOF );
608  }
609  if( c != EOF ) {
610  *std::streambuf::pptr( ) = c;
611  pbump( 1 );
612  }
613  if( flush_buffer( ) == EOF ) {
614  return( EOF );
615  }
616  return( c );
617  }
618  catch( std::ios_base::failure &e ) {
619  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
620  throw( std::ios_base::failure( msg ) );
621  }
622  catch( std::bad_alloc &e ) {
623  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
624  throw( std::runtime_error( msg ) );
625  }
626  catch( std::runtime_error &e ) {
627  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
628  throw( std::runtime_error( msg ) );
629  }
630  catch( const std::out_of_range &e ) {
631  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
632  throw( std::out_of_range( msg ) );
633  }
634  catch( const std::logic_error &e ) {
635  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
636  throw( std::logic_error( msg ) );
637  }
638  }
639 
640  inline int gzstreambuf::sync( ) {
641  try {
642  if( std::streambuf::pptr( ) && ( std::streambuf::pptr( ) > std::streambuf::pbase( ) ) ) {
643  if( flush_buffer( ) == EOF ) {
644  return( -1 );
645  }
646  }
647  return( 0 );
648  }
649  catch( std::ios_base::failure &e ) {
650  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
651  throw( std::ios_base::failure( msg ) );
652  }
653  catch( std::bad_alloc &e ) {
654  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
655  throw( std::runtime_error( msg ) );
656  }
657  catch( std::runtime_error &e ) {
658  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
659  throw( std::runtime_error( msg ) );
660  }
661  catch( const std::out_of_range &e ) {
662  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
663  throw( std::out_of_range( msg ) );
664  }
665  catch( const std::logic_error &e ) {
666  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
667  throw( std::logic_error( msg ) );
668  }
669  }
670 
671  /* gzstreambase ------------------------------------------------------------------------------------ */
672 
673  inline gzstreambase::gzstreambase( const char *name, int mode ) {
674  try {
675  init( &buf );
676  open( name, mode );
677  }
678  catch( std::ios_base::failure &e ) {
679  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
680  throw( std::ios_base::failure( msg ) );
681  }
682  catch( std::bad_alloc &e ) {
683  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
684  throw( std::runtime_error( msg ) );
685  }
686  catch( std::runtime_error &e ) {
687  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
688  throw( std::runtime_error( msg ) );
689  }
690  catch( const std::out_of_range &e ) {
691  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
692  throw( std::out_of_range( msg ) );
693  }
694  catch( const std::logic_error &e ) {
695  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
696  throw( std::logic_error( msg ) );
697  }
698  }
699 
701  try {
702  buf.close( );
703  }
704  catch( std::ios_base::failure &e ) {
705  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
706  throw( std::ios_base::failure( msg ) );
707  }
708  catch( std::bad_alloc &e ) {
709  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
710  throw( std::runtime_error( msg ) );
711  }
712  catch( std::runtime_error &e ) {
713  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
714  throw( std::runtime_error( msg ) );
715  }
716  catch( const std::out_of_range &e ) {
717  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
718  throw( std::out_of_range( msg ) );
719  }
720  catch( const std::logic_error &e ) {
721  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
722  throw( std::logic_error( msg ) );
723  }
724  }
725 
726  inline void gzstreambase::open( const char *name, int open_mode ) {
727  try {
728  if( !buf.open( name, open_mode ) ) {
729  clear( rdstate( ) | std::ios::badbit );
730  }
731  }
732  catch( std::ios_base::failure &e ) {
733  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
734  throw( std::ios_base::failure( msg ) );
735  }
736  catch( std::bad_alloc &e ) {
737  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
738  throw( std::runtime_error( msg ) );
739  }
740  catch( std::runtime_error &e ) {
741  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
742  throw( std::runtime_error( msg ) );
743  }
744  catch( const std::out_of_range &e ) {
745  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
746  throw( std::out_of_range( msg ) );
747  }
748  catch( const std::logic_error &e ) {
749  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
750  throw( std::logic_error( msg ) );
751  }
752  }
753 
754  inline void gzstreambase::close( ) {
755  try {
756  if( buf.is_open( ) ) {
757  if( !buf.close( ) ) {
758  clear( rdstate( ) | std::ios::badbit );
759  }
760  }
761  }
762  catch( std::ios_base::failure &e ) {
763  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "I/O error while reading file." ) );
764  throw( std::ios_base::failure( msg ) );
765  }
766  catch( std::bad_alloc &e ) {
767  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Memory allocation error." ) );
768  throw( std::runtime_error( msg ) );
769  }
770  catch( std::runtime_error &e ) {
771  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Runtime error." ) );
772  throw( std::runtime_error( msg ) );
773  }
774  catch( const std::out_of_range &e ) {
775  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Out of range exception." ) );
776  throw( std::out_of_range( msg ) );
777  }
778  catch( const std::logic_error &e ) {
779  std::string msg( e.what( ) + std::string( "\n" ) + BIAL_ERROR( "Logic Error." ) );
780  throw( std::logic_error( msg ) );
781  }
782  }
783 
784 }
785 
786 #endif
gzstreambuf * rdbuf()
Definition: gzstream.hpp:347
void open(const char *name, int open_mode=std::ios::in)
Definition: gzstream.hpp:219
virtual int overflow(int c=EOF)
Definition: gzstream.hpp:604
void open(const char *name, int open_mode)
Definition: gzstream.hpp:726
bool is_open() const
Definition: gzstream.hpp:399
virtual int underflow()
Definition: gzstream.hpp:520
#define BIAL_ERROR(exp)
Use BIAL_ERROR to compose runtime error messages. Note that ERROR generates a string, instead of a stream, because it is better to throw an exception, rather than exiting the program. This way, the exception may be threated by a higher level program, like an user IDE.
Definition: Common.hpp:161
int gzread(gzFile file, voidp buf, unsigned len)
Definition: gzread.c:288
gzFile gzopen(char *path, char *mode)
Definition: gzlib.c:268
Content: Commonly used macros, types, static variables and functions. Description: Some macros used...
static big_t * num
Definition: enough.c:175
ogzstream(ogzstream &&)
Definition: gzstream.hpp:299
igzstream(const char *name, int open_mode=std::ios::in)
Definition: gzstream.hpp:168
igzstream(igzstream &&)
Definition: gzstream.hpp:144
Adjacency relation with indexes following gray code sequency. Future add-on&#39;s: none.
Definition: Adjacency.hpp:19
int gzclose(gzFile file)
Definition: gzclose.c:11
ogzstream(const char *name, int mode=std::ios::out)
Definition: gzstream.hpp:323
static int out(void *out_desc, unsigned char *buf, unsigned len)
Definition: gun.c:131
gzstreambuf * rdbuf()
Definition: gzstream.hpp:193
gzstreambuf * rdbuf()
Definition: gzstream.hpp:105
bool is_open() const
Definition: gzstream.hpp:245
gzstreambuf buf
Definition: gzstream.hpp:96
gzstreambuf * open(const char *name, int open_mode)
Definition: gzstream.hpp:437
gzstreambuf * close()
Definition: gzstream.hpp:487
static unsigned in(void *in_desc, z_const unsigned char **buf)
Definition: gun.c:89
#define Z_OK
Definition: zlib.h:173
void open(const char *name, int open_mode=std::ios::out)
Definition: gzstream.hpp:373
int gzwrite(gzFile file, voidpc buf, unsigned len)
Definition: gzwrite.c:165
Definition: gzappend.c:170
virtual int sync()
Definition: gzstream.hpp:640
bool is_open() const
Definition: gzstream.hpp:80