xlnt/source/packaging/manifest.cpp

93 lines
2.6 KiB
C++
Raw Normal View History

2015-11-01 22:48:51 +08:00
#include <algorithm>
#include <stdexcept>
2015-11-01 22:48:51 +08:00
#include <xlnt/packaging/manifest.hpp>
2015-10-30 01:46:56 +08:00
2015-10-31 06:54:04 +08:00
namespace {
bool match_path(const std::string &path, const std::string &comparand)
2015-10-31 06:54:04 +08:00
{
if (path == comparand)
2015-10-31 06:54:04 +08:00
{
return true;
}
if (path[0] != '/' && path[0] != '.' && comparand[0] == '/')
2015-10-31 06:54:04 +08:00
{
return match_path("/" + path, comparand);
}
else if (comparand[0] != '/' && comparand[0] != '.' && path[0] == '/')
2015-10-31 06:54:04 +08:00
{
return match_path(path, "/" + comparand);
}
2015-10-31 06:54:04 +08:00
return false;
}
2015-10-31 06:54:04 +08:00
} // namespace
2015-10-30 01:46:56 +08:00
namespace xlnt {
bool manifest::has_default_type(const std::string &extension) const
2015-10-30 01:46:56 +08:00
{
return std::find_if(default_types_.begin(), default_types_.end(),
[&](const default_type &d) { return d.get_extension() == extension; }) != default_types_.end();
2015-10-30 01:46:56 +08:00
}
bool manifest::has_override_type(const std::string &part_name) const
2015-10-30 01:46:56 +08:00
{
return std::find_if(override_types_.begin(), override_types_.end(), [&](const override_type &d) {
return match_path(d.get_part_name(), part_name);
}) != override_types_.end();
2015-10-31 06:54:04 +08:00
}
void manifest::add_default_type(const std::string &extension, const std::string &content_type)
2015-10-31 06:54:04 +08:00
{
2015-11-02 12:52:19 +08:00
if(has_default_type(extension)) return;
2015-10-31 06:54:04 +08:00
default_types_.push_back(default_type(extension, content_type));
}
void manifest::add_override_type(const std::string &part_name, const std::string &content_type)
2015-10-31 06:54:04 +08:00
{
2015-11-02 12:52:19 +08:00
if(has_override_type(part_name)) return;
2015-10-31 06:54:04 +08:00
override_types_.push_back(override_type(part_name, content_type));
2015-10-30 01:46:56 +08:00
}
std::string manifest::get_default_type(const std::string &extension) const
2015-10-30 01:46:56 +08:00
{
auto match = std::find_if(default_types_.begin(), default_types_.end(),
[&](const default_type &d) { return d.get_extension() == extension; });
if (match == default_types_.end())
2015-10-30 01:46:56 +08:00
{
throw std::runtime_error("no default type found for extension: " + extension);
2015-10-30 01:46:56 +08:00
}
2015-10-30 01:46:56 +08:00
return match->get_content_type();
}
std::string manifest::get_override_type(const std::string &part_name) const
2015-10-30 01:46:56 +08:00
{
auto match = std::find_if(override_types_.begin(), override_types_.end(),
[&](const override_type &d) { return match_path(d.get_part_name(), part_name); });
if (match == override_types_.end())
2015-10-30 01:46:56 +08:00
{
throw std::runtime_error("no default type found for part name: " + part_name);
2015-10-30 01:46:56 +08:00
}
2015-10-30 01:46:56 +08:00
return match->get_content_type();
}
const std::vector<default_type> &manifest::get_default_types() const
{
return default_types_;
}
const std::vector<override_type> &manifest::get_override_types() const
{
return override_types_;
}
2015-10-30 01:46:56 +08:00
} // namespace xlnt