aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
CSVParser.h
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (c) 2005-2024 by Christophe GONZALES(_at_AMU) and Pierre-Henri WUILLEMIN(_at_LIP6) *
3 * info_at_agrum_dot_org *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
42
43#ifndef GUM_CSV_PARSER_H
44#define GUM_CSV_PARSER_H
45
46#include <istream>
47#include <string>
48#include <vector>
49
50#include <agrum/agrum.h>
51
52namespace gum {
53
54 namespace learning {
55
77 class CSVParser final {
78 public:
79 // ##########################################################################
81 // ##########################################################################
83
85
92 CSVParser(std::istream& in,
93 const std::string& filename,
94 const std::string& delimiter = ",",
95 const char commentmarker = '#',
96 const char quoteMarker = '"');
97
99 virtual ~CSVParser();
100
102
103
104 // ########################################################################
106 // ########################################################################
108
110
112 bool next();
113
115
117 const std::vector< std::string >& current() const;
118
120 std::size_t nbLine() const;
121
123 void useNewStream(std::istream& in,
124 const std::string& delimiter = ",",
125 const char commentmarker = '#',
126 const char quoteMarker = '"');
127
129
130
131#ifndef DOXYGEN_SHOULD_SKIP_THIS
132
133 private:
134 void _getNextTriplet_(const std::string& str,
135 std::size_t& first_letter_token,
136 std::size_t& next_token,
137 std::size_t& last_letter_token,
138 std::size_t from) const;
139
140 void _tokenize_(const std::string& str);
141
142 std::size_t _correspondingQuoteMarker_(const std::string& str, std::size_t pos) const;
143
144
145 std::string _line_;
146 std::string _delimiter_;
147 std::string _spaces_;
148 std::string _delimiterPlusSpaces_;
149 std::size_t _nbLine_;
150 char _commentMarker_;
151 char _quoteMarker_;
152 bool _emptyData_;
153
154 std::istream* _instream_;
155 std::vector< std::string > _data_;
156 const std::string _filename_;
157
158#endif /* DOXYGEN_SHOULD_SKIP_THIS */
159 };
160
161 } // namespace learning
162
163} // namespace gum
164
166#ifndef GUM_NO_INLINE
168#endif /* GUM_NO_INLINE */
169
170#endif // GUM_CSV_PARSER_H
CSVParser(std::istream &in, const std::string &filename, const std::string &delimiter=",", const char commentmarker='#', const char quoteMarker='"')
default constructor
void useNewStream(std::istream &in, const std::string &delimiter=",", const char commentmarker='#', const char quoteMarker='"')
reopens a new input stream to parse
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
virtual ~CSVParser()
destructor
const std::vector< std::string > & current() const
returns the current parsed line
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46