-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCSVReader.h
1087 lines (951 loc) · 40.4 KB
/
CSVReader.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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Kevin Waldock
*
* 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 CSVReader_H
#define CSVReader_H
#include <iostream>
#include <fstream>
#include <string>
#include <type_traits>
#include <sstream>
#include <array>
#include <exception>
#include <stdexcept>
#include <utility>
#include "invoke_default.hpp"
#ifdef _MSC_VER
#pragma warning (disable: 4244)
#pragma warning (disable: 4503)
#endif
namespace CSVReader
{
// ========= Exceptions START ===========
/*!
* \brief Exception for parsing errors.
*/
class parse_error : public std::logic_error
{
private:
int _line;
int _field;
public:
explicit parse_error(const char *msg, int line, int field) : std::logic_error(msg), _line(line), _field(field) {}
explicit parse_error(std::string msg, int line, int field) : parse_error(msg.c_str(), line, field) {}
inline int get_line_number() const
{
return _line;
}
inline int get_field_number() const
{
return _field;
}
};
// ========= Exceptions END ===========
// ========= Utils START ===========
// This is a feature built-in for C++14
namespace detail
{
template <std::size_t... I>
class index_sequence {};
template <std::size_t N, std::size_t ...I>
struct make_index_sequence : make_index_sequence < N - 1, N - 1, I... > {};
template <std::size_t ...I>
struct make_index_sequence<0, I...> : index_sequence<I...> {};
}
// This is for the CSVBatchReader
// This template class depends on .push_back()
template<class ContainerValueT, class ContainerT>
struct CommonContainerUtils
{
static void Add(ContainerT *container, const ContainerValueT &value)
{
container->push_back(value);
}
};
namespace detail
{
template<class StrT,
class CharT,
class StrTUtils,
class Converter>
class CSVReaderBase
{
protected:
size_t _currentCharIndex;
CharT _sep;
StrT _currentLine; // Will be written by the derived class
int _fieldTracker; // Will be written by the derived class
int _lineTracker; // Will be written by the derived class
CSVReaderBase(CharT sep) : _currentCharIndex(0u), _sep(sep),
_currentLine(""), _fieldTracker(0), _lineTracker(0) {}
inline StrT NextField()
{
size_t newCharIndex = _currentCharIndex;
if(!StrTUtils::find(_currentLine, _sep, newCharIndex))
newCharIndex = StrTUtils::length(_currentLine);
StrT next = StrTUtils::substring(_currentLine, _currentCharIndex, newCharIndex - _currentCharIndex);
_currentCharIndex = newCharIndex + 1;
return next;
}
inline void SkipField()
{
size_t newCharIndex = _currentCharIndex;
if(!StrTUtils::find(_currentLine, _sep, newCharIndex))
newCharIndex = StrTUtils::length(_currentLine);
_currentCharIndex = newCharIndex + 1;
}
inline bool HasNext()
{
return _currentCharIndex <= StrTUtils::length(_currentLine);
}
template<typename ToType>
inline void SafeConvert(ToType *to, const StrT &from)
{
try
{
Converter::Convert(to, from);
}
catch(...)
{
ThrowParseErrorInCatchContext();
}
}
inline void ThrowParseErrorInCatchContext()
{
std::throw_with_nested(parse_error(std::string("Failed to parse field ") + std::to_string(_fieldTracker) + " at line " + std::to_string(_lineTracker), _lineTracker, _fieldTracker));
}
};
}
// ========= Utils END ===========
// ========= Readers START ===========
template<class StrElementType, class StrElementTraits, class StrElementAlloc = std::allocator<StrElementType> >
struct IfStreamReader
{
using target_ifstream = std::basic_ifstream<StrElementType, StrElementTraits>;
using target_string = std::basic_string<StrElementType, StrElementTraits, StrElementAlloc>;
typedef target_string string_type;
target_ifstream *_reader;
IfStreamReader(target_ifstream *reader) : _reader(reader) {}
target_string read_line()
{
target_string str;
std::getline(*_reader, str);
return str;
}
};
template<class StrElementType, class StrElementTraits>
constexpr IfStreamReader<StrElementType, StrElementTraits> MakeIfStreamReader(std::basic_ifstream<StrElementType, StrElementTraits> *reader)
{
return IfStreamReader<StrElementType, StrElementTraits>(reader);
}
template<class StrElementType, class StrElementTraits, class StrElementAlloc>
struct StringReader
{
using target_istringstream = std::basic_istringstream<StrElementType, StrElementTraits, StrElementAlloc>;
using target_string = std::basic_string<StrElementType, StrElementTraits, StrElementAlloc>;
typedef target_string string_type;
target_istringstream _txt;
StringReader(target_string str) : _txt(str) {}
target_string read_line()
{
target_string str;
std::getline(_txt, str);
return str;
}
};
template<class StrElementType, class StrElementTraits, class StrElementAlloc>
constexpr StringReader<StrElementType, StrElementTraits, StrElementAlloc> MakeStringReader(const std::basic_string<StrElementType, StrElementTraits, StrElementAlloc> &str)
{
return StringReader<StrElementType, StrElementTraits, StrElementAlloc>(str);
}
template<class StrT>
struct DirectReader
{
typedef StrT string_type;
DirectReader(const StrT &data) : _data(data) {}
void read_line(StrT &ret)
{
ret = _data;
}
private:
StrT _data;
};
template<class StrT>
constexpr DirectReader<StrT> MakeDirectReader(const StrT &data)
{
return DirectReader<StrT>(data);
}
// ========= Readers END ===========
// ========= Special Attributes START ===========
// 1. Alternative Parameter - CSVDiscard
/*!
* \brief Special CSVReader parameter value for ignoring a field.
*/
struct CSVDiscard {};
// 2. Alternative Parameter - CSVValidator
/*!
* \brief Special CSVReader parameter value for validating a field.
* \see MakeCSVValidator()
*/
template<typename T, typename ValidatorFunc>
struct CSVValidator
{
typedef T value_type;
CSVValidator(T *value, const ValidatorFunc &validatorFunction) : _value(value), _validatorFunction(validatorFunction) {}
bool Validate() const
{
return idef::invoke_or_noop<bool>(idef::default_if_nullptr(_validatorFunction, [](T *)
{
return true;
}), _value);
}
T *Get()
{
return _value;
}
private:
T *_value;
ValidatorFunc _validatorFunction;
};
/*!
* \brief Creates a new CSVValidator.
* \see CSVValidator
*/
template<typename T, typename ValidatorFunc>
constexpr CSVValidator<T, ValidatorFunc> MakeCSVValidator(T *value, ValidatorFunc validatorFunction)
{
return CSVValidator<T, ValidatorFunc>(value, validatorFunction);
}
// 3. Alternative Parameter - CSVPostProcessor
/*!
* \brief Special CSVReader parameter value for post processing a field.
* \see MakeCSVPostProcessor()
*
* The post processor will be called before the value will be writting into the pointer.
*/
template<typename T, typename PostProcessorFunc, typename ValidatorFunc>
struct CSVPostProcessor
{
typedef T value_type;
CSVPostProcessor(T *value, const PostProcessorFunc &postProcessorFunction) : CSVPostProcessor(value, postProcessorFunction, nullptr) {}
CSVPostProcessor(T *value, const PostProcessorFunc &postProcessorFunction, const ValidatorFunc &validatorFunction)
: _value(value), _validatorFunction(validatorFunction), _postProcessorFunction(postProcessorFunction) {}
bool Validate() const
{
return idef::invoke_or_noop<bool>(idef::default_if_nullptr(_validatorFunction, [](T *)
{
return true;
}), _value);
}
inline void PostProcess()
{
idef::invoke_or_noop<void>(_postProcessorFunction, *_value);
}
T *Get()
{
return _value;
}
private:
T *_value;
ValidatorFunc _validatorFunction;
PostProcessorFunc _postProcessorFunction;
};
/*!
* \brief Creates a new CSVPostProcessor.
* \see CSVPostProcessor
*/
template<typename T, typename PostProcessorFunc>
constexpr CSVPostProcessor<T, PostProcessorFunc, std::nullptr_t> MakeCSVPostProcessor(T *value, PostProcessorFunc postProcessorFunction)
{
return CSVPostProcessor<T, PostProcessorFunc, std::nullptr_t>(value, postProcessorFunction);
}
/*!
* \brief Creates a new CSVPostProcessor.
* \see CSVPostProcessor
*/
template<typename T, typename PostProcessorFunc, typename ValidatorFunc>
constexpr CSVPostProcessor<T, PostProcessorFunc, ValidatorFunc> MakeCSVPostProcessor(T *value, PostProcessorFunc postProcessorFunction, ValidatorFunc validatorFunction)
{
return CSVPostProcessor<T, PostProcessorFunc, ValidatorFunc>(value, postProcessorFunction, validatorFunction);
}
// 4. Alternative parameter - CSVOptional
/*!
* \brief Special CSVReader parameter value for marking a field as optional.
* \see MakeCSVOptional()
*
* If the reader cannot read further, then the default value is used
* instead of throwing an exception.
*/
template<typename T, typename ValidatorFunc, typename PostProcessorFunc>
struct CSVOptional
{
typedef T value_type;
CSVOptional(T* value, const T defVal, bool shouldAssignDefaultOnEmpty) : CSVOptional(value, defVal, shouldAssignDefaultOnEmpty, nullptr) {}
CSVOptional(T* value, const T defVal, bool shouldAssignDefaultOnEmpty, const ValidatorFunc& validatorFunction) :
CSVOptional(value, defVal, shouldAssignDefaultOnEmpty, validatorFunction, nullptr) {}
CSVOptional(T* value, const T defVal, bool shouldAssignDefaultOnEmpty, const ValidatorFunc& validatorFunction, const PostProcessorFunc& postProcessorFunction) :
_value(value), _defaultValue(defVal), _shouldAssignDefaultOnEmpty(shouldAssignDefaultOnEmpty),
_validatorFunction(validatorFunction), _postProcessorFunction(postProcessorFunction) {}
bool Validate() const
{
return idef::invoke_or_noop<bool>(idef::default_if_nullptr(_validatorFunction, [](T *)
{
return true;
}), _value);
}
inline void PostProcess()
{
idef::invoke_or_noop<void>(_postProcessorFunction, *_value);
}
inline void AssignDefault()
{
*_value = _defaultValue;
}
inline T *Get()
{
return _value;
}
inline bool ShouldAssingDefaultOnEmpty() const {
return _shouldAssignDefaultOnEmpty;
}
private:
T *_value;
T _defaultValue;
bool _shouldAssignDefaultOnEmpty;
ValidatorFunc _validatorFunction;
PostProcessorFunc _postProcessorFunction;
};
/*!
* \brief Creates a new CSVOptional.
* \see CSVOptional
*/
template<typename T, typename OT>
constexpr CSVOptional<T, std::nullptr_t, std::nullptr_t> MakeCSVOptional(T* value, OT defVal) {
return CSVOptional<T, std::nullptr_t, std::nullptr_t>(value, defVal, false, nullptr, nullptr);
}
/*!
* \brief Creates a new CSVOptional.
* \see CSVOptional
*/
template<typename T, typename OT, typename ValidatorFunc>
constexpr CSVOptional<T, ValidatorFunc, std::nullptr_t> MakeCSVOptional(T* value, OT defVal, ValidatorFunc validatorFunction) {
return CSVOptional<T, ValidatorFunc, std::nullptr_t>(value, defVal, false, validatorFunction, nullptr);
}
/*!
* \brief Creates a new CSVOptional.
* \see CSVOptional
*/
template<typename T, typename OT, typename ValidatorFunc, typename PostProcessorFunc>
constexpr CSVOptional<T, ValidatorFunc, PostProcessorFunc> MakeCSVOptional(T* value, OT defVal, ValidatorFunc validatorFunction, PostProcessorFunc postProcessorFunction) {
return CSVOptional<T, ValidatorFunc, PostProcessorFunc>(value, defVal, false, validatorFunction, postProcessorFunction);
}
/*!
* \brief Creates a new CSVOptional.
* \see CSVOptional
*/
template<typename T, typename OT>
constexpr CSVOptional<T, std::nullptr_t, std::nullptr_t> MakeCSVOptionalEmpty(T* value, OT defVal) {
return CSVOptional<T, std::nullptr_t, std::nullptr_t>(value, defVal, true, nullptr, nullptr);
}
/*!
* \brief Creates a new CSVOptional.
* \see CSVOptional
*/
template<typename T, typename OT, typename ValidatorFunc>
constexpr CSVOptional<T, ValidatorFunc, std::nullptr_t> MakeCSVOptionalEmpty(T* value, OT defVal, ValidatorFunc validatorFunction) {
return CSVOptional<T, ValidatorFunc, std::nullptr_t>(value, defVal, true, validatorFunction, nullptr);
}
/*!
* \brief Creates a new CSVOptional.
* \see CSVOptional
*/
template<typename T, typename OT, typename ValidatorFunc, typename PostProcessorFunc>
constexpr CSVOptional<T, ValidatorFunc, PostProcessorFunc> MakeCSVOptionalEmpty(T* value, OT defVal, ValidatorFunc validatorFunction, PostProcessorFunc postProcessorFunction) {
return CSVOptional<T, ValidatorFunc, PostProcessorFunc>(value, defVal, true, validatorFunction, postProcessorFunction);
}
// 5. Reader in Reader - CSVOptional
/*!
* \brief Special CSVReader parameter value for creating a sub-reader.
* \see MakeCSVSubReader()
* This class can read fields, which are seperated again with another token.
*/
template<class Reader, class StrT, class CharT, class StrTUtils, class Converter, class... RestValues>
struct CSVSubReader;
// 6. Reading in container
/*!
* \brief Special CSVReader parameter value for reading a collection of literal types.
* \see MakeCSVBatchReader
*/
template<class ContainerValueT,
class Container,
class ContainerUtils,
class StrT,
class CharT,
class StrTUtils,
class Converter,
class PostProcessorFunc>
struct CSVBatchReader : detail::CSVReaderBase<StrT, CharT, StrTUtils, Converter>
{
private:
Container* _container;
PostProcessorFunc _postProcessorFunction;
public:
CSVBatchReader(CharT sep, Container* container, const PostProcessorFunc &postProcessorFunction) :
detail::CSVReaderBase<StrT, CharT, StrTUtils, Converter>(sep), _container(container), _postProcessorFunction(postProcessorFunction) {}
inline void ReadDataLine(const StrT &val)
{
this->_currentLine = val;
while(this->HasNext())
{
StrT from = this->NextField();
if(from == "")
continue;
ContainerValueT to;
this->SafeConvert(&to, from);
idef::invoke_or_noop<void>(_postProcessorFunction, to);
ContainerUtils::Add(_container, to);
}
}
};
// 7. Reading with iteration
/*!
* \brief Special CSVReader parameter value for iterating through an unknown number of types.
* \see MakeCSVIterator()
*/
template<class StrT,
class CharT,
class StrTUtils,
class Converter,
class IteratorFunc>
struct CSVIterator : detail::CSVReaderBase<StrT, CharT, StrTUtils, Converter>
{
private:
bool _isOptional;
IteratorFunc _iteratorFunc;
public:
CSVIterator(CharT sep, bool isOptional, const IteratorFunc &iteratorFunc) :
detail::CSVReaderBase<StrT, CharT, StrTUtils, Converter>(sep), _isOptional(isOptional), _iteratorFunc(iteratorFunc) {}
inline void ReadDataLine(const StrT &val)
{
this->_currentLine = val;
while(this->HasNext())
{
StrT next = this->NextField();
if(!(next == ""))
_iteratorFunc(next);
}
}
bool IsOptional() const
{
return _isOptional;
}
};
// ========= Special Attributes END ===========
/*
* The Default CSV Converter uses the STL library to do the most of the conversion.
*/
/*!
* \brief The default converter to convert strings to literal types.
*/
template<class StrType>
struct DefaultCSVConverter
{
static void Convert(double *out, const StrType &field)
{
*out = std::stod(field);
}
static void Convert(float *out, const StrType &field)
{
*out = std::stof(field);
}
static void Convert(int *out, const StrType &field)
{
*out = std::stoi(field);
}
static void Convert(long *out, const StrType &field)
{
*out = std::stol(field);
}
static void Convert(long long *out, const StrType &field)
{
*out = std::stoll(field);
}
static void Convert(long double *out, const StrType &field)
{
*out = std::stold(field);
}
static void Convert(unsigned int *out, const StrType &field)
{
*out = static_cast<unsigned int>(std::stoul(field));
}
static void Convert(unsigned long *out, const StrType &field)
{
*out = std::stoul(field);
}
static void Convert(unsigned long long *out, const StrType &field)
{
*out = std::stoull(field);
}
static void Convert(bool *out, const StrType &field)
{
if(field == "0" || field == "") // FIXME: Is it correct? Or too hackish?
*out = false;
else if(field == "!0" || field == "1") // 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\"), got \"") + field + std::string("\""));
}
static void Convert(StrType *out, const StrType &field)
{
*out = field;
}
};
/*!
* \brief The default wrapper for strings (Works for STL strings)
* \see MakeCSVReaderFromBasicString()
*/
template<class StrElementType, class StrElementTraits, class StrElementAlloc>
struct DefaultStringWrapper
{
using target_string = std::basic_string<StrElementType, StrElementTraits, StrElementAlloc>;
static bool find(const target_string &str, StrElementType sep, size_t &findIndex)
{
size_t pos = str.find(sep, findIndex);
if(pos == target_string::npos)
return false;
findIndex = pos;
return true;
}
static size_t length(const target_string &str)
{
return str.length();
}
static target_string substring(const target_string &str, size_t pos, size_t count)
{
return str.substr(pos, count);
}
};
/*!
* \brief Provides features to read out CSV data.
* \see MakeCSVReader()
*
* This class provides features to read out CSV data and converting them directly to literal types.
*
* Use MakeCSVReader or MakeCSVReaderFromBasicString for easy construction of this class.
*
*/
template<class Reader, class StrT, class CharT, class StrTUtils, class Converter>
class CSVReader : detail::CSVReaderBase<StrT, CharT, StrTUtils, Converter>
{
private:
Reader *_reader;
int _currentTotalFields;
bool _requireReadLine;
public:
CSVReader(Reader *reader, CharT sep) : detail::CSVReaderBase<StrT, CharT, StrTUtils, Converter>(sep), _reader(reader),
_currentTotalFields(0), _requireReadLine(true) {}
CSVReader(const CSVReader &other) = delete;
CSVReader(CSVReader &&other) = default;
~CSVReader() = default;
private:
inline void ThrowIfOutOfBounds()
{
if(this->_currentCharIndex > StrTUtils::length(this->_currentLine))
throw parse_error("Expected " + std::to_string(this->_currentTotalFields) + " CSV-Fields, got "
+ std::to_string(this->_fieldTracker) + " at line "
+ std::to_string(this->_lineTracker) + "!", this->_lineTracker, this->_fieldTracker);
}
template<class T, class... RestValues>
void ReadNext(T nextVal, RestValues &&... restVals)
{
static_assert(std::is_pointer<T>::value, "All values which are unpacked must be pointers (except CSVDiscard, CSVVaildate, CSVDiscard, CSVOptional, CSVSubReader)!");
ThrowIfOutOfBounds();
//Here do conversion code
this->SafeConvert(nextVal, this->NextField());
this->_fieldTracker++;
ReadNext(std::forward<RestValues>(restVals)...);
}
template<class... RestValues>
void ReadNext(CSVDiscard, RestValues &&... restVals)
{
this->_fieldTracker++;
this->SkipField();
ReadNext(std::forward<RestValues>(restVals)...);
}
template<class ValidateT, class ValidatorFunc, class... RestValues>
void ReadNext(CSVValidator<ValidateT, ValidatorFunc> nextVal, RestValues &&... restVals)
{
ThrowIfOutOfBounds();
this->SafeConvert(nextVal.Get(), this->NextField());
if(!nextVal.Validate())
throw std::logic_error("Validation failed at field " + std::to_string(this->_fieldTracker) + " at line " + std::to_string(this->_lineTracker) + "!");
this->_fieldTracker++;
ReadNext(std::forward<RestValues>(restVals)...);
}
template<class PostProcessorT, class PostProcessorFunc, class ValidatorFunc, class... RestValues>
void ReadNext(CSVPostProcessor<PostProcessorT, PostProcessorFunc, ValidatorFunc> nextVal, RestValues &&... restVals)
{
ThrowIfOutOfBounds();
this->SafeConvert(nextVal.Get(), this->NextField());
if(!nextVal.Validate())
throw std::logic_error("Validation failed at field " + std::to_string(this->_fieldTracker) + " at line " + std::to_string(this->_lineTracker) + "!");
nextVal.PostProcess();
this->_fieldTracker++;
ReadNext(std::forward<RestValues>(restVals)...);
}
template<class OptionalT, class ValidatorFunc, class PostProcessorFunc, class... RestValues>
void ReadNext(CSVOptional<OptionalT, ValidatorFunc, PostProcessorFunc> optionalObj, RestValues &&... restVals)
{
// If we already reached the end, then assign default
if(this->_currentCharIndex >= StrTUtils::length(this->_currentLine))
optionalObj.AssignDefault();
else
{
StrT nextField = this->NextField();
if(!optionalObj.ShouldAssingDefaultOnEmpty() || StrTUtils::length(nextField) > 0) {
this->SafeConvert(optionalObj.Get(), nextField);
if (!optionalObj.Validate())
throw std::logic_error("Validation failed at field " + std::to_string(this->_fieldTracker) + " at line " + std::to_string(this->_lineTracker) + "!");
optionalObj.PostProcess();
} else {
optionalObj.AssignDefault();
}
}
this->_fieldTracker++;
ReadNext(std::forward<RestValues>(restVals)...);
}
template<class SubReader, class SubStrT, class SubCharT, class SubStrTUtils, class SubConverter, class... SubValues, class... RestValues>
void ReadNext(CSVSubReader<SubReader, SubStrT, SubCharT, SubStrTUtils, SubConverter, SubValues...> subReaderObj, RestValues &&... restVals)
{
if(!subReaderObj.IsOptional())
ThrowIfOutOfBounds();
// We don't have to check for subReaderObj.IsOptional again, because
// ThrowIfOutOfBounds() would have thrown already
if(!(this->_currentCharIndex >= StrTUtils::length(this->_currentLine)))
{
try
{
subReaderObj.ReadDataLine(this->NextField());
}
catch(...)
{
this->ThrowParseErrorInCatchContext();
}
}
this->_fieldTracker++;
ReadNext(std::forward<RestValues>(restVals)...);
}
template<class ReaderContainerValueT, class ReaderContainer, class ReaderContainerUtils,
class ReaderStrT, class ReaderCharT, class ReaderStrTUtils, class ReaderConverter, class PostProcessorFunc, class... RestValues>
void ReadNext(CSVBatchReader<ReaderContainerValueT, ReaderContainer, ReaderContainerUtils, ReaderStrT, ReaderCharT, ReaderStrTUtils, ReaderConverter, PostProcessorFunc> subBatchReaderObj, RestValues &&... restVals)
{
ThrowIfOutOfBounds();
try
{
subBatchReaderObj.ReadDataLine(this->NextField());
}
catch(...)
{
this->ThrowParseErrorInCatchContext();
}
this->_fieldTracker++;
ReadNext(std::forward<RestValues>(restVals)...);
}
template<class IterStrT, class IterCharT, class IterStrTUtils, class IterConverter, class IteratorFunc, class... RestValues>
void ReadNext(CSVIterator<IterStrT, IterCharT, IterStrTUtils, IterConverter, IteratorFunc> iteratorObj, RestValues &&... restVals)
{
if(!iteratorObj.IsOptional())
ThrowIfOutOfBounds();
// We don't have to check for iteratorObj.IsOptional again, because
// ThrowIfOutOfBounds() would have thrown already
if(!(this->_currentCharIndex >= StrTUtils::length(this->_currentLine)))
{
try
{
iteratorObj.ReadDataLine(this->NextField());
}
catch(...)
{
this->ThrowParseErrorInCatchContext();
}
}
this->_fieldTracker++;
ReadNext(std::forward<RestValues>(restVals)...);
}
void ReadNext() {}
public:
/*!
* \brief Read the next data line and pushes the result directly to the parameter.
*
* allValues must be pointer with the exception of:
* * CSVDiscard
* * CSVValidator
* * CSVPostProcessor
* * CSVOptional
* * CSVSubReader
* * CSVBatchReader
* * CSVIterator
*
* \throws std::nested_exception When a parsing or conversion error happens.
*
*/
template<typename... Values>
CSVReader &ReadDataLine(Values &&... allValues)
{
this->_lineTracker++;
this->_currentCharIndex = 0;
_currentTotalFields = sizeof...(allValues);
this->_fieldTracker = 0; // We need the tracker at 0 (because of out of range exception)
if(_requireReadLine)
_reader->read_line(this->_currentLine);
ReadNext(std::forward<Values>(allValues)...);
_requireReadLine = true;
return *this;
}
template<typename T>
CSVReader &ReadRawLine(T && value)
{
this->_lineTracker++;
this->_currentCharIndex = 0;
_currentTotalFields = sizeof(value);
this->_fieldTracker = 0; // We need the tracker at 0 (because of out of range exception)
if(_requireReadLine)
_reader->read_line(this->_currentLine);
value = this->_currentLine;
_requireReadLine = true;
return *this;
}
/*!
* \brief Read the next data line and calls iteration function with passing every field.
*
* \throws std::nested_exception When a parsing or conversion error happens.
*/
template<class IteratorFunc>
CSVReader& IterateDataLine(const IteratorFunc &iteratorFunc)
{
this->_lineTracker++;
this->_currentCharIndex = 0;
this->_fieldTracker = 0; // We need the tracker at 0 (because of out of range exception)
_currentTotalFields = 0;
if(_requireReadLine)
_reader->read_line(this->_currentLine);
while(this->HasNext())
{
StrT next = this->NextField();
if(!(next == ""))
iteratorFunc(next);
}
_requireReadLine = true;
return *this;
}
// Begins with 1
/*!
* \brief Read out (peeking) a field without going to the next line.
*/
template<typename T>
T ReadField(int fieldNum)
{
if(_requireReadLine)
_reader->read_line(this->_currentLine);
_requireReadLine = false;
this->_currentCharIndex = 0;
StrT field;
for(int i = 1; i < fieldNum; i++)
{
if(this->_currentCharIndex >= StrTUtils::length(this->_currentLine))
throw std::logic_error("Expected " + std::to_string(fieldNum) + " CSV-Fields, got " + std::to_string(i - 1) + " @ line " + std::to_string(this->_lineTracker) + "!");
this->SkipField();
}
field = this->NextField();
T value;
Converter::Convert(&value, field);
return value;
}
};
/*!
* \brief Creates a new CSVReader.
* \see CSVReader
*
* StrT template argument is the string class type.
* StrTUtils is a wrapper for the StrT class providing following static functions:
* static bool find(const StrT& str, CharT sep, size_t& findIndex)
* static size_t length(const target_string& str)
* static target_string substring(const target_string& str, size_t pos, size_t count)
*
* Converter is a wrapper for converting StrT fields to literal types:
* template<typename T>
* static void Convert(T* out, const StrType& field)
*/
template<class StrT, class StrTUtils, class Converter, class Reader, class CharT>
constexpr CSVReader<Reader, StrT, CharT, StrTUtils, Converter> MakeCSVReader(Reader *reader, CharT /*sep*/)
{
return CSVReader<Reader, StrT, CharT, StrTUtils, Converter>(reader);
}
namespace detail
{
template<class Reader>
struct CSVReaderFromReaderType
{
typedef typename Reader::string_type string_type;
typedef typename string_type::value_type value_type;
typedef typename string_type::traits_type traits_type;
typedef typename string_type::allocator_type allocator_type;
typedef CSVReader<Reader, string_type, value_type,
DefaultStringWrapper<value_type, traits_type, allocator_type>, DefaultCSVConverter<string_type>> full_type;
};
}
/*!
* \brief Creates a new CSVReader for STL strings. (std::string, std::wstring, ...)
* \see CSVReader
*/
template<class Reader, class CharT>
constexpr typename detail::CSVReaderFromReaderType<Reader>::full_type MakeCSVReaderFromBasicString(Reader *reader, CharT sep)
{
typedef detail::CSVReaderFromReaderType<Reader> csv_reader_type;
typedef typename csv_reader_type::value_type value_type;
static_assert(std::is_same<CharT, value_type>::value, "Value type of basic_string must be the same as the type of the seperator!");
return typename csv_reader_type::full_type(reader, sep);
}
template<class Reader, class StrT, class CharT, class StrTUtils, class Converter, class... Values>
struct CSVSubReader
{
public:
CSVSubReader(CharT sep, bool isOptional, Values &&... allValues) : _sep(sep), _val(allValues...), _isOptional(isOptional)
{}
void ReadDataLine(const StrT &val)
{
ReadDataLineImpl(val, detail::make_index_sequence<sizeof...(Values)> {});
}
bool IsOptional() const
{
return _isOptional;
}
private:
template<std::size_t ...I>
void ReadDataLineImpl(const StrT &val, detail::index_sequence<I...>)
{
DirectReader<StrT> subReader(val);
CSVReader<decltype(subReader), StrT, CharT, StrTUtils, Converter> subCSVReader(&subReader, _sep);
subCSVReader.ReadDataLine(std::get<I>(_val)...);
}
CharT _sep;
std::tuple<Values...> _val;
bool _isOptional;
};
/*!
* \brief Creates a new CSVSubReader.
* \see CSVSubReader
*/
template<class Reader, class StrT, class CharT, class StrTUtils, class Converter, class SubChar, class... RestValues>
constexpr CSVSubReader<Reader, StrT, CharT, StrTUtils, Converter, RestValues...>
MakeCSVSubReader(const CSVReader<Reader, StrT, CharT, StrTUtils, Converter> &, SubChar sep, RestValues &&... values)
{
return CSVSubReader<Reader, StrT, CharT, StrTUtils, Converter, RestValues...>(sep, false, std::forward<RestValues>(values)...);
}
/*!
* \brief Creates a new CSVSubReader, which is optional. It acts the same as CSVOptional
* \see CSVSubReader
* \see CSVOptional
*/
template<class Reader, class StrT, class CharT, class StrTUtils, class Converter, class SubChar, class... RestValues>
constexpr CSVSubReader<Reader, StrT, CharT, StrTUtils, Converter, RestValues...>
MakeCSVOptionalSubReader(const CSVReader<Reader, StrT, CharT, StrTUtils, Converter> &, SubChar sep, RestValues &&... values)
{
return CSVSubReader<Reader, StrT, CharT, StrTUtils, Converter, RestValues...>(sep, true, std::forward<RestValues>(values)...);
}