forked from hasherezade/pe-sieve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_util.cpp
More file actions
112 lines (97 loc) · 2.48 KB
/
path_util.cpp
File metadata and controls
112 lines (97 loc) · 2.48 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "path_util.h"
#include "format_util.h"
char* pesieve::util::get_subpath_ptr(char *modulePath, char* searchedPath)
{
if (modulePath == nullptr || searchedPath == nullptr) {
return nullptr;
}
size_t modNameLen = strlen(modulePath);
size_t sysPathLen = strlen(searchedPath);
size_t i = 0;
for (; i < modNameLen && i < sysPathLen; i++) {
char c1 = tolower(modulePath[i]);
char c2 = tolower(searchedPath[i]);
if (c1 == '/') c1 = '\\'; //normalize
if (c1 != c2) {
break;
}
}
if (i == sysPathLen) {
return modulePath + i;
}
return nullptr;
}
std::string pesieve::util::escape_path_separators(std::string path)
{
size_t pos = std::string::npos;
size_t prev = 0;
const char to_escape = '\\';
const std::string escaped = "\\\\";
do
{
pos = path.find(to_escape, prev);
if (pos == std::string::npos) break;
path.replace(pos, 1, escaped);
prev = pos + escaped.length();
} while (pos < path.length() && prev < path.length());
return path;
}
std::string pesieve::util::get_system_drive()
{
char buf[MAX_PATH] = { 0 };
if (!GetWindowsDirectoryA(buf, MAX_PATH)) {
return "";
}
buf[2] = '\0'; // cut after the drive letter
return std::string(buf);
}
std::string get_full_path(const char* szPath)
{
char out_buf[MAX_PATH] = { 0 };
if (GetFullPathNameA(szPath, MAX_PATH, out_buf, nullptr) == 0) {
return "";
}
return out_buf;
}
bool pesieve::util::dir_exists(const char* szPath)
{
DWORD dwAttrib = GetFileAttributes(szPath);
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
bool pesieve::util::create_dir_recursively(const std::string& in_path)
{
std::string path = get_full_path(in_path.c_str());
if (path.length() == 0) path = in_path;
if (dir_exists(path.c_str())) {
return true;
}
size_t pos = 0;
do
{
pos = path.find_first_of("\\/", pos + 1);
if (CreateDirectoryA(path.substr(0, pos).c_str(), NULL) == FALSE) {
if (GetLastError() != ERROR_ALREADY_EXISTS) {
return false;
}
}
} while (pos != std::string::npos);
return true;
}
std::string pesieve::util::strip_prefix(std::string path, std::string prefix)
{
const size_t prefix_len = prefix.length();
if (prefix_len == 0) {
return path;
}
// case insensitive:
std::string my_path = to_lowercase(path);
prefix = to_lowercase(prefix);
size_t found_index = my_path.find(prefix);
if (found_index != std::string::npos
&& found_index == 0) //the found string must be at the beginning
{
path.erase(found_index, prefix_len);
}
return path;
}