From 3d7e7870c1c361215a23c8a055e35ddd26f34ac6 Mon Sep 17 00:00:00 2001 From: talregev Date: Sat, 22 Jul 2023 11:27:21 +0300 Subject: [PATCH] Set HOME_ENV according the OS (#3334) For windows is USERPROFILE for other is HOME --- gazebo/common/CommonIface.hh | 6 ++++++ gazebo/common/Console.cc | 7 ++++--- gazebo/common/ModelDatabase.cc | 3 ++- gazebo/common/SystemPaths.cc | 3 ++- gazebo/gui/DataLogger.cc | 6 +----- gazebo/gui/GuiIface.cc | 6 ------ gazebo/gui/MainWindow.cc | 5 +++-- gazebo/gui/SaveEntityDialog.cc | 3 ++- gazebo/rendering/Camera.cc | 3 ++- gazebo/util/Diagnostics.cc | 8 ++------ gazebo/util/LogRecord.cc | 11 ++++------- gazebo/util/LogRecord_TEST.cc | 6 +----- test/integration/plugin.cc | 7 ++++--- test/integration/sdf_errors.cc | 6 +----- 14 files changed, 34 insertions(+), 46 deletions(-) diff --git a/gazebo/common/CommonIface.hh b/gazebo/common/CommonIface.hh index cc990d8a14..82606a8528 100644 --- a/gazebo/common/CommonIface.hh +++ b/gazebo/common/CommonIface.hh @@ -85,6 +85,12 @@ namespace gazebo GZ_COMMON_VISIBLE const char *getEnv(const char *_name); +#ifdef _WIN32 + #define HOMEDIR "USERPROFILE" +#else + #define HOMEDIR "HOME" +#endif // _WIN32 + /// \brief Get the current working directory /// \return Name of the current directory GZ_COMMON_VISIBLE diff --git a/gazebo/common/Console.cc b/gazebo/common/Console.cc index 5bda931a05..6b86bc2d02 100644 --- a/gazebo/common/Console.cc +++ b/gazebo/common/Console.cc @@ -23,6 +23,7 @@ #include "gazebo/common/Exception.hh" #include "gazebo/common/Time.hh" #include "gazebo/common/Console.hh" +#include "gazebo/common/CommonIface.hh" #include "gazebo/gazebo_config.h" @@ -155,9 +156,9 @@ FileLogger::~FileLogger() ///////////////////////////////////////////////// void FileLogger::Init(const std::string &_prefix, const std::string &_filename) { - if (!getenv("HOME")) + if (!getenv(HOMEDIR)) { - gzerr << "Missing HOME environment variable." + gzerr << "Missing " << HOMEDIR << " environment variable." << "No log file will be generated."; return; } @@ -165,7 +166,7 @@ void FileLogger::Init(const std::string &_prefix, const std::string &_filename) FileLogger::Buffer *buf = static_cast( this->rdbuf()); - boost::filesystem::path logPath(getenv("HOME")); + boost::filesystem::path logPath(getenv(HOMEDIR)); // Create a subdirectory for the informational log. The name of the directory // will be . E.g.: server-11346. If the environment diff --git a/gazebo/common/ModelDatabase.cc b/gazebo/common/ModelDatabase.cc index 4e46add362..c434a1cdf3 100644 --- a/gazebo/common/ModelDatabase.cc +++ b/gazebo/common/ModelDatabase.cc @@ -40,6 +40,7 @@ #include "gazebo/common/ModelDatabasePrivate.hh" #include "gazebo/common/ModelDatabase.hh" #include "gazebo/common/SemanticVersion.hh" +#include "gazebo/common/CommonIface.hh" using namespace gazebo; using namespace common; @@ -503,7 +504,7 @@ std::string ModelDatabase::GetModelPath(const std::string &_uri, continue; } - std::string outputPath = getenv("HOME"); + std::string outputPath = getenv(HOMEDIR); outputPath += "/.gazebo/models"; #ifndef _WIN32 diff --git a/gazebo/common/SystemPaths.cc b/gazebo/common/SystemPaths.cc index 08e84f234e..788628eac7 100644 --- a/gazebo/common/SystemPaths.cc +++ b/gazebo/common/SystemPaths.cc @@ -38,6 +38,7 @@ #include "gazebo/common/Exception.hh" #include "gazebo/common/ModelDatabase.hh" #include "gazebo/common/SystemPaths.hh" +#include "gazebo/common/CommonIface.hh" using namespace gazebo; using namespace common; @@ -81,7 +82,7 @@ SystemPaths::SystemPaths() return; } - char *homePath = getenv("HOME"); + char *homePath = getenv(HOMEDIR); std::string home; if (!homePath) home = this->TmpPath() + "/gazebo"; diff --git a/gazebo/gui/DataLogger.cc b/gazebo/gui/DataLogger.cc index e25cb0157c..40f9eb32b9 100644 --- a/gazebo/gui/DataLogger.cc +++ b/gazebo/gui/DataLogger.cc @@ -254,11 +254,7 @@ DataLogger::DataLogger(QWidget *_parent) "~/log/status", &DataLogger::OnStatus, this); // Fill the path with the home folder - duplicated from util/LogRecord -#ifndef _WIN32 - const char *homePath = common::getEnv("HOME"); -#else - const char *homePath = common::getEnv("HOMEPATH"); -#endif + const char *homePath = common::getEnv(HOMEDIR); GZ_ASSERT(homePath, "HOME environment variable is missing"); diff --git a/gazebo/gui/GuiIface.cc b/gazebo/gui/GuiIface.cc index 018e3a6f23..1d6c03ed8a 100644 --- a/gazebo/gui/GuiIface.cc +++ b/gazebo/gui/GuiIface.cc @@ -43,12 +43,6 @@ #include "gazebo/gui/GuiPlugin.hh" #include "gazebo/gui/RenderWidget.hh" -#ifdef WIN32 -# define HOMEDIR "HOMEPATH" -#else -# define HOMEDIR "HOME" -#endif // WIN32 - // These are needed by QT. They need to stay valid during the entire // lifetime of the application, and argc > 0 and argv must contain one valid // character string diff --git a/gazebo/gui/MainWindow.cc b/gazebo/gui/MainWindow.cc index 9cd24dde47..1cc8681157 100644 --- a/gazebo/gui/MainWindow.cc +++ b/gazebo/gui/MainWindow.cc @@ -27,6 +27,7 @@ #include "gazebo/common/Console.hh" #include "gazebo/common/Events.hh" #include "gazebo/common/Exception.hh" +#include "gazebo/common/CommonIface.hh" #include "gazebo/msgs/msgs.hh" @@ -463,10 +464,10 @@ void MainWindow::Open() ///////////////////////////////////////////////// void MainWindow::SaveINI() { - char *home = getenv("HOME"); + char *home = getenv(HOMEDIR); if (!home) { - gzerr << "HOME environment variable not found. " + gzerr << HOMEDIR << " environment variable not found. " "Unable to save configuration file\n"; return; } diff --git a/gazebo/gui/SaveEntityDialog.cc b/gazebo/gui/SaveEntityDialog.cc index 296cf10fb7..66e49262aa 100644 --- a/gazebo/gui/SaveEntityDialog.cc +++ b/gazebo/gui/SaveEntityDialog.cc @@ -19,6 +19,7 @@ #include "gazebo/common/SystemPaths.hh" #include "gazebo/common/Console.hh" +#include "gazebo/common/CommonIface.hh" #include "gazebo/gui/GuiIface.hh" #include "gazebo/gui/SaveEntityDialog.hh" @@ -408,7 +409,7 @@ void SaveEntityDialog::AddDirToModelPaths(const std::string &_path) // Save any changes that were made to the property tree // TODO: check gui.ini env variable - char *home = getenv("HOME"); + char *home = getenv(HOMEDIR); if (home) { boost::filesystem::path guiINIPath = home; diff --git a/gazebo/rendering/Camera.cc b/gazebo/rendering/Camera.cc index 8b6e717e7e..246868298b 100644 --- a/gazebo/rendering/Camera.cc +++ b/gazebo/rendering/Camera.cc @@ -44,6 +44,7 @@ #include "gazebo/common/Console.hh" #include "gazebo/common/Exception.hh" #include "gazebo/common/VideoEncoder.hh" +#include "gazebo/common/CommonIface.hh" #include "gazebo/rendering/ogre_gazebo.h" #include "gazebo/rendering/RTShaderSystem.hh" @@ -101,7 +102,7 @@ Camera::Camera(const std::string &_name, ScenePtr _scene, this->sceneNode = NULL; - this->screenshotPath = getenv("HOME"); + this->screenshotPath = getenv(HOMEDIR); this->screenshotPath += "/.gazebo/pictures"; // Connect to the render signal diff --git a/gazebo/util/Diagnostics.cc b/gazebo/util/Diagnostics.cc index 55626b0fc1..a3bbe76f28 100644 --- a/gazebo/util/Diagnostics.cc +++ b/gazebo/util/Diagnostics.cc @@ -32,18 +32,14 @@ using namespace gazebo::util; DiagnosticManager::DiagnosticManager() : dataPtr(new DiagnosticManagerPrivate) { -#ifndef _WIN32 - const char *homePath = common::getEnv("HOME"); -#else - const char *homePath = common::getEnv("HOMEPATH"); -#endif + const char *homePath = common::getEnv(HOMEDIR); this->dataPtr->logPath = homePath; // Get the base of the time logging path if (!homePath) { common::SystemPaths *paths = common::SystemPaths::Instance(); - gzwarn << "HOME environment variable missing. Diagnostic timing " << + gzwarn << HOMEDIR << " environment variable missing. Diagnostic timing " << "information will be logged to " << paths->TmpPath() << "\n"; this->dataPtr->logPath = paths->TmpPath() + "/gazebo"; } diff --git a/gazebo/util/LogRecord.cc b/gazebo/util/LogRecord.cc index 19658f54a4..73eae372d0 100644 --- a/gazebo/util/LogRecord.cc +++ b/gazebo/util/LogRecord.cc @@ -71,13 +71,10 @@ LogRecord::LogRecord() this->dataPtr->readyToStart = false; // Get the user's home directory -#ifndef _WIN32 - const char *homePath = common::getEnv("HOME"); -#else - const char *homePath = common::getEnv("HOMEPATH"); -#endif - - GZ_ASSERT(homePath, "HOME environment variable is missing"); + const char *homePath = common::getEnv(HOMEDIR); + std::string home_warning = HOMEDIR; + home_warning += " environment variable is missing"; + GZ_ASSERT(homePath, home_warning.c_str()); if (!homePath) { diff --git a/gazebo/util/LogRecord_TEST.cc b/gazebo/util/LogRecord_TEST.cc index 7448c2bfcf..70912ac25f 100644 --- a/gazebo/util/LogRecord_TEST.cc +++ b/gazebo/util/LogRecord_TEST.cc @@ -33,11 +33,7 @@ TEST_F(LogRecord_TEST, Constructor) { gazebo::util::LogRecord *recorder = gazebo::util::LogRecord::Instance(); -#ifndef _WIN32 - const char *homePath = common::getEnv("HOME"); -#else - const char *homePath = common::getEnv("HOMEPATH"); -#endif + const char *homePath = common::getEnv(HOMEDIR); EXPECT_TRUE(homePath != NULL); diff --git a/test/integration/plugin.cc b/test/integration/plugin.cc index 19f605e535..e4240da9a6 100644 --- a/test/integration/plugin.cc +++ b/test/integration/plugin.cc @@ -17,6 +17,7 @@ #include #include "gazebo/test/ServerFixture.hh" +#include "gazebo/common/CommonIface.hh" using namespace gazebo; class PluginTest : public ServerFixture @@ -36,7 +37,7 @@ TEST_F(PluginTest, ModelExceptionConstructor) world->Step(100); - char *home = getenv("HOME"); + char *home = getenv(HOMEDIR); ASSERT_TRUE(home); boost::filesystem::path path(home); @@ -71,7 +72,7 @@ TEST_F(PluginTest, ModelExceptionInit) world->Step(100); - char *home = getenv("HOME"); + char *home = getenv(HOMEDIR); ASSERT_TRUE(home); boost::filesystem::path path(home); @@ -106,7 +107,7 @@ TEST_F(PluginTest, ModelExceptionLoad) world->Step(100); - char *home = getenv("HOME"); + char *home = getenv(HOMEDIR); ASSERT_TRUE(home); boost::filesystem::path path(home); diff --git a/test/integration/sdf_errors.cc b/test/integration/sdf_errors.cc index c1bce2b223..aa4e322e09 100644 --- a/test/integration/sdf_errors.cc +++ b/test/integration/sdf_errors.cc @@ -40,11 +40,7 @@ class SDFLogsTest : public ServerFixture { public: void SetUp() { -#ifndef _WIN32 - const boost::filesystem::path home = common::getEnv("HOME"); -#else - const boost::filesystem::path home = common::getEnv("HOMEPATH"); -#endif + const boost::filesystem::path home = common::getEnv(HOMEDIR); boost::filesystem::path log_path("/.gazebo/server-11345/default.log"); path = home / log_path; }