1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
// Description:
// A compressed (file) input/output stream.
//
// Copyright (C) 2005 Frank Becker
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
//
#include <cstdlib>
#include <Trace.hpp>
#include <Endian.hpp>
#include <zStream.hpp>
#include <zStreamBufferImplLZMA.hpp>
#include <zStreamBufferImplZLib.hpp>
ofstream &operator<<( ofstream &outfile, Uint32 i)
{
char *pi = (char*)&i;
if( ! ::isLittleEndian()) swap( pi, 4);
outfile.write( pi, 4);
return outfile;
}
ifstream &operator>>( ifstream &infile, Uint32 &i)
{
char *pi = (char*)&i;
infile.read( pi, 4);
if( ! ::isLittleEndian()) swap( pi, 4);
return infile;
}
//-----------------------------------------------------------------------------
ziStreamBuffer::ziStreamBuffer( ifstream &fd):
_fstream( fd),
_isOK(false),
_impl(0)
{
XTRACE();
_init();
}
void ziStreamBuffer::_init( void)
{
XTRACE();
if( !_fstream.is_open())
{
LOG_ERROR << "Input stream not open!" << endl;
}
// xsetflags( ios::binary);
Uint32 magic;
_fstream >> magic;
// LOG_INFO << "Magic 0x" << hex << magic << dec << endl;
if( magic == ZSTREAM_MAGIC)
{
_impl = new ziStreamBufferImplZLib( _fstream);
}
else if( magic == LZMASTREAM_MAGIC)
{
#ifdef USE_LZMA
_impl = new ziStreamBufferImplLZMA( _fstream);
#else
LOG_ERROR << "LZMA not supported\n";
exit(-1);
#endif
}
else
{
_fstream.seekg( -4, ios::cur);
_impl = new ziStreamBufferImpl( _fstream);
}
_isOK = _impl->init();
}
ziStreamBuffer::~ziStreamBuffer()
{
XTRACE();
delete _impl;
}
streamsize ziStreamBuffer::xsgetn(char* s, streamsize n)
{
XTRACE();
if( _impl->isEOF()) return EOF;
streamsize numBytes = _impl->read( s, n);
return numBytes;
}
//-----------------------------------------------------------------------------
zoStreamBuffer::zoStreamBuffer( ofstream &fd, CompressionType compressionType):
_fstream( fd),
_isOK(false),
_impl(0)
{
XTRACE();
switch( compressionType)
{
case eZLibCompression:
_impl = new zoStreamBufferImplZLib( fd);
break;
case eLZMACompression:
#ifdef USE_LZMA
_impl = new zoStreamBufferImplLZMA( fd);
#else
LOG_ERROR << "LZMA not supported\n";
#endif
break;
case eNoCompression:
default:
_impl = new zoStreamBufferImpl( fd);
break;
}
_init();
}
void zoStreamBuffer::_init( void)
{
XTRACE();
if( !_fstream.is_open())
{
LOG_ERROR << "Output stream not open!" << endl;
}
// xsetflags( ios::binary);
_isOK = _impl->init();
}
zoStreamBuffer::~zoStreamBuffer()
{
XTRACE();
delete _impl;
}
streamsize zoStreamBuffer::xsputn(const char* s, streamsize n)
{
// XTRACE();
streamsize numBytes = _impl->write( s, n);
return numBytes;
}
//-----------------------------------------------------------------------------
//Note: This rather stupid looking *new + ref for _streambuf is to avoid
//gcc asking for -fhuge-objects when cross compiling to windows.
ziStream::ziStream( ifstream &inf):
super(0),
_streambuf( * new ziStreamBuffer( inf))
{
XTRACE();
init( &_streambuf);
}
ziStream::~ziStream()
{
XTRACE();
delete &_streambuf;
}
//-----------------------------------------------------------------------------
zoStream::zoStream( ofstream &outf, CompressionType compressionType):
super(0),
_streambuf( * new zoStreamBuffer( outf, compressionType))
{
XTRACE();
init( &_streambuf);
}
zoStream::~zoStream()
{
XTRACE();
delete &_streambuf;
}
//-----------------------------------------------------------------------------
|