forked from WohlSoft/PGE-File-Library-STL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pge_x.h
385 lines (360 loc) · 12.2 KB
/
pge_x.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
/*
* 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.
*/
/*! \file pge_x.h
*
* \brief Contains PGE-X Format worker class allows to parse, generate and validate PGE-X based files
*
*/
#pragma once
#ifndef PGE_X_H
#define PGE_X_H
#include "pge_file_lib_globs.h"
#include "pge_file_lib_private.h"
/*!
* \brief Container of raw PGE-X data section
*/
typedef PGEPAIR<PGESTRING, PGESTRINGList> PGEXSct;
/*!
* \brief Provides parsing, generation and validating tools for PGE-X baded file formats such as LVLX, WLDX, SAVX and many other
*/
class PGEFile PGE_FILES_INHERED
{
#ifdef PGE_FILES_QT
Q_OBJECT
#endif
public:
/*!
* \brief Defines type of section or sub-section type of items
*/
enum PGEX_Item_type
{
//! Structured data
PGEX_Struct = 0,
//! Any random plain text data
PGEX_PlainText
};
/*!
* \brief PGE-X Encoded Item field
*/
struct PGEX_Val
{
//! Name of the entry field
PGESTRING marker;
//! Encoded value of the entry field
PGESTRING value;
};
/*!
* \brief PGE-X Data Item
*/
struct PGEX_Item
{
//! Type of entry
PGEX_Item_type type;
//! List of available values
PGELIST<PGEX_Val > values;
};
/*!
* \brief PGE-X Data tree branch
*/
struct PGEX_Entry
{
//! Name of branch
PGESTRING name;
//! Type of contained items
PGEX_Item_type type;
//! List of contained items except subtrees
PGELIST<PGEX_Item > data;
//! List of contained sub-branches
PGELIST<PGEX_Entry > subTree;
};
#ifdef PGE_FILES_QT
/*!
* \brief QObject-based constructor Constructor
* \param parent Parent QObject
*/
PGEFile(QObject *parent = NULL);
/*!
* \brief QObject-based constructor copy Constructor
* \param pgeFile Another PGEFile object
* \param parent Parent QObject
*/
PGEFile(const PGEFile &pgeFile, QObject *parent = NULL);
#else
/*!
* \brief Constructor
*/
PGEFile();
/*!
* \brief Copy Constructor
* \param pgeFile Another PGEFile object
*/
PGEFile(const PGEFile &pgeFile);
#endif
/*!
* \brief Constructor with pre-storing of raw data
* \param _rawData
*/
PGEFile(const PGESTRING &_rawData);
/*!
* \brief Stores raw data string
* \param _rawData String contains raw data of entire file
*/
void setRawData(const PGESTRING &_rawData);
/*!
* \brief Parses stored raw data into the data tree
* \return
*/
bool buildTreeFromRaw();
/*!
* \brief Returns last occouped error
* \return Last occouped error
*/
PGESTRING lastError();
//! Full data tree of all parsed data
PGELIST<PGEX_Entry > dataTree;
private:
//! Last occouped error
PGESTRING m_lastError;
//! Stored raw data set
PGESTRING m_rawData;
//! Unparsed data separated to their data sections
PGELIST<PGEXSct > m_rawDataTree;
//Static functions
public:
/*!
* \brief Builds a branch of PGE-X data tree
* \param List of raw data lines
* \param _valid given value will accept 'true' if everything is fine or false if error was occouped
* \return Parsed PGE-X tree branch
*/
static PGEX_Entry buildTree(PGESTRINGList &src_data, bool *_valid = 0);
// /////////////Validators///////////////
/*!
* \brief Validates title of data section
* \param in Input string which would be a title of the data section
* \return True if data section title is valid
*/
static bool IsSectionTitle(const PGESTRING &in);
/*!
* \brief Is given value is a quoted string?
* \param in Input data string with data required to valitade
* \return true if given value is passed or false if value is invalid
*/
static bool IsQoutedString(const PGESTRING &in);// QUOTED STRING
/*!
* \brief Is given value is a heximal number?
* \param in Input data string with data required to valitade
* \return true if given value is passed or false if value is invalid
*/
static bool IsHex(const PGESTRING &in);// Hex Encoded String
/*!
* \brief Is given value is an unsigned integer number?
* \param in Input data string with data required to valitade
* \return true if given value is passed or false if value is invalid
*/
static bool IsIntU(const PGESTRING &in);// UNSIGNED INT
/*!
* \brief Is given value is a signed integer number?
* \param in Input data string with data required to valitade
* \return true if given value is passed or false if value is invalid
*/
static bool IsIntS(const PGESTRING &in);// SIGNED INT
/*!
* \brief Is given value is a floating point number?
* \param in Input data string with data required to valitade
* \return true if given value is passed or false if value is invalid
*/
static bool IsFloat(PGESTRING &in);// FLOAT
/*!
* \brief Is given value is a boolean degit?
* \param in Input data string with data required to valitade
* \return true if given value is passed or false if value is invalid
*/
static bool IsBool(const PGESTRING &in);//BOOL
/*!
* \brief Is given value is a boolean array (string contains 0 or 1 degits only)?
* \param in Input data string with data required to valitade
* \return true if given value is passed or false if value is invalid
*/
static bool IsBoolArray(const PGESTRING &in);//Boolean array
/*!
* \brief Is given value is an integer array?
* \param in Input data string with data required to valitade
* \return true if given value is passed or false if value is invalid
*/
static bool IsIntArray(const PGESTRING &in);//Integer array
/*!
* \brief Is given value is a string array?
* \param in Input data string with data required to valitade
* \return true if given value is passed or false if value is invalid
*/
static bool IsStringArray(const PGESTRING &in);//String array
//Split string into data values
static PGELIST<PGESTRINGList> splitDataLine(const PGESTRING &src_data, bool *valid = nullptr);
//PGE Extended File parameter string generators
/*!
* \brief Encode integer numeric value into PGE-X string
* \param input signed or unsigned integer
* \return Encoded PGE-X raw string
*/
template<typename T>
static inline PGESTRING WriteInt(const T &input)
{
return fromNum(input);
}
/*!
* \brief Encode floating point numeric value with rounding into PGE-X string
* \param input floating point number
* \return Encoded PGE-X raw string
*/
template<typename T>
static inline PGESTRING WriteRoundFloat(const T &input)
{
return fromNum(std::round(input));
}
/*!
* \brief Encode boolean value into PGE-X string
* \param input boolean flag
* \return Encoded PGE-X raw string
*/
static inline PGESTRING WriteBool(const bool &input)
{
return PGESTRING((input) ? "1" : "0");
}
/*!
* \brief Encode floating point numeric value into PGE-X string
* \param input floating point number
* \return Encoded PGE-X raw string
*/
template<typename T>
static PGESTRING WriteFloat(const T &input)
{
return fromNum(input);
}
/*!
* \brief Encode string into PGE-X escaped string
* \param input Plain text string
* \return Encoded PGE-X raw string
*/
static inline PGESTRING WriteStr(const PGESTRING &input)
{
PGESTRING output;
escapeString(output, input, true);
return output;
}
static inline PGESTRING WriteStr(const char *input)
{
PGESTRING in(input);
PGESTRING output;
escapeString(output, in, true);
return output;
}
/*!
* \brief [WIP] This function must encode string into heximal line
* \param input Plain text string
* \return Encoded PGE-X raw string
*/
static PGESTRING hStrS(const PGESTRING &input);
/*!
* \brief Encode string-array into PGE-X escaped string
* \param input List of plain text strings
* \return Encoded PGE-X raw string
*/
static PGESTRING WriteStrArr(const PGESTRINGList &input);
/*!
* \brief Encode array of integers into PGE-X escaped string
* \param input List of integer numbers
* \return Encoded PGE-X raw string
*/
static PGESTRING WriteIntArr(const PGELIST<int > &input);
/*!
* \brief Encode array of integers into PGE-X escaped string
* \param input List of integer numbers
* \return Encoded PGE-X raw string
*/
static PGESTRING WriteIntArr(const PGELIST<long > &input);
/*!
* \brief Encode array of booleans into PGE-X escaped string
* \param input List of boolean flags
* \return Encoded PGE-X raw string
*/
static PGESTRING WriteBoolArr(const PGELIST<bool > &input);
/*!
* \brief Decodes PGE-X string into plain text string
* \param input Encoded PGE-X string value
* \return Plain text string
*/
static PGESTRING X2STRING(PGESTRING input);
/*!
* \brief Decodes PGE-X String array into array of plain text strings
* \param src Encoded PGE-X string value
* \return List of plain text strings
*/
static PGESTRINGList X2STRArr(const PGESTRING &in, bool *_valid = nullptr);
/*!
* \brief Decodes PGE-X String array into array of plain text strings
* \param src Encoded PGE-X string value
* \return List of plain text strings
*/
static PGELIST<long> X2IntArr(const PGESTRING &in, bool *_valid = nullptr);
/*!
* \brief Decodes PGE-X Boolean array into array of boolean flags
* \param src Encoded PGE-X boolean array
* \return List of boolean flags
*/
static PGELIST<bool> X2BollArr(const PGESTRING &src);
/*!
* \brief Applies PGE-X escape sequensions to the plain text string
* \param [__out] output Target string where result will be recorded
* \param [__in] input plain text string
* \param [__in] addQuotes adds quotes to begin and end of the output string
*/
static void escapeString(PGESTRING &output, const PGESTRING &input, bool addQuotes = false);
/*!
* \brief Decodes PGE-X escape-sequensions
* \param [__inout] input Plain text string with applied escape sequensions
*/
static void restoreString(PGESTRING &input, bool removeQuotes = false);
/*!
* \brief Builds PGE-X raw value with specific marker and raw data string
* \param marker Name of field
* \param data PGE-X raw string
* \return PGE-X Entry field
*/
static inline PGESTRING value(PGESTRING &&marker, PGESTRING &&data)
{
PGESTRING out;
out += marker + ":" + data + ";";
return out;
}
/*!
* \brief Removed double quites from begin and end of string if there are exists
* \param str Plain text string
* \return Plain text string with removed double quotes at begin and at end
*/
static PGESTRING removeQuotes(PGESTRING str);
};
#endif // PGE_X_H