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