osrm-backend/include/storage/io_config.hpp

90 lines
2.5 KiB
C++
Raw Normal View History

#ifndef OSRM_IO_CONFIG_HPP
#define OSRM_IO_CONFIG_HPP
#include "util/exception.hpp"
#include <boost/algorithm/string/predicate.hpp>
#include <array>
#include <filesystem>
#include <string>
namespace osrm::storage
{
struct IOConfig
{
IOConfig(std::vector<std::filesystem::path> required_input_files_,
std::vector<std::filesystem::path> optional_input_files_,
std::vector<std::filesystem::path> output_files_)
: required_input_files(std::move(required_input_files_)),
optional_input_files(std::move(optional_input_files_)),
output_files(std::move(output_files_))
{
}
2017-07-07 10:42:07 -04:00
bool IsValid() const;
std::vector<std::string> GetMissingFiles() const;
std::filesystem::path GetPath(const std::string &fileName) const
2017-07-07 10:42:07 -04:00
{
if (!IsConfigured(fileName, required_input_files) &&
!IsConfigured(fileName, optional_input_files) && !IsConfigured(fileName, output_files))
{
throw util::exception("Tried to access file which is not configured: " + fileName);
}
return {base_path.string() + fileName};
}
bool IsRequiredConfiguredInput(const std::string &fileName) const
{
return IsConfigured(fileName, required_input_files);
}
std::filesystem::path base_path;
2017-07-07 10:42:07 -04:00
protected:
// Infer the base path from the path of the .osrm file
void UseDefaultOutputNames(const std::filesystem::path &base)
{
// potentially strip off the .osrm (or other) extensions for
// determining the base path=
std::string path = base.string();
std::array<std::string, 6> known_extensions{
{".osm.bz2", ".osm.pbf", ".osm.xml", ".pbf", ".osm", ".osrm"}};
for (const auto &ext : known_extensions)
{
const auto pos = path.find(ext);
if (pos != std::string::npos)
{
path.replace(pos, ext.size(), "");
break;
}
}
base_path = {path};
}
private:
static bool IsConfigured(const std::string &fileName,
const std::vector<std::filesystem::path> &paths)
{
for (auto &path : paths)
{
if (boost::algorithm::ends_with(path.string(), fileName))
{
return true;
}
}
return false;
}
std::vector<std::filesystem::path> required_input_files;
std::vector<std::filesystem::path> optional_input_files;
std::vector<std::filesystem::path> output_files;
};
2022-12-20 12:00:11 -05:00
} // namespace osrm::storage
#endif