diff options
Diffstat (limited to 'src/KM_fileio.cpp')
| -rw-r--r-- | src/KM_fileio.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/KM_fileio.cpp b/src/KM_fileio.cpp index 5149d35..ed8aa67 100644 --- a/src/KM_fileio.cpp +++ b/src/KM_fileio.cpp @@ -43,6 +43,11 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define _rmdir rmdir #endif +// only needed by GetExecutablePath() +#if defined(KM_MACOSX) +#include <mach-o/dyld.h> +#endif + using namespace Kumu; #ifdef KM_WIN32 @@ -626,6 +631,55 @@ Kumu::PathMatchGlob::Match(const std::string& s) const { #endif + +//------------------------------------------------------------------------------------------ + +#define X_BUFSIZE 1024 + +// +std::string +Kumu::GetExecutablePath(const std::string& default_path) +{ + char path[X_BUFSIZE] = {0}; + bool success = false; + +#if defined(KM_WIN32) + DWORD size = X_BUFSIZE; + DWORD rc = GetModuleFileName(0, path, size); + success = ( rc != 0 ); +#elif defined(KM_MACOSX) + uint32_t size = X_BUFSIZE; + int rc = _NSGetExecutablePath(path, &size); + success = ( rc != -1 ); +#elif defined(__linux__) + size_t size = X_BUFSIZE; + ssize_t rc = readlink("/proc/self/exe", path, size); + success = ( rc != -1 ); +#elif defined(__OpenBSD__) || defined(__FreeBSD__) + size_t size = X_BUFSIZE; + ssize_t rc = readlink("/proc/curproc/file", path, size); + success = ( rc != -1 ); +#elif defined(__FreeBSD__) + size_t size = X_BUFSIZE; + ssize_t rc = readlink("/proc/curproc/file", path, size); + success = ( rc != -1 ); +#elif defined(__NetBSD__) + size_t size = X_BUFSIZE; + ssize_t rc = readlink("/proc/curproc/file", path, size); + success = ( rc != -1 ); +#else +#error GetExecutablePath --> Create a method for obtaining the executable name +#endif + + if ( success ) + { + return Kumu::PathMakeCanonical(path); + } + + return default_path; +} + + //------------------------------------------------------------------------------------------ // portable aspects of the file classes |
