Commit d6746819 authored by kulvait's avatar kulvait

RunTimeInfo

Class to get information about executable path
parent 159a4aea
#pragma once
#include <stdexcept>
#include <string>
// Linux specific block
#include <limits.h>
#include <unistd.h>
// Linux specific block
#include "rawop.h"
#include "stringFormatter.h"
namespace CTL {
namespace io {
/**
* @brief Class to provide run time information.
*
* Function can be used as follows
* @code
*
* #include "RunTimeInfo.hpp"
*
*
* io::RunTimeInfo rti;
* std::string exe = rti.getExecutableName();
* std::string path = rti.getExecutablePath();
* std::string dir = rti.getExecutableDirectoryPath();
* std::cout << io::xprintf("Executable is %s", exe.c_str());
* std::cout << io::xprintf("Executable path is %s", path.c_str());
* std::cout << io::xprintf("Executable dir is %s", dir.c_str());
* @endcode
*/
class RunTimeInfo
{
public:
RunTimeInfo(int argc, char* argv[]);
RunTimeInfo();
std::string getExecutablePath();
std::string getExecutableName();
std::string getExecutableDirectoryPath();
private:
int argc;
char** argv;
std::string executablePath;
};
} // namespace io
} // namespace CTL
#include "RunTimeInfo.hpp"
namespace CTL {
namespace io {
RunTimeInfo::RunTimeInfo()
{
char exepath[PATH_MAX + 1] = { 0 };
ssize_t len = ::readlink("/proc/self/exe", exepath, PATH_MAX);
if(len != -1)
{
exepath[len] = 0;
executablePath = std::string(exepath);
} else
{
throw std::runtime_error(io::xprintf("Can not read process path."));
}
}
RunTimeInfo::RunTimeInfo(int argc, char* argv[])
: argc(argc)
, argv(argv)
{
RunTimeInfo();
}
std::string RunTimeInfo::getExecutablePath() { return (executablePath); }
std::string RunTimeInfo::getExecutableName() { return io::getBasename(executablePath); }
std::string RunTimeInfo::getExecutableDirectoryPath() { return io::getParent(executablePath); }
} // namespace io
} // namespace CTL
// Logging, testing
#include "catch.hpp"
#include <plog/Log.h>
#include "RunTimeInfo.hpp"
using namespace CTL;
TEST_CASE("TEST: RunTimeInfo.hpp", "RunTimeInfo")
{
io::RunTimeInfo rti;
std::string exe = rti.getExecutableName();
std::string path = rti.getExecutablePath();
std::string dir = rti.getExecutableDirectoryPath();
REQUIRE(exe.compare("test_ctiol")==0);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment