]> git.saurik.com Git - wxWidgets.git/blob - src/common/zstream.cpp
Did somework on the generic dialogs,
[wxWidgets.git] / src / common / zstream.cpp
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 // When using configure, the path must be "zlib.h" I don't know
31 // what other ports (wxMac, wxMotif without configure) need here.
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__)
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, m_z_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 return (size - m_inflate->avail_in);
94 }
95 err = inflate(m_inflate, Z_FINISH);
96 if (err == Z_STREAM_END)
97 return (size - m_inflate->avail_in);
98 }
99
100 return size-m_inflate->avail_in;
101 }
102
103 //////////////////////
104 // wxZlibOutputStream
105 //////////////////////
106
107 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream)
108 : wxFilterOutputStream(stream)
109 {
110 int err;
111
112 m_deflate = new z_stream_s;
113
114 m_deflate->zalloc = (alloc_func)0;
115 m_deflate->zfree = (free_func)0;
116 m_deflate->opaque = (voidpf)0;
117
118 err = deflateInit(m_deflate, Z_DEFAULT_COMPRESSION);
119 if (err != Z_OK) {
120 deflateEnd(m_deflate);
121 return;
122 }
123
124 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
125 m_z_size = ZSTREAM_BUFFER_SIZE;
126
127 m_deflate->avail_in = 0;
128 m_deflate->next_out = m_z_buffer;
129 m_deflate->avail_out = m_z_size;
130 }
131
132 wxZlibOutputStream::~wxZlibOutputStream()
133 {
134 int err;
135
136 Sync();
137
138 err = deflate(m_deflate, Z_FINISH);
139 if (err != Z_STREAM_END)
140 {
141 wxLogDebug( _T("wxZlibOutputStream: an error occured while closing the stream.\n") );
142 return;
143 }
144
145 deflateEnd(m_deflate);
146
147 delete[] m_z_buffer;
148 }
149
150 void wxZlibOutputStream::Sync()
151 {
152 int err;
153
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;
157
158 err = deflate(m_deflate, Z_FULL_FLUSH);
159 if (err != Z_OK) {
160 return;
161 }
162
163 m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out);
164 m_deflate->next_out = m_z_buffer;
165 m_deflate->avail_out = m_z_size;
166 }
167
168 size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size)
169 {
170 int err;
171
172 m_deflate->next_in = (unsigned char *)buffer;
173 m_deflate->avail_in = size;
174
175 while (m_deflate->avail_in > 0) {
176
177 if (m_deflate->avail_out == 0) {
178 m_parent_o_stream->Write(m_z_buffer, m_z_size);
179 if (m_parent_o_stream->LastError() != wxStream_NOERROR)
180 return (size - m_deflate->avail_in);
181
182 m_deflate->next_out = m_z_buffer;
183 m_deflate->avail_out = m_z_size;
184 }
185
186 err = deflate(m_deflate, Z_NO_FLUSH);
187 if (err != Z_OK)
188 return (size - m_deflate->avail_in);
189 }
190 return size;
191 }
192
193 #endif
194 // wxUSE_ZLIB && wxUSE_STREAMS
195