1 /******************************************************************************
2 * $LastChangedDate: 2012-10-25 21:35:57 -0400 (Thu, 25 Oct 2012) $
3 * @file
4 * @author Jim E. Brooks http://www.palomino3d.org
5 * @brief gzstream : C++ iostream classes wrapping the zlib compression library
6 * @verbatim
7 *
8 * Standard streambuf implementation following Nicolai Josuttis, "The Standard C++ Library".
9 *
10 * Example of reading stream (see also test_gunzip.C in gzstream.tgz):
11 * igzstream compressedStream;
12 * compressedStream.open( pathname.c_str() );
13 * if ( not compressedStream.good() )
14 * throw Exception;
15 *
16 * @endverbatim
17 *//*
18 * LEGAL: COPYRIGHT (C) 2004 JIM E. BROOKS
19 * THIS SOURCE CODE IS RELEASED UNDER THE TERMS
20 * OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
21 *------------------------------------------------------------------------------
22 * Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
23 * License: LGPL (GNU Lesser General Public License 2.1)
24 * http://www.cs.unc.edu/Research/compgeom/gzstream/
25 ******************************************************************************/
26
27 // ============================================================================
28
29 #if COMPILE_ZLIB
30
31 #include <iostream>
32 #include <cstring> // for memcpy
33
34 //-- palomino --
35 #include "base/module.hh"
36 #include "base/stream_zlib.hh"
37
38 #pragma GCC diagnostic ignored "-Wshadow"
39
40 namespace base {
41
42 //-- palomino --
43
44 // ----------------------------------------------------------------------------
45 // Internal classes to implement gzstream. See header file for user classes.
46 // ----------------------------------------------------------------------------
47
48 // --------------------------------------
49 // class gzstreambuf:
50 // --------------------------------------
51
open(const char * name,int open_mode)52 gzstreambuf* gzstreambuf::open( const char* name, int open_mode) {
53 if ( is_open())
54 return NULL;
55 mode = open_mode;
56 // no append nor read/write mode
57 if ((mode & std::ios::ate) or (mode & std::ios::app)
58 or ((mode & std::ios::in) and (mode & std::ios::out)))
59 return NULL;
60 char fmode[10];
61 char* fmodeptr = fmode;
62 if ( mode & std::ios::in)
63 *fmodeptr++ = 'r';
64 else if ( mode & std::ios::out)
65 *fmodeptr++ = 'w';
66 *fmodeptr++ = 'b';
67 *fmodeptr = '\0';
68 file = gzopen( name, fmode);
69 if (file == 0)
70 return NULL;
71 opened = 1;
72 return this;
73 }
74
close()75 gzstreambuf * gzstreambuf::close() {
76 if ( is_open()) {
77 sync();
78 opened = 0;
79 if ( gzclose( file) == Z_OK)
80 return this;
81 }
82 return NULL;
83 }
84
underflow()85 int gzstreambuf::underflow() { // used for input buffer only
86 if ( gptr() and ( gptr() < egptr()))
87 return * reinterpret_cast<unsigned char *>( gptr());
88
89 if ( ! (mode & std::ios::in) or ! opened)
90 return EOF;
91 // Josuttis' implementation of inbuf
92 int n_putback = gptr() - eback();
93 if ( n_putback > 4)
94 n_putback = 4;
95 memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
96
97 int num = gzread( file, buffer+4, bufferSize-4);
98 if (num <= 0) // ERROR or EOF
99 return EOF;
100
101 // reset buffer pointers
102 setg( buffer + (4 - n_putback), // beginning of putback area
103 buffer + 4, // read position
104 buffer + 4 + num); // end of buffer
105
106 // return next character
107 return * reinterpret_cast<unsigned char *>( gptr());
108 }
109
flush_buffer()110 int gzstreambuf::flush_buffer() {
111 // Separate the writing of the buffer from overflow() and
112 // sync() operation.
113 int w = pptr() - pbase();
114 if ( gzwrite( file, pbase(), w) != w)
115 return EOF;
116 pbump( -w);
117 return w;
118 }
119
overflow(int c)120 int gzstreambuf::overflow( int c) { // used for output buffer only
121 if ( ! ( mode & std::ios::out) or ! opened)
122 return EOF;
123 if (c != EOF) {
124 *pptr() = c;
125 pbump(1);
126 }
127 if ( flush_buffer() == EOF)
128 return EOF;
129 return c;
130 }
131
sync()132 int gzstreambuf::sync() {
133 // Changed to use flush_buffer() instead of overflow( EOF)
134 // which caused improper behavior with std::endl and flush(),
135 // bug reported by Vincent Ricard.
136 if ( pptr() and pptr() > pbase()) {
137 if ( flush_buffer() == EOF)
138 return -1;
139 }
140 return 0;
141 }
142
143 // --------------------------------------
144 // class gzstreambase:
145 // --------------------------------------
146
gzstreambase(const char * name,int mode)147 gzstreambase::gzstreambase( const char* name, int mode) {
148 init( &buf);
149 open( name, mode);
150 }
151
~gzstreambase()152 gzstreambase::~gzstreambase() {
153 buf.close();
154 }
155
open(const char * name,int open_mode)156 void gzstreambase::open( const char* name, int open_mode) {
157 if ( ! buf.open( name, open_mode))
158 clear( rdstate() | std::ios::badbit);
159 }
160
close()161 void gzstreambase::close() {
162 if ( buf.is_open())
163 if ( ! buf.close())
164 clear( rdstate() | std::ios::badbit);
165 }
166
167 } // namespace base
168
169 #endif // COMPILE_ZLIB
170