Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.5 kB
2
Indexable
Never
#include <cassert>
#include <fstream>
#include <iostream>
#include <string_view>

#include "request_handler.h"

using namespace std;

void PrintUsage(std::ostream& stream = std::cerr) {
    stream << "Usage: transport_catalogue [make_base|process_requests]\n"sv;
}

const std::string& GetBaseFileName(const json::Dict& data) {
    return data.at("serialization_settings"s).AsMap().at("file"s).AsString();
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        PrintUsage();
        return 1;
    }

    const std::string_view mode(argv[1]);
const auto json = json::Load(std::cin).GetRoot().AsMap();
    transport_catalogue::TransportCatalogue catalogue;
    transport_catalogue::TransportCatalogueHandler catalogue_handler(catalogue);

    if (mode == "make_base"sv) {
        const std::string& file_name = GetBaseFileName(json);
            ifstream in(file_name, std::ios::binary);
        json_reader::JsonIO json(in);

        catalogue_handler.LoadDataFromJson(json);
        catalogue_handler.SerializeData();

    } else if (mode == "process_requests"sv) {
         const std::string& file_name = GetBaseFileName(json);
            std::ifstream in(file_name, std::ios::binary);
        json_reader::JsonIO json(in);

        catalogue_handler.LoadDataFromJson(json);
        catalogue_handler.DeserializeData();

        catalogue_handler.LoadRequestsAndAnswer(json, cout);

    } else {
        PrintUsage();
        return 1;
    }
}