xlnt/source/serialization/shared_strings_serializer.cpp

59 lines
1.4 KiB
C++
Raw Normal View History

2015-11-02 01:31:29 +08:00
#include <xlnt/serialization/shared_strings_serializer.hpp>
#include <xlnt/serialization/xml_document.hpp>
#include <xlnt/serialization/xml_node.hpp>
2015-10-30 01:46:56 +08:00
namespace xlnt {
xml_document shared_strings_serializer::write_shared_strings(const std::vector<std::string> &strings)
2015-10-31 06:54:04 +08:00
{
xml_document xml;
2015-10-31 06:54:04 +08:00
auto root_node = xml.add_child("sst");
2015-10-31 06:54:04 +08:00
xml.add_namespace("", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
root_node.add_attribute("uniqueCount", std::to_string(strings.size()));
for (const auto &string : strings)
2015-10-31 06:54:04 +08:00
{
root_node.add_child("si").add_child("t").set_text(string);
}
2015-10-31 06:54:04 +08:00
return xml;
}
2015-10-30 01:46:56 +08:00
bool shared_strings_serializer::read_shared_strings(const xml_document &xml, std::vector<std::string> &strings)
2015-10-30 01:46:56 +08:00
{
strings.clear();
2015-10-31 06:54:04 +08:00
auto root_node = xml.get_child("sst");
auto unique_count = std::stoull(root_node.get_attribute("uniqueCount"));
for (const auto &si_node : root_node.get_children())
2015-10-30 01:46:56 +08:00
{
if (si_node.get_name() != "si")
2015-10-31 06:54:04 +08:00
{
continue;
}
if (si_node.has_child("t"))
2015-10-31 06:54:04 +08:00
{
strings.push_back(si_node.get_child("t").get_text());
}
else if (si_node.has_child("r"))
2015-10-31 06:54:04 +08:00
{
strings.push_back(si_node.get_child("r").get_child("t").get_text());
}
2015-10-30 01:46:56 +08:00
}
if (unique_count != strings.size())
2015-10-30 01:46:56 +08:00
{
throw std::runtime_error("counts don't match");
}
2015-10-30 01:46:56 +08:00
return true;
}
} // namespace xlnt