Added encoding of special characters when writing

addons_installed.xml.


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@9511 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk 2011-08-15 23:32:18 +00:00
parent 5eff0a1773
commit c9057d6e74
3 changed files with 45 additions and 13 deletions

View File

@ -61,7 +61,7 @@ Addon::Addon(const XMLNode &xml)
std::string designer;
xml.get("name", &name );
m_name = StringUtils::removeHtmlEntities(name);
m_name = StringUtils::decodeFromHtmlEntities(name);
m_id = StringUtils::toLowerCase(name);
xml.get("id", &m_id );
xml.get("designer", &designer );
@ -73,8 +73,8 @@ Addon::Addon(const XMLNode &xml)
xml.get("file", &m_zip_file );
xml.get("description", &description );
m_description = StringUtils::removeHtmlEntities(description);
m_designer = StringUtils::removeHtmlEntities(designer);
m_description = StringUtils::decodeFromHtmlEntities(description);
m_designer = StringUtils::decodeFromHtmlEntities(designer);
// resolve XML entities
//m_description = StringUtils::replace(m_description, "
", "\n");
@ -114,9 +114,11 @@ void Addon::copyInstallData(const Addon &addon)
void Addon::writeXML(std::ofstream *out_stream)
{
(*out_stream) << " <" << m_type
<< " name=\"" << core::stringc(m_name).c_str()
<< " name=\""
<< StringUtils::encodeToHtmlEntities(m_name)
<< "\" id=\"" << m_id
<< "\" designer=\"" << core::stringc(m_designer).c_str()
<< "\" designer=\""
<< StringUtils::encodeToHtmlEntities(m_designer)
<< "\" status=\"" << m_status
<< "\" date=\"" << m_date
<< "\" installed=\""

View File

@ -475,8 +475,11 @@ namespace StringUtils
}
// ------------------------------------------------------------------------
irr::core::stringw removeHtmlEntities(const std::string& input)
/** Converts ASCII text with HTML entities (e.g. &xE9;) 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 output;
std::string entity;
@ -555,7 +558,35 @@ namespace StringUtils
}
return output;
}
} // decodeFromHtmlEntities
// ------------------------------------------------------------------------
/** Converts a unicode string to plain ASCII using html-like & codes.
* \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::ostringstream output;
for(unsigned int i=0; i<s.size(); i++)
{
if(s[i]=='&')
output<<"&amp;";
else
{
if(s[i]<128)
{
irr::c8 c=(char)(s[i]);
output<<c;
}
else
{
output <<"&#" << std::hex <<std::uppercase<< s[i]<<";";
}
}
}
return output.str();
} // encodeToHtmlEntities
} // namespace StringUtils

View File

@ -351,10 +351,9 @@ namespace StringUtils
*/
std::string replace(const std::string& other, const std::string& from, const std::string& to);
/**
* Converts ASCII text with HTML entities (e.g. &xE9;) to unicode string
*/
irr::core::stringw removeHtmlEntities(const std::string& input);
irr::core::stringw decodeFromHtmlEntities(const std::string& input);
std::string encodeToHtmlEntities(const irr::core::stringw &output);
} // namespace StringUtils