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 <tqstring.h> 00023 #include "kqiodevicegzip_p.h" 00024 00025 KQIODeviceGZip::KQIODeviceGZip(const TQString& 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(TQ_OpenMode 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(TQFile::encodeName(m_filename),"rb"); 00050 } 00051 else if (IO_WriteOnly==mode) 00052 { 00053 m_gzfile=gzopen(TQFile::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 #ifdef USE_QT4 00082 qint64 KQIODeviceGZip::size(void) const 00083 #else // USE_QT4 00084 TQIODevice::Offset KQIODeviceGZip::size(void) const 00085 #endif // USE_QT4 00086 { 00087 return 0; // You cannot determine size! 00088 } 00089 00090 TQIODevice::Offset KQIODeviceGZip::at() const 00091 { 00092 if (!m_gzfile) 00093 return 0; 00094 return gztell(m_gzfile); 00095 } 00096 00097 bool KQIODeviceGZip::at(TQIODevice::Offset pos) 00098 { 00099 if (!m_gzfile) 00100 return false; 00101 return (gzseek(m_gzfile,pos,SEEK_SET)>=0); 00102 } 00103 00104 bool KQIODeviceGZip::atEnd() const 00105 { 00106 if (!m_gzfile) 00107 return true; 00108 return gzeof(m_gzfile); 00109 } 00110 00111 bool KQIODeviceGZip::reset(void) 00112 { 00113 if (!m_gzfile) 00114 return false; //Say we arew at end of file 00115 return (gzrewind(m_gzfile)>=0); 00116 } 00117 00118 TQT_TQIO_LONG KQIODeviceGZip::tqreadBlock( char *data, TQT_TQIO_ULONG maxlen ) 00119 { 00120 TQ_LONG result=0; 00121 if (m_gzfile) 00122 { 00123 result=gzread(m_gzfile,data,maxlen); 00124 if (result<0) result=0; 00125 } 00126 return result; 00127 } 00128 00129 TQT_TQIO_LONG KQIODeviceGZip::tqwriteBlock( const char *data, TQT_TQIO_ULONG len ) 00130 { 00131 TQ_ULONG result=0; 00132 if (m_gzfile) 00133 { 00134 result=gzwrite(m_gzfile,(char*)data,len); 00135 } 00136 return result; 00137 } 00138 00139 int KQIODeviceGZip::getch() 00140 { 00141 if (m_ungetchar>0) 00142 { 00143 const int ch=m_ungetchar; 00144 m_ungetchar=-1; 00145 return ch; 00146 } 00147 if (!m_gzfile) 00148 return -1; 00149 return gzgetc(m_gzfile); 00150 } 00151 00152 int KQIODeviceGZip::putch( int ch) 00153 { 00154 if (!m_gzfile) 00155 return -1; 00156 return gzputc(m_gzfile,ch); 00157 } 00158 00159 int KQIODeviceGZip::ungetch(int ch) 00160 { 00161 m_ungetchar=ch; 00162 return ch; 00163 }