From 25b0adfbf9721f14d18350983ee5cd03b67aeea8 Mon Sep 17 00:00:00 2001 From: jackun Date: Wed, 25 Mar 2020 17:08:08 +0200 Subject: [PATCH] Move Wine exe name code from config.cpp to get_wine_exe_name(...) --- src/config.cpp | 23 +++-------------------- src/file_utils.cpp | 27 +++++++++++++++++++++++++++ src/file_utils.h | 1 + 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index 97de021..4ad9bf0 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -50,26 +50,9 @@ void parseConfigFile(overlay_params& params) { // find executable's path when run in Wine if (!env_config.empty() && (basename == "wine-preloader" || basename == "wine64-preloader")) { - std::string line; - std::ifstream stream("/proc/self/cmdline"); - while (std::getline(stream, line, '\0')) - { - if (!line.empty() - && ((n = line.find_last_of("/\\")) != std::string::npos) - && n < line.size() - 1) // have at least one character - { - auto dot = line.find_last_of('.'); - if (dot < n) - dot = line.size(); - paths.push_back(env_config + mangohud_dir + "wine-" + line.substr(n + 1, dot - n - 1) + ".conf"); - break; - } - else if (ends_with(line, ".exe", true)) - { - auto dot = line.find_last_of('.'); - paths.push_back(env_config + mangohud_dir + "wine-" + line.substr(0, dot) + ".conf"); - break; - } + std::string name; + if (get_wine_exe_name(name)) { + paths.push_back(env_config + mangohud_dir + "wine-" + name + ".conf"); } } } diff --git a/src/file_utils.cpp b/src/file_utils.cpp index 1459dce..17cf2d7 100644 --- a/src/file_utils.cpp +++ b/src/file_utils.cpp @@ -106,6 +106,33 @@ std::string get_exe_path() return std::string(result, (count > 0) ? count : 0); } +bool get_wine_exe_name(std::string& name, bool keep_ext) +{ + std::string line; + std::ifstream cmdline("/proc/self/cmdline"); + auto n = std::string::npos; + while (std::getline(cmdline, line, '\0')) + { + if (!line.empty() + && ((n = line.find_last_of("/\\")) != std::string::npos) + && n < line.size() - 1) // have at least one character + { + auto dot = keep_ext ? std::string::npos : line.find_last_of('.'); + if (dot < n) + dot = line.size(); + name = line.substr(n + 1, dot - n - 1); + return true; + } + else if (ends_with(line, ".exe", true)) + { + auto dot = keep_ext ? std::string::npos : line.find_last_of('.'); + name = line.substr(0, dot); + return true; + } + } + return false; +} + std::string get_home_dir() { std::string path; diff --git a/src/file_utils.h b/src/file_utils.h index eada2fb..9b845d8 100644 --- a/src/file_utils.h +++ b/src/file_utils.h @@ -16,6 +16,7 @@ std::vector ls(const char* root, const char* prefix = nullptr, LS_F bool file_exists(const std::string& path); bool dir_exists(const std::string& path); std::string get_exe_path(); +bool get_wine_exe_name(std::string& name, bool keep_ext = false); std::string get_home_dir(); std::string get_data_dir(); std::string get_config_dir();