From 4d2c7f116d5d70c516c0ca825c826acbf896af05 Mon Sep 17 00:00:00 2001 From: Peter LaValle Date: Tue, 22 Apr 2014 16:33:11 +0100 Subject: [PATCH] XML filename tweaks This change encodes the XML strings for scenarios involving `&` et al in material file names --- tools/assimp_cmd/WriteDumb.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tools/assimp_cmd/WriteDumb.cpp b/tools/assimp_cmd/WriteDumb.cpp index d33a83f64..669e71b3a 100644 --- a/tools/assimp_cmd/WriteDumb.cpp +++ b/tools/assimp_cmd/WriteDumb.cpp @@ -808,6 +808,27 @@ const char* TextureTypeToString(aiTextureType in) } +// ----------------------------------------------------------------------------------- +// Some chuncks of text will need to be encoded for XML +// http://stackoverflow.com/questions/5665231/most-efficient-way-to-escape-xml-html-in-c-string#5665377 +static std::string encodeXML(const std::string& data) { + std::string buffer; + buffer.reserve(data.size()); + for(size_t pos = 0; pos != data.size(); ++pos) { + switch(data[pos]) { + case '&': buffer.append("&"); break; + case '\"': buffer.append("""); break; + case '\'': buffer.append("'"); break; + case '<': buffer.append("<"); break; + case '>': buffer.append(">"); break; + default: buffer.append(&data[pos], 1); break; + } + } + return buffer; +} + + + // ----------------------------------------------------------------------------------- // Write a text model dump void WriteDump(const aiScene* scene, FILE* out, const char* src, const char* cmd, bool shortened) @@ -1014,7 +1035,7 @@ void WriteDump(const aiScene* scene, FILE* out, const char* src, const char* cmd } } else if (prop->mType == aiPTI_String) { - fprintf(out,">\n\t\t\t\"%s\"",prop->mData+4 /* skip length */); + fprintf(out,">\n\t\t\t\t\"%s\"",encodeXML(prop->mData+4).c_str() /* skip length */); } fprintf(out,"\n\t\t\t\n"); }