game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
desktop_entry_utils.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// desktop_entry_utils.cpp
4// ==============================================================================
5// Implementation of XDG Desktop Entry Specification helpers.
6// ==============================================================================
7
9
10namespace desktop_entry
11{
12
13std::string sanitizeField(const std::string& s)
14{
15 std::string out;
16 out.reserve(s.size());
17 for (char raw : s)
18 {
19 auto c = static_cast<unsigned char>(raw);
20 // Reject newlines (break the file format) and other control characters.
21 if (c == '\n' || c == '\r' || c < 0x20u)
22 continue;
23 out += raw;
24 }
25 return out;
26}
27
28std::string escapeExec(const std::string& s)
29{
30 std::string out;
31 out.reserve(s.size() + 2);
32 out.push_back('"');
33 for (char raw : s)
34 {
35 auto c = static_cast<unsigned char>(raw);
36 // Strip control characters first — defence in depth, since `Exec=`
37 // values flow from many sources (game names, prefix paths, custom
38 // arguments) and any embedded newline corrupts the .desktop file.
39 if (c == '\n' || c == '\r' || c < 0x20u)
40 continue;
41 // Reserved chars per the spec that need escaping inside the
42 // double-quoted form: `"`, `\`, `` ` ``, `$`.
43 if (raw == '"' || raw == '\\' || raw == '`' || raw == '$')
44 out.push_back('\\');
45 // Field-code escape: a literal `%` becomes `%%` so the parser
46 // doesn't consume `%` followed by an arbitrary byte as a field code.
47 if (raw == '%')
48 out.push_back('%');
49 out.push_back(raw);
50 }
51 out.push_back('"');
52 return out;
53}
54
55} // namespace desktop_entry
std::string escapeExec(const std::string &s)
std::string sanitizeField(const std::string &s)