aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
CSVParser_inl.h
Go to the documentation of this file.
1
20#ifndef DOXYGEN_SHOULD_SKIP_THIS
21
23
24namespace gum {
25
26 namespace learning {
27
28 // gets the next line of the csv stream and parses it
29 INLINE bool CSVParser::next() {
30 while (getline(*_instream_, _line_)) {
31 _nbLine_++;
32
33 if (_line_.size() == std::size_t(0)) continue;
34
35 // fast recognition of commented or empty lines lines
36 std::size_t lastPos = _line_.find_first_not_of(_spaces_, std::size_t(0));
37
38 if (lastPos == std::string::npos) continue;
39
40 if (_line_.at(lastPos) == _commentMarker_) continue;
41
42 _tokenize_(_line_);
43 return true;
44 }
45
46 return false;
47 }
48
49
50 // search for quote taking into account the '\'...
51 INLINE std::size_t CSVParser::_correspondingQuoteMarker_(const std::string& str,
52 std::size_t pos) const {
53 std::size_t res = pos, before;
54
55 while (true) {
56 res = str.find_first_of(_quoteMarker_, res + 1);
57
58 if (res == std::string::npos) return res; // no quote found
59
60 before = str.find_last_not_of('\\', res - 1);
61
62 if (before == std::string::npos) return res; // quote found, it is the good one
63
64 if ((res - before) % 2 == 1)
65 return res; // the quote is the good one, even if there are some '\'
66 // before
67 }
68 }
69
70
71 // returns the current parsed line
72 INLINE const std::vector< std::string >& CSVParser::current() const {
73 if (_emptyData_) GUM_ERROR(NullElement, "No parsed data")
74
75 return _data_;
76 }
77
78
79 // returns the current nbLine of parser line
80 INLINE std::size_t CSVParser::nbLine() const {
81 if (_nbLine_ == 0) GUM_ERROR(NullElement, "No parsed data")
82
83 return _nbLine_;
84 }
85
86 } /* namespace learning */
87
88} /* namespace gum */
89
90#endif /* DOXYGEN_SHOULD_SKIP_THIS */
Class for fast parsing of CSV file (never more than one line in application memory).
CSVParser(std::istream &in, const std::string &filename, const std::string &delimiter=",", const char commentmarker='#', const char quoteMarker='"')
default constructor
bool next()
gets the next line of the csv stream and parses it
std::size_t nbLine() const
returns the current line number within the stream
const std::vector< std::string > & current() const
returns the current parsed line
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46