You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
3.6 KiB
164 lines
3.6 KiB
15 years ago
|
/* This file is part of the KDE project
|
||
|
Copyright (C) 2001,2005 Nicolas GOUTTE <nicog@snafu.de>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library 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
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
// TODO: more error report and control
|
||
|
|
||
15 years ago
|
#include <tqstring.h>
|
||
15 years ago
|
#include "kqiodevicegzip_p.h"
|
||
|
|
||
15 years ago
|
KQIODeviceGZip::KQIODeviceGZip(const TQString& filename)
|
||
15 years ago
|
{
|
||
|
m_gzfile=0;
|
||
|
m_ungetchar=-1;
|
||
|
m_filename=filename;
|
||
|
setFlags(IO_Sequential); // We have no direct access, so it is sequential!
|
||
|
// NOTE: "sequential" also means that you cannot use size()
|
||
|
}
|
||
|
|
||
|
KQIODeviceGZip::~KQIODeviceGZip(void)
|
||
|
{
|
||
|
if (m_gzfile)
|
||
|
close();
|
||
|
}
|
||
|
|
||
14 years ago
|
bool KQIODeviceGZip::open(TQ_OpenMode mode)
|
||
15 years ago
|
{
|
||
|
if (m_gzfile)
|
||
|
close(); // One file is already open, so close it first.
|
||
|
if (m_filename.isEmpty())
|
||
|
return false; // No file name, cannot open!
|
||
|
|
||
|
if (IO_ReadOnly==mode)
|
||
|
{
|
||
15 years ago
|
m_gzfile=gzopen(TQFile::encodeName(m_filename),"rb");
|
||
15 years ago
|
}
|
||
|
else if (IO_WriteOnly==mode)
|
||
|
{
|
||
15 years ago
|
m_gzfile=gzopen(TQFile::encodeName(m_filename),"wb9"); // Always set best compression
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
// We only support read only or write only, nothing else!
|
||
|
return false;
|
||
|
}
|
||
|
return (m_gzfile!=0);
|
||
|
}
|
||
|
|
||
|
void KQIODeviceGZip::close(void)
|
||
|
{
|
||
|
if (m_gzfile)
|
||
|
{
|
||
|
gzclose(m_gzfile);
|
||
|
m_gzfile=0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KQIODeviceGZip::flush(void)
|
||
|
{
|
||
|
// Always try to flush, do not return any error!
|
||
|
if (m_gzfile)
|
||
|
{
|
||
|
gzflush(m_gzfile,Z_SYNC_FLUSH);
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
#ifdef USE_QT4
|
||
|
qint64 KQIODeviceGZip::size(void) const
|
||
|
#else // USE_QT4
|
||
15 years ago
|
TQIODevice::Offset KQIODeviceGZip::size(void) const
|
||
14 years ago
|
#endif // USE_QT4
|
||
15 years ago
|
{
|
||
|
return 0; // You cannot determine size!
|
||
|
}
|
||
|
|
||
15 years ago
|
TQIODevice::Offset KQIODeviceGZip::at() const
|
||
15 years ago
|
{
|
||
|
if (!m_gzfile)
|
||
|
return 0;
|
||
|
return gztell(m_gzfile);
|
||
|
}
|
||
|
|
||
15 years ago
|
bool KQIODeviceGZip::at(TQIODevice::Offset pos)
|
||
15 years ago
|
{
|
||
|
if (!m_gzfile)
|
||
|
return false;
|
||
|
return (gzseek(m_gzfile,pos,SEEK_SET)>=0);
|
||
|
}
|
||
|
|
||
|
bool KQIODeviceGZip::atEnd() const
|
||
|
{
|
||
|
if (!m_gzfile)
|
||
|
return true;
|
||
|
return gzeof(m_gzfile);
|
||
|
}
|
||
|
|
||
|
bool KQIODeviceGZip::reset(void)
|
||
|
{
|
||
|
if (!m_gzfile)
|
||
|
return false; //Say we arew at end of file
|
||
|
return (gzrewind(m_gzfile)>=0);
|
||
|
}
|
||
|
|
||
14 years ago
|
TQT_TQIO_LONG KQIODeviceGZip::tqreadBlock( char *data, TQT_TQIO_ULONG maxlen )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQ_LONG result=0;
|
||
15 years ago
|
if (m_gzfile)
|
||
|
{
|
||
|
result=gzread(m_gzfile,data,maxlen);
|
||
|
if (result<0) result=0;
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQT_TQIO_LONG KQIODeviceGZip::tqwriteBlock( const char *data, TQT_TQIO_ULONG len )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQ_ULONG result=0;
|
||
15 years ago
|
if (m_gzfile)
|
||
|
{
|
||
|
result=gzwrite(m_gzfile,(char*)data,len);
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
int KQIODeviceGZip::getch()
|
||
|
{
|
||
|
if (m_ungetchar>0)
|
||
|
{
|
||
|
const int ch=m_ungetchar;
|
||
|
m_ungetchar=-1;
|
||
|
return ch;
|
||
|
}
|
||
|
if (!m_gzfile)
|
||
|
return -1;
|
||
|
return gzgetc(m_gzfile);
|
||
|
}
|
||
|
|
||
|
int KQIODeviceGZip::putch( int ch)
|
||
|
{
|
||
|
if (!m_gzfile)
|
||
|
return -1;
|
||
|
return gzputc(m_gzfile,ch);
|
||
|
}
|
||
|
|
||
|
int KQIODeviceGZip::ungetch(int ch)
|
||
|
{
|
||
|
m_ungetchar=ch;
|
||
|
return ch;
|
||
|
}
|