aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DBInitializerFromCSV.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48
50
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
54# ifdef GUM_NO_INLINE
56# endif /* GUM_NO_INLINE */
57
58namespace gum {
59
60 namespace learning {
61
63 DBInitializerFromCSV::DBInitializerFromCSV(const std::string filename,
64 bool fileContainsNames,
65 const std::string delimiter,
66 const char commentmarker,
67 const char quoteMarker) :
68 IDBInitializer(IDBInitializer::InputType::STRING), _filename_(filename),
69 _delimiter_(delimiter), _comment_marker_(commentmarker), _quote_marker_(quoteMarker),
70 _first_row_has_names_(fileContainsNames), _input_stream_(_filename_, std::ifstream::in),
71 _parser_(_input_stream_, filename, delimiter, commentmarker, quoteMarker) {
72 // check that the input file was opened correctly
73 if ((_input_stream_.rdstate() & std::ifstream::failbit) != 0) {
74 GUM_ERROR(IOError, "File " << filename << " not found")
75 }
76
77 // if the first line contains names, store them into the intializer
78 if (fileContainsNames) {
79 _parser_.next();
80 _var_names_ = _parser_.current();
81 }
82
83 GUM_CONSTRUCTOR(DBInitializerFromCSV);
84 }
85
87 DBInitializerFromCSV::DBInitializerFromCSV(const DBInitializerFromCSV& from) :
88 DBInitializerFromCSV(from._filename_,
89 from._first_row_has_names_,
90 from._delimiter_,
91 from._quote_marker_,
92 from._comment_marker_) {}
93
95 DBInitializerFromCSV::DBInitializerFromCSV(DBInitializerFromCSV&& from) :
96 DBInitializerFromCSV(from._filename_,
97 from._first_row_has_names_,
98 from._delimiter_,
99 from._quote_marker_,
100 from._comment_marker_) {}
101
103 DBInitializerFromCSV* DBInitializerFromCSV::clone() const {
104 return new DBInitializerFromCSV(*this);
105 }
106
108 DBInitializerFromCSV::~DBInitializerFromCSV() { GUM_DESTRUCTOR(DBInitializerFromCSV); }
109
111 DBInitializerFromCSV& DBInitializerFromCSV::operator=(const DBInitializerFromCSV& from) {
112 if (this != &from) {
113 IDBInitializer::operator=(from);
114 _filename_ = from._filename_;
115 _delimiter_ = from._delimiter_;
116 _comment_marker_ = from._comment_marker_;
117 _quote_marker_ = from._quote_marker_;
118 _first_row_has_names_ = from._first_row_has_names_;
119
120 // open the CSV file
121 _input_stream_.close();
122 _input_stream_.open(_filename_, std::ifstream::in);
123
124 // check that the input file was opened correctly
125 if ((_input_stream_.rdstate() & std::ifstream::failbit) != 0) {
126 GUM_ERROR(IOError, "File " << _filename_ << " not found")
127 }
128
129 // make the parser use the new input stream
130 _parser_.useNewStream(_input_stream_, _delimiter_, _comment_marker_, _quote_marker_);
131
132 // if the first line contains names, store them into the intializer
133 if (_first_row_has_names_) {
134 _parser_.next();
135 _var_names_ = _parser_.current();
136 }
137 }
138
139 return *this;
140 }
141
143 DBInitializerFromCSV& DBInitializerFromCSV::operator=(DBInitializerFromCSV&& from) {
144 return operator=(from);
145 }
146
147 } /* namespace learning */
148
149} /* namespace gum */
150
151#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The class for initializing DatabaseTable and RawDatabaseTable instances from CSV files.
The class for initializing DatabaseTables and RawDatabaseTables from CSV files.
Exception : input/output problem.
DBInitializerFromCSV(const std::string filename, bool fileContainsNames=true, const std::string delimiter=",", const char commentmarker='#', const char quoteMarker='"')
default constructor
The base class for initializing DatabaseTable and RawDatabaseTable instances from CSV files or SQL da...
#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