-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathutil.h
222 lines (187 loc) · 5.21 KB
/
util.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
/*
Common code for Gust (Koei/Tecmo) PC games tools
Copyright © 2019-2021 VitaSmith
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
#if !defined(_WIN32)
#include <libgen.h>
#endif
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#pragma once
#define _STRINGIFY(x) #x
#define STRINGIFY(x) _STRINGIFY(x)
#ifndef APP_VERSION
#define APP_VERSION_STR "[DEV VERSION]"
#else
#define APP_VERSION_STR STRINGIFY(APP_VERSION)
#endif
#if defined(_WIN32)
#include <windows.h>
#define ftell64 _ftelli64
#define fseek64 _fseeki64
#if !defined(S_ISDIR)
#define S_ISDIR(ST_MODE) (((ST_MODE) & _S_IFMT) == _S_IFDIR)
#endif
#if !defined(S_ISREG)
#define S_ISREG(ST_MODE) (((ST_MODE) & _S_IFMT) == _S_IFREG)
#endif
#define CREATE_DIR(path) CreateDirectory_utf8(path, NULL)
#define PATH_SEP '\\'
#else
#if defined(__APPLE__)
#define ftell64 ftello
#define fseek64 fseeko
#else
#define ftell64 ftello64
#define fseek64 fseeko64
#endif
#define CREATE_DIR(path) (mkdir(path, 0755) == 0)
#define PATH_SEP '/'
#endif
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef array_size
#define array_size(a) (sizeof(a) / sizeof(*a))
#endif
#ifndef is_power_of_2
#define is_power_of_2(x) (((x) & ((x) - 1)) == 0)
#endif
#if defined(_WIN32)
char* _basename_win32(const char* path, bool remove_extension);
char* _dirname_win32(const char* path);
#define _basename(path) _basename_win32(path, false)
#define _appname(path) _basename_win32(path, true)
#define _dirname(path) _dirname_win32(path)
#else
char* _basename_unix(const char* path);
char* _dirname_unix(const char* path);
#define _basename(path) _basename_unix(path)
#define _appname(path) _basename_unix(path)
#define _dirname(path) _dirname_unix(path)
#endif
#if defined (_MSC_VER)
#include <stdlib.h>
#define bswap_uint16 _byteswap_ushort
#define bswap_uint32 _byteswap_ulong
#define bswap_uint64 _byteswap_uint64
#else
#define bswap_uint16 __builtin_bswap16
#define bswap_uint32 __builtin_bswap32
#define bswap_uint64 __builtin_bswap64
#endif
// Returns the position of the msb. v should be nonzero
static __inline uint32_t find_msb(uint32_t v)
{
#if defined (_MSC_VER)
DWORD pos;
_BitScanReverse(&pos, v);
return pos;
#else
return 31- __builtin_clz(v);
#endif
}
static __inline uint16_t getle16(const void* p)
{
return *(const uint16_t*)(const uint8_t*)(p);
}
static __inline void setle16(const void* p, const uint16_t v)
{
*((uint16_t*)p) = v;
}
static __inline uint16_t getbe16(const void* p)
{
return bswap_uint16(getle16(p));
}
static __inline void setbe16(const void* p, const uint16_t v)
{
setle16(p, bswap_uint16(v));
}
static __inline uint32_t getle24(const void* _p)
{
uint8_t* p = (uint8_t*)_p;
return p[0] | (p[1] << 8) | (p[2] << 16);
}
static __inline void setle24(const void* _p, const uint32_t v)
{
uint8_t* p = (uint8_t*)_p;
p[0] = v & 0xff;
p[1] = (v >> 8) & 0xff;
p[2] = (v >> 16) & 0xff;
}
static __inline uint32_t getbe24(const void* _p)
{
uint8_t* p = (uint8_t*)_p;
return (p[0] << 16) | (p[1] << 8) | p[2];
}
static __inline void setbe24(const void* _p, const uint32_t v)
{
uint8_t* p = (uint8_t*)_p;
p[0] = (v >> 16) & 0xff;
p[1] = (v >> 8) & 0xff;
p[2] = v & 0xff;
}
static __inline uint32_t getle32(const void* p)
{
return *(const uint32_t*)(const uint8_t*)(p);
}
static __inline void setle32(const void* p, const uint32_t v)
{
*((uint32_t*)p) = v;
}
static __inline uint32_t getbe32(const void* p)
{
return bswap_uint32(getle32(p));
}
static __inline void setbe32(const void* p, const uint32_t v)
{
setle32(p, bswap_uint32(v));
}
static __inline uint64_t getle64(const void* p)
{
return *(const uint64_t*)(const uint8_t*)(p);
}
static __inline void setle64(const void* p, const uint64_t v)
{
*((uint64_t*)p) = v;
}
static __inline uint64_t getbe64(const void* p)
{
return bswap_uint64(getle64(p));
}
static __inline void setbe64(const void* p, const uint64_t v)
{
setle64(p, bswap_uint64(v));
}
bool create_path(char* path);
char* change_extension(const char* path, const char* extension);
size_t get_trailing_slash(const char* path);
bool is_file(const char* path);
bool is_directory(const char* path);
uint32_t read_file_max(const char* path, uint8_t** buf, uint32_t max_size);
#define read_file(path, buf) read_file_max(path, buf, 0)
uint64_t get_file_size(const char* path);
void create_backup(const char* path);
bool write_file(const uint8_t* buf, const uint32_t size, const char* path, const bool backup);