Avoid outputting non-printable-ASCII characters in the network log

This was SVN commit r10433.
This commit is contained in:
Ykkrosh 2011-10-27 14:16:28 +00:00
parent a2bba82b9d
commit dfc92de51b
3 changed files with 33 additions and 3 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2010 Wildfire Games.
/* Copyright (C) 2011 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -27,12 +27,12 @@ static inline CStr NetMessageStringConvert(u32 arg)
static inline CStr NetMessageStringConvert(const CStr8& arg)
{
return arg;
return arg.EscapeToPrintableASCII();
}
static inline CStr NetMessageStringConvert(const CStrW& arg)
{
return arg.ToUTF8();
return arg.ToUTF8().EscapeToPrintableASCII();
}
#endif

View File

@ -356,6 +356,31 @@ CStr CStr::UnescapeBackslashes() const
return NewString;
}
CStr CStr::EscapeToPrintableASCII() const
{
CStr NewString;
for (size_t i = 0; i < length(); i++)
{
tchar ch = (*this)[i];
if (ch == '"')
NewString += _T("\\\"");
else if (ch == '\\')
NewString += _T("\\\\");
else if (ch == '\n')
NewString += _T("\\n");
else if (ch >= 32 && ch <= 126)
NewString += ch;
else
{
std::tstringstream ss;
ss << _T("\\x") << std::hex << std::setfill(_T('0')) << std::setw(2) << (int)(unsigned char)ch;
NewString += ss.str();
}
}
return NewString;
}
// Returns a trimmed string, removes whitespace from the left/right/both
CStr CStr::Trim(PS_TRIM_MODE Mode) const
{

View File

@ -310,6 +310,11 @@ public:
**/
CStr UnescapeBackslashes() const;
/**
* Convert strings to printable ASCII characters with C-style escapes
*/
CStr EscapeToPrintableASCII() const;
/**
* Return a trimmed copy of the CStr.
*