fix bug
[mdictionary] / src / plugins / stardict / CompressedReader.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21 /*!
22     \file CompressedReader.cpp
23     \brief Class implementing StarDictReader interface and handling reading from compressed
24     files like .gz or .dz
25
26     \author Mateusz Półrola
27 */
28
29 #include "CompressedReader.h"
30 #include <QtEndian>
31 #include <QDebug>
32
33 CompressedReader::CompressedReader(QObject *parent) :
34     StarDictReader(parent) {
35 }
36
37
38 CompressedReader::CompressedReader(QString filename, QObject *parent) :
39     StarDictReader(parent) {
40     open(filename);
41 }
42
43
44 CompressedReader::~CompressedReader() {
45     if(_file != NULL)
46         gzclose(_file);
47 }
48
49
50 bool CompressedReader::open(QString file) {
51     _file = gzopen(file.toStdString().c_str(), "rb");
52     if(_file == NULL)
53         return false;
54     return true;
55 }
56
57
58 void CompressedReader::close() {
59     gzclose(_file);
60     _file = NULL;
61 }
62
63
64 QChar CompressedReader::readChar() {
65     char c[4]={0};
66     QString cha;
67
68     gzread(_file, c, 1);
69     if(((unsigned char)c[0])>239)
70         gzread(_file, c+1, 3);
71     else if(((unsigned char)c[0])>223)
72         gzread(_file, c+1, 2);
73     else if(((unsigned char)c[0])>191)
74         gzread(_file, c+1, 1);
75     else if(((unsigned char)c[0])>127){
76         qDebug()<<"error - starDict - read wordList from UTF-8";
77         c[0]=0;
78     }
79
80     cha=QString::fromUtf8(c);
81     if(cha.size()!=0)
82         return cha.at(0);
83     else
84         return '\0';
85 }
86
87
88 qint32 CompressedReader::readInt32BigEndian() {
89     qint32 value;
90     gzread(_file, (void*)(&value), 4);
91     return qFromBigEndian(value);
92 }
93
94
95 qint64 CompressedReader::readInt64BigEndian() {
96     qint64 value;
97     gzread(_file, (void*)(&value), 8);
98     return value;
99 }
100
101
102 QString CompressedReader::readKeyword() {
103     QString result;
104     QChar c;
105
106     c = readChar();
107     while(c != '\0') {
108         result += c;
109         c = readChar();
110     }
111     return result;
112 }
113
114
115 QByteArray CompressedReader::readString(qint64 offset, qint32 len) {
116     char* buf;
117     buf = new char[len];
118
119     gzseek(_file, offset, SEEK_SET);
120     gzread(_file, buf, len);
121
122     QByteArray res(buf, len);
123     delete [] buf;
124     return res;
125 }
126