kqiodevicegzip_p.cpp
00001 /* This file is part of the KDE project 00002 Copyright (C) 2001,2005 Nicolas GOUTTE <nicog@snafu.de> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 // TODO: more error report and control 00021 00022 #include <qstring.h> 00023 #include "kqiodevicegzip_p.h" 00024 00025 KQIODeviceGZip::KQIODeviceGZip(const QString& filename) 00026 { 00027 m_gzfile=0; 00028 m_ungetchar=-1; 00029 m_filename=filename; 00030 setFlags(IO_Sequential); // We have no direct access, so it is sequential! 00031 // NOTE: "sequential" also means that you cannot use size() 00032 } 00033 00034 KQIODeviceGZip::~KQIODeviceGZip(void) 00035 { 00036 if (m_gzfile) 00037 close(); 00038 } 00039 00040 bool KQIODeviceGZip::open(int mode) 00041 { 00042 if (m_gzfile) 00043 close(); // One file is already open, so close it first. 00044 if (m_filename.isEmpty()) 00045 return false; // No file name, cannot open! 00046 00047 if (IO_ReadOnly==mode) 00048 { 00049 m_gzfile=gzopen(QFile::encodeName(m_filename),"rb"); 00050 } 00051 else if (IO_WriteOnly==mode) 00052 { 00053 m_gzfile=gzopen(QFile::encodeName(m_filename),"wb9"); // Always set best compression 00054 } 00055 else 00056 { 00057 // We only support read only or write only, nothing else! 00058 return false; 00059 } 00060 return (m_gzfile!=0); 00061 } 00062 00063 void KQIODeviceGZip::close(void) 00064 { 00065 if (m_gzfile) 00066 { 00067 gzclose(m_gzfile); 00068 m_gzfile=0; 00069 } 00070 } 00071 00072 void KQIODeviceGZip::flush(void) 00073 { 00074 // Always try to flush, do not return any error! 00075 if (m_gzfile) 00076 { 00077 gzflush(m_gzfile,Z_SYNC_FLUSH); 00078 } 00079 } 00080 00081 QIODevice::Offset KQIODeviceGZip::size(void) const 00082 { 00083 return 0; // You cannot determine size! 00084 } 00085 00086 QIODevice::Offset KQIODeviceGZip::at() const 00087 { 00088 if (!m_gzfile) 00089 return 0; 00090 return gztell(m_gzfile); 00091 } 00092 00093 bool KQIODeviceGZip::at(QIODevice::Offset pos) 00094 { 00095 if (!m_gzfile) 00096 return false; 00097 return (gzseek(m_gzfile,pos,SEEK_SET)>=0); 00098 } 00099 00100 bool KQIODeviceGZip::atEnd() const 00101 { 00102 if (!m_gzfile) 00103 return true; 00104 return gzeof(m_gzfile); 00105 } 00106 00107 bool KQIODeviceGZip::reset(void) 00108 { 00109 if (!m_gzfile) 00110 return false; //Say we arew at end of file 00111 return (gzrewind(m_gzfile)>=0); 00112 } 00113 00114 Q_LONG KQIODeviceGZip::readBlock( char *data, Q_ULONG maxlen ) 00115 { 00116 Q_LONG result=0; 00117 if (m_gzfile) 00118 { 00119 result=gzread(m_gzfile,data,maxlen); 00120 if (result<0) result=0; 00121 } 00122 return result; 00123 } 00124 00125 Q_LONG KQIODeviceGZip::writeBlock( const char *data, Q_ULONG len ) 00126 { 00127 Q_ULONG result=0; 00128 if (m_gzfile) 00129 { 00130 result=gzwrite(m_gzfile,(char*)data,len); 00131 } 00132 return result; 00133 } 00134 00135 int KQIODeviceGZip::getch() 00136 { 00137 if (m_ungetchar>0) 00138 { 00139 const int ch=m_ungetchar; 00140 m_ungetchar=-1; 00141 return ch; 00142 } 00143 if (!m_gzfile) 00144 return -1; 00145 return gzgetc(m_gzfile); 00146 } 00147 00148 int KQIODeviceGZip::putch( int ch) 00149 { 00150 if (!m_gzfile) 00151 return -1; 00152 return gzputc(m_gzfile,ch); 00153 } 00154 00155 int KQIODeviceGZip::ungetch(int ch) 00156 { 00157 m_ungetchar=ch; 00158 return ch; 00159 }