Browse Source

sys: retrieve executable path if available.

legacy
Sam Hocevar sam 12 years ago
parent
commit
ce04c94ff3
2 changed files with 39 additions and 15 deletions
  1. +7
    -2
      src/lol/sys/init.h
  2. +32
    -13
      src/sys/init.cpp

+ 7
- 2
src/lol/sys/init.h View File

@@ -34,12 +34,17 @@ namespace lol
# define LOL_CONFIG_SOLUTIONDIR "" # define LOL_CONFIG_SOLUTIONDIR ""
#endif #endif


/*
* System namespace. The platform-specific stuff in there makes the API
* not as clean as the rest of the framework.
*/

namespace System namespace System
{ {


extern void Init(int argc, char *argv[], extern void Init(int argc, char *argv[],
char const *projectdir = LOL_CONFIG_PROJECTDIR,
char const *solutiondir = LOL_CONFIG_SOLUTIONDIR);
String const projectdir = LOL_CONFIG_PROJECTDIR,
String const solutiondir = LOL_CONFIG_SOLUTIONDIR);


extern void SetDataDir(char const *dir); extern void SetDataDir(char const *dir);
extern char const *GetDataDir(); extern char const *GetDataDir();


+ 32
- 13
src/sys/init.cpp View File

@@ -12,6 +12,11 @@
# include "config.h" # include "config.h"
#endif #endif


#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <direct.h>
#endif

#include "core.h" #include "core.h"


namespace lol namespace lol
@@ -27,11 +32,31 @@ namespace System
#endif #endif


void Init(int argc, char *argv[], void Init(int argc, char *argv[],
char const *projectdir, char const *solutiondir)
String const projectdir, String const solutiondir)
{ {
bool got_rootdir = false;
/*
* Retrieve binary directory, defaulting to current dir.
*/

#if defined _WIN32
char const *cwd = _getcwd(NULL, 0);
String binarydir = String(cwd ? cwd : ".") + SEPARATOR;
free((void *)cwd);
#else
String binarydir = String(getcwd()) + SEPARATOR;
#endif
if (argc > 0)
{
char const *last_sep = strchr(argv[0], SEPARATOR);
if (last_sep)
binarydir = String(argv[0], last_sep - argv[0] + 1);
}


/* Find the common prefix between project dir and solution dir. */
/*
* Find the common prefix between project dir and solution dir.
*/

bool got_rootdir = false;
for (int i = 0; ; i++) for (int i = 0; ; i++)
{ {
if (projectdir[i] != solutiondir[i] || projectdir[i] == '\0') if (projectdir[i] != solutiondir[i] || projectdir[i] == '\0')
@@ -51,17 +76,11 @@ void Init(int argc, char *argv[],
} }
} }


/* Try to guess the data directory from the executable location. */
if (!got_rootdir && argc > 0)
/* If no rootdir found, use the executable location */
if (!got_rootdir)
{ {
char const *last_slash = strrchr(argv[0], SEPARATOR);

if (last_slash)
{
String dir(argv[0], last_slash - argv[0] + 1);
SetDataDir(&dir[0]);
got_rootdir = true;
}
SetDataDir(&binarydir[0]);
got_rootdir = true;
} }
} }




Loading…
Cancel
Save