OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
BinaryLocation.cpp
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 // Internal Includes
27 
29 
30 // Library/third-party includes
31 #ifdef OSVR_WINDOWS
32 #define WIN32_LEAN_AND_MEAN
33 #include <windows.h>
34 #endif // OSVR_WINDOWS
35 
36 #ifdef OSVR_MACOSX
37 #include <mach-o/dyld.h>
38 #endif // OSVR_MACOSX
39 
40 #include <boost/filesystem.hpp>
41 
42 // Standard includes
43 // - none
44 
45 namespace osvr {
46 namespace util {
47 
48  namespace fs = boost::filesystem;
49 
50  static inline std::string getCanonicalGenericString(const char path[]) {
51  return fs::canonical(path).generic_string();
52  }
53 
54 #if defined(OSVR_WINDOWS)
55  std::string getBinaryLocation() {
56  char buf[512] = {0};
57  DWORD len = GetModuleFileNameA(nullptr, buf, sizeof(buf));
58  std::string ret;
59  if (0 != len) {
60  ret.assign(buf, len);
61  }
62  return ret;
63  }
64 #elif defined(OSVR_LINUX) || defined(OSVR_ANDROID)
65  std::string getBinaryLocation() {
66  return getCanonicalGenericString("/proc/self/exe");
67  }
68 #elif defined(OSVR_NETBSD)
69  std::string getBinaryLocation() {
70  return getCanonicalGenericString("/proc/curproc/exe");
71  }
72 #elif defined(OSVR_FREEBSD)
73  std::string getBinaryLocation() {
74  if (fs::exists("proc/curproc/file")) {
75  return getCanonicalGenericString("/proc/curproc/file");
76  }
77  return std::string{};
79  }
80 #elif defined(OSVR_MACOSX)
81  std::string getBinaryLocation() {
82  char buf[1024] = {0};
83  uint32_t len = sizeof(buf);
84  if (0 == _NSGetExecutablePath(buf, &len)) {
85  return getCanonicalGenericString(buf);
86  }
87  return std::string{};
88  }
89 #else
90 #error "getBinaryLocation() not yet implemented for this platform!"
91  std::string getBinaryLocation() { return std::string{}; }
92 
94 #endif
95 
96 } // namespace util
97 } // namespace osvr
Auto-configured header.
std::string getBinaryLocation()