Skip to content

Commit

Permalink
Set HOME_ENV according the OS. for windows is USERPROFILE for other i…
Browse files Browse the repository at this point in the history
…s HOME
  • Loading branch information
talregev committed Jun 24, 2023
1 parent 17e09f5 commit 79e69ac
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 16 deletions.
7 changes: 7 additions & 0 deletions gazebo/common/CommonIface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ const char *common::getEnv(const char *_name)
#endif
}

/////////////////////////////////////////////////
#ifdef _WIN32
std::string common::HOME_ENV = "USERPROFILE";
#else
std::string common::HOME_ENV = "HOME";
#endif

/////////////////////////////////////////////////
std::vector<std::string> common::split(const std::string &_str,
const std::string &_delim)
Expand Down
2 changes: 2 additions & 0 deletions gazebo/common/CommonIface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ namespace gazebo
GZ_COMMON_VISIBLE
const char *getEnv(const char *_name);

extern std::string HOME_ENV;

/// \brief Get the current working directory
/// \return Name of the current directory
GZ_COMMON_VISIBLE
Expand Down
5 changes: 3 additions & 2 deletions gazebo/common/Console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -155,7 +156,7 @@ FileLogger::~FileLogger()
/////////////////////////////////////////////////
void FileLogger::Init(const std::string &_prefix, const std::string &_filename)
{
if (!getenv("HOME"))
if (!getenv(HOME_ENV.c_str()))
{
gzerr << "Missing HOME environment variable."
<< "No log file will be generated.";
Expand All @@ -165,7 +166,7 @@ void FileLogger::Init(const std::string &_prefix, const std::string &_filename)
FileLogger::Buffer *buf = static_cast<FileLogger::Buffer*>(
this->rdbuf());

boost::filesystem::path logPath(getenv("HOME"));
boost::filesystem::path logPath(getenv(HOME_ENV.c_str()));

// Create a subdirectory for the informational log. The name of the directory
// will be <PREFIX><MASTER_PORT>. E.g.: server-11346. If the environment
Expand Down
3 changes: 2 additions & 1 deletion gazebo/common/ModelDatabase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -503,7 +504,7 @@ std::string ModelDatabase::GetModelPath(const std::string &_uri,
continue;
}

std::string outputPath = getenv("HOME");
std::string outputPath = getenv(HOME_ENV.c_str());
outputPath += "/.gazebo/models";

#ifndef _WIN32
Expand Down
3 changes: 2 additions & 1 deletion gazebo/common/SystemPaths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,7 +82,7 @@ SystemPaths::SystemPaths()
return;
}

char *homePath = getenv("HOME");
char *homePath = getenv(HOME_ENV.c_str());
std::string home;
if (!homePath)
home = this->TmpPath() + "/gazebo";
Expand Down
2 changes: 1 addition & 1 deletion gazebo/gui/DataLogger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ DataLogger::DataLogger(QWidget *_parent)
#ifndef _WIN32
const char *homePath = common::getEnv("HOME");
#else
const char *homePath = common::getEnv("HOMEPATH");
const char *homePath = common::getEnv("USERPROFILE");
#endif

GZ_ASSERT(homePath, "HOME environment variable is missing");
Expand Down
2 changes: 1 addition & 1 deletion gazebo/gui/GuiIface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#include "gazebo/gui/RenderWidget.hh"

#ifdef WIN32
# define HOMEDIR "HOMEPATH"
# define HOMEDIR "USERPROFILE"
#else
# define HOMEDIR "HOME"
#endif // WIN32
Expand Down
9 changes: 8 additions & 1 deletion gazebo/gui/MainWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -463,7 +464,13 @@ void MainWindow::Open()
/////////////////////////////////////////////////
void MainWindow::SaveINI()
{
char *home = getenv("HOME");
#ifdef _WIN32
std::string HOME_ENV = "USERPROFILE";
#else
std::string HOME_ENV = "HOME";
#endif

char *home = getenv(HOME_ENV.c_str());
if (!home)
{
gzerr << "HOME environment variable not found. "
Expand Down
9 changes: 8 additions & 1 deletion gazebo/gui/SaveEntityDialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -408,7 +409,13 @@ 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");
#ifdef _WIN32
std::string HOME_ENV = "USERPROFILE";
#else
std::string HOME_ENV = "HOME";
#endif

char *home = getenv(HOME_ENV.c_str());
if (home)
{
boost::filesystem::path guiINIPath = home;
Expand Down
9 changes: 8 additions & 1 deletion gazebo/rendering/Camera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -101,7 +102,13 @@ Camera::Camera(const std::string &_name, ScenePtr _scene,

this->sceneNode = NULL;

this->screenshotPath = getenv("HOME");
#ifdef _WIN32
std::string HOME_ENV = "USERPROFILE";
#else
std::string HOME_ENV = "HOME";
#endif

this->screenshotPath = getenv(HOME_ENV.c_str());
this->screenshotPath += "/.gazebo/pictures";

// Connect to the render signal
Expand Down
2 changes: 1 addition & 1 deletion gazebo/util/Diagnostics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ DiagnosticManager::DiagnosticManager()
#ifndef _WIN32
const char *homePath = common::getEnv("HOME");
#else
const char *homePath = common::getEnv("HOMEPATH");
const char *homePath = common::getEnv("USERPROFILE");
#endif
this->dataPtr->logPath = homePath;

Expand Down
2 changes: 1 addition & 1 deletion gazebo/util/LogRecord.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ LogRecord::LogRecord()
#ifndef _WIN32
const char *homePath = common::getEnv("HOME");
#else
const char *homePath = common::getEnv("HOMEPATH");
const char *homePath = common::getEnv("USERPROFILE");
#endif

GZ_ASSERT(homePath, "HOME environment variable is missing");
Expand Down
2 changes: 1 addition & 1 deletion gazebo/util/LogRecord_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TEST_F(LogRecord_TEST, Constructor)
#ifndef _WIN32
const char *homePath = common::getEnv("HOME");
#else
const char *homePath = common::getEnv("HOMEPATH");
const char *homePath = common::getEnv("USERPROFILE");
#endif

EXPECT_TRUE(homePath != NULL);
Expand Down
7 changes: 4 additions & 3 deletions test/integration/plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <boost/filesystem.hpp>

#include "gazebo/test/ServerFixture.hh"
#include "gazebo/common/CommonIface.hh"

using namespace gazebo;
class PluginTest : public ServerFixture
Expand All @@ -36,7 +37,7 @@ TEST_F(PluginTest, ModelExceptionConstructor)

world->Step(100);

char *home = getenv("HOME");
char *home = getenv(HOME_ENV.c_str());
ASSERT_TRUE(home);

boost::filesystem::path path(home);
Expand Down Expand Up @@ -71,7 +72,7 @@ TEST_F(PluginTest, ModelExceptionInit)

world->Step(100);

char *home = getenv("HOME");
char *home = getenv(HOME_ENV.c_str());
ASSERT_TRUE(home);

boost::filesystem::path path(home);
Expand Down Expand Up @@ -106,7 +107,7 @@ TEST_F(PluginTest, ModelExceptionLoad)

world->Step(100);

char *home = getenv("HOME");
char *home = getenv(HOME_ENV.c_str());
ASSERT_TRUE(home);

boost::filesystem::path path(home);
Expand Down
2 changes: 1 addition & 1 deletion test/integration/sdf_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SDFLogsTest : public ServerFixture
#ifndef _WIN32
const boost::filesystem::path home = common::getEnv("HOME");
#else
const boost::filesystem::path home = common::getEnv("HOMEPATH");
const boost::filesystem::path home = common::getEnv("USERPROFILE");
#endif
boost::filesystem::path log_path("/.gazebo/server-11345/default.log");
path = home / log_path;
Expand Down

0 comments on commit 79e69ac

Please sign in to comment.