Create function getProcessPath and add unit test for it

Debugging: Relative and Absolute path alternatives
v0.27.3
Luis Díaz Más 7 years ago
parent 3009cc4c0c
commit 7485bb54d7

@ -175,6 +175,9 @@ namespace Exiv2
*/
EXIV2API std::string strError();
//! @brief Return the path of the current process.
EXIV2API std::string getProcessPath();
/*!
@brief A container for URL components. It also provides the method to parse a
URL to get the protocol, host, path, port, querystring, username, password.

@ -217,7 +217,8 @@ set_target_properties( exiv2lib_int PROPERTIES
target_compile_definitions(exiv2lib
PUBLIC
EXV_LOCALEDIR="${CMAKE_INSTALL_FULL_LOCALEDIR}"
EXV_LOCALEDIR="../${CMAKE_INSTALL_LOCALEDIR}"
#EXV_LOCALEDIR="${CMAKE_INSTALL_FULL_LOCALEDIR}"
)
if ( UNIX AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" )

@ -33,13 +33,20 @@
// + standard includes
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _MSC_VER
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#include <psapi.h> // For access to GetModuleFileNameEx
#elif defined(__APPLE__)
#include <libproc.h>
#endif
#ifdef EXV_HAVE_UNISTD_H
# include <unistd.h> // for stat()
# include <unistd.h> // for stat()
#endif
#include <cerrno>
#include <sstream>
#include <cstring>
@ -451,5 +458,43 @@ namespace Exiv2 {
result.QueryString = std::string(queryStart, uri.end());
return result;
} // Uri::Parse
}
std::string getProcessPath()
{
std::string ret("unknown");
#if defined(WIN32)
HANDLE processHandle = NULL;
processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId());
if (processHandle != NULL) {
TCHAR filename[MAX_PATH];
if (GetModuleFileNameEx(processHandle, NULL, filename, MAX_PATH) != 0) {
ret = filename;
}
CloseHandle(processHandle);
}
#elif defined(__APPLE__)
const int pid = getpid();
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
if (proc_pidpath (pid, pathbuf, sizeof(pathbuf)) > 0) {
ret = pathbuf;
}
#elif defined(__linux__)
// http://stackoverflow.com/questions/606041/how-do-i-get-the-path-of-a-process-in-unix-linux
char proc[100];
char path[500];
sprintf(proc,"/proc/%d/exe", getpid());
ssize_t l = readlink (proc, path,sizeof(path)-1);
if (l>0) {
path[l]=0;
ret = path;
}
#endif
#if defined(WIN32)
const size_t idxLastSeparator = ret.find_last_of('\\');
#else
const size_t idxLastSeparator = ret.find_last_of('/');
#endif
return ret.substr(0, idxLastSeparator);
}
} // namespace Exiv2

@ -153,3 +153,17 @@ TEST(AUri, parsesAndDecoreUrl)
Uri::Decode(uri);
}
TEST(getProcessPath, obtainPathOfUnitTestsExecutable)
{
#ifdef _WIN32
const std::string expectedName("bin");
#else
const std::string expectedName("bin");
#endif
const std::string path = getProcessPath();
ASSERT_FALSE(path.empty());
const size_t idxStart = path.size() - expectedName.size();
ASSERT_EQ(expectedName, path.substr(idxStart, expectedName.size()));
}

Loading…
Cancel
Save