]> git.saurik.com Git - wxWidgets.git/blame - src/common/zstream.cpp
ugly fix for warnings when wxUSE_STL==0 not breaking compilation when wxUSE_STL==1
[wxWidgets.git] / src / common / zstream.cpp
CommitLineData
79c3e0e1
GL
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/////////////////////////////////////////////////////////////////////////////
ce4169a4 11
79c3e0e1
GL
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"
ac57418f 18
ce4169a4
RR
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
ce4169a4 23#if wxUSE_ZLIB && wxUSE_STREAMS
ac57418f 24
ce4169a4 25#include "wx/zstream.h"
ac57418f
RR
26#include "wx/utils.h"
27#include "wx/intl.h"
3069ac4e 28#include "wx/log.h"
d1af991f 29
f6bcfd97
BP
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)
31e78e0c 35 #include "../zlib/zlib.h"
d1af991f 36#else
ac2834ab 37 #include "zlib.h"
d1af991f 38#endif
79c3e0e1 39
6d44bf31
GL
40#define ZSTREAM_BUFFER_SIZE 1024
41
79c3e0e1
GL
42//////////////////////
43// wxZlibInputStream
44//////////////////////
45
46wxZlibInputStream::wxZlibInputStream(wxInputStream& stream)
47 : wxFilterInputStream(stream)
48{
6d44bf31 49 // I need a private stream buffer.
856d2e52 50 m_inflate = new z_stream_s;
6d44bf31 51
856d2e52
GL
52 m_inflate->zalloc = (alloc_func)0;
53 m_inflate->zfree = (free_func)0;
54 m_inflate->opaque = (voidpf)0;
79c3e0e1 55
45805ba3 56 int err = inflateInit(m_inflate);
79c3e0e1 57 if (err != Z_OK) {
856d2e52
GL
58 inflateEnd(m_inflate);
59 delete m_inflate;
79c3e0e1
GL
60 return;
61 }
62
6d44bf31
GL
63 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
64 m_z_size = ZSTREAM_BUFFER_SIZE;
65
856d2e52
GL
66 m_inflate->avail_in = 0;
67 m_inflate->next_in = NULL;
79c3e0e1
GL
68}
69
70wxZlibInputStream::~wxZlibInputStream()
71{
856d2e52
GL
72 inflateEnd(m_inflate);
73 delete m_inflate;
45805ba3
VZ
74
75 delete [] m_z_buffer;
79c3e0e1
GL
76}
77
75ed1d15 78size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size)
79c3e0e1
GL
79{
80 int err;
81
856d2e52
GL
82 m_inflate->next_out = (unsigned char *)buffer;
83 m_inflate->avail_out = size;
79c3e0e1 84
856d2e52
GL
85 while (m_inflate->avail_out > 0) {
86 if (m_inflate->avail_in == 0) {
6d44bf31 87
f6bcfd97 88 m_parent_i_stream->Read(m_z_buffer, wxMin(m_z_size, size));
856d2e52
GL
89 m_inflate->next_in = m_z_buffer;
90 m_inflate->avail_in = m_parent_i_stream->LastRead();
6d44bf31 91
2b5f62a0
VZ
92 wxStreamError err = m_parent_i_stream->GetLastError();
93 if ( err != wxSTREAM_NO_ERROR && err != wxSTREAM_EOF)
777fd647 94 {
2b5f62a0 95 m_lasterror = err;
777fd647
VS
96 return 0; // failed to read anything
97 }
f6bcfd97
BP
98
99 if ( m_inflate->avail_in == 0 )
100 {
101 // EOF
2b5f62a0 102 m_lasterror = wxSTREAM_EOF;
f6bcfd97
BP
103 break;
104 }
79c3e0e1 105 }
856d2e52 106 err = inflate(m_inflate, Z_FINISH);
6d44bf31 107 if (err == Z_STREAM_END)
777fd647 108 return (size - m_inflate->avail_out);
79c3e0e1
GL
109 }
110
777fd647 111 return size-m_inflate->avail_out;
79c3e0e1
GL
112}
113
79c3e0e1
GL
114//////////////////////
115// wxZlibOutputStream
116//////////////////////
117
5824f314 118wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream, int level)
79c3e0e1
GL
119 : wxFilterOutputStream(stream)
120{
121 int err;
122
856d2e52 123 m_deflate = new z_stream_s;
6d44bf31 124
856d2e52
GL
125 m_deflate->zalloc = (alloc_func)0;
126 m_deflate->zfree = (free_func)0;
127 m_deflate->opaque = (voidpf)0;
79c3e0e1 128
af850422
VZ
129 if ( level == -1 )
130 {
131 level = Z_DEFAULT_COMPRESSION;
132 }
133 else
134 {
135 wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!"));
136 }
5824f314
VS
137
138 err = deflateInit(m_deflate, level);
79c3e0e1 139 if (err != Z_OK) {
856d2e52 140 deflateEnd(m_deflate);
79c3e0e1
GL
141 return;
142 }
6d44bf31
GL
143
144 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
145 m_z_size = ZSTREAM_BUFFER_SIZE;
146
856d2e52
GL
147 m_deflate->avail_in = 0;
148 m_deflate->next_out = m_z_buffer;
149 m_deflate->avail_out = m_z_size;
79c3e0e1
GL
150}
151
152wxZlibOutputStream::~wxZlibOutputStream()
153{
154 int err;
155
6d44bf31
GL
156 Sync();
157
856d2e52 158 err = deflate(m_deflate, Z_FINISH);
3069ac4e
RR
159 if (err != Z_STREAM_END)
160 {
223d09f6 161 wxLogDebug( wxT("wxZlibOutputStream: an error occured while closing the stream.\n") );
6d44bf31 162 return;
79c3e0e1 163 }
79c3e0e1 164
856d2e52 165 deflateEnd(m_deflate);
778d618a 166 delete m_deflate;
6d44bf31
GL
167
168 delete[] m_z_buffer;
79c3e0e1
GL
169}
170
6d44bf31
GL
171void wxZlibOutputStream::Sync()
172{
173 int err;
174
856d2e52
GL
175 m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out);
176 m_deflate->next_out = m_z_buffer;
177 m_deflate->avail_out = m_z_size;
6d44bf31 178
856d2e52 179 err = deflate(m_deflate, Z_FULL_FLUSH);
6d44bf31 180 if (err != Z_OK) {
6d44bf31
GL
181 return;
182 }
183
babd4308
GL
184 // Fixed by "Stefan Csomor" <csomor@advancedconcepts.ch>
185 while( m_deflate->avail_out == 0 )
186 {
187 m_parent_o_stream->Write(m_z_buffer, m_z_size );
188 m_deflate->next_out = m_z_buffer;
189 m_deflate->avail_out = m_z_size;
190 err = deflate(m_deflate, Z_FULL_FLUSH);
191 if (err != Z_OK) {
192 return;
193 }
194 }
195 // End
196
856d2e52
GL
197 m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out);
198 m_deflate->next_out = m_z_buffer;
199 m_deflate->avail_out = m_z_size;
6d44bf31
GL
200}
201
75ed1d15 202size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size)
79c3e0e1
GL
203{
204 int err;
205
856d2e52
GL
206 m_deflate->next_in = (unsigned char *)buffer;
207 m_deflate->avail_in = size;
79c3e0e1 208
856d2e52 209 while (m_deflate->avail_in > 0) {
6d44bf31 210
856d2e52 211 if (m_deflate->avail_out == 0) {
6d44bf31 212 m_parent_o_stream->Write(m_z_buffer, m_z_size);
2b5f62a0 213 if ( !*m_parent_o_stream )
856d2e52 214 return (size - m_deflate->avail_in);
6d44bf31 215
856d2e52
GL
216 m_deflate->next_out = m_z_buffer;
217 m_deflate->avail_out = m_z_size;
79c3e0e1 218 }
6d44bf31 219
856d2e52 220 err = deflate(m_deflate, Z_NO_FLUSH);
6d44bf31 221 if (err != Z_OK)
856d2e52 222 return (size - m_deflate->avail_in);
79c3e0e1 223 }
6d44bf31 224 return size;
79c3e0e1 225}
ac57418f
RR
226
227#endif
ce4169a4 228 // wxUSE_ZLIB && wxUSE_STREAMS
ac57418f 229