Move XMML and URL escape functions to common/utils

So we don't need to include notifications.h on the Wine side. The
alternative would be to put ifdef guards around everything involving
notification sending in `notifications.cpp` but that would be even more
problematic.
This commit is contained in:
Robbert van der Helm
2022-10-28 17:52:23 +02:00
parent 91832e5c0f
commit b58eca9ed1
6 changed files with 123 additions and 123 deletions
-109
View File
@@ -168,112 +168,3 @@ bool send_notification(const std::string& title,
},
result);
}
std::string xml_escape(std::string string) {
// Implementation idea stolen from https://stackoverflow.com/a/5665377
std::string escaped;
escaped.reserve(
static_cast<size_t>(static_cast<double>(string.size()) * 1.1));
for (const char& character : string) {
switch (character) {
case '&':
escaped.append("&amp;");
break;
case '\"':
escaped.append("&quot;");
break;
case '\'':
escaped.append("&apos;");
break;
case '<':
escaped.append("&lt;");
break;
case '>':
escaped.append("&gt;");
break;
default:
escaped.push_back(character);
break;
}
}
return escaped;
}
std::string url_encode_path(std::string path) {
// We only need to escape a couple of special characters here. This is used
// in the notifications as well as in the XDND proxy. We encode the reserved
// characters mentioned here, with the exception of the forward slash:
// https://en.wikipedia.org/wiki/Percent-encoding#Reserved_characters
std::string escaped;
escaped.reserve(
static_cast<size_t>(static_cast<double>(path.size()) * 1.1));
for (const char& character : path) {
switch (character) {
// Spaces are somehow in the above list, but Bitwig Studio requires
// spaces to be escaped in the `text/uri-list` format
case ' ':
escaped.append("%20");
break;
case '!':
escaped.append("%21");
break;
case '#':
escaped.append("%23");
break;
case '$':
escaped.append("%24");
break;
case '%':
escaped.append("%25");
break;
case '&':
escaped.append("%26");
break;
case '\'':
escaped.append("%27");
break;
case '(':
escaped.append("%28");
break;
case ')':
escaped.append("%29");
break;
case '*':
escaped.append("%2A");
break;
case '+':
escaped.append("%2B");
break;
case ',':
escaped.append("%2C");
break;
case ':':
escaped.append("%3A");
break;
case ';':
escaped.append("%3B");
break;
case '=':
escaped.append("%3D");
break;
case '?':
escaped.append("%3F");
break;
case '@':
escaped.append("%40");
break;
case '[':
escaped.append("%5B");
break;
case ']':
escaped.append("%5D");
break;
default:
escaped.push_back(character);
break;
}
}
return escaped;
}