23#ifndef SRC_HIPBIN_UTIL_H_
24#define SRC_HIPBIN_UTIL_H_
27#ifndef INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
29#if defined(__cpp_lib_filesystem)
30#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0
32#elif defined(__cpp_lib_experimental_filesystem)
33#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
36#elif !defined(__has_include)
37#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
39#elif __has_include(<filesystem>)
44#if __has_include(<yvals_core.h>)
45#include <yvals_core.h>
48#if defined(_HAS_CXX17) && _HAS_CXX17
50#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0
57#ifndef INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
58#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
63#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 0
67#elif __has_include(<experimental/filesystem>)
68#define INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 1
72#error Could not find system header "<filesystem>" ||
73 "<experimental/filesystem>"
77#if INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
79#include <experimental/filesystem>
81namespace fs = std::experimental::filesystem;
86namespace fs = std::filesystem;
104#if defined(_WIN32) || defined(_WIN64)
109 typedef wchar_t TCHAR;
110 typedef std::wstring TSTR;
111 typedef std::wstring::size_type TSIZE;
112#define ENDLINE L"/\\"
115 typedef std::string TSTR;
116 typedef std::string::size_type TSIZE;
130using std::regex_match;
131using std::regex_search;
132using std::regex_replace;
135using std::stringstream;
153 string getSelfPath()
const;
154 vector<string> splitStr(
string fullStr,
char delimiter)
const;
155 string replaceStr(
const string& s,
const string& toReplace,
156 const string& replaceWith)
const;
157 string replaceRegex(
const string& s, regex toReplace,
158 string replaceWith)
const;
159 SystemCmdOut exec(
const char* cmd,
bool printConsole)
const;
161 void deleteTempFiles();
162 string mktempFile(
string name);
163 string trim(
string str)
const;
164 string readConfigMap(map<string, string> hipVersionMap,
165 string keyName,
string defaultValue)
const;
166 map<string, string> parseConfigFile(fs::path configPath)
const;
167 bool substringPresent(
string fullString,
string subString)
const;
168 bool stringRegexMatch(
string fullString,
string pattern)
const;
169 bool checkCmd(
const vector<string>& commands,
const string& argument);
173 vector<string> tmpFiles_;
180HipBinUtil::~HipBinUtil() {
185string HipBinUtil::mktempFile(
string name) {
187#if defined(_WIN32) || defined(_WIN64)
188 fileName = _mktemp(&name[0]);
190 fileName = mkdtemp(&name[0]);
192 tmpFiles_.push_back(fileName);
197string HipBinUtil::getSelfPath()
const {
198 int MAX_PATH_CHAR = 1024;
201 #if defined(_WIN32) || defined(_WIN64)
202 TCHAR buffer[MAX_PATH] = { 0 };
203 bufferSize = GetModuleFileName(NULL, buffer, MAX_PATH_CHAR);
204 TSIZE pos = TSTR(buffer).find_last_of(ENDLINE);
205 TSTR wide = TSTR(buffer).substr(0, pos);
206 path = string(wide.begin(), wide.end());
208 char buff[MAX_PATH_CHAR];
209 ssize_t len = ::readlink(
"/proc/self/exe", buff,
sizeof(buff) - 1);
213 fs::path exePath(path);
214 path = exePath.parent_path().string();
216 std::cerr <<
"readlink: Error reading the exe path" << endl;
226string HipBinUtil::trim(
string str)
const {
227 string strChomp = str;
228 strChomp.erase(str.find_last_not_of(
" \n\r\t")+1);
233bool HipBinUtil::stringRegexMatch(
string fullString,
string pattern)
const {
234 return regex_match(fullString, regex(pattern));
238bool HipBinUtil::substringPresent(
string fullString,
string subString)
const {
239 return fullString.find(subString) != string::npos;
243vector<string> HipBinUtil::splitStr(
string fullStr,
char delimiter)
const {
244 vector <string> tokens;
245 stringstream check1(fullStr);
247 while (getline(check1, intermediate, delimiter)) {
248 tokens.push_back(intermediate);
254string HipBinUtil::replaceStr(
const string& s,
const string& toReplace,
255 const string& replaceWith)
const {
257 std::size_t pos = out.find(toReplace);
258 if (pos == string::npos)
return out;
259 return out.replace(pos, toReplace.length(), replaceWith);
264string HipBinUtil::replaceRegex(
const string& s, regex toReplace,
265 string replaceWith)
const {
267 while (regex_search(out, toReplace)) {
268 out = regex_replace(out, toReplace, replaceWith);
274map<string, string> HipBinUtil::parseConfigFile(fs::path configPath)
const {
275 map<string, string> configMap;
276 ifstream isFile(configPath.string());
278 if (isFile.is_open()) {
279 while (std::getline(isFile, line)) {
280 std::istringstream is_line(line);
282 if (std::getline(is_line, key,
'=')) {
284 if (std::getline(is_line, value)) {
285 configMap.insert({ key, value });
295void HipBinUtil::deleteTempFiles() {
297 for (
unsigned int i = 0; i < tmpFiles_.size(); i++) {
299 if (!fs::remove(tmpFiles_.at(i)))
300 std::cerr <<
"Error deleting temp name: "<< tmpFiles_.at(i) <<endl;
303 std::cerr <<
"Error deleting temp name: "<< tmpFiles_.at(i) <<endl;
309string HipBinUtil::getTempDir() {
312 string tmpdir = fs::temp_directory_path().string();
319 bool printConsole =
false)
const {
324 #if defined(_WIN32) || defined(_WIN64)
325 FILE* pipe = _popen(cmd,
"r");
327 FILE* pipe = popen(cmd,
"r");
329 if (!pipe)
throw std::runtime_error(
"popen() failed!");
331 while (fgets(buffer,
sizeof buffer, pipe) != NULL) {
335 std::cerr <<
"Error while executing the command: " << cmd << endl;
337 #if defined(_WIN32) || defined(_WIN64)
338 sysOut.exitCode = _pclose(pipe);
340 int closeStatus = pclose(pipe);
341 sysOut.exitCode = WEXITSTATUS(closeStatus);
343 if (printConsole ==
true) {
344 cout << result << endl;
349 sysOut.exitCode = -1;
355string HipBinUtil::readConfigMap(map<string, string> hipVersionMap,
356 string keyName,
string defaultValue)
const {
357 auto it = hipVersionMap.find(keyName);
358 if (it != hipVersionMap.end()) {
366bool HipBinUtil::checkCmd(
const vector<string>& commands,
367 const string& argument) {
369 for (
unsigned int i = 0; i < commands.size(); i++) {
370 if (argument.compare(commands.at(i)) == 0) {
Definition hipBin_util.h:144
Definition hipBin_util.h:138