aGrUM  0.14.2
DBInitializerFromCSV.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  ***************************************************************************/
26 #ifndef GUM_LEARNING_DB_INITILIALIZER_FROM_CSV_H
27 #define GUM_LEARNING_DB_INITILIALIZER_FROM_CSV_H
28 
29 #include <vector>
30 #include <string>
31 #include <fstream>
32 #include <iostream>
33 #include <sstream>
34 
35 #include <agrum/agrum.h>
38 
39 namespace gum {
40 
41  namespace learning {
42 
128  template < template < typename > class ALLOC = std::allocator >
129  class DBInitializerFromCSV : public IDBInitializer< ALLOC > {
130  public:
132  using allocator_type = ALLOC< std::string >;
133 
134  // ##########################################################################
136  // ##########################################################################
138 
140 
150  DBInitializerFromCSV(const std::string filename,
151  bool fileContainsNames = true,
152  const std::string delimiter = ",",
153  const char commentmarker = '#',
154  const char quoteMarker = '"',
155  const allocator_type& alloc = allocator_type());
156 
158 
161 
163 
166  const allocator_type& alloc);
167 
170 
173  const allocator_type& alloc);
174 
176  virtual DBInitializerFromCSV< ALLOC >* clone() const;
177 
180  clone(const allocator_type& alloc) const;
181 
183  virtual ~DBInitializerFromCSV();
184 
186 
187 
188  // ##########################################################################
190  // ##########################################################################
191 
193 
195 
199 
201 
205 
207 
208 
209  protected:
211  virtual std::vector< std::string, ALLOC< std::string > >
212  _variableNames() final;
213 
215  virtual const std::vector< std::string, ALLOC< std::string > >&
216  _currentStringRow() final;
217 
219  virtual bool _nextRow() final;
220 
221 
222 #ifndef DOXYGEN_SHOULD_SKIP_THIS
223 
224  private:
225  // the filename used for parsing
226  std::string __filename;
227 
228  // indicates the delimiter used within the CSV
229  std::string __delimiter;
230 
231  // indicates which character is a comment symbol in the CSV
232  char __comment_marker;
233 
234  // indicates which character is a quote symbol in the CSV
235  char __quote_marker;
236 
237  // indicates whether the first row of the file contains the names
238  bool __first_row_has_names;
239 
240  // the input stream read by the parser
241  std::ifstream __input_stream;
242 
243  // the CSV parser used for the reading the CSV file
244  CSVParser< ALLOC > __parser;
245 
246  // the variables names, if the first row has names
247  std::vector< std::string, ALLOC< std::string > > __var_names;
248 
249 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
250  };
251 
252  } /* namespace learning */
253 
254 } /* namespace gum */
255 
256 // always include the template implementation
258 
259 
260 #endif /* GUM_LEARNING_DB_INITILIALIZER_FROM_CSV_H */
virtual std::vector< std::string, ALLOC< std::string > > _variableNames() final
returns the names of the variables
virtual ~DBInitializerFromCSV()
destructor
DBInitializerFromCSV(const std::string filename, bool fileContainsNames=true, const std::string delimiter=",", const char commentmarker='#', const char quoteMarker='"', const allocator_type& alloc = allocator_type())
default constructor
The base class for initializing DatabaseTable and RawDatabaseTable instances from CSV files or SQL da...
Class for fast parsing of CSV file (never more than one line in application memory) ...
Definition: CSVParser.h:78
DBInitializerFromCSV< ALLOC > & operator=(const DBInitializerFromCSV< ALLOC > &from)
copy operator
STL namespace.
virtual DBInitializerFromCSV< ALLOC > * clone() const
virtual copy constructor
virtual bool _nextRow() final
indicates whether there is a next row to read (and point on it)
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
The class for initializing DatabaseTables and RawDatabaseTables from CSV files.
virtual const std::vector< std::string, ALLOC< std::string > > & _currentStringRow() final
returns the content of the current row using strings
The class for initializing DatabaseTable and RawDatabaseTable instances from CSV files.
ALLOC< std::string > allocator_type
type for the allocators passed in arguments of methods
The base class for initializing DatabaseTable and RawDatabaseTable instances from CSV files or SQL da...
Class for fast parsing of CSV file (never more than one line in application memory) ...