forked from WohlSoft/PGE-File-Library-STL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmbx64.h
443 lines (397 loc) · 13.4 KB
/
smbx64.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
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
/*
* PGE File Library - a library to process file formats, part of Moondust project
*
* Copyright (c) 2014-2022 Vitaly Novichkov <admin@wohlnet.ru>
*
* 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.
*/
/*! \file smbx64.h
* \brief Contains static class with SMBX64 standard file format fields validation and generation functions
*/
#pragma once
#ifndef SMBX64_H
#define SMBX64_H
#include "pge_file_lib_globs.h"
#include "pge_file_lib_private.h"
/*!
* \brief SMBX64 Standard validation and raw data conversion functions
*/
namespace SMBX64
{
/*******************Readers With Exception throwers********************/
inline void ReadUInt(unsigned int*out, PGESTRING &input)
{
#ifdef PGE_FILES_QT
bool ok=true;
*out = input.toUInt(&ok);
if(!ok) throw std::invalid_argument("Could not convert to unsigned int");
#else
*out = static_cast<unsigned int>(std::stoul(input));
#endif
}
inline void ReadUInt(unsigned long*out, PGESTRING &input)
{
#ifdef PGE_FILES_QT
bool ok=true;
*out = input.toULong(&ok);
if(!ok) throw std::invalid_argument("Could not convert to unsigned long");
#else
*out = std::stoul(input);
#endif
}
inline void ReadUInt(unsigned long long*out, PGESTRING &input)
{
#ifdef PGE_FILES_QT
bool ok=true;
*out = input.toULongLong(&ok);
if(!ok) throw std::invalid_argument("Could not convert to unsigned long long");
#else
*out = std::stoull(input);
#endif
}
inline void ReadUInt(int*out, PGESTRING &input)
{
#ifdef PGE_FILES_QT
bool ok=true;
*out = static_cast<int>(input.toUInt(&ok));
if(!ok) throw std::invalid_argument("Could not convert to unsigned int");
#else
*out = static_cast<int>(static_cast<unsigned int>(std::stoul(input)));
#endif
}
inline void ReadUInt(long*out, PGESTRING &input)
{
#ifdef PGE_FILES_QT
bool ok=true;
*out = static_cast<long>(input.toULong(&ok));
if(!ok) throw std::invalid_argument("Could not convert to unsigned long");
#else
*out = static_cast<long>(std::stoul(input));
#endif
}
inline void ReadUInt(long long*out, PGESTRING &input)
{
#ifdef PGE_FILES_QT
bool ok=true;
*out = static_cast<long long>(input.toULongLong(&ok));
if(!ok) throw std::invalid_argument("Could not convert to unsigned long long");
#else
*out = static_cast<long long>(std::stoull(input));
#endif
}
inline void ReadSInt(int*out, PGESTRING &input)
{
#ifdef PGE_FILES_QT
bool ok=true;
*out = input.toInt(&ok);
if(!ok) throw std::invalid_argument("Could not convert to int");
#else
*out = std::stoi(input);
#endif
}
inline void ReadSInt(long*out, PGESTRING &input)
{
#ifdef PGE_FILES_QT
bool ok=true;
*out = input.toLong(&ok);
if(!ok) throw std::invalid_argument("Could not convert to long");
#else
*out = std::stol(input);
#endif
}
inline void ReadSInt(long long*out, PGESTRING &input)
{
#ifdef PGE_FILES_QT
bool ok=true;
*out = input.toLongLong(&ok);
if(!ok) throw std::invalid_argument("Could not convert to long long");
#else
*out = std::stoll(input);
#endif
}
inline void ReadFloat(float*out, PGESTRING &input)
{
PGE_ReplSTRING(input, ",", ".");//Allow to parse floats of both comma and dot standard
#ifdef PGE_FILES_QT
bool ok=true;
*out = input.toFloat(&ok);
if(!ok) throw std::invalid_argument("Could not convert to Float");
#else
*out = std::stof(input);
#endif
}
inline void ReadFloat(double*out, PGESTRING &input)
{
PGE_ReplSTRING(input, ",", ".");//Allow to parse floats of both comma and dot standard
#ifdef PGE_FILES_QT
bool ok=true;
*out = input.toDouble(&ok);
if(!ok) throw std::invalid_argument("Could not convert to Float");
#else
*out = std::stod(input);
#endif
}
inline void ReadBool(bool*out, PGESTRING &input)
{
if(input == "0" || input == "") // FIXME: Is it correct? Or too hackish?
*out = false;
else if(input != "0") // FIXME: Is it correct? Or too hackish?
*out = true;
else
throw std::invalid_argument(std::string("Could not convert to bool (must be empty, \"0\", \"!0\" or \"1\")"));
}
inline void ReadCSVBool(bool*out, PGESTRING &input)
{
if(input == "#FALSE#")
*out = false;
else if(input == "#TRUE#")
*out = true;
else if(input == "false")
*out = false;
else if(input == "true")
*out = true;
else if( input == "0" || input == "" )
*out = false;
else if( input == "!0" || input == "1" )
*out = true;
else
throw std::invalid_argument(std::string("Could not convert CSV Bool (must be #TRUE# or #FALSE#)"));
}
inline void ReadCSVBool(int*out, PGESTRING &input)
{
if(input == "#FALSE#")
*out = 0;
else if(input == "#TRUE#")
*out = 1;
else if(input == "false")
*out = 0;
else if(input == "true")
*out = 1;
else if( input == "0" || input == "" )
*out = 0;
else if( input == "!0" || input == "1" )
*out = 1;
else
throw std::invalid_argument(std::string("Could not convert CSV Bool (must be #TRUE# or #FALSE#)"));
}
inline void ReadCSVBool(long*out, PGESTRING &input)
{
if(input == "#FALSE#")
*out = 0;
else if(input == "#TRUE#")
*out = 1;
else if(input == "false")
*out = 0;
else if(input == "true")
*out = 1;
else if( input == "0" || input == "" )
*out = 0;
else if( input == "!0" || input == "1" )
*out = 1;
else
throw std::invalid_argument(std::string("Could not convert CSV Bool (must be #TRUE# or #FALSE#)"));
}
inline void ReadSIntFromFloat(int*out, PGESTRING &input)
{
#ifdef PGE_FILES_QT
bool ok=true;
*out = qRound(input.toDouble(&ok));
if(!ok) throw std::invalid_argument("Could not convert to Double");
#else
*out = static_cast<int>(std::round(std::stod(input)));
#endif
}
inline void ReadSIntFromFloat(long*out, PGESTRING &input)
{
#ifdef PGE_FILES_QT
bool ok=true;
*out = static_cast<long>(std::round(input.toDouble(&ok)));
if(!ok) throw std::invalid_argument("Could not convert to Double");
#else
*out = static_cast<long>(std::round(std::stod(input)));
#endif
}
inline void ReadStr(PGESTRING*out, PGESTRING &input)
{
if(IsEmpty(input))
{
out->clear();
return;
}
*out = input;
PGESTRING &target = *out;
if(target[0] == PGEChar('\"'))
PGE_RemStrRng(target, 0, 1);
if( (!IsEmpty(target)) && (target[target.size()-1] == PGEChar('\"')) )
PGE_RemStrRng(target, int(target.size() - 1), 1);
target = PGE_ReplSTRING(target, "\"", "\'");//Correct damaged by SMBX line
}
/*********************Validations**********************/
/*!
* \brief Validate Unsigned Integer value
* \param in raw value
* \return true if value is valid
*/
bool IsUInt(const PGESTRING &in);
/*!
* \brief Validate Signed Integer value
* \param in raw value
* \return true if value is valid
*/
bool IsSInt(const PGESTRING &in);
/*!
* \brief Validate Floating Point value
* \param in raw value
* \return true if value is valid
*/
bool IsFloat(PGESTRING &in);
/*!
* \brief Validate quoted string value
* \param in raw value
* \return true if value is valid
*/
bool IsQuotedString(const PGESTRING &in);
/*!
* \brief Validate CSV-boolean value (#TRUE# or #FALSE#)
* \param in raw value
* \return true if value is INVALID
*/
inline bool IsCSVBool(const PGESTRING &in) //Worded BOOL
{
return ((in=="#TRUE#") || (in=="#FALSE#"));
}
/*!
* \brief Validate digital boolean value (0 or 1)
* \param in raw value
* \return true if value is valid
*/
inline bool IsBool(const PGESTRING &in) //Digital BOOL
{
if((in.size() != 1) || (IsEmpty(in)) )
return false;
return ((PGEGetChar(in[0])=='1') || (PGEGetChar(in[0])=='0'));
}
/*!
* \brief Converts CVS-bool raw string into boolean value
* \param in raw value
* \return boolean equivalent
*/
inline bool wBoolR(const PGESTRING &in)
{
return (in == "#TRUE#");
}
/******************RAW to Internal**********************/
/*!
* \brief Convert from SMBX64 string to internal with damage correction
* \param in raw value
* \return fixed string vale
*/
inline PGESTRING StrToStr(const PGESTRING &in)
{
PGESTRING target = in;
if(IsEmpty(target))
return target;
if(target[0] == PGEChar('\"'))
PGE_RemStrRng(target, 0, 1);
if((!IsEmpty(target)) && (target[target.size()-1]==PGEChar('\"')))
PGE_RemStrRng(target, int(target.size() - 1), 1);
target = PGE_ReplSTRING(target, "\"", "\'");//Correct damaged by SMBX line
return target;
}
/******************Internal to RAW**********************/
/*!
* \brief Generate raw string from integer value
* \param input Source signed integer value
* \return ASCII encoded signed integer value
*/
template<typename T>
inline PGESTRING WriteSInt(T input)
{ return fromNum(static_cast<long long>(input))+"\n"; }
/*!
* \brief Generate raw string from unsigned integer value
* \param input Source unsigned integer value
* \return ASCII encoded unsigned integer value
*/
template<typename T>
inline PGESTRING WriteUInt(T input)
{ return fromNum(static_cast<unsigned long long>(input))+"\n"; }
/*!
* \brief Generate raw CVS-bool string from boolean value
* \param input Source boolean value
* \return ASCII encoded CVS-bool value
*/
inline PGESTRING WriteCSVBool(bool input)
{ return PGESTRING( (input)?"#TRUE#":"#FALSE#" )+"\n"; }
/*!
* \brief Convert string into valid CVS string line (line feeds are will be removed)
* \param input Source string
* \return Valid CVS string
*/
inline PGESTRING WriteStr(PGESTRING input)
{
input = PGE_RemSubSTRING(input, "\n");
input = PGE_RemSubSTRING(input, "\r");
input = PGE_RemSubSTRING(input, "\t");
input = PGE_RemSubSTRING(input, "\"");
return PGESTRING("\"")+input+PGESTRING("\"\n");
}
/*!
* \brief Convert string into valid CVS string line (with line feeds keeping)
* \param input Source string
* \return Valid CVS string
*/
inline PGESTRING WriteStr_multiline(PGESTRING input)
{
input = PGE_RemSubSTRING(input, "\t");
input = PGE_ReplSTRING(input, "\"", "'");
return PGESTRING("\"")+input+PGESTRING("\"\n");
}
/*!
* \brief Generate raw string from floating point value
* \param input Source floating point vale
* \return ASCII encoded floating point value
*/
inline PGESTRING WriteFloat(float input)
{ return fromNum(input)+"\n"; }
/*!
* \brief Generate raw string from double floating point value
* \param input Source floating point vale
* \return ASCII encoded floating point value
*/
inline PGESTRING WriteFloat(double input)
{ return fromNum(input)+"\n"; }
/******************Units converters**********************/
/*!
* \brief Convert 1/65 seconds into milliseconds
* \param t65 1/65 time value
* \return millisecond time equivalent
*/
inline double t65_to_ms(double t65)
{ return t65 * (1000.0/65.0); }
/*!
* \brief Convert milliseconds into 1/65 seconds
* \param ms time in milliseconds
* \return 1/65 second time equivalent
*/
inline double ms_to_65(double ms)
{ return ms * (65.0/1000.0); }
}
#endif // SMBX64_H