-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathggwave-common.cpp
79 lines (63 loc) · 1.68 KB
/
ggwave-common.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
#include "ggwave-common.h"
#if !defined(_WIN32)
#include <dlfcn.h>
#include <unistd.h>
#endif
#include <cstring>
#include <fstream>
#include <iterator>
namespace {
void dummy() {}
}
std::map<std::string, std::string> parseCmdArguments(int argc, char ** argv) {
int last = argc;
std::map<std::string, std::string> res;
for (int i = 1; i < last; ++i) {
if (argv[i][0] == '-') {
if (strlen(argv[i]) > 1) {
res[std::string(1, argv[i][1])] = strlen(argv[i]) > 2 ? argv[i] + 2 : "";
}
}
}
return res;
}
std::vector<char> readFile(const char* filename) {
std::ifstream file(filename, std::ios::binary);
if (!file.is_open() || !file.good()) return {};
file.unsetf(std::ios::skipws);
std::streampos fileSize;
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> vec;
vec.reserve(fileSize);
vec.insert(vec.begin(),
std::istream_iterator<char>(file),
std::istream_iterator<char>());
return vec;
}
std::string getBinaryPath() {
#if defined(__EMSCRIPTEN__) || defined(_WIN32)
return "";
#else
std::string result;
void* p = reinterpret_cast<void*>(dummy);
Dl_info info;
dladdr(p, &info);
if (*info.dli_fname == '/') {
result = info.dli_fname;
} else {
char buff[2048];
auto len = readlink("/proc/self/exe", buff, sizeof(buff) - 1);
if (len > 0) {
buff[len] = 0;
result = buff;
}
}
auto slash = result.rfind('/');
if (slash != std::string::npos) {
result.erase(slash + 1);
}
return result;
#endif
}