aGrUM  0.14.2
DBInitializerFromCSV_tpl.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 DOXYGEN_SHOULD_SKIP_THIS
27 
28 namespace gum {
29 
30  namespace learning {
31 
32 
34  template < template < typename > class ALLOC >
36  const std::string filename,
37  bool fileContainsNames,
38  const std::string delimiter,
39  const char commentmarker,
40  const char quoteMarker,
41  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) :
42  IDBInitializer< ALLOC >(IDBInitializer< ALLOC >::InputType::STRING, alloc),
43  __filename(filename), __delimiter(delimiter),
44  __comment_marker(commentmarker), __quote_marker(quoteMarker),
45  __first_row_has_names(fileContainsNames),
46  __input_stream(__filename, std::ifstream::in),
47  __parser(__input_stream, delimiter, commentmarker, quoteMarker),
48  __var_names(alloc) {
49  // check that the input file was opened correctly
50  if ((__input_stream.rdstate() & std::ifstream::failbit) != 0) {
51  GUM_ERROR(IOError, "File " << filename << " not found");
52  }
53 
54  // if the first line contains names, store them into the intializer
55  if (fileContainsNames) {
56  __parser.next();
57  __var_names = __parser.current();
58  }
59 
60  GUM_CONSTRUCTOR(DBInitializerFromCSV);
61  }
62 
63 
65  template < template < typename > class ALLOC >
67  const DBInitializerFromCSV< ALLOC >& from,
68  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) :
69  DBInitializerFromCSV< ALLOC >(from.__filename,
70  from.__first_row_has_names,
71  from.__delimiter,
72  from.__quote_marker,
73  from.__comment_marker,
74  alloc) {}
75 
77  template < template < typename > class ALLOC >
79  const DBInitializerFromCSV< ALLOC >& from) :
80  DBInitializerFromCSV< ALLOC >(from, from.getAllocator()) {}
81 
83  template < template < typename > class ALLOC >
85  DBInitializerFromCSV< ALLOC >&& from,
86  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) :
87  DBInitializerFromCSV< ALLOC >(from.__filename,
88  from.__first_row_has_names,
89  from.__delimiter,
90  from.__quote_marker,
91  from.__comment_marker,
92  alloc) {}
93 
94 
96  template < template < typename > class ALLOC >
98  DBInitializerFromCSV< ALLOC >&& from) :
99  DBInitializerFromCSV< ALLOC >(std::move(from), from.getAllocator()) {}
100 
101 
103  template < template < typename > class ALLOC >
104  DBInitializerFromCSV< ALLOC >* DBInitializerFromCSV< ALLOC >::clone(
105  const typename DBInitializerFromCSV< ALLOC >::allocator_type& alloc) const {
106  ALLOC< DBInitializerFromCSV< ALLOC > > allocator(alloc);
107  DBInitializerFromCSV< ALLOC >* new_initializer = allocator.allocate(1);
108  try {
109  allocator.construct(new_initializer, *this, alloc);
110  } catch (...) {
111  allocator.deallocate(new_initializer, 1);
112  throw;
113  }
114 
115  return new_initializer;
116  }
117 
118 
120  template < template < typename > class ALLOC >
121  DBInitializerFromCSV< ALLOC >* DBInitializerFromCSV< ALLOC >::clone() const {
122  return clone(this->getAllocator());
123  }
124 
125 
127  template < template < typename > class ALLOC >
129  GUM_DESTRUCTOR(DBInitializerFromCSV);
130  }
131 
132 
134  template < template < typename > class ALLOC >
135  DBInitializerFromCSV< ALLOC >& DBInitializerFromCSV< ALLOC >::
136  operator=(const DBInitializerFromCSV< ALLOC >& from) {
137  if (this != &from) {
139  __filename = from.__filename;
140  __delimiter = from.__delimiter;
141  __comment_marker = from.__comment_marker;
142  __quote_marker = from.__quote_marker;
143  __first_row_has_names = from.__first_row_has_names;
144 
145  // open the CSV file
146  __input_stream.close();
147  __input_stream.open(__filename, std::ifstream::in);
148 
149  // check that the input file was opened correctly
150  if ((__input_stream.rdstate() & std::ifstream::failbit) != 0) {
151  GUM_ERROR(IOError, "File " << __filename << " not found");
152  }
153 
154  // make the parser use the new input stream
155  __parser.useNewStream(
156  __input_stream, __delimiter, __comment_marker, __quote_marker);
157 
158  // if the first line contains names, store them into the intializer
159  if (__first_row_has_names) {
160  __parser.next();
161  __var_names = __parser.current();
162  }
163  }
164 
165  return *this;
166  }
167 
168 
170  template < template < typename > class ALLOC >
171  DBInitializerFromCSV< ALLOC >& DBInitializerFromCSV< ALLOC >::
172  operator=(DBInitializerFromCSV< ALLOC >&& from) {
173  return operator=(from);
174  }
175 
176 
177  // ask the child class for the names of the variables
178  template < template < typename > class ALLOC >
179  INLINE std::vector< std::string, ALLOC< std::string > >
181  return __var_names;
182  }
183 
184 
185  // asks the child class for the content of the current row using strings
186  template < template < typename > class ALLOC >
187  INLINE const std::vector< std::string, ALLOC< std::string > >&
189  return __parser.current();
190  }
191 
192 
193  // indicates whether there is a next row to read (and point on it)
194  template < template < typename > class ALLOC >
196  return __parser.next();
197  }
198 
199  } /* namespace learning */
200 
201 } /* namespace gum */
202 
203 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
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
DBInitializerFromCSV< ALLOC > & operator=(const DBInitializerFromCSV< ALLOC > &from)
copy operator
STL namespace.
allocator_type getAllocator() const
returns the allocator used
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
IDBInitializer< ALLOC > & operator=(const IDBInitializer< ALLOC > &from)
copy operator
virtual const std::vector< std::string, ALLOC< std::string > > & _currentStringRow() final
returns the content of the current row using strings
ALLOC< std::string > allocator_type
type for the allocators passed in arguments of methods
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52