Stride Reference Manual  1.0
InstallDirs.cpp
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, Willem L, Kuylen E, Stijven S & Broeckhove J
14  */
15 
21 #include "InstallDirs.h"
22 
23 #include "util/StringUtils.h"
24 
25 #if defined(WIN32)
26 # include <stdlib.h>
27 # include <windows.h>
28 #elif defined(__linux__)
29 
30 #elif defined(__APPLE__)
31 # include <mach-o/dyld.h>
32 # include <limits.h>
33 #endif
34 
35 namespace stride {
36 namespace util {
37 
38 using namespace std;
39 using namespace boost::filesystem;
40 
47 
48 
49 inline void InstallDirs::check() {
50  static bool initialized = false;
51  if (!initialized) {
52  initialize();
53  initialized = true;
54  }
55 }
56 
58  //------- Retrieving path of executable
59  {
60  // Returns the full path to the currently running executable, or an empty string in case of failure.
61  // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe/33249023#33249023
62 
63  #if defined(WIN32)
64  char exePath[MAX_PATH];
65  HMODULE hModule = GetModuleHandle(NULL);
66  if (GetModuleFileName(NULL, exePath, sizeof(exePath)) !=0); {
67  g_exec_path = canonical(system_complete(exePath));
68  }
69  #elif defined(__linux__)
70  char exePath[PATH_MAX + 1];
71  size_t size = ::readlink("/proc/self/exe", exePath, sizeof(exePath));
72  exePath[size] = '\0';
73  if (size > 0 && size != sizeof(exePath)) {
74  exePath[size] = '\0';
75  g_exec_path = canonical(system_complete(exePath));
76  }
77  #elif defined(__APPLE__)
78  char exePath[PATH_MAX];
79  uint32_t size = sizeof(exePath);
80  if (_NSGetExecutablePath(exePath, &size) == 0) {
81  g_exec_path = canonical(system_complete(exePath));
82  }
83  #endif
84  }
85 
86  //------- Retrieving root and bin directory (the subdirectory of the install root)
87  {
88  path exec_dir = g_exec_path.parent_path();
89  if (!g_exec_path.empty()) {
90  #if (__APPLE__)
91  if (exec_dir.filename().string() == "MacOS") {
92  // app
93  // -Contents <-Root Path
94  // -MacOS
95  // -executables
96  g_bin_dir = exec_dir;
97  g_root_dir = exec_dir.parent_path();
98  } else
99  #endif
100  if (StringUtils::toLower(exec_dir.filename().string()) == "debug"
101  || StringUtils::toLower(exec_dir.filename().string()) == "release") {
102  //x/exec <-Root Path
103  // -bin
104  // -release/debug
105  // -executables
106  g_bin_dir = exec_dir.parent_path();
107  g_root_dir = exec_dir.parent_path().parent_path();
108  } else
109  #if (WIN32)
110  if (exec_dir.filename().string() != "bin") {
111  // Executables in root folder
112  g_bin_dir = exec_dir;
113  g_root_dir = exec_dir;
114  } else
115  #endif
116  {
117  //x/exec <-Root Path
118  // -bin
119  // -executables
120  g_bin_dir = exec_dir;
121  g_root_dir = exec_dir.parent_path();
122  }
123  }
124  }
125 
126  //------- Data Dir
127  {
128  g_data_dir = g_root_dir / "data";
129  g_data_dir = is_directory(g_data_dir) ? g_data_dir : path();
130  }
131  //------- Current Dir
132  {
133  g_current_dir = system_complete(current_path());
134  }
135  //------- Output Dir
136  {
137  g_output_dir = g_root_dir / "output";
138  g_output_dir = is_directory(g_output_dir) ? g_output_dir : path();
139  }
140 }
141 
143  check();
144  return g_bin_dir;
145 }
146 
148  check();
149  return g_current_dir;
150 }
151 
153  check();
154  return g_data_dir;
155 }
156 
158  check();
159  return g_exec_path;
160 }
161 
163  check();
164  return g_root_dir;
165 }
166 
168  check();
169  return g_output_dir;
170 }
171 
172 }
173 }
static boost::filesystem::path g_root_dir
Definition: InstallDirs.h:62
static boost::filesystem::path getOutputDir()
Utility method: get the path to the output directory.
Interface for install directory queries.
static boost::filesystem::path g_data_dir
Definition: InstallDirs.h:60
Time Dependent Person DataType.
Definition: NoBehaviour.h:17
Conversion from or to string.
static boost::filesystem::path getDataDir()
Utility method: get path to the directory for data files.
static boost::filesystem::path getExecPath()
Utility method: get name of executable.
static boost::filesystem::path getCurrentDir()
Utility method: get path to the current directory.
static std::string toLower(std::string const &source)
Builds a string with lower case characters only.
Definition: StringUtils.h:95
static boost::filesystem::path g_current_dir
Definition: InstallDirs.h:59
STL namespace.
static boost::filesystem::path getBinDir()
Utility method: get path to bin directory.
static boost::filesystem::path g_bin_dir
Definition: InstallDirs.h:58
static void initialize()
Initialize all paths.
Definition: InstallDirs.cpp:57
static boost::filesystem::path g_output_dir
Definition: InstallDirs.h:63
static boost::filesystem::path getRootDir()
Utility method: get application installation root directory.
static boost::filesystem::path g_exec_path
Definition: InstallDirs.h:61
static void check()
Check initialization.
Definition: InstallDirs.cpp:49