Debian Codegraph @

src/importer/HashImporter.cpp

raw symbols SCIP
1 /*
2 * chewing-editor: Chewing userphrase editor
3 * Copyright (C) 2014 Chewing Development Team
4
5 * This program 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 2 of the License, or
8 * (at your option) any later version.
9
10 * This program 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "HashImporter.h"
21
22 #include <cstring>
23 #include <vector>
24
25 #include <QByteArray>
26 #include <QDataStream>
27 #include <QDebug>
28 #include <QFile>
29 #include <QString>
30
31 const size_t SIGNATURE_SIZE = 4;
32 const size_t CHEWING_LIFETIME_SIZE = 4;
33 const size_t USERPHRASE_SIZE = 125;
34
35 const size_t USERPHRASE_LEN_OFFSET = 16;
36
37 const size_t BOPOMOFO_SIZE = 4;
38 const int BOPOMOFO_SHIFT[] = { 9, 7, 3, 0 };
39 const int BOPOMOFO_MASK[] = { 0x1F, 0x3, 0xF, 0x7 };
40 const int BOPOMOFO_LEN[] = { 3, 3, 3, 2 };
41 const char *const BOPOMOFO_TABLE[] = {
42 // ㄅㄆㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐㄑㄒㄓㄔㄕㄖㄗㄘㄙ
43 "\xE3\x84\x85\xE3\x84\x86\xE3\x84\x87\xE3\x84\x88\xE3\x84\x89"
44 "\xE3\x84\x8A\xE3\x84\x8B\xE3\x84\x8C\xE3\x84\x8D\xE3\x84\x8E"
45 "\xE3\x84\x8F\xE3\x84\x90\xE3\x84\x91\xE3\x84\x92\xE3\x84\x93"
46 "\xE3\x84\x94\xE3\x84\x95\xE3\x84\x96\xE3\x84\x97\xE3\x84\x98"
47 "\xE3\x84\x99",
48
49 // ㄧㄨㄩ
50 "\xE3\x84\xA7\xE3\x84\xA8\xE3\x84\xA9",
51
52 // ㄚㄛㄜㄝㄞㄟㄠㄡㄢㄣㄤㄥㄦ
53 "\xE3\x84\x9A\xE3\x84\x9B\xE3\x84\x9C\xE3\x84\x9D\xE3\x84\x9E"
54 "\xE3\x84\x9F\xE3\x84\xA0\xE3\x84\xA1\xE3\x84\xA2\xE3\x84\xA3"
55 "\xE3\x84\xA4\xE3\x84\xA5\xE3\x84\xA6",
56
57 // ˙ˊˇˋ
58 "\xCB\x99\xCB\x8A\xCB\x87\xCB\x8B"
59 };
60
61 static uint16_t getUint16(const char *ptr)
62 {
63 auto uptr = reinterpret_cast<const unsigned char *>(ptr);
64
65 return (uptr[0] << 0) | (uptr[1] << 8);
66 }
67
68 static QString getBopomofoFromPhoneArray(const std::vector<uint16_t> &phone_array)
69 {
70 QByteArray buf;
71
72 for (auto it = phone_array.begin(); it != phone_array.end(); ++it) {
73 qDebug() << "*it = " << *it;
74 for (size_t i = 0; i < BOPOMOFO_SIZE; ++i) {
75 size_t idx = ((*it >> BOPOMOFO_SHIFT[i]) & BOPOMOFO_MASK[i]);
76 if (idx == 0) {
77 continue;
78 }
79
80 buf.append(&BOPOMOFO_TABLE[i][(idx - 1)* BOPOMOFO_LEN[i]], BOPOMOFO_LEN[i]);
81 }
82 buf.append(' ');
83 }
84 buf[buf.length()-1] = 0;
85
86 qDebug() << "buf = " << buf;
87
88 return QString(buf);
89 }
90
91 HashImporter::HashImporter(const QString& path)
92 :UserphraseImporter{path}
93 {
94 QFile file{path};
95 int ret;
96
97 if (!file.open(QIODevice::ReadOnly)) {
98 qWarning() << "Cannot open file" << path_;
99 return;
100 }
101
102 QDataStream data{&file};
103
104 std::vector<char> buffer(USERPHRASE_SIZE);
105
106 ret = data.readRawData(&buffer[0], SIGNATURE_SIZE);
107 if (ret != SIGNATURE_SIZE) {
108 qWarning() << "Cannot read signature" << path_;
109 return;
110 }
111
112 if (memcmp(&buffer[0], "CBiH", CHEWING_LIFETIME_SIZE) != 0) {
113 qWarning() << "Cannot find signature `CBiH`" << path_;
114 return;
115 }
116
117 ret = data.readRawData(&buffer[0], CHEWING_LIFETIME_SIZE);
118 if (ret != CHEWING_LIFETIME_SIZE) {
119 qWarning() << "Cannot read chewing_lifetime" << path_;
120 return;
121 }
122
123 UserphraseSet result;
124
125 while (data.readRawData(&buffer[0], USERPHRASE_SIZE) == USERPHRASE_SIZE) {
126 size_t pos = USERPHRASE_LEN_OFFSET;
127
128 int phone_len = static_cast<unsigned char>(buffer[pos]);
129 ++pos;
130
131 std::vector<uint16_t> phone_array;
132 for (int i = 0; i < phone_len; ++i) {
133 phone_array.push_back(getUint16(&buffer[pos]));
134 pos += 2;
135 }
136
137 int phrase_len = static_cast<unsigned char>(buffer[pos]);
138 ++pos;
139
140 buffer[pos + phrase_len] = 0;
141
142 result.insert(Userphrase{
143 QString{&buffer[pos]},
144 getBopomofoFromPhoneArray(phone_array)
145 });
146 }
147
148 std::swap(userphrase_, result);
149 supportedFormat_ = true;
150 }