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