Repository navigation
Expand file tree
/
Copy pathUtils.cppm
More file actions
executable file
·76 lines (67 loc) · 1.96 KB
/
Copy pathUtils.cppm
File metadata and controls
executable file
·76 lines (67 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// Created by CommanderKeen on 14.10.24.
//
module;
#include <string>
#include <stdexcept>
#include <iostream>
#include <ctype.h>
#include <algorithm>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <limits.h>
#endif
export module Utils;
export namespace Utils {
std::string to_lower(const std::string& l_str){
std::string result = l_str;
for(auto& chr : result){
chr = (char)tolower(chr);
}
return result;
}
std::string get_project_path() {
const int MAX_PATH_LENGTH = 256;
char temp_result[MAX_PATH_LENGTH];
std::string result;
#ifdef _WIN32
DWORD size = GetModuleFileNameA(NULL, temp_result, MAX_PATH_LENGTH);
if (size > 0) {
std::string substring{ temp_result };
substring = substring.substr(0, substring.find_last_of('\\'));
substring = substring.substr(0, substring.find_last_of('\\'));
result = substring;
}
else {
throw std::runtime_error("Error in Utils::get_project_path() - could not retrieve working path!");
}
#elif __APPLE__
char currentPath[PATH_MAX];
if (getcwd(currentPath, sizeof(currentPath)) != nullptr) {
std::string subStr{currentPath};
subStr = subStr.substr(0, subStr.find_last_of('/'));
result = subStr;
result.append("/");
}
else {
throw std::runtime_error("Error in Utils::get_project_path() - could not retrieve working path!");
}
#endif
return result;
} // end of std::string get_project_path()
std::string formatPath(const std::string& l_str) {
std::string result = l_str;
#ifdef _WIN32
std::replace_if(result.begin(), result.end(), [](char c)-> bool {
return (c == '/' || c == '\\');
}, '\\');
#elif __APPLE__
std::replace_if(result.begin(), result.end(), [](char c)-> bool {
return (c == '/' || c == '\\');
}, '/');
#endif
return result;
}
}// end of namespace Utils