File indexing completed on 2024-12-29 04:50:10

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "config-kitinerary.h"
0008 #include "countrydb.h"
0009 #include "countrydb_data.cpp"
0010 
0011 #include <QDebug>
0012 #include <QString>
0013 
0014 #include <algorithm>
0015 
0016 using namespace KItinerary;
0017 using namespace KItinerary::KnowledgeDb;
0018 
0019 static_assert(sizeof(CountryId) <= 2, "CountryId too large");
0020 
0021 Country KnowledgeDb::countryForId(CountryId id)
0022 {
0023     const auto it = std::lower_bound(std::begin(country_table), std::end(country_table), id, [](const Country &lhs, CountryId rhs) {
0024         return lhs.id < rhs;
0025     });
0026     if (it == std::end(country_table) || (*it).id != id) {
0027         return {CountryId{}, DrivingSide::Unknown, Unknown};
0028     }
0029     return (*it);
0030 }
0031 
0032 struct PowerPlugCompatMap {
0033     PowerPlugType plug;
0034     PowerPlugTypes sockets;
0035 };
0036 
0037 static const PowerPlugCompatMap power_plug_compat_map[] = {
0038     { TypeA, TypeA | TypeB },
0039     { TypeB, TypeB },
0040     { TypeC, TypeC | TypeE | TypeF | TypeH | TypeJ | TypeK | TypeL | TypeN },
0041     { TypeD, TypeD },
0042     { TypeE, TypeE | TypeF | TypeK }, // TypeE <-> TypeF not strictly correct, but in practice almost always compatible
0043     { TypeF, TypeE | TypeF | TypeK },
0044     { TypeG, TypeG },
0045     { TypeH, TypeH },
0046     { TypeI, TypeI },
0047     { TypeJ, TypeJ },
0048     { TypeK, TypeK },
0049     { TypeL, TypeL },
0050     { TypeM, TypeM },
0051     { TypeN, TypeN }
0052 };
0053 
0054 PowerPlugTypes KnowledgeDb::incompatiblePowerPlugs(PowerPlugTypes plugs, PowerPlugTypes sockets)
0055 {
0056     PowerPlugTypes failPlugs{};
0057     for (const auto map : power_plug_compat_map) {
0058         if ((plugs & map.plug) == 0) {
0059             continue;
0060         }
0061         if ((map.sockets & sockets) == 0) {
0062             failPlugs |= map.plug;
0063         }
0064     }
0065     return failPlugs;
0066 }
0067 
0068 PowerPlugTypes KnowledgeDb::incompatiblePowerSockets(PowerPlugTypes plugs, PowerPlugTypes sockets)
0069 {
0070     PowerPlugTypes failSockets{};
0071     for (const auto map : power_plug_compat_map) {
0072         if ((plugs & map.plug) == 0) {
0073             continue;
0074         }
0075         if ((map.sockets & ~sockets) != 0) {
0076             failSockets |= (map.sockets ^ sockets) & sockets;
0077         }
0078     }
0079     return failSockets & ~plugs;
0080 }
0081 
0082 KnowledgeDb::CountryId KnowledgeDb::countryIdForUicCode(uint16_t uicCountryCode)
0083 {
0084     const auto it = std::lower_bound(std::begin(uic_country_code_table), std::end(uic_country_code_table), uicCountryCode, [](const auto &lhs, uint16_t rhs) {
0085         return lhs.uicCode < rhs;
0086     });
0087     if (it == std::end(uic_country_code_table) || (*it).uicCode != uicCountryCode) {
0088         return {};
0089     }
0090 
0091     return (*it).isoCode;
0092 }
0093 
0094 #include "moc_countrydb.cpp"