fix bug
[mdictionary] / src / plugins / stardict / UncompressedReader.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 UncompressedReader.cpp
23     \brief Class implementing StarDictReader interface and handling
24   reading from uncompressed files
25
26     \author Mateusz Półrola <mateusz.polrola@comarch.pl>
27  */
28
29 #include "UncompressedReader.h"
30 #include "QDebug"
31
32
33 UncompressedReader::UncompressedReader(QObject *parent) :
34         StarDictReader(parent) {
35
36 }
37
38
39 UncompressedReader::UncompressedReader(QString filename, QObject *parent) :
40 StarDictReader(parent) {
41     open(filename);
42 }
43
44
45 UncompressedReader::~UncompressedReader() {
46     if(_file.isOpen())
47         _file.close();
48 }
49
50
51 bool UncompressedReader::open(QString file) {
52     _file.setFileName(file);
53     _stream.setDevice(&_file);
54     return _file.open(QFile::ReadOnly);
55 }
56
57
58 void UncompressedReader::close() {
59     _file.close();
60 }
61
62
63 QChar UncompressedReader::readChar() {
64     char c[4]={0};
65     QString cha;
66     _stream.readRawData(c,1);
67     if(((unsigned char)c[0])>239)
68         _stream.readRawData(c+1,3);
69     else if(((unsigned char)c[0])>223)
70         _stream.readRawData(c+1,2);
71     else if(((unsigned char)c[0])>191)
72         _stream.readRawData(c+1,1);
73     else if(((unsigned char)c[0])>127)
74         qDebug()<<"error - starDict - read wordList from UTF-8";
75
76     cha=QString::fromUtf8(c);
77     if(cha.size()!=0)
78         return cha.at(0);
79     else
80         return '\0';
81 }
82
83
84 QString UncompressedReader::readKeyword() {
85     QString result;
86     QChar c;
87     c = readChar();
88     while(c != '\0') {
89         result += c;
90         c = readChar();
91     }
92     return result;
93 }
94
95
96 QByteArray UncompressedReader::readString(qint64 offset, qint32 len) {
97     char* buf;
98     buf = new char[len];
99
100     _file.seek(offset);
101     _stream.readRawData(buf, len);
102
103     QByteArray result(buf, len);
104     delete [] buf;
105     return result;
106 }
107
108
109 qint32 UncompressedReader::readInt32BigEndian() {
110     _stream.setByteOrder(QDataStream::BigEndian);
111     qint32 value;
112     _stream>>value;
113     _stream.setByteOrder(QDataStream::LittleEndian);
114     return value;
115 }
116
117
118 qint64 UncompressedReader::readInt64BigEndian() {
119     _stream.setByteOrder(QDataStream::BigEndian);
120     qint64 value;
121     _stream>>value;
122     _stream.setByteOrder(QDataStream::LittleEndian);
123     return value;
124 }