-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommon.h
312 lines (260 loc) · 8.34 KB
/
common.h
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#ifndef LIBIM_COMMON_H
#define LIBIM_COMMON_H
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <string>
#include <climits>
#include <filesystem>
#include <ios>
#include <memory>
#include <span>
#include <string>
#include <string_view>
#include <vector>
#include <type_traits>
#include <utility>
#include "platform.h"
#include <libim/types/optref.h>
#include <libim/utils/utils.h>
#ifdef PACKED
# undef PACKED
#endif
#ifndef _MSC_VER
# define PACKED( ... ) __VA_ARGS__ __attribute__((packed, aligned(1)))
#else
# define PACKED( ... ) __pragma( pack(push, 1) ) __VA_ARGS__ __pragma( pack(pop) )
#endif
namespace libim {
static_assert(CHAR_BIT == 8, "byte bit count != 8");
static constexpr std::size_t CHAR_BYTE = 1;
static constexpr std::size_t INT8_BYTE = 1;
static constexpr std::size_t INT16_BYTE = 2;
static constexpr std::size_t INT32_BYTE = 4;
static constexpr std::size_t INT64_BYTE = 8;
static constexpr std::size_t FLOAT_BYTE = 4;
static constexpr std::size_t DOUBLE_BYTE = 8;
static constexpr std::size_t BYTE_BIT = CHAR_BIT;
constexpr inline uint32_t kStaticResourceIndexMask = 0x8000;
using byte_t = uint8_t;
using ByteArray = std::vector<byte_t>;
using ByteView = std::span<const byte_t>;
using MutableByteView = std::span<byte_t>;
template <typename T,
typename R = std::enable_if_t<std::is_integral<T>::value,
typename std::make_unsigned<T>::type>>
inline constexpr R abs(T val)
{
R mask = val >> ((2 << sizeof(val)) - 1);
return (mask ^ val) - mask;
}
inline constexpr uint32_t bbs(uint32_t bits) { //Bits byte size
return bits / BYTE_BIT;
}
constexpr inline bool isStaticResource(const uint32_t idx) {
return (idx & kStaticResourceIndexMask) != 0;
}
constexpr inline uint32_t getStaticResourceIdx(const uint32_t idx) {
return idx & ~kStaticResourceIndexMask;
}
constexpr inline uint32_t makeStaticResourceIdx(const uint32_t idx) {
return idx | kStaticResourceIndexMask;
}
static std::vector<std::string> splitString(const std::string_view string, const std::string_view delim)
{
std::vector<std::string> tokens;
std::size_t prevPos = 0;
std::size_t pos = 0;
while ((pos = string.find(delim, prevPos)) != std::string::npos)
{
std::string token = std::string(string.substr(prevPos, pos - prevPos));
tokens.emplace_back(std::move(token));
prevPos = ++pos;
}
if(prevPos < string.size()) {
tokens.push_back(std::string(string.substr(prevPos)));
}
return tokens;
}
inline std::vector<std::string> splitString(const std::string& string, char delim)
{
return splitString(string, std::string(1, delim));
}
inline constexpr char pathSeparator()
{
#ifdef LIBIM_OS_WINDOWS
return '\\';
#else
return '/';
#endif
}
inline constexpr char noneNativePathSeparator()
{
#ifdef LIBIM_OS_WINDOWS
return '/';
#else
return '\\';
#endif
}
inline bool isNativePath(const std::string_view path)
{
return path.find(noneNativePathSeparator()) == std::string::npos;
}
inline std::string getNativePath(const std::string_view path)
{
std::string nativePath(path);
std::replace_if
(
nativePath.begin(),
nativePath.end(),
[](char ch) { return ch == noneNativePathSeparator(); },
pathSeparator()
);
return nativePath;
}
inline std::string getFilename(const std::string_view path)
{
std::string name(path);
if(!isNativePath(name)) {
name = getNativePath(name);
}
size_t sep = name.find_last_of(pathSeparator());
if (sep != std::string::npos) {
name = name.substr(sep + 1, name.size() - sep - 1);
}
return name;
}
inline std::string getBaseName(const std::string_view path)
{
std::string name = getFilename(path);
size_t dot = name.find_last_of(".");
if (dot != std::string::npos) {
name = name.substr(0, dot);
}
return name;
}
inline std::string getFileExtension(const std::string_view path)
{
size_t dotPos = path.find_last_of(".");
if (dotPos == std::string::npos) {
return "";
}
return std::string(path.substr(dotPos + 1));
}
inline bool fileExtMatch(const std::filesystem::path& path, std::string_view ext, bool icase = true)
{
auto pext = path.extension().string();
if (pext.empty() != ext.empty()) {
return false;
}
const std::string_view svpext(pext);
if (icase) {
return utils::iequal(svpext, ext);
}
return svpext == ext;
}
inline bool isFilePath(const std::filesystem::path& path, OptionalRef<std::error_code> ec = std::nullopt)
{
using namespace std::filesystem;
if (ec) {
return is_regular_file(path, *ec) || path.has_extension();
}
return is_regular_file(path) || path.has_extension();
}
inline bool isFilePath(const std::string_view path, OptionalRef<std::error_code> ec)
{
return isFilePath(std::filesystem::path(path), ec);
}
inline bool fileExists(const std::filesystem::path& path)
{
if(path.empty() || !isFilePath(path)) {
return false;
}
return std::filesystem::exists(path);
}
inline bool isDirPath(const std::filesystem::path& path, OptionalRef<std::error_code> ec = std::nullopt)
{
using namespace std::filesystem;
if (ec) {
return is_directory(path, *ec) || !isFilePath(path);
}
return is_directory(path) || !isFilePath(path);
}
inline bool isDirPath(const std::string_view path, OptionalRef<std::error_code> ec)
{
return isDirPath(std::filesystem::path(path), ec);
}
inline bool dirExists(const std::filesystem::path& dirPath)
{
if(isFilePath(dirPath)) {
return false;
}
else if(!isNativePath(dirPath.string())) {
return dirExists(getNativePath(dirPath.string()));
}
return std::filesystem::exists(dirPath);
}
inline bool makeDir(const std::filesystem::path& dirName)
{
return std::filesystem::create_directory(dirName);
}
static bool makePath(const std::string& path, bool createFile = false)
{
if(path.empty()) {
return false;
}
else if(!isNativePath(path)) {
return makePath(getNativePath(path));
}
std::string currentPath = path.at(0) == pathSeparator() ? std::string(1, pathSeparator()) : "";
auto pathParts = splitString(path, pathSeparator());
for(auto&& part : pathParts)
{
currentPath += std::move(part);
if(!isFilePath(currentPath))
{
if(!dirExists(currentPath) && !makeDir(currentPath)) {
return false;
}
}
else if(createFile && !fileExists(currentPath)) {
// TODO: make file
break;
}
currentPath += std::string(1, pathSeparator());
}
return true;
}
inline bool makePath(const std::filesystem::path& path, bool createFile = false)
{
return makePath(path.string(), createFile);
}
inline bool removeFile(const std::filesystem::path& file, OptionalRef<std::error_code> ec = std::nullopt)
{
if(ec) {
return std::filesystem::remove(file, *ec);
}
return std::filesystem::remove(file);
}
inline bool deleteFile(const std::filesystem::path& file)
{
static std::error_code ec;
return removeFile(file, ec);
}
inline bool renameFile(const std::filesystem::path& from, const std::filesystem::path& to, bool override = true)
{
if(fileExists(to) && !override) {
return false;
}
deleteFile(to);
try
{
std::filesystem::rename(from, to);
return true;
}
catch (...) {
return false;
}
}
}
#endif // LIBIM_COMMON_H