From 3f67db703ed38a138961da7665867c51312cb0df Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 7 Apr 2013 18:04:30 +0000 Subject: [PATCH] sys: refactor the binarydir detection code. --- src/sys/init.cpp | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/sys/init.cpp b/src/sys/init.cpp index 305cbb34..4d452f24 100644 --- a/src/sys/init.cpp +++ b/src/sys/init.cpp @@ -8,15 +8,15 @@ // http://www.wtfpl.net/ for more details. // -#if defined HAVE_CONFIG_H +#if HAVE_CONFIG_H # include "config.h" #endif -#if defined HAVE_UNISTD_H +#if HAVE_UNISTD_H # include #endif -#ifdef _WIN32 +#if _WIN32 # define WIN32_LEAN_AND_MEAN # include #endif @@ -29,7 +29,7 @@ namespace lol namespace System { -#if defined _WIN32 +#if _WIN32 # define SEPARATOR '\\' #else # define SEPARATOR '/' @@ -45,23 +45,29 @@ void Init(int argc, char *argv[], using namespace std; /* - * Retrieve binary directory, defaulting to current dir. + * Retrieve binary directory, defaulting to no directory on Android + * and emscripten, and the current directory on other platforms. */ -#if __ANDROID__ - /* Android assets are accessed using no prefix at all. */ +#if __ANDROID__ || EMSCRIPTEN String binarydir = ""; -#elif defined HAVE_GETCWD - char *cwd = getcwd(nullptr, 0); - String binarydir = String(cwd ? cwd : ".") + SEPARATOR; - free(cwd); -#elif defined HAVE__GETCWD || (defined _WIN32 && !defined _XBOX) - char *cwd = _getcwd(nullptr, 0); - String binarydir = String(cwd ? cwd : ".") + SEPARATOR; - free(cwd); #else - String binarydir = "./"; -#endif + String binarydir = "."; + char *cwd = nullptr; + +# if HAVE_GETCWD + cwd = getcwd(nullptr, 0); +# elif HAVE__GETCWD || (_WIN32 && !_XBOX) + cwd = _getcwd(nullptr, 0); +# endif + + if (cwd) + { + binarydir = cwd; + free(cwd); + } + + binarydir += SEPARATOR; if (argc > 0) { @@ -69,6 +75,7 @@ void Init(int argc, char *argv[], if (last_sep) binarydir = String(argv[0], last_sep - argv[0] + 1); } +#endif /* * Find the common prefix between project dir and solution dir.