]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: zstream.cpp | |
3 | // Purpose: Compressed stream classes | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 11/07/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "zstream.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #if wxUSE_ZLIB && wxUSE_STREAMS | |
24 | ||
25 | #include "wx/zstream.h" | |
26 | #include "wx/utils.h" | |
27 | #include "wx/intl.h" | |
28 | #include "wx/log.h" | |
29 | ||
30 | // normally, the compiler options should contain -I../zlib, but it is | |
31 | // apparently not the case for all MSW makefiles and so, unless we use | |
32 | // configure (which defines __WX_SETUP_H__) or it is explicitly overridden by | |
33 | // the user (who can define wxUSE_ZLIB_H_IN_PATH), we hardcode the path here | |
34 | #if defined(__WXMSW__) && !defined(__WX_SETUP_H__) && !defined(wxUSE_ZLIB_H_IN_PATH) | |
35 | #include "../zlib/zlib.h" | |
36 | #else | |
37 | #include <zlib.h> | |
38 | #endif | |
39 | ||
40 | #define ZSTREAM_BUFFER_SIZE 1024 | |
41 | ||
42 | ////////////////////// | |
43 | // wxZlibInputStream | |
44 | ////////////////////// | |
45 | ||
46 | wxZlibInputStream::wxZlibInputStream(wxInputStream& stream) | |
47 | : wxFilterInputStream(stream) | |
48 | { | |
49 | int err; | |
50 | ||
51 | // I need a private stream buffer. | |
52 | m_inflate = new z_stream_s; | |
53 | ||
54 | m_inflate->zalloc = (alloc_func)0; | |
55 | m_inflate->zfree = (free_func)0; | |
56 | m_inflate->opaque = (voidpf)0; | |
57 | ||
58 | err = inflateInit(m_inflate); | |
59 | if (err != Z_OK) { | |
60 | inflateEnd(m_inflate); | |
61 | delete m_inflate; | |
62 | return; | |
63 | } | |
64 | ||
65 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; | |
66 | m_z_size = ZSTREAM_BUFFER_SIZE; | |
67 | ||
68 | m_inflate->avail_in = 0; | |
69 | m_inflate->next_in = NULL; | |
70 | } | |
71 | ||
72 | wxZlibInputStream::~wxZlibInputStream() | |
73 | { | |
74 | inflateEnd(m_inflate); | |
75 | delete m_inflate; | |
76 | } | |
77 | ||
78 | size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size) | |
79 | { | |
80 | int err; | |
81 | ||
82 | m_inflate->next_out = (unsigned char *)buffer; | |
83 | m_inflate->avail_out = size; | |
84 | ||
85 | while (m_inflate->avail_out > 0) { | |
86 | if (m_inflate->avail_in == 0) { | |
87 | ||
88 | m_parent_i_stream->Read(m_z_buffer, wxMin(m_z_size, size)); | |
89 | m_inflate->next_in = m_z_buffer; | |
90 | m_inflate->avail_in = m_parent_i_stream->LastRead(); | |
91 | ||
92 | if (m_parent_i_stream->LastError() != wxStream_NOERROR && | |
93 | m_parent_i_stream->LastError() != wxStream_EOF) | |
94 | { | |
95 | m_lasterror = m_parent_i_stream->LastError(); | |
96 | return 0; // failed to read anything | |
97 | } | |
98 | ||
99 | if ( m_inflate->avail_in == 0 ) | |
100 | { | |
101 | // EOF | |
102 | m_lasterror = wxStream_EOF; | |
103 | break; | |
104 | } | |
105 | } | |
106 | err = inflate(m_inflate, Z_FINISH); | |
107 | if (err == Z_STREAM_END) | |
108 | return (size - m_inflate->avail_out); | |
109 | } | |
110 | ||
111 | return size-m_inflate->avail_out; | |
112 | } | |
113 | ||
114 | ////////////////////// | |
115 | // wxZlibOutputStream | |
116 | ////////////////////// | |
117 | ||
118 | wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream, int level) | |
119 | : wxFilterOutputStream(stream) | |
120 | { | |
121 | int err; | |
122 | ||
123 | m_deflate = new z_stream_s; | |
124 | ||
125 | m_deflate->zalloc = (alloc_func)0; | |
126 | m_deflate->zfree = (free_func)0; | |
127 | m_deflate->opaque = (voidpf)0; | |
128 | ||
129 | if (level == -1) level = Z_DEFAULT_COMPRESSION; | |
130 | wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!")); | |
131 | ||
132 | err = deflateInit(m_deflate, level); | |
133 | if (err != Z_OK) { | |
134 | deflateEnd(m_deflate); | |
135 | return; | |
136 | } | |
137 | ||
138 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; | |
139 | m_z_size = ZSTREAM_BUFFER_SIZE; | |
140 | ||
141 | m_deflate->avail_in = 0; | |
142 | m_deflate->next_out = m_z_buffer; | |
143 | m_deflate->avail_out = m_z_size; | |
144 | } | |
145 | ||
146 | wxZlibOutputStream::~wxZlibOutputStream() | |
147 | { | |
148 | int err; | |
149 | ||
150 | Sync(); | |
151 | ||
152 | err = deflate(m_deflate, Z_FINISH); | |
153 | if (err != Z_STREAM_END) | |
154 | { | |
155 | wxLogDebug( wxT("wxZlibOutputStream: an error occured while closing the stream.\n") ); | |
156 | return; | |
157 | } | |
158 | ||
159 | deflateEnd(m_deflate); | |
160 | ||
161 | delete[] m_z_buffer; | |
162 | } | |
163 | ||
164 | void wxZlibOutputStream::Sync() | |
165 | { | |
166 | int err; | |
167 | ||
168 | m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out); | |
169 | m_deflate->next_out = m_z_buffer; | |
170 | m_deflate->avail_out = m_z_size; | |
171 | ||
172 | err = deflate(m_deflate, Z_FULL_FLUSH); | |
173 | if (err != Z_OK) { | |
174 | return; | |
175 | } | |
176 | ||
177 | // Fixed by "Stefan Csomor" <csomor@advancedconcepts.ch> | |
178 | while( m_deflate->avail_out == 0 ) | |
179 | { | |
180 | m_parent_o_stream->Write(m_z_buffer, m_z_size ); | |
181 | m_deflate->next_out = m_z_buffer; | |
182 | m_deflate->avail_out = m_z_size; | |
183 | err = deflate(m_deflate, Z_FULL_FLUSH); | |
184 | if (err != Z_OK) { | |
185 | return; | |
186 | } | |
187 | } | |
188 | // End | |
189 | ||
190 | m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out); | |
191 | m_deflate->next_out = m_z_buffer; | |
192 | m_deflate->avail_out = m_z_size; | |
193 | } | |
194 | ||
195 | size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size) | |
196 | { | |
197 | int err; | |
198 | ||
199 | m_deflate->next_in = (unsigned char *)buffer; | |
200 | m_deflate->avail_in = size; | |
201 | ||
202 | while (m_deflate->avail_in > 0) { | |
203 | ||
204 | if (m_deflate->avail_out == 0) { | |
205 | m_parent_o_stream->Write(m_z_buffer, m_z_size); | |
206 | if (m_parent_o_stream->LastError() != wxStream_NOERROR) | |
207 | return (size - m_deflate->avail_in); | |
208 | ||
209 | m_deflate->next_out = m_z_buffer; | |
210 | m_deflate->avail_out = m_z_size; | |
211 | } | |
212 | ||
213 | err = deflate(m_deflate, Z_NO_FLUSH); | |
214 | if (err != Z_OK) | |
215 | return (size - m_deflate->avail_in); | |
216 | } | |
217 | return size; | |
218 | } | |
219 | ||
220 | #endif | |
221 | // wxUSE_ZLIB && wxUSE_STREAMS | |
222 |