-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cpp
96 lines (82 loc) · 2.12 KB
/
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
#include "Utils.h"
#include <sstream>
#include <iostream>
#include <cstring>
#ifdef _WIN32
#include <Windows.h>
#elif __linux__
#include <dlfcn.h>
#elif __APPLE__
#warning "Apple platform not implement yet"
#endif
std::string ReadFile(std::string_view path)
{
std::fstream file;
file.open(path.data(), std::ios::in | std::ios::binary);
if (!file.is_open())
ASSERT("Failed to open file:%s", path.data());
std::stringstream sstream;
sstream << file.rdbuf();
file.close();
return sstream.str();
}
void WriteFile(std::string_view path,std::string_view content)
{
std::fstream f;
f.open(path.data(),std::ios::out);
f<<content;
f.close();
}
std::string PointerAddressToString(void *pointer)
{
std::stringstream sstr;
sstr << pointer;
std::string address = sstr.str();
return address;
}
void RegisterDLLs(std::string rawDllPath)
{
using RegFn = void (*)();
if (rawDllPath.find(".") == std::string::npos) // no file suffix
{
#ifdef _WIN32
rawDllPath = "./lib" + rawDllPath + ".dll";
#elif __linux__
rawDllPath = "./lib" + rawDllPath + ".so";
#elif __APPLE__
#error "Apple platform not implement yet"
#endif
}
#ifdef _WIN32
HINSTANCE hInstance = GetModuleHandleA(rawDllPath.c_str());
if (!hInstance)
{
hInstance = LoadLibraryA(rawDllPath.c_str());
if (!hInstance)
ASSERT("Failed to load dll library:%s", rawDllPath.c_str());
RegFn RegisterBuiltins = (RegFn)(GetProcAddress(hInstance, "RegisterBuiltins"));
RegisterBuiltins();
}
#elif __linux__
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen(rawDllPath.c_str(), RTLD_LAZY);
if (!handle)
ASSERT("Failed to load dll library:%s", rawDllPath.c_str());
RegFn RegisterBuiltins = (RegFn)(dlsym(handle, "RegisterBuiltins"));
RegisterBuiltins();
#elif __APPLE__
#error "Apple platform not implement yet"
#endif
}
uint32_t HashString(char *str)
{
uint32_t hash = 2166136261u;
for (size_t i = 0; i < strlen(str); i++)
{
hash ^= (uint8_t)str[i];
hash *= 16777619;
}
return hash;
}