From 1cbedce7bf54108d73ffe3c46af5747c39135f8f Mon Sep 17 00:00:00 2001 From: John Lienau Date: Tue, 9 Jul 2024 11:41:20 +0200 Subject: [PATCH] Redundant boolean literal in conditional return statement and code cleanup --- src/hello_imgui/impl/runner_params.cpp | 38 ++++++++++++++------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/hello_imgui/impl/runner_params.cpp b/src/hello_imgui/impl/runner_params.cpp index ceb5acf3..7376e3bc 100644 --- a/src/hello_imgui/impl/runner_params.cpp +++ b/src/hello_imgui/impl/runner_params.cpp @@ -32,44 +32,41 @@ namespace HelloImGui auto _stringToSaneFilename=[](const std::string& s, const std::string& extension) -> std::string { std::string filenameSanitized; + for (char c : s) + { if (isalnum(c)) filenameSanitized += c; else filenameSanitized += "_"; + } + filenameSanitized += extension; return filenameSanitized; }; if (! runnerParams.iniFilename.empty()) return runnerParams.iniFilename; - else - { - if (runnerParams.iniFilename_useAppWindowTitle && !runnerParams.appWindowParams.windowTitle.empty()) - { - std::string iniFilenameSanitized = _stringToSaneFilename(runnerParams.appWindowParams.windowTitle, ".ini"); - return iniFilenameSanitized; - } - else - return "imgui.ini"; - } + + if (runnerParams.iniFilename_useAppWindowTitle && !runnerParams.appWindowParams.windowTitle.empty()) + return _stringToSaneFilename(runnerParams.appWindowParams.windowTitle, ".ini"); + + return "imgui.ini"; + }; auto mkdirToFilename = [](const std::string& filename) -> bool { std::filesystem::path p(filename); std::filesystem::path dir = p.parent_path(); + if (dir.empty()) return true; + if (std::filesystem::exists(dir)) - { - if (std::filesystem::is_directory(dir) || std::filesystem::is_symlink(dir)) - return true; - else - return false; - } - else - return std::filesystem::create_directories(dir); + return std::filesystem::is_directory(dir) || std::filesystem::is_symlink(dir); + + return std::filesystem::create_directories(dir); }; @@ -87,10 +84,13 @@ namespace HelloImGui void DeleteIniSettings(const RunnerParams& runnerParams) { std::string iniFullFilename = IniSettingsLocation(runnerParams); + if (iniFullFilename.empty()) return; + if (!std::filesystem::exists(iniFullFilename)) return; + bool success = std::filesystem::remove(iniFullFilename); IM_ASSERT(success && "Failed to delete ini file %s"); } @@ -99,8 +99,10 @@ namespace HelloImGui bool HasIniSettings(const RunnerParams& runnerParams) { std::string iniFullFilename = IniSettingsLocation(runnerParams); + if (iniFullFilename.empty()) return false; + return std::filesystem::exists(iniFullFilename); }