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