]> git.saurik.com Git - wxWidgets.git/blame - src/common/zstream.cpp
Dialog unit mods; wxProp tidying
[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/////////////////////////////////////////////////////////////////////////////
11#ifdef __GNUG__
12#pragma implementation "zstream.h"
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17#include <wx/stream.h>
18#include <wx/zstream.h>
19#include <wx/utils.h>
1a5a8367 20#include <wx/intl.h>
e3e65dac 21#include "../zlib/zlib.h" // don't change this, Robert
79c3e0e1
GL
22
23#ifdef __BORLANDC__
24#pragma hdrstop
25#endif
26
6d44bf31
GL
27#define ZSTREAM_BUFFER_SIZE 1024
28
79c3e0e1
GL
29//////////////////////
30// wxZlibInputStream
31//////////////////////
32
33wxZlibInputStream::wxZlibInputStream(wxInputStream& stream)
34 : wxFilterInputStream(stream)
35{
36 int err;
37
6d44bf31
GL
38 // I need a private stream buffer.
39 m_i_streambuf = new wxStreamBuffer(*this);
40 m_i_destroybuf = TRUE;
856d2e52 41 m_inflate = new z_stream_s;
6d44bf31 42
856d2e52
GL
43 m_inflate->zalloc = (alloc_func)0;
44 m_inflate->zfree = (free_func)0;
45 m_inflate->opaque = (voidpf)0;
79c3e0e1 46
856d2e52 47 err = inflateInit(m_inflate);
79c3e0e1 48 if (err != Z_OK) {
856d2e52
GL
49 inflateEnd(m_inflate);
50 delete m_inflate;
79c3e0e1
GL
51 return;
52 }
53
6d44bf31
GL
54 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
55 m_z_size = ZSTREAM_BUFFER_SIZE;
56
856d2e52
GL
57 m_inflate->avail_in = 0;
58 m_inflate->next_in = NULL;
79c3e0e1
GL
59}
60
61wxZlibInputStream::~wxZlibInputStream()
62{
856d2e52
GL
63 inflateEnd(m_inflate);
64 delete m_inflate;
79c3e0e1
GL
65}
66
6d44bf31 67size_t wxZlibInputStream::DoRead(void *buffer, size_t size)
79c3e0e1
GL
68{
69 int err;
70
856d2e52
GL
71 m_inflate->next_out = (unsigned char *)buffer;
72 m_inflate->avail_out = size;
79c3e0e1 73
856d2e52
GL
74 while (m_inflate->avail_out > 0) {
75 if (m_inflate->avail_in == 0) {
6d44bf31
GL
76
77 m_parent_i_stream->Read(m_z_buffer, m_z_size);
856d2e52
GL
78 m_inflate->next_in = m_z_buffer;
79 m_inflate->avail_in = m_parent_i_stream->LastRead();
6d44bf31
GL
80
81 if (m_parent_i_stream->Eof())
856d2e52 82 return (size - m_inflate->avail_in);
79c3e0e1 83 }
856d2e52 84 err = inflate(m_inflate, Z_FINISH);
6d44bf31 85 if (err == Z_STREAM_END)
856d2e52 86 return (size - m_inflate->avail_in);
79c3e0e1
GL
87 }
88
856d2e52 89 return size-m_inflate->avail_in;
79c3e0e1
GL
90}
91
92bool wxZlibInputStream::Eof() const
93{
94 if (!m_eof)
6d44bf31 95 return m_parent_i_stream->Eof();
79c3e0e1
GL
96 return m_eof;
97}
98
99//////////////////////
100// wxZlibOutputStream
101//////////////////////
102
103wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream)
104 : wxFilterOutputStream(stream)
105{
106 int err;
107
6d44bf31
GL
108 m_o_streambuf = new wxStreamBuffer(*this);
109 m_o_destroybuf = TRUE;
856d2e52 110 m_deflate = new z_stream_s;
6d44bf31 111
856d2e52
GL
112 m_deflate->zalloc = (alloc_func)0;
113 m_deflate->zfree = (free_func)0;
114 m_deflate->opaque = (voidpf)0;
79c3e0e1 115
856d2e52 116 err = deflateInit(m_deflate, Z_DEFAULT_COMPRESSION);
79c3e0e1 117 if (err != Z_OK) {
856d2e52 118 deflateEnd(m_deflate);
79c3e0e1
GL
119 return;
120 }
6d44bf31
GL
121
122 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
123 m_z_size = ZSTREAM_BUFFER_SIZE;
124
856d2e52
GL
125 m_deflate->avail_in = 0;
126 m_deflate->next_out = m_z_buffer;
127 m_deflate->avail_out = m_z_size;
79c3e0e1
GL
128}
129
130wxZlibOutputStream::~wxZlibOutputStream()
131{
132 int err;
133
6d44bf31
GL
134 Sync();
135
856d2e52 136 err = deflate(m_deflate, Z_FINISH);
6d44bf31 137 if (err != Z_STREAM_END) {
1a5a8367
DP
138 wxDebugMsg(_("wxZlibOutputStream: an error occured while we was closing "
139 "the stream.\n"));
6d44bf31 140 return;
79c3e0e1 141 }
79c3e0e1 142
856d2e52 143 deflateEnd(m_deflate);
6d44bf31
GL
144
145 delete[] m_z_buffer;
79c3e0e1
GL
146}
147
6d44bf31
GL
148void wxZlibOutputStream::Sync()
149{
150 int err;
151
856d2e52
GL
152 m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out);
153 m_deflate->next_out = m_z_buffer;
154 m_deflate->avail_out = m_z_size;
6d44bf31 155
856d2e52 156 err = deflate(m_deflate, Z_FULL_FLUSH);
6d44bf31
GL
157 if (err != Z_OK) {
158 m_bad = TRUE;
159 return;
160 }
161
856d2e52
GL
162 m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out);
163 m_deflate->next_out = m_z_buffer;
164 m_deflate->avail_out = m_z_size;
6d44bf31
GL
165}
166
167size_t wxZlibOutputStream::DoWrite(const void *buffer, size_t size)
79c3e0e1
GL
168{
169 int err;
170
856d2e52
GL
171 m_deflate->next_in = (unsigned char *)buffer;
172 m_deflate->avail_in = size;
79c3e0e1 173
856d2e52 174 while (m_deflate->avail_in > 0) {
6d44bf31 175
856d2e52 176 if (m_deflate->avail_out == 0) {
6d44bf31
GL
177 m_parent_o_stream->Write(m_z_buffer, m_z_size);
178 if (m_parent_o_stream->Bad())
856d2e52 179 return (size - m_deflate->avail_in);
6d44bf31 180
856d2e52
GL
181 m_deflate->next_out = m_z_buffer;
182 m_deflate->avail_out = m_z_size;
79c3e0e1 183 }
6d44bf31 184
856d2e52 185 err = deflate(m_deflate, Z_NO_FLUSH);
6d44bf31 186 if (err != Z_OK)
856d2e52 187 return (size - m_deflate->avail_in);
79c3e0e1 188 }
6d44bf31 189 return size;
79c3e0e1
GL
190}
191
192bool wxZlibOutputStream::Bad() const
193{
194 if (!m_bad)
6d44bf31 195 return m_parent_o_stream->Bad();
79c3e0e1
GL
196 return m_bad;
197}