]> git.saurik.com Git - wxWidgets.git/blame - src/common/zstream.cpp
msg boxes now have icons
[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
23#ifndef WX_PRECOMP
24 #include "wx/defs.h"
25#endif
ac57418f 26
ce4169a4 27#if wxUSE_ZLIB && wxUSE_STREAMS
ac57418f 28
ce4169a4 29#include "wx/zstream.h"
ac57418f
RR
30#include "wx/utils.h"
31#include "wx/intl.h"
3069ac4e 32#include "wx/log.h"
ce4169a4 33#include "zlib.h"
79c3e0e1 34
6d44bf31
GL
35#define ZSTREAM_BUFFER_SIZE 1024
36
79c3e0e1
GL
37//////////////////////
38// wxZlibInputStream
39//////////////////////
40
41wxZlibInputStream::wxZlibInputStream(wxInputStream& stream)
42 : wxFilterInputStream(stream)
43{
44 int err;
45
6d44bf31 46 // I need a private stream buffer.
75ed1d15 47 m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read);
6d44bf31 48 m_i_destroybuf = TRUE;
856d2e52 49 m_inflate = new z_stream_s;
6d44bf31 50
856d2e52
GL
51 m_inflate->zalloc = (alloc_func)0;
52 m_inflate->zfree = (free_func)0;
53 m_inflate->opaque = (voidpf)0;
79c3e0e1 54
856d2e52 55 err = inflateInit(m_inflate);
79c3e0e1 56 if (err != Z_OK) {
856d2e52
GL
57 inflateEnd(m_inflate);
58 delete m_inflate;
79c3e0e1
GL
59 return;
60 }
61
6d44bf31
GL
62 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
63 m_z_size = ZSTREAM_BUFFER_SIZE;
64
856d2e52
GL
65 m_inflate->avail_in = 0;
66 m_inflate->next_in = NULL;
79c3e0e1
GL
67}
68
69wxZlibInputStream::~wxZlibInputStream()
70{
856d2e52
GL
71 inflateEnd(m_inflate);
72 delete m_inflate;
79c3e0e1
GL
73}
74
75ed1d15 75size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size)
79c3e0e1
GL
76{
77 int err;
78
856d2e52
GL
79 m_inflate->next_out = (unsigned char *)buffer;
80 m_inflate->avail_out = size;
79c3e0e1 81
856d2e52
GL
82 while (m_inflate->avail_out > 0) {
83 if (m_inflate->avail_in == 0) {
6d44bf31
GL
84
85 m_parent_i_stream->Read(m_z_buffer, m_z_size);
856d2e52
GL
86 m_inflate->next_in = m_z_buffer;
87 m_inflate->avail_in = m_parent_i_stream->LastRead();
6d44bf31 88
75ed1d15 89 if (m_parent_i_stream->LastError() != wxStream_NOERROR)
856d2e52 90 return (size - m_inflate->avail_in);
79c3e0e1 91 }
856d2e52 92 err = inflate(m_inflate, Z_FINISH);
6d44bf31 93 if (err == Z_STREAM_END)
856d2e52 94 return (size - m_inflate->avail_in);
79c3e0e1
GL
95 }
96
856d2e52 97 return size-m_inflate->avail_in;
79c3e0e1
GL
98}
99
79c3e0e1
GL
100//////////////////////
101// wxZlibOutputStream
102//////////////////////
103
104wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream)
105 : wxFilterOutputStream(stream)
106{
107 int err;
108
75ed1d15 109 m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write);
6d44bf31 110 m_o_destroybuf = TRUE;
856d2e52 111 m_deflate = new z_stream_s;
6d44bf31 112
856d2e52
GL
113 m_deflate->zalloc = (alloc_func)0;
114 m_deflate->zfree = (free_func)0;
115 m_deflate->opaque = (voidpf)0;
79c3e0e1 116
856d2e52 117 err = deflateInit(m_deflate, Z_DEFAULT_COMPRESSION);
79c3e0e1 118 if (err != Z_OK) {
856d2e52 119 deflateEnd(m_deflate);
79c3e0e1
GL
120 return;
121 }
6d44bf31
GL
122
123 m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE];
124 m_z_size = ZSTREAM_BUFFER_SIZE;
125
856d2e52
GL
126 m_deflate->avail_in = 0;
127 m_deflate->next_out = m_z_buffer;
128 m_deflate->avail_out = m_z_size;
79c3e0e1
GL
129}
130
131wxZlibOutputStream::~wxZlibOutputStream()
132{
133 int err;
134
6d44bf31
GL
135 Sync();
136
856d2e52 137 err = deflate(m_deflate, Z_FINISH);
3069ac4e
RR
138 if (err != Z_STREAM_END)
139 {
84fff0b3 140 wxLogDebug( _T("wxZlibOutputStream: an error occured while closing the stream.\n") );
6d44bf31 141 return;
79c3e0e1 142 }
79c3e0e1 143
856d2e52 144 deflateEnd(m_deflate);
6d44bf31
GL
145
146 delete[] m_z_buffer;
79c3e0e1
GL
147}
148
6d44bf31
GL
149void wxZlibOutputStream::Sync()
150{
151 int err;
152
856d2e52
GL
153 m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out);
154 m_deflate->next_out = m_z_buffer;
155 m_deflate->avail_out = m_z_size;
6d44bf31 156
856d2e52 157 err = deflate(m_deflate, Z_FULL_FLUSH);
6d44bf31 158 if (err != Z_OK) {
6d44bf31
GL
159 return;
160 }
161
856d2e52
GL
162 m_parent_o_stream->Write(m_z_buffer, m_z_size-m_deflate->avail_out);
163 m_deflate->next_out = m_z_buffer;
164 m_deflate->avail_out = m_z_size;
6d44bf31
GL
165}
166
75ed1d15 167size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size)
79c3e0e1
GL
168{
169 int err;
170
856d2e52
GL
171 m_deflate->next_in = (unsigned char *)buffer;
172 m_deflate->avail_in = size;
79c3e0e1 173
856d2e52 174 while (m_deflate->avail_in > 0) {
6d44bf31 175
856d2e52 176 if (m_deflate->avail_out == 0) {
6d44bf31 177 m_parent_o_stream->Write(m_z_buffer, m_z_size);
75ed1d15 178 if (m_parent_o_stream->LastError() != wxStream_NOERROR)
856d2e52 179 return (size - m_deflate->avail_in);
6d44bf31 180
856d2e52
GL
181 m_deflate->next_out = m_z_buffer;
182 m_deflate->avail_out = m_z_size;
79c3e0e1 183 }
6d44bf31 184
856d2e52 185 err = deflate(m_deflate, Z_NO_FLUSH);
6d44bf31 186 if (err != Z_OK)
856d2e52 187 return (size - m_deflate->avail_in);
79c3e0e1 188 }
6d44bf31 189 return size;
79c3e0e1 190}
ac57418f
RR
191
192#endif
ce4169a4 193 // wxUSE_ZLIB && wxUSE_STREAMS
ac57418f 194