Documentation for OpenFLUID 2.2.1
ProgressiveChronFileReader.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2021-2026, INRAE
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 /**
34  @file ProgressiveChronFileReader.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37  @author Armel THÖNI <armel.thoni@inrae.fr>
38  */
39 
40 
41 #ifndef __OPENFLUID_TOOLS_PROGRESSIVECHRONFILEREADER_HPP__
42 #define __OPENFLUID_TOOLS_PROGRESSIVECHRONFILEREADER_HPP__
43 
44 
48 #include <openfluid/dllexport.hpp>
49 
50 
51 namespace openfluid { namespace tools {
52 
53 
54 template<class DataType>
56 {
57  private:
58 
59  std::string m_DateFormat;
60 
61 
62  public:
63 
64  ProgressiveChronFileReader(const std::string& FileName,
65  const std::string& DateFormat = "%Y-%m-%dT%H:%M:%S",
66  const std::string& ColSeparators = " \t\r\n") :
67  ProgressiveColumnFileReader(FileName,ColSeparators), m_DateFormat(DateFormat)
68  {
69  }
70 
72  {
73  }
74 
75 
77 
78 };
79 
80 
81 // =====================================================================
82 // =====================================================================
83 
84 
85 template<>
87 {
88  std::vector<std::string> Values;
90  double Val;
91 
92  while (getNextLine(Values))
93  {
94  if (Values.size() == 2)
95  {
96  if (DT.setFromString(Values.front(),m_DateFormat) &&
97  openfluid::tools::toNumeric(Values.back(),Val))
98  {
99  Value.first = DT;
100  Value.second = Val;
101  return true;
102  }
103  else
104  {
105  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
106  "wrong data in " + m_FileName);
107  }
108  }
109  }
110 
111  return false;
112 }
113 
114 
115 // =====================================================================
116 // =====================================================================
117 
118 
119 template<>
121 {
122  std::vector<std::string> Values;
124  int Val;
125 
126  while (getNextLine(Values))
127  {
128  if (Values.size() == 2)
129  {
130  if (DT.setFromString(Values.front(),m_DateFormat) &&
131  openfluid::tools::toNumeric(Values.back(),Val))
132  {
133  Value.first = DT;
134  Value.second = Val;
135  return true;
136  }
137  else
138  {
139  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
140  "wrong data in " + m_FileName);
141  }
142  }
143  }
144 
145  return false;
146 }
147 
148 
149 // =====================================================================
150 // =====================================================================
151 
152 
153 template<>
155 {
156  std::vector<std::string> Values;
158 
159  while (getNextLine(Values))
160  {
161  if (Values.size() == 2)
162  {
163  if (DT.setFromString(Values.front(),m_DateFormat))
164  {
165  Value.first = DT;
166  Value.second = openfluid::core::stringToBoolean(Values.back());
167  return true;
168  }
169  else
170  {
171  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
172  "wrong data in " + m_FileName);
173  }
174  }
175  }
176 
177  return false;
178 }
179 
180 
181 // =====================================================================
182 // =====================================================================
183 
184 
185 template<>
187 {
188  std::vector<std::string> Values;
190 
191  while (getNextLine(Values))
192  {
193  if (Values.size() == 1 || Values.size() == 2)
194  {
195  if (DT.setFromString(Values.front(), m_DateFormat))
196  {
197  Value.first = DT;
198  Value.second = (Values.size() == 2) ? Values.back() : "";
199  return true;
200  }
201  }
202  else // FIXME warning: not same throw logic than line before, not applied to same if
203  {
204  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
205  "wrong data in " + m_FileName);
206  }
207 
208  }
209 
210  return false;
211 }
212 
213 
214 // =====================================================================
215 // =====================================================================
216 
217 
218 template<>
220  ChronItem_t<std::vector<std::string>>& Value)
221 {
222  std::vector<std::string> Values;
224 
225  while (getNextLine(Values))
226  {
227  if (Values.size() >= 2)
228  {
229  if (DT.setFromString(Values.front(),m_DateFormat) )
230  {
231  Value.first = DT;
232  Value.second.clear();
233  for (std::size_t i=1;i<Values.size();i++)
234  {
235  Value.second.push_back(Values[i]);
236  }
237  return true;
238  }
239  else
240  {
241  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
242  "wrong data in " + m_FileName);
243  }
244  }
245  }
246 
247  return false;
248 }
249 
250 
251 } } // namespaces
252 
253 
254 #endif /* __OPENFLUID_TOOLS_PROGRESSIVECHRONFILEREADER_HPP__ */
Definition: FrameworkException.hpp:51
Class for management of date and time information.
Definition: DateTime.hpp:88
bool setFromString(const std::string &DateTimeStr, const std::string &FormatStr)
Definition: ProgressiveChronFileReader.hpp:56
virtual ~ProgressiveChronFileReader()
Definition: ProgressiveChronFileReader.hpp:71
ProgressiveChronFileReader(const std::string &FileName, const std::string &DateFormat="%Y-%m-%dT%H:%M:%S", const std::string &ColSeparators=" \t\r\n")
Definition: ProgressiveChronFileReader.hpp:64
bool getNextValue(ChronItem_t< DataType > &Value)
Definition: ProgressiveColumnFileReader.hpp:58
#define OPENFLUID_API
Definition: dllexport.hpp:86
bool OPENFLUID_API stringToBoolean(const std::string &Str)
bool toNumeric(const std::string &Str, T &Num)
Definition: StringHelpers.hpp:114
std::pair< openfluid::core::DateTime, DataType > ChronItem_t
Definition: ChronologicalSerie.hpp:55
Definition: ApplicationException.hpp:47