-
Notifications
You must be signed in to change notification settings - Fork 42
/
Yaml.hpp
656 lines (543 loc) · 14.9 KB
/
Yaml.hpp
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
* MIT License
*
* Copyright(c) 2018 Jimmie Bergmann
*
* 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.
*
*/
/*
YAML documentation:
http://yaml.org/spec/1.0/index.html
https://www.codeproject.com/Articles/28720/YAML-Parser-in-C
*/
#pragma once
#include <exception>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <map>
/**
* @breif Namespace wrapping mini-yaml classes.
*
*/
namespace Yaml
{
/**
* @breif Forward declarations.
*
*/
class Node;
/**
* @breif Helper classes and functions
*
*/
namespace impl
{
/**
* @breif Helper functionality, converting string to any data type.
* Strings are left untouched.
*
*/
template<typename T>
struct StringConverter
{
static T Get(const std::string & data)
{
T type;
std::stringstream ss(data);
ss >> type;
return type;
}
static T Get(const std::string & data, const T & defaultValue)
{
T type;
std::stringstream ss(data);
ss >> type;
if(ss.fail())
{
return defaultValue;
}
return type;
}
};
template<>
struct StringConverter<std::string>
{
static std::string Get(const std::string & data)
{
return data;
}
static std::string Get(const std::string & data, const std::string & defaultValue)
{
if(data.size() == 0)
{
return defaultValue;
}
return data;
}
};
template<>
struct StringConverter<bool>
{
static bool Get(const std::string & data)
{
std::string tmpData = data;
std::transform(tmpData.begin(), tmpData.end(), tmpData.begin(), ::tolower);
if(tmpData == "true" || tmpData == "yes" || tmpData == "1")
{
return true;
}
return false;
}
static bool Get(const std::string & data, const bool & defaultValue)
{
if(data.size() == 0)
{
return defaultValue;
}
return Get(data);
}
};
}
/**
* @breif Exception class.
*
*/
class Exception : public std::runtime_error
{
public:
/**
* @breif Enumeration of exception types.
*
*/
enum eType
{
InternalError, ///< Internal error.
ParsingError, ///< Invalid parsing data.
OperationError ///< User operation error.
};
/**
* @breif Constructor.
*
* @param message Exception message.
* @param type Type of exception.
*
*/
Exception(const std::string & message, const eType type);
/**
* @breif Get type of exception.
*
*/
eType Type() const;
/**
* @breif Get message of exception.
*
*/
const char * Message() const;
private:
eType m_Type; ///< Type of exception.
};
/**
* @breif Internal exception class.
*
* @see Exception
*
*/
class InternalException : public Exception
{
public:
/**
* @breif Constructor.
*
* @param message Exception message.
*
*/
InternalException(const std::string & message);
};
/**
* @breif Parsing exception class.
*
* @see Exception
*
*/
class ParsingException : public Exception
{
public:
/**
* @breif Constructor.
*
* @param message Exception message.
*
*/
ParsingException(const std::string & message);
};
/**
* @breif Operation exception class.
*
* @see Exception
*
*/
class OperationException : public Exception
{
public:
/**
* @breif Constructor.
*
* @param message Exception message.
*
*/
OperationException(const std::string & message);
};
/**
* @breif Iterator class.
*
*/
class Iterator
{
public:
friend class Node;
/**
* @breif Default constructor.
*
*/
Iterator();
/**
* @breif Copy constructor.
*
*/
Iterator(const Iterator & it);
/**
* @breif Assignment operator.
*
*/
Iterator & operator = (const Iterator & it);
/**
* @breif Destructor.
*
*/
~Iterator();
/**
* @breif Get node of iterator.
* First pair item is the key of map value, empty if type is sequence.
*
*/
std::pair<const std::string &, Node &> operator *();
/**
* @breif Post-increment operator.
*
*/
Iterator & operator ++ (int);
/**
* @breif Post-decrement operator.
*
*/
Iterator & operator -- (int);
/**
* @breif Check if iterator is equal to other iterator.
*
*/
bool operator == (const Iterator & it);
/**
* @breif Check if iterator is not equal to other iterator.
*
*/
bool operator != (const Iterator & it);
private:
enum eType
{
None,
SequenceType,
MapType
};
eType m_Type; ///< Type of iterator.
void * m_pImp; ///< Implementation of iterator class.
};
/**
* @breif Constant iterator class.
*
*/
class ConstIterator
{
public:
friend class Node;
/**
* @breif Default constructor.
*
*/
ConstIterator();
/**
* @breif Copy constructor.
*
*/
ConstIterator(const ConstIterator & it);
/**
* @breif Assignment operator.
*
*/
ConstIterator & operator = (const ConstIterator & it);
/**
* @breif Destructor.
*
*/
~ConstIterator();
/**
* @breif Get node of iterator.
* First pair item is the key of map value, empty if type is sequence.
*
*/
std::pair<const std::string &, const Node &> operator *();
/**
* @breif Post-increment operator.
*
*/
ConstIterator & operator ++ (int);
/**
* @breif Post-decrement operator.
*
*/
ConstIterator & operator -- (int);
/**
* @breif Check if iterator is equal to other iterator.
*
*/
bool operator == (const ConstIterator & it);
/**
* @breif Check if iterator is not equal to other iterator.
*
*/
bool operator != (const ConstIterator & it);
private:
enum eType
{
None,
SequenceType,
MapType
};
eType m_Type; ///< Type of iterator.
void * m_pImp; ///< Implementation of constant iterator class.
};
/**
* @breif Node class.
*
*/
class Node
{
public:
friend class Iterator;
/**
* @breif Enumeration of node types.
*
*/
enum eType
{
None,
SequenceType,
MapType,
ScalarType
};
/**
* @breif Default constructor.
*
*/
Node();
/**
* @breif Copy constructor.
*
*/
Node(const Node & node);
/**
* @breif Assignment constructors.
* Converts node to scalar type if needed.
*
*/
Node(const std::string & value);
Node(const char * value);
/**
* @breif Destructor.
*
*/
~Node();
/**
* @breif Functions for checking type of node.
*
*/
eType Type() const;
bool IsNone() const;
bool IsSequence() const;
bool IsMap() const;
bool IsScalar() const;
/**
* @breif Completely clear node.
*
*/
void Clear();
/**
* @breif Get node as given template type.
*
*/
template<typename T>
T As() const
{
return impl::StringConverter<T>::Get(AsString());
}
/**
* @breif Get node as given template type.
*
*/
template<typename T>
T As(const T & defaultValue) const
{
return impl::StringConverter<T>::Get(AsString(), defaultValue);
}
/**
* @breif Get size of node.
* Nodes of type None or Scalar will return 0.
*
*/
size_t Size() const;
// Sequence operators
/**
* @breif Insert sequence item at given index.
* Converts node to sequence type if needed.
* Adding new item to end of sequence if index is larger than sequence size.
*
*/
Node & Insert(const size_t index);
/**
* @breif Add new sequence index to back.
* Converts node to sequence type if needed.
*
*/
Node & PushFront();
/**
* @breif Add new sequence index to front.
* Converts node to sequence type if needed.
*
*/
Node & PushBack();
/**
* @breif Get sequence/map item.
* Converts node to sequence/map type if needed.
*
* @param index Sequence index. Returns None type Node if index is unknown.
* @param key Map key. Creates a new node if key is unknown.
*
*/
Node & operator [] (const size_t index);
Node & operator [] (const std::string & key);
/**
* @breif Erase item.
* No action if node is not a sequence or map.
*
*/
void Erase(const size_t index);
void Erase(const std::string & key);
/**
* @breif Assignment operators.
*
*/
Node & operator = (const Node & node);
Node & operator = (const std::string & value);
Node & operator = (const char * value);
/**
* @breif Get start iterator.
*
*/
Iterator Begin();
ConstIterator Begin() const;
/**
* @breif Get end iterator.
*
*/
Iterator End();
ConstIterator End() const;
private:
/**
* @breif Get as string. If type is scalar, else empty.
*
*/
const std::string & AsString() const;
void * m_pImp; ///< Implementation of node class.
};
/**
* @breif Parsing functions.
* Population given root node with deserialized data.
*
* @param root Root node to populate.
* @param filename Path of input file.
* @param stream Input stream.
* @param string String of input data.
* @param buffer Char array of input data.
* @param size Buffer size.
*
* @throw InternalException An internal error occurred.
* @throw ParsingException Invalid input YAML data.
* @throw OperationException If filename or buffer pointer is invalid.
*
*/
void Parse(Node & root, const char * filename);
void Parse(Node & root, std::iostream & stream);
void Parse(Node & root, const std::string & string);
void Parse(Node & root, const char * buffer, const size_t size);
/**
* @breif Serialization configuration structure,
* describing output behavior.
*
*/
struct SerializeConfig
{
/**
* @breif Constructor.
*
* @param spaceIndentation Number of spaces per indentation.
* @param scalarMaxLength Maximum length of scalars. Serialized as folder scalars if exceeded.
* Ignored if equal to 0.
* @param sequenceMapNewline Put maps on a new line if parent node is a sequence.
* @param mapScalarNewline Put scalars on a new line if parent node is a map.
*
*/
SerializeConfig(const size_t spaceIndentation = 2,
const size_t scalarMaxLength = 64,
const bool sequenceMapNewline = false,
const bool mapScalarNewline = false);
size_t SpaceIndentation; ///< Number of spaces per indentation.
size_t ScalarMaxLength; ///< Maximum length of scalars. Serialized as folder scalars if exceeded.
bool SequenceMapNewline; ///< Put maps on a new line if parent node is a sequence.
bool MapScalarNewline; ///< Put scalars on a new line if parent node is a map.
};
/**
* @breif Serialization functions.
*
* @param root Root node to serialize.
* @param filename Path of output file.
* @param stream Output stream.
* @param string String of output data.
* @param config Serialization configurations.
*
* @throw InternalException An internal error occurred.
* @throw OperationException If filename or buffer pointer is invalid.
* If config is invalid.
*
*/
void Serialize(const Node & root, const char * filename, const SerializeConfig & config = {2, 64, false, false});
void Serialize(const Node & root, std::iostream & stream, const SerializeConfig & config = {2, 64, false, false});
void Serialize(const Node & root, std::string & string, const SerializeConfig & config = {2, 64, false, false});
}