]>
git.saurik.com Git - wxWidgets.git/blob - src/common/zstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Compressed stream classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "zstream.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17 #include <wx/stream.h>
18 #include <wx/zstream.h>
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_CLASS(wxZlibInputStream
, wxFilterInputStream
)
28 IMPLEMENT_CLASS(wxZlibOutputStream
, wxFilterOutputStream
)
31 //////////////////////
33 //////////////////////
35 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
)
36 : wxFilterInputStream(stream
)
40 m_inflate
.zalloc
= (alloc_func
)0;
41 m_inflate
.zfree
= (free_func
)0;
42 m_inflate
.opaque
= (voidpf
)0;
44 err
= inflateInit(&m_inflate
);
46 inflateEnd(&m_inflate
);
50 m_inflate
.avail_in
= 0;
53 wxZlibInputStream::~wxZlibInputStream()
55 inflateEnd(&m_inflate
);
58 wxInputStream
& wxZlibInputStream::Read(void *buffer
, size_t size
)
62 m_inflate
.next_out
= (unsigned char *)buffer
;
63 m_inflate
.avail_out
= size
;
66 while (m_inflate
.avail_out
> 0) {
67 if (m_inflate
.avail_in
== 0) {
68 wxFilterInputStream::Read(m_z_buffer
, m_z_size
);
69 m_inflate
.next_in
= m_z_buffer
;
70 m_inflate
.avail_in
= wxFilterInputStream::LastRead();
71 if (wxFilterInputStream::Eof()) {
72 m_lastread
= size
- m_inflate
.avail_out
;
76 err
= inflate(&m_inflate
, Z_FINISH
);
77 if (err
== Z_STREAM_END
) {
78 m_lastread
= size
- m_inflate
.avail_out
;
88 off_t
wxZlibInputStream::SeekI(off_t
WXUNUSED(pos
), wxSeekMode
WXUNUSED(mode
))
93 off_t
wxZlibInputStream::TellI() const
98 bool wxZlibInputStream::Eof() const
101 return wxFilterInputStream::Eof();
105 //////////////////////
106 // wxZlibOutputStream
107 //////////////////////
109 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
)
110 : wxFilterOutputStream(stream
)
114 m_deflate
.zalloc
= (alloc_func
)0;
115 m_deflate
.zfree
= (free_func
)0;
116 m_deflate
.opaque
= (voidpf
)0;
118 err
= deflateInit(&m_deflate
, Z_DEFAULT_COMPRESSION
);
120 deflateEnd(&m_deflate
);
123 m_deflate
.avail_in
= 0;
124 m_deflate
.next_out
= m_z_buffer
;
125 m_deflate
.avail_out
= m_z_size
;
128 wxZlibOutputStream::~wxZlibOutputStream()
133 err
= deflate(&m_deflate
, Z_FINISH
);
134 if (err
== Z_STREAM_END
)
137 wxDebugMsg("wxZlibOutputStream: error during final deflate");
140 if (m_deflate
.avail_out
== 0) {
141 wxFilterOutputStream::Write(m_z_buffer
, m_z_size
);
142 if (wxFilterOutputStream::Bad()) {
143 wxDebugMsg("wxZlibOutputStream: error during final write");
146 m_deflate
.next_out
= m_z_buffer
;
147 m_deflate
.avail_out
= m_z_size
;
150 wxFilterOutputStream::Write(m_z_buffer
, m_z_size
-m_deflate
.avail_out
);
152 deflateEnd(&m_deflate
);
155 wxOutputStream
& wxZlibOutputStream::Write(const void *buffer
, size_t size
)
159 m_deflate
.next_in
= (unsigned char *)buffer
;
160 m_deflate
.avail_in
= size
;
163 while (m_deflate
.avail_in
> 0) {
164 if (m_deflate
.avail_out
== 0) {
165 wxFilterOutputStream::Write(m_z_buffer
, m_z_size
);
166 if (wxFilterOutputStream::Bad()) {
167 m_lastwrite
= size
- m_deflate
.avail_in
;
171 m_deflate
.next_out
= m_z_buffer
;
172 m_deflate
.avail_out
= m_z_size
;
174 err
= deflate(&m_deflate
, Z_NO_FLUSH
);
177 m_lastwrite
= size
- m_deflate
.avail_in
;
185 off_t
wxZlibOutputStream::SeekO(off_t
WXUNUSED(pos
), wxSeekMode
WXUNUSED(mode
))
190 off_t
wxZlibOutputStream::TellO() const
195 bool wxZlibOutputStream::Bad() const
198 return wxFilterOutputStream::Bad();