]>
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 //////////////////////
28 //////////////////////
30 wxZlibInputStream::wxZlibInputStream(wxInputStream
& stream
)
31 : wxFilterInputStream(stream
)
35 m_inflate
.zalloc
= (alloc_func
)0;
36 m_inflate
.zfree
= (free_func
)0;
37 m_inflate
.opaque
= (voidpf
)0;
39 err
= inflateInit(&m_inflate
);
41 inflateEnd(&m_inflate
);
45 m_inflate
.avail_in
= 0;
48 wxZlibInputStream::~wxZlibInputStream()
50 inflateEnd(&m_inflate
);
53 wxInputStream
& wxZlibInputStream::Read(void *buffer
, size_t size
)
57 m_inflate
.next_out
= (unsigned char *)buffer
;
58 m_inflate
.avail_out
= size
;
61 while (m_inflate
.avail_out
> 0) {
62 if (m_inflate
.avail_in
== 0) {
63 wxFilterInputStream::Read(m_z_buffer
, m_z_size
);
64 m_inflate
.next_in
= m_z_buffer
;
65 m_inflate
.avail_in
= wxFilterInputStream::LastRead();
66 if (wxFilterInputStream::Eof()) {
67 m_lastread
= size
- m_inflate
.avail_out
;
71 err
= inflate(&m_inflate
, Z_FINISH
);
72 if (err
== Z_STREAM_END
) {
73 m_lastread
= size
- m_inflate
.avail_out
;
83 off_t
wxZlibInputStream::SeekI(off_t
WXUNUSED(pos
), wxSeekMode
WXUNUSED(mode
))
88 off_t
wxZlibInputStream::TellI() const
93 bool wxZlibInputStream::Eof() const
96 return wxFilterInputStream::Eof();
100 //////////////////////
101 // wxZlibOutputStream
102 //////////////////////
104 wxZlibOutputStream::wxZlibOutputStream(wxOutputStream
& stream
)
105 : wxFilterOutputStream(stream
)
109 m_deflate
.zalloc
= (alloc_func
)0;
110 m_deflate
.zfree
= (free_func
)0;
111 m_deflate
.opaque
= (voidpf
)0;
113 err
= deflateInit(&m_deflate
, Z_DEFAULT_COMPRESSION
);
115 deflateEnd(&m_deflate
);
118 m_deflate
.avail_in
= 0;
119 m_deflate
.next_out
= m_z_buffer
;
120 m_deflate
.avail_out
= m_z_size
;
123 wxZlibOutputStream::~wxZlibOutputStream()
128 err
= deflate(&m_deflate
, Z_FINISH
);
129 if (err
== Z_STREAM_END
)
132 wxDebugMsg("wxZlibOutputStream: error during final deflate");
135 if (m_deflate
.avail_out
== 0) {
136 wxFilterOutputStream::Write(m_z_buffer
, m_z_size
);
137 if (wxFilterOutputStream::Bad()) {
138 wxDebugMsg("wxZlibOutputStream: error during final write");
141 m_deflate
.next_out
= m_z_buffer
;
142 m_deflate
.avail_out
= m_z_size
;
145 wxFilterOutputStream::Write(m_z_buffer
, m_z_size
-m_deflate
.avail_out
);
147 deflateEnd(&m_deflate
);
150 wxOutputStream
& wxZlibOutputStream::Write(const void *buffer
, size_t size
)
154 m_deflate
.next_in
= (unsigned char *)buffer
;
155 m_deflate
.avail_in
= size
;
158 while (m_deflate
.avail_in
> 0) {
159 if (m_deflate
.avail_out
== 0) {
160 wxFilterOutputStream::Write(m_z_buffer
, m_z_size
);
161 if (wxFilterOutputStream::Bad()) {
162 m_lastwrite
= size
- m_deflate
.avail_in
;
166 m_deflate
.next_out
= m_z_buffer
;
167 m_deflate
.avail_out
= m_z_size
;
169 err
= deflate(&m_deflate
, Z_NO_FLUSH
);
172 m_lastwrite
= size
- m_deflate
.avail_in
;
180 off_t
wxZlibOutputStream::SeekO(off_t
WXUNUSED(pos
), wxSeekMode
WXUNUSED(mode
))
185 off_t
wxZlibOutputStream::TellO() const
190 bool wxZlibOutputStream::Bad() const
193 return wxFilterOutputStream::Bad();