forked from flightlessmango/MangoHud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_utils.cpp
210 lines (180 loc) · 4.97 KB
/
file_utils.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "file_utils.h"
#include "string_utils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <limits.h>
#include <fstream>
#include <cstring>
#include <string>
#include <spdlog/spdlog.h>
#include <filesystem.h>
namespace fs = ghc::filesystem;
#ifndef PROCDIR
#define PROCDIR "/proc"
#endif
std::string read_line(const std::string& filename)
{
std::string line;
std::ifstream file(filename);
if (file.fail()){
return line;
}
std::getline(file, line);
return line;
}
std::string get_basename(const std::string&& path)
{
auto npos = path.find_last_of("/\\");
if (npos == std::string::npos)
return path;
if (npos < path.size() - 1)
return path.substr(npos + 1);
return path;
}
#ifdef __linux__
std::vector<std::string> ls(const char* root, const char* prefix, LS_FLAGS flags)
{
std::vector<std::string> list;
struct dirent* dp;
DIR* dirp = opendir(root);
if (!dirp) {
SPDLOG_ERROR("Error opening directory '{}': {}", root, strerror(errno));
return list;
}
while ((dp = readdir(dirp))) {
if ((prefix && !starts_with(dp->d_name, prefix))
|| !strcmp(dp->d_name, ".")
|| !strcmp(dp->d_name, ".."))
continue;
switch (dp->d_type) {
case DT_LNK: {
struct stat s;
std::string path(root);
if (path.back() != '/')
path += "/";
path += dp->d_name;
if (stat(path.c_str(), &s))
continue;
if (((flags & LS_DIRS) && S_ISDIR(s.st_mode))
|| ((flags & LS_FILES) && S_ISREG(s.st_mode))) {
list.push_back(dp->d_name);
}
break;
}
case DT_DIR:
if (flags & LS_DIRS)
list.push_back(dp->d_name);
break;
case DT_REG:
if (flags & LS_FILES)
list.push_back(dp->d_name);
break;
}
}
closedir(dirp);
return list;
}
bool file_exists(const std::string& path)
{
struct stat s;
return !stat(path.c_str(), &s) && !S_ISDIR(s.st_mode);
}
bool dir_exists(const std::string& path)
{
struct stat s;
return !stat(path.c_str(), &s) && S_ISDIR(s.st_mode);
}
std::string read_symlink(const char * link)
{
char result[PATH_MAX] {};
ssize_t count = readlink(link, result, PATH_MAX);
return std::string(result, (count > 0) ? count : 0);
}
std::string read_symlink(const std::string&& link)
{
return read_symlink(link.c_str());
}
std::string get_exe_path()
{
return read_symlink(PROCDIR "/self/exe");
}
std::string get_wine_exe_name(bool keep_ext)
{
const std::string exe_path = get_exe_path();
if (!ends_with(exe_path, "wine-preloader") && !ends_with(exe_path, "wine64-preloader")) {
return std::string();
}
std::string line = read_line(PROCDIR "/self/comm"); // max 16 characters though
if (ends_with(line, ".exe", true))
{
auto dot = keep_ext ? std::string::npos : line.find_last_of('.');
return line.substr(0, dot);
}
std::ifstream cmdline(PROCDIR "/self/cmdline");
// Iterate over arguments (separated by NUL byte).
while (std::getline(cmdline, line, '\0')) {
auto n = std::string::npos;
if (!line.empty()
&& ((n = line.find_last_of("/\\")) != std::string::npos)
&& n < line.size() - 1) // have at least one character
{
auto dot = keep_ext ? std::string::npos : line.find_last_of('.');
if (dot < n)
dot = line.size();
return line.substr(n + 1, dot - n - 1);
}
else if (ends_with(line, ".exe", true))
{
auto dot = keep_ext ? std::string::npos : line.find_last_of('.');
return line.substr(0, dot);
}
}
return std::string();
}
std::string get_home_dir()
{
std::string path;
const char* p = getenv("HOME");
if (p)
path = p;
return path;
}
std::string get_data_dir()
{
const char* p = getenv("XDG_DATA_HOME");
if (p)
return p;
std::string path = get_home_dir();
if (!path.empty())
path += "/.local/share";
return path;
}
std::string get_config_dir()
{
const char* p = getenv("XDG_CONFIG_HOME");
if (p)
return p;
std::string path = get_home_dir();
if (!path.empty())
path += "/.config";
return path;
}
bool lib_loaded(const std::string& lib) {
fs::path path(PROCDIR "/self/map_files/");
for (auto& p : fs::directory_iterator(path)) {
auto file = p.path().string();
auto sym = read_symlink(file.c_str());
if (sym.find(lib) != std::string::npos) {
return true;
}
}
return false;
}
std::string remove_parentheses(const std::string& text) {
// Remove parentheses and text between them
std::regex pattern("\\([^)]*\\)");
return std::regex_replace(text, pattern, "");
}
#endif // __linux__