Debian Codegraph @

src/importer/ChewingImporter.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 "ChewingImporter.h"
21
22 #include <QByteArray>
23 #include <QDebug>
24 #include <QFile>
25 #include <QJsonArray>
26 #include <QJsonDocument>
27 #include <QJsonObject>
28 #include <QJsonParseError>
29
30 ChewingImporter::ChewingImporter(const QString& path)
31 :UserphraseImporter{path}
32 {
33 UserphraseSet result;
34
35 QFile file{path_};
36 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
37 qWarning() << "Cannot open file" << path_;
38 return;
39 }
40
41 QJsonParseError err;
42 auto doc = QJsonDocument::fromJson(file.readAll(), &err);
43 if (doc.isNull()) {
44 qWarning() << "parse json error in " << path_ << err.errorString();
45 return;
46 }
47
48 if (!doc.isObject()) {
49 qWarning() << "root is not an object in" << path_;
50 return;
51 }
52
53 auto root = doc.object();
54 if (!root["userphrase"].isArray()) {
55 qWarning() << "userphrase is not an array in " << path_;
56 return;
57 }
58
59 auto array = root["userphrase"].toArray();
60 for (auto i = array.begin(); i != array.end(); ++i) {
61 if (!(*i).isObject()) {
62 qWarning() << "skip non object in " << path_;
63 continue;
64 }
65
66 auto obj = (*i).toObject();
67 if (!obj["phrase"].isString() || !obj["bopomofo"].isString()) {
68 qWarning() << "phrase or bopomofo is not a string in" << path_;
69 continue;
70 }
71
72 result.insert(Userphrase{
73 obj["phrase"].toString(),
74 obj["bopomofo"].toString()
75 });
76 }
77
78 std::swap(userphrase_, result);
79 supportedFormat_ = true;
80 }