-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cpp
160 lines (141 loc) · 4.67 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
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
#include "Utils.h"
#include "Logger.h"
#include <locale>
#include <codecvt>
#include <fstream>
#include <sstream>
namespace lwscript
{
STD_STRING ReadFile(std::string_view path)
{
#ifdef LWSCRIPT_UTF8_ENCODE
auto utf8Path = Utf8::Decode(path.data());
Logger::Record::mCurFilePath = utf8Path;
#else
Logger::Record::mCurFilePath = path;
#endif
STD_IFSTREAM file;
file.open(Logger::Record::mCurFilePath.c_str(), std::ios::in | std::ios::binary);
if (!file.is_open())
Logger::Error(TEXT("Failed to open file:{}"), Logger::Record::mCurFilePath);
STD_STRING_STREAM sstream;
sstream << file.rdbuf();
file.close();
return sstream.str();
}
void WriteBinaryFile(std::string_view path, const std::vector<uint8_t> &content)
{
std::ofstream file;
file.open(path.data(), std::ios::out | std::ios::binary);
if (!file.is_open())
#ifdef LWSCRIPT_UTF8_ENCODE
{
auto utf8Path = Utf8::Decode(path.data());
Logger::Error(TEXT("Failed to open file:{}"), utf8Path);
}
#else
Logger::Error(TEXT("Failed to open file:{}"), path);
#endif
file.write((const char *)content.data(), content.size());
file.close();
}
std::vector<uint8_t> LWSCRIPT_API ReadBinaryFile(std::string_view path)
{
std::ifstream file;
file.open(path.data(), std::ios::in | std::ios::binary);
if (!file.is_open())
#ifdef LWSCRIPT_UTF8_ENCODE
{
auto utf8Path = Utf8::Decode(path.data());
Logger::Error(TEXT("Failed to open file:{}"), utf8Path);
}
#else
Logger::Error(TEXT("Failed to open file:{}"), path);
#endif
std::stringstream sstream;
sstream << file.rdbuf();
file.close();
auto str = sstream.str();
std::vector<uint8_t> result;
result.assign(str.begin(), str.end());
return result;
}
STD_STRING PointerAddressToString(void *pointer)
{
STD_STRING_STREAM sstr;
sstr << pointer;
STD_STRING address = sstr.str();
return address;
}
int64_t Factorial(int64_t v, int64_t tmp)
{
if (v == 0)
return tmp;
return Factorial(v - 1, v * tmp);
}
uint64_t NormalizeIdx(int64_t idx, size_t dsSize)
{
bool isNeg = false;
if (idx < 0)
{
isNeg = true;
idx = abs(idx);
}
idx = idx % (int64_t)dsSize;
if (isNeg)
idx = dsSize - idx;
return idx;
}
namespace Utf8
{
std::string Encode(const std::wstring &str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.to_bytes(str);
}
std::wstring Decode(const std::string &str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.from_bytes(str);
}
}
namespace ByteConverter
{
std::array<uint8_t, 8> ToU64ByteList(int64_t integer)
{
std::array<uint8_t, 8> result{};
result[0] = (uint8_t((integer & 0xFF00000000000000) >> 56));
result[1] = (uint8_t((integer & 0x00FF000000000000) >> 48));
result[2] = (uint8_t((integer & 0x0000FF0000000000) >> 40));
result[3] = (uint8_t((integer & 0x000000FF00000000) >> 32));
result[4] = (uint8_t((integer & 0x00000000FF000000) >> 24));
result[5] = (uint8_t((integer & 0x0000000000FF0000) >> 16));
result[6] = (uint8_t((integer & 0x000000000000FF00) >> 8));
result[7] = (uint8_t((integer & 0x00000000000000FF) >> 0));
return result;
}
uint64_t GetU64Integer(const std::vector<uint8_t> data, uint32_t start)
{
uint64_t v{0};
for (int32_t i = 0; i < 8; ++i)
v |= ((uint64_t)(data[start + i] & 0x00000000000000FF) << ((7 - i) * 8));
return v;
}
std::array<uint8_t, 4> ToU32ByteList(int32_t integer)
{
std::array<uint8_t, 4> result{};
result[0] = (uint8_t((integer & 0xFF000000) >> 24));
result[1] = (uint8_t((integer & 0x00FF0000) >> 16));
result[2] = (uint8_t((integer & 0x0000FF00) >> 8));
result[3] = (uint8_t((integer & 0x000000FF) >> 0));
return result;
}
uint32_t GetU32Integer(const std::vector<uint8_t> data, uint32_t start)
{
uint32_t v{0};
for (int32_t i = 0; i < 4; ++i)
v |= ((uint32_t)(data[start + i] & 0x000000FF) << ((3 - i) * 8));
return v;
}
}
}