File indexing completed on 2024-04-28 04:41:31

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "gbfs.h"
0008 
0009 #include <QStringView>
0010 
0011 #include <cstring>
0012 #include <iterator>
0013 
0014 using namespace KPublicTransport;
0015 
0016 struct {
0017     const char *fileName;
0018     GBFS::FileType type;
0019 } static constexpr const file_info_map[] = {
0020     // ### keep the first part matching the enum order
0021     { "gbfs", GBFS::Discovery },
0022     { "system_information", GBFS::SystemInformation },
0023     { "station_information", GBFS::StationInformation },
0024     { "station_status", GBFS::StationStatus },
0025     { "free_bike_status", GBFS::FreeBikeStatus },
0026     { "gbfs_versions", GBFS::Versions },
0027     { "vehicle_types", GBFS::VehicleTypes },
0028     { "geofencing_zones", GBFS::GeofencingZones },
0029     { "system_hours", GBFS::SystemHours },
0030     { "system_calendar", GBFS::SystemCalendar },
0031     { "system_regions", GBFS::SystemRegions },
0032     { "system_pricing_plans", GBFS::SystemPricingPlans },
0033     { "system_alerts", GBFS::SystemAlerts },
0034 
0035     // typos observed in the wild
0036     { "vehicles_types", GBFS::VehicleTypes },
0037     { "system regions", GBFS::SystemRegions },
0038 };
0039 
0040 const char* GBFS::keyNameForType(GBFS::FileType type)
0041 {
0042     return file_info_map[type].fileName;
0043 }
0044 
0045 GBFS::FileType GBFS::typeForKeyName(QStringView v)
0046 {
0047     const auto s = v.toUtf8();
0048     for (auto it = std::begin(file_info_map); it != std::end(file_info_map); ++it) {
0049         if (std::strcmp((*it).fileName, s.constData()) == 0 || (s.endsWith(".json") && std::strncmp((*it).fileName, s.constData(), s.size() - 5) == 0)) {
0050             return (*it).type;
0051         }
0052     }
0053 
0054     return GBFS::Unknown;
0055 }