From 1c6d3c9c695849fd879527a2891eb35e4cdfaaec Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Mon, 23 Jul 2018 14:39:50 +0200 Subject: [PATCH] Added xml escape characters detection when exporting object and volumes names to 3mf files --- xs/src/libslic3r/Format/3mf.cpp | 4 ++-- xs/src/libslic3r/Format/AMF.cpp | 28 +--------------------------- xs/src/libslic3r/Utils.hpp | 2 ++ xs/src/libslic3r/utils.cpp | 27 +++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/xs/src/libslic3r/Format/3mf.cpp b/xs/src/libslic3r/Format/3mf.cpp index 2c32db1a6..dd3500eba 100644 --- a/xs/src/libslic3r/Format/3mf.cpp +++ b/xs/src/libslic3r/Format/3mf.cpp @@ -1989,7 +1989,7 @@ namespace Slic3r { // stores object's name if (!obj->name.empty()) - stream << " <" << METADATA_TAG << " " << TYPE_ATTR << "=\"" << OBJECT_TYPE << "\" " << KEY_ATTR << "=\"name\" " << VALUE_ATTR << "=\"" << obj->name << "\"/>\n"; + stream << " <" << METADATA_TAG << " " << TYPE_ATTR << "=\"" << OBJECT_TYPE << "\" " << KEY_ATTR << "=\"name\" " << VALUE_ATTR << "=\"" << xml_escape(obj->name) << "\"/>\n"; // stores object's config data for (const std::string& key : obj->config.keys()) @@ -2012,7 +2012,7 @@ namespace Slic3r { // stores volume's name if (!volume->name.empty()) - stream << " <" << METADATA_TAG << " " << TYPE_ATTR << "=\"" << VOLUME_TYPE << "\" " << KEY_ATTR << "=\"" << NAME_KEY << "\" " << VALUE_ATTR << "=\"" << volume->name << "\"/>\n"; + stream << " <" << METADATA_TAG << " " << TYPE_ATTR << "=\"" << VOLUME_TYPE << "\" " << KEY_ATTR << "=\"" << NAME_KEY << "\" " << VALUE_ATTR << "=\"" << xml_escape(volume->name) << "\"/>\n"; // stores volume's modifier field if (volume->modifier) diff --git a/xs/src/libslic3r/Format/AMF.cpp b/xs/src/libslic3r/Format/AMF.cpp index be513166e..600aa6cd9 100644 --- a/xs/src/libslic3r/Format/AMF.cpp +++ b/xs/src/libslic3r/Format/AMF.cpp @@ -8,6 +8,7 @@ #include "../libslic3r.h" #include "../Model.hpp" #include "../GCode.hpp" +#include "../Utils.hpp" #include "../slic3r/GUI/PresetBundle.hpp" #include "AMF.hpp" @@ -686,33 +687,6 @@ bool load_amf(const char *path, PresetBundle* bundle, Model *model) return false; } -std::string xml_escape(std::string text) -{ - std::string::size_type pos = 0; - for (;;) - { - pos = text.find_first_of("\"\'&<>", pos); - if (pos == std::string::npos) - break; - - std::string replacement; - switch (text[pos]) - { - case '\"': replacement = """; break; - case '\'': replacement = "'"; break; - case '&': replacement = "&"; break; - case '<': replacement = "<"; break; - case '>': replacement = ">"; break; - default: break; - } - - text.replace(pos, 1, replacement); - pos += replacement.size(); - } - - return text; -} - bool store_amf(const char *path, Model *model, Print* print, bool export_print_config) { if ((path == nullptr) || (model == nullptr) || (print == nullptr)) diff --git a/xs/src/libslic3r/Utils.hpp b/xs/src/libslic3r/Utils.hpp index a501fa4d3..349222854 100644 --- a/xs/src/libslic3r/Utils.hpp +++ b/xs/src/libslic3r/Utils.hpp @@ -84,6 +84,8 @@ inline T next_highest_power_of_2(T v) return ++ v; } +extern std::string xml_escape(std::string text); + class PerlCallback { public: PerlCallback(void *sv) : m_callback(nullptr) { this->register_callback(sv); } diff --git a/xs/src/libslic3r/utils.cpp b/xs/src/libslic3r/utils.cpp index 13ec1d066..55164bbdd 100644 --- a/xs/src/libslic3r/utils.cpp +++ b/xs/src/libslic3r/utils.cpp @@ -387,4 +387,31 @@ unsigned get_current_pid() #endif } +std::string xml_escape(std::string text) +{ + std::string::size_type pos = 0; + for (;;) + { + pos = text.find_first_of("\"\'&<>", pos); + if (pos == std::string::npos) + break; + + std::string replacement; + switch (text[pos]) + { + case '\"': replacement = """; break; + case '\'': replacement = "'"; break; + case '&': replacement = "&"; break; + case '<': replacement = "<"; break; + case '>': replacement = ">"; break; + default: break; + } + + text.replace(pos, 1, replacement); + pos += replacement.size(); + } + + return text; +} + }; // namespace Slic3r