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 <QCommandLineParser>
8 #include <QCoreApplication>
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")},
19  QStringLiteral("Location of iso_3166-1.json"),
20  QStringLiteral("iso-codes"),
21  QStringLiteral("/usr/share/iso-codes/json/iso_3166-1.json"));
22  p.addOption(inputFile);
23  QCommandLineOption outputFile({QStringLiteral("outputFile"), QStringLiteral("o")}, QStringLiteral("Output file"), QStringLiteral("output file"), {});
24  p.addOption(outputFile);
25  p.addHelpOption();
26  p.process(app);
27  if (!p.isSet(inputFile) || !p.isSet(outputFile)) {
28  p.showHelp(0);
29  }
30 
31  QFile input(p.value(inputFile));
32  if (!input.open(QFile::ReadOnly)) {
33  qFatal("failed to open input file: %s", qPrintable(input.errorString()));
34  }
35  const auto doc = QJsonDocument::fromJson(input.readAll());
36 
37  struct Entry {
38  QString isoCode;
39  QString name;
40  int offset;
41  };
42  std::vector<Entry> countries;
43  for (const auto v : doc.object().value(QLatin1String("3166-1")).toArray()) {
44  const auto obj = v.toObject();
45  countries.push_back(Entry{obj.value(QLatin1String("alpha_2")).toString().toLower(), obj.value(QLatin1String("name")).toString(), 0});
46  }
47  std::sort(countries.begin(), countries.end(), [](const Entry &lhs, const Entry &rhs) {
48  return lhs.isoCode < rhs.isoCode;
49  });
50 
51  QFile out(p.value(outputFile));
52  if (!out.open(QFile::WriteOnly)) {
53  qFatal("failed to open file: %s", qPrintable(out.errorString()));
54  }
55 
56  out.write(R"(
57 /*
58  Generated file based on iso-codes data. Do not edit!
59  See src/generator/ for how to re-generate this.
60 
61  SPDX-License-Identifier: LGPL-2.0-or-later
62 */
63 
64 #include "isotocountrymap_p.h"
65 
66 using namespace KContacts;
67 
68 static const char en_country_name_stringtable[] = {
69 )");
70 
71  int off = 0;
72  for (auto &c : countries) {
73  out.write(" \"");
74  out.write(c.name.toUtf8());
75  out.write("\\0\"\n");
76  c.offset = off;
77  off += c.name.toUtf8().size() + 1;
78  }
79  out.write(R"(
80 };
81 
82 static const IsoToCountryIndex iso_to_country_index[] = {
83 )");
84 
85  for (const auto &c : countries) {
86  out.write(" IsoToCountryIndex{\"");
87  out.write(c.isoCode.toUtf8());
88  out.write("\", ");
89  out.write(QByteArray::number(c.offset));
90  out.write("},\n");
91  }
92  out.write("};\n");
93 }
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-2021 The KDE developers.
Generated on Mon Sep 20 2021 22:56:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.