]> git.saurik.com Git - wxWidgets.git/blame - src/common/zstream.cpp
controls can now be put in the toolbars (MSW only so far, preliminary version)
[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
RR
29
30// When using configure, the path must be "zlib.h" I don't know
31// what other ports (wxMac, wxMotif without configure) need here.
bfcc7d7f
HH
32// If we are building with configure (defines __WX_SETUP_H__),
33// we trust the zlib path is given as a -I option.
34#if defined(__WXMSW__) && !defined(__WX_SETUP_H__)
d1af991f
RR
35 #include "..\zlib\zlib.h"
36#else
37 #include "zlib.h"
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{
49 int err;
50
6d44bf31 51 // I need a private stream buffer.
856d2e52 52 m_inflate = new z_stream_s;
6d44bf31 53
856d2e52
GL
54 m_inflate->zalloc = (alloc_func)0;
55 m_inflate->zfree = (free_func)0;
56 m_inflate->opaque = (voidpf)0;
79c3e0e1 57
856d2e52 58 err = inflateInit(m_inflate);
79c3e0e1 59 if (err != Z_OK) {
856d2e52
GL
60 inflateEnd(m_inflate);
61 delete m_inflate;
79c3e0e1
GL
62 return;
63 }
64
6d44bf31
GL
65 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
66 m_z_size = ZSTREAM_BUFFER_SIZE;
67
856d2e52
GL
68 m_inflate->avail_in = 0;
69 m_inflate->next_in = NULL;
79c3e0e1
GL
70}
71
72wxZlibInputStream::~wxZlibInputStream()
73{
856d2e52
GL
74 inflateEnd(m_inflate);
75 delete m_inflate;
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
GL
87
88 m_parent_i_stream->Read(m_z_buffer, m_z_size);
856d2e52
GL
89 m_inflate->next_in = m_z_buffer;
90 m_inflate->avail_in = m_parent_i_stream->LastRead();
6d44bf31 91
75ed1d15 92 if (m_parent_i_stream->LastError() != wxStream_NOERROR)
856d2e52 93 return (size - m_inflate->avail_in);
79c3e0e1 94 }
856d2e52 95 err = inflate(m_inflate, Z_FINISH);
6d44bf31 96 if (err == Z_STREAM_END)
856d2e52 97 return (size - m_inflate->avail_in);
79c3e0e1
GL
98 }
99
856d2e52 100 return size-m_inflate->avail_in;
79c3e0e1
GL
101}
102
79c3e0e1
GL
103//////////////////////
104// wxZlibOutputStream
105//////////////////////
106
107wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream)
108 : wxFilterOutputStream(stream)
109{
110 int err;
111
856d2e52 112 m_deflate = new z_stream_s;
6d44bf31 113
856d2e52
GL
114 m_deflate->zalloc = (alloc_func)0;
115 m_deflate->zfree = (free_func)0;
116 m_deflate->opaque = (voidpf)0;
79c3e0e1 117
856d2e52 118 err = deflateInit(m_deflate, Z_DEFAULT_COMPRESSION);
79c3e0e1 119 if (err != Z_OK) {
856d2e52 120 deflateEnd(m_deflate);
79c3e0e1
GL
121 return;
122 }
6d44bf31
GL
123
124 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
125 m_z_size = ZSTREAM_BUFFER_SIZE;
126
856d2e52
GL
127 m_deflate->avail_in = 0;
128 m_deflate->next_out = m_z_buffer;
129 m_deflate->avail_out = m_z_size;
79c3e0e1
GL
130}
131
132wxZlibOutputStream::~wxZlibOutputStream()
133{
134 int err;
135
6d44bf31
GL
136 Sync();
137
856d2e52 138 err = deflate(m_deflate, Z_FINISH);
3069ac4e
RR
139 if (err != Z_STREAM_END)
140 {
223d09f6 141 wxLogDebug( wxT("wxZlibOutputStream: an error occured while closing the stream.\n") );
6d44bf31 142 return;
79c3e0e1 143 }
79c3e0e1 144
856d2e52 145 deflateEnd(m_deflate);
6d44bf31
GL
146
147 delete[] m_z_buffer;
79c3e0e1
GL
148}
149
6d44bf31
GL
150void wxZlibOutputStream::Sync()
151{
152 int err;
153
856d2e52
GL
154 m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out);
155 m_deflate->next_out = m_z_buffer;
156 m_deflate->avail_out = m_z_size;
6d44bf31 157
856d2e52 158 err = deflate(m_deflate, Z_FULL_FLUSH);
6d44bf31 159 if (err != Z_OK) {
6d44bf31
GL
160 return;
161 }
162
babd4308
GL
163 // Fixed by "Stefan Csomor" <csomor@advancedconcepts.ch>
164 while( m_deflate->avail_out == 0 )
165 {
166 m_parent_o_stream->Write(m_z_buffer, m_z_size );
167 m_deflate->next_out = m_z_buffer;
168 m_deflate->avail_out = m_z_size;
169 err = deflate(m_deflate, Z_FULL_FLUSH);
170 if (err != Z_OK) {
171 return;
172 }
173 }
174 // End
175
856d2e52
GL
176 m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out);
177 m_deflate->next_out = m_z_buffer;
178 m_deflate->avail_out = m_z_size;
6d44bf31
GL
179}
180
75ed1d15 181size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size)
79c3e0e1
GL
182{
183 int err;
184
856d2e52
GL
185 m_deflate->next_in = (unsigned char *)buffer;
186 m_deflate->avail_in = size;
79c3e0e1 187
856d2e52 188 while (m_deflate->avail_in > 0) {
6d44bf31 189
856d2e52 190 if (m_deflate->avail_out == 0) {
6d44bf31 191 m_parent_o_stream->Write(m_z_buffer, m_z_size);
75ed1d15 192 if (m_parent_o_stream->LastError() != wxStream_NOERROR)
856d2e52 193 return (size - m_deflate->avail_in);
6d44bf31 194
856d2e52
GL
195 m_deflate->next_out = m_z_buffer;
196 m_deflate->avail_out = m_z_size;
79c3e0e1 197 }
6d44bf31 198
856d2e52 199 err = deflate(m_deflate, Z_NO_FLUSH);
6d44bf31 200 if (err != Z_OK)
856d2e52 201 return (size - m_deflate->avail_in);
79c3e0e1 202 }
6d44bf31 203 return size;
79c3e0e1 204}
ac57418f
RR
205
206#endif
ce4169a4 207 // wxUSE_ZLIB && wxUSE_STREAMS
ac57418f 208