Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/Cherry/Core/include/common.h
2 views
1
/*
2
* Gearcoleco - ColecoVision Emulator
3
* Copyright (C) 2021 Ignacio Sanchez
4
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* any later version.
9
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see http://www.gnu.org/licenses/
17
*
18
*/
19
20
#ifndef COMMON_H
21
#define COMMON_H
22
23
#include <stdlib.h>
24
#include <string>
25
#include <string.h>
26
#include <fstream>
27
#include <time.h>
28
#if defined(_WIN32)
29
#include <direct.h>
30
#include <windows.h>
31
#endif
32
#include "definitions.h"
33
#include "log.h"
34
35
inline u16 read_u16_le(const u8* p)
36
{
37
return (u16)p[0] | ((u16)p[1] << 8);
38
}
39
40
inline u32 read_u32_le(const u8* p)
41
{
42
return (u32)p[0] | ((u32)p[1] << 8) | ((u32)p[2] << 16) | ((u32)p[3] << 24);
43
}
44
45
inline u16 read_u16_be(const u8* p)
46
{
47
return (u16)p[1] | ((u16)p[0] << 8);
48
}
49
50
inline u32 read_u32_be(const u8* p)
51
{
52
return (u32)p[3] | ((u32)p[2] << 8) | ((u32)p[1] << 16) | ((u32)p[0] << 24);
53
}
54
55
inline u16 hi(u16 a)
56
{
57
return (u16)(a >> 8);
58
}
59
60
inline u16 lo(u16 a)
61
{
62
return (u16)(a & 0xFF);
63
}
64
65
inline int as_hex(const char c)
66
{
67
if (c >= '0' && c <= '9')
68
return c - '0';
69
if (c >= 'a' && c <= 'f')
70
return c - 'a' + 0xA;
71
if (c >= 'A' && c <= 'F')
72
return c - 'A' + 0xA;
73
return 0;
74
}
75
76
inline unsigned int pow_2_ceil(u16 n)
77
{
78
--n;
79
n |= n >> 1;
80
n |= n >> 2;
81
n |= n >> 4;
82
n |= n >> 8;
83
++n;
84
return n;
85
}
86
87
inline void get_date_time_string(time_t timestamp, char* buffer, size_t size)
88
{
89
struct tm* timeinfo = localtime(&timestamp);
90
strftime(buffer, size, "%Y-%m-%d %H:%M:%S", timeinfo);
91
}
92
93
inline void get_current_date_time_string(char* buffer, size_t size)
94
{
95
time_t timestamp = time(NULL);
96
get_date_time_string(timestamp, buffer, size);
97
}
98
99
inline bool is_hex_digit(char c)
100
{
101
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
102
}
103
104
template<typename T>
105
inline bool parse_hex_string(const char* str, size_t len, T* result, size_t max_digits = sizeof(T) * 2)
106
{
107
if (len == 0 || len > max_digits)
108
return false;
109
110
*result = 0;
111
for (size_t i = 0; i < len; i++)
112
{
113
if (!is_hex_digit(str[i]))
114
return false;
115
116
*result = (*result << 4);
117
118
if (str[i] >= '0' && str[i] <= '9')
119
*result |= (str[i] - '0');
120
else if (str[i] >= 'a' && str[i] <= 'f')
121
*result |= (str[i] - 'a' + 10);
122
else // (str[i] >= 'A' && str[i] <= 'F')
123
*result |= (str[i] - 'A' + 10);
124
}
125
return true;
126
}
127
128
inline bool parse_hex_string(const char* str, size_t len, u8* result)
129
{
130
return parse_hex_string<u8>(str, len, result, 2);
131
}
132
133
inline bool parse_hex_string(const char* str, size_t len, u16* result)
134
{
135
return parse_hex_string<u16>(str, len, result, 4);
136
}
137
138
inline bool parse_hex_string(const char* str, size_t len, u32* result)
139
{
140
return parse_hex_string<u32>(str, len, result, 8);
141
}
142
143
inline char* strncpy_fit(char* dest, const char* src, size_t dest_size)
144
{
145
if (dest_size != 0)
146
dest_size -= 1;
147
148
return strncpy(dest, src, dest_size);
149
}
150
151
inline char* strncat_fit(char* dest, const char* src, size_t dest_size)
152
{
153
if (dest_size != 0)
154
dest_size -= strlen(dest) + 1;
155
156
return strncat(dest, src, dest_size);
157
}
158
159
#if defined(_WIN32)
160
inline std::wstring utf8_to_wstring(const char* utf8_str)
161
{
162
if (!utf8_str || utf8_str[0] == '\0')
163
return std::wstring();
164
165
int size_needed = MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, NULL, 0);
166
if (size_needed <= 0)
167
return std::wstring();
168
169
std::wstring wstr(size_needed, 0);
170
MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, &wstr[0], size_needed);
171
return wstr;
172
}
173
174
inline FILE* fopen_utf8(const char* path, const char* mode)
175
{
176
std::wstring wpath = utf8_to_wstring(path);
177
std::wstring wmode = utf8_to_wstring(mode);
178
if (wpath.empty() || wmode.empty())
179
return NULL;
180
return _wfopen(wpath.c_str(), wmode.c_str());
181
}
182
183
inline void open_ifstream_utf8(std::ifstream& stream, const char* path, std::ios_base::openmode mode = std::ios_base::in)
184
{
185
if (!path)
186
return;
187
188
std::wstring wpath = utf8_to_wstring(path);
189
if (wpath.empty())
190
return;
191
192
stream.open(wpath.c_str(), mode);
193
}
194
195
inline void open_ofstream_utf8(std::ofstream& stream, const char* path, std::ios_base::openmode mode = std::ios_base::out)
196
{
197
if (!path)
198
return;
199
200
std::wstring wpath = utf8_to_wstring(path);
201
if (wpath.empty())
202
return;
203
204
stream.open(wpath.c_str(), mode);
205
}
206
#else
207
inline FILE* fopen_utf8(const char* path, const char* mode)
208
{
209
return fopen(path, mode);
210
}
211
212
inline void open_ifstream_utf8(std::ifstream& stream, const char* path, std::ios_base::openmode mode = std::ios_base::in)
213
{
214
stream.open(path, mode);
215
}
216
217
inline void open_ofstream_utf8(std::ofstream& stream, const char* path, std::ios_base::openmode mode = std::ios_base::out)
218
{
219
stream.open(path, mode);
220
}
221
#endif
222
223
#endif /* COMMON_H */
224
225