summaryrefslogtreecommitdiffstats
path: root/kdecore/kqiodevicegzip_p.cpp
blob: 84409ddb0c5bc2e0dffa260bd40108b89df6c5d5 (plain)
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
/* 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

#include <tqstring.h>
#include "kqiodevicegzip_p.h"

KQIODeviceGZip::KQIODeviceGZip(const TQString& filename)
{
    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();
}

bool KQIODeviceGZip::open(OpenMode mode)
{
    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)
    {
        m_gzfile=gzopen(TQFile::encodeName(m_filename),"rb");
    }
    else if (IO_WriteOnly==mode)
    {
        m_gzfile=gzopen(TQFile::encodeName(m_filename),"wb9"); // Always set best compression
    }
    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);
    }
}

#ifdef USE_QT4
qint64 KQIODeviceGZip::size(void) const
#else // USE_QT4
TQIODevice::Offset KQIODeviceGZip::size(void) const
#endif // USE_QT4
{
    return 0; // You cannot determine size!
}

TQIODevice::Offset  KQIODeviceGZip::at() const
{
    if (!m_gzfile)
        return 0;
    return gztell(m_gzfile);
}

bool KQIODeviceGZip::at(TQIODevice::Offset pos)
{
    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);
}

TQT_TQIO_LONG KQIODeviceGZip::tqreadBlock( char *data, TQT_TQIO_ULONG maxlen )
{
    TQ_LONG result=0;
    if (m_gzfile)
    {
        result=gzread(m_gzfile,data,maxlen);
        if (result<0) result=0;
    }
    return result;
}

TQT_TQIO_LONG KQIODeviceGZip::tqwriteBlock( const char *data, TQT_TQIO_ULONG len )
{
    TQ_ULONG result=0;
    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;
}