Files
WiVRn/dashboard/escape_string.cpp
T
2025-02-02 17:08:58 +01:00

108 lines
2.2 KiB
C++

/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "escape_string.h"
QString escape_string(const std::vector<std::string> & app)
{
std::string_view escaped_chars = R"( '"\)";
std::string app_string;
for (const std::string & i: app)
{
for (char c: i)
{
if (not escaped_chars.contains(c))
{
app_string += c;
}
else
{
app_string += '\\';
app_string += c;
}
}
app_string += ' ';
}
app_string.resize(app_string.size() - 1);
return QString::fromStdString(app_string);
}
std::vector<std::string> unescape_string(const QString & app_string)
{
std::vector<std::string> app;
app.emplace_back();
bool seen_backslash = false;
bool seen_single_quote = false;
bool seen_double_quote = false;
for (auto c: app_string.toStdString())
{
if (seen_backslash)
{
app.back() += c;
seen_backslash = false;
}
else if (seen_single_quote)
{
if (c == '\'')
seen_single_quote = false;
else if (c == '\\')
seen_backslash = true;
else
app.back() += c;
}
else if (seen_double_quote)
{
if (c == '"')
seen_double_quote = false;
else if (c == '\\')
seen_backslash = true;
else
app.back() += c;
}
else
{
switch (c)
{
case '\\':
seen_backslash = true;
break;
case '\'':
seen_single_quote = true;
break;
case '"':
seen_double_quote = true;
break;
case ' ':
if (app.back() != "")
app.emplace_back();
break;
default:
app.back() += c;
}
}
}
if (app.back() == "")
app.pop_back();
return app;
}