forked from WohlSoft/PGE-File-Library-STL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pge_file_lib_private.h
412 lines (392 loc) · 11.7 KB
/
pge_file_lib_private.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
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
/*
* PGE File Library - a library to process file formats, part of Moondust project
*
* Copyright (c) 2014-2022 Vitaly Novichkov <[email protected]>
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#ifndef PGE_FILE_LIB_PRIVATE_H_
#define PGE_FILE_LIB_PRIVATE_H_
#include "pge_file_lib_globs.h"
#ifdef PGE_FILES_QT
#include <QString>
#include <QStringList>
#include <QFile>
#include <QTextStream>
#include <QTextCodec>
#include <QVector>
#include <QPair>
#include <QMap>
#include <QHash>
#include <QObject>
#include <QUrl>
#include <cmath>
#include <QSize>
typedef int pge_size_t;
#define PGEGetChar(chr) chr.toLatin1()
typedef QStringList PGESTRINGList;
#define PGEVECTOR QVector
typedef int pge_size_t;
#define PGEMAP QMap
#define PGEHASH QHash
typedef QFile PGEFILE;
inline PGESTRING PGESTR_Simpl(const PGESTRING &str)
{
return str.simplified();
}
inline PGESTRING PGESTR_toLower(const PGESTRING &str)
{
return str.toLower();
}
inline PGESTRING PGESTR_Trim(const PGESTRING &str)
{
return str.trimmed();
}
#define PGEGetChar(chr) chr.toLatin1()
typedef QChar PGEChar;
#define PGEVECTOR QVector
#define PGELIST QList
typedef int pge_size_t;
#define PGEPAIR QPair
#define PGEMAP QMap
#define PGEHASH QHash
typedef QFile PGEFILE;
inline void PGE_SPLITSTRING(PGESTRINGList &dst, const PGESTRING &src, PGESTRING sep)
{
dst = src.split(sep);
}
inline PGESTRING PGE_ReplSTRING(PGESTRING src, PGESTRING from, PGESTRING to)
{
return src.replace(from, to);
}
inline PGESTRING PGE_RemSubSTRING(const PGESTRING &src, const PGESTRING &substr)
{
return QString(src).remove(substr);
}
inline PGESTRING PGE_RemStrRng(PGESTRING &str, int pos, int len)
{
return str.remove(pos, len);
}
inline PGESTRING PGE_SubStr(const PGESTRING &str, int pos, int len = -1)
{
return str.mid(pos, len);
}
inline void PGE_CutLength(PGESTRING &str, int maxlength)
{
if(str.size() > maxlength)
str.resize(maxlength);
}
inline void PGE_FilterBinary(PGESTRING &str)
{
for(int i = 0; i < str.size(); i++)
{
PGEChar c = str[i];
if(c == PGEChar('\r'))
str[i] = PGEChar('r');
else if(c == PGEChar('\n'))
str[i] = PGEChar('n');
else if(c.toLatin1() < 0x20 || c.toLatin1() == 0x7F)
str[i] = PGEChar('?');
}
}
inline bool IsNULL(const PGESTRING &str)
{
return str.isNull();
}
inline bool IsEmpty(const PGESTRING &str)
{
return str.isEmpty();
}
inline bool IsEmpty(const PGESTRINGList &str)
{
return str.isEmpty();
}
inline int toInt(const PGESTRING &str)
{
return str.toInt();
}
inline unsigned int toUInt(const PGESTRING &str)
{
return str.toUInt();
}
inline long toLong(const PGESTRING &str)
{
return str.toLong();
}
inline unsigned long toULong(const PGESTRING &str)
{
return str.toULong();
}
inline float toFloat(const PGESTRING &str)
{
return str.toFloat();
}
inline double toDouble(const PGESTRING &str)
{
return str.toDouble();
}
inline PGESTRING removeSpaces(PGESTRING src)
{
return src.remove(' ');
}
template<typename T>
PGESTRING fromNum(T num)
{
return QString::number(num);
}
inline PGESTRING fromBoolToNum(bool num)
{
return QString::number(static_cast<int>(num));
}
namespace PGE_FileFormats_misc
{
PGESTRING url_encode(const PGESTRING &sSrc);
std::string base64_encode(uint8_t const *bytes_to_encode, size_t in_len, bool no_padding = false);
std::string base64_encode(std::string const &source, bool no_padding = false);
std::string base64_decode(std::string const &encoded_string);
QString base64_encode(QString &source, bool no_padding = false);
QString base64_decode(QString &source);
QString base64_encodeW(QString &source, bool no_padding = false);
QString base64_decodeW(QString &source);
QString base64_encodeA(QString &source, bool no_padding = false);
QString base64_decodeA(QString &source);
}
inline PGESTRING PGE_URLENC(const PGESTRING &src)
{
return PGE_FileFormats_misc::url_encode(src);
}
inline PGESTRING PGE_URLDEC(const PGESTRING &src)
{
/* Don't call fromPercentEncoding() on Windows with empty string,
* or crash will happen! */
if(IsEmpty(src))
return PGESTRING();
return QUrl::fromPercentEncoding(src.toUtf8());
}
#define PGE_BASE64ENC(src) PGE_FileFormats_misc::base64_encode(src)
#define PGE_BASE64ENC_nopad(src) PGE_FileFormats_misc::base64_encode(src, true)
#define PGE_BASE64DEC(src) PGE_FileFormats_misc::base64_decode(src)
#define PGE_BASE64ENC_W(src) PGE_FileFormats_misc::base64_encodeW(src)
#define PGE_BASE64DEC_W(src) PGE_FileFormats_misc::base64_decodeW(src)
#define PGE_BASE64ENC_A(src) PGE_FileFormats_misc::base64_encodeA(src)
#define PGE_BASE64DEC_A(src) PGE_FileFormats_misc::base64_decodeA(src)
#else /* ------ PGE_FILES_QT ------ */
#include <string>
#include <vector>
#include <utility>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
#include <cctype>
#include <unordered_map>
#ifdef _MSC_VER
static char ToLowerFun(char ch)
{
return static_cast<char>(::tolower(ch));
}
#else
#define ToLowerFun ::tolower
#endif
typedef std::string::size_type pge_size_t;
inline PGESTRING PGESTR_Simpl(PGESTRING str)
{
str.erase(std::remove_if(str.begin(), str.end(), ::isspace), str.end());
return str;
}
inline PGESTRING PGESTR_toLower(PGESTRING str)
{
std::transform(str.begin(), str.end(), str.begin(), ToLowerFun);
return str;
}
// trim from start (in place)
static inline void PGESTR_ltrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch)
{
return !std::isspace(ch);
}));
}
// trim from end (in place)
static inline void PGESTR_rtrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch)
{
return !std::isspace(ch);
}).base(), s.end());
}
inline PGESTRING PGESTR_Trim(const PGESTRING &str)
{
PGESTRING s = str;
PGESTR_ltrim(s);
PGESTR_rtrim(s);
return s;
}
#define PGEGetChar(chr) chr
#define PGEVECTOR std::vector
typedef size_t pge_size_t;
#define PGEMAP std::map
#define PGEHASH std::unordered_map
typedef std::fstream PGEFILE;
namespace PGE_FileFormats_misc
{
void split(std::vector<std::string> &dest, const std::string &str, const std::string& separator);
void replaceAll(std::string &str, const std::string &from, const std::string &to);
void RemoveSub(std::string &sInput, const std::string &sub);
bool hasEnding(std::string const &fullString, std::string const &ending);
PGESTRING url_encode(const PGESTRING &sSrc);
PGESTRING url_decode(const std::string &sSrc);
std::string base64_encode(unsigned char const *bytes_to_encode, size_t in_len, bool no_padding = false);
std::string base64_encode(std::string const &source, bool no_padding = false);
std::string base64_decode(std::string const &encoded_string);
std::string base64_encodeW(std::string &source, bool no_padding = false);
std::string base64_decodeW(std::string &source);
std::string base64_encodeA(std::string &source, bool no_padding = false);
std::string base64_decodeA(std::string &source);
}
inline void PGE_SPLITSTRING(PGESTRINGList &dst, const PGESTRING &src, const PGESTRING &sep)
{
dst.clear();
PGE_FileFormats_misc::split(dst, src, sep);
}
inline PGESTRING PGE_ReplSTRING(PGESTRING src, const PGESTRING &from, const PGESTRING &to)
{
PGE_FileFormats_misc::replaceAll(src, from, to);
return src;
}
inline PGESTRING PGE_RemSubSTRING(PGESTRING src, const PGESTRING &substr)
{
PGE_FileFormats_misc::RemoveSub(src, substr);
return src;
}
inline PGESTRING PGE_RemStrRng(PGESTRING &str, int pos, int len)
{
str.erase(static_cast<size_t>(pos), static_cast<size_t>(len));
return str;
}
inline PGESTRING PGE_SubStr(const PGESTRING &str, int pos, int len = -1)
{
return str.substr(static_cast<std::string::size_type>(pos), static_cast<std::string::size_type>(len));
}
inline void PGE_CutLength(PGESTRING &str, int maxlength)
{
if(str.size() > size_t(maxlength))
str.resize(size_t(maxlength));
}
inline void PGE_FilterBinary(PGESTRING &str)
{
for(size_t i = 0; i < str.size(); i++)
{
PGEChar &c = str[i];
if(c == '\r')
c = 'r';
else if(c == '\n')
c = 'n';
else if(c < 0x20 || c == 0x7F)
c = '?';
}
}
inline bool IsNULL(const PGESTRING &str)
{
return (str.empty());
}
inline bool IsEmpty(const PGESTRING &str)
{
return str.empty();
}
inline bool IsEmpty(const PGESTRINGList &str)
{
return str.empty();
}
inline int toInt(const PGESTRING &str)
{
return std::atoi(str.c_str());
}
inline unsigned int toUInt(const PGESTRING &str)
{
return static_cast<unsigned int>(std::stoul(str, nullptr, 10));
}
inline long toLong(const PGESTRING &str)
{
return std::atol(str.c_str());
}
inline unsigned long toULong(const PGESTRING &str)
{
return static_cast<unsigned long>(std::atoll(str.c_str()));
}
inline float toFloat(const PGESTRING &str)
{
return static_cast<float>(std::atof(str.c_str()));
}
inline double toDouble(const PGESTRING &str)
{
return std::atof(str.c_str());
}
inline PGESTRING removeSpaces(const PGESTRING &src)
{
return PGE_RemSubSTRING(src, " ");
}
template<typename T>
PGESTRING fromNum(T num)
{
std::ostringstream n;
n << num;
return n.str();
}
inline PGESTRING fromBoolToNum(bool num)
{
std::ostringstream n;
n << static_cast<int>(num);
return n.str();
}
#define PGE_URLENC(src) PGE_FileFormats_misc::url_encode(src)
#define PGE_URLDEC(src) PGE_FileFormats_misc::url_decode(src)
#define PGE_BASE64ENC(src) PGE_FileFormats_misc::base64_encode(src)
#define PGE_BASE64ENC_nopad(src) PGE_FileFormats_misc::base64_encode(src, true)
#define PGE_BASE64DEC(src) PGE_FileFormats_misc::base64_decode(src)
#define PGE_BASE64ENC_W(src) PGE_FileFormats_misc::base64_encodeW(src)
#define PGE_BASE64DEC_W(src) PGE_FileFormats_misc::base64_decodeW(src)
#define PGE_BASE64ENC_A(src) PGE_FileFormats_misc::base64_encodeA(src)
#define PGE_BASE64DEC_A(src) PGE_FileFormats_misc::base64_decodeA(src)
#endif
inline bool PGE_floatEqual(double l, double r, double precission)
{
return static_cast<long long>(l * std::pow(10.0, precission)) ==
static_cast<long long>(r * std::pow(10.0, precission));
}
inline bool PGE_floatEqual(float l, float r, float precission)
{
return static_cast<long long>(l * std::pow(10.0f, precission)) ==
static_cast<long long>(r * std::pow(10.0f, precission));
}
inline bool PGE_StartsWith(const PGESTRING &src, const PGESTRING &with)
{
#ifdef PGE_FILES_QT
return src.startsWith(with, Qt::CaseSensitive);
#else
return !src.compare(0, with.size(), with);
#endif
}
#endif // PGE_FILE_LIB_PRIVATE_H_