Skip to content

Commit

Permalink
reconstruct getCurrentExeFileDir
Browse files Browse the repository at this point in the history
  • Loading branch information
vintagewang committed Jun 10, 2012
1 parent ce50270 commit 6cdd6f9
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/JWUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@

#ifdef WIN32
#include <Windows.h>
#else
#include <sys/types.h>
#include <unistd.h>
#endif

#ifdef WIN32
#define FILE_SEPARATOR "\\"
#else
#define FILE_SEPARATOR "/"
#endif

namespace JWUtil
Expand All @@ -42,6 +51,11 @@ namespace JWUtil
result += dir;
result += fname;
#else
char proc[64] = {0};
sprintf(proc, "/proc/%d/exe", getpid());
char path[MAX_PATH + 1] = {0};
readlink(proc, path, sizeof(path) - 1);
result = path;
#endif

return result;
Expand All @@ -51,35 +65,25 @@ namespace JWUtil
{
std::string result;
#ifdef WIN32
char path_buffer[_MAX_PATH + 1] = {0};
_getcwd(path_buffer, sizeof(path_buffer) - 1);
result = path_buffer;
char path[_MAX_PATH + 1] = {0};
_getcwd(path, sizeof(path) - 1);
result = path;
#else
char path_buffer[MAX_PATH + 1] = {0};
getcwd(path_buffer, sizeof(path_buffer) - 1);
result = path_buffer;
char path[MAX_PATH + 1] = {0};
getcwd(path, sizeof(path) - 1);
result = path;
#endif

return result;
}

std::string getCurrentExeFileDir()
{
std::string result;
#ifdef WIN32
char path_buffer[_MAX_PATH + 1] = {0};
char drive[_MAX_DRIVE + 1] = {0};
char dir[_MAX_DIR + 1] = {0};
char fname[_MAX_FNAME + 1] = {0};
char ext[_MAX_EXT + 1] = {0};

GetModuleFileNameA(NULL, path_buffer, sizeof(path_buffer) - 1);
_splitpath(path_buffer, drive, dir, fname, ext);

result += drive;
result += dir;
#else
#endif
std::string result = getCurrentExeFilePath();
std::string::size_type pos = result.find_last_of(FILE_SEPARATOR);
if(pos != std::string::npos) {
return result.substr(0, pos);
}

return result;
}
Expand Down

0 comments on commit 6cdd6f9

Please sign in to comment.