]> git.saurik.com Git - wxWidgets.git/blob - src/common/zstream.cpp
attempt to fix a report about compile problems in generic treectrl
[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 #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>
20 #include <wx/intl.h>
21 #include "wx/log.h"
22 #include "../zlib/zlib.h" // don't change this, Robert
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #define ZSTREAM_BUFFER_SIZE 1024
29
30 //////////////////////
31 // wxZlibInputStream
32 //////////////////////
33
34 wxZlibInputStream::wxZlibInputStream(wxInputStream& stream)
35 : wxFilterInputStream(stream)
36 {
37 int err;
38
39 // I need a private stream buffer.
40 m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read);
41 m_i_destroybuf = TRUE;
42 m_inflate = new z_stream_s;
43
44 m_inflate->zalloc = (alloc_func)0;
45 m_inflate->zfree = (free_func)0;
46 m_inflate->opaque = (voidpf)0;
47
48 err = inflateInit(m_inflate);
49 if (err != Z_OK) {
50 inflateEnd(m_inflate);
51 delete m_inflate;
52 return;
53 }
54
55 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
56 m_z_size = ZSTREAM_BUFFER_SIZE;
57
58 m_inflate->avail_in = 0;
59 m_inflate->next_in = NULL;
60 }
61
62 wxZlibInputStream::~wxZlibInputStream()
63 {
64 inflateEnd(m_inflate);
65 delete m_inflate;
66 }
67
68 size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size)
69 {
70 int err;
71
72 m_inflate->next_out = (unsigned char *)buffer;
73 m_inflate->avail_out = size;
74
75 while (m_inflate->avail_out > 0) {
76 if (m_inflate->avail_in == 0) {
77
78 m_parent_i_stream->Read(m_z_buffer, m_z_size);
79 m_inflate->next_in = m_z_buffer;
80 m_inflate->avail_in = m_parent_i_stream->LastRead();
81
82 if (m_parent_i_stream->LastError() != wxStream_NOERROR)
83 return (size - m_inflate->avail_in);
84 }
85 err = inflate(m_inflate, Z_FINISH);
86 if (err == Z_STREAM_END)
87 return (size - m_inflate->avail_in);
88 }
89
90 return size-m_inflate->avail_in;
91 }
92
93 //////////////////////
94 // wxZlibOutputStream
95 //////////////////////
96
97 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream)
98 : wxFilterOutputStream(stream)
99 {
100 int err;
101
102 m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write);
103 m_o_destroybuf = TRUE;
104 m_deflate = new z_stream_s;
105
106 m_deflate->zalloc = (alloc_func)0;
107 m_deflate->zfree = (free_func)0;
108 m_deflate->opaque = (voidpf)0;
109
110 err = deflateInit(m_deflate, Z_DEFAULT_COMPRESSION);
111 if (err != Z_OK) {
112 deflateEnd(m_deflate);
113 return;
114 }
115
116 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
117 m_z_size = ZSTREAM_BUFFER_SIZE;
118
119 m_deflate->avail_in = 0;
120 m_deflate->next_out = m_z_buffer;
121 m_deflate->avail_out = m_z_size;
122 }
123
124 wxZlibOutputStream::~wxZlibOutputStream()
125 {
126 int err;
127
128 Sync();
129
130 err = deflate(m_deflate, Z_FINISH);
131 if (err != Z_STREAM_END)
132 {
133 wxLogDebug( "wxZlibOutputStream: an error occured while closing the stream.\n" );
134 return;
135 }
136
137 deflateEnd(m_deflate);
138
139 delete[] m_z_buffer;
140 }
141
142 void wxZlibOutputStream::Sync()
143 {
144 int err;
145
146 m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out);
147 m_deflate->next_out = m_z_buffer;
148 m_deflate->avail_out = m_z_size;
149
150 err = deflate(m_deflate, Z_FULL_FLUSH);
151 if (err != Z_OK) {
152 return;
153 }
154
155 m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out);
156 m_deflate->next_out = m_z_buffer;
157 m_deflate->avail_out = m_z_size;
158 }
159
160 size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size)
161 {
162 int err;
163
164 m_deflate->next_in = (unsigned char *)buffer;
165 m_deflate->avail_in = size;
166
167 while (m_deflate->avail_in > 0) {
168
169 if (m_deflate->avail_out == 0) {
170 m_parent_o_stream->Write(m_z_buffer, m_z_size);
171 if (m_parent_o_stream->LastError() != wxStream_NOERROR)
172 return (size - m_deflate->avail_in);
173
174 m_deflate->next_out = m_z_buffer;
175 m_deflate->avail_out = m_z_size;
176 }
177
178 err = deflate(m_deflate, Z_NO_FLUSH);
179 if (err != Z_OK)
180 return (size - m_deflate->avail_in);
181 }
182 return size;
183 }