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