Skip to content

Commit

Permalink
Set HOME_ENV according the OS (#3334)
Browse files Browse the repository at this point in the history
For windows is USERPROFILE for other is HOME
  • Loading branch information
talregev authored Jul 22, 2023
1 parent d658ee6 commit 3d7e787
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 46 deletions.
6 changes: 6 additions & 0 deletions gazebo/common/CommonIface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 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,17 +156,17 @@ 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;
}

FileLogger::Buffer *buf = static_cast<FileLogger::Buffer*>(
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 <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(HOMEDIR);
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(HOMEDIR);
std::string home;
if (!homePath)
home = this->TmpPath() + "/gazebo";
Expand Down
6 changes: 1 addition & 5 deletions gazebo/gui/DataLogger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
6 changes: 0 additions & 6 deletions gazebo/gui/GuiIface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions 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,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;
}
Expand Down
3 changes: 2 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,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;
Expand Down
3 changes: 2 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,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
Expand Down
8 changes: 2 additions & 6 deletions gazebo/util/Diagnostics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
11 changes: 4 additions & 7 deletions gazebo/util/LogRecord.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
6 changes: 1 addition & 5 deletions gazebo/util/LogRecord_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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(HOMEDIR);
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(HOMEDIR);
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(HOMEDIR);
ASSERT_TRUE(home);

boost::filesystem::path path(home);
Expand Down
6 changes: 1 addition & 5 deletions test/integration/sdf_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 3d7e787

Please sign in to comment.