From e6aea6e0b676956d92e62aabfe0a728271020542 Mon Sep 17 00:00:00 2001 From: Marianne Gagnon Date: Sat, 14 Feb 2015 18:30:23 -0500 Subject: [PATCH] Improve tools and usage of XML encoding, fixes #1982 --- src/addons/addon.cpp | 10 +++++----- src/audio/music_information.cpp | 4 ++-- src/config/player_manager.cpp | 4 ++-- src/config/player_profile.cpp | 4 ++-- src/io/xml_node.cpp | 11 +++++++++++ src/io/xml_node.hpp | 1 + src/online/server.cpp | 2 +- src/tracks/track.cpp | 2 +- src/utils/string_utils.cpp | 29 ++++++++++++----------------- src/utils/string_utils.hpp | 4 ++-- 10 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/addons/addon.cpp b/src/addons/addon.cpp index 052d532a1..6f17829a9 100644 --- a/src/addons/addon.cpp +++ b/src/addons/addon.cpp @@ -66,7 +66,7 @@ Addon::Addon(const XMLNode &xml) std::string designer; xml.get("name", &name ); - m_name = StringUtils::decodeFromHtmlEntities(name); + m_name = StringUtils::xmlDecode(name); m_dir_name = StringUtils::toLowerCase(name); xml.get("id", &m_dir_name ); m_id = createAddonId(m_dir_name); @@ -83,8 +83,8 @@ Addon::Addon(const XMLNode &xml) xml.get("file", &m_zip_file ); xml.get("description", &description ); - m_description = StringUtils::decodeFromHtmlEntities(description); - m_designer = StringUtils::decodeFromHtmlEntities(designer); + m_description = StringUtils::xmlDecode(description); + m_designer = StringUtils::xmlDecode(designer); // resolve XML entities //m_description = StringUtils::replace(m_description, " ", "\n"); @@ -144,10 +144,10 @@ void Addon::writeXML(std::ofstream *out_stream) // We write m_dir_name as 'id' to stay backwards compatible (*out_stream) << " <" << m_type << " name=\"" - << StringUtils::encodeToHtmlEntities(m_name) + << StringUtils::xmlEncode(m_name) << "\" id=\"" << m_dir_name << "\" designer=\"" - << StringUtils::encodeToHtmlEntities(m_designer) + << StringUtils::xmlEncode(m_designer) << "\" status=\"" << m_status << "\" date=\"" << m_date << "\" installed=\"" diff --git a/src/audio/music_information.cpp b/src/audio/music_information.cpp index 096e85c96..c18a8ba01 100644 --- a/src/audio/music_information.cpp +++ b/src/audio/music_information.cpp @@ -91,9 +91,9 @@ MusicInformation::MusicInformation(const XMLNode *root, // -------------------------- std::string s; root->get("title", &s ); - m_title = StringUtils::decodeFromHtmlEntities(s); + m_title = StringUtils::xmlDecode(s); root->get("composer", &s ); - m_composer = StringUtils::decodeFromHtmlEntities(s); + m_composer = StringUtils::xmlDecode(s); root->get("file", &m_normal_filename); root->get("gain", &m_gain ); root->get("tracks", &m_all_tracks ); diff --git a/src/config/player_manager.cpp b/src/config/player_manager.cpp index c7025ad83..f1b84a52c 100644 --- a/src/config/player_manager.cpp +++ b/src/config/player_manager.cpp @@ -214,7 +214,7 @@ void PlayerManager::load() if(current) { stringw name; - current->get("player", &name); + current->getAndDecode("player", &name); m_current_player = getPlayer(name); } @@ -269,7 +269,7 @@ void PlayerManager::save() if(m_current_player) { players_file << L" getName() << L"\"/>\n"; + << StringUtils::xmlEncode(m_current_player->getName()) << L"\"/>\n"; } // Save all non-guest players diff --git a/src/config/player_profile.cpp b/src/config/player_profile.cpp index dca58f8ae..e5df0c1ba 100644 --- a/src/config/player_profile.cpp +++ b/src/config/player_profile.cpp @@ -78,7 +78,7 @@ PlayerProfile::PlayerProfile(const XMLNode* node) m_achievements_status = NULL; m_icon_filename = ""; - node->get("name", &m_local_name ); + node->getAndDecode("name", &m_local_name); node->get("guest", &m_is_guest_account ); node->get("use-frequency", &m_use_frequency ); node->get("unique-id", &m_unique_id ); @@ -196,7 +196,7 @@ const std::string PlayerProfile::getIconFilename() const */ void PlayerProfile::save(UTFWriter &out) { - out << L" ::const_iterator o; + o = m_attributes.find(attribute); + if (o == m_attributes.end()) return 0; + std::string raw_value = core::stringc(o->second).c_str(); + *value = StringUtils::xmlDecode(raw_value); + return 1; +} // get +// ---------------------------------------------------------------------------- int XMLNode::get(const std::string &attribute, core::vector2df *value) const { std::string s = ""; diff --git a/src/io/xml_node.hpp b/src/io/xml_node.hpp index fb1bbaf47..675e68b0c 100644 --- a/src/io/xml_node.hpp +++ b/src/io/xml_node.hpp @@ -73,6 +73,7 @@ public: unsigned int getNumNodes() const {return (unsigned int) m_nodes.size(); } int get(const std::string &attribute, std::string *value) const; int get(const std::string &attribute, core::stringw *value) const; + int getAndDecode(const std::string &attribute, core::stringw *value) const; int get(const std::string &attribute, int32_t *value) const; int get(const std::string &attribute, uint16_t *value) const; int get(const std::string &attribute, uint32_t *value) const; diff --git a/src/online/server.cpp b/src/online/server.cpp index db72dad4c..f716da58a 100644 --- a/src/online/server.cpp +++ b/src/online/server.cpp @@ -39,7 +39,7 @@ namespace Online m_max_players = 0; xml.get("name", &m_lower_case_name); - m_name = StringUtils::decodeFromHtmlEntities(m_lower_case_name); + m_name = StringUtils::xmlDecode(m_lower_case_name); m_lower_case_name = StringUtils::toLowerCase(m_lower_case_name); xml.get("id", &m_server_id); diff --git a/src/tracks/track.cpp b/src/tracks/track.cpp index ed96aa286..36f567a00 100644 --- a/src/tracks/track.cpp +++ b/src/tracks/track.cpp @@ -480,7 +480,7 @@ void Track::loadTrackInfo() std::string designer; root->get("designer", &designer); - m_designer = StringUtils::decodeFromHtmlEntities(designer); + m_designer = StringUtils::xmlDecode(designer); root->get("version", &m_version); std::vector filenames; diff --git a/src/utils/string_utils.cpp b/src/utils/string_utils.cpp index 8d1966308..3aa6f2167 100644 --- a/src/utils/string_utils.cpp +++ b/src/utils/string_utils.cpp @@ -574,11 +574,11 @@ namespace StringUtils } // ------------------------------------------------------------------------ - /** Converts ASCII text with HTML entities (e.g. &xE9;) to unicode strings + /** Converts ASCII text with XML entities (e.g. &x00;) to unicode strings * \param input The input string which should be decoded. * \return A irrlicht wide string with unicode characters. */ - irr::core::stringw decodeFromHtmlEntities(const std::string& input) + irr::core::stringw xmlDecode(const std::string& input) { irr::core::stringw output; std::string entity; @@ -659,35 +659,30 @@ namespace StringUtils } return output; - } // decodeFromHtmlEntities + } // xmlDecode // ------------------------------------------------------------------------ - /** Converts a unicode string to plain ASCII using html-like & codes. + /** Converts a unicode string to plain ASCII using XML entites (e.g. &x00;) * \param s The input string which should be encoded. * \return A std:;string with ASCII characters. */ - std::string encodeToHtmlEntities(const irr::core::stringw &s) + std::string xmlEncode(const irr::core::stringw &s) { std::ostringstream output; for(unsigned int i=0; i= 128 || s[i] == '&' || s[i] == '<' || s[i] == '>' || s[i] == '\"') + { + output << "&#x" << std::hex << std::uppercase << s[i] << ";"; + } else { - if(s[i]<128) - { - irr::c8 c=(char)(s[i]); - output< splitPath(const std::string& path); std::string replace(const std::string& other, const std::string& from, const std::string& to); - irr::core::stringw decodeFromHtmlEntities(const std::string& input); + irr::core::stringw xmlDecode(const std::string& input); - std::string encodeToHtmlEntities(const irr::core::stringw &output); + std::string xmlEncode(const irr::core::stringw &output); // ------------------------------------------------------------------------ template