aGrUM  0.16.0
CSVParser.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
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  ***************************************************************************/
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 
52 namespace gum {
53 
54  namespace learning {
55 
77  template <template<typename> class ALLOC = std::allocator>
78  class CSVParser {
79  public:
80 
82  using allocator_type = ALLOC<std::string>;
83 
84 
85  // ##########################################################################
87  // ##########################################################################
89 
91 
99  CSVParser( std::istream& in,
100  const std::string& delimiter = ",",
101  const char commentmarker = '#',
102  const char quoteMarker = '"',
103  const allocator_type& alloc = allocator_type () );
104 
106  virtual ~CSVParser();
107 
109 
110 
111  // ########################################################################
113  // ########################################################################
115 
117 
119  bool next();
120 
122 
124  const std::vector<std::string,ALLOC<std::string>>& current() const;
125 
127  const std::size_t nbLine() const;
128 
130  void useNewStream ( std::istream& in,
131  const std::string& delimiter = ",",
132  const char commentmarker = '#',
133  const char quoteMarker = '"' );
134 
136 
137 
138 #ifndef DOXYGEN_SHOULD_SKIP_THIS
139 
140  private:
141  void __getNextTriplet( const std::string& str,
142  std::size_t& first_letter_token,
143  std::size_t& next_token,
144  std::size_t& last_letter_token,
145  std::size_t from ) const;
146 
147  void __tokenize( const std::string& str );
148 
149  std::size_t __correspondingQuoteMarker( const std::string& str,
150  std::size_t pos ) const;
151 
152 
153  std::string __line;
154  std::string __delimiter;
155  std::string __spaces;
156  std::string __delimiterPlusSpaces;
157  std::size_t __nbLine;
158  char __commentMarker;
159  char __quoteMarker;
160  bool __emptyData;
161 
162  std::istream* __instream;
163  std::vector<std::string,ALLOC<std::string>> __data;
164 
165 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
166 
167  };
168 
169  } // namespace learning
170 
171 } // namespace gum
172 
174 
175 #endif // GUM_CSV_PARSER_H
Class for fast parsing of CSV file (never more than one line in application memory) ...
Definition: CSVParser.h:78
Class for fast parsing of CSV file (never more than one line in application memory) ...
void useNewStream(std::istream &in, const std::string &delimiter=",", const char commentmarker='#', const char quoteMarker='"' )
reopens a new input stream to parse
const std::size_t nbLine() const
returns the current line number within the stream
ALLOC< std::string > allocator_type
type for the allocators passed in arguments of methods
Definition: CSVParser.h:82
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
CSVParser(std::istream &in, const std::string &delimiter=",", const char commentmarker='#', const char quoteMarker='"', const allocator_type& alloc = allocator_type () )
default constructor
bool next()
gets the next line of the csv stream and parses it
virtual ~CSVParser()
destructor
const std::vector< std::string, ALLOC< std::string > > & current() const
returns the current parsed line