diff --git a/include/xlnt/packaging/manifest.hpp b/include/xlnt/packaging/manifest.hpp index bf4be407..ca8970c1 100644 --- a/include/xlnt/packaging/manifest.hpp +++ b/include/xlnt/packaging/manifest.hpp @@ -24,7 +24,7 @@ #pragma once #include -#include +#include #include #include @@ -41,17 +41,17 @@ class XLNT_CLASS manifest public: bool has_default_type(const std::string &extension) const; std::string get_default_type(const std::string &extension) const; - const std::vector &get_default_types() const; + const std::unordered_map &get_default_types() const; void add_default_type(const std::string &extension, const std::string &content_type); bool has_override_type(const std::string &part_name) const; std::string get_override_type(const std::string &part_name) const; - const std::vector &get_override_types() const; + const std::unordered_map &get_override_types() const; void add_override_type(const std::string &part_name, const std::string &content_type); private: - std::vector default_types_; - std::vector override_types_; + std::unordered_map default_types_; + std::unordered_map override_types_; }; } // namespace xlnt diff --git a/source/detail/manifest_serializer.cpp b/source/detail/manifest_serializer.cpp index 769f9d30..8e20675b 100644 --- a/source/detail/manifest_serializer.cpp +++ b/source/detail/manifest_serializer.cpp @@ -59,15 +59,15 @@ void manifest_serializer::write_manifest(pugi::xml_document &xml) const for (const auto default_type : manifest_.get_default_types()) { auto type_node = root_node.append_child("Default"); - type_node.append_attribute("Extension").set_value(default_type.get_extension().c_str()); - type_node.append_attribute("ContentType").set_value(default_type.get_content_type().c_str()); + type_node.append_attribute("Extension").set_value(default_type.second.get_extension().c_str()); + type_node.append_attribute("ContentType").set_value(default_type.second.get_content_type().c_str()); } for (const auto override_type : manifest_.get_override_types()) { auto type_node = root_node.append_child("Override"); - type_node.append_attribute("PartName").set_value(override_type.get_part_name().c_str()); - type_node.append_attribute("ContentType").set_value(override_type.get_content_type().c_str()); + type_node.append_attribute("PartName").set_value(override_type.second.get_part_name().c_str()); + type_node.append_attribute("ContentType").set_value(override_type.second.get_content_type().c_str()); } } diff --git a/source/packaging/manifest.cpp b/source/packaging/manifest.cpp index b7c9b74d..f246d443 100644 --- a/source/packaging/manifest.cpp +++ b/source/packaging/manifest.cpp @@ -26,88 +26,47 @@ #include -namespace { - -bool match_path(const std::string &path, const std::string &comparand) -{ - if (path == comparand) - { - return true; - } - - if (path[0] != '/' && path[0] != '.' && comparand[0] == '/') - { - return match_path("/" + path, comparand); - } - else if (comparand[0] != '/' && comparand[0] != '.' && path[0] == '/') - { - return match_path(path, "/" + comparand); - } - - return false; -} - -} // namespace - namespace xlnt { bool manifest::has_default_type(const std::string &extension) const { - return std::find_if(default_types_.begin(), default_types_.end(), - [&](const default_type &d) { return d.get_extension() == extension; }) != default_types_.end(); + return default_types_.find(extension) != default_types_.end(); } bool manifest::has_override_type(const std::string &part_name) const { - 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(); + auto absolute = part_name.front() == '/' ? part_name : ("/" + part_name); + return override_types_.find(absolute) != override_types_.end(); } void manifest::add_default_type(const std::string &extension, const std::string &content_type) { - if(has_default_type(extension)) return; - default_types_.push_back(default_type(extension, content_type)); + default_types_[extension] = default_type(extension, content_type); } void manifest::add_override_type(const std::string &part_name, const std::string &content_type) { - if(has_override_type(part_name)) return; - override_types_.push_back(override_type(part_name, content_type)); + auto absolute = part_name.front() == '/' ? part_name : ("/" + part_name); + override_types_[absolute] = override_type(absolute, content_type); } std::string manifest::get_default_type(const std::string &extension) const { - 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()) - { - throw std::runtime_error("no default type found for extension: " + extension); - } - - return match->get_content_type(); + return default_types_.at(extension).get_content_type(); } std::string manifest::get_override_type(const std::string &part_name) const { - 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()) - { - throw std::runtime_error("no default type found for part name: " + part_name); - } - - return match->get_content_type(); + auto absolute = part_name.front() == '/' ? part_name : ("/" + part_name); + return override_types_.at(absolute).get_content_type(); } -const std::vector &manifest::get_default_types() const +const std::unordered_map &manifest::get_default_types() const { return default_types_; } -const std::vector &manifest::get_override_types() const +const std::unordered_map &manifest::get_override_types() const { return override_types_; } diff --git a/source/styles/tests/test_stylesheet.hpp b/source/styles/tests/test_stylesheet.hpp index 7d486228..f3a42331 100644 --- a/source/styles/tests/test_stylesheet.hpp +++ b/source/styles/tests/test_stylesheet.hpp @@ -53,6 +53,11 @@ public: wb.add_style(s); TS_ASSERT_EQUALS(e.get_stylesheet().styles.size(), 2); + + xlnt::style copy; + copy = s; + + TS_ASSERT_EQUALS(s, copy); } /*