(Unix) Improved the .desktop file parser.

This commit is contained in:
Leon Styhre 2022-12-17 18:37:58 +01:00
parent 19d79080a1
commit 4ef3e94c56

View file

@ -27,6 +27,7 @@
#include "views/ViewController.h" #include "views/ViewController.h"
#include <assert.h> #include <assert.h>
#include <regex>
FileData::FileData(FileType type, FileData::FileData(FileType type,
const std::string& path, const std::string& path,
@ -1463,10 +1464,19 @@ void FileData::launchGame()
validFile = true; validFile = true;
if (line.substr(0, 5) == "Exec=") { if (line.substr(0, 5) == "Exec=") {
romPath = {line.substr(5, line.size() - 5)}; romPath = {line.substr(5, line.size() - 5)};
romPath = Utils::String::replace(romPath, "%F", ""); const std::string regexString {"[^%]%"};
romPath = Utils::String::replace(romPath, "%f", ""); // Field codes, some of these are deprecated but may still exist in older
romPath = Utils::String::replace(romPath, "%U", ""); // .desktop files. Any matching codes escaped by double %% characters will
romPath = Utils::String::replace(romPath, "%u", ""); // be left as-is.
std::string codeCharacters {"fFuUdDnNickvm"};
std::smatch searchResult;
while (!codeCharacters.empty()) {
while (std::regex_search(romPath, searchResult,
std::regex(regexString + codeCharacters.back())))
romPath.replace(searchResult.position(0) + 1, searchResult.length() - 1,
"");
codeCharacters.pop_back();
}
romPath = Utils::String::replace(romPath, "%%", "%"); romPath = Utils::String::replace(romPath, "%%", "%");
romPath = Utils::String::trim(romPath); romPath = Utils::String::trim(romPath);
command = Utils::String::replace(command, binaryPath, ""); command = Utils::String::replace(command, binaryPath, "");