COMMON: Add an optional parameter to tag2str() for printing non-printables

This commit is contained in:
Eugene Sandulenko 2021-06-29 20:41:56 +02:00
parent d3f2402669
commit c11fedb47e
No known key found for this signature in database
GPG key ID: 014D387312D34F08
2 changed files with 22 additions and 14 deletions

View file

@ -689,19 +689,24 @@ void replace(Common::String &source, const Common::String &what, const Common::S
} }
} }
String tag2string(uint32 tag) { String tag2string(uint32 tag, bool nonPrintable) {
char str[5]; Common::String res;
str[0] = (char)(tag >> 24);
str[1] = (char)(tag >> 16); for (int i = 3; i >= 0; i--) {
str[2] = (char)(tag >> 8); byte b = (tag >> (8 * i)) & 0xff;
str[3] = (char)tag;
str[4] = '\0'; if (!Common::isPrint(b)) {
// Replace non-printable chars by dot if (nonPrintable) {
for (int i = 0; i < 4; ++i) { res += Common::String::format("\\%03o", b);
if (!Common::isPrint(str[i])) } else {
str[i] = '.'; res += '.';
}
} else {
res += b;
}
} }
return String(str);
return res;
} }
#endif #endif

View file

@ -349,8 +349,11 @@ void replace(Common::String &source, const Common::String &what, const Common::S
* Take a 32 bit value and turn it into a four character string, where each of * Take a 32 bit value and turn it into a four character string, where each of
* the four bytes is turned into one character. Most significant byte is printed * the four bytes is turned into one character. Most significant byte is printed
* first. * first.
*
* @param tag tag value to convert
* @param nonPrintable indicate if non-printable characters need to be printed as octals
*/ */
String tag2string(uint32 tag); String tag2string(uint32 tag, bool nonPrintable = false);
/** /**
* Copy up to size - 1 characters from src to dst and also zero terminate the * Copy up to size - 1 characters from src to dst and also zero terminate the
@ -405,7 +408,7 @@ size_t strnlen(const char *src, size_t maxSize);
* Note: It is *NOT* safe to do anything with the return value other than directly * Note: It is *NOT* safe to do anything with the return value other than directly
* copying or printing it. * copying or printing it.
*/ */
#define tag2str(x) Common::tag2string(x).c_str() #define tag2str(...) Common::tag2string(__VA_ARGS__).c_str()
/** /**
* Converts string with all non-printable characters properly escaped * Converts string with all non-printable characters properly escaped