KContacts

generateisotocountrymap.cpp
1 /*
2  SPDX-FileCopyrightText: 2018 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include <QCoreApplication>
8 #include <QCommandLineParser>
9 #include <QFile>
10 #include <QJsonArray>
11 #include <QJsonDocument>
12 #include <QJsonObject>
13 
14 int main(int argc, char **argv)
15 {
16  QCoreApplication app(argc, argv);
18  QCommandLineOption inputFile({QStringLiteral("i")}, QStringLiteral("Location of iso_3166-1.json"), QStringLiteral("iso-codes"), QStringLiteral("/usr/share/iso-codes/json/iso_3166-1.json"));
19  p.addOption(inputFile);
20  QCommandLineOption outputFile({QStringLiteral("outputFile"), QStringLiteral("o")}, QStringLiteral("Output file"), QStringLiteral("output file"), {});
21  p.addOption(outputFile);
22  p.addHelpOption();
23  p.process(app);
24  if (!p.isSet(inputFile) || !p.isSet(outputFile)) {
25  p.showHelp(0);
26  }
27 
28  QFile input(p.value(inputFile));
29  if (!input.open(QFile::ReadOnly)) {
30  qFatal("failed to open input file: %s", qPrintable(input.errorString()));
31  }
32  const auto doc = QJsonDocument::fromJson(input.readAll());
33 
34  struct Entry {
35  QString isoCode;
36  QString name;
37  int offset;
38  };
39  std::vector<Entry> countries;
40  for (const auto v : doc.object().value(QLatin1String("3166-1")).toArray()) {
41  const auto obj = v.toObject();
42  countries.push_back(Entry{obj.value(QLatin1String("alpha_2")).toString().toLower(), obj.value(QLatin1String("name")).toString(), 0});
43  }
44  std::sort(countries.begin(), countries.end(), [](const Entry &lhs, const Entry &rhs) {
45  return lhs.isoCode < rhs.isoCode;
46  });
47 
48  QFile out(p.value(outputFile));
49  if (!out.open(QFile::WriteOnly)) {
50  qFatal("failed to open file: %s", qPrintable(out.errorString()));
51  }
52 
53  out.write(R"(
54 /*
55  Generated file based on iso-codes data. Do not edit!
56  See src/generator/ for how to re-generate this.
57 
58  SPDX-License-Identifier: LGPL-2.0-or-later
59 */
60 
61 #include "isotocountrymap_p.h"
62 
63 using namespace KContacts;
64 
65 static const char en_country_name_stringtable[] = {
66 )");
67 
68  int off = 0;
69  for (auto &c : countries) {
70  out.write(" \"");
71  out.write(c.name.toUtf8());
72  out.write("\\0\"\n");
73  c.offset = off;
74  off += c.name.toUtf8().size() + 1;
75  }
76  out.write(R"(
77 };
78 
79 static const IsoToCountryIndex iso_to_country_index[] = {
80 )");
81 
82  for (const auto &c: countries) {
83  out.write(" IsoToCountryIndex{\"");
84  out.write(c.isoCode.toUtf8());
85  out.write("\", ");
86  out.write(QByteArray::number(c.offset));
87  out.write("},\n");
88  }
89  out.write("};\n");
90 }
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QString name(const QVariant &location)
QByteArray number(int n, int base)
QCommandLineOption addHelpOption()
bool isSet(const QString &name) const const
void push_back(QChar ch)
void process(const QStringList &arguments)
void showHelp(int exitCode)
bool addOption(const QCommandLineOption &option)
QString value(const QString &optionName) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jul 6 2020 22:54:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.