Changeset View
Changeset View
Standalone View
Standalone View
src/generator/generateisotocountrymap.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2018 Volker Krause <vkrause@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include <QCoreApplication> | ||||
21 | #include <QCommandLineParser> | ||||
22 | #include <QDebug> | ||||
23 | #include <QFile> | ||||
24 | #include <QJsonArray> | ||||
25 | #include <QJsonDocument> | ||||
26 | #include <QJsonObject> | ||||
27 | | ||||
28 | int main(int argc, char **argv) | ||||
29 | { | ||||
30 | QCoreApplication app(argc, argv); | ||||
31 | QCommandLineParser p; | ||||
32 | QCommandLineOption inputFile({QStringLiteral("i")}, QStringLiteral("Location of iso_3166-1.json"), QStringLiteral("iso-codes"), QStringLiteral("/usr/share/iso-codes/json/iso_3166-1.json")); | ||||
33 | p.addOption(inputFile); | ||||
34 | QCommandLineOption outputFile({QStringLiteral("outputFile"), QStringLiteral("o")}, QStringLiteral("Output file"), QStringLiteral("output file"), {}); | ||||
35 | p.addOption(outputFile); | ||||
36 | p.addHelpOption(); | ||||
37 | p.process(app); | ||||
38 | if (!p.isSet(inputFile) || !p.isSet(outputFile)) { | ||||
39 | p.showHelp(0); | ||||
40 | } | ||||
41 | | ||||
42 | QFile input(p.value(inputFile)); | ||||
43 | if (!input.open(QFile::ReadOnly)) { | ||||
44 | qFatal("failed to open input file: %s", qPrintable(input.errorString())); | ||||
45 | } | ||||
46 | const auto doc = QJsonDocument::fromJson(input.readAll()); | ||||
47 | | ||||
48 | struct Entry { | ||||
49 | QString isoCode; | ||||
50 | QString name; | ||||
51 | int offset; | ||||
52 | }; | ||||
53 | std::vector<Entry> countries; | ||||
54 | for (const auto v : doc.object().value(QLatin1String("3166-1")).toArray()) { | ||||
55 | const auto obj = v.toObject(); | ||||
56 | countries.push_back(Entry{obj.value(QLatin1String("alpha_2")).toString().toLower(), obj.value(QLatin1String("name")).toString(), 0}); | ||||
57 | } | ||||
58 | std::sort(countries.begin(), countries.end(), [](const Entry &lhs, const Entry &rhs) { | ||||
59 | return lhs.isoCode < rhs.isoCode; | ||||
60 | }); | ||||
61 | | ||||
62 | QFile out(p.value(outputFile)); | ||||
63 | if (!out.open(QFile::WriteOnly)) { | ||||
64 | qFatal("failed to open file: %s", qPrintable(out.errorString())); | ||||
65 | } | ||||
66 | | ||||
67 | out.write(R"( | ||||
68 | /* | ||||
69 | Generated file based on iso-codes data. Do not edit! | ||||
70 | See src/generator/ for how to re-generate this. | ||||
71 | | ||||
72 | This library is free software; you can redistribute it and/or | ||||
73 | modify it under the terms of the GNU Library General Public | ||||
74 | License as published by the Free Software Foundation; either | ||||
75 | version 2 of the License, or (at your option) any later version. | ||||
76 | | ||||
77 | This library is distributed in the hope that it will be useful, | ||||
78 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
79 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
80 | Library General Public License for more details. | ||||
81 | | ||||
82 | You should have received a copy of the GNU Library General Public License | ||||
83 | along with this library; see the file COPYING.LIB. If not, write to | ||||
84 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
85 | Boston, MA 02110-1301, USA. | ||||
86 | */ | ||||
87 | | ||||
88 | #include "isotocountrymap_p.h" | ||||
89 | | ||||
90 | using namespace KContacts; | ||||
91 | | ||||
92 | static const char en_country_name_stringtable[] = { | ||||
93 | )"); | ||||
94 | | ||||
95 | int off = 0; | ||||
96 | for (auto &c : countries) { | ||||
97 | out.write(" \""); | ||||
98 | out.write(c.name.toUtf8()); | ||||
99 | out.write("\\0\"\n"); | ||||
100 | c.offset = off; | ||||
101 | off += c.name.toUtf8().size() + 1; | ||||
102 | } | ||||
103 | out.write(R"( | ||||
104 | }; | ||||
105 | | ||||
106 | static const IsoToCountryIndex iso_to_country_index[] = { | ||||
107 | )"); | ||||
108 | | ||||
109 | for (const auto &c: countries) { | ||||
110 | out.write(" IsoToCountryIndex{\""); | ||||
111 | out.write(c.isoCode.toUtf8()); | ||||
112 | out.write("\", "); | ||||
113 | out.write(QByteArray::number(c.offset)); | ||||
114 | out.write("},\n"); | ||||
115 | } | ||||
116 | out.write("};\n"); | ||||
117 | } |