Skip to content

Commit 644f552

Browse files
committed
Add function osgDB::executableFilePath() to find the executable path platform independent
The implementation has been taken from the VulkanSceneGraph project.
1 parent 7fc36a5 commit 644f552

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

include/osgDB/FileUtils

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ extern OSGDB_EXPORT bool setCurrentWorkingDirectory( const std::string &newCurre
4343
/** return true if a file exists. */
4444
extern OSGDB_EXPORT bool fileExists(const std::string& filename);
4545

46+
/** returns the path/filename of the currently executed program. */
47+
extern OSGDB_EXPORT std::string executableFilePath();
48+
4649
enum FileType
4750
{
4851
FILE_NOT_FOUND,

src/osgDB/FileUtils.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,20 @@ typedef char TCHAR;
4343
#ifndef F_OK
4444
#define F_OK 4
4545
#endif
46+
#ifdef _MSC_VER
47+
#ifndef PATH_MAX
48+
#define PATH_MAX MAX_PATH
49+
#endif
50+
#endif
4651

4752
#else // unix
4853

4954
#if defined( __APPLE__ )
5055
// I'm not sure how we would handle this in raw Darwin
5156
// without the AvailablilityMacros.
5257
#include <AvailabilityMacros.h>
58+
#include <libgen.h>
59+
#include <mach-o/dyld.h>
5360

5461
//>OSG_IOS
5562
//IOS includes
@@ -116,6 +123,7 @@ typedef char TCHAR;
116123
#include <osgDB/Registry>
117124

118125
#include <errno.h>
126+
#include <limits.h>
119127
#include <string.h>
120128

121129
#include <stack>
@@ -526,6 +534,59 @@ std::string osgDB::findFileInDirectory(const std::string& fileName,const std::st
526534
return "";
527535
}
528536

537+
/* This function has be taken from the VSG project */
538+
std::string osgDB::executableFilePath()
539+
{
540+
std::string path;
541+
542+
#if defined(WIN32)
543+
TCHAR buf[PATH_MAX + 1];
544+
DWORD result = GetModuleFileName(NULL, buf, static_cast<DWORD>(std::size(buf) - 1));
545+
if (result && result < std::size(buf))
546+
path = buf;
547+
#elif defined(__linux__)
548+
549+
std::vector<char> buffer(1024);
550+
ssize_t len = 0;
551+
while ((len = ::readlink("/proc/self/exe", buffer.data(), buffer.size())) == static_cast<ssize_t>(buffer.size()))
552+
{
553+
buffer.resize(buffer.size() * 2);
554+
}
555+
556+
// add terminator to string.
557+
buffer[len] = '\0';
558+
559+
return buffer.data();
560+
561+
#elif defined(__APPLE__)
562+
# if TARGET_OS_MAC
563+
char realPathName[PATH_MAX + 1];
564+
char buf[PATH_MAX + 1];
565+
uint32_t size = (uint32_t)sizeof(buf);
566+
567+
if (!_NSGetExecutablePath(buf, &size))
568+
{
569+
realpath(buf, realPathName);
570+
path = realPathName;
571+
}
572+
# elif TARGET_IPHONE_SIMULATOR
573+
// iOS, tvOS, or watchOS Simulator
574+
// Not currently implemented
575+
# elif TARGET_OS_MACCATALYST
576+
// Mac's Catalyst (ports iOS API into Mac, like UIKit).
577+
// Not currently implemented
578+
# elif TARGET_OS_IPHONE
579+
// iOS, tvOS, or watchOS device
580+
// Not currently implemented
581+
# else
582+
# error "Unknown Apple platform"
583+
# endif
584+
#elif defined(__ANDROID__)
585+
// Not currently implemented
586+
#endif
587+
return path;
588+
}
589+
529590
static void appendInstallationLibraryFilePaths(osgDB::FilePathList& filepath)
530591
{
531592
#ifdef OSG_DEFAULT_LIBRARY_PATH

0 commit comments

Comments
 (0)