]> git.saurik.com Git - wxWidgets.git/blame - src/common/zstream.cpp
IsModified() function now works correctly
[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/////////////////////////////////////////////////////////////////////////////
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>
79c3e0e1
GL
20
21#ifdef __BORLANDC__
22#pragma hdrstop
23#endif
24
79c3e0e1
GL
25//////////////////////
26// wxZlibInputStream
27//////////////////////
28
29wxZlibInputStream::wxZlibInputStream(wxInputStream& stream)
30 : wxFilterInputStream(stream)
31{
32 int err;
33
34 m_inflate.zalloc = (alloc_func)0;
35 m_inflate.zfree = (free_func)0;
36 m_inflate.opaque = (voidpf)0;
37
38 err = inflateInit(&m_inflate);
39 if (err != Z_OK) {
40 inflateEnd(&m_inflate);
41 return;
42 }
43
44 m_inflate.avail_in = 0;
45}
46
47wxZlibInputStream::~wxZlibInputStream()
48{
49 inflateEnd(&m_inflate);
50}
51
52wxInputStream& wxZlibInputStream::Read(void *buffer, size_t size)
53{
54 int err;
55
56 m_inflate.next_out = (unsigned char *)buffer;
57 m_inflate.avail_out = size;
58 m_eof = FALSE;
59
60 while (m_inflate.avail_out > 0) {
61 if (m_inflate.avail_in == 0) {
62 wxFilterInputStream::Read(m_z_buffer, m_z_size);
63 m_inflate.next_in = m_z_buffer;
64 m_inflate.avail_in = wxFilterInputStream::LastRead();
65 if (wxFilterInputStream::Eof()) {
66 m_lastread = size - m_inflate.avail_out;
67 return *this;
68 }
69 }
70 err = inflate(&m_inflate, Z_FINISH);
71 if (err == Z_STREAM_END) {
72 m_lastread = size - m_inflate.avail_out;
73 m_eof = TRUE;
74 return *this;
75 }
76 }
77
78 m_lastread = size;
79 return *this;
80}
81
82off_t wxZlibInputStream::SeekI(off_t WXUNUSED(pos), wxSeekMode WXUNUSED(mode))
83{
84 return 0;
85}
86
87off_t wxZlibInputStream::TellI() const
88{
89 return 0;
90}
91
92bool wxZlibInputStream::Eof() const
93{
94 if (!m_eof)
95 return wxFilterInputStream::Eof();
96 return m_eof;
97}
98
99//////////////////////
100// wxZlibOutputStream
101//////////////////////
102
103wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream)
104 : wxFilterOutputStream(stream)
105{
106 int err;
107
108 m_deflate.zalloc = (alloc_func)0;
109 m_deflate.zfree = (free_func)0;
110 m_deflate.opaque = (voidpf)0;
111
112 err = deflateInit(&m_deflate, Z_DEFAULT_COMPRESSION);
113 if (err != Z_OK) {
114 deflateEnd(&m_deflate);
115 return;
116 }
117 m_deflate.avail_in = 0;
118 m_deflate.next_out = m_z_buffer;
119 m_deflate.avail_out = m_z_size;
120}
121
122wxZlibOutputStream::~wxZlibOutputStream()
123{
124 int err;
125
126 while (1) {
127 err = deflate(&m_deflate, Z_FINISH);
128 if (err == Z_STREAM_END)
129 break;
130 if (err < 0) {
131 wxDebugMsg("wxZlibOutputStream: error during final deflate");
132 break;
133 }
134 if (m_deflate.avail_out == 0) {
135 wxFilterOutputStream::Write(m_z_buffer, m_z_size);
136 if (wxFilterOutputStream::Bad()) {
137 wxDebugMsg("wxZlibOutputStream: error during final write");
138 break;
139 }
140 m_deflate.next_out = m_z_buffer;
141 m_deflate.avail_out = m_z_size;
142 }
143 }
144 wxFilterOutputStream::Write(m_z_buffer, m_z_size-m_deflate.avail_out);
145
146 deflateEnd(&m_deflate);
147}
148
149wxOutputStream& wxZlibOutputStream::Write(const void *buffer, size_t size)
150{
151 int err;
152
153 m_deflate.next_in = (unsigned char *)buffer;
154 m_deflate.avail_in = size;
155
156 m_bad = FALSE;
157 while (m_deflate.avail_in > 0) {
158 if (m_deflate.avail_out == 0) {
159 wxFilterOutputStream::Write(m_z_buffer, m_z_size);
160 if (wxFilterOutputStream::Bad()) {
161 m_lastwrite = size - m_deflate.avail_in;
162 return *this;
163 }
164
165 m_deflate.next_out = m_z_buffer;
166 m_deflate.avail_out = m_z_size;
167 }
168 err = deflate(&m_deflate, Z_NO_FLUSH);
169 if (err < 0) {
170 m_bad = TRUE;
171 m_lastwrite = size - m_deflate.avail_in;
172 return *this;
173 }
174 }
175 m_lastwrite = size;
176 return *this;
177}
178
179off_t wxZlibOutputStream::SeekO(off_t WXUNUSED(pos), wxSeekMode WXUNUSED(mode))
180{
181 return 0;
182}
183
184off_t wxZlibOutputStream::TellO() const
185{
186 return 0;
187}
188
189bool wxZlibOutputStream::Bad() const
190{
191 if (!m_bad)
192 return wxFilterOutputStream::Bad();
193 return m_bad;
194}