diff --git a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp index 8479dbf3d7d..dc1bb6f718e 100644 --- a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp @@ -41,6 +41,7 @@ # include # include # include "Shlwapi.h" // for PathFileExists() +#include #else # include # include @@ -497,6 +498,34 @@ std::string FileSystem::append(const std::string_view& existingPath, const std:: return std::string(existingPath) + "/" + std::string(toAppend); } +bool FileSystem::openFileWithDefaultApplication(const std::string& filename) +{ + bool success = false; + + if (!filename.empty()) + { + if (!FileSystem::exists(filename)) + { + msg_error("FileSystem::openFileWithDefaultApplication()") << "File does not exist: " << filename; + return success; + } + +#ifdef WIN32 + if ((INT_PTR)ShellExecuteA(nullptr, "open", filename.c_str(), nullptr, nullptr, SW_SHOWNORMAL) > 32) + success = true; +#elif defined(__APPLE__) + const std::string command = "open \"" + filename + "\""; + if (std::system(command.c_str()) == 0) + success = true; +#else + const std::string command = "xdg-open \"" + filename + "\""; + if (std::system(command.c_str()) == 0) + success = true; +#endif + } + + return success; +} } // namespace system } // namespace helper diff --git a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.h b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.h index e42e7acb031..ace9bfd9a13 100644 --- a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.h +++ b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.h @@ -172,6 +172,9 @@ static std::string append(const std::string_view& existingPath, const std::strin return append(append(existingPath, toAppend), args...); } +/// Open a file with the default application associated to its type by the OS +static bool openFileWithDefaultApplication(const std::string& filename); + };