-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.cpp
More file actions
477 lines (421 loc) · 10.3 KB
/
utils.cpp
File metadata and controls
477 lines (421 loc) · 10.3 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Copyright (C) 2015-2025 Marco Bortolin
*
* This file is part of IBMulator.
*
* IBMulator 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.
*
* IBMulator 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 IBMulator. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* cstr_*() functions copyright (C) 2022 The DOSBox Staging Team
*/
#include <string>
#include <sstream>
#include <array>
#include <algorithm>
#include <regex>
#include <numeric>
#include <iomanip>
#include <cstring>
#include <iconv.h>
#include "utils.h"
#ifdef _WIN32
#include "wincompat.h"
#endif
bool is_ascii_digit(uint8_t _byte)
{
return (_byte >= '0' && _byte <= '9');
}
bool is_ascii_letter(uint8_t _byte)
{
return (_byte >= 'A' && _byte <= 'Z') || (_byte >= 'a' && _byte <= 'z');
}
int str_parse_int_num(const std::string &_str)
{
const char* value = _str.c_str();
char* end;
// This parses "1234" (decimal) and also "0x4D2" (hex)
int n = strtol(value, &end, 0);
if(end <= value) {
throw std::runtime_error(str_format("not an integer number: '%s'", _str.c_str()));
}
return n;
}
double str_parse_real_num(const std::string &_str)
{
const char *value = _str.c_str();
char *end;
double n = strtod(value, &end);
if(end <= value) {
throw std::runtime_error(str_format("not a real number: '%s'", _str.c_str()));
}
return n;
}
bool str_is_int_num(const std::string &_str)
{
const char *value = _str.c_str();
char *end;
strtol(value, &end, 0);
return (end > value);
}
bool str_is_real_num(const std::string &_str)
{
const char *value = _str.c_str();
char *end;
strtod(value, &end);
return (end > value);
}
bool str_is_numeric(const std::string &_str)
{
return str_is_int_num(_str) || str_is_real_num(_str);
}
std::string str_implode(const std::vector<std::string> &_list, const std::string &_delim)
{
// thanks, random internet stranger @stackoverflow!
return std::accumulate(_list.begin(), _list.end(), std::string(),
[&](const std::string& a, const std::string& b) -> std::string {
return a + (a.length() > 0 ? _delim : "") + b;
} );
}
std::string str_implode(const std::vector<const char*> &_list, const std::string &_delim)
{
std::vector<std::string> list;
for(auto s : _list) {
list.push_back(std::string(s));
}
return str_implode(list, _delim);
}
void str_replace_all(std::string &_str, const std::string &_search, const std::string &_replace)
{
std::string::size_type i = _str.find(_search);
while(i != std::string::npos) {
_str.replace(i, _search.length(), _replace);
i = _str.find(_search, i+_replace.length());
}
}
std::string str_replace_all_const(const std::string &_str, const std::string &_search, const std::string &_replace)
{
std::string subj = _str;
str_replace_all(subj, _search, _replace);
return subj;
}
std::string str_to_lower(std::string _str)
{
std::transform(_str.begin(), _str.end(), _str.begin(), ::tolower);
return _str;
}
std::string str_to_upper(std::string _str)
{
std::transform(_str.begin(), _str.end(), _str.begin(), ::toupper);
return _str;
}
std::string str_trim(std::string _str)
{
const char* ws = " \t\n\r\f\v";
_str.erase(0, _str.find_first_not_of(ws));
_str.erase(_str.find_last_not_of(ws) + 1);
return _str;
}
char * cstr_to_lower(char *_str)
{
for(char *idx = _str; *idx; idx++) {
*idx = tolower(*reinterpret_cast<unsigned char*>(idx));
}
return _str;
}
char * cstr_to_upper(char *_str)
{
for(char *idx = _str; *idx; idx++) {
*idx = toupper(*reinterpret_cast<unsigned char*>(idx));
}
return _str;
}
char * cstr_ltrim(char *_str)
{
while (*_str && isspace(*reinterpret_cast<unsigned char *>(_str))) {
_str++;
}
return _str;
}
char * cstr_rtrim(char *_str)
{
char *p;
p = strchr(_str, '\0');
while (--p >= _str && isspace(*reinterpret_cast<unsigned char *>(p))) {};
p[1] = '\0';
return _str;
}
char * cstr_trim(char *_str)
{
return cstr_ltrim(cstr_rtrim(_str));
}
std::string str_compress_spaces(std::string _str)
{
static const std::regex spaces_re("[' ']{2,}");
return std::regex_replace(_str, spaces_re, " ");
}
std::vector<std::string> str_parse_tokens_re(std::string _str, const std::regex &_regex)
{
std::vector<std::string> tokens;
std::sregex_token_iterator it{_str.begin(), _str.end(), _regex, -1}, end;
for(; it != end; it++) {
auto s = str_trim(*it);
if(!s.empty()) {
tokens.push_back(s);
}
}
return tokens;
}
std::vector<std::string> str_parse_tokens(std::string _str, std::string _regex_sep)
{
return str_parse_tokens_re(_str, std::regex(_regex_sep));
}
std::string::const_iterator str_find_ci(const std::string &_haystack, const std::string &_needle)
{
return std::search(
_haystack.begin(), _haystack.end(),
_needle.begin(), _needle.end(),
[](unsigned char ch1, unsigned char ch2) { return std::toupper(ch1) == std::toupper(ch2); }
);
}
std::string str_format_time(time_t _time, const std::string &_fmt)
{
#if 1
std::string old_locale(setlocale(LC_TIME, NULL));
setlocale(LC_TIME, "");
std::string s(100,0);
size_t size = std::strftime(s.data(), 100, _fmt.c_str(), std::localtime(&_time));
setlocale(LC_TIME, old_locale.c_str());
if(size == 0) {
return "";
}
s.resize(size);
return s;
#else
// In MinGW the only currently supported locale is "C".
// Practically C++ locales work only on Linux.
// Keeping this implementation here waiting for better times.
std::stringstream s;
try {
s.imbue(std::locale(""));
} catch(std::runtime_error &) {
// It throws an exception when compiled with MinGW and run in a terminal.
}
auto &tmput = std::use_facet<std::time_put<char>>(s.getloc());
std::tm *my_time = std::localtime(&_time);
tmput.put({s}, s, ' ', my_time, &_fmt[0], &_fmt[0]+_fmt.size());
return s.str();
#endif
}
const char * ASCII_control_characters[32] = {
"NUL", // 00
"SOH", // 01
"STX", // 02
"ETX", // 03
"EOT", // 04
"ENQ", // 05
"ACK", // 06
"BEL", // 07
"BS", // 08
"HT", // 09
"LF", // 0A
"VT", // 0B
"FF", // 0C
"CR", // 0D
"SO", // 0E
"SI", // 0F
"DLE", // 10
"DC1", // 11
"DC2", // 12
"DC3", // 13
"DC4", // 14
"NAK", // 15
"SYN", // 16
"ETB", // 17
"CAN", // 18
"EM", // 19
"SUB", // 1A
"ESC", // 1B
"FS", // 1C
"GS", // 1D
"RS", // 1E
"US" // 1F
};
std::string str_format_special(const char *_str)
{
std::string str;
char buf[20] = {0};
while(_str && *_str) {
if(*_str < 0 || *_str == 127) {
snprintf(&buf[0], 20, "<%02X>", uint8_t(*_str));
str += buf;
} else if(*_str <= 31) {
snprintf(&buf[0], 10, "<%s>", ASCII_control_characters[int(*_str)]);
str += buf;
} else {
str += *_str;
}
_str++;
}
return str;
}
std::string str_format_special(char _ch)
{
std::string str;
char buf[20] = {0};
if(_ch < 0 || _ch == 127) {
snprintf(&buf[0], 20, "<%02X>", uint8_t(_ch));
return std::string(buf);
} else if(_ch <= 31) {
snprintf(&buf[0], 10, "<%s>", ASCII_control_characters[int(_ch)]);
return std::string(buf);
} else {
return std::string(1, _ch);
}
}
std::string str_convert(std::string _str, const char *_to_code, const char *_from_code)
{
std::string buf;
buf.resize(_str.size() * 2);
iconv_t cd = iconv_open(_to_code, _from_code);
if(cd == (iconv_t)(-1)) {
return "";
}
size_t inbytes = _str.size();
size_t outbytes = buf.size();
char *in = _str.data();
char *out = buf.data();
size_t rc = iconv(cd, &in, &inbytes, &out, &outbytes);
iconv_close(cd);
if(rc == (size_t)(-1)) {
return "";
}
buf.resize(outbytes);
return buf;
}
bool str_convert_is_valid(const char *_to_code, const char *_from_code)
{
iconv_t cd = iconv_open(_to_code, _from_code);
bool valid = cd != (iconv_t)(-1);
iconv_close(cd);
return valid;
}
std::string str_to_html(std::string _text, bool _nbsp)
{
if(_nbsp) {
str_replace_all(_text, " ", " ");
}
str_replace_all(_text, "<", "<");
str_replace_all(_text, ">", ">");
str_replace_all(_text, "\n", "<br />");
return _text;
}
std::string bitfield_to_string(uint8_t _bitfield,
const std::array<std::string, 8> &_set_names)
{
return bitfield_to_string(_bitfield, _set_names, {"","","","","","","",""});
}
std::string bitfield_to_string(uint8_t _bitfield,
const std::array<std::string, 8> &_set_names,
const std::array<std::string, 8> &_clear_names)
{
std::string s;
for(int i=7; i>=0; i--) {
if(_bitfield & 1<<i) {
if(!_set_names[i].empty()) {
s += _set_names[i] + " ";
}
} else {
if(!_clear_names[i].empty()) {
s += _clear_names[i] + " ";
}
}
}
if(!s.empty()) {
s.pop_back();
}
return s;
}
const char *register_to_string(uint8_t _register,
const std::vector<std::pair<int, std::string> > &_fields)
{
std::ostringstream sstr;
thread_local static std::string s;
sstr << std::hex;
int pos = 0;
for(auto const &p : _fields) {
int bits = p.first;
int mask = 0;
while(bits--) {
mask |= 1<<bits;
}
if(!p.second.empty()) {
sstr << p.second << "=" << (_register >> pos & mask) << " ";
}
pos += p.first;
if(pos > 7) {
break;
}
}
s = sstr.str();
if(!s.empty()) {
s.pop_back();
}
return s.c_str();
}
std::string bytearray_to_string(const uint8_t *_data, unsigned _len)
{
std::stringstream ss;
ss << std::setfill('0');
ss << "[";
for(unsigned i=0; i<_len; i++) {
ss << std::hex << std::setw(2) << int(_data[i]);
if(i<_len-1) {
ss << "|";
}
}
ss << "]";
return ss.str();
}
std::string get_error_string(int _error_id)
{
#ifdef _WIN32
if(_error_id == 0) {
_error_id = ::GetLastError();
if(_error_id == NO_ERROR) {
return std::string("No error");
}
}
LPWSTR buff = nullptr;
DWORD size = FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, //lpSource
_error_id, //dwMessageId
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //dwLanguageId
(LPWSTR)&buff, //lpBuffer
0, //nSize
NULL //Arguments
);
std::wstring message(buff, size);
LocalFree(buff);
return str_trim(utf8::narrow(buff));
#else
if(_error_id == 0) {
_error_id = errno;
if(_error_id == 0) {
return std::string("No error");
}
}
return std::string(::strerror(errno));
#endif
}