-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsdk_library_handler_unix.cpp
More file actions
157 lines (125 loc) · 3.13 KB
/
Copy pathsdk_library_handler_unix.cpp
File metadata and controls
157 lines (125 loc) · 3.13 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
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
#include "sdk_library_handler.h"
#include <dlfcn.h>
#include <string>
#include <vector>
#ifdef __APPLE__
#include <CoreFoundation/CoreFoundation.h>
static std::string getBundleFrameworkPath()
{
CFBundleRef appBundle = CFBundleGetMainBundle();
CFURLRef bundleUrlRef = CFBundleCopyBundleURL(appBundle);
CFURLRef frameworkUrlRef = CFBundleCopyPrivateFrameworksURL(appBundle);
CFStringRef frameworkPathRef = CFURLCopyPath(frameworkUrlRef);
CFURLRef frameworkFullUrlRef = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, bundleUrlRef, frameworkPathRef, true);
CFStringRef frameworkFullPathRef = CFURLCopyFileSystemPath(frameworkFullUrlRef, kCFURLPOSIXPathStyle);
CFIndex length = CFStringGetLength(frameworkFullPathRef);
CFIndex byteSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingASCII);
std::string result;
try {
std::vector<char> strData(byteSize + 1, '\0');
bool ok = CFStringGetCString(frameworkFullPathRef, strData.data(), strData.size(), kCFStringEncodingASCII);
if (ok) {
result = std::string(strData.data());
}
}
catch(...) { }
CFRelease(frameworkFullPathRef);
CFRelease(frameworkFullUrlRef);
CFRelease(frameworkPathRef);
CFRelease(frameworkUrlRef);
CFRelease(bundleUrlRef);
return result;
}
#else
#include <libgen.h>
#include <linux/limits.h>
#include <unistd.h>
static std::string getCurrentExecutableDir()
{
char path[PATH_MAX];
ssize_t count = readlink("/proc/self/exe", path, sizeof(path));
if (count < 1) {
return std::string();
}
return std::string(dirname(path));
}
#endif
static std::string getLibName()
{
const std::string name = "libtsvb";
#ifdef __APPLE__
const std::string ext = ".dylib";
#else
const std::string ext = ".so";
#endif
return name + ext;
}
class BGLibraryHandler::Impl
{
void* _handle = nullptr;
tsvb::pfnCreateSDKFactory _createFactory = nullptr;
public:
Impl() = default;
~Impl()
{
if (nullptr != _handle) {
dlclose(_handle);
_handle = nullptr;
}
}
bool isValid() const
{
return
(nullptr != _handle) &&
(nullptr != _createFactory);
}
void load(const char* path)
{
void* handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
if (nullptr == handle) {
return;
}
_createFactory = reinterpret_cast<tsvb::pfnCreateSDKFactory>(
dlsym(handle, "createSDKFactory")
);
if (nullptr != _createFactory) {
_handle = handle;
}
else {
dlclose(handle);
}
}
tsvb::ISDKFactory* createFactory()
{
if (isValid()) {
return _createFactory();
}
return nullptr;
}
};
BGLibraryHandler::BGLibraryHandler():
_impl(new Impl)
{
#ifdef __APPLE__
const std::string path = getBundleFrameworkPath() + "/" + getLibName();
#else
const std::string absolutePath = getCurrentExecutableDir() + "/" + getLibName();
bool exists = (access(absolutePath.c_str(), 0) == 0);
const std::string path = exists ? absolutePath : getLibName();
#endif
_impl->load(path.c_str());
}
BGLibraryHandler::~BGLibraryHandler()
{
}
tsvb::ISDKFactory* BGLibraryHandler::createSDKFactory()
{
if ((nullptr != _impl) && _impl->isValid()) {
return _impl->createFactory();
}
return nullptr;
}
bool BGLibraryHandler::isValid() const
{
return _impl->isValid();
}